api.c 1.5 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2021 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #ifndef FOURK_BUF
  33. #define FOURK_BUF 4096
  34. #endif
  35. #ifndef TWOK_BUF
  36. #define TWOK_BUF 2048
  37. #endif
  38. #ifndef ONEK_BUF
  39. #define ONEK_BUF 1024
  40. #endif
  41. #if defined(WOLFSSL_STATIC_MEMORY)
  42. #include <wolfssl/wolfcrypt/memory.h>
  43. #endif /* WOLFSSL_STATIC_MEMORY */
  44. #ifndef HEAP_HINT
  45. #define HEAP_HINT NULL
  46. #endif /* WOLFSSL_STAIC_MEMORY */
  47. #ifdef WOLFSSL_ASNC_CRYPT
  48. #include <wolfssl/wolfcrypt/async.h>
  49. #endif
  50. #ifdef HAVE_ECC
  51. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  52. #ifndef ECC_ASN963_MAX_BUF_SZ
  53. #define ECC_ASN963_MAX_BUF_SZ 133
  54. #endif
  55. #ifndef ECC_PRIV_KEY_BUF
  56. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  57. #endif
  58. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  59. /* logic to choose right key ECC size */
  60. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  61. #define KEY14 14
  62. #else
  63. #define KEY14 32
  64. #endif
  65. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  66. #define KEY16 16
  67. #else
  68. #define KEY16 32
  69. #endif
  70. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  71. #define KEY20 20
  72. #else
  73. #define KEY20 32
  74. #endif
  75. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  76. #define KEY24 24
  77. #else
  78. #define KEY24 32
  79. #endif
  80. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  81. #define KEY28 28
  82. #else
  83. #define KEY28 32
  84. #endif
  85. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  86. #define KEY30 30
  87. #else
  88. #define KEY30 32
  89. #endif
  90. #define KEY32 32
  91. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  92. #define KEY40 40
  93. #else
  94. #define KEY40 32
  95. #endif
  96. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  97. #define KEY48 48
  98. #else
  99. #define KEY48 32
  100. #endif
  101. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  102. #define KEY64 64
  103. #else
  104. #define KEY64 32
  105. #endif
  106. #if !defined(HAVE_COMP_KEY)
  107. #if !defined(NOCOMP)
  108. #define NOCOMP 0
  109. #endif
  110. #else
  111. #if !defined(COMP)
  112. #define COMP 1
  113. #endif
  114. #endif
  115. #if !defined(DER_SZ)
  116. #define DER_SZ(ks) (ks * 2 + 1)
  117. #endif
  118. #endif
  119. #ifndef NO_ASN
  120. #include <wolfssl/wolfcrypt/asn_public.h>
  121. #endif
  122. #include <wolfssl/error-ssl.h>
  123. #include <stdlib.h>
  124. #include <wolfssl/ssl.h> /* compatibility layer */
  125. #include <wolfssl/test.h>
  126. #include <tests/unit.h>
  127. #include "examples/server/server.h"
  128. /* for testing compatibility layer callbacks */
  129. #ifndef NO_MD5
  130. #include <wolfssl/wolfcrypt/md5.h>
  131. #endif
  132. #ifndef NO_SHA
  133. #include <wolfssl/wolfcrypt/sha.h>
  134. #endif
  135. #ifndef NO_SHA256
  136. #include <wolfssl/wolfcrypt/sha256.h>
  137. #endif
  138. #ifdef WOLFSSL_SHA512
  139. #include <wolfssl/wolfcrypt/sha512.h>
  140. #endif
  141. #ifdef WOLFSSL_SHA384
  142. #include <wolfssl/wolfcrypt/sha512.h>
  143. #endif
  144. #ifdef WOLFSSL_SHA3
  145. #include <wolfssl/wolfcrypt/sha3.h>
  146. #ifndef HEAP_HINT
  147. #define HEAP_HINT NULL
  148. #endif
  149. #endif
  150. #ifndef NO_AES
  151. #include <wolfssl/wolfcrypt/aes.h>
  152. #ifdef HAVE_AES_DECRYPT
  153. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  154. #endif
  155. #endif
  156. #ifdef WOLFSSL_RIPEMD
  157. #include <wolfssl/wolfcrypt/ripemd.h>
  158. #endif
  159. #ifdef HAVE_IDEA
  160. #include <wolfssl/wolfcrypt/idea.h>
  161. #endif
  162. #ifndef NO_DES3
  163. #include <wolfssl/wolfcrypt/des3.h>
  164. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  165. #endif
  166. #ifdef WC_RC2
  167. #include <wolfssl/wolfcrypt/rc2.h>
  168. #endif
  169. #ifndef NO_HMAC
  170. #include <wolfssl/wolfcrypt/hmac.h>
  171. #endif
  172. #ifdef HAVE_CHACHA
  173. #include <wolfssl/wolfcrypt/chacha.h>
  174. #endif
  175. #ifdef HAVE_POLY1305
  176. #include <wolfssl/wolfcrypt/poly1305.h>
  177. #endif
  178. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  179. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  180. #endif
  181. #ifdef HAVE_CAMELLIA
  182. #include <wolfssl/wolfcrypt/camellia.h>
  183. #endif
  184. #ifndef NO_RABBIT
  185. #include <wolfssl/wolfcrypt/rabbit.h>
  186. #endif
  187. #ifndef NO_RC4
  188. #include <wolfssl/wolfcrypt/arc4.h>
  189. #endif
  190. #ifdef HAVE_BLAKE2
  191. #include <wolfssl/wolfcrypt/blake2.h>
  192. #endif
  193. #include <wolfssl/wolfcrypt/hash.h>
  194. #ifndef NO_RSA
  195. #include <wolfssl/wolfcrypt/rsa.h>
  196. #define FOURK_BUF 4096
  197. #define GEN_BUF 294
  198. #ifndef USER_CRYPTO_ERROR
  199. #define USER_CRYPTO_ERROR -101 /* error returned by IPP lib. */
  200. #endif
  201. #endif
  202. #ifndef NO_SIG_WRAPPER
  203. #include <wolfssl/wolfcrypt/signature.h>
  204. #endif
  205. #ifdef HAVE_AESCCM
  206. #include <wolfssl/wolfcrypt/aes.h>
  207. #endif
  208. #ifdef HAVE_HC128
  209. #include <wolfssl/wolfcrypt/hc128.h>
  210. #endif
  211. #ifdef HAVE_PKCS7
  212. #include <wolfssl/wolfcrypt/pkcs7.h>
  213. #include <wolfssl/wolfcrypt/asn.h>
  214. #ifdef HAVE_LIBZ
  215. #include <wolfssl/wolfcrypt/compress.h>
  216. #endif
  217. #endif
  218. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  219. #include <wolfssl/wolfcrypt/asn.h>
  220. #endif
  221. #ifndef NO_DSA
  222. #include <wolfssl/wolfcrypt/dsa.h>
  223. #ifndef ONEK_BUF
  224. #define ONEK_BUF 1024
  225. #endif
  226. #ifndef TWOK_BUF
  227. #define TWOK_BUF 2048
  228. #endif
  229. #ifndef FOURK_BUF
  230. #define FOURK_BUF 4096
  231. #endif
  232. #ifndef DSA_SIG_SIZE
  233. #define DSA_SIG_SIZE 40
  234. #endif
  235. #ifndef MAX_DSA_PARAM_SIZE
  236. #define MAX_DSA_PARAM_SIZE 256
  237. #endif
  238. #endif
  239. #ifdef WOLFSSL_CMAC
  240. #include <wolfssl/wolfcrypt/cmac.h>
  241. #endif
  242. #ifdef HAVE_ED25519
  243. #include <wolfssl/wolfcrypt/ed25519.h>
  244. #endif
  245. #ifdef HAVE_CURVE25519
  246. #include <wolfssl/wolfcrypt/curve25519.h>
  247. #endif
  248. #ifdef HAVE_ED448
  249. #include <wolfssl/wolfcrypt/ed448.h>
  250. #endif
  251. #ifdef HAVE_CURVE448
  252. #include <wolfssl/wolfcrypt/curve448.h>
  253. #endif
  254. #ifdef HAVE_PKCS12
  255. #include <wolfssl/wolfcrypt/pkcs12.h>
  256. #endif
  257. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  258. #include <wolfssl/openssl/ssl.h>
  259. #ifndef NO_ASN
  260. /* for ASN_COMMON_NAME DN_tags enum */
  261. #include <wolfssl/wolfcrypt/asn.h>
  262. #endif
  263. #ifdef HAVE_OCSP
  264. #include <wolfssl/openssl/ocsp.h>
  265. #endif
  266. #endif
  267. #ifdef OPENSSL_EXTRA
  268. #include <wolfssl/openssl/cmac.h>
  269. #include <wolfssl/openssl/x509v3.h>
  270. #include <wolfssl/openssl/asn1.h>
  271. #include <wolfssl/openssl/crypto.h>
  272. #include <wolfssl/openssl/pkcs12.h>
  273. #include <wolfssl/openssl/evp.h>
  274. #include <wolfssl/openssl/dh.h>
  275. #include <wolfssl/openssl/bn.h>
  276. #include <wolfssl/openssl/buffer.h>
  277. #include <wolfssl/openssl/pem.h>
  278. #include <wolfssl/openssl/ec.h>
  279. #include <wolfssl/openssl/engine.h>
  280. #include <wolfssl/openssl/crypto.h>
  281. #include <wolfssl/openssl/hmac.h>
  282. #include <wolfssl/openssl/objects.h>
  283. #include <wolfssl/openssl/rand.h>
  284. #ifdef OPENSSL_ALL
  285. #include <wolfssl/openssl/txt_db.h>
  286. #include <wolfssl/openssl/lhash.h>
  287. #endif
  288. #ifndef NO_AES
  289. #include <wolfssl/openssl/aes.h>
  290. #endif
  291. #ifndef NO_DES3
  292. #include <wolfssl/openssl/des.h>
  293. #endif
  294. #ifdef HAVE_ECC
  295. #include <wolfssl/openssl/ecdsa.h>
  296. #endif
  297. #ifdef HAVE_PKCS7
  298. #include <wolfssl/openssl/pkcs7.h>
  299. #endif
  300. #ifdef HAVE_ED25519
  301. #include <wolfssl/openssl/ed25519.h>
  302. #endif
  303. #ifdef HAVE_ED448
  304. #include <wolfssl/openssl/ed448.h>
  305. #endif
  306. #endif /* OPENSSL_EXTRA */
  307. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  308. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  309. #include <wolfssl/wolfcrypt/srp.h>
  310. #endif
  311. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  312. #include "wolfssl/internal.h" /* for testing SSL_get_peer_cert_chain */
  313. #endif
  314. /* force enable test buffers */
  315. #ifndef USE_CERT_BUFFERS_2048
  316. #define USE_CERT_BUFFERS_2048
  317. #endif
  318. #ifndef USE_CERT_BUFFERS_256
  319. #define USE_CERT_BUFFERS_256
  320. #endif
  321. #include <wolfssl/certs_test.h>
  322. typedef struct testVector {
  323. const char* input;
  324. const char* output;
  325. size_t inLen;
  326. size_t outLen;
  327. } testVector;
  328. #if defined(HAVE_PKCS7)
  329. typedef struct {
  330. const byte* content;
  331. word32 contentSz;
  332. int contentOID;
  333. int encryptOID;
  334. int keyWrapOID;
  335. int keyAgreeOID;
  336. byte* cert;
  337. size_t certSz;
  338. byte* privateKey;
  339. word32 privateKeySz;
  340. } pkcs7EnvelopedVector;
  341. #ifndef NO_PKCS7_ENCRYPTED_DATA
  342. typedef struct {
  343. const byte* content;
  344. word32 contentSz;
  345. int contentOID;
  346. int encryptOID;
  347. byte* encryptionKey;
  348. word32 encryptionKeySz;
  349. } pkcs7EncryptedVector;
  350. #endif
  351. #endif /* HAVE_PKCS7 */
  352. /*----------------------------------------------------------------------------*
  353. | Constants
  354. *----------------------------------------------------------------------------*/
  355. #define TEST_SUCCESS (1)
  356. #define TEST_FAIL (0)
  357. #define testingFmt " %s:"
  358. #define resultFmt " %s\n"
  359. static const char* passed = "passed";
  360. static const char* failed = "failed";
  361. #define TEST_STRING "Everyone gets Friday off."
  362. #define TEST_STRING_SZ 25
  363. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  364. #define TEST_RSA_BITS 1024
  365. #else
  366. #define TEST_RSA_BITS 2048
  367. #endif
  368. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  369. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  370. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  371. static const char* bogusFile =
  372. #ifdef _WIN32
  373. "NUL"
  374. #else
  375. "/dev/null"
  376. #endif
  377. ;
  378. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  379. enum {
  380. TESTING_RSA = 1,
  381. TESTING_ECC = 2
  382. };
  383. #ifdef WOLFSSL_QNX_CAAM
  384. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  385. static int devId = WOLFSSL_CAAM_DEVID;
  386. #else
  387. static int devId = INVALID_DEVID;
  388. #endif
  389. /*----------------------------------------------------------------------------*
  390. | Setup
  391. *----------------------------------------------------------------------------*/
  392. static int test_wolfSSL_Init(void)
  393. {
  394. int result;
  395. printf(testingFmt, "wolfSSL_Init()");
  396. result = wolfSSL_Init();
  397. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  398. return result;
  399. }
  400. static int test_wolfSSL_Cleanup(void)
  401. {
  402. int result;
  403. printf(testingFmt, "wolfSSL_Cleanup()");
  404. result = wolfSSL_Cleanup();
  405. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  406. return result;
  407. }
  408. /* Initialize the wolfCrypt state.
  409. * POST: 0 success.
  410. */
  411. static int test_wolfCrypt_Init(void)
  412. {
  413. int result;
  414. printf(testingFmt, "wolfCrypt_Init()");
  415. result = wolfCrypt_Init();
  416. printf(resultFmt, result == 0 ? passed : failed);
  417. return result;
  418. } /* END test_wolfCrypt_Init */
  419. /*----------------------------------------------------------------------------*
  420. | Platform dependent function test
  421. *----------------------------------------------------------------------------*/
  422. static int test_fileAccess(void)
  423. {
  424. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  425. const char *fname[] = {
  426. svrCertFile, svrKeyFile, caCertFile,
  427. eccCertFile, eccKeyFile, eccRsaCertFile,
  428. cliCertFile, cliCertDerFile, cliKeyFile,
  429. ntruCertFile, ntruKeyFile, dhParamFile,
  430. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  431. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  432. NULL
  433. };
  434. const char derfile[] = "./certs/server-cert.der";
  435. XFILE f;
  436. size_t sz;
  437. byte *buff;
  438. int i;
  439. printf(testingFmt, "test_fileAccess()");
  440. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  441. for(i=0; fname[i] != NULL ; i++){
  442. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  443. XFCLOSE(f);
  444. }
  445. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  446. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  447. sz = (size_t) XFTELL(f);
  448. XREWIND(f);
  449. AssertTrue(sz == sizeof_server_cert_der_2048);
  450. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  451. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  452. XMEMCMP(server_cert_der_2048, buff, sz);
  453. printf(resultFmt, passed);
  454. #endif
  455. return WOLFSSL_SUCCESS;
  456. }
  457. /*----------------------------------------------------------------------------*
  458. | Method Allocators
  459. *----------------------------------------------------------------------------*/
  460. static void test_wolfSSL_Method_Allocators(void)
  461. {
  462. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  463. do { \
  464. WOLFSSL_METHOD *method; \
  465. condition(method = allocator()); \
  466. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  467. } while(0)
  468. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  469. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  470. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  471. TEST_METHOD_ALLOCATOR(a, AssertNull)
  472. #ifndef NO_OLD_TLS
  473. #ifdef WOLFSSL_ALLOW_SSLV3
  474. #ifndef NO_WOLFSSL_SERVER
  475. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  476. #endif
  477. #ifndef NO_WOLFSSL_CLIENT
  478. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  479. #endif
  480. #endif
  481. #ifdef WOLFSSL_ALLOW_TLSV10
  482. #ifndef NO_WOLFSSL_SERVER
  483. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  484. #endif
  485. #ifndef NO_WOLFSSL_CLIENT
  486. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  487. #endif
  488. #endif
  489. #ifndef NO_WOLFSSL_SERVER
  490. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  491. #endif
  492. #ifndef NO_WOLFSSL_CLIENT
  493. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  494. #endif
  495. #endif /* !NO_OLD_TLS */
  496. #ifndef WOLFSSL_NO_TLS12
  497. #ifndef NO_WOLFSSL_SERVER
  498. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  499. #endif
  500. #ifndef NO_WOLFSSL_CLIENT
  501. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  502. #endif
  503. #endif /* !WOLFSSL_NO_TLS12 */
  504. #ifdef WOLFSSL_TLS13
  505. #ifndef NO_WOLFSSL_SERVER
  506. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  507. #endif
  508. #ifndef NO_WOLFSSL_CLIENT
  509. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  510. #endif
  511. #endif /* WOLFSSL_TLS13 */
  512. #ifndef NO_WOLFSSL_SERVER
  513. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  514. #endif
  515. #ifndef NO_WOLFSSL_CLIENT
  516. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  517. #endif
  518. #ifdef WOLFSSL_DTLS
  519. #ifndef NO_OLD_TLS
  520. #ifndef NO_WOLFSSL_SERVER
  521. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  522. #endif
  523. #ifndef NO_WOLFSSL_CLIENT
  524. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  525. #endif
  526. #endif
  527. #ifndef WOLFSSL_NO_TLS12
  528. #ifndef NO_WOLFSSL_SERVER
  529. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  530. #endif
  531. #ifndef NO_WOLFSSL_CLIENT
  532. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  533. #endif
  534. #endif
  535. #endif /* WOLFSSL_DTLS */
  536. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  537. /* Stubs */
  538. #ifndef NO_WOLFSSL_SERVER
  539. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  540. #endif
  541. #ifndef NO_WOLFSSL_CLIENT
  542. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  543. #endif
  544. #endif
  545. /* Test Either Method (client or server) */
  546. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  547. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  548. #ifndef NO_OLD_TLS
  549. #ifdef WOLFSSL_ALLOW_TLSV10
  550. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  551. #endif
  552. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  553. #endif /* !NO_OLD_TLS */
  554. #ifndef WOLFSSL_NO_TLS12
  555. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  556. #endif /* !WOLFSSL_NO_TLS12 */
  557. #ifdef WOLFSSL_TLS13
  558. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  559. #endif /* WOLFSSL_TLS13 */
  560. #ifdef WOLFSSL_DTLS
  561. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  562. #ifndef NO_OLD_TLS
  563. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  564. #endif /* !NO_OLD_TLS */
  565. #ifndef WOLFSSL_NO_TLS12
  566. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  567. #endif /* !WOLFSSL_NO_TLS12 */
  568. #endif /* WOLFSSL_DTLS */
  569. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  570. }
  571. /*----------------------------------------------------------------------------*
  572. | Context
  573. *----------------------------------------------------------------------------*/
  574. #ifndef NO_WOLFSSL_SERVER
  575. static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
  576. {
  577. WOLFSSL_CTX *ctx;
  578. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  579. AssertNotNull(method);
  580. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  581. wolfSSL_CTX_free(ctx);
  582. }
  583. #endif
  584. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  585. (!defined(NO_RSA) || defined(HAVE_ECC))
  586. static void test_for_double_Free(void)
  587. {
  588. WOLFSSL_CTX* ctx;
  589. WOLFSSL* ssl;
  590. int skipTest = 0;
  591. const char* testCertFile;
  592. const char* testKeyFile;
  593. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  594. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  595. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  596. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  597. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  598. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  599. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  600. "NULL-SHA:HC128-MD5:HC128-SHA:RABBIT-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  601. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  602. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  603. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  604. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  605. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  606. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  607. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  608. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  609. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  610. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  611. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  612. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  613. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  614. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  615. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  616. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  617. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  618. "LY1305-OLD:IDEA-CBC-SHA:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  619. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  620. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  621. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  622. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  623. #ifndef NO_RSA
  624. testCertFile = svrCertFile;
  625. testKeyFile = svrKeyFile;
  626. #elif defined(HAVE_ECC)
  627. testCertFile = eccCertFile;
  628. testKeyFile = eccKeyFile;
  629. #else
  630. skipTest = 1;
  631. #endif
  632. if (skipTest != 1) {
  633. #ifndef NO_WOLFSSL_SERVER
  634. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  635. AssertNotNull(ctx);
  636. #else
  637. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  638. AssertNotNull(ctx);
  639. #endif
  640. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  641. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  642. ssl = wolfSSL_new(ctx);
  643. AssertNotNull(ssl);
  644. /* First test freeing SSL, then CTX */
  645. wolfSSL_free(ssl);
  646. wolfSSL_CTX_free(ctx);
  647. #ifndef NO_WOLFSSL_CLIENT
  648. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  649. AssertNotNull(ctx);
  650. #else
  651. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  652. AssertNotNull(ctx);
  653. #endif
  654. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  655. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  656. ssl = wolfSSL_new(ctx);
  657. AssertNotNull(ssl);
  658. /* Next test freeing CTX then SSL */
  659. wolfSSL_CTX_free(ctx);
  660. wolfSSL_free(ssl);
  661. #ifndef NO_WOLFSSL_SERVER
  662. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  663. AssertNotNull(ctx);
  664. #else
  665. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  666. AssertNotNull(ctx);
  667. #endif
  668. /* Test setting ciphers at ctx level */
  669. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  670. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  671. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  672. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  673. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  674. /* only update TLSv13 suites */
  675. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  676. #endif
  677. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  678. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  679. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  680. /* only update pre-TLSv13 suites */
  681. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
  682. #endif
  683. AssertNotNull(ssl = wolfSSL_new(ctx));
  684. wolfSSL_CTX_free(ctx);
  685. wolfSSL_free(ssl);
  686. #ifndef NO_WOLFSSL_CLIENT
  687. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  688. AssertNotNull(ctx);
  689. #else
  690. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  691. AssertNotNull(ctx);
  692. #endif
  693. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  694. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  695. ssl = wolfSSL_new(ctx);
  696. AssertNotNull(ssl);
  697. /* test setting ciphers at SSL level */
  698. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  699. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  700. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  701. /* only update TLSv13 suites */
  702. AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  703. #endif
  704. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  705. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  706. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  707. /* only update pre-TLSv13 suites */
  708. AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  709. #endif
  710. wolfSSL_CTX_free(ctx);
  711. wolfSSL_free(ssl);
  712. }
  713. }
  714. #endif
  715. static void test_wolfSSL_CTX_use_certificate_file(void)
  716. {
  717. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  718. WOLFSSL_CTX *ctx;
  719. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  720. /* invalid context */
  721. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  722. WOLFSSL_FILETYPE_PEM));
  723. /* invalid cert file */
  724. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  725. WOLFSSL_FILETYPE_PEM));
  726. /* invalid cert type */
  727. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  728. #ifdef NO_RSA
  729. /* rsa needed */
  730. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  731. #else
  732. /* success */
  733. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  734. #endif
  735. wolfSSL_CTX_free(ctx);
  736. #endif
  737. }
  738. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  739. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  740. {
  741. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  742. WOLFSSL_CTX* ctx;
  743. int ret;
  744. printf(testingFmt, "wolfSSL_CTX_use_certificate_ASN1()");
  745. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  746. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  747. server_cert_der_2048);
  748. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  749. wolfSSL_CTX_free(ctx);
  750. return ret;
  751. #else
  752. return WOLFSSL_SUCCESS;
  753. #endif
  754. }
  755. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  756. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  757. * context using buffer.
  758. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  759. * --enable-testcert flag.
  760. */
  761. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  762. {
  763. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  764. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  765. WOLFSSL_CTX* ctx;
  766. int ret;
  767. printf(testingFmt, "wolfSSL_CTX_use_certificate_buffer()");
  768. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  769. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  770. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  771. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  772. wolfSSL_CTX_free(ctx);
  773. return ret;
  774. #else
  775. return WOLFSSL_SUCCESS;
  776. #endif
  777. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  778. static void test_wolfSSL_CTX_use_PrivateKey_file(void)
  779. {
  780. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  781. WOLFSSL_CTX *ctx;
  782. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  783. /* invalid context */
  784. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  785. WOLFSSL_FILETYPE_PEM));
  786. /* invalid key file */
  787. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  788. WOLFSSL_FILETYPE_PEM));
  789. /* invalid key type */
  790. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  791. /* success */
  792. #ifdef NO_RSA
  793. /* rsa needed */
  794. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  795. #else
  796. /* success */
  797. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  798. #endif
  799. wolfSSL_CTX_free(ctx);
  800. #endif
  801. }
  802. /* test both file and buffer versions along with unloading trusted peer certs */
  803. static void test_wolfSSL_CTX_trust_peer_cert(void)
  804. {
  805. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  806. !defined(NO_WOLFSSL_CLIENT)
  807. WOLFSSL_CTX *ctx;
  808. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  809. #if !defined(NO_FILESYSTEM)
  810. /* invalid file */
  811. assert(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  812. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
  813. assert(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  814. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
  815. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  816. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
  817. /* success */
  818. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM)
  819. == WOLFSSL_SUCCESS);
  820. /* unload cert */
  821. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
  822. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
  823. #endif
  824. /* Test of loading certs from buffers */
  825. /* invalid buffer */
  826. assert(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  827. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
  828. /* success */
  829. #ifdef USE_CERT_BUFFERS_1024
  830. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  831. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
  832. #endif
  833. #ifdef USE_CERT_BUFFERS_2048
  834. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  835. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
  836. #endif
  837. /* unload cert */
  838. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
  839. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
  840. wolfSSL_CTX_free(ctx);
  841. #endif
  842. }
  843. static void test_wolfSSL_CTX_load_verify_locations(void)
  844. {
  845. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  846. WOLFSSL_CTX *ctx;
  847. #ifndef NO_RSA
  848. WOLFSSL_CERT_MANAGER* cm;
  849. #ifdef PERSIST_CERT_CACHE
  850. int cacheSz;
  851. #endif
  852. #endif
  853. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  854. const char* load_certs_path = "./certs/external";
  855. const char* load_no_certs_path = "./examples";
  856. const char* load_expired_path = "./certs/test/expired";
  857. #endif
  858. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  859. /* invalid arguments */
  860. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  861. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  862. /* invalid ca file */
  863. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  864. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  865. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  866. (defined(WOLFSSL_QT) && \
  867. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  868. /* invalid path */
  869. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  870. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  871. #endif
  872. /* load ca cert */
  873. #ifdef NO_RSA
  874. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  875. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  876. #else /* Skip the following test without RSA certs. */
  877. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  878. #ifdef PERSIST_CERT_CACHE
  879. /* Get cert cache size */
  880. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  881. #endif
  882. /* Test unloading CA's */
  883. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  884. #ifdef PERSIST_CERT_CACHE
  885. /* Verify no certs (result is less than cacheSz) */
  886. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  887. #endif
  888. /* load ca cert again */
  889. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  890. /* Test getting CERT_MANAGER */
  891. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  892. /* Test unloading CA's using CM */
  893. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  894. #ifdef PERSIST_CERT_CACHE
  895. /* Verify no certs (result is less than cacheSz) */
  896. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  897. #endif
  898. #endif
  899. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  900. /* Test loading CA certificates using a path */
  901. #ifdef NO_RSA
  902. /* failure here okay since certs in external directory are RSA */
  903. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  904. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  905. #else
  906. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  907. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  908. #endif
  909. /* Test loading path with no files */
  910. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  911. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  912. /* Test loading expired CA certificates */
  913. #ifdef NO_RSA
  914. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  915. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  916. WOLFSSL_SUCCESS);
  917. #else
  918. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  919. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  920. WOLFSSL_SUCCESS);
  921. #endif
  922. /* Test loading CA certificates and ignoring all errors */
  923. #ifdef NO_RSA
  924. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  925. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  926. #else
  927. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  928. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  929. #endif
  930. #endif
  931. wolfSSL_CTX_free(ctx);
  932. #endif
  933. }
  934. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  935. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  936. {
  937. int ret;
  938. WOLFSSL_CERT_MANAGER* cm;
  939. cm = wolfSSL_CertManagerNew();
  940. if (cm == NULL) {
  941. printf("test_cm_load_ca failed\n");
  942. return -1;
  943. }
  944. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  945. wolfSSL_CertManagerFree(cm);
  946. return ret;
  947. }
  948. static int test_cm_load_ca_file(const char* ca_cert_file)
  949. {
  950. int ret = 0;
  951. byte* cert_buf = NULL;
  952. size_t cert_sz = 0;
  953. #if defined(WOLFSSL_PEM_TO_DER)
  954. DerBuffer* pDer = NULL;
  955. #endif
  956. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  957. if (ret == 0) {
  958. /* normal test */
  959. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  960. if (ret == WOLFSSL_SUCCESS) {
  961. /* test including null terminator in length */
  962. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  963. if (tmp == NULL) {
  964. ret = MEMORY_E;
  965. }
  966. else {
  967. cert_buf = tmp;
  968. cert_buf[cert_sz] = '\0';
  969. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  970. WOLFSSL_FILETYPE_PEM);
  971. }
  972. }
  973. #if defined(WOLFSSL_PEM_TO_DER)
  974. if (ret == WOLFSSL_SUCCESS) {
  975. /* test loading DER */
  976. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  977. if (ret == 0 && pDer != NULL) {
  978. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  979. WOLFSSL_FILETYPE_ASN1);
  980. wc_FreeDer(&pDer);
  981. }
  982. }
  983. #endif
  984. }
  985. free(cert_buf);
  986. return ret;
  987. }
  988. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  989. static void test_wolfSSL_CertManagerCheckOCSPResponse(void)
  990. {
  991. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  992. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  993. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  994. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  995. defined(HAVE_LIGHTY)
  996. WOLFSSL_CERT_MANAGER* cm = NULL;
  997. /* Raw OCSP response bytes captured using the following setup:
  998. * - Run responder with
  999. * openssl ocsp -port 9999 -ndays 9999
  1000. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1001. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1002. * -rkey certs/ocsp/ocsp-responder-key.pem
  1003. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1004. * - Run client with
  1005. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1006. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1007. * -cert certs/ocsp/server1-cert.pem
  1008. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1009. * - Copy raw response from Wireshark.
  1010. */
  1011. byte response[] = {
  1012. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1013. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1014. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1015. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1016. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1017. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1018. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1019. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1020. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1021. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1022. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1023. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1024. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1025. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1026. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1027. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1028. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1029. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1030. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1031. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1032. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1033. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1034. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1035. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1036. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1037. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1038. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1039. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1040. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1041. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1042. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1043. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1044. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1045. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1046. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1047. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1048. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1049. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1050. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1051. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1052. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1053. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1054. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1055. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1056. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1057. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1058. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1059. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1060. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1061. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1062. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1063. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1064. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1065. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1066. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1067. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1068. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1069. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1070. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1071. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1072. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1073. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1074. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1075. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1076. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1077. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1078. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1079. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1080. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1081. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1082. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1083. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1084. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1085. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1086. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1087. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1088. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1089. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1090. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1091. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1092. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1093. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1094. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1095. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1096. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1097. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1098. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1099. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1100. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1101. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1102. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1103. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1104. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1105. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1106. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1107. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1108. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1109. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1110. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1111. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1112. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1113. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1114. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1115. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1116. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1117. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1118. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1119. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1120. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1121. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1122. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1123. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1124. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1125. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1126. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1127. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1128. 0x59, 0x59, 0xa0, 0x07
  1129. };
  1130. OcspEntry entry[1];
  1131. CertStatus status[1];
  1132. OcspRequest* request;
  1133. byte serial[] = {0x05};
  1134. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1135. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1136. printf(testingFmt, "wolfSSL_CertManagerCheckOCSPResponse()");
  1137. XMEMSET(entry, 0, sizeof(OcspEntry));
  1138. XMEMSET(status, 0, sizeof(CertStatus));
  1139. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1140. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1141. DYNAMIC_TYPE_OCSP_REQUEST);
  1142. AssertNotNull(request->serial);
  1143. request->serialSz = sizeof(serial);
  1144. XMEMCPY(request->serial, serial, sizeof(serial));
  1145. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1146. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1147. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1148. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1149. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1150. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1151. /* Response should be valid. */
  1152. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1153. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1154. /* Flip a byte in the request serial number, response should be invalid
  1155. * now. */
  1156. request->serial[0] ^= request->serial[0];
  1157. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1158. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1159. wolfSSL_OCSP_REQUEST_free(request);
  1160. wolfSSL_CertManagerFree(cm);
  1161. printf(resultFmt, passed);
  1162. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1163. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1164. #endif /* HAVE_OCSP */
  1165. }
  1166. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1167. {
  1168. int ret = 0;
  1169. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1170. const char* ca_cert = "./certs/ca-cert.pem";
  1171. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1172. ret = test_cm_load_ca_file(ca_cert);
  1173. #ifdef NO_RSA
  1174. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1175. #else
  1176. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1177. #endif
  1178. ret = test_cm_load_ca_file(ca_expired_cert);
  1179. #ifdef NO_RSA
  1180. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1181. #else
  1182. #if !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1183. !defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1184. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1185. #else
  1186. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1187. #endif
  1188. #endif
  1189. #endif
  1190. return ret;
  1191. }
  1192. static void test_wolfSSL_CertManagerGetCerts(void)
  1193. {
  1194. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1195. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1196. defined(WOLFSSL_SIGNER_DER_CERT)
  1197. WOLFSSL_CERT_MANAGER* cm = NULL;
  1198. WOLFSSL_STACK* sk = NULL;
  1199. X509* x509 = NULL;
  1200. X509* cert1 = NULL;
  1201. FILE* file1 = NULL;
  1202. #ifdef DEBUG_WOLFSSL_VERBOSE
  1203. WOLFSSL_BIO* bio = NULL;
  1204. #endif
  1205. int i = 0;
  1206. printf(testingFmt, "wolfSSL_CertManagerGetCerts()");
  1207. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1208. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1209. fclose(file1);
  1210. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1211. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1212. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1213. "./certs/ca-cert.pem", NULL));
  1214. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1215. for (i = 0; i < sk_X509_num(sk); i++) {
  1216. x509 = sk_X509_value(sk, i);
  1217. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1218. #ifdef DEBUG_WOLFSSL_VERBOSE
  1219. bio = BIO_new(wolfSSL_BIO_s_file());
  1220. if (bio != NULL) {
  1221. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  1222. X509_print(bio, x509);
  1223. BIO_free(bio);
  1224. }
  1225. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1226. }
  1227. wolfSSL_X509_free(cert1);
  1228. sk_X509_free(sk);
  1229. wolfSSL_CertManagerFree(cm);
  1230. printf(resultFmt, passed);
  1231. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1232. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1233. defined(WOLFSSL_SIGNER_DER_CERT) */
  1234. }
  1235. static int test_wolfSSL_CertManagerSetVerify(void)
  1236. {
  1237. int ret = 0;
  1238. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1239. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1240. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1241. WOLFSSL_CERT_MANAGER* cm;
  1242. int tmp = myVerifyAction;
  1243. const char* ca_cert = "./certs/ca-cert.pem";
  1244. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1245. cm = wolfSSL_CertManagerNew();
  1246. AssertNotNull(cm);
  1247. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1248. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1249. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1250. /* Use the test CB that always accepts certs */
  1251. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1252. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1253. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1254. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1255. {
  1256. const char* verifyCert = "./certs/server-cert.pem";
  1257. /* Use the test CB that always fails certs */
  1258. myVerifyAction = VERIFY_FORCE_FAIL;
  1259. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1260. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1261. }
  1262. #endif
  1263. wolfSSL_CertManagerFree(cm);
  1264. myVerifyAction = tmp;
  1265. #endif
  1266. return ret;
  1267. }
  1268. static void test_wolfSSL_CertManagerNameConstraint(void)
  1269. {
  1270. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1271. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1272. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1273. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1274. WOLFSSL_CERT_MANAGER* cm;
  1275. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1276. int i = 0;
  1277. static const byte extNameConsOid[] = {85, 29, 30};
  1278. RsaKey key;
  1279. WC_RNG rng;
  1280. byte *der;
  1281. int derSz;
  1282. word32 idx = 0;
  1283. byte *pt;
  1284. WOLFSSL_X509 *x509;
  1285. wc_InitRng(&rng);
  1286. /* load in CA private key for signing */
  1287. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, devId), 0);
  1288. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1289. sizeof_server_key_der_2048), 0);
  1290. /* get ca certificate then alter it */
  1291. AssertNotNull(der =
  1292. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1293. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1294. WOLFSSL_FILETYPE_ASN1));
  1295. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1296. XMEMCPY(der, pt, derSz);
  1297. /* find the name constraint extension and alter it */
  1298. pt = der;
  1299. for (i = 0; i < derSz - 3; i++) {
  1300. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1301. pt += 3;
  1302. break;
  1303. }
  1304. pt++;
  1305. }
  1306. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1307. /* go to the length value and set it to 0 */
  1308. while (i < derSz && *pt != 0x81) {
  1309. pt++;
  1310. i++;
  1311. }
  1312. AssertIntNE(i, derSz); /* did not place to alter */
  1313. pt++;
  1314. *pt = 0x00;
  1315. /* resign the altered certificate */
  1316. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1317. FOURK_BUF, &key, NULL, &rng)), 0);
  1318. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1319. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1320. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1321. wolfSSL_CertManagerFree(cm);
  1322. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1323. wolfSSL_X509_free(x509);
  1324. wc_FreeRsaKey(&key);
  1325. wc_FreeRng(&rng);
  1326. #endif
  1327. }
  1328. static void test_wolfSSL_CertManagerNameConstraint2(void)
  1329. {
  1330. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1331. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1332. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1333. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1334. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  1335. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  1336. const char* server_cert = "./certs/server-cert.pem";
  1337. WOLFSSL_CERT_MANAGER* cm;
  1338. WOLFSSL_X509 *x509, *ca;
  1339. const unsigned char *der;
  1340. const unsigned char *pt;
  1341. WOLFSSL_EVP_PKEY *priv;
  1342. WOLFSSL_X509_NAME* name;
  1343. int derSz;
  1344. /* C=US*/
  1345. char altName[] = {
  1346. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1347. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  1348. };
  1349. /* C=ID */
  1350. char altNameFail[] = {
  1351. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1352. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  1353. };
  1354. /* C=US ST=California*/
  1355. char altNameExc[] = {
  1356. 0x30, 0x22,
  1357. 0x31, 0x0B,
  1358. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  1359. 0x31, 0x13,
  1360. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  1361. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  1362. };
  1363. /* load in CA private key for signing */
  1364. pt = ca_key_der_2048;
  1365. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  1366. sizeof_ca_key_der_2048));
  1367. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1368. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1369. WOLFSSL_FILETYPE_ASN1));
  1370. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1371. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1372. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1373. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1374. WOLFSSL_FILETYPE_PEM));
  1375. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1376. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1377. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1378. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1379. #else
  1380. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1381. #endif
  1382. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1383. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1384. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1385. /* add in matching DIR alt name and resign */
  1386. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1387. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1388. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1389. #else
  1390. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1391. #endif
  1392. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1393. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1394. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1395. wolfSSL_X509_free(x509);
  1396. /* check verify fail */
  1397. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1398. WOLFSSL_FILETYPE_PEM));
  1399. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1400. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1401. /* add in miss matching DIR alt name and resign */
  1402. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1403. ASN_DIR_TYPE);
  1404. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1405. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1406. #else
  1407. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1408. #endif
  1409. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1410. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1411. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1412. wolfSSL_CertManagerFree(cm);
  1413. wolfSSL_X509_free(x509);
  1414. wolfSSL_X509_free(ca);
  1415. /* now test with excluded name constraint */
  1416. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1417. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  1418. WOLFSSL_FILETYPE_ASN1));
  1419. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1420. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1421. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1422. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1423. WOLFSSL_FILETYPE_PEM));
  1424. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  1425. ASN_DIR_TYPE);
  1426. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1427. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1428. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1429. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1430. #else
  1431. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1432. #endif
  1433. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1434. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1435. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1436. wolfSSL_CertManagerFree(cm);
  1437. wolfSSL_X509_free(x509);
  1438. wolfSSL_X509_free(ca);
  1439. wolfSSL_EVP_PKEY_free(priv);
  1440. #endif
  1441. }
  1442. static void test_wolfSSL_CertManagerCRL(void)
  1443. {
  1444. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  1445. !defined(NO_RSA)
  1446. const char* ca_cert = "./certs/ca-cert.pem";
  1447. const char* crl1 = "./certs/crl/crl.pem";
  1448. const char* crl2 = "./certs/crl/crl2.pem";
  1449. WOLFSSL_CERT_MANAGER* cm = NULL;
  1450. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1451. AssertIntEQ(WOLFSSL_SUCCESS,
  1452. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1453. AssertIntEQ(WOLFSSL_SUCCESS,
  1454. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1455. AssertIntEQ(WOLFSSL_SUCCESS,
  1456. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  1457. wolfSSL_CertManagerFreeCRL(cm);
  1458. AssertIntEQ(WOLFSSL_SUCCESS,
  1459. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1460. AssertIntEQ(WOLFSSL_SUCCESS,
  1461. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1462. wolfSSL_CertManagerFree(cm);
  1463. #endif
  1464. }
  1465. static void test_wolfSSL_CTX_load_verify_locations_ex(void)
  1466. {
  1467. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1468. !defined(NO_WOLFSSL_CLIENT)
  1469. WOLFSSL_CTX* ctx;
  1470. const char* ca_cert = "./certs/ca-cert.pem";
  1471. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1472. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1473. AssertNotNull(ctx);
  1474. /* test good CA */
  1475. AssertTrue(WOLFSSL_SUCCESS ==
  1476. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  1477. WOLFSSL_LOAD_FLAG_NONE));
  1478. /* test expired CA */
  1479. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  1480. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1481. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  1482. #else
  1483. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1484. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  1485. #endif
  1486. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1487. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  1488. wolfSSL_CTX_free(ctx);
  1489. #endif
  1490. }
  1491. static void test_wolfSSL_CTX_load_verify_buffer_ex(void)
  1492. {
  1493. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1494. defined(USE_CERT_BUFFERS_2048)
  1495. WOLFSSL_CTX* ctx;
  1496. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  1497. byte ca_expired_cert[TWOK_BUF];
  1498. word32 sizeof_ca_expired_cert;
  1499. XFILE fp;
  1500. #ifndef NO_WOLFSSL_CLIENT
  1501. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1502. #else
  1503. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1504. #endif
  1505. AssertNotNull(ctx);
  1506. /* test good CA */
  1507. AssertTrue(WOLFSSL_SUCCESS ==
  1508. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  1509. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  1510. WOLFSSL_LOAD_FLAG_NONE));
  1511. /* load expired CA */
  1512. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  1513. fp = XFOPEN(ca_expired_cert_file, "rb");
  1514. AssertTrue(fp != XBADFILE);
  1515. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  1516. sizeof(ca_expired_cert), fp);
  1517. XFCLOSE(fp);
  1518. /* test expired CA failure */
  1519. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  1520. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  1521. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  1522. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  1523. #else
  1524. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  1525. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  1526. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  1527. #endif
  1528. /* test expired CA success */
  1529. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  1530. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  1531. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  1532. wolfSSL_CTX_free(ctx);
  1533. #endif
  1534. }
  1535. static void test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  1536. {
  1537. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  1538. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048)
  1539. WOLFSSL_CTX* ctx;
  1540. #ifndef NO_WOLFSSL_CLIENT
  1541. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1542. #else
  1543. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1544. #endif
  1545. AssertTrue(WOLFSSL_SUCCESS ==
  1546. wolfSSL_CTX_load_verify_chain_buffer_format(ctx, ca_cert_chain_der,
  1547. sizeof_ca_cert_chain_der,
  1548. WOLFSSL_FILETYPE_ASN1));
  1549. wolfSSL_CTX_free(ctx);
  1550. #endif
  1551. }
  1552. static void test_wolfSSL_CTX_add1_chain_cert(void)
  1553. {
  1554. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  1555. defined(KEEP_OUR_CERT)
  1556. WOLFSSL_CTX* ctx;
  1557. WOLFSSL* ssl;
  1558. const char *certChain[] = {
  1559. "./certs/intermediate/client-int-cert.pem",
  1560. "./certs/intermediate/ca-int2-cert.pem",
  1561. "./certs/intermediate/ca-int-cert.pem",
  1562. "./certs/ca-cert.pem",
  1563. NULL
  1564. };
  1565. const char** cert;
  1566. WOLFSSL_X509* x509;
  1567. WOLF_STACK_OF(X509)* chain = NULL;
  1568. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1569. AssertNotNull(ssl = wolfSSL_new(ctx));
  1570. for (cert = certChain; *cert != NULL; cert++) {
  1571. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  1572. AssertNotNull(x509);
  1573. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  1574. X509_free(x509);
  1575. }
  1576. for (cert = certChain; *cert != NULL; cert++) {
  1577. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  1578. AssertNotNull(x509);
  1579. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  1580. X509_free(x509);
  1581. }
  1582. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  1583. AssertIntEQ(sk_X509_num(chain), 3);
  1584. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  1585. AssertIntEQ(sk_X509_num(chain), 3);
  1586. SSL_free(ssl);
  1587. SSL_CTX_free(ctx);
  1588. #endif
  1589. }
  1590. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  1591. {
  1592. int ret = 0;
  1593. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  1594. const char* server_chain_der = "./certs/server-cert-chain.der";
  1595. WOLFSSL_CTX* ctx;
  1596. #ifndef NO_WOLFSSL_CLIENT
  1597. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1598. AssertNotNull(ctx);
  1599. #else
  1600. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1601. AssertNotNull(ctx);
  1602. #endif
  1603. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  1604. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1605. wolfSSL_CTX_free(ctx);
  1606. #endif
  1607. return ret;
  1608. }
  1609. static void test_wolfSSL_CTX_SetTmpDH_file(void)
  1610. {
  1611. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH)
  1612. WOLFSSL_CTX *ctx;
  1613. #ifndef NO_WOLFSSL_CLIENT
  1614. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1615. #else
  1616. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1617. #endif
  1618. /* invalid context */
  1619. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  1620. dhParamFile, WOLFSSL_FILETYPE_PEM));
  1621. /* invalid dhParamFile file */
  1622. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  1623. NULL, WOLFSSL_FILETYPE_PEM));
  1624. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  1625. bogusFile, WOLFSSL_FILETYPE_PEM));
  1626. /* success */
  1627. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  1628. WOLFSSL_FILETYPE_PEM));
  1629. wolfSSL_CTX_free(ctx);
  1630. #endif
  1631. }
  1632. static void test_wolfSSL_CTX_SetTmpDH_buffer(void)
  1633. {
  1634. #if !defined(NO_CERTS) && !defined(NO_DH)
  1635. WOLFSSL_CTX *ctx;
  1636. #ifndef NO_WOLFSSL_CLIENT
  1637. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1638. #else
  1639. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1640. #endif
  1641. /* invalid context */
  1642. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  1643. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1644. /* invalid dhParamFile file */
  1645. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  1646. 0, WOLFSSL_FILETYPE_ASN1));
  1647. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  1648. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1649. /* success */
  1650. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1651. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1652. wolfSSL_CTX_free(ctx);
  1653. #endif
  1654. }
  1655. static void test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  1656. {
  1657. #if !defined(NO_CERTS) && !defined(NO_DH)
  1658. WOLFSSL_CTX *ctx;
  1659. #ifndef NO_WOLFSSL_CLIENT
  1660. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1661. AssertNotNull(ctx);
  1662. #else
  1663. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1664. AssertNotNull(ctx);
  1665. #endif
  1666. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  1667. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1668. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1669. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  1670. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1671. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1672. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  1673. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1674. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1675. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  1676. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  1677. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1678. wolfSSL_CTX_free(ctx);
  1679. #endif
  1680. }
  1681. static void test_wolfSSL_CTX_der_load_verify_locations(void)
  1682. {
  1683. #ifdef WOLFSSL_DER_LOAD
  1684. WOLFSSL_CTX* ctx = NULL;
  1685. const char* derCert = "./certs/server-cert.der";
  1686. const char* nullPath = NULL;
  1687. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  1688. const char* emptyPath = "";
  1689. /* der load Case 1 ctx NULL */
  1690. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  1691. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1692. #ifndef NO_WOLFSSL_CLIENT
  1693. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1694. #else
  1695. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1696. #endif
  1697. /* Case 2 filePath NULL */
  1698. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  1699. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1700. /* Case 3 invalid format */
  1701. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  1702. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  1703. /* Case 4 filePath not valid */
  1704. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  1705. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1706. /* Case 5 filePath empty */
  1707. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  1708. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  1709. #ifndef NO_RSA
  1710. /* Case 6 success case */
  1711. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  1712. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1713. #endif
  1714. wolfSSL_CTX_free(ctx);
  1715. #endif
  1716. }
  1717. static void test_wolfSSL_CTX_enable_disable(void)
  1718. {
  1719. #ifndef NO_CERTS
  1720. WOLFSSL_CTX* ctx = NULL;
  1721. #ifdef HAVE_CRL
  1722. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  1723. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  1724. #endif
  1725. #ifdef HAVE_OCSP
  1726. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  1727. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  1728. #endif
  1729. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1730. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1731. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  1732. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  1733. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  1734. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  1735. #endif
  1736. #ifndef NO_WOLFSSL_CLIENT
  1737. #ifdef HAVE_EXTENDED_MASTER
  1738. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  1739. #endif
  1740. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1741. AssertNotNull(ctx);
  1742. #ifdef HAVE_EXTENDED_MASTER
  1743. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  1744. #endif
  1745. #elif !defined(NO_WOLFSSL_SERVER)
  1746. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1747. #else
  1748. return;
  1749. #endif
  1750. #ifdef HAVE_CRL
  1751. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  1752. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  1753. #endif
  1754. #ifdef HAVE_OCSP
  1755. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  1756. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  1757. WOLFSSL_SUCCESS);
  1758. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  1759. WOLFSSL_SUCCESS);
  1760. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  1761. WOLFSSL_SUCCESS);
  1762. #endif
  1763. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1764. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1765. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  1766. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  1767. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  1768. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  1769. #endif
  1770. wolfSSL_CTX_free(ctx);
  1771. #endif /* NO_CERTS */
  1772. }
  1773. static void test_wolfSSL_CTX_ticket_API(void)
  1774. {
  1775. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  1776. WOLFSSL_CTX* ctx = NULL;
  1777. void *userCtx = (void*)"this is my ctx";
  1778. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1779. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  1780. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  1781. wolfSSL_CTX_free(ctx);
  1782. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  1783. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  1784. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  1785. }
  1786. /*----------------------------------------------------------------------------*
  1787. | SSL
  1788. *----------------------------------------------------------------------------*/
  1789. static void test_server_wolfSSL_new(void)
  1790. {
  1791. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1792. !defined(NO_WOLFSSL_SERVER)
  1793. WOLFSSL_CTX *ctx;
  1794. WOLFSSL_CTX *ctx_nocert;
  1795. WOLFSSL *ssl;
  1796. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1797. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1798. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1799. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1800. /* invalid context */
  1801. AssertNull(ssl = wolfSSL_new(NULL));
  1802. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  1803. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  1804. #endif
  1805. /* success */
  1806. AssertNotNull(ssl = wolfSSL_new(ctx));
  1807. wolfSSL_free(ssl);
  1808. wolfSSL_CTX_free(ctx);
  1809. wolfSSL_CTX_free(ctx_nocert);
  1810. #endif
  1811. }
  1812. static void test_client_wolfSSL_new(void)
  1813. {
  1814. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1815. !defined(NO_WOLFSSL_CLIENT)
  1816. WOLFSSL_CTX *ctx;
  1817. WOLFSSL_CTX *ctx_nocert;
  1818. WOLFSSL *ssl;
  1819. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1820. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1821. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  1822. /* invalid context */
  1823. AssertNull(ssl = wolfSSL_new(NULL));
  1824. /* success */
  1825. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  1826. wolfSSL_free(ssl);
  1827. /* success */
  1828. AssertNotNull(ssl = wolfSSL_new(ctx));
  1829. wolfSSL_free(ssl);
  1830. wolfSSL_CTX_free(ctx);
  1831. wolfSSL_CTX_free(ctx_nocert);
  1832. #endif
  1833. }
  1834. static void test_wolfSSL_SetTmpDH_file(void)
  1835. {
  1836. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  1837. !defined(NO_WOLFSSL_SERVER)
  1838. WOLFSSL_CTX *ctx;
  1839. WOLFSSL *ssl;
  1840. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1841. #ifndef NO_RSA
  1842. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1843. WOLFSSL_FILETYPE_PEM));
  1844. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1845. WOLFSSL_FILETYPE_PEM));
  1846. #elif defined(HAVE_ECC)
  1847. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  1848. WOLFSSL_FILETYPE_PEM));
  1849. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  1850. WOLFSSL_FILETYPE_PEM));
  1851. #elif defined(HAVE_ED25519)
  1852. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  1853. WOLFSSL_FILETYPE_PEM));
  1854. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  1855. WOLFSSL_FILETYPE_PEM));
  1856. #elif defined(HAVE_ED448)
  1857. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  1858. WOLFSSL_FILETYPE_PEM));
  1859. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  1860. WOLFSSL_FILETYPE_PEM));
  1861. #endif
  1862. AssertNotNull(ssl = wolfSSL_new(ctx));
  1863. /* invalid ssl */
  1864. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  1865. dhParamFile, WOLFSSL_FILETYPE_PEM));
  1866. /* invalid dhParamFile file */
  1867. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  1868. NULL, WOLFSSL_FILETYPE_PEM));
  1869. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  1870. bogusFile, WOLFSSL_FILETYPE_PEM));
  1871. /* success */
  1872. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  1873. WOLFSSL_FILETYPE_PEM));
  1874. wolfSSL_free(ssl);
  1875. wolfSSL_CTX_free(ctx);
  1876. #endif
  1877. }
  1878. static void test_wolfSSL_SetTmpDH_buffer(void)
  1879. {
  1880. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  1881. WOLFSSL_CTX *ctx;
  1882. WOLFSSL *ssl;
  1883. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1884. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  1885. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  1886. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  1887. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1888. AssertNotNull(ssl = wolfSSL_new(ctx));
  1889. /* invalid ssl */
  1890. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  1891. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1892. /* invalid dhParamFile file */
  1893. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  1894. 0, WOLFSSL_FILETYPE_ASN1));
  1895. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  1896. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1897. /* success */
  1898. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1899. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1900. wolfSSL_free(ssl);
  1901. wolfSSL_CTX_free(ctx);
  1902. #endif
  1903. }
  1904. static void test_wolfSSL_SetMinMaxDhKey_Sz(void)
  1905. {
  1906. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  1907. WOLFSSL_CTX *ctx, *ctx2;
  1908. WOLFSSL *ssl, *ssl2;
  1909. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1910. AssertNotNull(ctx);
  1911. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  1912. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  1913. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  1914. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1915. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  1916. ssl = wolfSSL_new(ctx);
  1917. AssertNotNull(ssl);
  1918. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  1919. AssertNotNull(ctx2);
  1920. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  1921. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  1922. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  1923. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1924. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  1925. ssl2 = wolfSSL_new(ctx2);
  1926. AssertNotNull(ssl2);
  1927. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1928. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1929. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  1930. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1931. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1932. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  1933. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1934. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1935. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  1936. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1937. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  1938. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  1939. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1940. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  1941. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  1942. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  1943. wolfSSL_free(ssl2);
  1944. wolfSSL_CTX_free(ctx2);
  1945. wolfSSL_free(ssl);
  1946. wolfSSL_CTX_free(ctx);
  1947. #endif
  1948. }
  1949. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  1950. * allowed.
  1951. * POST: return 1 on success.
  1952. */
  1953. static int test_wolfSSL_SetMinVersion(void)
  1954. {
  1955. int failFlag = WOLFSSL_SUCCESS;
  1956. #ifndef NO_WOLFSSL_CLIENT
  1957. WOLFSSL_CTX* ctx;
  1958. WOLFSSL* ssl;
  1959. int itr;
  1960. #ifndef NO_OLD_TLS
  1961. const int versions[] = {
  1962. #ifdef WOLFSSL_ALLOW_TLSV10
  1963. WOLFSSL_TLSV1,
  1964. #endif
  1965. WOLFSSL_TLSV1_1,
  1966. WOLFSSL_TLSV1_2};
  1967. #elif !defined(WOLFSSL_NO_TLS12)
  1968. const int versions[] = { WOLFSSL_TLSV1_2 };
  1969. #else
  1970. const int versions[] = { WOLFSSL_TLSV1_3 };
  1971. #endif
  1972. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1973. ssl = wolfSSL_new(ctx);
  1974. printf(testingFmt, "wolfSSL_SetMinVersion()");
  1975. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  1976. if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
  1977. failFlag = WOLFSSL_FAILURE;
  1978. }
  1979. }
  1980. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  1981. wolfSSL_free(ssl);
  1982. wolfSSL_CTX_free(ctx);
  1983. #endif
  1984. return failFlag;
  1985. } /* END test_wolfSSL_SetMinVersion */
  1986. /*----------------------------------------------------------------------------*
  1987. | EC
  1988. *----------------------------------------------------------------------------*/
  1989. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  1990. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  1991. */
  1992. # if defined(OPENSSL_EXTRA) && \
  1993. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  1994. static void test_wolfSSL_EC(void)
  1995. {
  1996. #if defined(HAVE_ECC)
  1997. BN_CTX *ctx;
  1998. EC_GROUP *group;
  1999. EC_GROUP *group2;
  2000. EC_POINT *Gxy, *new_point, *set_point;
  2001. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  2002. BIGNUM *X, *Y;
  2003. BIGNUM *set_point_bn;
  2004. char* hexStr;
  2005. int group_bits;
  2006. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  2007. /* NISTP256R1 Gx/Gy */
  2008. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2009. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2010. #ifndef HAVE_SELFTEST
  2011. EC_POINT *tmp;
  2012. size_t bin_len;
  2013. unsigned char* buf = NULL;
  2014. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2015. const unsigned char binUncompG[] = {
  2016. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2017. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2018. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2019. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  2020. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  2021. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  2022. };
  2023. #ifdef HAVE_COMP_KEY
  2024. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2025. const unsigned char binCompG[] = {
  2026. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2027. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2028. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2029. };
  2030. #endif
  2031. #endif
  2032. AssertNotNull(ctx = BN_CTX_new());
  2033. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  2034. AssertNotNull(group2 = EC_GROUP_dup(group));
  2035. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  2036. AssertNotNull(Gxy = EC_POINT_new(group));
  2037. AssertNotNull(new_point = EC_POINT_new(group));
  2038. AssertNotNull(set_point = EC_POINT_new(group));
  2039. AssertNotNull(X = BN_new());
  2040. AssertNotNull(Y = BN_new());
  2041. AssertNotNull(set_point_bn = BN_new());
  2042. /* load test values */
  2043. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  2044. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  2045. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  2046. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  2047. /* populate coordinates for input point */
  2048. Gxy->X = Gx;
  2049. Gxy->Y = Gy;
  2050. Gxy->Z = Gz;
  2051. #ifndef HAVE_SELFTEST
  2052. /* perform point multiplication */
  2053. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
  2054. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
  2055. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2056. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2057. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2058. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  2059. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2060. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2061. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2062. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
  2063. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2064. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2065. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2066. #else
  2067. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  2068. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2069. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2070. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2071. #endif
  2072. /* check if point X coordinate is zero */
  2073. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2074. #ifdef USE_ECC_B_PARAM
  2075. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  2076. #endif /* USE_ECC_B_PARAM */
  2077. /* Force non-affine coordinates */
  2078. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2079. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  2080. new_point->inSet = 0;
  2081. /* extract the coordinates from point */
  2082. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2083. /* check if point X coordinate is zero */
  2084. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  2085. /* set the same X and Y points in another object */
  2086. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2087. /* compare points as they should be the same */
  2088. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  2089. /* Test copying */
  2090. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  2091. /* Test inverting */
  2092. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  2093. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  2094. set_point_bn, ctx), set_point_bn);
  2095. /* check bn2hex */
  2096. hexStr = BN_bn2hex(k);
  2097. AssertStrEQ(hexStr, kTest);
  2098. #ifndef NO_FILESYSTEM
  2099. BN_print_fp(stdout, k);
  2100. printf("\n");
  2101. #endif
  2102. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2103. hexStr = BN_bn2hex(Gx);
  2104. AssertStrEQ(hexStr, kGx);
  2105. #ifndef NO_FILESYSTEM
  2106. BN_print_fp(stdout, Gx);
  2107. printf("\n");
  2108. #endif
  2109. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2110. hexStr = BN_bn2hex(Gy);
  2111. AssertStrEQ(hexStr, kGy);
  2112. #ifndef NO_FILESYSTEM
  2113. BN_print_fp(stdout, Gy);
  2114. printf("\n");
  2115. #endif
  2116. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2117. #ifndef HAVE_SELFTEST
  2118. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  2119. AssertStrEQ(hexStr, uncompG);
  2120. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2121. #ifdef HAVE_COMP_KEY
  2122. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  2123. AssertStrEQ(hexStr, compG);
  2124. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2125. #endif
  2126. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  2127. AssertIntEQ(bin_len, sizeof(binUncompG));
  2128. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2129. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  2130. bin_len, ctx), bin_len);
  2131. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  2132. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2133. #ifdef HAVE_COMP_KEY
  2134. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  2135. AssertIntEQ(bin_len, sizeof(binCompG));
  2136. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2137. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  2138. bin_len, ctx), bin_len);
  2139. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  2140. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2141. #endif
  2142. AssertNotNull(tmp = EC_POINT_new(group));
  2143. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  2144. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2145. EC_POINT_free(tmp);
  2146. #ifdef HAVE_COMP_KEY
  2147. AssertNotNull(tmp = EC_POINT_new(group));
  2148. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  2149. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2150. EC_POINT_free(tmp);
  2151. #endif
  2152. #endif
  2153. /* test BN_mod_add */
  2154. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2155. (WOLFSSL_BIGNUM*)BN_value_one(),
  2156. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  2157. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  2158. /* cleanup */
  2159. BN_free(X);
  2160. BN_free(Y);
  2161. BN_free(k);
  2162. BN_free(set_point_bn);
  2163. EC_POINT_free(new_point);
  2164. EC_POINT_free(set_point);
  2165. EC_POINT_free(Gxy);
  2166. EC_GROUP_free(group);
  2167. EC_GROUP_free(group2);
  2168. BN_CTX_free(ctx);
  2169. #endif /* HAVE_ECC */
  2170. }
  2171. #endif /* OPENSSL_EXTRA && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  2172. #ifndef NO_BIO
  2173. static void test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  2174. {
  2175. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  2176. EC_GROUP *group;
  2177. BIO* bio;
  2178. AssertNotNull(bio = BIO_new(BIO_s_file()));
  2179. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  2180. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  2181. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  2182. EC_GROUP_free(group);
  2183. BIO_free(bio);
  2184. #endif /* HAVE_ECC */
  2185. }
  2186. #endif /* !NO_BIO */
  2187. # if defined(OPENSSL_EXTRA)
  2188. static void test_wolfSSL_ECDSA_SIG(void)
  2189. {
  2190. #ifdef HAVE_ECC
  2191. WOLFSSL_ECDSA_SIG* sig = NULL;
  2192. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  2193. const unsigned char* cp;
  2194. unsigned char* p;
  2195. unsigned char outSig[8];
  2196. unsigned char sigData[8] =
  2197. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  2198. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  2199. AssertNull(sig);
  2200. cp = sigData;
  2201. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  2202. AssertIntEQ((cp == sigData + 8), 1);
  2203. cp = sigData;
  2204. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  2205. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  2206. AssertIntEQ((sig == sig2), 1);
  2207. cp = outSig;
  2208. p = outSig;
  2209. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  2210. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  2211. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  2212. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  2213. AssertIntEQ((p == outSig + 8), 1);
  2214. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  2215. wolfSSL_ECDSA_SIG_free(sig);
  2216. #endif /* HAVE_ECC */
  2217. }
  2218. static void test_EC_i2d(void)
  2219. {
  2220. #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
  2221. EC_KEY *key;
  2222. EC_KEY *copy;
  2223. int len;
  2224. unsigned char *buf = NULL;
  2225. const unsigned char *tmp = NULL;
  2226. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2227. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2228. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  2229. AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
  2230. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2231. buf = NULL;
  2232. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  2233. AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
  2234. tmp = buf;
  2235. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  2236. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2237. buf = NULL;
  2238. AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
  2239. tmp = buf;
  2240. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  2241. AssertIntEQ(EC_KEY_check_key(key), 1);
  2242. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  2243. EC_KEY_free(key);
  2244. EC_KEY_free(copy);
  2245. #endif /* HAVE_ECC */
  2246. }
  2247. static void test_ECDSA_size_sign(void)
  2248. {
  2249. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  2250. EC_KEY *key;
  2251. int id;
  2252. byte hash[WC_MAX_DIGEST_SIZE];
  2253. byte sig[ECC_BUFSIZE];
  2254. unsigned int sigSz = sizeof(sig);
  2255. XMEMSET(hash, 123, sizeof(hash));
  2256. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  2257. AssertIntEQ(id, ECC_SECP256R1);
  2258. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2259. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2260. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  2261. AssertIntGE(ECDSA_size(key), sigSz);
  2262. EC_KEY_free(key);
  2263. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  2264. }
  2265. static void test_ED25519(void)
  2266. {
  2267. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  2268. defined(WOLFSSL_KEY_GEN)
  2269. byte priv[ED25519_PRV_KEY_SIZE];
  2270. unsigned int privSz = (unsigned int)sizeof(priv);
  2271. byte pub[ED25519_PUB_KEY_SIZE];
  2272. unsigned int pubSz = (unsigned int)sizeof(pub);
  2273. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  2274. const char* msg = TEST_STRING;
  2275. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  2276. byte sig[ED25519_SIG_SIZE];
  2277. unsigned int sigSz = (unsigned int)sizeof(sig);
  2278. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  2279. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  2280. WOLFSSL_SUCCESS);
  2281. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  2282. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  2283. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  2284. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  2285. &sigSz), WOLFSSL_SUCCESS);
  2286. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  2287. #ifdef HAVE_ED25519_VERIFY
  2288. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  2289. sigSz), WOLFSSL_SUCCESS);
  2290. #endif /* HAVE_ED25519_VERIFY */
  2291. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  2292. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  2293. }
  2294. static void test_ED448(void)
  2295. {
  2296. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  2297. defined(WOLFSSL_KEY_GEN)
  2298. byte priv[ED448_PRV_KEY_SIZE];
  2299. unsigned int privSz = (unsigned int)sizeof(priv);
  2300. byte pub[ED448_PUB_KEY_SIZE];
  2301. unsigned int pubSz = (unsigned int)sizeof(pub);
  2302. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  2303. const char* msg = TEST_STRING;
  2304. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  2305. byte sig[ED448_SIG_SIZE];
  2306. unsigned int sigSz = (unsigned int)sizeof(sig);
  2307. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  2308. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  2309. WOLFSSL_SUCCESS);
  2310. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  2311. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  2312. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  2313. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  2314. &sigSz), WOLFSSL_SUCCESS);
  2315. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  2316. #ifdef HAVE_ED448_VERIFY
  2317. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  2318. sigSz), WOLFSSL_SUCCESS);
  2319. #endif /* HAVE_ED448_VERIFY */
  2320. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  2321. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  2322. }
  2323. #endif /* OPENSSL_EXTRA */
  2324. #include <wolfssl/openssl/pem.h>
  2325. /*----------------------------------------------------------------------------*
  2326. | EVP
  2327. *----------------------------------------------------------------------------*/
  2328. static void test_wolfSSL_EVP_PKEY_print_public(void)
  2329. {
  2330. #if defined(OPENSSL_EXTRA)
  2331. WOLFSSL_BIO* rbio = NULL;
  2332. WOLFSSL_BIO* wbio = NULL;
  2333. WOLFSSL_EVP_PKEY* pkey = NULL;
  2334. char line[256] = { 0 };
  2335. char line1[256] = { 0 };
  2336. int i;
  2337. printf(testingFmt, "EVP_PKEY_print_public()");
  2338. /* test error cases */
  2339. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  2340. /*
  2341. * test RSA public key print
  2342. * in this test, pass '3' for indent
  2343. */
  2344. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  2345. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  2346. sizeof_client_keypub_der_1024);
  2347. AssertNotNull(rbio);
  2348. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2349. AssertNotNull(pkey);
  2350. wbio = BIO_new(BIO_s_mem());
  2351. AssertNotNull(wbio);
  2352. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  2353. BIO_gets(wbio, line, sizeof(line));
  2354. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  2355. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2356. BIO_gets(wbio, line, sizeof(line));
  2357. strcpy(line1, " Modulus:\n");
  2358. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2359. BIO_gets(wbio, line, sizeof(line));
  2360. strcpy(line1, " 00:BC:73:0E:A8:49:F3:74:A2:A9:EF:18:A5:DA:55:\n");
  2361. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2362. /* skip to the end of modulus element*/
  2363. for( i = 0; i < 8 ;i++) {
  2364. BIO_gets(wbio, line, sizeof(line));
  2365. }
  2366. BIO_gets(wbio, line, sizeof(line));
  2367. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  2368. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2369. /* should reach EOF */
  2370. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  2371. EVP_PKEY_free(pkey);
  2372. pkey = NULL;
  2373. BIO_free(rbio);
  2374. BIO_free(wbio);
  2375. rbio = NULL;
  2376. wbio = NULL;
  2377. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  2378. /*
  2379. * test DSA public key print
  2380. */
  2381. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  2382. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  2383. sizeof_dsa_pub_key_der_2048);
  2384. AssertNotNull(rbio);
  2385. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2386. AssertNotNull(pkey);
  2387. wbio = BIO_new(BIO_s_mem());
  2388. AssertNotNull(wbio);
  2389. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  2390. BIO_gets(wbio, line, sizeof(line));
  2391. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  2392. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2393. BIO_gets(wbio, line, sizeof(line));
  2394. strcpy(line1, "pub:\n");
  2395. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2396. BIO_gets(wbio, line, sizeof(line));
  2397. strcpy(line1,
  2398. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  2399. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2400. /* skip to the end of pub element*/
  2401. for( i = 0; i < 17 ;i++) {
  2402. BIO_gets(wbio, line, sizeof(line));
  2403. }
  2404. BIO_gets(wbio, line, sizeof(line));
  2405. strcpy(line1, "P:\n");
  2406. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2407. /* skip to the end of P element*/
  2408. for( i = 0; i < 18 ;i++) {
  2409. BIO_gets(wbio, line, sizeof(line));
  2410. }
  2411. BIO_gets(wbio, line, sizeof(line));
  2412. strcpy(line1, "Q:\n");
  2413. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2414. /* skip to the end of Q element*/
  2415. for( i = 0; i < 3 ;i++) {
  2416. BIO_gets(wbio, line, sizeof(line));
  2417. }
  2418. BIO_gets(wbio, line, sizeof(line));
  2419. strcpy(line1, "G:\n");
  2420. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2421. /* skip to the end of G element*/
  2422. for( i = 0; i < 18 ;i++) {
  2423. BIO_gets(wbio, line, sizeof(line));
  2424. }
  2425. /* should reach EOF */
  2426. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  2427. EVP_PKEY_free(pkey);
  2428. pkey = NULL;
  2429. BIO_free(rbio);
  2430. BIO_free(wbio);
  2431. rbio = NULL;
  2432. wbio = NULL;
  2433. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  2434. /*
  2435. * test ECC public key print
  2436. */
  2437. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  2438. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  2439. sizeof_ecc_clikeypub_der_256);
  2440. AssertNotNull(rbio);
  2441. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2442. AssertNotNull(pkey);
  2443. wbio = BIO_new(BIO_s_mem());
  2444. AssertNotNull(wbio);
  2445. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  2446. BIO_gets(wbio, line, sizeof(line));
  2447. strcpy(line1, "Public-Key: (256 bit)\n");
  2448. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2449. BIO_gets(wbio, line, sizeof(line));
  2450. strcpy(line1, "pub:\n");
  2451. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2452. BIO_gets(wbio, line, sizeof(line));
  2453. strcpy(line1,
  2454. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  2455. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2456. /* skip to the end of pub element*/
  2457. for( i = 0; i < 4 ;i++) {
  2458. BIO_gets(wbio, line, sizeof(line));
  2459. }
  2460. BIO_gets(wbio, line, sizeof(line));
  2461. strcpy(line1, "ASN1 OID: prime256v1\n");
  2462. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2463. BIO_gets(wbio, line, sizeof(line));
  2464. strcpy(line1, "NIST CURVE: P-256\n");
  2465. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2466. /* should reach EOF */
  2467. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  2468. EVP_PKEY_free(pkey);
  2469. pkey = NULL;
  2470. BIO_free(rbio);
  2471. BIO_free(wbio);
  2472. rbio = NULL;
  2473. wbio = NULL;
  2474. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  2475. /*
  2476. * test DH public key print
  2477. */
  2478. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  2479. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  2480. sizeof_dh_pub_key_der_2048);
  2481. AssertNotNull(rbio);
  2482. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2483. AssertNotNull(pkey);
  2484. wbio = BIO_new(BIO_s_mem());
  2485. AssertNotNull(wbio);
  2486. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  2487. BIO_gets(wbio, line, sizeof(line));
  2488. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  2489. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2490. BIO_gets(wbio, line, sizeof(line));
  2491. strcpy(line1, "public-key:\n");
  2492. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2493. BIO_gets(wbio, line, sizeof(line));
  2494. strcpy(line1,
  2495. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  2496. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2497. /* skip to the end of public-key element*/
  2498. for( i = 0; i < 17 ;i++) {
  2499. BIO_gets(wbio, line, sizeof(line));
  2500. }
  2501. BIO_gets(wbio, line, sizeof(line));
  2502. strcpy(line1, "prime:\n");
  2503. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2504. BIO_gets(wbio, line, sizeof(line));
  2505. strcpy(line1,
  2506. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  2507. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2508. /* skip to the end of prime element*/
  2509. for( i = 0; i < 17 ;i++) {
  2510. BIO_gets(wbio, line, sizeof(line));
  2511. }
  2512. BIO_gets(wbio, line, sizeof(line));
  2513. strcpy(line1, "generator: 2 (0x02)\n");
  2514. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2515. /* should reach EOF */
  2516. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  2517. EVP_PKEY_free(pkey);
  2518. pkey = NULL;
  2519. BIO_free(rbio);
  2520. BIO_free(wbio);
  2521. rbio = NULL;
  2522. wbio = NULL;
  2523. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  2524. /* to prevent "unused variable" warning */
  2525. (void)pkey;
  2526. (void)wbio;
  2527. (void)rbio;
  2528. (void)line;
  2529. (void)line1;
  2530. (void)i;
  2531. printf(resultFmt, passed);
  2532. #endif /* OPENSSL_EXTRA */
  2533. }
  2534. /* Test functions for base64 encode/decode */
  2535. static void test_wolfSSL_EVP_ENCODE_CTX_new(void)
  2536. {
  2537. #if defined(OPENSSL_EXTRA) && \
  2538. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  2539. printf(testingFmt, "EVP_ENCODE_CTX_new()");
  2540. EVP_ENCODE_CTX* ctx = NULL;
  2541. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  2542. AssertIntEQ( ctx->remaining,0);
  2543. AssertIntEQ( ctx->data[0],0);
  2544. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  2545. EVP_ENCODE_CTX_free(ctx);
  2546. printf(resultFmt, passed);
  2547. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  2548. }
  2549. static void test_wolfSSL_EVP_ENCODE_CTX_free(void)
  2550. {
  2551. #if defined(OPENSSL_EXTRA) && \
  2552. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  2553. printf(testingFmt, "EVP_ENCODE_CTX_free()");
  2554. EVP_ENCODE_CTX* ctx = NULL;
  2555. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  2556. EVP_ENCODE_CTX_free(ctx);
  2557. printf(resultFmt, passed);
  2558. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  2559. }
  2560. static void test_wolfSSL_EVP_EncodeInit(void)
  2561. {
  2562. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  2563. printf(testingFmt, "EVP_EncodeInit()");
  2564. EVP_ENCODE_CTX* ctx = NULL;
  2565. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  2566. AssertIntEQ( ctx->remaining,0);
  2567. AssertIntEQ( ctx->data[0],0);
  2568. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  2569. /* make ctx dirty */
  2570. ctx->remaining = 10;
  2571. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  2572. EVP_EncodeInit(ctx);
  2573. AssertIntEQ( ctx->remaining,0);
  2574. AssertIntEQ( ctx->data[0],0);
  2575. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  2576. EVP_ENCODE_CTX_free(ctx);
  2577. printf(resultFmt, passed);
  2578. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  2579. }
  2580. static void test_wolfSSL_EVP_EncodeUpdate(void)
  2581. {
  2582. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  2583. printf(testingFmt, "EVP_EncodeUpdate()");
  2584. int outl;
  2585. int total;
  2586. const unsigned char plain0[] = {"Th"};
  2587. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  2588. const unsigned char plain2[] = {"This is additional data."};
  2589. const unsigned char enc0[] = {"VGg=\n"};
  2590. /* expected encoded result for the first output 64 chars plus trailing LF*/
  2591. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  2592. const unsigned char enc2[] =
  2593. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  2594. unsigned char encOutBuff[300];
  2595. EVP_ENCODE_CTX* ctx = NULL;
  2596. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  2597. EVP_EncodeInit(ctx);
  2598. /* illegal parameter test */
  2599. AssertIntEQ(
  2600. EVP_EncodeUpdate(
  2601. NULL, /* pass NULL as ctx */
  2602. encOutBuff,
  2603. &outl,
  2604. plain1,
  2605. sizeof(plain1)-1),
  2606. 0 /* expected result code 0: fail */
  2607. );
  2608. AssertIntEQ(
  2609. EVP_EncodeUpdate(
  2610. ctx,
  2611. NULL, /* pass NULL as out buff */
  2612. &outl,
  2613. plain1,
  2614. sizeof(plain1)-1),
  2615. 0 /* expected result code 0: fail */
  2616. );
  2617. AssertIntEQ(
  2618. EVP_EncodeUpdate(
  2619. ctx,
  2620. encOutBuff,
  2621. NULL, /* pass NULL as outl */
  2622. plain1,
  2623. sizeof(plain1)-1),
  2624. 0 /* expected result code 0: fail */
  2625. );
  2626. AssertIntEQ(
  2627. EVP_EncodeUpdate(
  2628. ctx,
  2629. encOutBuff,
  2630. &outl,
  2631. NULL, /* pass NULL as in */
  2632. sizeof(plain1)-1),
  2633. 0 /* expected result code 0: fail */
  2634. );
  2635. /* meaningless parameter test */
  2636. AssertIntEQ(
  2637. EVP_EncodeUpdate(
  2638. ctx,
  2639. encOutBuff,
  2640. &outl,
  2641. plain1,
  2642. 0), /* pass zero input */
  2643. 1 /* expected result code 1: success */
  2644. );
  2645. /* very small data encoding test */
  2646. EVP_EncodeInit(ctx);
  2647. AssertIntEQ(
  2648. EVP_EncodeUpdate(
  2649. ctx,
  2650. encOutBuff,
  2651. &outl,
  2652. plain0,
  2653. sizeof(plain0)-1),
  2654. 1 /* expected result code 1: success */
  2655. );
  2656. AssertIntEQ(outl,0);
  2657. EVP_EncodeFinal(
  2658. ctx,
  2659. encOutBuff + outl,
  2660. &outl);
  2661. AssertIntEQ( outl, sizeof(enc0)-1);
  2662. AssertIntEQ(
  2663. XSTRNCMP(
  2664. (const char*)encOutBuff,
  2665. (const char*)enc0,sizeof(enc0) ),
  2666. 0);
  2667. /* pass small size( < 48bytes ) input, then make sure they are not
  2668. * encoded and just stored in ctx
  2669. */
  2670. EVP_EncodeInit(ctx);
  2671. total = 0;
  2672. outl = 0;
  2673. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  2674. AssertIntEQ(
  2675. EVP_EncodeUpdate(
  2676. ctx,
  2677. encOutBuff, /* buffer for output */
  2678. &outl, /* size of output */
  2679. plain1, /* input */
  2680. sizeof(plain1)-1), /* size of input */
  2681. 1); /* expected result code 1:success */
  2682. total += outl;
  2683. AssertIntEQ(outl, 0); /* no output expected */
  2684. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  2685. AssertTrue(
  2686. XSTRNCMP((const char*)(ctx->data),
  2687. (const char*)plain1,
  2688. ctx->remaining) ==0 );
  2689. AssertTrue(encOutBuff[0] == 0);
  2690. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  2691. * the stored data and the new input together
  2692. */
  2693. AssertIntEQ(
  2694. EVP_EncodeUpdate(
  2695. ctx,
  2696. encOutBuff + outl, /* buffer for output */
  2697. &outl, /* size of output */
  2698. plain2, /* additional input */
  2699. sizeof(plain2) -1), /* size of additional input */
  2700. 1); /* expected result code 1:success */
  2701. total += outl;
  2702. AssertIntNE(outl, 0); /* some output is expected this time*/
  2703. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  2704. AssertIntEQ(
  2705. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  2706. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  2707. EVP_EncodeFinal(
  2708. ctx,
  2709. encOutBuff + outl,
  2710. &outl);
  2711. total += outl;
  2712. AssertIntNE(total,0);
  2713. AssertIntNE(outl,0);
  2714. AssertIntEQ(XSTRNCMP(
  2715. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  2716. /* test with illeagal parameters */
  2717. outl = 1;
  2718. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  2719. AssertIntEQ(outl, 0);
  2720. outl = 1;
  2721. EVP_EncodeFinal(ctx, NULL, &outl);
  2722. AssertIntEQ(outl, 0);
  2723. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  2724. EVP_EncodeFinal(NULL, NULL, NULL);
  2725. EVP_ENCODE_CTX_free(ctx);
  2726. printf(resultFmt, passed);
  2727. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  2728. }
  2729. static void test_wolfSSL_EVP_EncodeFinal(void)
  2730. {
  2731. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  2732. printf(testingFmt, "wolfSSL_EVP_EncodeFinal()");
  2733. /* tests for wolfSSL_EVP_EncodeFinal are included in
  2734. * test_wolfSSL_EVP_EncodeUpdate
  2735. */
  2736. printf(resultFmt, passed);
  2737. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  2738. }
  2739. static void test_wolfSSL_EVP_DecodeInit(void)
  2740. {
  2741. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  2742. printf(testingFmt, "EVP_DecodeInit()");
  2743. EVP_ENCODE_CTX* ctx = NULL;
  2744. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  2745. AssertIntEQ( ctx->remaining,0);
  2746. AssertIntEQ( ctx->data[0],0);
  2747. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  2748. /* make ctx dirty */
  2749. ctx->remaining = 10;
  2750. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  2751. EVP_DecodeInit(ctx);
  2752. AssertIntEQ( ctx->remaining,0);
  2753. AssertIntEQ( ctx->data[0],0);
  2754. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  2755. EVP_ENCODE_CTX_free(ctx);
  2756. printf(resultFmt, passed);
  2757. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  2758. }
  2759. static void test_wolfSSL_EVP_DecodeUpdate(void)
  2760. {
  2761. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  2762. printf(testingFmt, "EVP_DecodeUpdate()");
  2763. int outl;
  2764. unsigned char decOutBuff[300];
  2765. EVP_ENCODE_CTX* ctx = EVP_ENCODE_CTX_new();
  2766. EVP_DecodeInit(ctx);
  2767. const unsigned char enc1[] =
  2768. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  2769. /* const unsigned char plain1[] =
  2770. {"This is a base64 decoding test."} */
  2771. /* illegal parameter tests */
  2772. /* pass NULL as ctx */
  2773. AssertIntEQ(
  2774. EVP_DecodeUpdate(
  2775. NULL, /* pass NULL as ctx */
  2776. decOutBuff,
  2777. &outl,
  2778. enc1,
  2779. sizeof(enc1)-1),
  2780. -1 /* expected result code -1: fail */
  2781. );
  2782. AssertIntEQ( outl, 0);
  2783. /* pass NULL as output */
  2784. AssertIntEQ(
  2785. EVP_DecodeUpdate(
  2786. ctx,
  2787. NULL, /* pass NULL as out buff */
  2788. &outl,
  2789. enc1,
  2790. sizeof(enc1)-1),
  2791. -1 /* expected result code -1: fail */
  2792. );
  2793. AssertIntEQ( outl, 0);
  2794. /* pass NULL as outl */
  2795. AssertIntEQ(
  2796. EVP_DecodeUpdate(
  2797. ctx,
  2798. decOutBuff,
  2799. NULL, /* pass NULL as outl */
  2800. enc1,
  2801. sizeof(enc1)-1),
  2802. -1 /* expected result code -1: fail */
  2803. );
  2804. /* pass NULL as input */
  2805. AssertIntEQ(
  2806. EVP_DecodeUpdate(
  2807. ctx,
  2808. decOutBuff,
  2809. &outl,
  2810. NULL, /* pass NULL as in */
  2811. sizeof(enc1)-1),
  2812. -1 /* expected result code -1: fail */
  2813. );
  2814. AssertIntEQ( outl, 0);
  2815. /* pass zero length input */
  2816. AssertIntEQ(
  2817. EVP_DecodeUpdate(
  2818. ctx,
  2819. decOutBuff,
  2820. &outl,
  2821. enc1,
  2822. 0), /* pass zero as input len */
  2823. 1 /* expected result code 1: success */
  2824. );
  2825. /* decode correct base64 string */
  2826. const unsigned char enc2[] =
  2827. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  2828. const unsigned char plain2[] =
  2829. {"This is a base64 decoding test."};
  2830. EVP_EncodeInit(ctx);
  2831. AssertIntEQ(
  2832. EVP_DecodeUpdate(
  2833. ctx,
  2834. decOutBuff,
  2835. &outl,
  2836. enc2,
  2837. sizeof(enc2)-1),
  2838. 0 /* expected result code 0: success */
  2839. );
  2840. AssertIntEQ(outl,sizeof(plain2) -1);
  2841. AssertIntEQ(
  2842. EVP_DecodeFinal(
  2843. ctx,
  2844. decOutBuff + outl,
  2845. &outl),
  2846. 1 /* expected result code 1: success */
  2847. );
  2848. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  2849. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  2850. sizeof(plain2) -1 ),0);
  2851. /* decode correct base64 string which does not have '\n' in its last*/
  2852. const unsigned char enc3[] =
  2853. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  2854. const unsigned char plain3[] =
  2855. {"This is a base64 decoding test."}; /* 31 chars */
  2856. EVP_EncodeInit(ctx);
  2857. AssertIntEQ(
  2858. EVP_DecodeUpdate(
  2859. ctx,
  2860. decOutBuff,
  2861. &outl,
  2862. enc3,
  2863. sizeof(enc3)-1),
  2864. 0 /* expected result code 0: success */
  2865. );
  2866. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  2867. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  2868. sizeof(plain3) -1 ),0);
  2869. AssertIntEQ(
  2870. EVP_DecodeFinal(
  2871. ctx,
  2872. decOutBuff + outl,
  2873. &outl),
  2874. 1 /* expected result code 1: success */
  2875. );
  2876. AssertIntEQ(outl,0 );
  2877. /* decode string which has a padding char ('=') in the illegal position*/
  2878. const unsigned char enc4[] =
  2879. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  2880. EVP_EncodeInit(ctx);
  2881. AssertIntEQ(
  2882. EVP_DecodeUpdate(
  2883. ctx,
  2884. decOutBuff,
  2885. &outl,
  2886. enc4,
  2887. sizeof(enc4)-1),
  2888. -1 /* expected result code -1: error */
  2889. );
  2890. AssertIntEQ(outl,0);
  2891. /* small data decode test */
  2892. const unsigned char enc00[] = {"VG"};
  2893. const unsigned char enc01[] = {"g=\n"};
  2894. const unsigned char plain4[] = {"Th"};
  2895. EVP_EncodeInit(ctx);
  2896. AssertIntEQ(
  2897. EVP_DecodeUpdate(
  2898. ctx,
  2899. decOutBuff,
  2900. &outl,
  2901. enc00,
  2902. sizeof(enc00)-1),
  2903. 1 /* expected result code 1: success */
  2904. );
  2905. AssertIntEQ(outl,0);
  2906. AssertIntEQ(
  2907. EVP_DecodeUpdate(
  2908. ctx,
  2909. decOutBuff + outl,
  2910. &outl,
  2911. enc01,
  2912. sizeof(enc01)-1),
  2913. 0 /* expected result code 0: success */
  2914. );
  2915. AssertIntEQ(outl,sizeof(plain4)-1);
  2916. /* test with illegal parameters */
  2917. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  2918. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  2919. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  2920. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  2921. EVP_DecodeFinal(
  2922. ctx,
  2923. decOutBuff + outl,
  2924. &outl);
  2925. AssertIntEQ( outl, 0);
  2926. AssertIntEQ(
  2927. XSTRNCMP(
  2928. (const char*)decOutBuff,
  2929. (const char*)plain4,sizeof(plain4)-1 ),
  2930. 0);
  2931. EVP_ENCODE_CTX_free(ctx);
  2932. printf(resultFmt, passed);
  2933. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  2934. }
  2935. static void test_wolfSSL_EVP_DecodeFinal(void)
  2936. {
  2937. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  2938. printf(testingFmt, "EVP_DecodeFinal()");
  2939. /* tests for wolfSSL_EVP_DecodeFinal are included in
  2940. * test_wolfSSL_EVP_DecodeUpdate
  2941. */
  2942. printf(resultFmt, passed);
  2943. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  2944. }
  2945. /* Test function for wolfSSL_EVP_get_cipherbynid.
  2946. */
  2947. #ifdef OPENSSL_EXTRA
  2948. static void test_wolfSSL_EVP_get_cipherbynid(void)
  2949. {
  2950. #ifndef NO_AES
  2951. const WOLFSSL_EVP_CIPHER* c;
  2952. c = wolfSSL_EVP_get_cipherbynid(419);
  2953. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  2954. defined(WOLFSSL_AES_128)
  2955. AssertNotNull(c);
  2956. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  2957. #else
  2958. AssertNull(c);
  2959. #endif
  2960. c = wolfSSL_EVP_get_cipherbynid(423);
  2961. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  2962. defined(WOLFSSL_AES_192)
  2963. AssertNotNull(c);
  2964. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  2965. #else
  2966. AssertNull(c);
  2967. #endif
  2968. c = wolfSSL_EVP_get_cipherbynid(427);
  2969. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  2970. defined(WOLFSSL_AES_256)
  2971. AssertNotNull(c);
  2972. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  2973. #else
  2974. AssertNull(c);
  2975. #endif
  2976. c = wolfSSL_EVP_get_cipherbynid(904);
  2977. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  2978. AssertNotNull(c);
  2979. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  2980. #else
  2981. AssertNull(c);
  2982. #endif
  2983. c = wolfSSL_EVP_get_cipherbynid(905);
  2984. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  2985. AssertNotNull(c);
  2986. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  2987. #else
  2988. AssertNull(c);
  2989. #endif
  2990. c = wolfSSL_EVP_get_cipherbynid(906);
  2991. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  2992. AssertNotNull(c);
  2993. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  2994. #else
  2995. AssertNull(c);
  2996. #endif
  2997. c = wolfSSL_EVP_get_cipherbynid(418);
  2998. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  2999. AssertNotNull(c);
  3000. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  3001. #else
  3002. AssertNull(c);
  3003. #endif
  3004. c = wolfSSL_EVP_get_cipherbynid(422);
  3005. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  3006. AssertNotNull(c);
  3007. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  3008. #else
  3009. AssertNull(c);
  3010. #endif
  3011. c = wolfSSL_EVP_get_cipherbynid(426);
  3012. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  3013. AssertNotNull(c);
  3014. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  3015. #else
  3016. AssertNull(c);
  3017. #endif
  3018. #endif /* !NO_AES */
  3019. #ifndef NO_DES3
  3020. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  3021. #ifdef WOLFSSL_DES_ECB
  3022. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  3023. #endif
  3024. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  3025. #ifdef WOLFSSL_DES_ECB
  3026. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  3027. #endif
  3028. #endif /* !NO_DES3 */
  3029. #ifdef HAVE_IDEA
  3030. AssertNotNull(strcmp("EVP_IDEA_CBC", wolfSSL_EVP_get_cipherbynid(34)));
  3031. #endif
  3032. /* test for nid is out of range */
  3033. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  3034. }
  3035. static void test_wolfSSL_EVP_CIPHER_CTX(void)
  3036. {
  3037. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  3038. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  3039. const EVP_CIPHER *init = EVP_aes_128_cbc();
  3040. const EVP_CIPHER *test;
  3041. byte key[AES_BLOCK_SIZE] = {0};
  3042. byte iv[AES_BLOCK_SIZE] = {0};
  3043. AssertNotNull(ctx);
  3044. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  3045. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  3046. test = EVP_CIPHER_CTX_cipher(ctx);
  3047. AssertTrue(init == test);
  3048. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  3049. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  3050. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  3051. EVP_CIPHER_CTX_free(ctx);
  3052. /* test EVP_CIPHER_CTX_cleanup with NULL */
  3053. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  3054. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  3055. }
  3056. #endif /* OPENSSL_EXTRA */
  3057. /*----------------------------------------------------------------------------*
  3058. | IO
  3059. *----------------------------------------------------------------------------*/
  3060. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  3061. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  3062. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  3063. #define HAVE_IO_TESTS_DEPENDENCIES
  3064. #endif
  3065. /* helper functions */
  3066. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  3067. #ifdef WOLFSSL_SESSION_EXPORT
  3068. /* set up function for sending session information */
  3069. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  3070. {
  3071. WOLFSSL_CTX* ctx;
  3072. WOLFSSL* ssl;
  3073. AssertNotNull(inSsl);
  3074. AssertNotNull(buf);
  3075. AssertIntNE(0, sz);
  3076. /* Set ctx to DTLS 1.2 */
  3077. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  3078. AssertNotNull(ctx);
  3079. ssl = wolfSSL_new(ctx);
  3080. AssertNotNull(ssl);
  3081. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  3082. wolfSSL_free(ssl);
  3083. wolfSSL_CTX_free(ctx);
  3084. (void)userCtx;
  3085. return WOLFSSL_SUCCESS;
  3086. }
  3087. /* returns negative value on fail and positive (including 0) on success */
  3088. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  3089. {
  3090. int ret, err, loop_count, count, timeout = 10;
  3091. char msg[] = "I hear you fa shizzle!";
  3092. char input[1024];
  3093. loop_count = ((func_args*)args)->argc;
  3094. do {
  3095. #ifdef WOLFSSL_ASYNC_CRYPT
  3096. if (err == WC_PENDING_E) {
  3097. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3098. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3099. }
  3100. #endif
  3101. err = 0; /* Reset error */
  3102. ret = wolfSSL_accept(ssl);
  3103. if (ret != WOLFSSL_SUCCESS) {
  3104. err = wolfSSL_get_error(ssl, 0);
  3105. if (err == WOLFSSL_ERROR_WANT_READ ||
  3106. err == WOLFSSL_ERROR_WANT_WRITE) {
  3107. int select_ret;
  3108. err = WC_PENDING_E;
  3109. select_ret = tcp_select(*sockfd, timeout);
  3110. if (select_ret == TEST_TIMEOUT) {
  3111. return WOLFSSL_FATAL_ERROR;
  3112. }
  3113. }
  3114. }
  3115. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  3116. if (ret != WOLFSSL_SUCCESS) {
  3117. char buff[WOLFSSL_MAX_ERROR_SZ];
  3118. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3119. return ret;
  3120. }
  3121. for (count = 0; count < loop_count; count++) {
  3122. int select_ret;
  3123. select_ret = tcp_select(*sockfd, timeout);
  3124. if (select_ret == TEST_TIMEOUT) {
  3125. ret = WOLFSSL_FATAL_ERROR;
  3126. break;
  3127. }
  3128. do {
  3129. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  3130. if (ret > 0) {
  3131. input[ret] = '\0';
  3132. printf("Client message: %s\n", input);
  3133. }
  3134. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3135. do {
  3136. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  3137. return WOLFSSL_FATAL_ERROR;
  3138. }
  3139. err = wolfSSL_get_error(ssl, ret);
  3140. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3141. }
  3142. return ret;
  3143. }
  3144. #endif /* WOLFSSL_SESSION_EXPORT */
  3145. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  3146. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  3147. defined(HAVE_AES_CBC)
  3148. typedef struct openssl_key_ctx {
  3149. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  3150. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  3151. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  3152. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  3153. } openssl_key_ctx;
  3154. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  3155. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  3156. static WC_INLINE int OpenSSLTicketInit(void)
  3157. {
  3158. int ret = wc_InitRng(&myOpenSSLKey_rng);
  3159. if (ret != 0) return ret;
  3160. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  3161. sizeof(myOpenSSLKey_ctx.name));
  3162. if (ret != 0) return ret;
  3163. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  3164. sizeof(myOpenSSLKey_ctx.key));
  3165. if (ret != 0) return ret;
  3166. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  3167. sizeof(myOpenSSLKey_ctx.hmacKey));
  3168. if (ret != 0) return ret;
  3169. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  3170. sizeof(myOpenSSLKey_ctx.iv));
  3171. if (ret != 0) return ret;
  3172. return 0;
  3173. }
  3174. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  3175. byte name[WOLFSSL_TICKET_NAME_SZ],
  3176. byte iv[WOLFSSL_TICKET_IV_SZ],
  3177. WOLFSSL_EVP_CIPHER_CTX *ectx,
  3178. WOLFSSL_HMAC_CTX *hctx, int enc) {
  3179. (void)ssl;
  3180. if (enc) {
  3181. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  3182. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  3183. }
  3184. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  3185. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  3186. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  3187. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  3188. return 0;
  3189. }
  3190. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  3191. if (enc)
  3192. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3193. else
  3194. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3195. return 1;
  3196. }
  3197. static WC_INLINE void OpenSSLTicketCleanup(void)
  3198. {
  3199. wc_FreeRng(&myOpenSSLKey_rng);
  3200. }
  3201. #endif
  3202. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  3203. {
  3204. SOCKET_T sockfd = 0;
  3205. SOCKET_T clientfd = 0;
  3206. word16 port;
  3207. callback_functions* cbf;
  3208. WOLFSSL_CTX* ctx = 0;
  3209. WOLFSSL* ssl = 0;
  3210. char msg[] = "I hear you fa shizzle!";
  3211. char input[1024];
  3212. int idx;
  3213. int ret, err = 0;
  3214. int sharedCtx = 0;
  3215. #ifdef WOLFSSL_TIRTOS
  3216. fdOpenSession(Task_self());
  3217. #endif
  3218. ((func_args*)args)->return_code = TEST_FAIL;
  3219. cbf = ((func_args*)args)->callbacks;
  3220. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3221. if (cbf != NULL && cbf->ctx) {
  3222. ctx = cbf->ctx;
  3223. sharedCtx = 1;
  3224. }
  3225. else
  3226. #endif
  3227. {
  3228. WOLFSSL_METHOD* method = NULL;
  3229. if (cbf != NULL && cbf->method != NULL) {
  3230. method = cbf->method();
  3231. }
  3232. else {
  3233. method = wolfSSLv23_server_method();
  3234. }
  3235. ctx = wolfSSL_CTX_new(method);
  3236. }
  3237. if (ctx == NULL) {
  3238. goto done;
  3239. }
  3240. #if defined(HAVE_SESSION_TICKET) && \
  3241. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  3242. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  3243. OpenSSLTicketInit();
  3244. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  3245. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  3246. TicketInit();
  3247. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  3248. #endif
  3249. #endif
  3250. #if defined(USE_WINDOWS_API)
  3251. port = ((func_args*)args)->signal->port;
  3252. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  3253. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  3254. /* Let tcp_listen assign port */
  3255. port = 0;
  3256. #else
  3257. /* Use default port */
  3258. port = wolfSSLPort;
  3259. #endif
  3260. /* do it here to detect failure */
  3261. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  3262. CloseSocket(sockfd);
  3263. wolfSSL_CTX_set_verify(ctx,
  3264. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  3265. #ifdef WOLFSSL_ENCRYPTED_KEYS
  3266. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  3267. #endif
  3268. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  3269. != WOLFSSL_SUCCESS) {
  3270. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  3271. goto done;
  3272. }
  3273. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3274. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3275. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3276. #else
  3277. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3278. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3279. #endif
  3280. /*err_sys("can't load server cert chain file, "
  3281. "Please run from wolfSSL home dir");*/
  3282. goto done;
  3283. }
  3284. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3285. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3286. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3287. #else
  3288. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3289. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3290. #endif
  3291. /*err_sys("can't load server key file, "
  3292. "Please run from wolfSSL home dir");*/
  3293. goto done;
  3294. }
  3295. /* call ctx setup callback */
  3296. if (cbf != NULL && cbf->ctx_ready != NULL) {
  3297. cbf->ctx_ready(ctx);
  3298. }
  3299. ssl = wolfSSL_new(ctx);
  3300. if (ssl == NULL) {
  3301. goto done;
  3302. }
  3303. #ifdef WOLFSSL_SESSION_EXPORT
  3304. /* only add in more complex nonblocking case with session export tests */
  3305. if (args && ((func_args*)args)->argc > 0) {
  3306. /* set as nonblock and time out for waiting on read/write */
  3307. tcp_set_nonblocking(&clientfd);
  3308. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  3309. }
  3310. #endif
  3311. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3312. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  3313. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3314. #else
  3315. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  3316. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3317. #endif
  3318. /*err_sys("can't load server cert chain file, "
  3319. "Please run from wolfSSL home dir");*/
  3320. goto done;
  3321. }
  3322. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3323. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  3324. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3325. #else
  3326. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  3327. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3328. #endif
  3329. /*err_sys("can't load server key file, "
  3330. "Please run from wolfSSL home dir");*/
  3331. goto done;
  3332. }
  3333. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  3334. /*err_sys("SSL_set_fd failed");*/
  3335. goto done;
  3336. }
  3337. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  3338. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  3339. #elif !defined(NO_DH)
  3340. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  3341. #endif
  3342. /* call ssl setup callback */
  3343. if (cbf != NULL && cbf->ssl_ready != NULL) {
  3344. cbf->ssl_ready(ssl);
  3345. }
  3346. #ifdef WOLFSSL_SESSION_EXPORT
  3347. /* only add in more complex nonblocking case with session export tests */
  3348. if (((func_args*)args)->argc > 0) {
  3349. ret = nonblocking_accept_read(args, ssl, &clientfd);
  3350. if (ret >= 0) {
  3351. ((func_args*)args)->return_code = TEST_SUCCESS;
  3352. }
  3353. #ifdef WOLFSSL_TIRTOS
  3354. Task_yield();
  3355. #endif
  3356. goto done;
  3357. }
  3358. #endif
  3359. do {
  3360. #ifdef WOLFSSL_ASYNC_CRYPT
  3361. if (err == WC_PENDING_E) {
  3362. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3363. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3364. }
  3365. #endif
  3366. err = 0; /* Reset error */
  3367. ret = wolfSSL_accept(ssl);
  3368. if (ret != WOLFSSL_SUCCESS) {
  3369. err = wolfSSL_get_error(ssl, 0);
  3370. }
  3371. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  3372. if (ret != WOLFSSL_SUCCESS) {
  3373. char buff[WOLFSSL_MAX_ERROR_SZ];
  3374. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3375. /*err_sys("SSL_accept failed");*/
  3376. goto done;
  3377. }
  3378. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  3379. if (idx > 0) {
  3380. input[idx] = '\0';
  3381. printf("Client message: %s\n", input);
  3382. }
  3383. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  3384. /*err_sys("SSL_write failed");*/
  3385. #ifdef WOLFSSL_TIRTOS
  3386. return;
  3387. #else
  3388. return 0;
  3389. #endif
  3390. }
  3391. #ifdef WOLFSSL_TIRTOS
  3392. Task_yield();
  3393. #endif
  3394. ((func_args*)args)->return_code = TEST_SUCCESS;
  3395. done:
  3396. wolfSSL_shutdown(ssl);
  3397. wolfSSL_free(ssl);
  3398. if (!sharedCtx)
  3399. wolfSSL_CTX_free(ctx);
  3400. CloseSocket(clientfd);
  3401. #ifdef WOLFSSL_TIRTOS
  3402. fdCloseSession(Task_self());
  3403. #endif
  3404. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  3405. && defined(HAVE_THREAD_LS)
  3406. wc_ecc_fp_free(); /* free per thread cache */
  3407. #endif
  3408. #if defined(HAVE_SESSION_TICKET) && \
  3409. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  3410. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  3411. OpenSSLTicketCleanup();
  3412. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  3413. TicketCleanup();
  3414. #endif
  3415. #endif
  3416. #ifndef WOLFSSL_TIRTOS
  3417. return 0;
  3418. #endif
  3419. }
  3420. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  3421. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  3422. {
  3423. SOCKET_T sockfd = 0;
  3424. SOCKET_T clientfd = 0;
  3425. word16 port;
  3426. callback_functions* cbf;
  3427. WOLFSSL_CTX* ctx = 0;
  3428. WOLFSSL* ssl = 0;
  3429. char msg[] = "I hear you fa shizzle!";
  3430. char input[1024];
  3431. int idx;
  3432. int ret, err = 0;
  3433. int sharedCtx = 0;
  3434. int loop_count = ((func_args*)args)->argc;
  3435. int count = 0;
  3436. #ifdef WOLFSSL_TIRTOS
  3437. fdOpenSession(Task_self());
  3438. #endif
  3439. ((func_args*)args)->return_code = TEST_FAIL;
  3440. cbf = ((func_args*)args)->callbacks;
  3441. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3442. if (cbf != NULL && cbf->ctx) {
  3443. ctx = cbf->ctx;
  3444. sharedCtx = 1;
  3445. }
  3446. else
  3447. #endif
  3448. {
  3449. WOLFSSL_METHOD* method = NULL;
  3450. if (cbf != NULL && cbf->method != NULL) {
  3451. method = cbf->method();
  3452. }
  3453. else {
  3454. method = wolfSSLv23_server_method();
  3455. }
  3456. ctx = wolfSSL_CTX_new(method);
  3457. }
  3458. #if defined(USE_WINDOWS_API)
  3459. port = ((func_args*)args)->signal->port;
  3460. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  3461. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  3462. /* Let tcp_listen assign port */
  3463. port = 0;
  3464. #else
  3465. /* Use default port */
  3466. port = wolfSSLPort;
  3467. #endif
  3468. wolfSSL_CTX_set_verify(ctx,
  3469. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  3470. #ifdef WOLFSSL_ENCRYPTED_KEYS
  3471. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  3472. #endif
  3473. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  3474. != WOLFSSL_SUCCESS) {
  3475. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  3476. goto done;
  3477. }
  3478. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3479. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3480. /*err_sys("can't load server cert chain file, "
  3481. "Please run from wolfSSL home dir");*/
  3482. goto done;
  3483. }
  3484. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3485. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3486. /*err_sys("can't load server key file, "
  3487. "Please run from wolfSSL home dir");*/
  3488. goto done;
  3489. }
  3490. /* call ctx setup callback */
  3491. if (cbf != NULL && cbf->ctx_ready != NULL) {
  3492. cbf->ctx_ready(ctx);
  3493. }
  3494. while(count != loop_count) {
  3495. ssl = wolfSSL_new(ctx);
  3496. if (ssl == NULL) {
  3497. goto done;
  3498. }
  3499. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  3500. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3501. /*err_sys("can't load server cert chain file, "
  3502. "Please run from wolfSSL home dir");*/
  3503. goto done;
  3504. }
  3505. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  3506. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3507. /*err_sys("can't load server key file, "
  3508. "Please run from wolfSSL home dir");*/
  3509. goto done;
  3510. }
  3511. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  3512. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  3513. #elif !defined(NO_DH)
  3514. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  3515. #endif
  3516. /* call ssl setup callback */
  3517. if (cbf != NULL && cbf->ssl_ready != NULL) {
  3518. cbf->ssl_ready(ssl);
  3519. }
  3520. /* do it here to detect failure */
  3521. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  3522. CloseSocket(sockfd);
  3523. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  3524. /*err_sys("SSL_set_fd failed");*/
  3525. goto done;
  3526. }
  3527. do {
  3528. #ifdef WOLFSSL_ASYNC_CRYPT
  3529. if (err == WC_PENDING_E) {
  3530. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3531. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3532. }
  3533. #endif
  3534. err = 0; /* Reset error */
  3535. ret = wolfSSL_accept(ssl);
  3536. if (ret != WOLFSSL_SUCCESS) {
  3537. err = wolfSSL_get_error(ssl, 0);
  3538. }
  3539. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  3540. if (ret != WOLFSSL_SUCCESS) {
  3541. char buff[WOLFSSL_MAX_ERROR_SZ];
  3542. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3543. /*err_sys("SSL_accept failed");*/
  3544. goto done;
  3545. }
  3546. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  3547. if (idx > 0) {
  3548. input[idx] = '\0';
  3549. printf("Client message: %s\n", input);
  3550. }
  3551. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  3552. /*err_sys("SSL_write failed");*/
  3553. #ifdef WOLFSSL_TIRTOS
  3554. return;
  3555. #else
  3556. return 0;
  3557. #endif
  3558. }
  3559. /* free ssl for this connection */
  3560. wolfSSL_shutdown(ssl);
  3561. wolfSSL_free(ssl); ssl = NULL;
  3562. CloseSocket(clientfd);
  3563. count++;
  3564. }
  3565. #ifdef WOLFSSL_TIRTOS
  3566. Task_yield();
  3567. #endif
  3568. ((func_args*)args)->return_code = TEST_SUCCESS;
  3569. done:
  3570. if(ssl != NULL) {
  3571. wolfSSL_shutdown(ssl);
  3572. wolfSSL_free(ssl);
  3573. }
  3574. if (!sharedCtx)
  3575. wolfSSL_CTX_free(ctx);
  3576. CloseSocket(clientfd);
  3577. #ifdef WOLFSSL_TIRTOS
  3578. fdCloseSession(Task_self());
  3579. #endif
  3580. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  3581. && defined(HAVE_THREAD_LS)
  3582. wc_ecc_fp_free(); /* free per thread cache */
  3583. #endif
  3584. #ifndef WOLFSSL_TIRTOS
  3585. return 0;
  3586. #endif
  3587. }
  3588. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  3589. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  3590. static void test_client_nofail(void* args, cbType cb)
  3591. {
  3592. SOCKET_T sockfd = 0;
  3593. callback_functions* cbf;
  3594. WOLFSSL_CTX* ctx = 0;
  3595. WOLFSSL* ssl = 0;
  3596. WOLFSSL_CIPHER* cipher;
  3597. char msg[64] = "hello wolfssl!";
  3598. char reply[1024];
  3599. int input;
  3600. int msgSz = (int)XSTRLEN(msg);
  3601. int ret, err = 0;
  3602. int cipherSuite;
  3603. int sharedCtx = 0;
  3604. const char* cipherName1, *cipherName2;
  3605. #ifdef WOLFSSL_TIRTOS
  3606. fdOpenSession(Task_self());
  3607. #endif
  3608. ((func_args*)args)->return_code = TEST_FAIL;
  3609. cbf = ((func_args*)args)->callbacks;
  3610. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3611. if (cbf != NULL && cbf->ctx) {
  3612. ctx = cbf->ctx;
  3613. sharedCtx = cbf->isSharedCtx;
  3614. }
  3615. else
  3616. #endif
  3617. {
  3618. WOLFSSL_METHOD* method = NULL;
  3619. if (cbf != NULL && cbf->method != NULL) {
  3620. method = cbf->method();
  3621. }
  3622. else {
  3623. method = wolfSSLv23_client_method();
  3624. }
  3625. ctx = wolfSSL_CTX_new(method);
  3626. }
  3627. #ifdef WOLFSSL_ENCRYPTED_KEYS
  3628. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  3629. #endif
  3630. /* Do connect here so server detects failures */
  3631. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  3632. 0, 0, NULL);
  3633. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  3634. {
  3635. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  3636. goto done;
  3637. }
  3638. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3639. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  3640. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3641. #else
  3642. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  3643. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3644. #endif
  3645. /*err_sys("can't load client cert file, "
  3646. "Please run from wolfSSL home dir");*/
  3647. goto done;
  3648. }
  3649. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3650. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  3651. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3652. #else
  3653. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  3654. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3655. #endif
  3656. /*err_sys("can't load client key file, "
  3657. "Please run from wolfSSL home dir");*/
  3658. goto done;
  3659. }
  3660. /* call ctx setup callback */
  3661. if (cbf != NULL && cbf->ctx_ready != NULL) {
  3662. cbf->ctx_ready(ctx);
  3663. }
  3664. ssl = wolfSSL_new(ctx);
  3665. if (ssl == NULL) {
  3666. goto done;
  3667. }
  3668. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3669. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  3670. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3671. #else
  3672. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  3673. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3674. #endif
  3675. /*err_sys("can't load client cert file, "
  3676. "Please run from wolfSSL home dir");*/
  3677. goto done;
  3678. }
  3679. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3680. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  3681. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3682. #else
  3683. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  3684. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3685. #endif
  3686. /*err_sys("can't load client key file, "
  3687. "Please run from wolfSSL home dir");*/
  3688. goto done;
  3689. }
  3690. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  3691. /*err_sys("SSL_set_fd failed");*/
  3692. goto done;
  3693. }
  3694. /* call ssl setup callback */
  3695. if (cbf != NULL && cbf->ssl_ready != NULL) {
  3696. cbf->ssl_ready(ssl);
  3697. }
  3698. do {
  3699. #ifdef WOLFSSL_ASYNC_CRYPT
  3700. if (err == WC_PENDING_E) {
  3701. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3702. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3703. }
  3704. #endif
  3705. err = 0; /* Reset error */
  3706. ret = wolfSSL_connect(ssl);
  3707. if (ret != WOLFSSL_SUCCESS) {
  3708. err = wolfSSL_get_error(ssl, 0);
  3709. }
  3710. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  3711. if (ret != WOLFSSL_SUCCESS) {
  3712. char buff[WOLFSSL_MAX_ERROR_SZ];
  3713. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3714. /*err_sys("SSL_connect failed");*/
  3715. goto done;
  3716. }
  3717. /* test the various get cipher methods */
  3718. /* Internal cipher suite names */
  3719. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  3720. cipherName1 = wolfSSL_get_cipher_name(ssl);
  3721. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  3722. (cipherSuite >> 8), cipherSuite & 0xFF);
  3723. AssertStrEQ(cipherName1, cipherName2);
  3724. /* IANA Cipher Suites Names */
  3725. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  3726. then it's the internal cipher suite name */
  3727. cipher = wolfSSL_get_current_cipher(ssl);
  3728. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  3729. cipherName2 = wolfSSL_get_cipher(ssl);
  3730. AssertStrEQ(cipherName1, cipherName2);
  3731. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  3732. !defined(WOLFSSL_QT)
  3733. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  3734. (cipherSuite >> 8), cipherSuite & 0xFF);
  3735. AssertStrEQ(cipherName1, cipherName2);
  3736. #endif
  3737. if (cb != NULL)
  3738. (cb)(ctx, ssl);
  3739. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  3740. /*err_sys("SSL_write failed");*/
  3741. goto done;
  3742. }
  3743. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  3744. if (input > 0) {
  3745. reply[input] = '\0';
  3746. printf("Server response: %s\n", reply);
  3747. }
  3748. ((func_args*)args)->return_code = TEST_SUCCESS;
  3749. done:
  3750. wolfSSL_free(ssl);
  3751. if (!sharedCtx)
  3752. wolfSSL_CTX_free(ctx);
  3753. CloseSocket(sockfd);
  3754. #ifdef WOLFSSL_TIRTOS
  3755. fdCloseSession(Task_self());
  3756. #endif
  3757. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  3758. && defined(HAVE_THREAD_LS)
  3759. wc_ecc_fp_free(); /* free per thread cache */
  3760. #endif
  3761. return;
  3762. }
  3763. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  3764. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  3765. {
  3766. SOCKET_T sockfd = 0;
  3767. callback_functions* cbf;
  3768. WOLFSSL_CTX* ctx = 0;
  3769. WOLFSSL* ssl = 0;
  3770. WOLFSSL_SESSION* session = NULL;
  3771. char msg[64] = "hello wolfssl!";
  3772. char reply[1024];
  3773. int input;
  3774. int msgSz = (int)XSTRLEN(msg);
  3775. int ret, err = 0;
  3776. int sharedCtx = 0;
  3777. #ifdef WOLFSSL_TIRTOS
  3778. fdOpenSession(Task_self());
  3779. #endif
  3780. ((func_args*)args)->return_code = TEST_FAIL;
  3781. cbf = ((func_args*)args)->callbacks;
  3782. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3783. if (cbf != NULL && cbf->ctx) {
  3784. ctx = cbf->ctx;
  3785. sharedCtx = 1;
  3786. }
  3787. else
  3788. #endif
  3789. {
  3790. WOLFSSL_METHOD* method = NULL;
  3791. if (cbf != NULL && cbf->method != NULL) {
  3792. method = cbf->method();
  3793. }
  3794. else {
  3795. method = wolfSSLv23_client_method();
  3796. }
  3797. ctx = wolfSSL_CTX_new(method);
  3798. }
  3799. #ifdef WOLFSSL_ENCRYPTED_KEYS
  3800. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  3801. #endif
  3802. /* Do connect here so server detects failures */
  3803. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  3804. 0, 0, NULL);
  3805. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  3806. {
  3807. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  3808. goto done;
  3809. }
  3810. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  3811. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3812. /*err_sys("can't load client cert file, "
  3813. "Please run from wolfSSL home dir");*/
  3814. goto done;
  3815. }
  3816. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  3817. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3818. /*err_sys("can't load client key file, "
  3819. "Please run from wolfSSL home dir");*/
  3820. goto done;
  3821. }
  3822. /* call ctx setup callback */
  3823. if (cbf != NULL && cbf->ctx_ready != NULL) {
  3824. cbf->ctx_ready(ctx);
  3825. }
  3826. ssl = wolfSSL_new(ctx);
  3827. if (ssl == NULL) {
  3828. goto done;
  3829. }
  3830. /* keep handshakre resources for re-using WOLFSSL obj */
  3831. wolfSSL_KeepArrays(ssl);
  3832. if(wolfSSL_KeepHandshakeResources(ssl)) {
  3833. /* err_sys("SSL_KeepHandshakeResources failed"); */
  3834. goto done;
  3835. }
  3836. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  3837. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3838. /*err_sys("can't load client cert file, "
  3839. "Please run from wolfSSL home dir");*/
  3840. goto done;
  3841. }
  3842. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  3843. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3844. /*err_sys("can't load client key file, "
  3845. "Please run from wolfSSL home dir");*/
  3846. goto done;
  3847. }
  3848. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  3849. /*err_sys("SSL_set_fd failed");*/
  3850. goto done;
  3851. }
  3852. /* call ssl setup callback */
  3853. if (cbf != NULL && cbf->ssl_ready != NULL) {
  3854. cbf->ssl_ready(ssl);
  3855. }
  3856. do {
  3857. #ifdef WOLFSSL_ASYNC_CRYPT
  3858. if (err == WC_PENDING_E) {
  3859. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3860. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3861. }
  3862. #endif
  3863. err = 0; /* Reset error */
  3864. ret = wolfSSL_connect(ssl);
  3865. if (ret != WOLFSSL_SUCCESS) {
  3866. err = wolfSSL_get_error(ssl, 0);
  3867. }
  3868. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  3869. if (ret != WOLFSSL_SUCCESS) {
  3870. char buff[WOLFSSL_MAX_ERROR_SZ];
  3871. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3872. /*err_sys("SSL_connect failed");*/
  3873. goto done;
  3874. }
  3875. /* Build first session */
  3876. if (cb != NULL)
  3877. ((cbType)cb)(ctx, ssl);
  3878. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  3879. /*err_sys("SSL_write failed");*/
  3880. goto done;
  3881. }
  3882. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  3883. if (input > 0) {
  3884. reply[input] = '\0';
  3885. printf("Server response: %s\n", reply);
  3886. }
  3887. /* Session Resumption by re-using WOLFSSL object */
  3888. wolfSSL_set_quiet_shutdown(ssl, 1);
  3889. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  3890. /* err_sys ("SSL shutdown failed"); */
  3891. goto done;
  3892. }
  3893. session = wolfSSL_get_session(ssl);
  3894. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  3895. /* err_sys ("SSL_clear failed"); */
  3896. goto done;
  3897. }
  3898. wolfSSL_set_session(ssl, session);
  3899. /* close socket once */
  3900. CloseSocket(sockfd);
  3901. sockfd = 0;
  3902. /* wait until server ready */
  3903. wait_tcp_ready((func_args*)server_args);
  3904. printf("session resumption\n");
  3905. /* Do re-connect */
  3906. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  3907. 0, 0, NULL);
  3908. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  3909. /*err_sys("SSL_set_fd failed");*/
  3910. goto done;
  3911. }
  3912. do {
  3913. #ifdef WOLFSSL_ASYNC_CRYPT
  3914. if (err == WC_PENDING_E) {
  3915. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3916. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3917. }
  3918. #endif
  3919. err = 0; /* Reset error */
  3920. ret = wolfSSL_connect(ssl);
  3921. if (ret != WOLFSSL_SUCCESS) {
  3922. err = wolfSSL_get_error(ssl, 0);
  3923. }
  3924. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  3925. if (ret != WOLFSSL_SUCCESS) {
  3926. char buff[WOLFSSL_MAX_ERROR_SZ];
  3927. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3928. /*err_sys("SSL_connect failed");*/
  3929. goto done;
  3930. }
  3931. /* Build first session */
  3932. if (cb != NULL)
  3933. ((cbType)cb)(ctx, ssl);
  3934. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  3935. /*err_sys("SSL_write failed");*/
  3936. goto done;
  3937. }
  3938. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  3939. if (input > 0) {
  3940. reply[input] = '\0';
  3941. printf("Server response: %s\n", reply);
  3942. }
  3943. ((func_args*)args)->return_code = TEST_SUCCESS;
  3944. done:
  3945. wolfSSL_free(ssl);
  3946. if (!sharedCtx)
  3947. wolfSSL_CTX_free(ctx);
  3948. CloseSocket(sockfd);
  3949. #ifdef WOLFSSL_TIRTOS
  3950. fdCloseSession(Task_self());
  3951. #endif
  3952. return;
  3953. }
  3954. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  3955. static void test_client_verifyDepth(void* args)
  3956. {
  3957. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  3958. SOCKET_T sockfd = 0;
  3959. callback_functions* cbf;
  3960. WOLFSSL_CTX* ctx = 0;
  3961. WOLFSSL* ssl = 0;
  3962. char msg[64] = "hello wolfssl!";
  3963. char reply[1024];
  3964. int input;
  3965. int msgSz = (int)XSTRLEN(msg);
  3966. int ret, err = 0;
  3967. int verify_depth = ((func_args*)args)->argc;
  3968. ((func_args*)args)->return_code = TEST_FAIL;
  3969. cbf = ((func_args*)args)->callbacks;
  3970. {
  3971. WOLFSSL_METHOD* method = NULL;
  3972. if (cbf != NULL && cbf->method != NULL) {
  3973. method = cbf->method();
  3974. }
  3975. else {
  3976. method = wolfSSLv23_client_method();
  3977. }
  3978. ctx = wolfSSL_CTX_new(method);
  3979. }
  3980. /* Do connect here so server detects failures */
  3981. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  3982. 0, 0, NULL);
  3983. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  3984. != WOLFSSL_SUCCESS)
  3985. {
  3986. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  3987. goto done;
  3988. }
  3989. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  3990. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3991. /*err_sys("can't load client cert file, "
  3992. "Please run from wolfSSL home dir");*/
  3993. goto done;
  3994. }
  3995. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  3996. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3997. /*err_sys("can't load client key file, "
  3998. "Please run from wolfSSL home dir");*/
  3999. goto done;
  4000. }
  4001. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  4002. /* set verify depth */
  4003. if (verify_depth == 0) {
  4004. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  4005. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4006. } else if (verify_depth == -1) {
  4007. myVerifyAction = VERIFY_USE_PREVERFIY;
  4008. SSL_CTX_set_verify_depth(ctx, 0);
  4009. } else if (verify_depth > 0) {
  4010. myVerifyAction = VERIFY_USE_PREVERFIY;
  4011. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4012. }
  4013. ssl = wolfSSL_new(ctx);
  4014. if (ssl == NULL) {
  4015. goto done;
  4016. }
  4017. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4018. /*err_sys("SSL_set_fd failed");*/
  4019. goto done;
  4020. }
  4021. do {
  4022. #ifdef WOLFSSL_ASYNC_CRYPT
  4023. if (err == WC_PENDING_E) {
  4024. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4025. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4026. }
  4027. #endif
  4028. err = 0; /* Reset error */
  4029. ret = wolfSSL_connect(ssl);
  4030. if (ret != WOLFSSL_SUCCESS) {
  4031. err = wolfSSL_get_error(ssl, 0);
  4032. }
  4033. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  4034. if (ret != WOLFSSL_SUCCESS) {
  4035. char buff[WOLFSSL_MAX_ERROR_SZ];
  4036. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4037. goto done;
  4038. }
  4039. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4040. goto done;
  4041. }
  4042. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4043. if (input > 0) {
  4044. reply[input] = '\0';
  4045. printf("Server response: %s\n", reply);
  4046. }
  4047. ((func_args*)args)->return_code = TEST_SUCCESS;
  4048. done:
  4049. wolfSSL_free(ssl);
  4050. wolfSSL_CTX_free(ctx);
  4051. CloseSocket(sockfd);
  4052. #else
  4053. (void)args;
  4054. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  4055. }
  4056. /* SNI / ALPN / session export helper functions */
  4057. #if defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLFSSL_SESSION_EXPORT)
  4058. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  4059. {
  4060. callback_functions* callbacks = ((func_args*)args)->callbacks;
  4061. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  4062. WOLFSSL* ssl = NULL;
  4063. SOCKET_T sfd = 0;
  4064. SOCKET_T cfd = 0;
  4065. word16 port;
  4066. char msg[] = "I hear you fa shizzle!";
  4067. int len = (int) XSTRLEN(msg);
  4068. char input[1024];
  4069. int idx;
  4070. int ret, err = 0;
  4071. #ifdef WOLFSSL_TIRTOS
  4072. fdOpenSession(Task_self());
  4073. #endif
  4074. ((func_args*)args)->return_code = TEST_FAIL;
  4075. #if defined(USE_WINDOWS_API)
  4076. port = ((func_args*)args)->signal->port;
  4077. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4078. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4079. /* Let tcp_listen assign port */
  4080. port = 0;
  4081. #else
  4082. /* Use default port */
  4083. port = wolfSSLPort;
  4084. #endif
  4085. wolfSSL_CTX_set_verify(ctx,
  4086. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4087. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4088. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4089. #endif
  4090. #ifdef WOLFSSL_SESSION_EXPORT
  4091. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  4092. #endif
  4093. AssertIntEQ(WOLFSSL_SUCCESS,
  4094. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  4095. AssertIntEQ(WOLFSSL_SUCCESS,
  4096. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4097. WOLFSSL_FILETYPE_PEM));
  4098. AssertIntEQ(WOLFSSL_SUCCESS,
  4099. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  4100. if (callbacks->ctx_ready)
  4101. callbacks->ctx_ready(ctx);
  4102. ssl = wolfSSL_new(ctx);
  4103. if (wolfSSL_dtls(ssl)) {
  4104. SOCKADDR_IN_T cliAddr;
  4105. socklen_t cliLen;
  4106. cliLen = sizeof(cliAddr);
  4107. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  4108. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  4109. (struct sockaddr*)&cliAddr, &cliLen);
  4110. AssertIntGT(idx, 0);
  4111. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4112. }
  4113. else {
  4114. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4115. CloseSocket(sfd);
  4116. }
  4117. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  4118. #ifdef NO_PSK
  4119. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4120. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4121. #elif !defined(NO_DH)
  4122. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4123. #endif
  4124. #endif
  4125. if (callbacks->ssl_ready)
  4126. callbacks->ssl_ready(ssl);
  4127. do {
  4128. #ifdef WOLFSSL_ASYNC_CRYPT
  4129. if (err == WC_PENDING_E) {
  4130. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4131. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4132. }
  4133. #endif
  4134. err = 0; /* Reset error */
  4135. ret = wolfSSL_accept(ssl);
  4136. if (ret != WOLFSSL_SUCCESS) {
  4137. err = wolfSSL_get_error(ssl, 0);
  4138. }
  4139. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  4140. if (ret != WOLFSSL_SUCCESS) {
  4141. char buff[WOLFSSL_MAX_ERROR_SZ];
  4142. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4143. /*err_sys("SSL_accept failed");*/
  4144. }
  4145. else {
  4146. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  4147. input[idx] = 0;
  4148. printf("Client message: %s\n", input);
  4149. }
  4150. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  4151. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL)
  4152. if (wolfSSL_dtls(ssl)) {
  4153. byte* import;
  4154. word32 sz;
  4155. wolfSSL_dtls_export(ssl, NULL, &sz);
  4156. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4157. AssertNotNull(import);
  4158. idx = wolfSSL_dtls_export(ssl, import, &sz);
  4159. AssertIntGE(idx, 0);
  4160. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  4161. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4162. }
  4163. #endif
  4164. #ifdef WOLFSSL_TIRTOS
  4165. Task_yield();
  4166. #endif
  4167. ((func_args*)args)->return_code = TEST_SUCCESS;
  4168. }
  4169. if (callbacks->on_result)
  4170. callbacks->on_result(ssl);
  4171. wolfSSL_shutdown(ssl);
  4172. wolfSSL_free(ssl);
  4173. wolfSSL_CTX_free(ctx);
  4174. CloseSocket(cfd);
  4175. #ifdef WOLFSSL_TIRTOS
  4176. fdCloseSession(Task_self());
  4177. #endif
  4178. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4179. && defined(HAVE_THREAD_LS)
  4180. wc_ecc_fp_free(); /* free per thread cache */
  4181. #endif
  4182. #ifndef WOLFSSL_TIRTOS
  4183. return 0;
  4184. #endif
  4185. }
  4186. static void run_wolfssl_client(void* args)
  4187. {
  4188. callback_functions* callbacks = ((func_args*)args)->callbacks;
  4189. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  4190. WOLFSSL* ssl = NULL;
  4191. SOCKET_T sfd = 0;
  4192. char msg[] = "hello wolfssl server!";
  4193. int len = (int) XSTRLEN(msg);
  4194. char input[1024];
  4195. int idx;
  4196. int ret, err = 0;
  4197. #ifdef WOLFSSL_TIRTOS
  4198. fdOpenSession(Task_self());
  4199. #endif
  4200. ((func_args*)args)->return_code = TEST_FAIL;
  4201. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4202. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4203. #endif
  4204. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4205. AssertIntEQ(WOLFSSL_SUCCESS,
  4206. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM));
  4207. AssertIntEQ(WOLFSSL_SUCCESS,
  4208. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, WOLFSSL_FILETYPE_PEM));
  4209. if (callbacks->ctx_ready)
  4210. callbacks->ctx_ready(ctx);
  4211. ssl = wolfSSL_new(ctx);
  4212. if (wolfSSL_dtls(ssl)) {
  4213. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4214. 1, 0, ssl);
  4215. }
  4216. else {
  4217. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4218. 0, 0, ssl);
  4219. }
  4220. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  4221. if (callbacks->ssl_ready)
  4222. callbacks->ssl_ready(ssl);
  4223. do {
  4224. #ifdef WOLFSSL_ASYNC_CRYPT
  4225. if (err == WC_PENDING_E) {
  4226. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4227. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4228. }
  4229. #endif
  4230. err = 0; /* Reset error */
  4231. ret = wolfSSL_connect(ssl);
  4232. if (ret != WOLFSSL_SUCCESS) {
  4233. err = wolfSSL_get_error(ssl, 0);
  4234. }
  4235. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  4236. if (ret != WOLFSSL_SUCCESS) {
  4237. char buff[WOLFSSL_MAX_ERROR_SZ];
  4238. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4239. /*err_sys("SSL_connect failed");*/
  4240. }
  4241. else {
  4242. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  4243. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  4244. input[idx] = 0;
  4245. printf("Server response: %s\n", input);
  4246. }
  4247. ((func_args*)args)->return_code = TEST_SUCCESS;
  4248. }
  4249. if (callbacks->on_result)
  4250. callbacks->on_result(ssl);
  4251. wolfSSL_free(ssl);
  4252. wolfSSL_CTX_free(ctx);
  4253. CloseSocket(sfd);
  4254. #ifdef WOLFSSL_TIRTOS
  4255. fdCloseSession(Task_self());
  4256. #endif
  4257. }
  4258. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) ||
  4259. defined(WOLFSSL_SESSION_EXPORT) */
  4260. static void test_wolfSSL_read_write(void)
  4261. {
  4262. /* The unit testing for read and write shall happen simultaneously, since
  4263. * one can't do anything with one without the other. (Except for a failure
  4264. * test case.) This function will call all the others that will set up,
  4265. * execute, and report their test findings.
  4266. *
  4267. * Set up the success case first. This function will become the template
  4268. * for the other tests. This should eventually be renamed
  4269. *
  4270. * The success case isn't interesting, how can this fail?
  4271. * - Do not give the client context a CA certificate. The connect should
  4272. * fail. Do not need server for this?
  4273. * - Using NULL for the ssl object on server. Do not need client for this.
  4274. * - Using NULL for the ssl object on client. Do not need server for this.
  4275. * - Good ssl objects for client and server. Client write() without server
  4276. * read().
  4277. * - Good ssl objects for client and server. Server write() without client
  4278. * read().
  4279. * - Forgetting the password callback?
  4280. */
  4281. tcp_ready ready;
  4282. func_args client_args;
  4283. func_args server_args;
  4284. THREAD_TYPE serverThread;
  4285. XMEMSET(&client_args, 0, sizeof(func_args));
  4286. XMEMSET(&server_args, 0, sizeof(func_args));
  4287. #ifdef WOLFSSL_TIRTOS
  4288. fdOpenSession(Task_self());
  4289. #endif
  4290. StartTCP();
  4291. InitTcpReady(&ready);
  4292. #if defined(USE_WINDOWS_API)
  4293. /* use RNG to get random port if using windows */
  4294. ready.port = GetRandomPort();
  4295. #endif
  4296. server_args.signal = &ready;
  4297. client_args.signal = &ready;
  4298. start_thread(test_server_nofail, &server_args, &serverThread);
  4299. wait_tcp_ready(&server_args);
  4300. test_client_nofail(&client_args, NULL);
  4301. join_thread(serverThread);
  4302. AssertTrue(client_args.return_code);
  4303. AssertTrue(server_args.return_code);
  4304. FreeTcpReady(&ready);
  4305. #ifdef WOLFSSL_TIRTOS
  4306. fdOpenSession(Task_self());
  4307. #endif
  4308. }
  4309. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4310. static void test_wolfSSL_reuse_WOLFSSLobj(void)
  4311. {
  4312. /* The unit test for session resumption by re-using WOLFSSL object.
  4313. * WOLFSSL object is not cleared after first session. It re-use the obeject
  4314. * for second connection.
  4315. */
  4316. tcp_ready ready;
  4317. func_args client_args;
  4318. func_args server_args;
  4319. THREAD_TYPE serverThread;
  4320. XMEMSET(&client_args, 0, sizeof(func_args));
  4321. XMEMSET(&server_args, 0, sizeof(func_args));
  4322. #ifdef WOLFSSL_TIRTOS
  4323. fdOpenSession(Task_self());
  4324. #endif
  4325. StartTCP();
  4326. InitTcpReady(&ready);
  4327. #if defined(USE_WINDOWS_API)
  4328. /* use RNG to get random port if using windows */
  4329. ready.port = GetRandomPort();
  4330. #endif
  4331. server_args.signal = &ready;
  4332. client_args.signal = &ready;
  4333. /* the var is used for loop number */
  4334. server_args.argc = 2;
  4335. start_thread(test_server_loop, &server_args, &serverThread);
  4336. wait_tcp_ready(&server_args);
  4337. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  4338. join_thread(serverThread);
  4339. AssertTrue(client_args.return_code);
  4340. AssertTrue(server_args.return_code);
  4341. FreeTcpReady(&ready);
  4342. #ifdef WOLFSSL_TIRTOS
  4343. fdOpenSession(Task_self());
  4344. #endif
  4345. }
  4346. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4347. static void test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  4348. {
  4349. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  4350. /* This unit test is to check set verify Depth */
  4351. tcp_ready ready;
  4352. func_args client_args;
  4353. func_args server_args;
  4354. THREAD_TYPE serverThread;
  4355. callback_functions client_cbf;
  4356. XMEMSET(&client_args, 0, sizeof(func_args));
  4357. XMEMSET(&server_args, 0, sizeof(func_args));
  4358. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  4359. printf(testingFmt, "test_wolfSSL_CTX_verifyDepth_ServerClient()\n");
  4360. #ifdef WOLFSSL_TLS13
  4361. client_cbf.method = wolfTLSv1_3_client_method;
  4362. #endif /* WOLFSSL_TLS13 */
  4363. client_args.callbacks = &client_cbf;
  4364. StartTCP();
  4365. InitTcpReady(&ready);
  4366. #if defined(USE_WINDOWS_API)
  4367. /* use RNG to get random port if using windows */
  4368. ready.port = GetRandomPort();
  4369. #endif
  4370. server_args.signal = &ready;
  4371. client_args.signal = &ready;
  4372. /* the var is used for loop number */
  4373. server_args.argc = 1;
  4374. /* test case 1 verify depth is equal to peer chain */
  4375. {
  4376. start_thread(test_server_nofail, &server_args, &serverThread);
  4377. wait_tcp_ready(&server_args);
  4378. /* the var is used for verify depth */
  4379. client_args.argc = 2;
  4380. test_client_verifyDepth(&client_args);
  4381. join_thread(serverThread);
  4382. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  4383. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  4384. }
  4385. /* test case 2
  4386. * verify depth is zero, number of peer's chain is 2.
  4387. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  4388. * callback.
  4389. */
  4390. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  4391. {
  4392. start_thread(test_server_nofail, &server_args, &serverThread);
  4393. wait_tcp_ready(&server_args);
  4394. client_args.argc = 0;
  4395. test_client_verifyDepth(&client_args);
  4396. join_thread(serverThread);
  4397. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  4398. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  4399. }
  4400. /* test case 3
  4401. * verify depth is zero, number of peer's chain is 2
  4402. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  4403. * therefore, handshake becomes failure.
  4404. */
  4405. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  4406. {
  4407. start_thread(test_server_nofail, &server_args, &serverThread);
  4408. wait_tcp_ready(&server_args);
  4409. client_args.argc = -1;
  4410. test_client_verifyDepth(&client_args);
  4411. join_thread(serverThread);
  4412. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  4413. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  4414. }
  4415. FreeTcpReady(&ready);
  4416. printf(resultFmt, passed);
  4417. #else
  4418. (void)test_client_verifyDepth;
  4419. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  4420. }
  4421. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  4422. /* canned export of a session using older version 3 */
  4423. static unsigned char version_3[] = {
  4424. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  4425. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  4426. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  4427. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  4428. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  4429. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4430. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  4431. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  4432. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  4433. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4434. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  4435. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  4436. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4437. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  4438. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  4439. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  4440. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  4441. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  4442. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  4443. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  4444. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4445. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4446. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4447. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4448. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4449. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4450. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4451. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4452. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4453. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4454. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4455. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  4456. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  4457. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  4458. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  4459. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  4460. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  4461. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  4462. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  4463. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  4464. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  4465. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  4466. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  4467. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  4468. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4469. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  4470. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  4471. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  4472. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  4473. 0x2E, 0x31, 0xED, 0x4F
  4474. };
  4475. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  4476. static void test_wolfSSL_dtls_export(void)
  4477. {
  4478. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  4479. tcp_ready ready;
  4480. func_args client_args;
  4481. func_args server_args;
  4482. THREAD_TYPE serverThread;
  4483. callback_functions server_cbf;
  4484. callback_functions client_cbf;
  4485. #ifdef WOLFSSL_TIRTOS
  4486. fdOpenSession(Task_self());
  4487. #endif
  4488. InitTcpReady(&ready);
  4489. #if defined(USE_WINDOWS_API)
  4490. /* use RNG to get random port if using windows */
  4491. ready.port = GetRandomPort();
  4492. #endif
  4493. /* set using dtls */
  4494. XMEMSET(&client_args, 0, sizeof(func_args));
  4495. XMEMSET(&server_args, 0, sizeof(func_args));
  4496. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  4497. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  4498. server_cbf.method = wolfDTLSv1_2_server_method;
  4499. client_cbf.method = wolfDTLSv1_2_client_method;
  4500. server_args.callbacks = &server_cbf;
  4501. client_args.callbacks = &client_cbf;
  4502. server_args.signal = &ready;
  4503. client_args.signal = &ready;
  4504. start_thread(run_wolfssl_server, &server_args, &serverThread);
  4505. wait_tcp_ready(&server_args);
  4506. run_wolfssl_client(&client_args);
  4507. join_thread(serverThread);
  4508. AssertTrue(client_args.return_code);
  4509. AssertTrue(server_args.return_code);
  4510. FreeTcpReady(&ready);
  4511. #ifdef WOLFSSL_TIRTOS
  4512. fdOpenSession(Task_self());
  4513. #endif
  4514. {
  4515. SOCKET_T sockfd = 0;
  4516. WOLFSSL_CTX* ctx;
  4517. WOLFSSL* ssl;
  4518. char msg[64] = "hello wolfssl!";
  4519. char reply[1024];
  4520. int msgSz = (int)XSTRLEN(msg);
  4521. byte *session, *window;
  4522. unsigned int sessionSz, windowSz;
  4523. struct sockaddr_in peerAddr;
  4524. int i;
  4525. /* Set ctx to DTLS 1.2 */
  4526. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  4527. AssertNotNull(ssl = wolfSSL_new(ctx));
  4528. /* test importing version 3 */
  4529. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  4530. /* test importing bad length and bad version */
  4531. version_3[2] += 1;
  4532. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  4533. version_3[2] -= 1; version_3[1] = 0XA0;
  4534. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  4535. wolfSSL_free(ssl);
  4536. wolfSSL_CTX_free(ctx);
  4537. /* check storing client state after connection and storing window only */
  4538. #ifdef WOLFSSL_TIRTOS
  4539. fdOpenSession(Task_self());
  4540. #endif
  4541. InitTcpReady(&ready);
  4542. #if defined(USE_WINDOWS_API)
  4543. /* use RNG to get random port if using windows */
  4544. ready.port = GetRandomPort();
  4545. #endif
  4546. /* set using dtls */
  4547. XMEMSET(&server_args, 0, sizeof(func_args));
  4548. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  4549. server_cbf.method = wolfDTLSv1_2_server_method;
  4550. server_args.callbacks = &server_cbf;
  4551. server_args.argc = 3; /* set loop_count to 3 */
  4552. server_args.signal = &ready;
  4553. start_thread(test_server_nofail, &server_args, &serverThread);
  4554. wait_tcp_ready(&server_args);
  4555. /* create and connect with client */
  4556. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  4557. AssertIntEQ(WOLFSSL_SUCCESS,
  4558. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4559. AssertIntEQ(WOLFSSL_SUCCESS,
  4560. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  4561. AssertIntEQ(WOLFSSL_SUCCESS,
  4562. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  4563. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  4564. AssertNotNull(ssl = wolfSSL_new(ctx));
  4565. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  4566. /* store server information connected too */
  4567. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  4568. peerAddr.sin_family = AF_INET;
  4569. peerAddr.sin_port = XHTONS(server_args.signal->port);
  4570. wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr));
  4571. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  4572. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  4573. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4574. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  4575. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  4576. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  4577. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  4578. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4579. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  4580. wolfSSL_free(ssl);
  4581. for (i = 1; i < server_args.argc; i++) {
  4582. /* restore state */
  4583. AssertNotNull(ssl = wolfSSL_new(ctx));
  4584. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  4585. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  4586. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  4587. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  4588. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  4589. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  4590. wolfSSL_free(ssl);
  4591. }
  4592. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4593. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  4594. wolfSSL_CTX_free(ctx);
  4595. printf("done and waiting for server\n");
  4596. join_thread(serverThread);
  4597. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  4598. FreeTcpReady(&ready);
  4599. #ifdef WOLFSSL_TIRTOS
  4600. fdOpenSession(Task_self());
  4601. #endif
  4602. }
  4603. printf(testingFmt, "wolfSSL_dtls_export()");
  4604. printf(resultFmt, passed);
  4605. #endif
  4606. }
  4607. /*----------------------------------------------------------------------------*
  4608. | TLS extensions tests
  4609. *----------------------------------------------------------------------------*/
  4610. #if defined(HAVE_SNI) || defined(HAVE_ALPN)
  4611. /* connection test runner */
  4612. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  4613. callback_functions* server_callbacks)
  4614. {
  4615. tcp_ready ready;
  4616. func_args client_args;
  4617. func_args server_args;
  4618. THREAD_TYPE serverThread;
  4619. XMEMSET(&client_args, 0, sizeof(func_args));
  4620. XMEMSET(&server_args, 0, sizeof(func_args));
  4621. StartTCP();
  4622. client_args.callbacks = client_callbacks;
  4623. server_args.callbacks = server_callbacks;
  4624. #ifdef WOLFSSL_TIRTOS
  4625. fdOpenSession(Task_self());
  4626. #endif
  4627. /* RUN Server side */
  4628. InitTcpReady(&ready);
  4629. #if defined(USE_WINDOWS_API)
  4630. /* use RNG to get random port if using windows */
  4631. ready.port = GetRandomPort();
  4632. #endif
  4633. server_args.signal = &ready;
  4634. client_args.signal = &ready;
  4635. start_thread(run_wolfssl_server, &server_args, &serverThread);
  4636. wait_tcp_ready(&server_args);
  4637. /* RUN Client side */
  4638. run_wolfssl_client(&client_args);
  4639. join_thread(serverThread);
  4640. FreeTcpReady(&ready);
  4641. #ifdef WOLFSSL_TIRTOS
  4642. fdCloseSession(Task_self());
  4643. #endif
  4644. }
  4645. #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) */
  4646. #ifdef HAVE_SNI
  4647. static void test_wolfSSL_UseSNI_params(void)
  4648. {
  4649. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4650. WOLFSSL *ssl = wolfSSL_new(ctx);
  4651. AssertNotNull(ctx);
  4652. AssertNotNull(ssl);
  4653. /* invalid [ctx|ssl] */
  4654. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  4655. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  4656. /* invalid type */
  4657. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  4658. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  4659. /* invalid data */
  4660. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  4661. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  4662. /* success case */
  4663. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  4664. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  4665. wolfSSL_free(ssl);
  4666. wolfSSL_CTX_free(ctx);
  4667. }
  4668. /* BEGIN of connection tests callbacks */
  4669. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  4670. {
  4671. AssertIntEQ(WOLFSSL_SUCCESS,
  4672. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  4673. }
  4674. static void use_SNI_at_ssl(WOLFSSL* ssl)
  4675. {
  4676. AssertIntEQ(WOLFSSL_SUCCESS,
  4677. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  4678. }
  4679. static void different_SNI_at_ssl(WOLFSSL* ssl)
  4680. {
  4681. AssertIntEQ(WOLFSSL_SUCCESS,
  4682. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  4683. }
  4684. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  4685. {
  4686. use_SNI_at_ssl(ssl);
  4687. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  4688. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  4689. }
  4690. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  4691. {
  4692. use_SNI_at_ssl(ssl);
  4693. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  4694. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  4695. }
  4696. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  4697. {
  4698. use_SNI_at_ctx(ctx);
  4699. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  4700. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  4701. }
  4702. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  4703. {
  4704. use_SNI_at_ssl(ssl);
  4705. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  4706. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  4707. }
  4708. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  4709. {
  4710. use_SNI_at_ctx(ctx);
  4711. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  4712. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  4713. }
  4714. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  4715. {
  4716. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  4717. }
  4718. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  4719. {
  4720. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  4721. }
  4722. static void verify_SNI_no_matching(WOLFSSL* ssl)
  4723. {
  4724. byte type = WOLFSSL_SNI_HOST_NAME;
  4725. char* request = (char*) &type; /* to be overwritten */
  4726. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  4727. AssertNotNull(request);
  4728. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  4729. AssertNull(request);
  4730. }
  4731. static void verify_SNI_real_matching(WOLFSSL* ssl)
  4732. {
  4733. byte type = WOLFSSL_SNI_HOST_NAME;
  4734. char* request = NULL;
  4735. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  4736. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  4737. AssertNotNull(request);
  4738. AssertStrEQ("www.wolfssl.com", request);
  4739. }
  4740. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  4741. {
  4742. byte type = WOLFSSL_SNI_HOST_NAME;
  4743. char* request = NULL;
  4744. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  4745. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  4746. AssertNotNull(request);
  4747. AssertStrEQ("ww2.wolfssl.com", request);
  4748. }
  4749. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  4750. {
  4751. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  4752. }
  4753. /* END of connection tests callbacks */
  4754. static void test_wolfSSL_UseSNI_connection(void)
  4755. {
  4756. unsigned long i;
  4757. callback_functions callbacks[] = {
  4758. /* success case at ctx */
  4759. {0, use_SNI_at_ctx, 0, 0, 0, 0},
  4760. {0, use_SNI_at_ctx, 0, verify_SNI_real_matching, 0, 0},
  4761. /* success case at ssl */
  4762. {0, 0, use_SNI_at_ssl, verify_SNI_real_matching, 0, 0},
  4763. {0, 0, use_SNI_at_ssl, verify_SNI_real_matching, 0, 0},
  4764. /* default mismatch behavior */
  4765. {0, 0, different_SNI_at_ssl, verify_FATAL_ERROR_on_client, 0, 0},
  4766. {0, 0, use_SNI_at_ssl, verify_UNKNOWN_SNI_on_server, 0, 0},
  4767. /* continue on mismatch */
  4768. {0, 0, different_SNI_at_ssl, 0, 0, 0},
  4769. {0, 0, use_SNI_WITH_CONTINUE_at_ssl, verify_SNI_no_matching, 0, 0},
  4770. /* fake answer on mismatch */
  4771. {0, 0, different_SNI_at_ssl, 0, 0, 0},
  4772. {0, 0, use_SNI_WITH_FAKE_ANSWER_at_ssl, verify_SNI_fake_matching, 0, 0},
  4773. /* sni abort - success */
  4774. {0, use_SNI_at_ctx, 0, 0, 0, 0},
  4775. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_real_matching, 0, 0},
  4776. /* sni abort - abort when absent (ctx) */
  4777. {0, 0, 0, verify_FATAL_ERROR_on_client, 0, 0},
  4778. {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_ABSENT_on_server, 0, 0},
  4779. /* sni abort - abort when absent (ssl) */
  4780. {0, 0, 0, verify_FATAL_ERROR_on_client, 0, 0},
  4781. {0, 0, use_MANDATORY_SNI_at_ssl, verify_SNI_ABSENT_on_server, 0, 0},
  4782. /* sni abort - success when overwritten */
  4783. {0, 0, 0, 0, 0, 0},
  4784. {0, use_MANDATORY_SNI_at_ctx, use_SNI_at_ssl, verify_SNI_no_matching, 0, 0},
  4785. /* sni abort - success when allowing mismatches */
  4786. {0, 0, different_SNI_at_ssl, 0, 0, 0},
  4787. {0, use_PSEUDO_MANDATORY_SNI_at_ctx, 0, verify_SNI_fake_matching, 0, 0},
  4788. };
  4789. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  4790. callbacks[i ].method = wolfSSLv23_client_method;
  4791. callbacks[i + 1].method = wolfSSLv23_server_method;
  4792. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  4793. }
  4794. }
  4795. static void test_wolfSSL_SNI_GetFromBuffer(void)
  4796. {
  4797. byte buff[] = { /* www.paypal.com */
  4798. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  4799. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  4800. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  4801. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  4802. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  4803. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  4804. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  4805. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  4806. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  4807. };
  4808. byte buff2[] = { /* api.textmate.org */
  4809. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  4810. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  4811. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  4812. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  4813. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  4814. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  4815. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  4816. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  4817. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  4818. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  4819. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  4820. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  4821. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  4822. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  4823. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  4824. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  4825. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  4826. };
  4827. byte buff3[] = { /* no sni extension */
  4828. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  4829. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  4830. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  4831. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  4832. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  4833. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  4834. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  4835. };
  4836. byte buff4[] = { /* last extension has zero size */
  4837. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  4838. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  4839. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  4840. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  4841. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  4842. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  4843. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  4844. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  4845. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  4846. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  4847. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  4848. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  4849. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  4850. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  4851. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  4852. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  4853. 0x12, 0x00, 0x00
  4854. };
  4855. byte buff5[] = { /* SSL v2.0 client hello */
  4856. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  4857. /* dummy bytes bellow, just to pass size check */
  4858. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  4859. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  4860. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  4861. };
  4862. byte result[32] = {0};
  4863. word32 length = 32;
  4864. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  4865. 0, result, &length));
  4866. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  4867. 0, result, &length));
  4868. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  4869. 1, result, &length));
  4870. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  4871. 0, result, &length));
  4872. buff[0] = 0x16;
  4873. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  4874. 0, result, &length));
  4875. buff[1] = 0x03;
  4876. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  4877. sizeof(buff), 0, result, &length));
  4878. buff[2] = 0x03;
  4879. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  4880. sizeof(buff), 0, result, &length));
  4881. buff[4] = 0x64;
  4882. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  4883. 0, result, &length));
  4884. result[length] = 0;
  4885. AssertStrEQ("www.paypal.com", (const char*) result);
  4886. length = 32;
  4887. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  4888. 0, result, &length));
  4889. result[length] = 0;
  4890. AssertStrEQ("api.textmate.org", (const char*) result);
  4891. /* SSL v2.0 tests */
  4892. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  4893. sizeof(buff5), 0, result, &length));
  4894. buff5[2] = 0x02;
  4895. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  4896. sizeof(buff5), 0, result, &length));
  4897. buff5[2] = 0x01; buff5[6] = 0x08;
  4898. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  4899. sizeof(buff5), 0, result, &length));
  4900. buff5[6] = 0x09; buff5[8] = 0x01;
  4901. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  4902. sizeof(buff5), 0, result, &length));
  4903. }
  4904. #endif /* HAVE_SNI */
  4905. static void test_wolfSSL_UseSNI(void)
  4906. {
  4907. #ifdef HAVE_SNI
  4908. test_wolfSSL_UseSNI_params();
  4909. test_wolfSSL_UseSNI_connection();
  4910. test_wolfSSL_SNI_GetFromBuffer();
  4911. #endif
  4912. }
  4913. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  4914. static void test_wolfSSL_UseTrustedCA(void)
  4915. {
  4916. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  4917. && !defined(NO_RSA)
  4918. WOLFSSL_CTX *ctx;
  4919. WOLFSSL *ssl;
  4920. byte id[20];
  4921. #ifndef NO_WOLFSSL_SERVER
  4922. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  4923. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  4924. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  4925. #else
  4926. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  4927. #endif
  4928. AssertNotNull((ssl = wolfSSL_new(ctx)));
  4929. XMEMSET(id, 0, sizeof(id));
  4930. /* error cases */
  4931. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  4932. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  4933. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  4934. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  4935. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  4936. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  4937. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  4938. #ifdef NO_SHA
  4939. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  4940. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  4941. #endif
  4942. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  4943. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  4944. /* success cases */
  4945. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  4946. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  4947. #ifndef NO_SHA
  4948. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  4949. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  4950. #endif
  4951. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  4952. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  4953. wolfSSL_free(ssl);
  4954. wolfSSL_CTX_free(ctx);
  4955. #endif /* HAVE_TRUSTED_CA */
  4956. }
  4957. static void test_wolfSSL_UseMaxFragment(void)
  4958. {
  4959. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  4960. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  4961. #ifndef NO_WOLFSSL_SERVER
  4962. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  4963. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  4964. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  4965. #else
  4966. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4967. #endif
  4968. WOLFSSL *ssl = wolfSSL_new(ctx);
  4969. #ifdef OPENSSL_EXTRA
  4970. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  4971. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  4972. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  4973. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  4974. #else
  4975. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  4976. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  4977. UseMaxFragment = wolfSSL_UseMaxFragment;
  4978. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  4979. #endif
  4980. AssertNotNull(ctx);
  4981. AssertNotNull(ssl);
  4982. /* error cases */
  4983. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  4984. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  4985. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  4986. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  4987. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  4988. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  4989. /* success case */
  4990. #ifdef OPENSSL_EXTRA
  4991. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  4992. #else
  4993. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  4994. #endif
  4995. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  4996. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  4997. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  4998. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  4999. #ifdef OPENSSL_EXTRA
  5000. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  5001. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  5002. #else
  5003. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  5004. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  5005. #endif
  5006. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  5007. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  5008. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  5009. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  5010. #ifdef OPENSSL_EXTRA
  5011. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  5012. #else
  5013. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  5014. #endif
  5015. wolfSSL_free(ssl);
  5016. wolfSSL_CTX_free(ctx);
  5017. #endif
  5018. }
  5019. static void test_wolfSSL_UseTruncatedHMAC(void)
  5020. {
  5021. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  5022. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  5023. #ifndef NO_WOLFSSL_SERVER
  5024. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  5025. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  5026. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  5027. #else
  5028. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  5029. #endif
  5030. WOLFSSL *ssl = wolfSSL_new(ctx);
  5031. AssertNotNull(ctx);
  5032. AssertNotNull(ssl);
  5033. /* error cases */
  5034. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  5035. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  5036. /* success case */
  5037. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  5038. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  5039. wolfSSL_free(ssl);
  5040. wolfSSL_CTX_free(ctx);
  5041. #endif
  5042. }
  5043. static void test_wolfSSL_UseSupportedCurve(void)
  5044. {
  5045. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  5046. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  5047. WOLFSSL *ssl = wolfSSL_new(ctx);
  5048. AssertNotNull(ctx);
  5049. AssertNotNull(ssl);
  5050. /* error cases */
  5051. AssertIntNE(WOLFSSL_SUCCESS,
  5052. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  5053. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  5054. AssertIntNE(WOLFSSL_SUCCESS,
  5055. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  5056. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  5057. /* success case */
  5058. AssertIntEQ(WOLFSSL_SUCCESS,
  5059. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  5060. AssertIntEQ(WOLFSSL_SUCCESS,
  5061. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  5062. wolfSSL_free(ssl);
  5063. wolfSSL_CTX_free(ctx);
  5064. #endif
  5065. }
  5066. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) && \
  5067. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5068. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  5069. {
  5070. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  5071. }
  5072. static void use_ALPN_all(WOLFSSL* ssl)
  5073. {
  5074. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  5075. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  5076. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  5077. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  5078. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  5079. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  5080. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  5081. }
  5082. static void use_ALPN_all_continue(WOLFSSL* ssl)
  5083. {
  5084. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  5085. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  5086. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  5087. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  5088. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  5089. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  5090. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  5091. }
  5092. static void use_ALPN_one(WOLFSSL* ssl)
  5093. {
  5094. /* spdy/2 */
  5095. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  5096. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  5097. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  5098. }
  5099. static void use_ALPN_unknown(WOLFSSL* ssl)
  5100. {
  5101. /* http/2.0 */
  5102. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  5103. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  5104. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  5105. }
  5106. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  5107. {
  5108. /* http/2.0 */
  5109. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  5110. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  5111. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  5112. }
  5113. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  5114. {
  5115. /* spdy/3 */
  5116. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  5117. char *proto = NULL;
  5118. word16 protoSz = 0;
  5119. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  5120. /* check value */
  5121. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  5122. if (proto) {
  5123. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  5124. }
  5125. }
  5126. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  5127. {
  5128. char *proto = NULL;
  5129. word16 protoSz = 0;
  5130. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  5131. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  5132. /* check value */
  5133. AssertIntEQ(1, (0 == protoSz));
  5134. AssertIntEQ(1, (NULL == proto));
  5135. }
  5136. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  5137. {
  5138. /* http/1.1 */
  5139. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  5140. char *proto;
  5141. word16 protoSz = 0;
  5142. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  5143. /* check value */
  5144. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  5145. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  5146. }
  5147. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  5148. {
  5149. /* spdy/2 */
  5150. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  5151. char *proto;
  5152. word16 protoSz = 0;
  5153. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  5154. /* check value */
  5155. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  5156. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  5157. }
  5158. static void verify_ALPN_client_list(WOLFSSL* ssl)
  5159. {
  5160. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  5161. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  5162. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  5163. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  5164. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  5165. char *clist = NULL;
  5166. word16 clistSz = 0;
  5167. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  5168. &clistSz));
  5169. /* check value */
  5170. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  5171. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  5172. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  5173. }
  5174. static void test_wolfSSL_UseALPN_connection(void)
  5175. {
  5176. unsigned long i;
  5177. callback_functions callbacks[] = {
  5178. /* success case same list */
  5179. {0, 0, use_ALPN_all, 0, 0, 0},
  5180. {0, 0, use_ALPN_all, verify_ALPN_matching_http1, 0, 0},
  5181. /* success case only one for server */
  5182. {0, 0, use_ALPN_all, 0, 0, 0},
  5183. {0, 0, use_ALPN_one, verify_ALPN_matching_spdy2, 0, 0},
  5184. /* success case only one for client */
  5185. {0, 0, use_ALPN_one, 0, 0, 0},
  5186. {0, 0, use_ALPN_all, verify_ALPN_matching_spdy2, 0, 0},
  5187. /* success case none for client */
  5188. {0, 0, 0, 0, 0, 0},
  5189. {0, 0, use_ALPN_all, 0, 0, 0},
  5190. /* success case mismatch behavior but option 'continue' set */
  5191. {0, 0, use_ALPN_all_continue, verify_ALPN_not_matching_continue, 0, 0},
  5192. {0, 0, use_ALPN_unknown_continue, 0, 0, 0},
  5193. /* success case read protocol send by client */
  5194. {0, 0, use_ALPN_all, 0, 0, 0},
  5195. {0, 0, use_ALPN_one, verify_ALPN_client_list, 0, 0},
  5196. /* mismatch behavior with same list
  5197. * the first and only this one must be taken */
  5198. {0, 0, use_ALPN_all, 0, 0, 0},
  5199. {0, 0, use_ALPN_all, verify_ALPN_not_matching_spdy3, 0, 0},
  5200. /* default mismatch behavior */
  5201. {0, 0, use_ALPN_all, 0, 0, 0},
  5202. {0, 0, use_ALPN_unknown, verify_ALPN_FATAL_ERROR_on_client, 0, 0},
  5203. };
  5204. for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
  5205. callbacks[i ].method = wolfSSLv23_client_method;
  5206. callbacks[i + 1].method = wolfSSLv23_server_method;
  5207. test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
  5208. }
  5209. }
  5210. static void test_wolfSSL_UseALPN_params(void)
  5211. {
  5212. #ifndef NO_WOLFSSL_CLIENT
  5213. /* "http/1.1" */
  5214. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  5215. /* "spdy/1" */
  5216. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  5217. /* "spdy/2" */
  5218. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  5219. /* "spdy/3" */
  5220. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  5221. char buff[256];
  5222. word32 idx;
  5223. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  5224. WOLFSSL *ssl = wolfSSL_new(ctx);
  5225. AssertNotNull(ctx);
  5226. AssertNotNull(ssl);
  5227. /* error cases */
  5228. AssertIntNE(WOLFSSL_SUCCESS,
  5229. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  5230. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  5231. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  5232. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  5233. /* success case */
  5234. /* http1 only */
  5235. AssertIntEQ(WOLFSSL_SUCCESS,
  5236. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  5237. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  5238. /* http1, spdy1 */
  5239. XMEMCPY(buff, http1, sizeof(http1));
  5240. idx = sizeof(http1);
  5241. buff[idx++] = ',';
  5242. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  5243. idx += sizeof(spdy1);
  5244. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  5245. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  5246. /* http1, spdy2, spdy1 */
  5247. XMEMCPY(buff, http1, sizeof(http1));
  5248. idx = sizeof(http1);
  5249. buff[idx++] = ',';
  5250. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  5251. idx += sizeof(spdy2);
  5252. buff[idx++] = ',';
  5253. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  5254. idx += sizeof(spdy1);
  5255. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  5256. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  5257. /* spdy3, http1, spdy2, spdy1 */
  5258. XMEMCPY(buff, spdy3, sizeof(spdy3));
  5259. idx = sizeof(spdy3);
  5260. buff[idx++] = ',';
  5261. XMEMCPY(buff+idx, http1, sizeof(http1));
  5262. idx += sizeof(http1);
  5263. buff[idx++] = ',';
  5264. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  5265. idx += sizeof(spdy2);
  5266. buff[idx++] = ',';
  5267. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  5268. idx += sizeof(spdy1);
  5269. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  5270. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  5271. wolfSSL_free(ssl);
  5272. wolfSSL_CTX_free(ctx);
  5273. #endif
  5274. }
  5275. #endif /* HAVE_ALPN */
  5276. static void test_wolfSSL_UseALPN(void)
  5277. {
  5278. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
  5279. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5280. test_wolfSSL_UseALPN_connection();
  5281. test_wolfSSL_UseALPN_params();
  5282. #endif
  5283. }
  5284. static void test_wolfSSL_DisableExtendedMasterSecret(void)
  5285. {
  5286. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  5287. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  5288. WOLFSSL *ssl = wolfSSL_new(ctx);
  5289. AssertNotNull(ctx);
  5290. AssertNotNull(ssl);
  5291. /* error cases */
  5292. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  5293. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  5294. /* success cases */
  5295. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  5296. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  5297. wolfSSL_free(ssl);
  5298. wolfSSL_CTX_free(ctx);
  5299. #endif
  5300. }
  5301. static void test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  5302. {
  5303. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  5304. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  5305. WOLFSSL *ssl = wolfSSL_new(ctx);
  5306. AssertNotNull(ctx);
  5307. AssertNotNull(ssl);
  5308. /* error cases */
  5309. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  5310. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  5311. /* success cases */
  5312. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  5313. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  5314. wolfSSL_free(ssl);
  5315. wolfSSL_CTX_free(ctx);
  5316. #endif
  5317. }
  5318. /*----------------------------------------------------------------------------*
  5319. | X509 Tests
  5320. *----------------------------------------------------------------------------*/
  5321. static void test_wolfSSL_X509_NAME_get_entry(void)
  5322. {
  5323. #if !defined(NO_CERTS) && !defined(NO_RSA)
  5324. #if defined(OPENSSL_ALL) || \
  5325. (defined(OPENSSL_EXTRA) && \
  5326. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  5327. printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
  5328. {
  5329. /* use openssl like name to test mapping */
  5330. X509_NAME_ENTRY* ne;
  5331. X509_NAME* name;
  5332. X509* x509;
  5333. #ifndef NO_FILESYSTEM
  5334. ASN1_STRING* asn;
  5335. char* subCN = NULL;
  5336. #endif
  5337. int idx;
  5338. ASN1_OBJECT *object = NULL;
  5339. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  5340. #ifndef NO_BIO
  5341. BIO* bio;
  5342. #endif
  5343. #endif
  5344. #ifndef NO_FILESYSTEM
  5345. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  5346. AssertNotNull(x509);
  5347. name = X509_get_subject_name(x509);
  5348. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  5349. AssertIntGE(idx, 0);
  5350. ne = X509_NAME_get_entry(name, idx);
  5351. AssertNotNull(ne);
  5352. asn = X509_NAME_ENTRY_get_data(ne);
  5353. AssertNotNull(asn);
  5354. subCN = (char*)ASN1_STRING_data(asn);
  5355. AssertNotNull(subCN);
  5356. wolfSSL_FreeX509(x509);
  5357. #endif
  5358. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  5359. AssertNotNull(x509);
  5360. name = X509_get_subject_name(x509);
  5361. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  5362. AssertIntGE(idx, 0);
  5363. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  5364. #ifndef NO_BIO
  5365. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  5366. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  5367. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  5368. AssertIntEQ(X509_NAME_print_ex_fp(stdout, name, 4,
  5369. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  5370. BIO_free(bio);
  5371. #endif
  5372. #endif
  5373. ne = X509_NAME_get_entry(name, idx);
  5374. AssertNotNull(ne);
  5375. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  5376. wolfSSL_FreeX509(x509);
  5377. }
  5378. printf(resultFmt, passed);
  5379. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  5380. #endif /* !NO_CERTS && !NO_RSA */
  5381. }
  5382. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  5383. static void test_wolfSSL_PKCS12(void)
  5384. {
  5385. /* .p12 file is encrypted with DES3 */
  5386. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  5387. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  5388. * Password Key
  5389. */
  5390. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  5391. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  5392. !defined(NO_SHA) && defined(HAVE_PKCS12)
  5393. byte buffer[6000];
  5394. char file[] = "./certs/test-servercert.p12";
  5395. char order[] = "./certs/ecc-rsa-server.p12";
  5396. #ifdef WC_RC2
  5397. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  5398. #endif
  5399. char pass[] = "a password";
  5400. const char goodPsw[] = "wolfSSL test";
  5401. const char badPsw[] = "bad";
  5402. #ifdef HAVE_ECC
  5403. WOLFSSL_X509_NAME* subject;
  5404. WOLFSSL_X509 *x509;
  5405. #endif
  5406. XFILE f;
  5407. int bytes, ret, goodPswLen, badPswLen;
  5408. WOLFSSL_BIO *bio;
  5409. WOLFSSL_EVP_PKEY *pkey;
  5410. WC_PKCS12 *pkcs12;
  5411. WC_PKCS12 *pkcs12_2;
  5412. WOLFSSL_X509 *cert;
  5413. WOLFSSL_X509 *tmp;
  5414. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  5415. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  5416. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  5417. WOLFSSL_CTX *ctx;
  5418. WOLFSSL *ssl;
  5419. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  5420. #endif
  5421. printf(testingFmt, "wolfSSL_PKCS12()");
  5422. f = XFOPEN(file, "rb");
  5423. AssertTrue((f != XBADFILE));
  5424. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  5425. XFCLOSE(f);
  5426. goodPswLen = (int)XSTRLEN(goodPsw);
  5427. badPswLen = (int)XSTRLEN(badPsw);
  5428. bio = BIO_new_mem_buf((void*)buffer, bytes);
  5429. AssertNotNull(bio);
  5430. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  5431. AssertNotNull(pkcs12);
  5432. PKCS12_free(pkcs12);
  5433. d2i_PKCS12_bio(bio, &pkcs12);
  5434. AssertNotNull(pkcs12);
  5435. BIO_free(bio);
  5436. /* check verify MAC directly */
  5437. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  5438. AssertIntEQ(ret, 1);
  5439. /* check verify MAC fail case directly */
  5440. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  5441. AssertIntEQ(ret, 0);
  5442. /* check verify MAC fail case */
  5443. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  5444. AssertIntEQ(ret, 0);
  5445. AssertNull(pkey);
  5446. AssertNull(cert);
  5447. /* check parse with no extra certs kept */
  5448. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  5449. AssertIntEQ(ret, 1);
  5450. AssertNotNull(pkey);
  5451. AssertNotNull(cert);
  5452. wolfSSL_EVP_PKEY_free(pkey);
  5453. wolfSSL_X509_free(cert);
  5454. /* check parse with extra certs kept */
  5455. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  5456. AssertIntEQ(ret, 1);
  5457. AssertNotNull(pkey);
  5458. AssertNotNull(cert);
  5459. AssertNotNull(ca);
  5460. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  5461. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  5462. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  5463. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  5464. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  5465. #else
  5466. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  5467. #endif
  5468. /* Copy stack structure */
  5469. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  5470. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  5471. /* CTX now owns the tmp_ca stack structure */
  5472. tmp_ca = NULL;
  5473. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  5474. AssertNotNull(tmp_ca);
  5475. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  5476. /* Check that the main cert is also set */
  5477. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  5478. AssertNotNull(ssl = SSL_new(ctx));
  5479. AssertNotNull(SSL_get_certificate(ssl));
  5480. SSL_free(ssl);
  5481. SSL_CTX_free(ctx);
  5482. #endif
  5483. /* should be 2 other certs on stack */
  5484. tmp = sk_X509_pop(ca);
  5485. AssertNotNull(tmp);
  5486. X509_free(tmp);
  5487. tmp = sk_X509_pop(ca);
  5488. AssertNotNull(tmp);
  5489. X509_free(tmp);
  5490. AssertNull(sk_X509_pop(ca));
  5491. EVP_PKEY_free(pkey);
  5492. X509_free(cert);
  5493. sk_X509_pop_free(ca, X509_free);
  5494. /* check PKCS12_create */
  5495. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  5496. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  5497. SSL_SUCCESS);
  5498. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  5499. -1, -1, 100, -1, 0)));
  5500. EVP_PKEY_free(pkey);
  5501. X509_free(cert);
  5502. sk_X509_free(ca);
  5503. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  5504. SSL_SUCCESS);
  5505. PKCS12_free(pkcs12_2);
  5506. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  5507. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  5508. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  5509. 2000, 1, 0)));
  5510. EVP_PKEY_free(pkey);
  5511. X509_free(cert);
  5512. sk_X509_free(ca);
  5513. /* convert to DER then back and parse */
  5514. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  5515. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  5516. PKCS12_free(pkcs12_2);
  5517. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  5518. BIO_free(bio);
  5519. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  5520. SSL_SUCCESS);
  5521. /* should be 2 other certs on stack */
  5522. tmp = sk_X509_pop(ca);
  5523. AssertNotNull(tmp);
  5524. X509_free(tmp);
  5525. tmp = sk_X509_pop(ca);
  5526. AssertNotNull(tmp);
  5527. X509_free(tmp);
  5528. AssertNull(sk_X509_pop(ca));
  5529. #ifndef NO_RC4
  5530. PKCS12_free(pkcs12_2);
  5531. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  5532. NID_pbe_WithSHA1And128BitRC4,
  5533. NID_pbe_WithSHA1And128BitRC4,
  5534. 2000, 1, 0)));
  5535. EVP_PKEY_free(pkey);
  5536. X509_free(cert);
  5537. sk_X509_free(ca);
  5538. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  5539. SSL_SUCCESS);
  5540. #endif /* NO_RC4 */
  5541. EVP_PKEY_free(pkey);
  5542. X509_free(cert);
  5543. PKCS12_free(pkcs12);
  5544. PKCS12_free(pkcs12_2);
  5545. sk_X509_free(ca);
  5546. #ifdef HAVE_ECC
  5547. /* test order of parsing */
  5548. f = XFOPEN(order, "rb");
  5549. AssertTrue(f != XBADFILE);
  5550. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  5551. XFCLOSE(f);
  5552. AssertNotNull(bio = BIO_new_mem_buf((void*)buffer, bytes));
  5553. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  5554. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  5555. WOLFSSL_SUCCESS);
  5556. AssertNotNull(pkey);
  5557. AssertNotNull(cert);
  5558. AssertNotNull(ca);
  5559. /* compare subject lines of certificates */
  5560. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  5561. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  5562. SSL_FILETYPE_PEM));
  5563. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  5564. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  5565. X509_free(x509);
  5566. /* test expected fail case */
  5567. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  5568. SSL_FILETYPE_PEM));
  5569. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  5570. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  5571. X509_free(x509);
  5572. X509_free(cert);
  5573. /* get subject line from ca stack */
  5574. AssertNotNull(cert = sk_X509_pop(ca));
  5575. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  5576. /* compare subject from certificate in ca to expected */
  5577. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  5578. SSL_FILETYPE_PEM));
  5579. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  5580. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  5581. EVP_PKEY_free(pkey);
  5582. X509_free(x509);
  5583. X509_free(cert);
  5584. BIO_free(bio);
  5585. PKCS12_free(pkcs12);
  5586. sk_X509_free(ca); /* TEST d2i_PKCS12_fp */
  5587. /* test order of parsing */
  5588. f = XFOPEN(file, "rb");
  5589. AssertTrue(f != XBADFILE);
  5590. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  5591. XFCLOSE(f);
  5592. /* check verify MAC fail case */
  5593. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  5594. AssertIntEQ(ret, 0);
  5595. AssertNull(pkey);
  5596. AssertNull(cert);
  5597. /* check parse with no extra certs kept */
  5598. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  5599. AssertIntEQ(ret, 1);
  5600. AssertNotNull(pkey);
  5601. AssertNotNull(cert);
  5602. wolfSSL_EVP_PKEY_free(pkey);
  5603. wolfSSL_X509_free(cert);
  5604. /* check parse with extra certs kept */
  5605. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  5606. AssertIntEQ(ret, 1);
  5607. AssertNotNull(pkey);
  5608. AssertNotNull(cert);
  5609. AssertNotNull(ca);
  5610. wolfSSL_EVP_PKEY_free(pkey);
  5611. wolfSSL_X509_free(cert);
  5612. sk_X509_free(ca);
  5613. PKCS12_free(pkcs12);
  5614. #endif /* HAVE_ECC */
  5615. #ifdef WC_RC2
  5616. /* test PKCS#12 with RC2 encryption */
  5617. f = XFOPEN(rc2p12, "rb");
  5618. AssertTrue(f != XBADFILE);
  5619. bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
  5620. XFCLOSE(f);
  5621. AssertNotNull(bio = BIO_new_mem_buf((void*)buffer, bytes));
  5622. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  5623. /* check verify MAC fail case */
  5624. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  5625. AssertIntEQ(ret, 0);
  5626. AssertNull(pkey);
  5627. AssertNull(cert);
  5628. /* check parse iwth not extra certs kept */
  5629. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  5630. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  5631. AssertNotNull(pkey);
  5632. AssertNotNull(cert);
  5633. /* check parse with extra certs kept */
  5634. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  5635. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  5636. AssertNotNull(pkey);
  5637. AssertNotNull(cert);
  5638. AssertNotNull(ca);
  5639. wolfSSL_EVP_PKEY_free(pkey);
  5640. wolfSSL_X509_free(cert);
  5641. sk_X509_free(ca);
  5642. BIO_free(bio);
  5643. PKCS12_free(pkcs12);
  5644. #endif /* WC_RC2 */
  5645. /* Test i2d_PKCS12_bio */
  5646. f = XFOPEN(file, "rb");
  5647. AssertTrue((f != XBADFILE));
  5648. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  5649. XFCLOSE(f);
  5650. bio = BIO_new(BIO_s_mem());
  5651. AssertNotNull(bio);
  5652. ret = i2d_PKCS12_bio(bio, pkcs12);
  5653. AssertIntEQ(ret, 1);
  5654. ret = i2d_PKCS12_bio(NULL, pkcs12);
  5655. AssertIntEQ(ret, 0);
  5656. ret = i2d_PKCS12_bio(bio, NULL);
  5657. AssertIntEQ(ret, 0);
  5658. PKCS12_free(pkcs12);
  5659. BIO_free(bio);
  5660. (void)order;
  5661. printf(resultFmt, passed);
  5662. #endif /* OPENSSL_EXTRA */
  5663. #endif /* HAVE_FIPS */
  5664. }
  5665. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  5666. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  5667. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  5668. #define TEST_PKCS8_ENC
  5669. #endif
  5670. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  5671. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  5672. /* used to keep track if FailTestCallback was called */
  5673. static int failTestCallbackCalled = 0;
  5674. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  5675. {
  5676. (void)passwd;
  5677. (void)sz;
  5678. (void)rw;
  5679. (void)userdata;
  5680. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  5681. failTestCallbackCalled = 1;
  5682. return -1;
  5683. }
  5684. #endif
  5685. static void test_wolfSSL_no_password_cb(void)
  5686. {
  5687. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  5688. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  5689. WOLFSSL_CTX* ctx;
  5690. byte buff[FOURK_BUF];
  5691. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  5692. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  5693. XFILE f;
  5694. int bytes;
  5695. printf(testingFmt, "test_wolfSSL_no_password_cb()");
  5696. #ifndef NO_WOLFSSL_CLIENT
  5697. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  5698. #else
  5699. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  5700. #endif
  5701. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  5702. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  5703. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5704. XFCLOSE(f);
  5705. AssertIntLE(bytes, sizeof(buff));
  5706. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5707. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  5708. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  5709. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5710. XFCLOSE(f);
  5711. AssertIntLE(bytes, sizeof(buff));
  5712. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5713. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5714. wolfSSL_CTX_free(ctx);
  5715. if (failTestCallbackCalled != 0) {
  5716. Fail(("Password callback should not be called by default"),
  5717. ("Password callback was called without attempting "
  5718. "to first decipher private key without password."));
  5719. }
  5720. printf(resultFmt, passed);
  5721. #endif
  5722. }
  5723. #ifdef TEST_PKCS8_ENC
  5724. /* for PKCS8 test case */
  5725. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  5726. {
  5727. int flag = 0;
  5728. (void)rw;
  5729. if (userdata != NULL) {
  5730. flag = *((int*)userdata); /* user set data */
  5731. }
  5732. switch (flag) {
  5733. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  5734. * can be anything the user wishes to be passed to the callback
  5735. * associated with the WOLFSSL_CTX */
  5736. XSTRNCPY(passwd, "yassl123", sz);
  5737. return 8;
  5738. default:
  5739. return BAD_FUNC_ARG;
  5740. }
  5741. }
  5742. #endif /* TEST_PKCS8_ENC */
  5743. /* Testing functions dealing with PKCS8 */
  5744. static void test_wolfSSL_PKCS8(void)
  5745. {
  5746. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  5747. byte buff[FOURK_BUF];
  5748. byte der[FOURK_BUF];
  5749. #ifndef NO_RSA
  5750. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  5751. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  5752. #endif
  5753. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  5754. #ifdef HAVE_ECC
  5755. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  5756. #endif
  5757. XFILE f;
  5758. int bytes;
  5759. WOLFSSL_CTX* ctx;
  5760. #if defined(HAVE_ECC) && !defined(NO_CODING)
  5761. int ret;
  5762. ecc_key key;
  5763. word32 x = 0;
  5764. #endif
  5765. #ifdef TEST_PKCS8_ENC
  5766. #if !defined(NO_RSA) && !defined(NO_SHA)
  5767. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  5768. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  5769. #endif
  5770. #if defined(HAVE_ECC) && !defined(NO_SHA)
  5771. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  5772. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  5773. #endif
  5774. int flag;
  5775. #endif
  5776. (void)der;
  5777. printf(testingFmt, "wolfSSL_PKCS8()");
  5778. #ifndef NO_WOLFSSL_CLIENT
  5779. #ifndef WOLFSSL_NO_TLS12
  5780. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  5781. #else
  5782. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  5783. #endif
  5784. #else
  5785. #ifndef WOLFSSL_NO_TLS12
  5786. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  5787. #else
  5788. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  5789. #endif
  5790. #endif
  5791. #ifdef TEST_PKCS8_ENC
  5792. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  5793. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  5794. flag = 1; /* used by password callback as return code */
  5795. #if !defined(NO_RSA) && !defined(NO_SHA)
  5796. /* test loading PEM PKCS8 encrypted file */
  5797. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  5798. AssertTrue((f != XBADFILE));
  5799. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5800. XFCLOSE(f);
  5801. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5802. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5803. /* this next case should fail because of password callback return code */
  5804. flag = 0; /* used by password callback as return code */
  5805. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5806. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5807. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  5808. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  5809. "yassl123"), 0);
  5810. /* test that error value is returned with a bad password */
  5811. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  5812. "bad"), 0);
  5813. /* test loading PEM PKCS8 encrypted file */
  5814. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  5815. AssertTrue((f != XBADFILE));
  5816. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5817. XFCLOSE(f);
  5818. flag = 1; /* used by password callback as return code */
  5819. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5820. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  5821. /* this next case should fail because of password callback return code */
  5822. flag = 0; /* used by password callback as return code */
  5823. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5824. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  5825. #endif /* !NO_RSA && !NO_SHA */
  5826. #if defined(HAVE_ECC) && !defined(NO_SHA)
  5827. /* test loading PEM PKCS8 encrypted ECC Key file */
  5828. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  5829. AssertTrue((f != XBADFILE));
  5830. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5831. XFCLOSE(f);
  5832. flag = 1; /* used by password callback as return code */
  5833. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5834. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5835. /* this next case should fail because of password callback return code */
  5836. flag = 0; /* used by password callback as return code */
  5837. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5838. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5839. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  5840. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  5841. "yassl123"), 0);
  5842. /* test that error value is returned with a bad password */
  5843. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  5844. "bad"), 0);
  5845. /* test loading DER PKCS8 encrypted ECC Key file */
  5846. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  5847. AssertTrue((f != XBADFILE));
  5848. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5849. XFCLOSE(f);
  5850. flag = 1; /* used by password callback as return code */
  5851. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5852. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  5853. /* this next case should fail because of password callback return code */
  5854. flag = 0; /* used by password callback as return code */
  5855. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5856. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  5857. /* leave flag as "okay" */
  5858. flag = 1;
  5859. #endif /* HAVE_ECC && !NO_SHA */
  5860. #endif /* TEST_PKCS8_ENC */
  5861. #ifndef NO_RSA
  5862. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  5863. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  5864. AssertTrue((f != XBADFILE));
  5865. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5866. XFCLOSE(f);
  5867. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5868. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  5869. /* test loading PEM PKCS8 private key file (not encrypted) */
  5870. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  5871. AssertTrue((f != XBADFILE));
  5872. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5873. XFCLOSE(f);
  5874. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5875. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5876. #endif /* !NO_RSA */
  5877. /* Test PKCS8 PEM ECC key no crypt */
  5878. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  5879. AssertTrue((f != XBADFILE));
  5880. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5881. XFCLOSE(f);
  5882. #ifdef HAVE_ECC
  5883. /* Test PKCS8 PEM ECC key no crypt */
  5884. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5885. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5886. #ifndef NO_CODING
  5887. /* decrypt PKCS8 PEM to key in DER format */
  5888. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  5889. (word32)sizeof(der), NULL)), 0);
  5890. ret = wc_ecc_init(&key);
  5891. if (ret == 0) {
  5892. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  5893. wc_ecc_free(&key);
  5894. }
  5895. AssertIntEQ(ret, 0);
  5896. #endif
  5897. /* Test PKCS8 DER ECC key no crypt */
  5898. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  5899. AssertTrue((f != XBADFILE));
  5900. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  5901. XFCLOSE(f);
  5902. /* Test using a PKCS8 ECC PEM */
  5903. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  5904. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  5905. #else
  5906. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  5907. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  5908. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  5909. #endif /* HAVE_ECC */
  5910. wolfSSL_CTX_free(ctx);
  5911. printf(resultFmt, passed);
  5912. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  5913. }
  5914. static void test_wolfSSL_PKCS8_ED25519(void)
  5915. {
  5916. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  5917. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  5918. defined(HAVE_ED25519_KEY_IMPORT)
  5919. const byte encPrivKey[] = \
  5920. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  5921. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  5922. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  5923. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  5924. "2L9W4v7swXkV+X+a1ww=\n"
  5925. "-----END ENCRYPTED PRIVATE KEY-----\n";
  5926. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  5927. byte der[FOURK_BUF];
  5928. WOLFSSL_CTX* ctx;
  5929. int bytes;
  5930. XMEMSET(der, 0, sizeof(der));
  5931. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  5932. (word32)sizeof(der), password)), 0);
  5933. #ifndef NO_WOLFSSL_SERVER
  5934. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  5935. #else
  5936. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  5937. #endif
  5938. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  5939. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  5940. wolfSSL_CTX_free(ctx);
  5941. #endif
  5942. }
  5943. static void test_wolfSSL_PKCS8_ED448(void)
  5944. {
  5945. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  5946. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  5947. defined(HAVE_ED448_KEY_IMPORT)
  5948. const byte encPrivKey[] = \
  5949. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  5950. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  5951. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  5952. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  5953. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  5954. "-----END ENCRYPTED PRIVATE KEY-----\n";
  5955. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  5956. byte der[FOURK_BUF];
  5957. WOLFSSL_CTX* ctx;
  5958. int bytes;
  5959. XMEMSET(der, 0, sizeof(der));
  5960. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  5961. (word32)sizeof(der), password)), 0);
  5962. #ifndef NO_WOLFSSL_SERVER
  5963. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  5964. #else
  5965. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  5966. #endif
  5967. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  5968. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  5969. wolfSSL_CTX_free(ctx);
  5970. #endif
  5971. }
  5972. /* Testing functions dealing with PKCS5 */
  5973. static void test_wolfSSL_PKCS5(void)
  5974. {
  5975. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  5976. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  5977. const char* passwd = "myfipsPa$$W0rd";
  5978. #else
  5979. const char *passwd = "pass1234";
  5980. #endif
  5981. const unsigned char *salt = (unsigned char *)"salt1234";
  5982. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  5983. DYNAMIC_TYPE_TMP_BUFFER);
  5984. int ret = 0;
  5985. AssertNotNull(out);
  5986. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  5987. (int)XSTRLEN((const char *) salt), 10,
  5988. WC_SHA_DIGEST_SIZE,out);
  5989. AssertIntEQ(ret, SSL_SUCCESS);
  5990. #ifdef WOLFSSL_SHA512
  5991. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  5992. (int)XSTRLEN((const char *) salt), 10,
  5993. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  5994. AssertIntEQ(ret, SSL_SUCCESS);
  5995. #endif
  5996. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5997. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  5998. }
  5999. /* test parsing URI from certificate */
  6000. static void test_wolfSSL_URI(void)
  6001. {
  6002. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  6003. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  6004. defined(OPENSSL_EXTRA))
  6005. WOLFSSL_X509* x509;
  6006. const char uri[] = "./certs/client-uri-cert.pem";
  6007. const char badUri[] = "./certs/client-relative-uri.pem";
  6008. printf(testingFmt, "wolfSSL URI parse");
  6009. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  6010. AssertNotNull(x509);
  6011. wolfSSL_FreeX509(x509);
  6012. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  6013. #ifndef IGNORE_NAME_CONSTRAINTS
  6014. AssertNull(x509);
  6015. #else
  6016. AssertNotNull(x509);
  6017. #endif
  6018. printf(resultFmt, passed);
  6019. #endif
  6020. }
  6021. static void test_wolfSSL_TBS(void)
  6022. {
  6023. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  6024. && defined(OPENSSL_EXTRA)
  6025. WOLFSSL_X509* x509;
  6026. const unsigned char* tbs;
  6027. int tbsSz;
  6028. printf(testingFmt, "wolfSSL TBS");
  6029. AssertNotNull(x509 =
  6030. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  6031. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  6032. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  6033. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  6034. AssertIntEQ(tbsSz, 981);
  6035. wolfSSL_FreeX509(x509);
  6036. printf(resultFmt, passed);
  6037. #endif
  6038. }
  6039. static void test_wolfSSL_X509_verify(void)
  6040. {
  6041. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  6042. && defined(OPENSSL_EXTRA)
  6043. WOLFSSL_X509* ca;
  6044. WOLFSSL_X509* serv;
  6045. WOLFSSL_EVP_PKEY* pkey;
  6046. unsigned char buf[2048];
  6047. const unsigned char* pt = NULL;
  6048. int bufSz;
  6049. printf(testingFmt, "wolfSSL X509 verify");
  6050. AssertNotNull(ca =
  6051. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  6052. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  6053. WOLFSSL_SUCCESS);
  6054. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  6055. WOLFSSL_SUCCESS);
  6056. AssertIntEQ(bufSz, 294);
  6057. bufSz = 2048;
  6058. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  6059. WOLFSSL_SUCCESS);
  6060. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  6061. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  6062. AssertNotNull(serv =
  6063. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  6064. /* success case */
  6065. pt = buf;
  6066. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  6067. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  6068. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  6069. wolfSSL_EVP_PKEY_free(pkey);
  6070. /* fail case */
  6071. bufSz = 2048;
  6072. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  6073. WOLFSSL_SUCCESS);
  6074. pt = buf;
  6075. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  6076. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  6077. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  6078. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  6079. wolfSSL_EVP_PKEY_free(pkey);
  6080. wolfSSL_FreeX509(ca);
  6081. wolfSSL_FreeX509(serv);
  6082. printf(resultFmt, passed);
  6083. #endif
  6084. }
  6085. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  6086. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  6087. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  6088. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN)
  6089. /* create certificate with version 2 */
  6090. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  6091. {
  6092. WOLFSSL_X509 *x509, *x509v2;
  6093. WOLFSSL_EVP_PKEY *priv, *pub;
  6094. unsigned char *der = NULL, *key = NULL, *pt;
  6095. char *header, *name;
  6096. int derSz;
  6097. long keySz;
  6098. XFILE fp;
  6099. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  6100. time_t t;
  6101. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  6102. WOLFSSL_FILETYPE_PEM));
  6103. fp = XFOPEN(cliKeyFile, "rb");
  6104. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  6105. WOLFSSL_SUCCESS);
  6106. XFCLOSE(fp);
  6107. pt = key;
  6108. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  6109. (const unsigned char**)&pt, keySz));
  6110. /* create the version 2 certificate */
  6111. AssertNotNull(x509v2 = X509_new());
  6112. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  6113. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  6114. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  6115. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  6116. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  6117. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  6118. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  6119. t = time(NULL);
  6120. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  6121. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  6122. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  6123. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  6124. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  6125. derSz = wolfSSL_i2d_X509(x509v2, &der);
  6126. AssertIntGT(derSz, 0);
  6127. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  6128. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  6129. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  6130. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6131. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6132. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6133. wolfSSL_X509_free(x509);
  6134. wolfSSL_X509_free(x509v2);
  6135. wolfSSL_EVP_PKEY_free(priv);
  6136. wolfSSL_EVP_PKEY_free(pub);
  6137. wolfSSL_ASN1_TIME_free(notBefore);
  6138. wolfSSL_ASN1_TIME_free(notAfter);
  6139. }
  6140. /* override certificate version error */
  6141. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  6142. {
  6143. AssertIntEQ(store->error, ASN_VERSION_E);
  6144. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  6145. (void)preverify;
  6146. return 1;
  6147. }
  6148. /* set verify callback that will override bad certificate version */
  6149. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  6150. {
  6151. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  6152. }
  6153. #endif
  6154. static void test_wolfSSL_X509_TLS_version(void)
  6155. {
  6156. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  6157. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  6158. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  6159. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN)
  6160. tcp_ready ready;
  6161. func_args server_args;
  6162. func_args client_args;
  6163. THREAD_TYPE serverThread;
  6164. callback_functions func_cb_client;
  6165. callback_functions func_cb_server;
  6166. printf(testingFmt, "test_wolfSSL_X509_TLS_version");
  6167. /* test server rejects a client certificate that is not version 3 */
  6168. #ifdef WOLFSSL_TIRTOS
  6169. fdOpenSession(Task_self());
  6170. #endif
  6171. XMEMSET(&server_args, 0, sizeof(func_args));
  6172. XMEMSET(&client_args, 0, sizeof(func_args));
  6173. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  6174. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  6175. StartTCP();
  6176. InitTcpReady(&ready);
  6177. #if defined(USE_WINDOWS_API)
  6178. /* use RNG to get random port if using windows */
  6179. ready.port = GetRandomPort();
  6180. #endif
  6181. server_args.signal = &ready;
  6182. client_args.signal = &ready;
  6183. server_args.return_code = TEST_FAIL;
  6184. client_args.return_code = TEST_FAIL;
  6185. func_cb_client.ctx_ready = &test_set_x509_badversion;
  6186. #ifndef WOLFSSL_NO_TLS12
  6187. func_cb_client.method = wolfTLSv1_2_client_method;
  6188. #else
  6189. func_cb_client.method = wolfTLSv1_3_client_method;
  6190. #endif
  6191. client_args.callbacks = &func_cb_client;
  6192. #ifndef WOLFSSL_NO_TLS12
  6193. func_cb_server.method = wolfTLSv1_2_server_method;
  6194. #else
  6195. func_cb_server.method = wolfTLSv1_3_server_method;
  6196. #endif
  6197. server_args.callbacks = &func_cb_server;
  6198. start_thread(test_server_nofail, &server_args, &serverThread);
  6199. wait_tcp_ready(&server_args);
  6200. test_client_nofail(&client_args, NULL);
  6201. join_thread(serverThread);
  6202. AssertIntEQ(client_args.return_code, TEST_FAIL);
  6203. AssertIntEQ(server_args.return_code, TEST_FAIL);
  6204. FreeTcpReady(&ready);
  6205. #ifdef WOLFSSL_TIRTOS
  6206. fdCloseSession(Task_self());
  6207. #endif
  6208. /* Now re run but override the bad X509 version */
  6209. #ifdef WOLFSSL_TIRTOS
  6210. fdOpenSession(Task_self());
  6211. #endif
  6212. XMEMSET(&server_args, 0, sizeof(func_args));
  6213. XMEMSET(&client_args, 0, sizeof(func_args));
  6214. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  6215. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  6216. StartTCP();
  6217. InitTcpReady(&ready);
  6218. #if defined(USE_WINDOWS_API)
  6219. /* use RNG to get random port if using windows */
  6220. ready.port = GetRandomPort();
  6221. #endif
  6222. server_args.signal = &ready;
  6223. client_args.signal = &ready;
  6224. server_args.return_code = TEST_FAIL;
  6225. client_args.return_code = TEST_FAIL;
  6226. func_cb_client.ctx_ready = &test_set_x509_badversion;
  6227. func_cb_server.ctx_ready = &test_set_override_x509;
  6228. #ifndef WOLFSSL_NO_TLS12
  6229. func_cb_client.method = wolfTLSv1_2_client_method;
  6230. #else
  6231. func_cb_client.method = wolfTLSv1_3_client_method;
  6232. #endif
  6233. client_args.callbacks = &func_cb_client;
  6234. #ifndef WOLFSSL_NO_TLS12
  6235. func_cb_server.method = wolfTLSv1_2_server_method;
  6236. #else
  6237. func_cb_server.method = wolfTLSv1_3_server_method;
  6238. #endif
  6239. server_args.callbacks = &func_cb_server;
  6240. start_thread(test_server_nofail, &server_args, &serverThread);
  6241. wait_tcp_ready(&server_args);
  6242. test_client_nofail(&client_args, NULL);
  6243. join_thread(serverThread);
  6244. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  6245. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6246. FreeTcpReady(&ready);
  6247. #ifdef WOLFSSL_TIRTOS
  6248. fdCloseSession(Task_self());
  6249. #endif
  6250. printf(resultFmt, passed);
  6251. #endif
  6252. }
  6253. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  6254. * version allowed.
  6255. * POST: 1 on success.
  6256. */
  6257. static int test_wolfSSL_CTX_SetMinVersion(void)
  6258. {
  6259. int failFlag = WOLFSSL_SUCCESS;
  6260. #ifndef NO_WOLFSSL_CLIENT
  6261. WOLFSSL_CTX* ctx;
  6262. int itr;
  6263. #ifndef NO_OLD_TLS
  6264. const int versions[] = {
  6265. #ifdef WOLFSSL_ALLOW_TLSV10
  6266. WOLFSSL_TLSV1,
  6267. #endif
  6268. WOLFSSL_TLSV1_1,
  6269. WOLFSSL_TLSV1_2 };
  6270. #elif !defined(WOLFSSL_NO_TLS12)
  6271. const int versions[] = { WOLFSSL_TLSV1_2 };
  6272. #elif defined(WOLFSSL_TLS13)
  6273. const int versions[] = { WOLFSSL_TLSV1_3 };
  6274. #else
  6275. const int versions[0];
  6276. #endif
  6277. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6278. printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
  6279. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  6280. if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
  6281. failFlag = WOLFSSL_FAILURE;
  6282. }
  6283. }
  6284. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  6285. wolfSSL_CTX_free(ctx);
  6286. #endif
  6287. return failFlag;
  6288. } /* END test_wolfSSL_CTX_SetMinVersion */
  6289. /*----------------------------------------------------------------------------*
  6290. | OCSP Stapling
  6291. *----------------------------------------------------------------------------*/
  6292. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  6293. * need to contact the CA, lowering the cost of cert revocation checking.
  6294. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  6295. * POST: 1 returned for success.
  6296. */
  6297. static int test_wolfSSL_UseOCSPStapling(void)
  6298. {
  6299. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  6300. !defined(NO_WOLFSSL_CLIENT)
  6301. int ret;
  6302. WOLFSSL_CTX* ctx;
  6303. WOLFSSL* ssl;
  6304. #ifndef NO_WOLFSSL_CLIENT
  6305. #ifndef WOLFSSL_NO_TLS12
  6306. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  6307. #else
  6308. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  6309. #endif
  6310. #else
  6311. #ifndef WOLFSSL_NO_TLS12
  6312. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  6313. #else
  6314. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  6315. #endif
  6316. #endif
  6317. ssl = wolfSSL_new(ctx);
  6318. printf(testingFmt, "wolfSSL_UseOCSPStapling()");
  6319. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  6320. WOLFSSL_CSR2_OCSP_USE_NONCE);
  6321. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  6322. wolfSSL_free(ssl);
  6323. wolfSSL_CTX_free(ctx);
  6324. return ret;
  6325. #else
  6326. return WOLFSSL_SUCCESS;
  6327. #endif
  6328. } /*END test_wolfSSL_UseOCSPStapling */
  6329. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  6330. * stapling eliminates the need to contact the CA and lowers cert revocation
  6331. * check.
  6332. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  6333. */
  6334. static int test_wolfSSL_UseOCSPStaplingV2 (void)
  6335. {
  6336. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  6337. !defined(NO_WOLFSSL_CLIENT)
  6338. int ret;
  6339. WOLFSSL_CTX* ctx;
  6340. WOLFSSL* ssl;
  6341. #ifndef NO_WOLFSSL_CLIENT
  6342. #ifndef WOLFSSL_NO_TLS12
  6343. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  6344. #else
  6345. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  6346. #endif
  6347. #else
  6348. #ifndef WOLFSSL_NO_TLS12
  6349. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  6350. #else
  6351. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  6352. #endif
  6353. #endif
  6354. ssl = wolfSSL_new(ctx);
  6355. printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
  6356. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  6357. WOLFSSL_CSR2_OCSP_USE_NONCE );
  6358. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  6359. wolfSSL_free(ssl);
  6360. wolfSSL_CTX_free(ctx);
  6361. return ret;
  6362. #else
  6363. return WOLFSSL_SUCCESS;
  6364. #endif
  6365. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  6366. /*----------------------------------------------------------------------------*
  6367. | Multicast Tests
  6368. *----------------------------------------------------------------------------*/
  6369. static void test_wolfSSL_mcast(void)
  6370. {
  6371. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  6372. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  6373. WOLFSSL_CTX* ctx;
  6374. WOLFSSL* ssl;
  6375. int result;
  6376. byte preMasterSecret[512];
  6377. byte clientRandom[32];
  6378. byte serverRandom[32];
  6379. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  6380. byte buf[256];
  6381. word16 newId;
  6382. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  6383. AssertNotNull(ctx);
  6384. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  6385. AssertIntEQ(result, WOLFSSL_SUCCESS);
  6386. ssl = wolfSSL_new(ctx);
  6387. AssertNotNull(ssl);
  6388. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  6389. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  6390. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  6391. result = wolfSSL_set_secret(ssl, 23,
  6392. preMasterSecret, sizeof(preMasterSecret),
  6393. clientRandom, serverRandom, suite);
  6394. AssertIntEQ(result, WOLFSSL_SUCCESS);
  6395. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  6396. AssertIntLE(result, 0);
  6397. AssertIntLE(newId, 100);
  6398. wolfSSL_free(ssl);
  6399. wolfSSL_CTX_free(ctx);
  6400. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 || WOLFSSL_SNIFFER) */
  6401. }
  6402. /*----------------------------------------------------------------------------*
  6403. | Wolfcrypt
  6404. *----------------------------------------------------------------------------*/
  6405. /*
  6406. * Unit test for the wc_InitBlake2b()
  6407. */
  6408. static int test_wc_InitBlake2b (void)
  6409. {
  6410. int ret = 0;
  6411. #ifdef HAVE_BLAKE2
  6412. Blake2b blake2b;
  6413. printf(testingFmt, "wc_InitBlake2B()");
  6414. /* Test good arg. */
  6415. ret = wc_InitBlake2b(&blake2b, 64);
  6416. if (ret != 0) {
  6417. ret = WOLFSSL_FATAL_ERROR;
  6418. }
  6419. /* Test bad arg. */
  6420. if (!ret) {
  6421. ret = wc_InitBlake2b(NULL, 64);
  6422. if (ret == 0) {
  6423. ret = WOLFSSL_FATAL_ERROR;
  6424. } else {
  6425. ret = 0;
  6426. }
  6427. }
  6428. if (!ret) {
  6429. ret = wc_InitBlake2b(NULL, 128);
  6430. if (ret == 0) {
  6431. ret = WOLFSSL_FATAL_ERROR;
  6432. } else {
  6433. ret = 0;
  6434. }
  6435. }
  6436. if (!ret) {
  6437. ret = wc_InitBlake2b(&blake2b, 128);
  6438. if (ret == 0) {
  6439. ret = WOLFSSL_FATAL_ERROR;
  6440. } else {
  6441. ret = 0;
  6442. }
  6443. }
  6444. if (!ret) {
  6445. ret = wc_InitBlake2b(NULL, 0);
  6446. if (ret == 0) {
  6447. ret = WOLFSSL_FATAL_ERROR;
  6448. } else {
  6449. ret = 0;
  6450. }
  6451. }
  6452. if (!ret) {
  6453. ret = wc_InitBlake2b(&blake2b, 0);
  6454. if (ret == 0) {
  6455. ret = WOLFSSL_FATAL_ERROR;
  6456. } else {
  6457. ret = 0;
  6458. }
  6459. }
  6460. printf(resultFmt, ret == 0 ? passed : failed);
  6461. #endif
  6462. return ret;
  6463. } /*END test_wc_InitBlake2b*/
  6464. /*
  6465. * Unit test for the wc_InitBlake2b_WithKey()
  6466. */
  6467. static int test_wc_InitBlake2b_WithKey (void)
  6468. {
  6469. int ret = 0;
  6470. #ifdef HAVE_BLAKE2
  6471. Blake2b blake2b;
  6472. word32 digestSz = BLAKE2B_KEYBYTES;
  6473. byte key[BLAKE2B_KEYBYTES];
  6474. word32 keylen = BLAKE2B_KEYBYTES;
  6475. printf(testingFmt, "wc_InitBlake2b_WithKey()");
  6476. /* Test good arg. */
  6477. ret = wc_InitBlake2b_WithKey(&blake2b, digestSz, key, keylen);
  6478. if (ret != 0) {
  6479. ret = WOLFSSL_FATAL_ERROR;
  6480. }
  6481. /* Test bad args. */
  6482. if (ret == 0) {
  6483. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  6484. if (ret == BAD_FUNC_ARG) {
  6485. ret = 0;
  6486. }
  6487. }
  6488. if (ret == 0) {
  6489. ret = wc_InitBlake2b_WithKey(&blake2b, digestSz, key, 256);
  6490. if (ret == BAD_FUNC_ARG) {
  6491. ret = 0;
  6492. }
  6493. }
  6494. if (ret == 0) {
  6495. ret = wc_InitBlake2b_WithKey(&blake2b, digestSz, NULL, keylen);
  6496. }
  6497. printf(resultFmt, ret == 0 ? passed : failed);
  6498. #endif
  6499. return ret;
  6500. } /*END wc_InitBlake2b_WithKey*/
  6501. /*
  6502. * Unit test for the wc_InitBlake2s_WithKey()
  6503. */
  6504. static int test_wc_InitBlake2s_WithKey (void)
  6505. {
  6506. int ret = 0;
  6507. #ifdef HAVE_BLAKE2S
  6508. Blake2s blake2s;
  6509. word32 digestSz = BLAKE2S_KEYBYTES;
  6510. byte *key = (byte*)"01234567890123456789012345678901";
  6511. word32 keylen = BLAKE2S_KEYBYTES;
  6512. printf(testingFmt, "wc_InitBlake2s_WithKey()");
  6513. /* Test good arg. */
  6514. ret = wc_InitBlake2s_WithKey(&blake2s, digestSz, key, keylen);
  6515. if (ret != 0) {
  6516. ret = WOLFSSL_FATAL_ERROR;
  6517. }
  6518. /* Test bad args. */
  6519. if (ret == 0) {
  6520. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  6521. if (ret == BAD_FUNC_ARG) {
  6522. ret = 0;
  6523. }
  6524. }
  6525. if (ret == 0) {
  6526. ret = wc_InitBlake2s_WithKey(&blake2s, digestSz, key, 256);
  6527. if (ret == BAD_FUNC_ARG) {
  6528. ret = 0;
  6529. }
  6530. }
  6531. if (ret == 0) {
  6532. ret = wc_InitBlake2s_WithKey(&blake2s, digestSz, NULL, keylen);
  6533. }
  6534. printf(resultFmt, ret == 0 ? passed : failed);
  6535. #endif
  6536. return ret;
  6537. } /*END wc_InitBlake2s_WithKey*/
  6538. /*
  6539. * Unit test for the wc_InitMd5()
  6540. */
  6541. static int test_wc_InitMd5 (void)
  6542. {
  6543. int flag = 0;
  6544. #ifndef NO_MD5
  6545. wc_Md5 md5;
  6546. int ret;
  6547. printf(testingFmt, "wc_InitMd5()");
  6548. /* Test good arg. */
  6549. ret = wc_InitMd5(&md5);
  6550. if (ret != 0) {
  6551. flag = WOLFSSL_FATAL_ERROR;
  6552. }
  6553. /* Test bad arg. */
  6554. if (!flag) {
  6555. ret = wc_InitMd5(NULL);
  6556. if (ret != BAD_FUNC_ARG) {
  6557. flag = WOLFSSL_FATAL_ERROR;
  6558. }
  6559. }
  6560. wc_Md5Free(&md5);
  6561. printf(resultFmt, flag == 0 ? passed : failed);
  6562. #endif
  6563. return flag;
  6564. } /* END test_wc_InitMd5 */
  6565. /*
  6566. * Testing wc_UpdateMd5()
  6567. */
  6568. static int test_wc_Md5Update (void)
  6569. {
  6570. int flag = 0;
  6571. #ifndef NO_MD5
  6572. wc_Md5 md5;
  6573. byte hash[WC_MD5_DIGEST_SIZE];
  6574. testVector a, b, c;
  6575. int ret;
  6576. ret = wc_InitMd5(&md5);
  6577. if (ret != 0) {
  6578. flag = ret;
  6579. }
  6580. printf(testingFmt, "wc_Md5Update()");
  6581. /* Input */
  6582. if (!flag) {
  6583. a.input = "a";
  6584. a.inLen = XSTRLEN(a.input);
  6585. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  6586. if (ret != 0) {
  6587. flag = ret;
  6588. }
  6589. }
  6590. if (!flag) {
  6591. ret = wc_Md5Final(&md5, hash);
  6592. if (ret != 0) {
  6593. flag = ret;
  6594. }
  6595. }
  6596. /* Update input. */
  6597. if (!flag) {
  6598. a.input = "abc";
  6599. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  6600. "\x72";
  6601. a.inLen = XSTRLEN(a.input);
  6602. a.outLen = XSTRLEN(a.output);
  6603. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  6604. if (ret != 0) {
  6605. flag = ret;
  6606. }
  6607. }
  6608. if (!flag) {
  6609. ret = wc_Md5Final(&md5, hash);
  6610. if (ret != 0) {
  6611. flag = ret;
  6612. }
  6613. }
  6614. if (!flag) {
  6615. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  6616. flag = WOLFSSL_FATAL_ERROR;
  6617. }
  6618. }
  6619. /*Pass in bad values. */
  6620. if (!flag) {
  6621. b.input = NULL;
  6622. b.inLen = 0;
  6623. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  6624. if (ret != 0) {
  6625. flag = ret;
  6626. }
  6627. }
  6628. if (!flag) {
  6629. c.input = NULL;
  6630. c.inLen = WC_MD5_DIGEST_SIZE;
  6631. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  6632. if (ret != BAD_FUNC_ARG) {
  6633. flag = WOLFSSL_FATAL_ERROR;
  6634. }
  6635. }
  6636. if (!flag) {
  6637. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  6638. if (ret != BAD_FUNC_ARG) {
  6639. flag = WOLFSSL_FATAL_ERROR;
  6640. }
  6641. }
  6642. wc_Md5Free(&md5);
  6643. printf(resultFmt, flag == 0 ? passed : failed);
  6644. #endif
  6645. return flag;
  6646. } /* END test_wc_Md5Update() */
  6647. /*
  6648. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  6649. */
  6650. static int test_wc_Md5Final (void)
  6651. {
  6652. int flag = 0;
  6653. #ifndef NO_MD5
  6654. /* Instantiate */
  6655. wc_Md5 md5;
  6656. byte* hash_test[3];
  6657. byte hash1[WC_MD5_DIGEST_SIZE];
  6658. byte hash2[2*WC_MD5_DIGEST_SIZE];
  6659. byte hash3[5*WC_MD5_DIGEST_SIZE];
  6660. int times, i, ret;
  6661. /* Initialize */
  6662. ret = wc_InitMd5(&md5);
  6663. if (ret != 0) {
  6664. flag = ret;
  6665. }
  6666. if (!flag) {
  6667. hash_test[0] = hash1;
  6668. hash_test[1] = hash2;
  6669. hash_test[2] = hash3;
  6670. }
  6671. times = sizeof(hash_test)/sizeof(byte*);
  6672. /* Test good args. */
  6673. printf(testingFmt, "wc_Md5Final()");
  6674. for (i = 0; i < times; i++) {
  6675. if (!flag) {
  6676. ret = wc_Md5Final(&md5, hash_test[i]);
  6677. if (ret != 0) {
  6678. flag = WOLFSSL_FATAL_ERROR;
  6679. }
  6680. }
  6681. }
  6682. /* Test bad args. */
  6683. if (!flag) {
  6684. ret = wc_Md5Final(NULL, NULL);
  6685. if (ret != BAD_FUNC_ARG) {
  6686. flag = WOLFSSL_FATAL_ERROR;
  6687. }
  6688. }
  6689. if (!flag) {
  6690. ret = wc_Md5Final(NULL, hash1);
  6691. if (ret != BAD_FUNC_ARG) {
  6692. flag = WOLFSSL_FATAL_ERROR;
  6693. }
  6694. }
  6695. if (!flag) {
  6696. ret = wc_Md5Final(&md5, NULL);
  6697. if (ret != BAD_FUNC_ARG) {
  6698. flag = WOLFSSL_FATAL_ERROR;
  6699. }
  6700. }
  6701. wc_Md5Free(&md5);
  6702. printf(resultFmt, flag == 0 ? passed : failed);
  6703. #endif
  6704. return flag;
  6705. }
  6706. /*
  6707. * Unit test for the wc_InitSha()
  6708. */
  6709. static int test_wc_InitSha(void)
  6710. {
  6711. int flag = 0;
  6712. #ifndef NO_SHA
  6713. wc_Sha sha;
  6714. int ret;
  6715. printf(testingFmt, "wc_InitSha()");
  6716. /* Test good arg. */
  6717. ret = wc_InitSha(&sha);
  6718. if (ret != 0) {
  6719. flag = WOLFSSL_FATAL_ERROR;
  6720. }
  6721. /* Test bad arg. */
  6722. if (!flag) {
  6723. ret = wc_InitSha(NULL);
  6724. if (ret != BAD_FUNC_ARG) {
  6725. flag = WOLFSSL_FATAL_ERROR;
  6726. }
  6727. }
  6728. wc_ShaFree(&sha);
  6729. printf(resultFmt, flag == 0 ? passed : failed);
  6730. #endif
  6731. return flag;
  6732. } /* END test_wc_InitSha */
  6733. /*
  6734. * Tesing wc_ShaUpdate()
  6735. */
  6736. static int test_wc_ShaUpdate (void)
  6737. {
  6738. int flag = 0;
  6739. #ifndef NO_SHA
  6740. wc_Sha sha;
  6741. byte hash[WC_SHA_DIGEST_SIZE];
  6742. testVector a, b, c;
  6743. int ret;
  6744. ret = wc_InitSha(&sha);
  6745. if (ret != 0) {
  6746. flag = ret;
  6747. }
  6748. printf(testingFmt, "wc_ShaUpdate()");
  6749. /* Input. */
  6750. if (!flag) {
  6751. a.input = "a";
  6752. a.inLen = XSTRLEN(a.input);
  6753. ret = wc_ShaUpdate(&sha, NULL, 0);
  6754. if (ret != 0) {
  6755. flag = ret;
  6756. }
  6757. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  6758. if (ret != 0) {
  6759. flag = ret;
  6760. }
  6761. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  6762. if (ret != 0) {
  6763. flag = ret;
  6764. }
  6765. }
  6766. if (!flag) {
  6767. ret = wc_ShaFinal(&sha, hash);
  6768. if (ret != 0) {
  6769. flag = ret;
  6770. }
  6771. }
  6772. /* Update input. */
  6773. if (!flag) {
  6774. a.input = "abc";
  6775. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  6776. "\x6C\x9C\xD0\xD8\x9D";
  6777. a.inLen = XSTRLEN(a.input);
  6778. a.outLen = XSTRLEN(a.output);
  6779. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  6780. if (ret != 0) {
  6781. flag = ret;
  6782. }
  6783. }
  6784. if (!flag) {
  6785. ret = wc_ShaFinal(&sha, hash);
  6786. if (ret !=0) {
  6787. flag = ret;
  6788. }
  6789. }
  6790. if (!flag) {
  6791. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  6792. flag = WOLFSSL_FATAL_ERROR;
  6793. }
  6794. }
  6795. /* Try passing in bad values. */
  6796. if (!flag) {
  6797. b.input = NULL;
  6798. b.inLen = 0;
  6799. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  6800. if (ret != 0) {
  6801. flag = ret;
  6802. }
  6803. }
  6804. if (!flag) {
  6805. c.input = NULL;
  6806. c.inLen = WC_SHA_DIGEST_SIZE;
  6807. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  6808. if (ret != BAD_FUNC_ARG) {
  6809. flag = WOLFSSL_FATAL_ERROR;
  6810. }
  6811. }
  6812. if (!flag) {
  6813. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  6814. if (ret != BAD_FUNC_ARG) {
  6815. flag = WOLFSSL_FATAL_ERROR;
  6816. }
  6817. }
  6818. wc_ShaFree(&sha);
  6819. /* If not returned then the unit test passed test vectors. */
  6820. printf(resultFmt, flag == 0 ? passed : failed);
  6821. #endif
  6822. return flag;
  6823. } /* END test_wc_ShaUpdate() */
  6824. /*
  6825. * Unit test on wc_ShaFinal
  6826. */
  6827. static int test_wc_ShaFinal (void)
  6828. {
  6829. int flag = 0;
  6830. #ifndef NO_SHA
  6831. wc_Sha sha;
  6832. byte* hash_test[3];
  6833. byte hash1[WC_SHA_DIGEST_SIZE];
  6834. byte hash2[2*WC_SHA_DIGEST_SIZE];
  6835. byte hash3[5*WC_SHA_DIGEST_SIZE];
  6836. int times, i, ret;
  6837. /*Initialize*/
  6838. ret = wc_InitSha(&sha);
  6839. if (ret) {
  6840. flag = ret;
  6841. }
  6842. if (!flag) {
  6843. hash_test[0] = hash1;
  6844. hash_test[1] = hash2;
  6845. hash_test[2] = hash3;
  6846. }
  6847. times = sizeof(hash_test)/sizeof(byte*);
  6848. /* Good test args. */
  6849. printf(testingFmt, "wc_ShaFinal()");
  6850. for (i = 0; i < times; i++) {
  6851. if (!flag) {
  6852. ret = wc_ShaFinal(&sha, hash_test[i]);
  6853. if (ret != 0) {
  6854. flag = WOLFSSL_FATAL_ERROR;
  6855. }
  6856. }
  6857. }
  6858. /* Test bad args. */
  6859. if (!flag) {
  6860. ret = wc_ShaFinal(NULL, NULL);
  6861. if (ret != BAD_FUNC_ARG) {
  6862. flag = WOLFSSL_FATAL_ERROR;
  6863. }
  6864. }
  6865. if (!flag) {
  6866. ret = wc_ShaFinal(NULL, hash1);
  6867. if (ret != BAD_FUNC_ARG) {
  6868. flag = WOLFSSL_FATAL_ERROR;
  6869. }
  6870. }
  6871. if (!flag) {
  6872. ret = wc_ShaFinal(&sha, NULL);
  6873. if (ret != BAD_FUNC_ARG) {
  6874. flag = WOLFSSL_FATAL_ERROR;
  6875. }
  6876. }
  6877. wc_ShaFree(&sha);
  6878. printf(resultFmt, flag == 0 ? passed : failed);
  6879. #endif
  6880. return flag;
  6881. } /* END test_wc_ShaFinal */
  6882. /*
  6883. * Unit test for wc_InitSha256()
  6884. */
  6885. static int test_wc_InitSha256 (void)
  6886. {
  6887. int flag = 0;
  6888. #ifndef NO_SHA256
  6889. wc_Sha256 sha256;
  6890. int ret;
  6891. printf(testingFmt, "wc_InitSha256()");
  6892. /* Test good arg. */
  6893. ret = wc_InitSha256(&sha256);
  6894. if (ret != 0) {
  6895. flag = WOLFSSL_FATAL_ERROR;
  6896. }
  6897. /* Test bad arg. */
  6898. if (!flag) {
  6899. ret = wc_InitSha256(NULL);
  6900. if (ret != BAD_FUNC_ARG) {
  6901. flag = WOLFSSL_FATAL_ERROR;
  6902. }
  6903. }
  6904. wc_Sha256Free(&sha256);
  6905. printf(resultFmt, flag == 0 ? passed : failed);
  6906. #endif
  6907. return flag;
  6908. } /* END test_wc_InitSha256 */
  6909. /*
  6910. * Unit test for wc_Sha256Update()
  6911. */
  6912. static int test_wc_Sha256Update (void)
  6913. {
  6914. int flag = 0;
  6915. #ifndef NO_SHA256
  6916. wc_Sha256 sha256;
  6917. byte hash[WC_SHA256_DIGEST_SIZE];
  6918. testVector a, b, c;
  6919. int ret;
  6920. ret = wc_InitSha256(&sha256);
  6921. if (ret != 0) {
  6922. flag = ret;
  6923. }
  6924. printf(testingFmt, "wc_Sha256Update()");
  6925. /* Input. */
  6926. if (!flag) {
  6927. a.input = "a";
  6928. a.inLen = XSTRLEN(a.input);
  6929. ret = wc_Sha256Update(&sha256, NULL, 0);
  6930. if (ret != 0) {
  6931. flag = ret;
  6932. }
  6933. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  6934. if (ret != 0) {
  6935. flag = ret;
  6936. }
  6937. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  6938. if (ret != 0) {
  6939. flag = ret;
  6940. }
  6941. }
  6942. if (!flag) {
  6943. ret = wc_Sha256Final(&sha256, hash);
  6944. if (ret != 0) {
  6945. flag = ret;
  6946. }
  6947. }
  6948. /* Update input. */
  6949. if (!flag) {
  6950. a.input = "abc";
  6951. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  6952. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  6953. "\x15\xAD";
  6954. a.inLen = XSTRLEN(a.input);
  6955. a.outLen = XSTRLEN(a.output);
  6956. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  6957. if (ret != 0) {
  6958. flag = ret;
  6959. }
  6960. }
  6961. if (!flag) {
  6962. ret = wc_Sha256Final(&sha256, hash);
  6963. if (ret != 0) {
  6964. flag = ret;
  6965. }
  6966. }
  6967. if (!flag) {
  6968. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  6969. flag = WOLFSSL_FATAL_ERROR;
  6970. }
  6971. }
  6972. /* Try passing in bad values */
  6973. if (!flag) {
  6974. b.input = NULL;
  6975. b.inLen = 0;
  6976. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  6977. if (ret != 0) {
  6978. flag = ret;
  6979. }
  6980. }
  6981. if (!flag) {
  6982. c.input = NULL;
  6983. c.inLen = WC_SHA256_DIGEST_SIZE;
  6984. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  6985. if (ret != BAD_FUNC_ARG) {
  6986. flag = WOLFSSL_FATAL_ERROR;
  6987. }
  6988. }
  6989. if (!flag) {
  6990. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  6991. if (ret != BAD_FUNC_ARG) {
  6992. flag = WOLFSSL_FATAL_ERROR;
  6993. }
  6994. }
  6995. wc_Sha256Free(&sha256);
  6996. /* If not returned then the unit test passed. */
  6997. printf(resultFmt, flag == 0 ? passed : failed);
  6998. #endif
  6999. return flag;
  7000. } /* END test_wc_Sha256Update */
  7001. /*
  7002. * Unit test function for wc_Sha256Final()
  7003. */
  7004. static int test_wc_Sha256Final (void)
  7005. {
  7006. int flag = 0;
  7007. #ifndef NO_SHA256
  7008. wc_Sha256 sha256;
  7009. byte* hash_test[3];
  7010. byte hash1[WC_SHA256_DIGEST_SIZE];
  7011. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  7012. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  7013. int times, i, ret;
  7014. /* Initialize */
  7015. ret = wc_InitSha256(&sha256);
  7016. if (ret != 0) {
  7017. flag = ret;
  7018. }
  7019. if (!flag) {
  7020. hash_test[0] = hash1;
  7021. hash_test[1] = hash2;
  7022. hash_test[2] = hash3;
  7023. }
  7024. times = sizeof(hash_test) / sizeof(byte*);
  7025. /* Good test args. */
  7026. printf(testingFmt, "wc_Sha256Final()");
  7027. for (i = 0; i < times; i++) {
  7028. if (!flag) {
  7029. ret = wc_Sha256Final(&sha256, hash_test[i]);
  7030. if (ret != 0) {
  7031. flag = WOLFSSL_FATAL_ERROR;
  7032. }
  7033. }
  7034. }
  7035. /* Test bad args. */
  7036. if (!flag ) {
  7037. ret = wc_Sha256Final(NULL, NULL);
  7038. if (ret != BAD_FUNC_ARG) {
  7039. flag = WOLFSSL_FATAL_ERROR;
  7040. }
  7041. }
  7042. if (!flag) {
  7043. ret = wc_Sha256Final(NULL, hash1);
  7044. if (ret != BAD_FUNC_ARG) {
  7045. flag = WOLFSSL_FATAL_ERROR;
  7046. }
  7047. }
  7048. if (!flag) {
  7049. ret = wc_Sha256Final(&sha256, NULL);
  7050. if (ret != BAD_FUNC_ARG) {
  7051. flag = WOLFSSL_FATAL_ERROR;
  7052. }
  7053. }
  7054. wc_Sha256Free(&sha256);
  7055. printf(resultFmt, flag == 0 ? passed : failed);
  7056. #endif
  7057. return flag;
  7058. } /* END test_wc_Sha256Final */
  7059. /*
  7060. * Unit test function for wc_Sha256FinalRaw()
  7061. */
  7062. static int test_wc_Sha256FinalRaw (void)
  7063. {
  7064. int flag = 0;
  7065. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  7066. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  7067. !defined(WOLFSSL_NO_HASH_RAW)
  7068. wc_Sha256 sha256;
  7069. byte* hash_test[3];
  7070. byte hash1[WC_SHA256_DIGEST_SIZE];
  7071. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  7072. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  7073. int times, i, ret;
  7074. /* Initialize */
  7075. ret = wc_InitSha256(&sha256);
  7076. if (ret != 0) {
  7077. flag = ret;
  7078. }
  7079. if (!flag) {
  7080. hash_test[0] = hash1;
  7081. hash_test[1] = hash2;
  7082. hash_test[2] = hash3;
  7083. }
  7084. times = sizeof(hash_test) / sizeof(byte*);
  7085. /* Good test args. */
  7086. printf(testingFmt, "wc_Sha256FinalRaw()");
  7087. for (i = 0; i < times; i++) {
  7088. if (!flag) {
  7089. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  7090. if (ret != 0) {
  7091. flag = WOLFSSL_FATAL_ERROR;
  7092. }
  7093. }
  7094. }
  7095. /* Test bad args. */
  7096. if (!flag ) {
  7097. ret = wc_Sha256FinalRaw(NULL, NULL);
  7098. if (ret != BAD_FUNC_ARG) {
  7099. flag = WOLFSSL_FATAL_ERROR;
  7100. }
  7101. }
  7102. if (!flag) {
  7103. ret = wc_Sha256FinalRaw(NULL, hash1);
  7104. if (ret != BAD_FUNC_ARG) {
  7105. flag = WOLFSSL_FATAL_ERROR;
  7106. }
  7107. }
  7108. if (!flag) {
  7109. ret = wc_Sha256FinalRaw(&sha256, NULL);
  7110. if (ret != BAD_FUNC_ARG) {
  7111. flag = WOLFSSL_FATAL_ERROR;
  7112. }
  7113. }
  7114. wc_Sha256Free(&sha256);
  7115. printf(resultFmt, flag == 0 ? passed : failed);
  7116. #endif
  7117. return flag;
  7118. } /* END test_wc_Sha256FinalRaw */
  7119. /*
  7120. * Unit test function for wc_Sha256GetFlags()
  7121. */
  7122. static int test_wc_Sha256GetFlags (void)
  7123. {
  7124. int flag = 0;
  7125. #if !defined(NO_SHA256) && \
  7126. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  7127. wc_Sha256 sha256;
  7128. word32 flags = 0;
  7129. printf(testingFmt, "wc_Sha256GetFlags()");
  7130. /* Initialize */
  7131. flag = wc_InitSha256(&sha256);
  7132. if (flag == 0) {
  7133. flag = wc_Sha256GetFlags(&sha256, &flags);
  7134. }
  7135. if (flag == 0) {
  7136. if (flags & WC_HASH_FLAG_ISCOPY) {
  7137. flag = 0;
  7138. }
  7139. }
  7140. wc_Sha256Free(&sha256);
  7141. printf(resultFmt, flag == 0 ? passed : failed);
  7142. #endif
  7143. return flag;
  7144. } /* END test_wc_Sha256GetFlags */
  7145. /*
  7146. * Unit test function for wc_Sha256Free()
  7147. */
  7148. static int test_wc_Sha256Free (void)
  7149. {
  7150. int flag = 0;
  7151. #ifndef NO_SHA256
  7152. printf(testingFmt, "wc_Sha256Free()");
  7153. wc_Sha256Free(NULL);
  7154. printf(resultFmt, flag == 0 ? passed : failed);
  7155. #endif
  7156. return flag;
  7157. } /* END test_wc_Sha256Free */
  7158. /*
  7159. * Unit test function for wc_Sha256GetHash()
  7160. */
  7161. static int test_wc_Sha256GetHash (void)
  7162. {
  7163. int flag = 0;
  7164. #ifndef NO_SHA256
  7165. wc_Sha256 sha256;
  7166. byte hash1[WC_SHA256_DIGEST_SIZE];
  7167. printf(testingFmt, "wc_Sha256GetHash()");
  7168. /* Initialize */
  7169. flag = wc_InitSha256(&sha256);
  7170. if (flag == 0) {
  7171. flag = wc_Sha256GetHash(&sha256, hash1);
  7172. }
  7173. /*test bad arguements*/
  7174. if (flag == 0) {
  7175. flag = wc_Sha256GetHash(NULL, NULL);
  7176. if (flag == BAD_FUNC_ARG) {
  7177. flag = 0;
  7178. }
  7179. }
  7180. if (flag == 0) {
  7181. flag = wc_Sha256GetHash(NULL, hash1);
  7182. if (flag == BAD_FUNC_ARG) {
  7183. flag = 0;
  7184. }
  7185. }
  7186. if (flag == 0) {
  7187. flag = wc_Sha256GetHash(&sha256, NULL);
  7188. if (flag == BAD_FUNC_ARG) {
  7189. flag = 0;
  7190. }
  7191. }
  7192. wc_Sha256Free(&sha256);
  7193. printf(resultFmt, flag == 0 ? passed : failed);
  7194. #endif
  7195. return flag;
  7196. } /* END test_wc_Sha256GetHash */
  7197. /*
  7198. * Unit test function for wc_Sha256Copy()
  7199. */
  7200. static int test_wc_Sha256Copy (void)
  7201. {
  7202. int flag = 0;
  7203. #ifndef NO_SHA256
  7204. wc_Sha256 sha256;
  7205. wc_Sha256 temp;
  7206. printf(testingFmt, "wc_Sha256Copy()");
  7207. /* Initialize */
  7208. flag = wc_InitSha256(&sha256);
  7209. if (flag == 0) {
  7210. flag = wc_InitSha256(&temp);
  7211. }
  7212. if (flag == 0) {
  7213. flag = wc_Sha256Copy(&sha256, &temp);
  7214. }
  7215. /*test bad arguements*/
  7216. if (flag == 0) {
  7217. flag = wc_Sha256Copy(NULL, NULL);
  7218. if (flag == BAD_FUNC_ARG) {
  7219. flag = 0;
  7220. }
  7221. }
  7222. if (flag == 0) {
  7223. flag = wc_Sha256Copy(NULL, &temp);
  7224. if (flag == BAD_FUNC_ARG) {
  7225. flag = 0;
  7226. }
  7227. }
  7228. if (flag == 0) {
  7229. flag = wc_Sha256Copy(&sha256, NULL);
  7230. if (flag == BAD_FUNC_ARG) {
  7231. flag = 0;
  7232. }
  7233. }
  7234. wc_Sha256Free(&sha256);
  7235. wc_Sha256Free(&temp);
  7236. printf(resultFmt, flag == 0 ? passed : failed);
  7237. #endif
  7238. return flag;
  7239. } /* END test_wc_Sha256Copy */
  7240. /*
  7241. * Testing wc_InitSha512()
  7242. */
  7243. static int test_wc_InitSha512 (void)
  7244. {
  7245. int flag = 0;
  7246. #ifdef WOLFSSL_SHA512
  7247. wc_Sha512 sha512;
  7248. int ret;
  7249. printf(testingFmt, "wc_InitSha512()");
  7250. /* Test good arg. */
  7251. ret = wc_InitSha512(&sha512);
  7252. if (ret != 0) {
  7253. flag = WOLFSSL_FATAL_ERROR;
  7254. }
  7255. /* Test bad arg. */
  7256. if (!flag) {
  7257. ret = wc_InitSha512(NULL);
  7258. if (ret != BAD_FUNC_ARG) {
  7259. flag = WOLFSSL_FATAL_ERROR;
  7260. }
  7261. }
  7262. wc_Sha512Free(&sha512);
  7263. printf(resultFmt, flag == 0 ? passed : failed);
  7264. #endif
  7265. return flag;
  7266. } /* END test_wc_InitSha512 */
  7267. /*
  7268. * wc_Sha512Update() test.
  7269. */
  7270. static int test_wc_Sha512Update (void)
  7271. {
  7272. int flag = 0;
  7273. #ifdef WOLFSSL_SHA512
  7274. wc_Sha512 sha512;
  7275. byte hash[WC_SHA512_DIGEST_SIZE];
  7276. testVector a, b, c;
  7277. int ret;
  7278. ret = wc_InitSha512(&sha512);
  7279. if (ret != 0) {
  7280. flag = ret;
  7281. }
  7282. printf(testingFmt, "wc_Sha512Update()");
  7283. /* Input. */
  7284. if (!flag) {
  7285. a.input = "a";
  7286. a.inLen = XSTRLEN(a.input);
  7287. ret = wc_Sha512Update(&sha512, NULL, 0);
  7288. if (ret != 0) {
  7289. flag = ret;
  7290. }
  7291. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  7292. if (ret != 0) {
  7293. flag = ret;
  7294. }
  7295. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  7296. if (ret != 0) {
  7297. flag = ret;
  7298. }
  7299. ret = wc_Sha512Final(&sha512, hash);
  7300. if (ret != 0) {
  7301. flag = ret;
  7302. }
  7303. }
  7304. /* Update input. */
  7305. if (!flag) {
  7306. a.input = "abc";
  7307. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  7308. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  7309. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  7310. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  7311. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  7312. a.inLen = XSTRLEN(a.input);
  7313. a.outLen = XSTRLEN(a.output);
  7314. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  7315. if (ret != 0) {
  7316. flag = ret;
  7317. }
  7318. }
  7319. if (!flag) {
  7320. ret = wc_Sha512Final(&sha512, hash);
  7321. if (ret != 0) {
  7322. flag = ret;
  7323. }
  7324. }
  7325. if (!flag) {
  7326. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  7327. flag = WOLFSSL_FATAL_ERROR;
  7328. }
  7329. }
  7330. /* Try passing in bad values */
  7331. if (!flag) {
  7332. b.input = NULL;
  7333. b.inLen = 0;
  7334. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  7335. if (ret != 0) {
  7336. flag = ret;
  7337. }
  7338. }
  7339. if (!flag) {
  7340. c.input = NULL;
  7341. c.inLen = WC_SHA512_DIGEST_SIZE;
  7342. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  7343. if (ret != BAD_FUNC_ARG) {
  7344. flag = WOLFSSL_FATAL_ERROR;
  7345. }
  7346. }
  7347. if (!flag) {
  7348. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  7349. if (ret != BAD_FUNC_ARG) {
  7350. flag = WOLFSSL_FATAL_ERROR;
  7351. }
  7352. }
  7353. wc_Sha512Free(&sha512);
  7354. /* If not returned then the unit test passed test vectors. */
  7355. printf(resultFmt, flag == 0 ? passed : failed);
  7356. #endif
  7357. return flag;
  7358. } /* END test_wc_Sha512Update */
  7359. #ifdef WOLFSSL_SHA512
  7360. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  7361. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  7362. /* Perfoms test for
  7363. * - wc_Sha512Final/wc_Sha512FinalRaw
  7364. * - wc_Sha512_224Final/wc_Sha512_224Final
  7365. * - wc_Sha512_256Final/wc_Sha512_256Final
  7366. * parameter:
  7367. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  7368. * WC_HASH_TYPE_SHA512_256
  7369. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  7370. *return 0 on success
  7371. */
  7372. static int test_Sha512_Family_Final(int type, int isRaw)
  7373. {
  7374. wc_Sha512 sha512;
  7375. byte* hash_test[3];
  7376. byte hash1[WC_SHA512_DIGEST_SIZE];
  7377. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  7378. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  7379. int times, i, ret;
  7380. int(*initFp)(wc_Sha512*);
  7381. int(*finalFp)(wc_Sha512*, byte*);
  7382. void(*freeFp)(wc_Sha512*);
  7383. if (type == WC_HASH_TYPE_SHA512) {
  7384. initFp = wc_InitSha512;
  7385. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7386. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  7387. #else
  7388. finalFp = (isRaw)? NULL : wc_Sha512Final;
  7389. #endif
  7390. freeFp = wc_Sha512Free;
  7391. }
  7392. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7393. #if !defined(WOLFSSL_NOSHA512_224)
  7394. else if (type == WC_HASH_TYPE_SHA512_224) {
  7395. initFp = wc_InitSha512_224;
  7396. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  7397. freeFp = wc_Sha512_224Free;
  7398. }
  7399. #endif
  7400. #if !defined(WOLFSSL_NOSHA512_256)
  7401. else if (type == WC_HASH_TYPE_SHA512_256) {
  7402. initFp = wc_InitSha512_256;
  7403. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  7404. freeFp = wc_Sha512_256Free;
  7405. }
  7406. #endif
  7407. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7408. else
  7409. return BAD_FUNC_ARG;
  7410. /* Initialize */
  7411. ret = initFp(&sha512);
  7412. if (!ret) {
  7413. hash_test[0] = hash1;
  7414. hash_test[1] = hash2;
  7415. hash_test[2] = hash3;
  7416. }
  7417. times = sizeof(hash_test) / sizeof(byte *);
  7418. /* Good test args. */
  7419. for (i = 0; i < times && ret == 0; i++) {
  7420. ret = finalFp(&sha512, hash_test[i]);
  7421. }
  7422. /* Test bad args. */
  7423. if (!ret) {
  7424. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  7425. ret = WOLFSSL_FATAL_ERROR;
  7426. }
  7427. }
  7428. if (!ret) {
  7429. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  7430. ret = WOLFSSL_FATAL_ERROR;
  7431. }
  7432. }
  7433. if (!ret) {
  7434. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  7435. ret = WOLFSSL_FATAL_ERROR;
  7436. }
  7437. }
  7438. freeFp(&sha512);
  7439. return ret;
  7440. }
  7441. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  7442. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  7443. #endif /* WOLFSSL_SHA512 */
  7444. /*
  7445. * Unit test function for wc_Sha512Final()
  7446. */
  7447. static int test_wc_Sha512Final (void)
  7448. {
  7449. int flag = 0;
  7450. #ifdef WOLFSSL_SHA512
  7451. wc_Sha512 sha512;
  7452. byte* hash_test[3];
  7453. byte hash1[WC_SHA512_DIGEST_SIZE];
  7454. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  7455. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  7456. int times, i, ret;
  7457. /* Initialize */
  7458. ret = wc_InitSha512(&sha512);
  7459. if (ret != 0) {
  7460. flag = ret;
  7461. }
  7462. if (!flag) {
  7463. hash_test[0] = hash1;
  7464. hash_test[1] = hash2;
  7465. hash_test[2] = hash3;
  7466. }
  7467. times = sizeof(hash_test) / sizeof(byte *);
  7468. /* Good test args. */
  7469. printf(testingFmt, "wc_Sha512Final()");
  7470. for (i = 0; i < times; i++) {
  7471. if (!flag) {
  7472. ret = wc_Sha512Final(&sha512, hash_test[i]);
  7473. if (ret != 0) {
  7474. flag = WOLFSSL_FATAL_ERROR;
  7475. }
  7476. }
  7477. }
  7478. /* Test bad args. */
  7479. if (!flag) {
  7480. ret = wc_Sha512Final(NULL, NULL);
  7481. if (ret != BAD_FUNC_ARG) {
  7482. flag = WOLFSSL_FATAL_ERROR;
  7483. }
  7484. if (!flag) {}
  7485. ret = wc_Sha512Final(NULL, hash1);
  7486. if (ret != BAD_FUNC_ARG) {
  7487. flag = WOLFSSL_FATAL_ERROR;
  7488. }
  7489. }
  7490. if (!flag) {
  7491. ret = wc_Sha512Final(&sha512, NULL);
  7492. if (ret != BAD_FUNC_ARG) {
  7493. flag = WOLFSSL_FATAL_ERROR;
  7494. }
  7495. }
  7496. wc_Sha512Free(&sha512);
  7497. printf(resultFmt, flag == 0 ? passed : failed);
  7498. #endif
  7499. return flag;
  7500. } /* END test_wc_Sha512Final */
  7501. /*
  7502. * Unit test function for wc_Sha512GetFlags()
  7503. */
  7504. static int test_wc_Sha512GetFlags (void)
  7505. {
  7506. int flag = 0;
  7507. #if defined(WOLFSSL_SHA512) && \
  7508. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  7509. wc_Sha512 sha512;
  7510. word32 flags = 0;
  7511. printf(testingFmt, "wc_Sha512GetFlags()");
  7512. /* Initialize */
  7513. flag = wc_InitSha512(&sha512);
  7514. if (flag == 0) {
  7515. flag = wc_Sha512GetFlags(&sha512, &flags);
  7516. }
  7517. if (flag == 0) {
  7518. if (flags & WC_HASH_FLAG_ISCOPY) {
  7519. flag = 0;
  7520. }
  7521. }
  7522. wc_Sha512Free(&sha512);
  7523. printf(resultFmt, flag == 0 ? passed : failed);
  7524. #endif
  7525. return flag;
  7526. } /* END test_wc_Sha512GetFlags */
  7527. /*
  7528. * Unit test function for wc_Sha512FinalRaw()
  7529. */
  7530. static int test_wc_Sha512FinalRaw (void)
  7531. {
  7532. int flag = 0;
  7533. #if defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  7534. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))
  7535. wc_Sha512 sha512;
  7536. byte* hash_test[3];
  7537. byte hash1[WC_SHA512_DIGEST_SIZE];
  7538. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  7539. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  7540. int times, i, ret;
  7541. /* Initialize */
  7542. ret = wc_InitSha512(&sha512);
  7543. if (ret != 0) {
  7544. flag = ret;
  7545. }
  7546. if (!flag) {
  7547. hash_test[0] = hash1;
  7548. hash_test[1] = hash2;
  7549. hash_test[2] = hash3;
  7550. }
  7551. times = sizeof(hash_test) / sizeof(byte*);
  7552. /* Good test args. */
  7553. printf(testingFmt, "wc_Sha512FinalRaw()");
  7554. for (i = 0; i < times; i++) {
  7555. if (!flag) {
  7556. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  7557. if (ret != 0) {
  7558. flag = WOLFSSL_FATAL_ERROR;
  7559. }
  7560. }
  7561. }
  7562. /* Test bad args. */
  7563. if (!flag ) {
  7564. ret = wc_Sha512FinalRaw(NULL, NULL);
  7565. if (ret != BAD_FUNC_ARG) {
  7566. flag = WOLFSSL_FATAL_ERROR;
  7567. }
  7568. }
  7569. if (!flag) {
  7570. ret = wc_Sha512FinalRaw(NULL, hash1);
  7571. if (ret != BAD_FUNC_ARG) {
  7572. flag = WOLFSSL_FATAL_ERROR;
  7573. }
  7574. }
  7575. if (!flag) {
  7576. ret = wc_Sha512FinalRaw(&sha512, NULL);
  7577. if (ret != BAD_FUNC_ARG) {
  7578. flag = WOLFSSL_FATAL_ERROR;
  7579. }
  7580. }
  7581. wc_Sha512Free(&sha512);
  7582. printf(resultFmt, flag == 0 ? passed : failed);
  7583. #endif
  7584. return flag;
  7585. } /* END test_wc_Sha512FinalRaw */
  7586. /*
  7587. * Unit test function for wc_Sha512Free()
  7588. */
  7589. static int test_wc_Sha512Free (void)
  7590. {
  7591. int flag = 0;
  7592. #ifdef WOLFSSL_SHA512
  7593. printf(testingFmt, "wc_Sha512Free()");
  7594. wc_Sha512Free(NULL);
  7595. printf(resultFmt, flag == 0 ? passed : failed);
  7596. #endif
  7597. return flag;
  7598. } /* END test_wc_Sha512Free */
  7599. #ifdef WOLFSSL_SHA512
  7600. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  7601. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  7602. static int test_Sha512_Family_GetHash(int type )
  7603. {
  7604. int flag = 0;
  7605. int(*initFp)(wc_Sha512*);
  7606. int(*ghashFp)(wc_Sha512*, byte*);
  7607. wc_Sha512 sha512;
  7608. byte hash1[WC_SHA512_DIGEST_SIZE];
  7609. if (type == WC_HASH_TYPE_SHA512) {
  7610. initFp = wc_InitSha512;
  7611. ghashFp = wc_Sha512GetHash;
  7612. }
  7613. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7614. #if !defined(WOLFSSL_NOSHA512_224)
  7615. else if (type == WC_HASH_TYPE_SHA512_224) {
  7616. initFp = wc_InitSha512_224;
  7617. ghashFp = wc_Sha512_224GetHash;
  7618. }
  7619. #endif
  7620. #if !defined(WOLFSSL_NOSHA512_256)
  7621. else if (type == WC_HASH_TYPE_SHA512_256) {
  7622. initFp = wc_InitSha512_256;
  7623. ghashFp = wc_Sha512_256GetHash;
  7624. }
  7625. #endif
  7626. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7627. else {
  7628. initFp = NULL;
  7629. ghashFp = NULL;
  7630. }
  7631. if (initFp == NULL || ghashFp == NULL)
  7632. return WOLFSSL_FATAL_ERROR;
  7633. if (!flag) {
  7634. flag = initFp(&sha512);
  7635. }
  7636. if (!flag) {
  7637. flag = ghashFp(&sha512, hash1);
  7638. }
  7639. /*test bad arguements*/
  7640. if (!flag) {
  7641. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  7642. flag = WOLFSSL_FATAL_ERROR;
  7643. }
  7644. if (!flag) {
  7645. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  7646. flag = WOLFSSL_FATAL_ERROR;
  7647. }
  7648. if (!flag) {
  7649. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  7650. flag = WOLFSSL_FATAL_ERROR;
  7651. }
  7652. wc_Sha512Free(&sha512);
  7653. return flag;
  7654. }
  7655. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  7656. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  7657. #endif /* WOLFSSL_SHA512 */
  7658. /*
  7659. * Unit test function for wc_Sha512GetHash()
  7660. */
  7661. static int test_wc_Sha512GetHash (void)
  7662. {
  7663. int flag = 0;
  7664. #ifdef WOLFSSL_SHA512
  7665. wc_Sha512 sha512;
  7666. byte hash1[WC_SHA512_DIGEST_SIZE];
  7667. printf(testingFmt, "wc_Sha512GetHash()");
  7668. /* Initialize */
  7669. flag = wc_InitSha512(&sha512);
  7670. if (flag == 0) {
  7671. flag = wc_Sha512GetHash(&sha512, hash1);
  7672. }
  7673. /*test bad arguements*/
  7674. if (flag == 0) {
  7675. flag = wc_Sha512GetHash(NULL, NULL);
  7676. if (flag == BAD_FUNC_ARG) {
  7677. flag = 0;
  7678. }
  7679. }
  7680. if (flag == 0) {
  7681. flag = wc_Sha512GetHash(NULL, hash1);
  7682. if (flag == BAD_FUNC_ARG) {
  7683. flag = 0;
  7684. }
  7685. }
  7686. if (flag == 0) {
  7687. flag = wc_Sha512GetHash(&sha512, NULL);
  7688. if (flag == BAD_FUNC_ARG) {
  7689. flag = 0;
  7690. }
  7691. }
  7692. wc_Sha512Free(&sha512);
  7693. printf(resultFmt, flag == 0 ? passed : failed);
  7694. #endif
  7695. return flag;
  7696. } /* END test_wc_Sha512GetHash */
  7697. /*
  7698. * Unit test function for wc_Sha512Copy()
  7699. */
  7700. static int test_wc_Sha512Copy (void)
  7701. {
  7702. int flag = 0;
  7703. #ifdef WOLFSSL_SHA512
  7704. wc_Sha512 sha512;
  7705. wc_Sha512 temp;
  7706. printf(testingFmt, "wc_Sha512Copy()");
  7707. /* Initialize */
  7708. flag = wc_InitSha512(&sha512);
  7709. if (flag == 0) {
  7710. flag = wc_InitSha512(&temp);
  7711. }
  7712. if (flag == 0) {
  7713. flag = wc_Sha512Copy(&sha512, &temp);
  7714. }
  7715. /*test bad arguements*/
  7716. if (flag == 0) {
  7717. flag = wc_Sha512Copy(NULL, NULL);
  7718. if (flag == BAD_FUNC_ARG) {
  7719. flag = 0;
  7720. }
  7721. }
  7722. if (flag == 0) {
  7723. flag = wc_Sha512Copy(NULL, &temp);
  7724. if (flag == BAD_FUNC_ARG) {
  7725. flag = 0;
  7726. }
  7727. }
  7728. if (flag == 0) {
  7729. flag = wc_Sha512Copy(&sha512, NULL);
  7730. if (flag == BAD_FUNC_ARG) {
  7731. flag = 0;
  7732. }
  7733. }
  7734. wc_Sha512Free(&sha512);
  7735. wc_Sha512Free(&temp);
  7736. printf(resultFmt, flag == 0 ? passed : failed);
  7737. #endif
  7738. return flag;
  7739. } /* END test_wc_Sha512Copy */
  7740. static int test_wc_InitSha512_224 (void)
  7741. {
  7742. int flag = 0;
  7743. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7744. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  7745. wc_Sha512 sha512;
  7746. int ret;
  7747. printf(testingFmt, "wc_InitSha512_224()");
  7748. /* Test good arg. */
  7749. ret = wc_InitSha512_224(&sha512);
  7750. if (ret != 0) {
  7751. flag = WOLFSSL_FATAL_ERROR;
  7752. }
  7753. /* Test bad arg. */
  7754. if (!flag) {
  7755. ret = wc_InitSha512_224(NULL);
  7756. if (ret != BAD_FUNC_ARG) {
  7757. flag = WOLFSSL_FATAL_ERROR;
  7758. }
  7759. }
  7760. wc_Sha512_224Free(&sha512);
  7761. printf(resultFmt, flag == 0 ? passed : failed);
  7762. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  7763. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7764. return flag;
  7765. }
  7766. static int test_wc_Sha512_224Update (void)
  7767. {
  7768. int flag = 0;
  7769. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7770. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  7771. wc_Sha512 sha512;
  7772. byte hash[WC_SHA512_DIGEST_SIZE];
  7773. testVector a, c;
  7774. int ret;
  7775. ret = wc_InitSha512_224(&sha512);
  7776. if (ret != 0) {
  7777. flag = ret;
  7778. }
  7779. printf(testingFmt, "wc_Sha512_224Update()");
  7780. /* Input. */
  7781. if (!flag) {
  7782. a.input = "a";
  7783. a.inLen = XSTRLEN(a.input);
  7784. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  7785. if (ret != 0) {
  7786. flag = ret;
  7787. }
  7788. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  7789. if (ret != 0) {
  7790. flag = ret;
  7791. }
  7792. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  7793. if (ret != 0) {
  7794. flag = ret;
  7795. }
  7796. ret = wc_Sha512_224Final(&sha512, hash);
  7797. if (ret != 0) {
  7798. flag = ret;
  7799. }
  7800. }
  7801. /* Update input. */
  7802. if (!flag) {
  7803. a.input = "abc";
  7804. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  7805. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  7806. a.inLen = XSTRLEN(a.input);
  7807. a.outLen = XSTRLEN(a.output);
  7808. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  7809. if (ret != 0) {
  7810. flag = ret;
  7811. }
  7812. }
  7813. if (!flag) {
  7814. ret = wc_Sha512_224Final(&sha512, hash);
  7815. if (ret != 0) {
  7816. flag = ret;
  7817. }
  7818. }
  7819. if (!flag) {
  7820. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  7821. flag = WOLFSSL_FATAL_ERROR;
  7822. }
  7823. }
  7824. if (!flag) {
  7825. c.input = NULL;
  7826. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  7827. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  7828. if (ret != BAD_FUNC_ARG) {
  7829. flag = WOLFSSL_FATAL_ERROR;
  7830. }
  7831. }
  7832. if (!flag) {
  7833. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  7834. if (ret != BAD_FUNC_ARG) {
  7835. flag = WOLFSSL_FATAL_ERROR;
  7836. }
  7837. }
  7838. wc_Sha512_224Free(&sha512);
  7839. /* If not returned then the unit test passed test vectors. */
  7840. printf(resultFmt, flag == 0 ? passed : failed);
  7841. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  7842. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7843. return flag;
  7844. }
  7845. static int test_wc_Sha512_224Final (void)
  7846. {
  7847. int flag = 0;
  7848. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7849. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  7850. printf(testingFmt, "wc_Sha512_224Final()");
  7851. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  7852. printf(resultFmt, flag == 0 ? passed : failed);
  7853. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  7854. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7855. return flag;
  7856. }
  7857. static int test_wc_Sha512_224GetFlags (void)
  7858. {
  7859. int flag = 0;
  7860. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7861. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  7862. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  7863. wc_Sha512 sha512, copy;
  7864. word32 flags = 0;
  7865. printf(testingFmt, "wc_Sha512_224GetFlags()");
  7866. /* Initialize */
  7867. flag = wc_InitSha512_224(&sha512);
  7868. if (!flag) {
  7869. flag = wc_InitSha512_224(&copy);
  7870. }
  7871. if (!flag) {
  7872. flag = wc_Sha512_224Copy(&sha512, &copy);
  7873. }
  7874. if (!flag) {
  7875. flag = wc_Sha512_224GetFlags(&copy, &flags);
  7876. }
  7877. if (!flag) {
  7878. if (flags & WC_HASH_FLAG_ISCOPY)
  7879. flag = 0;
  7880. else
  7881. flag = WOLFSSL_FATAL_ERROR;
  7882. }
  7883. wc_Sha512_224Free(&copy);
  7884. wc_Sha512_224Free(&sha512);
  7885. printf(resultFmt, flag == 0 ? passed : failed);
  7886. #endif
  7887. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7888. return flag;
  7889. }
  7890. static int test_wc_Sha512_224FinalRaw (void)
  7891. {
  7892. int flag = 0;
  7893. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  7894. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  7895. printf(testingFmt, "wc_Sha512_224FinalRaw()");
  7896. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  7897. printf(resultFmt, flag == 0 ? passed : failed);
  7898. #endif
  7899. return flag;
  7900. }
  7901. static int test_wc_Sha512_224Free (void)
  7902. {
  7903. int flag = 0;
  7904. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7905. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  7906. printf(testingFmt, "wc_Sha512_224Free()");
  7907. wc_Sha512_224Free(NULL);
  7908. printf(resultFmt, passed);
  7909. #endif
  7910. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7911. return flag;
  7912. }
  7913. static int test_wc_Sha512_224GetHash (void)
  7914. {
  7915. int flag = 0;
  7916. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7917. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  7918. printf(testingFmt, "wc_Sha512_224GetHash()");
  7919. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  7920. printf(resultFmt, flag == 0 ? passed : failed);
  7921. #endif
  7922. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7923. return flag;
  7924. }
  7925. static int test_wc_Sha512_224Copy (void)
  7926. {
  7927. int flag = 0;
  7928. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7929. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  7930. wc_Sha512 sha512;
  7931. wc_Sha512 temp;
  7932. printf(testingFmt, "wc_Sha512_224Copy()");
  7933. /* Initialize */
  7934. flag = wc_InitSha512_224(&sha512);
  7935. if (flag == 0) {
  7936. flag = wc_InitSha512_224(&temp);
  7937. }
  7938. if (flag == 0) {
  7939. flag = wc_Sha512_224Copy(&sha512, &temp);
  7940. }
  7941. /*test bad arguements*/
  7942. if (flag == 0) {
  7943. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  7944. flag = WOLFSSL_FATAL_ERROR;
  7945. }
  7946. if (flag == 0) {
  7947. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  7948. flag = WOLFSSL_FATAL_ERROR;
  7949. }
  7950. if (flag == 0) {
  7951. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  7952. flag = WOLFSSL_FATAL_ERROR;
  7953. }
  7954. wc_Sha512_224Free(&sha512);
  7955. wc_Sha512_224Free(&temp);
  7956. printf(resultFmt, flag == 0 ? passed : failed);
  7957. #endif
  7958. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7959. return flag;
  7960. }
  7961. static int test_wc_InitSha512_256 (void)
  7962. {
  7963. int flag = 0;
  7964. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7965. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  7966. wc_Sha512 sha512;
  7967. int ret;
  7968. printf(testingFmt, "wc_InitSha512_256()");
  7969. /* Test good arg. */
  7970. ret = wc_InitSha512_256(&sha512);
  7971. if (ret != 0) {
  7972. flag = WOLFSSL_FATAL_ERROR;
  7973. }
  7974. /* Test bad arg. */
  7975. if (!flag) {
  7976. ret = wc_InitSha512_256(NULL);
  7977. if (ret != BAD_FUNC_ARG) {
  7978. flag = WOLFSSL_FATAL_ERROR;
  7979. }
  7980. }
  7981. wc_Sha512_256Free(&sha512);
  7982. printf(resultFmt, flag == 0 ? passed : failed);
  7983. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  7984. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  7985. return flag;
  7986. }
  7987. static int test_wc_Sha512_256Update (void)
  7988. {
  7989. int flag = 0;
  7990. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7991. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  7992. wc_Sha512 sha512;
  7993. byte hash[WC_SHA512_DIGEST_SIZE];
  7994. testVector a, c;
  7995. int ret;
  7996. ret = wc_InitSha512_256(&sha512);
  7997. if (ret != 0) {
  7998. flag = ret;
  7999. }
  8000. printf(testingFmt, "wc_Sha512_256Update()");
  8001. /* Input. */
  8002. if (!flag) {
  8003. a.input = "a";
  8004. a.inLen = XSTRLEN(a.input);
  8005. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  8006. if (ret != 0) {
  8007. flag = ret;
  8008. }
  8009. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  8010. if (ret != 0) {
  8011. flag = ret;
  8012. }
  8013. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  8014. if (ret != 0) {
  8015. flag = ret;
  8016. }
  8017. ret = wc_Sha512_256Final(&sha512, hash);
  8018. if (ret != 0) {
  8019. flag = ret;
  8020. }
  8021. }
  8022. /* Update input. */
  8023. if (!flag) {
  8024. a.input = "abc";
  8025. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  8026. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  8027. "\x07\xe7\xaf\x23";
  8028. a.inLen = XSTRLEN(a.input);
  8029. a.outLen = XSTRLEN(a.output);
  8030. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  8031. if (ret != 0) {
  8032. flag = ret;
  8033. }
  8034. }
  8035. if (!flag) {
  8036. ret = wc_Sha512_256Final(&sha512, hash);
  8037. if (ret != 0) {
  8038. flag = ret;
  8039. }
  8040. }
  8041. if (!flag) {
  8042. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  8043. flag = WOLFSSL_FATAL_ERROR;
  8044. }
  8045. }
  8046. if (!flag) {
  8047. c.input = NULL;
  8048. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  8049. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  8050. if (ret != BAD_FUNC_ARG) {
  8051. flag = WOLFSSL_FATAL_ERROR;
  8052. }
  8053. }
  8054. if (!flag) {
  8055. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  8056. if (ret != BAD_FUNC_ARG) {
  8057. flag = WOLFSSL_FATAL_ERROR;
  8058. }
  8059. }
  8060. wc_Sha512_256Free(&sha512);
  8061. /* If not returned then the unit test passed test vectors. */
  8062. printf(resultFmt, flag == 0 ? passed : failed);
  8063. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  8064. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  8065. return flag;
  8066. }
  8067. static int test_wc_Sha512_256Final (void)
  8068. {
  8069. int flag = 0;
  8070. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8071. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  8072. printf(testingFmt, "wc_Sha512_256Final()");
  8073. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  8074. printf(resultFmt, flag == 0 ? passed : failed);
  8075. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  8076. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  8077. return flag;
  8078. }
  8079. static int test_wc_Sha512_256GetFlags (void)
  8080. {
  8081. int flag = 0;
  8082. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8083. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  8084. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  8085. wc_Sha512 sha512, copy;
  8086. word32 flags = 0;
  8087. printf(testingFmt, "wc_Sha512_256GetFlags()");
  8088. /* Initialize */
  8089. flag = wc_InitSha512_256(&sha512);
  8090. if (!flag ) {
  8091. flag = wc_InitSha512_256(&copy);
  8092. }
  8093. if (!flag ) {
  8094. flag = wc_Sha512_256Copy(&sha512, &copy);
  8095. }
  8096. if (!flag ) {
  8097. flag = wc_Sha512_256GetFlags(&copy, &flags);
  8098. }
  8099. if (!flag) {
  8100. if (flags & WC_HASH_FLAG_ISCOPY)
  8101. flag = 0;
  8102. else
  8103. flag = WOLFSSL_FATAL_ERROR;
  8104. }
  8105. wc_Sha512_256Free(&sha512);
  8106. printf(resultFmt, flag == 0 ? passed : failed);
  8107. #endif
  8108. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  8109. return flag;
  8110. }
  8111. static int test_wc_Sha512_256FinalRaw (void)
  8112. {
  8113. int flag = 0;
  8114. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  8115. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  8116. printf(testingFmt, "wc_Sha512_256FinalRaw()");
  8117. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  8118. printf(resultFmt, flag == 0 ? passed : failed);
  8119. #endif
  8120. return flag;
  8121. }
  8122. static int test_wc_Sha512_256Free (void)
  8123. {
  8124. int flag = 0;
  8125. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8126. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  8127. printf(testingFmt, "wc_Sha512_256Free()");
  8128. wc_Sha512_256Free(NULL);
  8129. printf(resultFmt, passed);
  8130. #endif
  8131. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  8132. return flag;
  8133. }
  8134. static int test_wc_Sha512_256GetHash (void)
  8135. {
  8136. int flag = 0;
  8137. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8138. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  8139. printf(testingFmt, "wc_Sha512_256GetHash()");
  8140. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  8141. printf(resultFmt, flag == 0 ? passed : failed);
  8142. #endif
  8143. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  8144. return flag;
  8145. }
  8146. static int test_wc_Sha512_256Copy (void)
  8147. {
  8148. int flag = 0;
  8149. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8150. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  8151. wc_Sha512 sha512;
  8152. wc_Sha512 temp;
  8153. printf(testingFmt, "wc_Sha512_256Copy()");
  8154. /* Initialize */
  8155. flag = wc_InitSha512_256(&sha512);
  8156. if (flag == 0) {
  8157. flag = wc_InitSha512_256(&temp);
  8158. }
  8159. if (flag == 0) {
  8160. flag = wc_Sha512_256Copy(&sha512, &temp);
  8161. }
  8162. /*test bad arguements*/
  8163. if (flag == 0) {
  8164. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  8165. flag = WOLFSSL_FATAL_ERROR;
  8166. }
  8167. if (flag == 0) {
  8168. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  8169. flag = WOLFSSL_FATAL_ERROR;
  8170. }
  8171. if (flag == 0) {
  8172. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  8173. flag = WOLFSSL_FATAL_ERROR;
  8174. }
  8175. wc_Sha512_256Free(&sha512);
  8176. wc_Sha512_256Free(&temp);
  8177. printf(resultFmt, flag == 0 ? passed : failed);
  8178. #endif
  8179. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  8180. return flag;
  8181. }
  8182. /*
  8183. * Testing wc_InitSha384()
  8184. */
  8185. static int test_wc_InitSha384 (void)
  8186. {
  8187. int flag = 0;
  8188. #ifdef WOLFSSL_SHA384
  8189. wc_Sha384 sha384;
  8190. int ret;
  8191. printf(testingFmt, "wc_InitSha384()");
  8192. /* Test good arg. */
  8193. ret = wc_InitSha384(&sha384);
  8194. if (ret != 0) {
  8195. flag = WOLFSSL_FATAL_ERROR;
  8196. }
  8197. /* Test bad arg. */
  8198. if (!flag) {
  8199. ret = wc_InitSha384(NULL);
  8200. if (ret != BAD_FUNC_ARG) {
  8201. flag = WOLFSSL_FATAL_ERROR;
  8202. }
  8203. }
  8204. wc_Sha384Free(&sha384);
  8205. printf(resultFmt, flag == 0 ? passed : failed);
  8206. #endif
  8207. return flag;
  8208. } /* END test_wc_InitSha384 */
  8209. /*
  8210. * test wc_Sha384Update()
  8211. */
  8212. static int test_wc_Sha384Update (void)
  8213. {
  8214. int flag = 0;
  8215. #ifdef WOLFSSL_SHA384
  8216. wc_Sha384 sha384;
  8217. byte hash[WC_SHA384_DIGEST_SIZE];
  8218. testVector a, b, c;
  8219. int ret;
  8220. ret = wc_InitSha384(&sha384);
  8221. if (ret != 0) {
  8222. flag = ret;
  8223. }
  8224. printf(testingFmt, "wc_Sha384Update()");
  8225. /* Input */
  8226. if (!flag) {
  8227. a.input = "a";
  8228. a.inLen = XSTRLEN(a.input);
  8229. ret = wc_Sha384Update(&sha384, NULL, 0);
  8230. if (ret != 0) {
  8231. flag = ret;
  8232. }
  8233. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  8234. if (ret != 0) {
  8235. flag = ret;
  8236. }
  8237. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  8238. if (ret != 0) {
  8239. flag = ret;
  8240. }
  8241. }
  8242. if (!flag) {
  8243. ret = wc_Sha384Final(&sha384, hash);
  8244. if (ret != 0) {
  8245. flag = ret;
  8246. }
  8247. }
  8248. /* Update input. */
  8249. if (!flag) {
  8250. a.input = "abc";
  8251. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  8252. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  8253. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  8254. "\xc8\x25\xa7";
  8255. a.inLen = XSTRLEN(a.input);
  8256. a.outLen = XSTRLEN(a.output);
  8257. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  8258. if (ret != 0) {
  8259. flag = ret;
  8260. }
  8261. }
  8262. if (!flag) {
  8263. ret = wc_Sha384Final(&sha384, hash);
  8264. if (ret != 0) {
  8265. flag = ret;
  8266. }
  8267. }
  8268. if (!flag) {
  8269. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  8270. flag = WOLFSSL_FATAL_ERROR;
  8271. }
  8272. }
  8273. /* Pass in bad values. */
  8274. if (!flag) {
  8275. b.input = NULL;
  8276. b.inLen = 0;
  8277. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  8278. if (ret != 0) {
  8279. flag = ret;
  8280. }
  8281. }
  8282. if (!flag) {
  8283. c.input = NULL;
  8284. c.inLen = WC_SHA384_DIGEST_SIZE;
  8285. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  8286. if (ret != BAD_FUNC_ARG) {
  8287. flag = WOLFSSL_FATAL_ERROR;
  8288. }
  8289. }
  8290. if (!flag) {
  8291. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  8292. if (ret != BAD_FUNC_ARG) {
  8293. flag = WOLFSSL_FATAL_ERROR;
  8294. }
  8295. }
  8296. wc_Sha384Free(&sha384);
  8297. /* If not returned then the unit test passed test vectors. */
  8298. printf(resultFmt, flag == 0 ? passed : failed);
  8299. #endif
  8300. return flag;
  8301. } /* END test_wc_Sha384Update */
  8302. /*
  8303. * Unit test function for wc_Sha384Final();
  8304. */
  8305. static int test_wc_Sha384Final (void)
  8306. {
  8307. int flag = 0;
  8308. #ifdef WOLFSSL_SHA384
  8309. wc_Sha384 sha384;
  8310. byte* hash_test[3];
  8311. byte hash1[WC_SHA384_DIGEST_SIZE];
  8312. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  8313. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  8314. int times, i, ret;
  8315. /* Initialize */
  8316. ret = wc_InitSha384(&sha384);
  8317. if (ret) {
  8318. flag = ret;
  8319. }
  8320. if (!flag) {
  8321. hash_test[0] = hash1;
  8322. hash_test[1] = hash2;
  8323. hash_test[2] = hash3;
  8324. }
  8325. times = sizeof(hash_test) / sizeof(byte*);
  8326. /* Good test args. */
  8327. printf(testingFmt, "wc_Sha384Final()");
  8328. for (i = 0; i < times; i++) {
  8329. if (!flag) {
  8330. ret = wc_Sha384Final(&sha384, hash_test[i]);
  8331. if (ret != 0) {
  8332. flag = WOLFSSL_FATAL_ERROR;
  8333. }
  8334. }
  8335. }
  8336. /* Test bad args. */
  8337. if (!flag) {
  8338. ret = wc_Sha384Final(NULL, NULL);
  8339. if (ret != BAD_FUNC_ARG) {
  8340. flag = WOLFSSL_FATAL_ERROR;
  8341. }
  8342. }
  8343. if (!flag) {
  8344. ret = wc_Sha384Final(NULL, hash1);
  8345. if (ret != BAD_FUNC_ARG) {
  8346. flag = WOLFSSL_FATAL_ERROR;
  8347. }
  8348. }
  8349. if (!flag) {
  8350. ret = wc_Sha384Final(&sha384, NULL);
  8351. if (ret != BAD_FUNC_ARG) {
  8352. flag = WOLFSSL_FATAL_ERROR;
  8353. }
  8354. }
  8355. wc_Sha384Free(&sha384);
  8356. printf(resultFmt, flag == 0 ? passed : failed);
  8357. #endif
  8358. return flag;
  8359. } /* END test_wc_Sha384Final */
  8360. /*
  8361. * Unit test function for wc_Sha384GetFlags()
  8362. */
  8363. static int test_wc_Sha384GetFlags (void)
  8364. {
  8365. int flag = 0;
  8366. #if defined(WOLFSSL_SHA384) && \
  8367. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  8368. wc_Sha384 sha384;
  8369. word32 flags = 0;
  8370. printf(testingFmt, "wc_Sha384GetFlags()");
  8371. /* Initialize */
  8372. flag = wc_InitSha384(&sha384);
  8373. if (flag == 0) {
  8374. flag = wc_Sha384GetFlags(&sha384, &flags);
  8375. }
  8376. if (flag == 0) {
  8377. if (flags & WC_HASH_FLAG_ISCOPY) {
  8378. flag = 0;
  8379. }
  8380. }
  8381. wc_Sha384Free(&sha384);
  8382. printf(resultFmt, flag == 0 ? passed : failed);
  8383. #endif
  8384. return flag;
  8385. } /* END test_wc_Sha384GetFlags */
  8386. /*
  8387. * Unit test function for wc_Sha384FinalRaw()
  8388. */
  8389. static int test_wc_Sha384FinalRaw (void)
  8390. {
  8391. int flag = 0;
  8392. #if defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  8393. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))
  8394. wc_Sha384 sha384;
  8395. byte* hash_test[3];
  8396. byte hash1[WC_SHA384_DIGEST_SIZE];
  8397. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  8398. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  8399. int times, i, ret;
  8400. /* Initialize */
  8401. ret = wc_InitSha384(&sha384);
  8402. if (ret != 0) {
  8403. flag = ret;
  8404. }
  8405. if (!flag) {
  8406. hash_test[0] = hash1;
  8407. hash_test[1] = hash2;
  8408. hash_test[2] = hash3;
  8409. }
  8410. times = sizeof(hash_test) / sizeof(byte*);
  8411. /* Good test args. */
  8412. printf(testingFmt, "wc_Sha384FinalRaw()");
  8413. for (i = 0; i < times; i++) {
  8414. if (!flag) {
  8415. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  8416. if (ret != 0) {
  8417. flag = WOLFSSL_FATAL_ERROR;
  8418. }
  8419. }
  8420. }
  8421. /* Test bad args. */
  8422. if (!flag ) {
  8423. ret = wc_Sha384FinalRaw(NULL, NULL);
  8424. if (ret != BAD_FUNC_ARG) {
  8425. flag = WOLFSSL_FATAL_ERROR;
  8426. }
  8427. }
  8428. if (!flag) {
  8429. ret = wc_Sha384FinalRaw(NULL, hash1);
  8430. if (ret != BAD_FUNC_ARG) {
  8431. flag = WOLFSSL_FATAL_ERROR;
  8432. }
  8433. }
  8434. if (!flag) {
  8435. ret = wc_Sha384FinalRaw(&sha384, NULL);
  8436. if (ret != BAD_FUNC_ARG) {
  8437. flag = WOLFSSL_FATAL_ERROR;
  8438. }
  8439. }
  8440. wc_Sha384Free(&sha384);
  8441. printf(resultFmt, flag == 0 ? passed : failed);
  8442. #endif
  8443. return flag;
  8444. } /* END test_wc_Sha384FinalRaw */
  8445. /*
  8446. * Unit test function for wc_Sha384Free()
  8447. */
  8448. static int test_wc_Sha384Free (void)
  8449. {
  8450. int flag = 0;
  8451. #ifdef WOLFSSL_SHA384
  8452. printf(testingFmt, "wc_Sha384Free()");
  8453. wc_Sha384Free(NULL);
  8454. printf(resultFmt, flag == 0 ? passed : failed);
  8455. #endif
  8456. return flag;
  8457. } /* END test_wc_Sha384Free */
  8458. /*
  8459. * Unit test function for wc_Sha384GetHash()
  8460. */
  8461. static int test_wc_Sha384GetHash (void)
  8462. {
  8463. int flag = 0;
  8464. #ifdef WOLFSSL_SHA384
  8465. wc_Sha384 sha384;
  8466. byte hash1[WC_SHA384_DIGEST_SIZE];
  8467. printf(testingFmt, "wc_Sha384GetHash()");
  8468. /* Initialize */
  8469. flag = wc_InitSha384(&sha384);
  8470. if (flag == 0) {
  8471. flag = wc_Sha384GetHash(&sha384, hash1);
  8472. }
  8473. /*test bad arguements*/
  8474. if (flag == 0) {
  8475. flag = wc_Sha384GetHash(NULL, NULL);
  8476. if (flag == BAD_FUNC_ARG) {
  8477. flag = 0;
  8478. }
  8479. }
  8480. if (flag == 0) {
  8481. flag = wc_Sha384GetHash(NULL, hash1);
  8482. if (flag == BAD_FUNC_ARG) {
  8483. flag = 0;
  8484. }
  8485. }
  8486. if (flag == 0) {
  8487. flag = wc_Sha384GetHash(&sha384, NULL);
  8488. if (flag == BAD_FUNC_ARG) {
  8489. flag = 0;
  8490. }
  8491. }
  8492. wc_Sha384Free(&sha384);
  8493. printf(resultFmt, flag == 0 ? passed : failed);
  8494. #endif
  8495. return flag;
  8496. } /* END test_wc_Sha384GetHash */
  8497. /*
  8498. * Unit test function for wc_Sha384Copy()
  8499. */
  8500. static int test_wc_Sha384Copy (void)
  8501. {
  8502. int flag = 0;
  8503. #ifdef WOLFSSL_SHA384
  8504. wc_Sha384 sha384;
  8505. wc_Sha384 temp;
  8506. printf(testingFmt, "wc_Sha384Copy()");
  8507. /* Initialize */
  8508. flag = wc_InitSha384(&sha384);
  8509. if (flag == 0) {
  8510. flag = wc_InitSha384(&temp);
  8511. }
  8512. if (flag == 0) {
  8513. flag = wc_Sha384Copy(&sha384, &temp);
  8514. }
  8515. /*test bad arguements*/
  8516. if (flag == 0) {
  8517. flag = wc_Sha384Copy(NULL, NULL);
  8518. if (flag == BAD_FUNC_ARG) {
  8519. flag = 0;
  8520. }
  8521. }
  8522. if (flag == 0) {
  8523. flag = wc_Sha384Copy(NULL, &temp);
  8524. if (flag == BAD_FUNC_ARG) {
  8525. flag = 0;
  8526. }
  8527. }
  8528. if (flag == 0) {
  8529. flag = wc_Sha384Copy(&sha384, NULL);
  8530. if (flag == BAD_FUNC_ARG) {
  8531. flag = 0;
  8532. }
  8533. }
  8534. wc_Sha384Free(&sha384);
  8535. wc_Sha384Free(&temp);
  8536. printf(resultFmt, flag == 0 ? passed : failed);
  8537. #endif
  8538. return flag;
  8539. } /* END test_wc_Sha384Copy */
  8540. /*
  8541. * Testing wc_InitSha224();
  8542. */
  8543. static int test_wc_InitSha224 (void)
  8544. {
  8545. int flag = 0;
  8546. #ifdef WOLFSSL_SHA224
  8547. wc_Sha224 sha224;
  8548. int ret;
  8549. printf(testingFmt, "wc_InitSha224()");
  8550. /* Test good arg. */
  8551. ret = wc_InitSha224(&sha224);
  8552. if (ret != 0) {
  8553. flag = WOLFSSL_FATAL_ERROR;
  8554. }
  8555. /* Test bad arg. */
  8556. if (!flag) {
  8557. ret = wc_InitSha224(NULL);
  8558. if (ret != BAD_FUNC_ARG) {
  8559. flag = WOLFSSL_FATAL_ERROR;
  8560. }
  8561. }
  8562. wc_Sha224Free(&sha224);
  8563. printf(resultFmt, flag == 0 ? passed : failed);
  8564. #endif
  8565. return flag;
  8566. } /* END test_wc_InitSha224 */
  8567. /*
  8568. * Unit test on wc_Sha224Update
  8569. */
  8570. static int test_wc_Sha224Update (void)
  8571. {
  8572. int flag = 0;
  8573. #ifdef WOLFSSL_SHA224
  8574. wc_Sha224 sha224;
  8575. byte hash[WC_SHA224_DIGEST_SIZE];
  8576. testVector a, b, c;
  8577. int ret;
  8578. ret = wc_InitSha224(&sha224);
  8579. if (ret != 0) {
  8580. flag = ret;
  8581. }
  8582. printf(testingFmt, "wc_Sha224Update()");
  8583. /* Input. */
  8584. if (!flag) {
  8585. a.input = "a";
  8586. a.inLen = XSTRLEN(a.input);
  8587. ret = wc_Sha224Update(&sha224, NULL, 0);
  8588. if (ret != 0) {
  8589. flag = ret;
  8590. }
  8591. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  8592. if (ret != 0) {
  8593. flag = ret;
  8594. }
  8595. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  8596. if (ret != 0) {
  8597. flag = ret;
  8598. }
  8599. }
  8600. if (!flag) {
  8601. ret = wc_Sha224Final(&sha224, hash);
  8602. if (ret != 0) {
  8603. flag = ret;
  8604. }
  8605. }
  8606. /* Update input. */
  8607. if (!flag) {
  8608. a.input = "abc";
  8609. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  8610. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  8611. a.inLen = XSTRLEN(a.input);
  8612. a.outLen = XSTRLEN(a.output);
  8613. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  8614. if (ret != 0) {
  8615. flag = ret;
  8616. }
  8617. }
  8618. if (!flag) {
  8619. ret = wc_Sha224Final(&sha224, hash);
  8620. if (ret != 0) {
  8621. flag = ret;
  8622. }
  8623. }
  8624. if (!flag) {
  8625. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  8626. flag = WOLFSSL_FATAL_ERROR;
  8627. }
  8628. }
  8629. /* Pass in bad values. */
  8630. if (!flag) {
  8631. b.input = NULL;
  8632. b.inLen = 0;
  8633. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  8634. if (ret != 0) {
  8635. flag = ret;
  8636. }
  8637. }
  8638. if (!flag) {
  8639. c.input = NULL;
  8640. c.inLen = WC_SHA224_DIGEST_SIZE;
  8641. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  8642. if (ret != BAD_FUNC_ARG) {
  8643. flag = WOLFSSL_FATAL_ERROR;
  8644. }
  8645. }
  8646. if (!flag) {
  8647. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  8648. if (ret != BAD_FUNC_ARG) {
  8649. flag = WOLFSSL_FATAL_ERROR;
  8650. }
  8651. }
  8652. wc_Sha224Free(&sha224);
  8653. /* If not returned then the unit test passed test vectors. */
  8654. printf(resultFmt, flag == 0 ? passed : failed);
  8655. #endif
  8656. return flag;
  8657. } /* END test_wc_Sha224Update */
  8658. /*
  8659. * Unit test for wc_Sha224Final();
  8660. */
  8661. static int test_wc_Sha224Final (void)
  8662. {
  8663. int flag = 0;
  8664. #ifdef WOLFSSL_SHA224
  8665. wc_Sha224 sha224;
  8666. byte* hash_test[3];
  8667. byte hash1[WC_SHA224_DIGEST_SIZE];
  8668. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  8669. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  8670. int times, i, ret;
  8671. /* Initialize */
  8672. ret = wc_InitSha224(&sha224);
  8673. if (ret) {
  8674. flag = ret;
  8675. }
  8676. if (!flag) {
  8677. hash_test[0] = hash1;
  8678. hash_test[1] = hash2;
  8679. hash_test[2] = hash3;
  8680. }
  8681. times = sizeof(hash_test) / sizeof(byte*);
  8682. /* Good test args. */
  8683. printf(testingFmt, "wc_sha224Final()");
  8684. /* Testing oversized buffers. */
  8685. for (i = 0; i < times; i++) {
  8686. if (!flag) {
  8687. ret = wc_Sha224Final(&sha224, hash_test[i]);
  8688. if (ret != 0) {
  8689. flag = WOLFSSL_FATAL_ERROR;
  8690. }
  8691. }
  8692. }
  8693. /* Test bad args. */
  8694. if (!flag) {
  8695. ret = wc_Sha224Final(NULL, NULL);
  8696. if (ret != BAD_FUNC_ARG) {
  8697. flag = WOLFSSL_FATAL_ERROR;
  8698. }
  8699. }
  8700. if (!flag) {
  8701. ret = wc_Sha224Final(NULL, hash1);
  8702. if (ret != BAD_FUNC_ARG) {
  8703. flag = WOLFSSL_FATAL_ERROR;
  8704. }
  8705. }
  8706. if (!flag) {
  8707. ret = wc_Sha224Final(&sha224, NULL);
  8708. if (ret != BAD_FUNC_ARG) {
  8709. flag = WOLFSSL_FATAL_ERROR;
  8710. }
  8711. }
  8712. wc_Sha224Free(&sha224);
  8713. printf(resultFmt, flag == 0 ? passed : failed);
  8714. #endif
  8715. return flag;
  8716. } /* END test_wc_Sha224Final */
  8717. /*
  8718. * Unit test function for wc_Sha224SetFlags()
  8719. */
  8720. static int test_wc_Sha224SetFlags (void)
  8721. {
  8722. int flag = 0;
  8723. #if defined(WOLFSSL_SHA224) && \
  8724. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  8725. wc_Sha224 sha224;
  8726. word32 flags = 0;
  8727. printf(testingFmt, "wc_Sha224SetFlags()");
  8728. /* Initialize */
  8729. flag = wc_InitSha224(&sha224);
  8730. if (flag == 0) {
  8731. flag = wc_Sha224SetFlags(&sha224, flags);
  8732. }
  8733. if (flag == 0) {
  8734. if (flags & WC_HASH_FLAG_ISCOPY) {
  8735. flag = 0;
  8736. }
  8737. }
  8738. wc_Sha224Free(&sha224);
  8739. printf(resultFmt, flag == 0 ? passed : failed);
  8740. #endif
  8741. return flag;
  8742. } /* END test_wc_Sha224SetFlags */
  8743. /*
  8744. * Unit test function for wc_Sha224GetFlags()
  8745. */
  8746. static int test_wc_Sha224GetFlags (void)
  8747. {
  8748. int flag = 0;
  8749. #if defined(WOLFSSL_SHA224) && \
  8750. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  8751. wc_Sha224 sha224;
  8752. word32 flags = 0;
  8753. printf(testingFmt, "wc_Sha224GetFlags()");
  8754. /* Initialize */
  8755. flag = wc_InitSha224(&sha224);
  8756. if (flag == 0) {
  8757. flag = wc_Sha224GetFlags(&sha224, &flags);
  8758. }
  8759. if (flag == 0) {
  8760. if (flags & WC_HASH_FLAG_ISCOPY) {
  8761. flag = 0;
  8762. }
  8763. }
  8764. wc_Sha224Free(&sha224);
  8765. printf(resultFmt, flag == 0 ? passed : failed);
  8766. #endif
  8767. return flag;
  8768. } /* END test_wc_Sha224GetFlags */
  8769. /*
  8770. * Unit test function for wc_Sha224Free()
  8771. */
  8772. static int test_wc_Sha224Free (void)
  8773. {
  8774. int flag = 0;
  8775. #ifdef WOLFSSL_SHA224
  8776. printf(testingFmt, "wc_Sha224Free()");
  8777. wc_Sha224Free(NULL);
  8778. printf(resultFmt, flag == 0 ? passed : failed);
  8779. #endif
  8780. return flag;
  8781. } /* END test_wc_Sha224Free */
  8782. /*
  8783. * Unit test function for wc_Sha224GetHash()
  8784. */
  8785. static int test_wc_Sha224GetHash (void)
  8786. {
  8787. int flag = 0;
  8788. #ifdef WOLFSSL_SHA224
  8789. wc_Sha224 sha224;
  8790. byte hash1[WC_SHA224_DIGEST_SIZE];
  8791. printf(testingFmt, "wc_Sha224GetHash()");
  8792. /* Initialize */
  8793. flag = wc_InitSha224(&sha224);
  8794. if (flag == 0) {
  8795. flag = wc_Sha224GetHash(&sha224, hash1);
  8796. }
  8797. /*test bad arguements*/
  8798. if (flag == 0) {
  8799. flag = wc_Sha224GetHash(NULL, NULL);
  8800. if (flag == BAD_FUNC_ARG) {
  8801. flag = 0;
  8802. }
  8803. }
  8804. if (flag == 0) {
  8805. flag = wc_Sha224GetHash(NULL, hash1);
  8806. if (flag == BAD_FUNC_ARG) {
  8807. flag = 0;
  8808. }
  8809. }
  8810. if (flag == 0) {
  8811. flag = wc_Sha224GetHash(&sha224, NULL);
  8812. if (flag == BAD_FUNC_ARG) {
  8813. flag = 0;
  8814. }
  8815. }
  8816. wc_Sha224Free(&sha224);
  8817. printf(resultFmt, flag == 0 ? passed : failed);
  8818. #endif
  8819. return flag;
  8820. } /* END test_wc_Sha224GetHash */
  8821. /*
  8822. * Unit test function for wc_Sha224Copy()
  8823. */
  8824. static int test_wc_Sha224Copy (void)
  8825. {
  8826. int flag = 0;
  8827. #ifdef WOLFSSL_SHA224
  8828. wc_Sha224 sha224;
  8829. wc_Sha224 temp;
  8830. printf(testingFmt, "wc_Sha224Copy()");
  8831. /* Initialize */
  8832. flag = wc_InitSha224(&sha224);
  8833. if (flag == 0) {
  8834. flag = wc_InitSha224(&temp);
  8835. }
  8836. if (flag == 0) {
  8837. flag = wc_Sha224Copy(&sha224, &temp);
  8838. }
  8839. /*test bad arguements*/
  8840. if (flag == 0) {
  8841. flag = wc_Sha224Copy(NULL, NULL);
  8842. if (flag == BAD_FUNC_ARG) {
  8843. flag = 0;
  8844. }
  8845. }
  8846. if (flag == 0) {
  8847. flag = wc_Sha224Copy(NULL, &temp);
  8848. if (flag == BAD_FUNC_ARG) {
  8849. flag = 0;
  8850. }
  8851. }
  8852. if (flag == 0) {
  8853. flag = wc_Sha224Copy(&sha224, NULL);
  8854. if (flag == BAD_FUNC_ARG) {
  8855. flag = 0;
  8856. }
  8857. }
  8858. wc_Sha224Free(&sha224);
  8859. wc_Sha224Free(&temp);
  8860. printf(resultFmt, flag == 0 ? passed : failed);
  8861. #endif
  8862. return flag;
  8863. } /* END test_wc_Sha224Copy */
  8864. /*
  8865. * Testing wc_InitRipeMd()
  8866. */
  8867. static int test_wc_InitRipeMd (void)
  8868. {
  8869. int flag = 0;
  8870. #ifdef WOLFSSL_RIPEMD
  8871. RipeMd ripemd;
  8872. int ret;
  8873. printf(testingFmt, "wc_InitRipeMd()");
  8874. /* Test good arg. */
  8875. ret = wc_InitRipeMd(&ripemd);
  8876. if (ret != 0) {
  8877. flag = WOLFSSL_FATAL_ERROR;
  8878. }
  8879. /* Test bad arg. */
  8880. if (!flag) {
  8881. ret = wc_InitRipeMd(NULL);
  8882. if (ret != BAD_FUNC_ARG) {
  8883. flag = WOLFSSL_FATAL_ERROR;
  8884. }
  8885. }
  8886. printf(resultFmt, flag == 0 ? passed : failed);
  8887. #endif
  8888. return flag;
  8889. } /* END test_wc_InitRipeMd */
  8890. /*
  8891. * Testing wc_RipeMdUpdate()
  8892. */
  8893. static int test_wc_RipeMdUpdate (void)
  8894. {
  8895. int flag = 0;
  8896. #ifdef WOLFSSL_RIPEMD
  8897. RipeMd ripemd;
  8898. byte hash[RIPEMD_DIGEST_SIZE];
  8899. testVector a, b, c;
  8900. int ret;
  8901. ret = wc_InitRipeMd(&ripemd);
  8902. if (ret != 0) {
  8903. flag = ret;
  8904. }
  8905. printf(testingFmt, "wc_RipeMdUpdate()");
  8906. /* Input */
  8907. if (!flag) {
  8908. a.input = "a";
  8909. a.inLen = XSTRLEN(a.input);
  8910. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  8911. if (ret != 0) {
  8912. flag = ret;
  8913. }
  8914. }
  8915. if (!flag) {
  8916. ret = wc_RipeMdFinal(&ripemd, hash);
  8917. if (ret != 0) {
  8918. flag = ret;
  8919. }
  8920. }
  8921. /* Update input. */
  8922. if (!flag) {
  8923. a.input = "abc";
  8924. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  8925. "\xb0\x87\xf1\x5a\x0b\xfc";
  8926. a.inLen = XSTRLEN(a.input);
  8927. a.outLen = XSTRLEN(a.output);
  8928. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  8929. if (ret != 0) {
  8930. flag = ret;
  8931. }
  8932. }
  8933. if (!flag) {
  8934. ret = wc_RipeMdFinal(&ripemd, hash);
  8935. if (ret != 0) {
  8936. flag = ret;
  8937. }
  8938. }
  8939. if (!flag) {
  8940. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  8941. flag = WOLFSSL_FATAL_ERROR;
  8942. }
  8943. }
  8944. /* Pass in bad values. */
  8945. if (!flag) {
  8946. b.input = NULL;
  8947. b.inLen = 0;
  8948. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  8949. if (ret != 0) {
  8950. flag = ret;
  8951. }
  8952. }
  8953. if (!flag) {
  8954. c.input = NULL;
  8955. c.inLen = RIPEMD_DIGEST_SIZE;
  8956. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  8957. if (ret != BAD_FUNC_ARG) {
  8958. flag = WOLFSSL_FATAL_ERROR;
  8959. }
  8960. }
  8961. if (!flag) {
  8962. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8963. if (ret != BAD_FUNC_ARG) {
  8964. flag = WOLFSSL_FATAL_ERROR;
  8965. }
  8966. }
  8967. printf(resultFmt, flag == 0 ? passed : failed);
  8968. #endif
  8969. return flag;
  8970. } /* END test_wc_RipeMdUdpate */
  8971. /*
  8972. * Unit test function for wc_RipeMdFinal()
  8973. */
  8974. static int test_wc_RipeMdFinal (void)
  8975. {
  8976. int flag = 0;
  8977. #ifdef WOLFSSL_RIPEMD
  8978. RipeMd ripemd;
  8979. byte* hash_test[3];
  8980. byte hash1[RIPEMD_DIGEST_SIZE];
  8981. byte hash2[2*RIPEMD_DIGEST_SIZE];
  8982. byte hash3[5*RIPEMD_DIGEST_SIZE];
  8983. int times, i, ret;
  8984. /* Initialize */
  8985. ret = wc_InitRipeMd(&ripemd);
  8986. if (ret != 0) {
  8987. flag = ret;
  8988. }
  8989. if (!flag) {
  8990. hash_test[0] = hash1;
  8991. hash_test[1] = hash2;
  8992. hash_test[2] = hash3;
  8993. }
  8994. times = sizeof(hash_test) / sizeof(byte*);
  8995. /* Good test args. */
  8996. printf(testingFmt, "wc_RipeMdFinal()");
  8997. /* Testing oversized buffers. */
  8998. for (i = 0; i < times; i++) {
  8999. if (!flag) {
  9000. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  9001. if (ret != 0) {
  9002. flag = WOLFSSL_FATAL_ERROR;
  9003. }
  9004. }
  9005. }
  9006. /* Test bad args. */
  9007. if (!flag) {
  9008. ret = wc_RipeMdFinal(NULL, NULL);
  9009. if (ret != BAD_FUNC_ARG) {
  9010. flag = WOLFSSL_FATAL_ERROR;
  9011. }
  9012. }
  9013. if (!flag) {
  9014. ret = wc_RipeMdFinal(NULL, hash1);
  9015. if (ret != BAD_FUNC_ARG) {
  9016. flag = WOLFSSL_FATAL_ERROR;
  9017. }
  9018. }
  9019. if (!flag) {
  9020. ret = wc_RipeMdFinal(&ripemd, NULL);
  9021. if (ret != BAD_FUNC_ARG) {
  9022. flag = WOLFSSL_FATAL_ERROR;
  9023. }
  9024. }
  9025. printf(resultFmt, flag == 0 ? passed : failed);
  9026. #endif
  9027. return flag;
  9028. } /* END test_wc_RipeMdFinal */
  9029. /*
  9030. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  9031. * wc_InitSha3_512
  9032. */
  9033. static int test_wc_InitSha3 (void)
  9034. {
  9035. int ret = 0;
  9036. #if defined(WOLFSSL_SHA3)
  9037. wc_Sha3 sha3;
  9038. (void)sha3;
  9039. #if !defined(WOLFSSL_NOSHA3_224)
  9040. printf(testingFmt, "wc_InitSha3_224()");
  9041. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  9042. /* Test bad args. */
  9043. if (ret == 0) {
  9044. ret = wc_InitSha3_224(NULL, HEAP_HINT, devId);
  9045. if (ret == BAD_FUNC_ARG) {
  9046. ret = 0;
  9047. } else if (ret == 0) {
  9048. ret = WOLFSSL_FATAL_ERROR;
  9049. }
  9050. }
  9051. wc_Sha3_224_Free(&sha3);
  9052. printf(resultFmt, ret == 0 ? passed : failed);
  9053. #endif /* NOSHA3_224 */
  9054. #if !defined(WOLFSSL_NOSHA3_256)
  9055. if (ret == 0) {
  9056. printf(testingFmt, "wc_InitSha3_256()");
  9057. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  9058. /* Test bad args. */
  9059. if (ret == 0) {
  9060. ret = wc_InitSha3_256(NULL, HEAP_HINT, devId);
  9061. if (ret == BAD_FUNC_ARG) {
  9062. ret = 0;
  9063. } else if (ret == 0) {
  9064. ret = WOLFSSL_FATAL_ERROR;
  9065. }
  9066. }
  9067. wc_Sha3_256_Free(&sha3);
  9068. printf(resultFmt, ret == 0 ? passed : failed);
  9069. } /* END sha3_256 */
  9070. #endif /* NOSHA3_256 */
  9071. #if !defined(WOLFSSL_NOSHA3_384)
  9072. if (ret == 0) {
  9073. printf(testingFmt, "wc_InitSha3_384()");
  9074. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  9075. /* Test bad args. */
  9076. if (ret == 0) {
  9077. ret = wc_InitSha3_384(NULL, HEAP_HINT, devId);
  9078. if (ret == BAD_FUNC_ARG) {
  9079. ret = 0;
  9080. } else if (ret == 0) {
  9081. ret = WOLFSSL_FATAL_ERROR;
  9082. }
  9083. }
  9084. wc_Sha3_384_Free(&sha3);
  9085. printf(resultFmt, ret == 0 ? passed : failed);
  9086. } /* END sha3_384 */
  9087. #endif /* NOSHA3_384 */
  9088. #if !defined(WOLFSSL_NOSHA3_512)
  9089. if (ret == 0) {
  9090. printf(testingFmt, "wc_InitSha3_512()");
  9091. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  9092. /* Test bad args. */
  9093. if (ret == 0) {
  9094. ret = wc_InitSha3_512(NULL, HEAP_HINT, devId);
  9095. if (ret == BAD_FUNC_ARG) {
  9096. ret = 0;
  9097. } else if (ret == 0) {
  9098. ret = WOLFSSL_FATAL_ERROR;
  9099. }
  9100. }
  9101. wc_Sha3_512_Free(&sha3);
  9102. printf(resultFmt, ret == 0 ? passed : failed);
  9103. } /* END sha3_512 */
  9104. #endif /* NOSHA3_512 */
  9105. #endif
  9106. return ret;
  9107. } /* END test_wc_InitSha3 */
  9108. /*
  9109. * Testing wc_Sha3_Update()
  9110. */
  9111. static int testing_wc_Sha3_Update (void)
  9112. {
  9113. int ret = 0;
  9114. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  9115. !defined(WOLFSSL_AFALG_XILINX)
  9116. wc_Sha3 sha3;
  9117. byte msg[] = "Everybody's working for the weekend.";
  9118. byte msg2[] = "Everybody gets Friday off.";
  9119. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  9120. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  9121. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  9122. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  9123. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  9124. word32 msglen = sizeof(msg) - 1;
  9125. word32 msg2len = sizeof(msg2);
  9126. word32 msgCmplen = sizeof(msgCmp);
  9127. #if !defined(WOLFSSL_NOSHA3_224)
  9128. printf(testingFmt, "wc_Sha3_224_Update()");
  9129. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  9130. if (ret != 0) {
  9131. return ret;
  9132. }
  9133. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  9134. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  9135. ret = WOLFSSL_FATAL_ERROR;
  9136. }
  9137. if (ret == 0) {
  9138. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  9139. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  9140. ret = WOLFSSL_FATAL_ERROR;
  9141. }
  9142. }
  9143. /* Pass bad args. */
  9144. if (ret == 0) {
  9145. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  9146. if (ret == BAD_FUNC_ARG) {
  9147. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  9148. }
  9149. if (ret == BAD_FUNC_ARG) {
  9150. wc_Sha3_224_Free(&sha3);
  9151. if (wc_InitSha3_224(&sha3, HEAP_HINT, devId)) {
  9152. return ret;
  9153. }
  9154. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  9155. if (ret == 0) {
  9156. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  9157. }
  9158. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  9159. ret = WOLFSSL_FATAL_ERROR;
  9160. }
  9161. }
  9162. }
  9163. wc_Sha3_224_Free(&sha3);
  9164. printf(resultFmt, ret == 0 ? passed : failed);
  9165. #endif /* SHA3_224 */
  9166. #if !defined(WOLFSSL_NOSHA3_256)
  9167. if (ret == 0) {
  9168. printf(testingFmt, "wc_Sha3_256_Update()");
  9169. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  9170. if (ret != 0) {
  9171. return ret;
  9172. }
  9173. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  9174. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  9175. ret = WOLFSSL_FATAL_ERROR;
  9176. }
  9177. if (ret == 0) {
  9178. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  9179. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  9180. ret = WOLFSSL_FATAL_ERROR;
  9181. }
  9182. }
  9183. /* Pass bad args. */
  9184. if (ret == 0) {
  9185. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  9186. if (ret == BAD_FUNC_ARG) {
  9187. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  9188. }
  9189. if (ret == BAD_FUNC_ARG) {
  9190. wc_Sha3_256_Free(&sha3);
  9191. if (wc_InitSha3_256(&sha3, HEAP_HINT, devId)) {
  9192. return ret;
  9193. }
  9194. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  9195. if (ret == 0) {
  9196. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  9197. }
  9198. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  9199. ret = WOLFSSL_FATAL_ERROR;
  9200. }
  9201. }
  9202. }
  9203. wc_Sha3_256_Free(&sha3);
  9204. printf(resultFmt, ret == 0 ? passed : failed);
  9205. }
  9206. #endif /* SHA3_256 */
  9207. #if !defined(WOLFSSL_NOSHA3_384)
  9208. if (ret == 0) {
  9209. printf(testingFmt, "wc_Sha3_384_Update()");
  9210. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  9211. if (ret != 0) {
  9212. return ret;
  9213. }
  9214. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  9215. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  9216. ret = WOLFSSL_FATAL_ERROR;
  9217. }
  9218. if (ret == 0) {
  9219. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  9220. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  9221. ret = WOLFSSL_FATAL_ERROR;
  9222. }
  9223. }
  9224. /* Pass bad args. */
  9225. if (ret == 0) {
  9226. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  9227. if (ret == BAD_FUNC_ARG) {
  9228. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  9229. }
  9230. if (ret == BAD_FUNC_ARG) {
  9231. wc_Sha3_384_Free(&sha3);
  9232. if (wc_InitSha3_384(&sha3, HEAP_HINT, devId)) {
  9233. return ret;
  9234. }
  9235. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  9236. if (ret == 0) {
  9237. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  9238. }
  9239. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  9240. ret = WOLFSSL_FATAL_ERROR;
  9241. }
  9242. }
  9243. }
  9244. wc_Sha3_384_Free(&sha3);
  9245. printf(resultFmt, ret == 0 ? passed : failed);
  9246. }
  9247. #endif /* SHA3_384 */
  9248. #if !defined(WOLFSSL_NOSHA3_512)
  9249. if (ret == 0) {
  9250. printf(testingFmt, "wc_Sha3_512_Update()");
  9251. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  9252. if (ret != 0) {
  9253. return ret;
  9254. }
  9255. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  9256. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  9257. ret = WOLFSSL_FATAL_ERROR;
  9258. }
  9259. if (ret == 0) {
  9260. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  9261. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  9262. ret = WOLFSSL_FATAL_ERROR;
  9263. }
  9264. }
  9265. /* Pass bad args. */
  9266. if (ret == 0) {
  9267. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  9268. if (ret == BAD_FUNC_ARG) {
  9269. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  9270. }
  9271. if (ret == BAD_FUNC_ARG) {
  9272. wc_Sha3_512_Free(&sha3);
  9273. if (wc_InitSha3_512(&sha3, HEAP_HINT, devId)) {
  9274. return ret;
  9275. }
  9276. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  9277. if (ret == 0) {
  9278. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  9279. }
  9280. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  9281. ret = WOLFSSL_FATAL_ERROR;
  9282. }
  9283. }
  9284. }
  9285. wc_Sha3_512_Free(&sha3);
  9286. printf(resultFmt, ret == 0 ? passed : failed);
  9287. }
  9288. #endif /* SHA3_512 */
  9289. #endif /* WOLFSSL_SHA3 */
  9290. return ret;
  9291. } /* END testing_wc_Sha3_Update */
  9292. /*
  9293. * Testing wc_Sha3_224_Final()
  9294. */
  9295. static int test_wc_Sha3_224_Final (void)
  9296. {
  9297. int ret = 0;
  9298. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  9299. wc_Sha3 sha3;
  9300. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  9301. "nopnopq";
  9302. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  9303. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  9304. "\x64\xea\xd0\xfc\xce\x33";
  9305. byte hash[WC_SHA3_224_DIGEST_SIZE];
  9306. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  9307. /* Init stack variables. */
  9308. XMEMSET(hash, 0, sizeof(hash));
  9309. printf(testingFmt, "wc_Sha3_224_Final()");
  9310. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  9311. if (ret != 0) {
  9312. return ret;
  9313. }
  9314. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  9315. if (ret == 0) {
  9316. ret = wc_Sha3_224_Final(&sha3, hash);
  9317. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  9318. ret = WOLFSSL_FATAL_ERROR;
  9319. }
  9320. }
  9321. /* Test bad args. */
  9322. if (ret == 0) {
  9323. ret = wc_Sha3_224_Final(NULL, hash);
  9324. if (ret == 0) {
  9325. ret = wc_Sha3_224_Final(&sha3, NULL);
  9326. }
  9327. if (ret == BAD_FUNC_ARG) {
  9328. ret = 0;
  9329. } else if (ret == 0) {
  9330. ret = WOLFSSL_FATAL_ERROR;
  9331. }
  9332. }
  9333. wc_Sha3_224_Free(&sha3);
  9334. printf(resultFmt, ret == 0 ? passed : failed);
  9335. if (ret == 0) {
  9336. printf(testingFmt, "wc_Sha3_224_GetHash()");
  9337. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  9338. if (ret != 0) {
  9339. return ret;
  9340. }
  9341. /* Init stack variables. */
  9342. XMEMSET(hash, 0, sizeof(hash));
  9343. XMEMSET(hashRet, 0, sizeof(hashRet));
  9344. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  9345. if (ret == 0) {
  9346. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  9347. }
  9348. if (ret == 0) {
  9349. ret = wc_Sha3_224_Final(&sha3, hash);
  9350. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  9351. ret = WOLFSSL_FATAL_ERROR;
  9352. }
  9353. }
  9354. if (ret == 0) {
  9355. /* Test bad args. */
  9356. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  9357. if (ret == BAD_FUNC_ARG) {
  9358. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  9359. }
  9360. if (ret == BAD_FUNC_ARG) {
  9361. ret = 0;
  9362. } else if (ret == 0) {
  9363. ret = WOLFSSL_FATAL_ERROR;
  9364. }
  9365. }
  9366. printf(resultFmt, ret == 0 ? passed : failed);
  9367. }
  9368. wc_Sha3_224_Free(&sha3);
  9369. #endif
  9370. return ret;
  9371. } /* END test_wc_Sha3_224_Final */
  9372. /*
  9373. * Testing wc_Sha3_256_Final()
  9374. */
  9375. static int test_wc_Sha3_256_Final (void)
  9376. {
  9377. int ret = 0;
  9378. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  9379. wc_Sha3 sha3;
  9380. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  9381. "nopnopq";
  9382. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  9383. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  9384. "\xdd\x97\x49\x6d\x33\x76";
  9385. byte hash[WC_SHA3_256_DIGEST_SIZE];
  9386. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  9387. /* Init stack variables. */
  9388. XMEMSET(hash, 0, sizeof(hash));
  9389. printf(testingFmt, "wc_Sha3_256_Final()");
  9390. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  9391. if (ret != 0) {
  9392. return ret;
  9393. }
  9394. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  9395. if (ret == 0) {
  9396. ret = wc_Sha3_256_Final(&sha3, hash);
  9397. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  9398. ret = WOLFSSL_FATAL_ERROR;
  9399. }
  9400. }
  9401. /* Test bad args. */
  9402. if (ret == 0) {
  9403. ret = wc_Sha3_256_Final(NULL, hash);
  9404. if (ret == 0) {
  9405. ret = wc_Sha3_256_Final(&sha3, NULL);
  9406. }
  9407. if (ret == BAD_FUNC_ARG) {
  9408. ret = 0;
  9409. } else if (ret == 0) {
  9410. ret = WOLFSSL_FATAL_ERROR;
  9411. }
  9412. }
  9413. wc_Sha3_256_Free(&sha3);
  9414. printf(resultFmt, ret == 0 ? passed : failed);
  9415. if (ret == 0) {
  9416. printf(testingFmt, "wc_Sha3_256_GetHash()");
  9417. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  9418. if (ret != 0) {
  9419. return ret;
  9420. }
  9421. /* Init stack variables. */
  9422. XMEMSET(hash, 0, sizeof(hash));
  9423. XMEMSET(hashRet, 0, sizeof(hashRet));
  9424. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  9425. if (ret == 0) {
  9426. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  9427. }
  9428. if (ret == 0) {
  9429. ret = wc_Sha3_256_Final(&sha3, hash);
  9430. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  9431. ret = WOLFSSL_FATAL_ERROR;
  9432. }
  9433. }
  9434. if (ret == 0) {
  9435. /* Test bad args. */
  9436. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  9437. if (ret == BAD_FUNC_ARG) {
  9438. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  9439. }
  9440. if (ret == BAD_FUNC_ARG) {
  9441. ret = 0;
  9442. } else if (ret == 0) {
  9443. ret = WOLFSSL_FATAL_ERROR;
  9444. }
  9445. }
  9446. printf(resultFmt, ret == 0 ? passed : failed);
  9447. }
  9448. wc_Sha3_256_Free(&sha3);
  9449. #endif
  9450. return ret;
  9451. } /* END test_wc_Sha3_256_Final */
  9452. /*
  9453. * Testing wc_Sha3_384_Final()
  9454. */
  9455. static int test_wc_Sha3_384_Final (void)
  9456. {
  9457. int ret = 0;
  9458. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  9459. wc_Sha3 sha3;
  9460. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  9461. "nopnopq";
  9462. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  9463. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  9464. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  9465. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  9466. byte hash[WC_SHA3_384_DIGEST_SIZE];
  9467. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  9468. /* Init stack variables. */
  9469. XMEMSET(hash, 0, sizeof(hash));
  9470. printf(testingFmt, "wc_Sha3_384_Final()");
  9471. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  9472. if (ret != 0) {
  9473. return ret;
  9474. }
  9475. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  9476. if (ret == 0) {
  9477. ret = wc_Sha3_384_Final(&sha3, hash);
  9478. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  9479. ret = WOLFSSL_FATAL_ERROR;
  9480. }
  9481. }
  9482. /* Test bad args. */
  9483. if (ret == 0) {
  9484. ret = wc_Sha3_384_Final(NULL, hash);
  9485. if (ret == 0) {
  9486. ret = wc_Sha3_384_Final(&sha3, NULL);
  9487. }
  9488. if (ret == BAD_FUNC_ARG) {
  9489. ret = 0;
  9490. } else if (ret == 0) {
  9491. ret = WOLFSSL_FATAL_ERROR;
  9492. }
  9493. }
  9494. wc_Sha3_384_Free(&sha3);
  9495. printf(resultFmt, ret == 0 ? passed : failed);
  9496. if (ret == 0) {
  9497. printf(testingFmt, "wc_Sha3_384_GetHash()");
  9498. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  9499. if (ret != 0) {
  9500. return ret;
  9501. }
  9502. /* Init stack variables. */
  9503. XMEMSET(hash, 0, sizeof(hash));
  9504. XMEMSET(hashRet, 0, sizeof(hashRet));
  9505. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  9506. if (ret == 0) {
  9507. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  9508. }
  9509. if (ret == 0) {
  9510. ret = wc_Sha3_384_Final(&sha3, hash);
  9511. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  9512. ret = WOLFSSL_FATAL_ERROR;
  9513. }
  9514. }
  9515. if (ret == 0) {
  9516. /* Test bad args. */
  9517. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  9518. if (ret == BAD_FUNC_ARG) {
  9519. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  9520. }
  9521. if (ret == BAD_FUNC_ARG) {
  9522. ret = 0;
  9523. } else if (ret == 0) {
  9524. ret = WOLFSSL_FATAL_ERROR;
  9525. }
  9526. }
  9527. printf(resultFmt, ret == 0 ? passed : failed);
  9528. }
  9529. wc_Sha3_384_Free(&sha3);
  9530. #endif
  9531. return ret;
  9532. } /* END test_wc_Sha3_384_Final */
  9533. /*
  9534. * Testing wc_Sha3_512_Final()
  9535. */
  9536. static int test_wc_Sha3_512_Final (void)
  9537. {
  9538. int ret = 0;
  9539. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  9540. !defined(WOLFSSL_NOSHA3_384)
  9541. wc_Sha3 sha3;
  9542. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  9543. "nopnopq";
  9544. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  9545. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  9546. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  9547. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  9548. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  9549. byte hash[WC_SHA3_512_DIGEST_SIZE];
  9550. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  9551. /* Init stack variables. */
  9552. XMEMSET(hash, 0, sizeof(hash));
  9553. printf(testingFmt, "wc_Sha3_512_Final()");
  9554. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  9555. if (ret != 0) {
  9556. return ret;
  9557. }
  9558. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  9559. if (ret == 0) {
  9560. ret = wc_Sha3_512_Final(&sha3, hash);
  9561. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  9562. ret = WOLFSSL_FATAL_ERROR;
  9563. }
  9564. }
  9565. /* Test bad args. */
  9566. if (ret == 0) {
  9567. ret = wc_Sha3_512_Final(NULL, hash);
  9568. if (ret == 0) {
  9569. ret = wc_Sha3_384_Final(&sha3, NULL);
  9570. }
  9571. if (ret == BAD_FUNC_ARG) {
  9572. ret = 0;
  9573. } else if (ret == 0) {
  9574. ret = WOLFSSL_FATAL_ERROR;
  9575. }
  9576. }
  9577. wc_Sha3_512_Free(&sha3);
  9578. printf(resultFmt, ret == 0 ? passed : failed);
  9579. if (ret == 0) {
  9580. printf(testingFmt, "wc_Sha3_512_GetHash()");
  9581. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  9582. if (ret != 0) {
  9583. return ret;
  9584. }
  9585. /* Init stack variables. */
  9586. XMEMSET(hash, 0, sizeof(hash));
  9587. XMEMSET(hashRet, 0, sizeof(hashRet));
  9588. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  9589. if (ret == 0) {
  9590. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  9591. }
  9592. if (ret == 0) {
  9593. ret = wc_Sha3_512_Final(&sha3, hash);
  9594. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  9595. ret = WOLFSSL_FATAL_ERROR;
  9596. }
  9597. }
  9598. if (ret == 0) {
  9599. /* Test bad args. */
  9600. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  9601. if (ret == BAD_FUNC_ARG) {
  9602. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  9603. }
  9604. if (ret == BAD_FUNC_ARG) {
  9605. ret = 0;
  9606. } else if (ret == 0) {
  9607. ret = WOLFSSL_FATAL_ERROR;
  9608. }
  9609. }
  9610. printf(resultFmt, ret == 0 ? passed : failed);
  9611. }
  9612. wc_Sha3_512_Free(&sha3);
  9613. #endif
  9614. return ret;
  9615. } /* END test_wc_Sha3_512_Final */
  9616. /*
  9617. * Testing wc_Sha3_224_Copy()
  9618. */
  9619. static int test_wc_Sha3_224_Copy (void)
  9620. {
  9621. int ret = 0;
  9622. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  9623. wc_Sha3 sha3, sha3Cpy;
  9624. const char* msg = TEST_STRING;
  9625. word32 msglen = (word32)TEST_STRING_SZ;
  9626. byte hash[WC_SHA3_224_DIGEST_SIZE];
  9627. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  9628. XMEMSET(hash, 0, sizeof(hash));
  9629. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  9630. printf(testingFmt, "wc_Sha3_224_Copy()");
  9631. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  9632. if (ret != 0) {
  9633. return ret;
  9634. }
  9635. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, devId);
  9636. if (ret != 0) {
  9637. wc_Sha3_224_Free(&sha3);
  9638. return ret;
  9639. }
  9640. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  9641. if (ret == 0) {
  9642. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  9643. if (ret == 0) {
  9644. ret = wc_Sha3_224_Final(&sha3, hash);
  9645. if (ret == 0) {
  9646. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  9647. }
  9648. }
  9649. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  9650. ret = WOLFSSL_FATAL_ERROR;
  9651. }
  9652. }
  9653. /* Test bad args. */
  9654. if (ret == 0) {
  9655. ret = wc_Sha3_224_Copy(NULL, &sha3);
  9656. if (ret == BAD_FUNC_ARG) {
  9657. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  9658. }
  9659. if (ret == BAD_FUNC_ARG) {
  9660. ret = 0;
  9661. } else if (ret == 0) {
  9662. ret = WOLFSSL_FATAL_ERROR;
  9663. }
  9664. }
  9665. printf(resultFmt, ret == 0 ? passed : failed);
  9666. #endif
  9667. return ret;
  9668. } /* END test_wc_Sha3_224_Copy */
  9669. /*
  9670. * Testing wc_Sha3_256_Copy()
  9671. */
  9672. static int test_wc_Sha3_256_Copy (void)
  9673. {
  9674. int ret = 0;
  9675. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  9676. wc_Sha3 sha3, sha3Cpy;
  9677. const char* msg = TEST_STRING;
  9678. word32 msglen = (word32)TEST_STRING_SZ;
  9679. byte hash[WC_SHA3_256_DIGEST_SIZE];
  9680. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  9681. XMEMSET(hash, 0, sizeof(hash));
  9682. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  9683. printf(testingFmt, "wc_Sha3_256_Copy()");
  9684. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  9685. if (ret != 0) {
  9686. return ret;
  9687. }
  9688. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, devId);
  9689. if (ret != 0) {
  9690. wc_Sha3_256_Free(&sha3);
  9691. return ret;
  9692. }
  9693. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  9694. if (ret == 0) {
  9695. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  9696. if (ret == 0) {
  9697. ret = wc_Sha3_256_Final(&sha3, hash);
  9698. if (ret == 0) {
  9699. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  9700. }
  9701. }
  9702. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  9703. ret = WOLFSSL_FATAL_ERROR;
  9704. }
  9705. }
  9706. /* Test bad args. */
  9707. if (ret == 0) {
  9708. ret = wc_Sha3_256_Copy(NULL, &sha3);
  9709. if (ret == BAD_FUNC_ARG) {
  9710. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  9711. }
  9712. if (ret == BAD_FUNC_ARG) {
  9713. ret = 0;
  9714. } else if (ret == 0) {
  9715. ret = WOLFSSL_FATAL_ERROR;
  9716. }
  9717. }
  9718. printf(resultFmt, ret == 0 ? passed : failed);
  9719. #endif
  9720. return ret;
  9721. } /* END test_wc_Sha3_256_Copy */
  9722. /*
  9723. * Testing wc_Sha3_384_Copy()
  9724. */
  9725. static int test_wc_Sha3_384_Copy (void)
  9726. {
  9727. int ret = 0;
  9728. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  9729. wc_Sha3 sha3, sha3Cpy;
  9730. const char* msg = TEST_STRING;
  9731. word32 msglen = (word32)TEST_STRING_SZ;
  9732. byte hash[WC_SHA3_384_DIGEST_SIZE];
  9733. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  9734. XMEMSET(hash, 0, sizeof(hash));
  9735. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  9736. printf(testingFmt, "wc_Sha3_384_Copy()");
  9737. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  9738. if (ret != 0) {
  9739. return ret;
  9740. }
  9741. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, devId);
  9742. if (ret != 0) {
  9743. wc_Sha3_384_Free(&sha3);
  9744. return ret;
  9745. }
  9746. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  9747. if (ret == 0) {
  9748. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  9749. if (ret == 0) {
  9750. ret = wc_Sha3_384_Final(&sha3, hash);
  9751. if (ret == 0) {
  9752. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  9753. }
  9754. }
  9755. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  9756. ret = WOLFSSL_FATAL_ERROR;
  9757. }
  9758. }
  9759. /* Test bad args. */
  9760. if (ret == 0) {
  9761. ret = wc_Sha3_384_Copy(NULL, &sha3);
  9762. if (ret == BAD_FUNC_ARG) {
  9763. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  9764. }
  9765. if (ret == BAD_FUNC_ARG) {
  9766. ret = 0;
  9767. } else if (ret == 0) {
  9768. ret = WOLFSSL_FATAL_ERROR;
  9769. }
  9770. }
  9771. printf(resultFmt, ret == 0 ? passed : failed);
  9772. #endif
  9773. return ret;
  9774. } /* END test_wc_Sha3_384_Copy */
  9775. /*
  9776. * Testing wc_Sha3_512_Copy()
  9777. */
  9778. static int test_wc_Sha3_512_Copy (void)
  9779. {
  9780. int ret = 0;
  9781. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  9782. wc_Sha3 sha3, sha3Cpy;
  9783. const char* msg = TEST_STRING;
  9784. word32 msglen = (word32)TEST_STRING_SZ;
  9785. byte hash[WC_SHA3_512_DIGEST_SIZE];
  9786. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  9787. XMEMSET(hash, 0, sizeof(hash));
  9788. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  9789. printf(testingFmt, "wc_Sha3_512_Copy()");
  9790. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  9791. if (ret != 0) {
  9792. return ret;
  9793. }
  9794. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, devId);
  9795. if (ret != 0) {
  9796. wc_Sha3_512_Free(&sha3);
  9797. return ret;
  9798. }
  9799. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  9800. if (ret == 0) {
  9801. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  9802. if (ret == 0) {
  9803. ret = wc_Sha3_512_Final(&sha3, hash);
  9804. if (ret == 0) {
  9805. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  9806. }
  9807. }
  9808. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  9809. ret = WOLFSSL_FATAL_ERROR;
  9810. }
  9811. }
  9812. /* Test bad args. */
  9813. if (ret == 0) {
  9814. ret = wc_Sha3_512_Copy(NULL, &sha3);
  9815. if (ret == BAD_FUNC_ARG) {
  9816. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  9817. }
  9818. if (ret == BAD_FUNC_ARG) {
  9819. ret = 0;
  9820. } else if (ret == 0) {
  9821. ret = WOLFSSL_FATAL_ERROR;
  9822. }
  9823. }
  9824. printf(resultFmt, ret == 0 ? passed : failed);
  9825. #endif
  9826. return ret;
  9827. } /* END test_wc_Sha3_512_Copy */
  9828. /*
  9829. * Unit test function for wc_Sha3_GetFlags()
  9830. */
  9831. static int test_wc_Sha3_GetFlags (void)
  9832. {
  9833. int ret = 0;
  9834. #if defined(WOLFSSL_SHA3) && \
  9835. (defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB))
  9836. wc_Sha3 sha3;
  9837. word32 flags = 0;
  9838. printf(testingFmt, "wc_Sha3_GetFlags()");
  9839. /* Initialize */
  9840. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  9841. if (ret != 0) {
  9842. return ret;
  9843. }
  9844. if (ret == 0) {
  9845. ret = wc_Sha3_GetFlags(&sha3, &flags);
  9846. }
  9847. if (ret == 0) {
  9848. if (flags & WC_HASH_FLAG_ISCOPY) {
  9849. ret = 0;
  9850. }
  9851. }
  9852. wc_Sha3_224_Free(&sha3);
  9853. printf(resultFmt, ret == 0 ? passed : failed);
  9854. #endif
  9855. return ret;
  9856. } /* END test_wc_Sha3_GetFlags */
  9857. static int test_wc_InitShake256 (void)
  9858. {
  9859. int ret = 0;
  9860. #ifdef WOLFSSL_SHAKE256
  9861. wc_Shake shake;
  9862. printf(testingFmt, "wc_InitShake256()");
  9863. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  9864. /* Test bad args. */
  9865. if (ret == 0) {
  9866. ret = wc_InitShake256(NULL, HEAP_HINT, devId);
  9867. if (ret == BAD_FUNC_ARG) {
  9868. ret = 0;
  9869. } else if (ret == 0) {
  9870. ret = WOLFSSL_FATAL_ERROR;
  9871. }
  9872. }
  9873. wc_Shake256_Free(&shake);
  9874. printf(resultFmt, ret == 0 ? passed : failed);
  9875. #endif
  9876. return ret;
  9877. } /* END test_wc_InitSha3 */
  9878. static int testing_wc_Shake256_Update (void)
  9879. {
  9880. int ret = 0;
  9881. #ifdef WOLFSSL_SHAKE256
  9882. wc_Shake shake;
  9883. byte msg[] = "Everybody's working for the weekend.";
  9884. byte msg2[] = "Everybody gets Friday off.";
  9885. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  9886. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  9887. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  9888. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  9889. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  9890. word32 msglen = sizeof(msg) - 1;
  9891. word32 msg2len = sizeof(msg2);
  9892. word32 msgCmplen = sizeof(msgCmp);
  9893. printf(testingFmt, "wc_Shake256_Update()");
  9894. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  9895. if (ret != 0) {
  9896. return ret;
  9897. }
  9898. ret = wc_Shake256_Update(&shake, msg, msglen);
  9899. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  9900. ret = WOLFSSL_FATAL_ERROR;
  9901. }
  9902. if (ret == 0) {
  9903. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  9904. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  9905. ret = WOLFSSL_FATAL_ERROR;
  9906. }
  9907. }
  9908. /* Pass bad args. */
  9909. if (ret == 0) {
  9910. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  9911. if (ret == BAD_FUNC_ARG) {
  9912. ret = wc_Shake256_Update(&shake, NULL, 5);
  9913. }
  9914. if (ret == BAD_FUNC_ARG) {
  9915. wc_Shake256_Free(&shake);
  9916. if (wc_InitShake256(&shake, HEAP_HINT, devId)) {
  9917. return ret;
  9918. }
  9919. ret = wc_Shake256_Update(&shake, NULL, 0);
  9920. if (ret == 0) {
  9921. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  9922. }
  9923. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  9924. ret = WOLFSSL_FATAL_ERROR;
  9925. }
  9926. }
  9927. }
  9928. wc_Shake256_Free(&shake);
  9929. printf(resultFmt, ret == 0 ? passed : failed);
  9930. #endif /* WOLFSSL_SHAKE256 */
  9931. return ret;
  9932. }
  9933. static int test_wc_Shake256_Final (void)
  9934. {
  9935. int ret = 0;
  9936. #ifdef WOLFSSL_SHAKE256
  9937. wc_Shake shake;
  9938. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  9939. "nopnopq";
  9940. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  9941. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  9942. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  9943. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  9944. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  9945. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  9946. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  9947. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  9948. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  9949. byte hash[114];
  9950. /* Init stack variables. */
  9951. XMEMSET(hash, 0, sizeof(hash));
  9952. printf(testingFmt, "wc_Shake256_Final()");
  9953. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  9954. if (ret != 0) {
  9955. return ret;
  9956. }
  9957. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  9958. if (ret == 0) {
  9959. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  9960. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  9961. ret = WOLFSSL_FATAL_ERROR;
  9962. }
  9963. }
  9964. /* Test bad args. */
  9965. if (ret == 0) {
  9966. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  9967. if (ret == 0) {
  9968. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  9969. }
  9970. if (ret == BAD_FUNC_ARG) {
  9971. ret = 0;
  9972. } else if (ret == 0) {
  9973. ret = WOLFSSL_FATAL_ERROR;
  9974. }
  9975. }
  9976. wc_Shake256_Free(&shake);
  9977. printf(resultFmt, ret == 0 ? passed : failed);
  9978. #endif
  9979. return ret;
  9980. }
  9981. /*
  9982. * Testing wc_Shake256_Copy()
  9983. */
  9984. static int test_wc_Shake256_Copy (void)
  9985. {
  9986. int ret = 0;
  9987. #ifdef WOLFSSL_SHAKE256
  9988. wc_Shake shake, shakeCpy;
  9989. const char* msg = TEST_STRING;
  9990. word32 msglen = (word32)TEST_STRING_SZ;
  9991. byte hash[144];
  9992. byte hashCpy[144];
  9993. word32 hashLen = sizeof(hash);
  9994. word32 hashLenCpy = sizeof(hashCpy);
  9995. XMEMSET(hash, 0, sizeof(hash));
  9996. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  9997. printf(testingFmt, "wc_Shake256_Copy()");
  9998. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  9999. if (ret != 0) {
  10000. return ret;
  10001. }
  10002. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, devId);
  10003. if (ret != 0) {
  10004. wc_Shake256_Free(&shake);
  10005. return ret;
  10006. }
  10007. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  10008. if (ret == 0) {
  10009. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  10010. if (ret == 0) {
  10011. ret = wc_Shake256_Final(&shake, hash, hashLen);
  10012. if (ret == 0) {
  10013. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  10014. }
  10015. }
  10016. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  10017. ret = WOLFSSL_FATAL_ERROR;
  10018. }
  10019. }
  10020. /* Test bad args. */
  10021. if (ret == 0) {
  10022. ret = wc_Shake256_Copy(NULL, &shake);
  10023. if (ret == BAD_FUNC_ARG) {
  10024. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  10025. }
  10026. if (ret == BAD_FUNC_ARG) {
  10027. ret = 0;
  10028. } else if (ret == 0) {
  10029. ret = WOLFSSL_FATAL_ERROR;
  10030. }
  10031. }
  10032. wc_Shake256_Free(&shake);
  10033. printf(resultFmt, ret == 0 ? passed : failed);
  10034. #endif
  10035. return ret;
  10036. } /* END test_wc_Shake256_Copy */
  10037. /*
  10038. * Unit test function for wc_Shake256Hash()
  10039. */
  10040. static int test_wc_Shake256Hash(void)
  10041. {
  10042. int ret = 0;
  10043. #ifdef WOLFSSL_SHAKE256
  10044. const byte data[] = { /* Hello World */
  10045. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  10046. 0x72,0x6c,0x64
  10047. };
  10048. word32 len = sizeof(data);
  10049. byte hash[144];
  10050. word32 hashLen = sizeof(hash);
  10051. printf(testingFmt, "wc_Shake256Hash()");
  10052. ret = wc_Shake256Hash(data, len, hash, hashLen);
  10053. printf(resultFmt, ret == 0 ? passed : failed);
  10054. #endif
  10055. return ret;
  10056. } /* END test_wc_Shake256Hash */
  10057. /*
  10058. * unit test for wc_IdeaSetKey()
  10059. */
  10060. static int test_wc_IdeaSetKey (void)
  10061. {
  10062. int ret = 0;
  10063. #ifdef HAVE_IDEA
  10064. Idea idea;
  10065. const byte key[] =
  10066. {
  10067. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  10068. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  10069. };
  10070. int flag = 0;
  10071. printf(testingFmt, "wc_IdeaSetKey()");
  10072. /*IV can be NULL, default value is 0*/
  10073. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  10074. if (ret == 0) {
  10075. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  10076. }
  10077. /* Bad args. */
  10078. if (ret == 0) {
  10079. ret = wc_IdeaSetKey(NULL, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  10080. if (ret != BAD_FUNC_ARG) {
  10081. flag = 1;
  10082. }
  10083. ret = wc_IdeaSetKey(&idea, NULL, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  10084. if (ret != BAD_FUNC_ARG) {
  10085. flag = 1;
  10086. }
  10087. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE - 1,
  10088. NULL, IDEA_ENCRYPTION);
  10089. if (ret != BAD_FUNC_ARG) {
  10090. flag = 1;
  10091. }
  10092. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, -1);
  10093. if (ret != BAD_FUNC_ARG) {
  10094. flag = 1;
  10095. }
  10096. if (flag == 1) {
  10097. ret = WOLFSSL_FATAL_ERROR;
  10098. } else {
  10099. ret = 0;
  10100. }
  10101. } /* END Test Bad Args. */
  10102. printf(resultFmt, ret == 0 ? passed : failed);
  10103. #endif
  10104. return ret;
  10105. } /* END test_wc_IdeaSetKey */
  10106. /*
  10107. * Unit test for wc_IdeaSetIV()
  10108. */
  10109. static int test_wc_IdeaSetIV (void)
  10110. {
  10111. int ret = 0;
  10112. #ifdef HAVE_IDEA
  10113. Idea idea;
  10114. printf(testingFmt, "wc_IdeaSetIV()");
  10115. ret = wc_IdeaSetIV(&idea, NULL);
  10116. /* Test bad args. */
  10117. if (ret == 0) {
  10118. ret = wc_IdeaSetIV(NULL, NULL);
  10119. if (ret == BAD_FUNC_ARG) {
  10120. ret = 0;
  10121. } else {
  10122. ret = WOLFSSL_FATAL_ERROR;
  10123. }
  10124. }
  10125. printf(resultFmt, ret == 0 ? passed : failed);
  10126. #endif
  10127. return ret;
  10128. } /* END test_wc_IdeaSetIV */
  10129. /*
  10130. * Unit test for wc_IdeaCipher()
  10131. */
  10132. static int test_wc_IdeaCipher (void)
  10133. {
  10134. int ret = 0;
  10135. #ifdef HAVE_IDEA
  10136. Idea idea;
  10137. const byte key[] =
  10138. {
  10139. 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  10140. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48
  10141. };
  10142. const byte plain[] =
  10143. {
  10144. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  10145. };
  10146. byte enc[sizeof(plain)];
  10147. byte dec[sizeof(enc)];
  10148. printf(testingFmt, "wc_IdeaCipher()");
  10149. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  10150. if (ret == 0) {
  10151. ret = wc_IdeaCipher(&idea, enc, plain);
  10152. if (ret != 0) {
  10153. ret = WOLFSSL_FATAL_ERROR;
  10154. }
  10155. }
  10156. if (ret == 0) {
  10157. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  10158. if (ret == 0) {
  10159. ret = wc_IdeaCipher(&idea, dec, enc);
  10160. }
  10161. if (ret == 0) {
  10162. ret = XMEMCMP(plain, dec, IDEA_BLOCK_SIZE);
  10163. }
  10164. if (ret != 0) {
  10165. ret = WOLFSSL_FATAL_ERROR;
  10166. }
  10167. }
  10168. /* Pass Bad Args. */
  10169. if (ret == 0) {
  10170. ret = wc_IdeaCipher(NULL, enc, dec);
  10171. if (ret == BAD_FUNC_ARG) {
  10172. ret = wc_IdeaCipher(&idea, NULL, dec);
  10173. }
  10174. if (ret == BAD_FUNC_ARG) {
  10175. ret = wc_IdeaCipher(&idea, enc, NULL);
  10176. }
  10177. if (ret == BAD_FUNC_ARG) {
  10178. ret = 0;
  10179. } else {
  10180. ret = WOLFSSL_FATAL_ERROR;
  10181. }
  10182. }
  10183. printf(resultFmt, ret == 0 ? passed : failed);
  10184. #endif
  10185. return ret;
  10186. } /* END test_wc_IdeaCipher */
  10187. /*
  10188. * Unit test for functions wc_IdeaCbcEncrypt and wc_IdeaCbcDecrypt
  10189. */
  10190. static int test_wc_IdeaCbcEncyptDecrypt (void)
  10191. {
  10192. int ret = 0;
  10193. #ifdef HAVE_IDEA
  10194. Idea idea;
  10195. const byte key[] =
  10196. {
  10197. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  10198. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  10199. };
  10200. const char* message = "International Data Encryption Algorithm";
  10201. byte msg_enc[40];
  10202. byte msg_dec[40];
  10203. printf(testingFmt, "wc_IdeaCbcEncrypt()");
  10204. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_ENCRYPTION);
  10205. if (ret == 0) {
  10206. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  10207. (word32)XSTRLEN(message) + 1);
  10208. }
  10209. if (ret == 0) {
  10210. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_DECRYPTION);
  10211. }
  10212. if (ret == 0) {
  10213. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  10214. (word32)XSTRLEN(message) + 1);
  10215. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  10216. ret = WOLFSSL_FATAL_ERROR;
  10217. }
  10218. }
  10219. /* Test bad args. Enc */
  10220. if (ret == 0) {
  10221. ret = wc_IdeaCbcEncrypt(NULL, msg_enc, (byte*)message,
  10222. (word32)XSTRLEN(message) + 1);
  10223. if (ret == BAD_FUNC_ARG) {
  10224. ret = wc_IdeaCbcEncrypt(&idea, NULL, (byte*)message,
  10225. (word32)XSTRLEN(message) + 1);
  10226. }
  10227. if (ret == BAD_FUNC_ARG) {
  10228. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, NULL,
  10229. (word32)XSTRLEN(message) + 1);
  10230. }
  10231. if (ret != BAD_FUNC_ARG) {
  10232. ret = WOLFSSL_FATAL_ERROR;
  10233. } else {
  10234. ret = 0;
  10235. }
  10236. } /* END test bad args ENC */
  10237. /* Test bad args DEC */
  10238. if (ret == 0) {
  10239. ret = wc_IdeaCbcDecrypt(NULL, msg_dec, msg_enc,
  10240. (word32)XSTRLEN(message) + 1);
  10241. if (ret == BAD_FUNC_ARG) {
  10242. ret = wc_IdeaCbcDecrypt(&idea, NULL, msg_enc,
  10243. (word32)XSTRLEN(message) + 1);
  10244. }
  10245. if (ret == BAD_FUNC_ARG) {
  10246. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, NULL,
  10247. (word32)XSTRLEN(message) + 1);
  10248. }
  10249. if (ret != BAD_FUNC_ARG) {
  10250. ret = WOLFSSL_FATAL_ERROR;
  10251. } else {
  10252. ret = 0;
  10253. }
  10254. }
  10255. printf(resultFmt, ret == 0 ? passed : failed);
  10256. #endif
  10257. return ret;
  10258. } /* END test_wc_IdeaCbcEncryptDecrypt */
  10259. /*
  10260. * Test function for wc_HmacSetKey
  10261. */
  10262. static int test_wc_Md5HmacSetKey (void)
  10263. {
  10264. int flag = 0;
  10265. #if !defined(NO_HMAC) && !defined(NO_MD5)
  10266. Hmac hmac;
  10267. int ret, times, itr;
  10268. const char* keys[]=
  10269. {
  10270. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  10271. #ifndef HAVE_FIPS
  10272. "Jefe", /* smaller than minimum FIPS key size */
  10273. #endif
  10274. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  10275. };
  10276. times = sizeof(keys) / sizeof(char*);
  10277. flag = 0;
  10278. printf(testingFmt, "wc_HmacSetKey() with MD5");
  10279. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10280. if (ret != 0)
  10281. return ret;
  10282. for (itr = 0; itr < times; itr++) {
  10283. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  10284. (word32)XSTRLEN(keys[itr]));
  10285. if (ret != 0) {
  10286. flag = ret;
  10287. }
  10288. }
  10289. /* Bad args. */
  10290. if (!flag) {
  10291. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  10292. (word32)XSTRLEN(keys[0]));
  10293. if (ret != BAD_FUNC_ARG) {
  10294. flag = WOLFSSL_FATAL_ERROR;
  10295. }
  10296. }
  10297. if (!flag) {
  10298. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  10299. if (ret != BAD_FUNC_ARG) {
  10300. flag = WOLFSSL_FATAL_ERROR;
  10301. }
  10302. }
  10303. if (!flag) {
  10304. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  10305. (word32)XSTRLEN(keys[0]));
  10306. if (ret != BAD_FUNC_ARG) {
  10307. flag = WOLFSSL_FATAL_ERROR;
  10308. }
  10309. }
  10310. if (!flag) {
  10311. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  10312. #ifdef HAVE_FIPS
  10313. if (ret != HMAC_MIN_KEYLEN_E) {
  10314. flag = WOLFSSL_FATAL_ERROR;
  10315. }
  10316. #else
  10317. if (ret != 0) {
  10318. flag = WOLFSSL_FATAL_ERROR;
  10319. }
  10320. #endif
  10321. }
  10322. wc_HmacFree(&hmac);
  10323. printf(resultFmt, flag == 0 ? passed : failed);
  10324. #endif
  10325. return flag;
  10326. } /* END test_wc_Md5HmacSetKey */
  10327. /*
  10328. * testing wc_HmacSetKey() on wc_Sha hash.
  10329. */
  10330. static int test_wc_ShaHmacSetKey (void)
  10331. {
  10332. int flag = 0;
  10333. #if !defined(NO_HMAC) && !defined(NO_SHA)
  10334. Hmac hmac;
  10335. int ret, times, itr;
  10336. const char* keys[]=
  10337. {
  10338. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  10339. "\x0b\x0b\x0b",
  10340. #ifndef HAVE_FIPS
  10341. "Jefe", /* smaller than minimum FIPS key size */
  10342. #endif
  10343. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  10344. "\xAA\xAA\xAA"
  10345. };
  10346. times = sizeof(keys) / sizeof(char*);
  10347. flag = 0;
  10348. printf(testingFmt, "wc_HmacSetKey() with SHA");
  10349. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10350. if (ret != 0)
  10351. return ret;
  10352. for (itr = 0; itr < times; itr++) {
  10353. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  10354. (word32)XSTRLEN(keys[itr]));
  10355. if (ret != 0) {
  10356. flag = ret;
  10357. }
  10358. }
  10359. /* Bad args. */
  10360. if (!flag) {
  10361. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  10362. (word32)XSTRLEN(keys[0]));
  10363. if (ret != BAD_FUNC_ARG) {
  10364. flag = WOLFSSL_FATAL_ERROR;
  10365. }
  10366. }
  10367. if (!flag) {
  10368. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  10369. if (ret != BAD_FUNC_ARG) {
  10370. flag = WOLFSSL_FATAL_ERROR;
  10371. }
  10372. }
  10373. if (!flag) {
  10374. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  10375. (word32)XSTRLEN(keys[0]));
  10376. if (ret != BAD_FUNC_ARG) {
  10377. flag = WOLFSSL_FATAL_ERROR;
  10378. }
  10379. }
  10380. if (!flag) {
  10381. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  10382. #ifdef HAVE_FIPS
  10383. if (ret != HMAC_MIN_KEYLEN_E) {
  10384. flag = WOLFSSL_FATAL_ERROR;
  10385. }
  10386. #else
  10387. if (ret != 0) {
  10388. flag = WOLFSSL_FATAL_ERROR;
  10389. }
  10390. #endif
  10391. }
  10392. wc_HmacFree(&hmac);
  10393. printf(resultFmt, flag == 0 ? passed : failed);
  10394. #endif
  10395. return flag;
  10396. } /* END test_wc_ShaHmacSetKey() */
  10397. /*
  10398. * testing wc_HmacSetKey() on Sha224 hash.
  10399. */
  10400. static int test_wc_Sha224HmacSetKey (void)
  10401. {
  10402. int flag = 0;
  10403. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  10404. Hmac hmac;
  10405. int ret, times, itr;
  10406. const char* keys[]=
  10407. {
  10408. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  10409. "\x0b\x0b\x0b",
  10410. #ifndef HAVE_FIPS
  10411. "Jefe", /* smaller than minimum FIPS key size */
  10412. #endif
  10413. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  10414. "\xAA\xAA\xAA"
  10415. };
  10416. times = sizeof(keys) / sizeof(char*);
  10417. flag = 0;
  10418. printf(testingFmt, "wc_HmacSetKey() with SHA 224");
  10419. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10420. if (ret != 0)
  10421. return ret;
  10422. for (itr = 0; itr < times; itr++) {
  10423. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  10424. (word32)XSTRLEN(keys[itr]));
  10425. if (ret != 0) {
  10426. flag = ret;
  10427. }
  10428. }
  10429. /* Bad args. */
  10430. if (!flag) {
  10431. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  10432. (word32)XSTRLEN(keys[0]));
  10433. if (ret != BAD_FUNC_ARG) {
  10434. flag = WOLFSSL_FATAL_ERROR;
  10435. }
  10436. }
  10437. if (!flag) {
  10438. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  10439. if (ret != BAD_FUNC_ARG) {
  10440. flag = WOLFSSL_FATAL_ERROR;
  10441. }
  10442. }
  10443. if (!flag) {
  10444. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  10445. (word32)XSTRLEN(keys[0]));
  10446. if (ret != BAD_FUNC_ARG) {
  10447. flag = WOLFSSL_FATAL_ERROR;
  10448. }
  10449. }
  10450. if (!flag) {
  10451. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  10452. #ifdef HAVE_FIPS
  10453. if (ret != HMAC_MIN_KEYLEN_E) {
  10454. flag = WOLFSSL_FATAL_ERROR;
  10455. }
  10456. #else
  10457. if (ret != 0) {
  10458. flag = WOLFSSL_FATAL_ERROR;
  10459. }
  10460. #endif
  10461. }
  10462. wc_HmacFree(&hmac);
  10463. printf(resultFmt, flag == 0 ? passed : failed);
  10464. #endif
  10465. return flag;
  10466. } /* END test_wc_Sha224HmacSetKey() */
  10467. /*
  10468. * testing wc_HmacSetKey() on Sha256 hash
  10469. */
  10470. static int test_wc_Sha256HmacSetKey (void)
  10471. {
  10472. int flag = 0;
  10473. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  10474. Hmac hmac;
  10475. int ret, times, itr;
  10476. const char* keys[]=
  10477. {
  10478. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  10479. "\x0b\x0b\x0b",
  10480. #ifndef HAVE_FIPS
  10481. "Jefe", /* smaller than minimum FIPS key size */
  10482. #endif
  10483. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  10484. "\xAA\xAA\xAA"
  10485. };
  10486. times = sizeof(keys) / sizeof(char*);
  10487. flag = 0;
  10488. printf(testingFmt, "wc_HmacSetKey() with SHA256");
  10489. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10490. if (ret != 0)
  10491. return ret;
  10492. for (itr = 0; itr < times; itr++) {
  10493. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  10494. (word32)XSTRLEN(keys[itr]));
  10495. if (ret != 0) {
  10496. flag = ret;
  10497. }
  10498. }
  10499. /* Bad args. */
  10500. if (!flag) {
  10501. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  10502. (word32)XSTRLEN(keys[0]));
  10503. if (ret != BAD_FUNC_ARG) {
  10504. flag = WOLFSSL_FATAL_ERROR;
  10505. }
  10506. }
  10507. if (!flag) {
  10508. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  10509. if (ret != BAD_FUNC_ARG) {
  10510. flag = WOLFSSL_FATAL_ERROR;
  10511. }
  10512. }
  10513. if (!flag) {
  10514. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  10515. (word32)XSTRLEN(keys[0]));
  10516. if (ret != BAD_FUNC_ARG) {
  10517. flag = WOLFSSL_FATAL_ERROR;
  10518. }
  10519. }
  10520. if (!flag) {
  10521. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  10522. #ifdef HAVE_FIPS
  10523. if (ret != HMAC_MIN_KEYLEN_E) {
  10524. flag = WOLFSSL_FATAL_ERROR;
  10525. }
  10526. #else
  10527. if (ret != 0) {
  10528. flag = WOLFSSL_FATAL_ERROR;
  10529. }
  10530. #endif
  10531. }
  10532. wc_HmacFree(&hmac);
  10533. printf(resultFmt, flag == 0 ? passed : failed);
  10534. #endif
  10535. return flag;
  10536. } /* END test_wc_Sha256HmacSetKey() */
  10537. /*
  10538. * testing wc_HmacSetKey on Sha384 hash.
  10539. */
  10540. static int test_wc_Sha384HmacSetKey (void)
  10541. {
  10542. int flag = 0;
  10543. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  10544. Hmac hmac;
  10545. int ret, times, itr;
  10546. const char* keys[]=
  10547. {
  10548. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  10549. "\x0b\x0b\x0b",
  10550. #ifndef HAVE_FIPS
  10551. "Jefe", /* smaller than minimum FIPS key size */
  10552. #endif
  10553. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  10554. "\xAA\xAA\xAA"
  10555. };
  10556. times = sizeof(keys) / sizeof(char*);
  10557. flag = 0;
  10558. printf(testingFmt, "wc_HmacSetKey() with SHA384");
  10559. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10560. if (ret != 0)
  10561. return ret;
  10562. for (itr = 0; itr < times; itr++) {
  10563. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  10564. (word32)XSTRLEN(keys[itr]));
  10565. if (ret != 0) {
  10566. flag = ret;
  10567. }
  10568. }
  10569. /* Bad args. */
  10570. if (!flag) {
  10571. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  10572. (word32)XSTRLEN(keys[0]));
  10573. if (ret != BAD_FUNC_ARG) {
  10574. flag = WOLFSSL_FATAL_ERROR;
  10575. }
  10576. }
  10577. if (!flag) {
  10578. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  10579. if (ret != BAD_FUNC_ARG) {
  10580. flag = WOLFSSL_FATAL_ERROR;
  10581. }
  10582. }
  10583. if (!flag) {
  10584. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  10585. (word32)XSTRLEN(keys[0]));
  10586. if (ret != BAD_FUNC_ARG) {
  10587. flag = WOLFSSL_FATAL_ERROR;
  10588. }
  10589. }
  10590. if (!flag) {
  10591. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  10592. #ifdef HAVE_FIPS
  10593. if (ret != HMAC_MIN_KEYLEN_E) {
  10594. flag = WOLFSSL_FATAL_ERROR;
  10595. }
  10596. #else
  10597. if (ret != 0) {
  10598. flag = WOLFSSL_FATAL_ERROR;
  10599. }
  10600. #endif
  10601. }
  10602. wc_HmacFree(&hmac);
  10603. printf(resultFmt, flag == 0 ? passed : failed);
  10604. #endif
  10605. return flag;
  10606. } /* END test_wc_Sha384HmacSetKey() */
  10607. /*
  10608. * testing wc_HmacUpdate on wc_Md5 hash.
  10609. */
  10610. static int test_wc_Md5HmacUpdate (void)
  10611. {
  10612. int flag = 0;
  10613. #if !defined(NO_HMAC) && !defined(NO_MD5)
  10614. Hmac hmac;
  10615. testVector a, b;
  10616. int ret;
  10617. #ifdef HAVE_FIPS
  10618. const char* keys =
  10619. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  10620. #else
  10621. const char* keys = "Jefe";
  10622. #endif
  10623. a.input = "what do ya want for nothing?";
  10624. a.inLen = XSTRLEN(a.input);
  10625. b.input = "Hi There";
  10626. b.inLen = XSTRLEN(b.input);
  10627. flag = 0;
  10628. printf(testingFmt, "wc_HmacUpdate() with MD5");
  10629. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10630. if (ret != 0)
  10631. return ret;
  10632. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  10633. if (ret != 0) {
  10634. flag = ret;
  10635. }
  10636. if (!flag) {
  10637. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  10638. if (ret != 0) {
  10639. flag = ret;
  10640. }
  10641. }
  10642. /* Update Hmac. */
  10643. if (!flag) {
  10644. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  10645. if (ret != 0) {
  10646. flag = ret;
  10647. }
  10648. }
  10649. /* Test bad args. */
  10650. if (!flag) {
  10651. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10652. if (ret != BAD_FUNC_ARG) {
  10653. flag = WOLFSSL_FATAL_ERROR;
  10654. }
  10655. }
  10656. if (!flag) {
  10657. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  10658. if (ret != BAD_FUNC_ARG) {
  10659. flag = WOLFSSL_FATAL_ERROR;
  10660. }
  10661. }
  10662. if (!flag) {
  10663. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  10664. if (ret != 0) {
  10665. flag = ret;
  10666. }
  10667. }
  10668. wc_HmacFree(&hmac);
  10669. printf(resultFmt, flag == 0 ? passed : failed);
  10670. #endif
  10671. return flag;
  10672. } /* END test_wc_Md5HmacUpdate */
  10673. /*
  10674. * testing wc_HmacUpdate on SHA hash.
  10675. */
  10676. static int test_wc_ShaHmacUpdate (void)
  10677. {
  10678. int flag = 0;
  10679. #if !defined(NO_HMAC) && !defined(NO_SHA)
  10680. Hmac hmac;
  10681. testVector a, b;
  10682. int ret;
  10683. #ifdef HAVE_FIPS
  10684. const char* keys =
  10685. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  10686. #else
  10687. const char* keys = "Jefe";
  10688. #endif
  10689. a.input = "what do ya want for nothing?";
  10690. a.inLen = XSTRLEN(a.input);
  10691. b.input = "Hi There";
  10692. b.inLen = XSTRLEN(b.input);
  10693. flag = 0;
  10694. printf(testingFmt, "wc_HmacUpdate() with SHA");
  10695. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10696. if (ret != 0)
  10697. return ret;
  10698. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  10699. if (ret != 0) {
  10700. flag = ret;
  10701. }
  10702. if (!flag) {
  10703. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  10704. if (ret != 0) {
  10705. flag = ret;
  10706. }
  10707. }
  10708. /* Update Hmac. */
  10709. if (!flag) {
  10710. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  10711. if (ret != 0) {
  10712. flag = ret;
  10713. }
  10714. }
  10715. /* Test bad args. */
  10716. if (!flag) {
  10717. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10718. if (ret != BAD_FUNC_ARG) {
  10719. flag = WOLFSSL_FATAL_ERROR;
  10720. }
  10721. }
  10722. if (!flag) {
  10723. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  10724. if (ret != BAD_FUNC_ARG) {
  10725. flag = WOLFSSL_FATAL_ERROR;
  10726. }
  10727. }
  10728. if (!flag) {
  10729. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  10730. if (ret != 0) {
  10731. flag = ret;
  10732. }
  10733. }
  10734. wc_HmacFree(&hmac);
  10735. printf(resultFmt, flag == 0 ? passed : failed);
  10736. #endif
  10737. return flag;
  10738. } /* END test_wc_ShaHmacUpdate */
  10739. /*
  10740. * testing wc_HmacUpdate on SHA224 hash.
  10741. */
  10742. static int test_wc_Sha224HmacUpdate (void)
  10743. {
  10744. int flag = 0;
  10745. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  10746. Hmac hmac;
  10747. testVector a, b;
  10748. int ret;
  10749. #ifdef HAVE_FIPS
  10750. const char* keys =
  10751. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  10752. #else
  10753. const char* keys = "Jefe";
  10754. #endif
  10755. a.input = "what do ya want for nothing?";
  10756. a.inLen = XSTRLEN(a.input);
  10757. b.input = "Hi There";
  10758. b.inLen = XSTRLEN(b.input);
  10759. flag = 0;
  10760. printf(testingFmt, "wc_HmacUpdate() with SHA224");
  10761. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10762. if (ret != 0)
  10763. return ret;
  10764. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  10765. if (ret != 0) {
  10766. flag = ret;
  10767. }
  10768. if (!flag) {
  10769. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  10770. if (ret != 0) {
  10771. flag = ret;
  10772. }
  10773. }
  10774. /* Update Hmac. */
  10775. if (!flag) {
  10776. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  10777. if (ret != 0) {
  10778. flag = ret;
  10779. }
  10780. }
  10781. /* Test bad args. */
  10782. if (!flag) {
  10783. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10784. if (ret != BAD_FUNC_ARG) {
  10785. flag = WOLFSSL_FATAL_ERROR;
  10786. }
  10787. }
  10788. if (!flag) {
  10789. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  10790. if (ret != BAD_FUNC_ARG) {
  10791. flag = WOLFSSL_FATAL_ERROR;
  10792. }
  10793. }
  10794. if (!flag) {
  10795. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  10796. if (ret != 0) {
  10797. flag = ret;
  10798. }
  10799. }
  10800. wc_HmacFree(&hmac);
  10801. printf(resultFmt, flag == 0 ? passed : failed);
  10802. #endif
  10803. return flag;
  10804. } /* END test_wc_Sha224HmacUpdate */
  10805. /*
  10806. * testing wc_HmacUpdate on SHA256 hash.
  10807. */
  10808. static int test_wc_Sha256HmacUpdate (void)
  10809. {
  10810. int flag = 0;
  10811. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  10812. Hmac hmac;
  10813. testVector a, b;
  10814. int ret;
  10815. #ifdef HAVE_FIPS
  10816. const char* keys =
  10817. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  10818. #else
  10819. const char* keys = "Jefe";
  10820. #endif
  10821. a.input = "what do ya want for nothing?";
  10822. a.inLen = XSTRLEN(a.input);
  10823. b.input = "Hi There";
  10824. b.inLen = XSTRLEN(b.input);
  10825. flag = 0;
  10826. printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
  10827. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10828. if (ret != 0)
  10829. return ret;
  10830. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  10831. if (ret != 0) {
  10832. flag = ret;
  10833. }
  10834. if (!flag) {
  10835. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  10836. if (ret != 0) {
  10837. flag = ret;
  10838. }
  10839. }
  10840. /* Update Hmac. */
  10841. if (!flag) {
  10842. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  10843. if (ret != 0) {
  10844. flag = ret;
  10845. }
  10846. }
  10847. /* Test bad args. */
  10848. if (!flag) {
  10849. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10850. if (ret != BAD_FUNC_ARG) {
  10851. flag = WOLFSSL_FATAL_ERROR;
  10852. }
  10853. }
  10854. if (!flag) {
  10855. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  10856. if (ret != BAD_FUNC_ARG) {
  10857. flag = WOLFSSL_FATAL_ERROR;
  10858. }
  10859. }
  10860. if (!flag) {
  10861. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  10862. if (ret != 0) {
  10863. flag = ret;
  10864. }
  10865. }
  10866. wc_HmacFree(&hmac);
  10867. printf(resultFmt, flag == 0 ? passed : failed);
  10868. #endif
  10869. return flag;
  10870. } /* END test_wc_Sha256HmacUpdate */
  10871. /*
  10872. * testing wc_HmacUpdate on SHA384 hash.
  10873. */
  10874. static int test_wc_Sha384HmacUpdate (void)
  10875. {
  10876. int flag = 0;
  10877. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  10878. Hmac hmac;
  10879. testVector a, b;
  10880. int ret;
  10881. #ifdef HAVE_FIPS
  10882. const char* keys =
  10883. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  10884. #else
  10885. const char* keys = "Jefe";
  10886. #endif
  10887. a.input = "what do ya want for nothing?";
  10888. a.inLen = XSTRLEN(a.input);
  10889. b.input = "Hi There";
  10890. b.inLen = XSTRLEN(b.input);
  10891. flag = 0;
  10892. printf(testingFmt, "wc_HmacUpdate() with SHA384");
  10893. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10894. if (ret != 0)
  10895. return ret;
  10896. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  10897. if (ret != 0) {
  10898. flag = ret;
  10899. }
  10900. if (!flag) {
  10901. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  10902. if (ret != 0) {
  10903. flag = ret;
  10904. }
  10905. }
  10906. /* Update Hmac. */
  10907. if (!flag) {
  10908. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  10909. if (ret != 0) {
  10910. flag = ret;
  10911. }
  10912. }
  10913. /* Test bad args. */
  10914. if (!flag) {
  10915. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10916. if (ret != BAD_FUNC_ARG) {
  10917. flag = WOLFSSL_FATAL_ERROR;
  10918. }
  10919. }
  10920. if (!flag) {
  10921. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  10922. if (ret != BAD_FUNC_ARG) {
  10923. flag = WOLFSSL_FATAL_ERROR;
  10924. }
  10925. }
  10926. if (!flag) {
  10927. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  10928. if (ret != 0) {
  10929. flag = ret;
  10930. }
  10931. }
  10932. wc_HmacFree(&hmac);
  10933. printf(resultFmt, flag == 0 ? passed : failed);
  10934. #endif
  10935. return flag;
  10936. } /* END test_wc_Sha384HmacUpdate */
  10937. /*
  10938. * Testing wc_HmacFinal() with MD5
  10939. */
  10940. static int test_wc_Md5HmacFinal (void)
  10941. {
  10942. int flag = 0;
  10943. #if !defined(NO_HMAC) && !defined(NO_MD5)
  10944. Hmac hmac;
  10945. byte hash[WC_MD5_DIGEST_SIZE];
  10946. testVector a;
  10947. int ret;
  10948. const char* key;
  10949. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  10950. a.input = "Hi There";
  10951. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  10952. "\x9d";
  10953. a.inLen = XSTRLEN(a.input);
  10954. a.outLen = XSTRLEN(a.output);
  10955. flag = 0;
  10956. printf(testingFmt, "wc_HmacFinal() with MD5");
  10957. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  10958. if (ret != 0)
  10959. return ret;
  10960. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  10961. if (ret != 0) {
  10962. flag = ret;
  10963. }
  10964. if (!flag) {
  10965. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  10966. if (ret != 0) {
  10967. flag = ret;
  10968. }
  10969. }
  10970. if (!flag) {
  10971. ret = wc_HmacFinal(&hmac, hash);
  10972. if (ret != 0) {
  10973. flag = ret;
  10974. }
  10975. }
  10976. if (!flag) {
  10977. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  10978. flag = WOLFSSL_FATAL_ERROR;
  10979. }
  10980. }
  10981. /* Try bad parameters. */
  10982. if (!flag) {
  10983. ret = wc_HmacFinal(NULL, hash);
  10984. if (ret != BAD_FUNC_ARG) {
  10985. flag = WOLFSSL_FATAL_ERROR;
  10986. }
  10987. }
  10988. #ifndef HAVE_FIPS
  10989. if (!flag) {
  10990. ret = wc_HmacFinal(&hmac, NULL);
  10991. if (ret != BAD_FUNC_ARG) {
  10992. flag = WOLFSSL_FATAL_ERROR;
  10993. }
  10994. }
  10995. #endif
  10996. wc_HmacFree(&hmac);
  10997. printf(resultFmt, flag == 0 ? passed : failed);
  10998. #endif
  10999. return flag;
  11000. } /* END test_wc_Md5HmacFinal */
  11001. /*
  11002. * Testing wc_HmacFinal() with SHA
  11003. */
  11004. static int test_wc_ShaHmacFinal (void)
  11005. {
  11006. int flag = 0;
  11007. #if !defined(NO_HMAC) && !defined(NO_SHA)
  11008. Hmac hmac;
  11009. byte hash[WC_SHA_DIGEST_SIZE];
  11010. testVector a;
  11011. int ret;
  11012. const char* key;
  11013. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11014. "\x0b\x0b\x0b";
  11015. a.input = "Hi There";
  11016. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  11017. "\x8e\xf1\x46\xbe\x00";
  11018. a.inLen = XSTRLEN(a.input);
  11019. a.outLen = XSTRLEN(a.output);
  11020. flag = 0;
  11021. printf(testingFmt, "wc_HmacFinal() with SHA");
  11022. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11023. if (ret != 0)
  11024. return ret;
  11025. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  11026. if (ret != 0) {
  11027. flag = ret;
  11028. }
  11029. if (!flag) {
  11030. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  11031. if (ret != 0) {
  11032. flag = ret;
  11033. }
  11034. }
  11035. if (!flag) {
  11036. ret = wc_HmacFinal(&hmac, hash);
  11037. if (ret != 0) {
  11038. flag = ret;
  11039. }
  11040. }
  11041. if (!flag) {
  11042. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  11043. flag = WOLFSSL_FATAL_ERROR;
  11044. }
  11045. }
  11046. /* Try bad parameters. */
  11047. if (!flag) {
  11048. ret = wc_HmacFinal(NULL, hash);
  11049. if (ret != BAD_FUNC_ARG) {
  11050. flag = WOLFSSL_FATAL_ERROR;
  11051. }
  11052. }
  11053. #ifndef HAVE_FIPS
  11054. if (!flag) {
  11055. ret = wc_HmacFinal(&hmac, NULL);
  11056. if (ret != BAD_FUNC_ARG) {
  11057. flag = WOLFSSL_FATAL_ERROR;
  11058. }
  11059. }
  11060. #endif
  11061. wc_HmacFree(&hmac);
  11062. printf(resultFmt, flag == 0 ? passed : failed);
  11063. #endif
  11064. return flag;
  11065. } /* END test_wc_ShaHmacFinal */
  11066. /*
  11067. * Testing wc_HmacFinal() with SHA224
  11068. */
  11069. static int test_wc_Sha224HmacFinal (void)
  11070. {
  11071. int flag = 0;
  11072. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  11073. Hmac hmac;
  11074. byte hash[WC_SHA224_DIGEST_SIZE];
  11075. testVector a;
  11076. int ret;
  11077. const char* key;
  11078. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11079. "\x0b\x0b\x0b";
  11080. a.input = "Hi There";
  11081. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  11082. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  11083. a.inLen = XSTRLEN(a.input);
  11084. a.outLen = XSTRLEN(a.output);
  11085. flag = 0;
  11086. printf(testingFmt, "wc_HmacFinal() with SHA224");
  11087. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11088. if (ret != 0)
  11089. return ret;
  11090. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  11091. if (ret != 0) {
  11092. flag = ret;
  11093. }
  11094. if (!flag) {
  11095. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  11096. if (ret != 0) {
  11097. flag = ret;
  11098. }
  11099. }
  11100. if (!flag) {
  11101. ret = wc_HmacFinal(&hmac, hash);
  11102. if (ret != 0) {
  11103. flag = ret;
  11104. }
  11105. }
  11106. if (!flag) {
  11107. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  11108. flag = WOLFSSL_FATAL_ERROR;
  11109. }
  11110. }
  11111. /* Try bad parameters. */
  11112. if (!flag) {
  11113. ret = wc_HmacFinal(NULL, hash);
  11114. if (ret != BAD_FUNC_ARG) {
  11115. flag = WOLFSSL_FATAL_ERROR;
  11116. }
  11117. }
  11118. #ifndef HAVE_FIPS
  11119. if (!flag) {
  11120. ret = wc_HmacFinal(&hmac, NULL);
  11121. if (ret != BAD_FUNC_ARG) {
  11122. flag = WOLFSSL_FATAL_ERROR;
  11123. }
  11124. }
  11125. #endif
  11126. wc_HmacFree(&hmac);
  11127. printf(resultFmt, flag == 0 ? passed : failed);
  11128. #endif
  11129. return flag;
  11130. } /* END test_wc_Sha224HmacFinal */
  11131. /*
  11132. * Testing wc_HmacFinal() with SHA256
  11133. */
  11134. static int test_wc_Sha256HmacFinal (void)
  11135. {
  11136. int flag = 0;
  11137. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  11138. Hmac hmac;
  11139. byte hash[WC_SHA256_DIGEST_SIZE];
  11140. testVector a;
  11141. int ret;
  11142. const char* key;
  11143. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11144. "\x0b\x0b\x0b";
  11145. a.input = "Hi There";
  11146. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  11147. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  11148. "\xcf\xf7";
  11149. a.inLen = XSTRLEN(a.input);
  11150. a.outLen = XSTRLEN(a.output);
  11151. flag = 0;
  11152. printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
  11153. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11154. if (ret != 0)
  11155. return ret;
  11156. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  11157. if (ret != 0) {
  11158. flag = ret;
  11159. }
  11160. if (!flag) {
  11161. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  11162. if (ret != 0) {
  11163. flag = ret;
  11164. }
  11165. }
  11166. if (!flag) {
  11167. ret = wc_HmacFinal(&hmac, hash);
  11168. if (ret != 0) {
  11169. flag = ret;
  11170. }
  11171. }
  11172. if (!flag) {
  11173. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  11174. flag = WOLFSSL_FATAL_ERROR;
  11175. }
  11176. }
  11177. /* Try bad parameters. */
  11178. if (!flag) {
  11179. ret = wc_HmacFinal(NULL, hash);
  11180. if (ret != BAD_FUNC_ARG) {
  11181. flag = WOLFSSL_FATAL_ERROR;
  11182. }
  11183. }
  11184. #ifndef HAVE_FIPS
  11185. if (!flag) {
  11186. ret = wc_HmacFinal(&hmac, NULL);
  11187. if (ret != BAD_FUNC_ARG) {
  11188. flag = WOLFSSL_FATAL_ERROR;
  11189. }
  11190. }
  11191. #endif
  11192. wc_HmacFree(&hmac);
  11193. printf(resultFmt, flag == 0 ? passed : failed);
  11194. #endif
  11195. return flag;
  11196. } /* END test_wc_Sha256HmacFinal */
  11197. /*
  11198. * Testing wc_HmacFinal() with SHA384
  11199. */
  11200. static int test_wc_Sha384HmacFinal (void)
  11201. {
  11202. int flag = 0;
  11203. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  11204. Hmac hmac;
  11205. byte hash[WC_SHA384_DIGEST_SIZE];
  11206. testVector a;
  11207. int ret;
  11208. const char* key;
  11209. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11210. "\x0b\x0b\x0b";
  11211. a.input = "Hi There";
  11212. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  11213. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  11214. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  11215. "\xfa\x9c\xb6";
  11216. a.inLen = XSTRLEN(a.input);
  11217. a.outLen = XSTRLEN(a.output);
  11218. flag = 0;
  11219. printf(testingFmt, "wc_HmacFinal() with SHA384");
  11220. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11221. if (ret != 0)
  11222. return ret;
  11223. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  11224. if (ret != 0) {
  11225. flag = ret;
  11226. }
  11227. if (!flag) {
  11228. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  11229. if (ret != 0) {
  11230. flag = ret;
  11231. }
  11232. }
  11233. if (!flag) {
  11234. ret = wc_HmacFinal(&hmac, hash);
  11235. if (ret != 0) {
  11236. flag = ret;
  11237. }
  11238. }
  11239. if (!flag) {
  11240. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  11241. flag = WOLFSSL_FATAL_ERROR;
  11242. }
  11243. }
  11244. /* Try bad parameters. */
  11245. if (!flag) {
  11246. ret = wc_HmacFinal(NULL, hash);
  11247. if (ret != BAD_FUNC_ARG) {
  11248. flag = WOLFSSL_FATAL_ERROR;
  11249. }
  11250. }
  11251. #ifndef HAVE_FIPS
  11252. if (!flag) {
  11253. ret = wc_HmacFinal(&hmac, NULL);
  11254. if (ret != BAD_FUNC_ARG) {
  11255. flag = WOLFSSL_FATAL_ERROR;
  11256. }
  11257. }
  11258. #endif
  11259. wc_HmacFree(&hmac);
  11260. printf(resultFmt, flag == 0 ? passed : failed);
  11261. #endif
  11262. return flag;
  11263. } /* END test_wc_Sha384HmacFinal */
  11264. /*
  11265. * Testing wc_InitCmac()
  11266. */
  11267. static int test_wc_InitCmac (void)
  11268. {
  11269. int ret = 0;
  11270. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  11271. Cmac cmac1, cmac2, cmac3;
  11272. /* AES 128 key. */
  11273. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  11274. "\x09\x10\x11\x12\x13\x14\x15\x16";
  11275. /* AES 192 key. */
  11276. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  11277. "\x09\x01\x11\x12\x13\x14\x15\x16"
  11278. "\x01\x02\x03\x04\x05\x06\x07\x08";
  11279. /* AES 256 key. */
  11280. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  11281. "\x09\x01\x11\x12\x13\x14\x15\x16"
  11282. "\x01\x02\x03\x04\x05\x06\x07\x08"
  11283. "\x09\x01\x11\x12\x13\x14\x15\x16";
  11284. word32 key1Sz = (word32)sizeof(key1) - 1;
  11285. word32 key2Sz = (word32)sizeof(key2) - 1;
  11286. word32 key3Sz = (word32)sizeof(key3) - 1;
  11287. int type = WC_CMAC_AES;
  11288. printf(testingFmt, "wc_InitCmac()");
  11289. #ifdef WOLFSSL_AES_128
  11290. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  11291. #endif
  11292. #ifdef WOLFSSL_AES_192
  11293. if (ret == 0)
  11294. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  11295. #endif
  11296. #ifdef WOLFSSL_AES_256
  11297. if (ret == 0)
  11298. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  11299. #endif
  11300. /* Test bad args. */
  11301. if (ret == 0) {
  11302. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  11303. if (ret == BAD_FUNC_ARG) {
  11304. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  11305. }
  11306. if (ret == BAD_FUNC_ARG) {
  11307. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  11308. }
  11309. if (ret == BAD_FUNC_ARG) {
  11310. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  11311. }
  11312. if (ret == BAD_FUNC_ARG) {
  11313. ret = 0;
  11314. } else {
  11315. ret = WOLFSSL_FATAL_ERROR;
  11316. }
  11317. }
  11318. (void)key1;
  11319. (void)key1Sz;
  11320. (void)key2;
  11321. (void)key2Sz;
  11322. (void)cmac1;
  11323. (void)cmac2;
  11324. printf(resultFmt, ret == 0 ? passed : failed);
  11325. #endif
  11326. return ret;
  11327. } /* END test_wc_InitCmac */
  11328. /*
  11329. * Testing wc_CmacUpdate()
  11330. */
  11331. static int test_wc_CmacUpdate (void)
  11332. {
  11333. int ret = 0;
  11334. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  11335. Cmac cmac;
  11336. byte key[] =
  11337. {
  11338. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  11339. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  11340. };
  11341. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  11342. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  11343. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  11344. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  11345. "\xf4";
  11346. word32 inSz = (word32)sizeof(in) - 1;
  11347. word32 keySz = (word32)sizeof(key);
  11348. int type = WC_CMAC_AES;
  11349. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  11350. if (ret != 0) {
  11351. return ret;
  11352. }
  11353. printf(testingFmt, "wc_CmacUpdate()");
  11354. ret = wc_CmacUpdate(&cmac, in, inSz);
  11355. /* Test bad args. */
  11356. if (ret == 0) {
  11357. ret = wc_CmacUpdate(NULL, in, inSz);
  11358. if (ret == BAD_FUNC_ARG) {
  11359. ret = wc_CmacUpdate(&cmac, NULL, 30);
  11360. }
  11361. if (ret == BAD_FUNC_ARG) {
  11362. ret = 0;
  11363. } else if (ret == 0) {
  11364. ret = WOLFSSL_FATAL_ERROR;
  11365. }
  11366. }
  11367. printf(resultFmt, ret == 0 ? passed : failed);
  11368. #endif
  11369. return ret;
  11370. } /* END test_wc_CmacUpdate */
  11371. /*
  11372. * Testing wc_CmacFinal()
  11373. */
  11374. static int test_wc_CmacFinal (void)
  11375. {
  11376. int ret = 0;
  11377. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  11378. Cmac cmac;
  11379. byte key[] =
  11380. {
  11381. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  11382. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  11383. };
  11384. byte msg[] =
  11385. {
  11386. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  11387. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  11388. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  11389. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  11390. 0xf4
  11391. };
  11392. /* Test vectors from CMACGenAES128.rsp from
  11393. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  11394. * Per RFC4493 truncation of lsb is possible.
  11395. */
  11396. byte expMac[] =
  11397. {
  11398. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  11399. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  11400. };
  11401. byte mac[AES_BLOCK_SIZE];
  11402. word32 msgSz = (word32)sizeof(msg);
  11403. word32 keySz = (word32)sizeof(key);
  11404. word32 macSz = sizeof(mac);
  11405. word32 badMacSz = 17;
  11406. int expMacSz = sizeof(expMac);
  11407. int type = WC_CMAC_AES;
  11408. XMEMSET(mac, 0, macSz);
  11409. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  11410. if (ret != 0) {
  11411. return ret;
  11412. }
  11413. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  11414. printf(testingFmt, "wc_CmacFinal()");
  11415. if (ret == 0) {
  11416. ret = wc_CmacFinal(&cmac, mac, &macSz);
  11417. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  11418. ret = WOLFSSL_FATAL_ERROR;
  11419. }
  11420. /* Pass in bad args. */
  11421. if (ret == 0) {
  11422. ret = wc_CmacFinal(NULL, mac, &macSz);
  11423. if (ret == BAD_FUNC_ARG) {
  11424. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  11425. }
  11426. if (ret == BAD_FUNC_ARG) {
  11427. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  11428. if (ret == BUFFER_E) {
  11429. ret = 0;
  11430. }
  11431. } else if (ret == 0) {
  11432. ret = WOLFSSL_FATAL_ERROR;
  11433. }
  11434. }
  11435. }
  11436. printf(resultFmt, ret == 0 ? passed : failed);
  11437. #endif
  11438. return ret;
  11439. } /* END test_wc_CmacFinal */
  11440. /*
  11441. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  11442. */
  11443. static int test_wc_AesCmacGenerate (void)
  11444. {
  11445. int ret = 0;
  11446. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  11447. Cmac cmac;
  11448. byte key[] =
  11449. {
  11450. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  11451. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  11452. };
  11453. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  11454. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  11455. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  11456. "\x3d\x32\x65\x4c\x66\x23\xc5";
  11457. byte mac[AES_BLOCK_SIZE];
  11458. word32 keySz = sizeof(key);
  11459. word32 macSz = sizeof(mac);
  11460. word32 msgSz = sizeof(msg) - 1;
  11461. word32 expMacSz = sizeof(expMac) - 1;
  11462. int type = WC_CMAC_AES;
  11463. XMEMSET(mac, 0, macSz);
  11464. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  11465. if (ret != 0) {
  11466. return ret;
  11467. }
  11468. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  11469. if (ret != 0) {
  11470. return ret;
  11471. }
  11472. printf(testingFmt, "wc_AesCmacGenerate()");
  11473. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  11474. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  11475. ret = WOLFSSL_FATAL_ERROR;
  11476. }
  11477. /* Pass in bad args. */
  11478. if (ret == 0) {
  11479. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  11480. if (ret == BAD_FUNC_ARG) {
  11481. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  11482. }
  11483. if (ret == BAD_FUNC_ARG) {
  11484. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  11485. }
  11486. if (ret == BAD_FUNC_ARG) {
  11487. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  11488. }
  11489. if (ret == BAD_FUNC_ARG) {
  11490. ret = 0;
  11491. } else if (ret == 0) {
  11492. ret = WOLFSSL_FATAL_ERROR;
  11493. }
  11494. }
  11495. printf(resultFmt, ret == 0 ? passed : failed);
  11496. if (ret == 0) {
  11497. printf(testingFmt, "wc_AesCmacVerify()");
  11498. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  11499. /* Test bad args. */
  11500. if (ret == 0) {
  11501. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  11502. if (ret == BAD_FUNC_ARG) {
  11503. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  11504. }
  11505. if (ret == BAD_FUNC_ARG) {
  11506. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  11507. }
  11508. if (ret == BAD_FUNC_ARG) {
  11509. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  11510. }
  11511. if (ret == BAD_FUNC_ARG) {
  11512. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  11513. }
  11514. if (ret == BAD_FUNC_ARG) {
  11515. ret = 0;
  11516. } else if (ret == 0) {
  11517. ret = WOLFSSL_FATAL_ERROR;
  11518. }
  11519. }
  11520. printf(resultFmt, ret == 0 ? passed : failed);
  11521. }
  11522. #endif
  11523. return ret;
  11524. } /* END test_wc_AesCmacGenerate */
  11525. /*
  11526. * Testing streaming AES-GCM API.
  11527. */
  11528. static int test_wc_AesGcmStream (void)
  11529. {
  11530. int ret = 0;
  11531. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  11532. defined(WOLFSSL_AESGCM_STREAM)
  11533. int i;
  11534. WC_RNG rng[1];
  11535. Aes aesEnc[1];
  11536. Aes aesDec[1];
  11537. byte tag[AES_BLOCK_SIZE];
  11538. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  11539. byte out[AES_BLOCK_SIZE * 3 + 2];
  11540. byte plain[AES_BLOCK_SIZE * 3 + 2];
  11541. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  11542. byte key[AES_128_KEY_SIZE] = { 0, };
  11543. byte iv[AES_IV_SIZE] = { 1, };
  11544. byte ivOut[AES_IV_SIZE];
  11545. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  11546. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  11547. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  11548. };
  11549. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  11550. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  11551. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  11552. };
  11553. static const byte expTag[AES_BLOCK_SIZE] = {
  11554. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  11555. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  11556. };
  11557. /* Create a random for generating IV/nonce. */
  11558. AssertIntEQ(wc_InitRng(rng), 0);
  11559. /* Initialize data structures. */
  11560. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  11561. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  11562. /* BadParameters to streaming init. */
  11563. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  11564. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  11565. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  11566. BAD_FUNC_ARG);
  11567. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  11568. BAD_FUNC_ARG);
  11569. /* Bad parameters to encrypt update. */
  11570. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  11571. BAD_FUNC_ARG);
  11572. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  11573. BAD_FUNC_ARG);
  11574. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  11575. BAD_FUNC_ARG);
  11576. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  11577. BAD_FUNC_ARG);
  11578. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  11579. BAD_FUNC_ARG);
  11580. /* Bad parameters to decrypt update. */
  11581. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  11582. BAD_FUNC_ARG);
  11583. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  11584. BAD_FUNC_ARG);
  11585. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  11586. BAD_FUNC_ARG);
  11587. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  11588. BAD_FUNC_ARG);
  11589. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  11590. BAD_FUNC_ARG);
  11591. /* Bad parameters to encrypt final. */
  11592. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  11593. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  11594. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  11595. BAD_FUNC_ARG);
  11596. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  11597. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  11598. BAD_FUNC_ARG);
  11599. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  11600. BAD_FUNC_ARG);
  11601. /* Bad parameters to decrypt final. */
  11602. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  11603. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  11604. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  11605. BAD_FUNC_ARG);
  11606. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  11607. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  11608. BAD_FUNC_ARG);
  11609. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  11610. BAD_FUNC_ARG);
  11611. /* Check calling final before setting key fails. */
  11612. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  11613. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  11614. /* Check calling update before setting key else fails. */
  11615. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  11616. MISSING_KEY);
  11617. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  11618. MISSING_KEY);
  11619. /* Set key but not IV. */
  11620. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  11621. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  11622. /* Check calling final before setting IV fails. */
  11623. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  11624. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  11625. /* Check calling update before setting IV else fails. */
  11626. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  11627. MISSING_IV);
  11628. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  11629. MISSING_IV);
  11630. /* Set IV using fixed part IV and external IV APIs. */
  11631. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  11632. rng), 0);
  11633. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  11634. GCM_NONCE_MID_SZ), 0);
  11635. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  11636. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  11637. /* Encrypt and decrypt data. */
  11638. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  11639. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  11640. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  11641. /* Finalize and check tag matches. */
  11642. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  11643. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  11644. /* Set key and IV through streaming init API. */
  11645. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  11646. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  11647. /* Encrypt/decrypt one block and AAD of one block. */
  11648. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  11649. AES_BLOCK_SIZE), 0);
  11650. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  11651. AES_BLOCK_SIZE), 0);
  11652. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  11653. /* Finalize and check tag matches. */
  11654. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  11655. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  11656. /* Set key and IV through streaming init API. */
  11657. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  11658. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  11659. /* No data to encrypt/decrypt one byte of AAD. */
  11660. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  11661. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  11662. /* Finalize and check tag matches. */
  11663. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  11664. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  11665. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  11666. /* Set key and IV through streaming init API. */
  11667. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  11668. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  11669. /* Encrypt/decrypt one byte and no AAD. */
  11670. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  11671. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  11672. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  11673. /* Finalize and check tag matches. */
  11674. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  11675. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  11676. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  11677. /* Set key and IV through streaming init API. */
  11678. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  11679. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  11680. /* Encryption AES is one byte at a time */
  11681. for (i = 0; i < (int)sizeof(aad); i++) {
  11682. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  11683. 0);
  11684. }
  11685. for (i = 0; i < (int)sizeof(in); i++) {
  11686. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  11687. 0);
  11688. }
  11689. /* Decryption AES is two bytes at a time */
  11690. for (i = 0; i < (int)sizeof(aad); i += 2) {
  11691. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  11692. 0);
  11693. }
  11694. for (i = 0; i < (int)sizeof(aad); i += 2) {
  11695. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  11696. 0), 0);
  11697. }
  11698. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  11699. /* Finalize and check tag matches. */
  11700. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  11701. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  11702. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  11703. /* Check streaming encryption can be decrypted with one shot. */
  11704. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  11705. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  11706. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  11707. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  11708. wc_AesFree(aesEnc);
  11709. wc_AesFree(aesDec);
  11710. wc_FreeRng(rng);
  11711. #endif
  11712. return ret;
  11713. } /* END test_wc_AesGcmStream */
  11714. /*
  11715. * unit test for wc_Des3_SetIV()
  11716. */
  11717. static int test_wc_Des3_SetIV (void)
  11718. {
  11719. int ret = 0;
  11720. #ifndef NO_DES3
  11721. Des3 des;
  11722. const byte key[] =
  11723. {
  11724. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  11725. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  11726. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  11727. };
  11728. const byte iv[] =
  11729. {
  11730. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  11731. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  11732. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  11733. };
  11734. printf(testingFmt, "wc_Des3_SetIV()");
  11735. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  11736. if (ret != 0)
  11737. return ret;
  11738. /* DES_ENCRYPTION or DES_DECRYPTION */
  11739. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  11740. if (ret == 0) {
  11741. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  11742. ret = WOLFSSL_FATAL_ERROR;
  11743. }
  11744. }
  11745. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  11746. /* Test explicitly wc_Des3_SetIV() */
  11747. if (ret == 0) {
  11748. ret = wc_Des3_SetIV(NULL, iv);
  11749. if (ret == BAD_FUNC_ARG) {
  11750. ret = wc_Des3_SetIV(&des, NULL);
  11751. } else if (ret == 0) {
  11752. ret = WOLFSSL_FATAL_ERROR;
  11753. }
  11754. }
  11755. #endif
  11756. wc_Des3Free(&des);
  11757. printf(resultFmt, ret == 0 ? passed : failed);
  11758. #endif
  11759. return ret;
  11760. } /* END test_wc_Des3_SetIV */
  11761. /*
  11762. * unit test for wc_Des3_SetKey()
  11763. */
  11764. static int test_wc_Des3_SetKey (void)
  11765. {
  11766. int ret = 0;
  11767. #ifndef NO_DES3
  11768. Des3 des;
  11769. const byte key[] =
  11770. {
  11771. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  11772. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  11773. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  11774. };
  11775. const byte iv[] =
  11776. {
  11777. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  11778. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  11779. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  11780. };
  11781. printf(testingFmt, "wc_Des3_SetKey()");
  11782. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  11783. if (ret != 0)
  11784. return ret;
  11785. /* DES_ENCRYPTION or DES_DECRYPTION */
  11786. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  11787. if (ret == 0) {
  11788. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  11789. ret = WOLFSSL_FATAL_ERROR;
  11790. }
  11791. }
  11792. /* Test bad args. */
  11793. if (ret == 0) {
  11794. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  11795. if (ret == BAD_FUNC_ARG) {
  11796. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  11797. }
  11798. if (ret == BAD_FUNC_ARG) {
  11799. ret = wc_Des3_SetKey(&des, key, iv, -1);
  11800. }
  11801. if (ret == BAD_FUNC_ARG) {
  11802. /* Default case. Should return 0. */
  11803. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  11804. }
  11805. } /* END if ret != 0 */
  11806. wc_Des3Free(&des);
  11807. printf(resultFmt, ret == 0 ? passed : failed);
  11808. #endif
  11809. return ret;
  11810. } /* END test_wc_Des3_SetKey */
  11811. /*
  11812. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  11813. */
  11814. static int test_wc_Des3_CbcEncryptDecrypt (void)
  11815. {
  11816. int ret = 0;
  11817. #ifndef NO_DES3
  11818. Des3 des;
  11819. byte cipher[24];
  11820. byte plain[24];
  11821. const byte key[] =
  11822. {
  11823. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  11824. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  11825. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  11826. };
  11827. const byte iv[] =
  11828. {
  11829. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  11830. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  11831. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  11832. };
  11833. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  11834. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11835. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11836. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11837. };
  11838. printf(testingFmt, "wc_Des3_CbcEncrypt()");
  11839. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  11840. if (ret != 0)
  11841. return ret;
  11842. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  11843. if (ret == 0) {
  11844. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  11845. if (ret == 0) {
  11846. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  11847. }
  11848. if (ret == 0) {
  11849. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  11850. }
  11851. }
  11852. if (ret == 0) {
  11853. if (XMEMCMP(plain, vector, 24) != 0) {
  11854. ret = WOLFSSL_FATAL_ERROR;
  11855. }
  11856. }
  11857. /* Pass in bad args. */
  11858. if (ret == 0) {
  11859. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  11860. if (ret == BAD_FUNC_ARG) {
  11861. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  11862. }
  11863. if (ret == BAD_FUNC_ARG) {
  11864. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  11865. }
  11866. if (ret != BAD_FUNC_ARG) {
  11867. ret = WOLFSSL_FATAL_ERROR;
  11868. } else {
  11869. ret = 0;
  11870. }
  11871. }
  11872. if (ret == 0) {
  11873. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  11874. if (ret == BAD_FUNC_ARG) {
  11875. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  11876. }
  11877. if (ret == BAD_FUNC_ARG) {
  11878. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  11879. }
  11880. if (ret != BAD_FUNC_ARG) {
  11881. ret = WOLFSSL_FATAL_ERROR;
  11882. } else {
  11883. ret = 0;
  11884. }
  11885. }
  11886. wc_Des3Free(&des);
  11887. printf(resultFmt, ret == 0 ? passed : failed);
  11888. #endif
  11889. return ret;
  11890. } /* END wc_Des3_CbcEncrypt */
  11891. /*
  11892. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  11893. */
  11894. static int test_wc_Des3_CbcEncryptDecryptWithKey (void)
  11895. {
  11896. int ret = 0;
  11897. #ifndef NO_DES3
  11898. word32 vectorSz, cipherSz;
  11899. byte cipher[24];
  11900. byte plain[24];
  11901. byte vector[] = /* Now is the time for all w/o trailing 0 */
  11902. {
  11903. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11904. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11905. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11906. };
  11907. byte key[] =
  11908. {
  11909. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  11910. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  11911. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  11912. };
  11913. byte iv[] =
  11914. {
  11915. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  11916. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  11917. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  11918. };
  11919. vectorSz = sizeof(byte) * 24;
  11920. cipherSz = sizeof(byte) * 24;
  11921. printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
  11922. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  11923. if (ret == 0) {
  11924. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  11925. if (ret == 0) {
  11926. if (XMEMCMP(plain, vector, 24) != 0) {
  11927. ret = WOLFSSL_FATAL_ERROR;
  11928. }
  11929. }
  11930. }
  11931. /* pass in bad args. */
  11932. if (ret == 0) {
  11933. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  11934. if (ret == BAD_FUNC_ARG) {
  11935. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  11936. }
  11937. if (ret == BAD_FUNC_ARG) {
  11938. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  11939. }
  11940. if (ret == BAD_FUNC_ARG) {
  11941. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  11942. key, NULL);
  11943. } else {
  11944. /* Return code catch. */
  11945. ret = WOLFSSL_FAILURE;
  11946. }
  11947. }
  11948. if (ret == 0) {
  11949. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  11950. if (ret == BAD_FUNC_ARG) {
  11951. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  11952. }
  11953. if (ret == BAD_FUNC_ARG) {
  11954. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  11955. }
  11956. if (ret == BAD_FUNC_ARG) {
  11957. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  11958. } else {
  11959. ret = WOLFSSL_FAILURE;
  11960. }
  11961. }
  11962. printf(resultFmt, ret == 0 ? passed : failed);
  11963. #endif
  11964. return ret;
  11965. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  11966. /*
  11967. * Unit test for wc_Des3_EcbEncrypt
  11968. */
  11969. static int test_wc_Des3_EcbEncrypt (void)
  11970. {
  11971. int ret = 0;
  11972. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  11973. Des3 des;
  11974. byte cipher[24];
  11975. word32 cipherSz = sizeof(cipher);
  11976. const byte key[] =
  11977. {
  11978. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  11979. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  11980. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  11981. };
  11982. const byte iv[] =
  11983. {
  11984. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  11985. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  11986. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  11987. };
  11988. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  11989. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  11990. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  11991. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  11992. };
  11993. printf(testingFmt, "wc_Des3_EcbEncrypt()");
  11994. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  11995. if (ret != 0) {
  11996. return ret;
  11997. }
  11998. if (ret == 0 ) {
  11999. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  12000. }
  12001. /* Bad Cases */
  12002. if (ret == 0) {
  12003. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  12004. if (ret == BAD_FUNC_ARG) {
  12005. ret = 0;
  12006. }
  12007. }
  12008. if (ret == 0) {
  12009. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  12010. if (ret == BAD_FUNC_ARG) {
  12011. ret = 0;
  12012. }
  12013. }
  12014. if (ret == 0) {
  12015. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  12016. if (ret == BAD_FUNC_ARG) {
  12017. ret = 0;
  12018. }
  12019. }
  12020. if (ret == 0) {
  12021. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  12022. if (ret == BAD_FUNC_ARG) {
  12023. ret = 0;
  12024. }
  12025. }
  12026. if (ret == 0) {
  12027. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  12028. if (ret == BAD_FUNC_ARG) {
  12029. ret = 0;
  12030. }
  12031. }
  12032. /* Good Cases */
  12033. if (ret == 0) {
  12034. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  12035. }
  12036. wc_Des3Free(&des);
  12037. printf(resultFmt, ret == 0 ? passed : failed);
  12038. #endif
  12039. return ret;
  12040. } /* END test_wc_Des3_EcbEncrypt */
  12041. /*
  12042. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  12043. */
  12044. static int test_wc_Chacha_SetKey (void)
  12045. {
  12046. int ret = 0;
  12047. #ifdef HAVE_CHACHA
  12048. ChaCha ctx;
  12049. const byte key[] =
  12050. {
  12051. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  12052. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  12053. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  12054. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  12055. };
  12056. byte cipher[128];
  12057. printf(testingFmt, "wc_Chacha_SetKey()");
  12058. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  12059. /* Test bad args. */
  12060. if (ret == 0) {
  12061. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  12062. if (ret == BAD_FUNC_ARG) {
  12063. ret = wc_Chacha_SetKey(&ctx, key, 18);
  12064. }
  12065. if (ret == BAD_FUNC_ARG) {
  12066. ret = 0;
  12067. } else {
  12068. ret = WOLFSSL_FATAL_ERROR;
  12069. }
  12070. }
  12071. printf(resultFmt, ret == 0 ? passed : failed);
  12072. if (ret != 0) {
  12073. return ret;
  12074. }
  12075. printf(testingFmt, "wc_Chacha_SetIV");
  12076. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  12077. if (ret == 0) {
  12078. /* Test bad args. */
  12079. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  12080. if (ret == BAD_FUNC_ARG) {
  12081. ret = 0;
  12082. } else {
  12083. ret = WOLFSSL_FAILURE;
  12084. }
  12085. }
  12086. printf(resultFmt, ret == 0 ? passed : failed);
  12087. #endif
  12088. return ret;
  12089. } /* END test_wc_Chacha_SetKey */
  12090. /*
  12091. * unit test for wc_Poly1305SetKey()
  12092. */
  12093. static int test_wc_Poly1305SetKey(void)
  12094. {
  12095. int ret = 0;
  12096. #ifdef HAVE_POLY1305
  12097. Poly1305 ctx;
  12098. const byte key[] =
  12099. {
  12100. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  12101. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  12102. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  12103. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  12104. };
  12105. printf(testingFmt, "wc_Poly1305_SetKey()");
  12106. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  12107. /* Test bad args. */
  12108. if (ret == 0) {
  12109. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  12110. if(ret == BAD_FUNC_ARG) {
  12111. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  12112. }
  12113. if (ret == BAD_FUNC_ARG) {
  12114. ret = wc_Poly1305SetKey(&ctx, key, 18);
  12115. }
  12116. if (ret == BAD_FUNC_ARG) {
  12117. ret = 0;
  12118. } else {
  12119. ret = WOLFSSL_FATAL_ERROR;
  12120. }
  12121. }
  12122. printf(resultFmt, ret == 0 ? passed : failed);
  12123. #endif
  12124. return ret;
  12125. } /* END test_wc_Poly1305_SetKey() */
  12126. /*
  12127. * Testing wc_Chacha_Process()
  12128. */
  12129. static int test_wc_Chacha_Process (void)
  12130. {
  12131. int ret = 0;
  12132. #ifdef HAVE_CHACHA
  12133. ChaCha enc, dec;
  12134. byte cipher[128];
  12135. byte plain[128];
  12136. const byte key[] =
  12137. {
  12138. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  12139. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  12140. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  12141. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  12142. };
  12143. const char* input = "Everybody gets Friday off.";
  12144. word32 keySz = sizeof(key)/sizeof(byte);
  12145. unsigned long int inlen = XSTRLEN(input);
  12146. /*Initialize stack varialbes.*/
  12147. XMEMSET(cipher, 0, 128);
  12148. XMEMSET(plain, 0, 128);
  12149. printf(testingFmt, "wc_Chacha_Process()");
  12150. ret = wc_Chacha_SetKey(&enc, key, keySz);
  12151. AssertIntEQ(ret, 0);
  12152. ret = wc_Chacha_SetKey(&dec, key, keySz);
  12153. AssertIntEQ(ret, 0);
  12154. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  12155. AssertIntEQ(ret, 0);
  12156. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  12157. AssertIntEQ(ret, 0);
  12158. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  12159. AssertIntEQ(ret, 0);
  12160. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  12161. AssertIntEQ(ret, 0);
  12162. ret = XMEMCMP(input, plain, (int)inlen);
  12163. AssertIntEQ(ret, 0);
  12164. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  12165. /* test checking and using leftovers, currently just in C code */
  12166. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  12167. AssertIntEQ(ret, 0);
  12168. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  12169. AssertIntEQ(ret, 0);
  12170. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  12171. AssertIntEQ(ret, 0);
  12172. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  12173. (byte*)input + (inlen - 2), 2);
  12174. AssertIntEQ(ret, 0);
  12175. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  12176. AssertIntEQ(ret, 0);
  12177. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  12178. (byte*)input + (inlen - 2), 2);
  12179. AssertIntEQ(ret, 0);
  12180. ret = XMEMCMP(input, plain, (int)inlen);
  12181. AssertIntEQ(ret, 0);
  12182. /* check edge cases with counter increment */
  12183. {
  12184. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  12185. const byte expected[] = {
  12186. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  12187. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  12188. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  12189. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  12190. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  12191. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  12192. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  12193. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  12194. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  12195. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  12196. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  12197. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  12198. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  12199. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  12200. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  12201. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  12202. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  12203. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  12204. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  12205. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  12206. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  12207. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  12208. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  12209. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  12210. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  12211. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  12212. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  12213. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  12214. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  12215. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  12216. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  12217. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  12218. };
  12219. const byte iv2[] = {
  12220. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  12221. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  12222. };
  12223. byte input2[256];
  12224. int i;
  12225. for (i = 0; i < 256; i++)
  12226. input2[i] = i;
  12227. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  12228. AssertIntEQ(ret, 0);
  12229. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  12230. AssertIntEQ(ret, 0);
  12231. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  12232. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  12233. AssertIntEQ(ret, 0);
  12234. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  12235. /* partial */
  12236. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  12237. AssertIntEQ(ret, 0);
  12238. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  12239. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  12240. AssertIntEQ(ret, 0);
  12241. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  12242. }
  12243. #endif
  12244. /* Test bad args. */
  12245. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  12246. AssertIntEQ(ret, BAD_FUNC_ARG);
  12247. if (ret == BAD_FUNC_ARG) {
  12248. ret = 0;
  12249. }
  12250. printf(resultFmt, ret == 0 ? passed : failed);
  12251. #endif
  12252. return ret;
  12253. } /* END test_wc_Chacha_Process */
  12254. /*
  12255. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  12256. */
  12257. static int test_wc_ChaCha20Poly1305_aead (void)
  12258. {
  12259. int ret = 0;
  12260. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  12261. const byte key[] = {
  12262. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  12263. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  12264. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  12265. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  12266. };
  12267. const byte plaintext[] = {
  12268. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  12269. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  12270. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  12271. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  12272. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  12273. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  12274. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  12275. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  12276. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  12277. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  12278. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  12279. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  12280. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  12281. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  12282. 0x74, 0x2e
  12283. };
  12284. const byte iv[] = {
  12285. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  12286. 0x44, 0x45, 0x46, 0x47
  12287. };
  12288. const byte aad[] = { /* additional data */
  12289. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  12290. 0xc4, 0xc5, 0xc6, 0xc7
  12291. };
  12292. const byte cipher[] = { /* expected output from operation */
  12293. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  12294. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  12295. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  12296. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  12297. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  12298. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  12299. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  12300. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  12301. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  12302. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  12303. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  12304. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  12305. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  12306. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  12307. 0x61, 0x16
  12308. };
  12309. const byte authTag[] = { /* expected output from operation */
  12310. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  12311. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  12312. };
  12313. byte generatedCiphertext[272];
  12314. byte generatedPlaintext[272];
  12315. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  12316. /* Initialize stack variables. */
  12317. XMEMSET(generatedCiphertext, 0, 272);
  12318. XMEMSET(generatedPlaintext, 0, 272);
  12319. /* Test Encrypt */
  12320. printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
  12321. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  12322. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  12323. AssertIntEQ(ret, 0);
  12324. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  12325. AssertIntEQ(ret, 0);
  12326. /* Test bad args. */
  12327. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  12328. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  12329. AssertIntEQ(ret, BAD_FUNC_ARG);
  12330. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  12331. plaintext, sizeof(plaintext),
  12332. generatedCiphertext, generatedAuthTag);
  12333. AssertIntEQ(ret, BAD_FUNC_ARG);
  12334. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  12335. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  12336. AssertIntEQ(ret, BAD_FUNC_ARG);
  12337. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  12338. plaintext, 0, generatedCiphertext, generatedAuthTag);
  12339. AssertIntEQ(ret, BAD_FUNC_ARG);
  12340. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  12341. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  12342. AssertIntEQ(ret, BAD_FUNC_ARG);
  12343. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  12344. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  12345. if (ret == BAD_FUNC_ARG) {
  12346. ret = 0;
  12347. }
  12348. printf(resultFmt, ret == 0 ? passed : failed);
  12349. if (ret != 0) {
  12350. return ret;
  12351. }
  12352. printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
  12353. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  12354. sizeof(cipher), authTag, generatedPlaintext);
  12355. AssertIntEQ(ret, 0);
  12356. ret = XMEMCMP(generatedPlaintext, plaintext,
  12357. sizeof(plaintext)/sizeof(byte));
  12358. AssertIntEQ(ret, 0);
  12359. /* Test bad args. */
  12360. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  12361. sizeof(cipher), authTag, generatedPlaintext);
  12362. AssertIntEQ(ret, BAD_FUNC_ARG);
  12363. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  12364. cipher, sizeof(cipher), authTag, generatedPlaintext);
  12365. AssertIntEQ(ret, BAD_FUNC_ARG);
  12366. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  12367. sizeof(cipher), authTag, generatedPlaintext);
  12368. AssertIntEQ(ret, BAD_FUNC_ARG);
  12369. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  12370. sizeof(cipher), NULL, generatedPlaintext);
  12371. AssertIntEQ(ret, BAD_FUNC_ARG);
  12372. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  12373. sizeof(cipher), authTag, NULL);
  12374. AssertIntEQ(ret, BAD_FUNC_ARG);
  12375. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  12376. 0, authTag, generatedPlaintext);
  12377. AssertIntEQ(ret, BAD_FUNC_ARG);
  12378. if (ret == BAD_FUNC_ARG) {
  12379. ret = 0;
  12380. }
  12381. printf(resultFmt, ret == 0 ? passed : failed);
  12382. #endif
  12383. return ret;
  12384. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  12385. /*
  12386. * Testing function for wc_Rc2SetKey().
  12387. */
  12388. static int test_wc_Rc2SetKey(void)
  12389. {
  12390. int ret = 0;
  12391. #ifdef WC_RC2
  12392. Rc2 rc2;
  12393. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  12394. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  12395. printf(testingFmt, "wc_Rc2SetKey()");
  12396. /* valid key and IV */
  12397. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  12398. iv, 40);
  12399. if (ret == 0) {
  12400. /* valid key, no IV */
  12401. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  12402. NULL, 40);
  12403. }
  12404. /* bad arguments */
  12405. if (ret == 0) {
  12406. /* null Rc2 struct */
  12407. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  12408. iv, 40);
  12409. if (ret == BAD_FUNC_ARG) {
  12410. ret = 0;
  12411. }
  12412. }
  12413. if (ret == 0) {
  12414. /* null key */
  12415. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  12416. iv, 40);
  12417. if (ret == BAD_FUNC_ARG) {
  12418. ret = 0;
  12419. }
  12420. }
  12421. if (ret == 0) {
  12422. /* key size == 0 */
  12423. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  12424. if (ret == WC_KEY_SIZE_E) {
  12425. ret = 0;
  12426. }
  12427. }
  12428. if (ret == 0) {
  12429. /* key size > 128 */
  12430. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  12431. if (ret == WC_KEY_SIZE_E) {
  12432. ret = 0;
  12433. }
  12434. }
  12435. if (ret == 0) {
  12436. /* effective bits == 0 */
  12437. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  12438. iv, 0);
  12439. if (ret == WC_KEY_SIZE_E) {
  12440. ret = 0;
  12441. }
  12442. }
  12443. if (ret == 0) {
  12444. /* effective bits > 1024 */
  12445. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  12446. iv, 1025);
  12447. if (ret == WC_KEY_SIZE_E) {
  12448. ret = 0;
  12449. }
  12450. }
  12451. printf(resultFmt, ret == 0 ? passed : failed);
  12452. #endif
  12453. return ret;
  12454. } /* END test_wc_Rc2SetKey */
  12455. /*
  12456. * Testing function for wc_Rc2SetIV().
  12457. */
  12458. static int test_wc_Rc2SetIV(void)
  12459. {
  12460. int ret = 0;
  12461. #ifdef WC_RC2
  12462. Rc2 rc2;
  12463. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  12464. printf(testingFmt, "wc_Rc2SetIV()");
  12465. /* valid IV */
  12466. ret = wc_Rc2SetIV(&rc2, iv);
  12467. if (ret == 0) {
  12468. /* valid NULL IV */
  12469. ret = wc_Rc2SetIV(&rc2, NULL);
  12470. }
  12471. /* bad arguments */
  12472. if (ret == 0) {
  12473. ret = wc_Rc2SetIV(NULL, iv);
  12474. if (ret == BAD_FUNC_ARG) {
  12475. ret = 0;
  12476. }
  12477. }
  12478. printf(resultFmt, ret == 0 ? passed : failed);
  12479. #endif
  12480. return ret;
  12481. } /* END test_wc_Rc2SetKey */
  12482. /*
  12483. * Testing function for wc_Rc2EcbEncrypt().
  12484. */
  12485. static int test_wc_Rc2EcbEncryptDecrypt(void)
  12486. {
  12487. int ret = 0;
  12488. #ifdef WC_RC2
  12489. Rc2 rc2;
  12490. int effectiveKeyBits = 63;
  12491. byte cipher[RC2_BLOCK_SIZE];
  12492. byte plain[RC2_BLOCK_SIZE];
  12493. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  12494. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  12495. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  12496. printf(testingFmt, "wc_Rc2EcbEncryptDecrypt()");
  12497. XMEMSET(cipher, 0, sizeof(cipher));
  12498. XMEMSET(plain, 0, sizeof(plain));
  12499. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  12500. NULL, effectiveKeyBits);
  12501. if (ret == 0) {
  12502. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  12503. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  12504. ret = WOLFSSL_FATAL_ERROR;
  12505. }
  12506. if (ret == 0) {
  12507. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  12508. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  12509. ret = WOLFSSL_FATAL_ERROR;
  12510. }
  12511. }
  12512. }
  12513. /* Rc2EcbEncrypt bad arguments */
  12514. if (ret == 0) {
  12515. /* null Rc2 struct */
  12516. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  12517. if (ret == BAD_FUNC_ARG) {
  12518. ret = 0;
  12519. }
  12520. }
  12521. if (ret == 0) {
  12522. /* null out buffer */
  12523. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  12524. if (ret == BAD_FUNC_ARG) {
  12525. ret = 0;
  12526. }
  12527. }
  12528. if (ret == 0) {
  12529. /* null input buffer */
  12530. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  12531. if (ret == BAD_FUNC_ARG) {
  12532. ret = 0;
  12533. }
  12534. }
  12535. if (ret == 0) {
  12536. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  12537. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  12538. if (ret == BUFFER_E) {
  12539. ret = 0;
  12540. }
  12541. }
  12542. /* Rc2EcbDecrypt bad arguments */
  12543. if (ret == 0) {
  12544. /* null Rc2 struct */
  12545. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  12546. if (ret == BAD_FUNC_ARG) {
  12547. ret = 0;
  12548. }
  12549. }
  12550. if (ret == 0) {
  12551. /* null out buffer */
  12552. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  12553. if (ret == BAD_FUNC_ARG) {
  12554. ret = 0;
  12555. }
  12556. }
  12557. if (ret == 0) {
  12558. /* null input buffer */
  12559. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  12560. if (ret == BAD_FUNC_ARG) {
  12561. ret = 0;
  12562. }
  12563. }
  12564. if (ret == 0) {
  12565. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  12566. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  12567. if (ret == BUFFER_E) {
  12568. ret = 0;
  12569. }
  12570. }
  12571. printf(resultFmt, ret == 0 ? passed : failed);
  12572. #endif
  12573. return ret;
  12574. } /* END test_wc_Rc2SetKey */
  12575. /*
  12576. * Testing function for wc_Rc2CbcEncrypt().
  12577. */
  12578. static int test_wc_Rc2CbcEncryptDecrypt(void)
  12579. {
  12580. int ret = 0;
  12581. #ifdef WC_RC2
  12582. Rc2 rc2;
  12583. int effectiveKeyBits = 63;
  12584. byte cipher[RC2_BLOCK_SIZE*2];
  12585. byte plain[RC2_BLOCK_SIZE*2];
  12586. /* vector taken from test.c */
  12587. byte key[] = {
  12588. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  12589. };
  12590. byte iv[] = {
  12591. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  12592. };
  12593. byte input[] = {
  12594. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  12595. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  12596. };
  12597. byte output[] = {
  12598. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  12599. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  12600. };
  12601. printf(testingFmt, "wc_Rc2CbcEncryptDecrypt()");
  12602. XMEMSET(cipher, 0, sizeof(cipher));
  12603. XMEMSET(plain, 0, sizeof(plain));
  12604. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  12605. iv, effectiveKeyBits);
  12606. if (ret == 0) {
  12607. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  12608. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  12609. ret = WOLFSSL_FATAL_ERROR;
  12610. } else {
  12611. /* reset IV for decrypt */
  12612. ret = wc_Rc2SetIV(&rc2, iv);
  12613. }
  12614. if (ret == 0) {
  12615. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  12616. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  12617. ret = WOLFSSL_FATAL_ERROR;
  12618. }
  12619. }
  12620. }
  12621. /* Rc2CbcEncrypt bad arguments */
  12622. if (ret == 0) {
  12623. /* null Rc2 struct */
  12624. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  12625. if (ret == BAD_FUNC_ARG) {
  12626. ret = 0;
  12627. }
  12628. }
  12629. if (ret == 0) {
  12630. /* null out buffer */
  12631. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  12632. if (ret == BAD_FUNC_ARG) {
  12633. ret = 0;
  12634. }
  12635. }
  12636. if (ret == 0) {
  12637. /* null input buffer */
  12638. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  12639. if (ret == BAD_FUNC_ARG) {
  12640. ret = 0;
  12641. }
  12642. }
  12643. /* Rc2CbcDecrypt bad arguments */
  12644. if (ret == 0) {
  12645. /* in size is 0 */
  12646. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  12647. if (ret != 0) {
  12648. ret = WOLFSSL_FATAL_ERROR;
  12649. }
  12650. }
  12651. if (ret == 0) {
  12652. /* null Rc2 struct */
  12653. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  12654. if (ret == BAD_FUNC_ARG) {
  12655. ret = 0;
  12656. }
  12657. }
  12658. if (ret == 0) {
  12659. /* null out buffer */
  12660. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  12661. if (ret == BAD_FUNC_ARG) {
  12662. ret = 0;
  12663. }
  12664. }
  12665. if (ret == 0) {
  12666. /* null input buffer */
  12667. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  12668. if (ret == BAD_FUNC_ARG) {
  12669. ret = 0;
  12670. }
  12671. }
  12672. printf(resultFmt, ret == 0 ? passed : failed);
  12673. #endif
  12674. return ret;
  12675. } /* END test_wc_Rc2SetKey */
  12676. /*
  12677. * Testing function for wc_AesSetIV
  12678. */
  12679. static int test_wc_AesSetIV (void)
  12680. {
  12681. int ret = 0;
  12682. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  12683. Aes aes;
  12684. byte key16[] =
  12685. {
  12686. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12687. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  12688. };
  12689. byte iv1[] = "1234567890abcdef";
  12690. byte iv2[] = "0987654321fedcba";
  12691. printf(testingFmt, "wc_AesSetIV()");
  12692. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  12693. if (ret != 0)
  12694. return ret;
  12695. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  12696. iv1, AES_ENCRYPTION);
  12697. if(ret == 0) {
  12698. ret = wc_AesSetIV(&aes, iv2);
  12699. }
  12700. /* Test bad args. */
  12701. if(ret == 0) {
  12702. ret = wc_AesSetIV(NULL, iv1);
  12703. if(ret == BAD_FUNC_ARG) {
  12704. /* NULL iv should return 0. */
  12705. ret = wc_AesSetIV(&aes, NULL);
  12706. } else {
  12707. ret = WOLFSSL_FATAL_ERROR;
  12708. }
  12709. }
  12710. wc_AesFree(&aes);
  12711. printf(resultFmt, ret == 0 ? passed : failed);
  12712. #endif
  12713. return ret;
  12714. } /* test_wc_AesSetIV */
  12715. /*
  12716. * Testing function for wc_AesSetKey().
  12717. */
  12718. static int test_wc_AesSetKey (void)
  12719. {
  12720. int ret = 0;
  12721. #ifndef NO_AES
  12722. Aes aes;
  12723. byte key16[] =
  12724. {
  12725. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12726. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  12727. };
  12728. #ifdef WOLFSSL_AES_192
  12729. byte key24[] =
  12730. {
  12731. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12732. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  12733. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  12734. };
  12735. #endif
  12736. #ifdef WOLFSSL_AES_256
  12737. byte key32[] =
  12738. {
  12739. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12740. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  12741. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12742. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  12743. };
  12744. #endif
  12745. byte badKey16[] =
  12746. {
  12747. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12748. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  12749. };
  12750. byte iv[] = "1234567890abcdef";
  12751. printf(testingFmt, "wc_AesSetKey()");
  12752. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  12753. if (ret != 0)
  12754. return ret;
  12755. #ifdef WOLFSSL_AES_128
  12756. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  12757. iv, AES_ENCRYPTION);
  12758. #endif
  12759. #ifdef WOLFSSL_AES_192
  12760. if (ret == 0) {
  12761. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  12762. iv, AES_ENCRYPTION);
  12763. }
  12764. #endif
  12765. #ifdef WOLFSSL_AES_256
  12766. if (ret == 0) {
  12767. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  12768. iv, AES_ENCRYPTION);
  12769. }
  12770. #endif
  12771. /* Pass in bad args. */
  12772. if (ret == 0) {
  12773. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  12774. iv, AES_ENCRYPTION);
  12775. if (ret == BAD_FUNC_ARG) {
  12776. ret = wc_AesSetKey(&aes, badKey16,
  12777. (word32) sizeof(badKey16) / sizeof(byte),
  12778. iv, AES_ENCRYPTION);
  12779. }
  12780. if (ret == BAD_FUNC_ARG) {
  12781. ret = 0;
  12782. } else {
  12783. ret = WOLFSSL_FATAL_ERROR;
  12784. }
  12785. }
  12786. wc_AesFree(&aes);
  12787. printf(resultFmt, ret == 0 ? passed : failed);
  12788. #endif
  12789. return ret;
  12790. } /* END test_wc_AesSetKey */
  12791. /*
  12792. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  12793. * and wc_AesCbcDecryptWithKey()
  12794. */
  12795. static int test_wc_AesCbcEncryptDecrypt (void)
  12796. {
  12797. int ret = 0;
  12798. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  12799. defined(WOLFSSL_AES_256)
  12800. Aes aes;
  12801. byte key32[] =
  12802. {
  12803. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12804. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  12805. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12806. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  12807. };
  12808. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  12809. {
  12810. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  12811. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  12812. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  12813. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  12814. };
  12815. byte iv[] = "1234567890abcdef";
  12816. byte enc[sizeof(vector)];
  12817. byte dec[sizeof(vector)];
  12818. int cbcE = WOLFSSL_FATAL_ERROR;
  12819. int cbcD = WOLFSSL_FATAL_ERROR;
  12820. int cbcDWK = WOLFSSL_FATAL_ERROR;
  12821. byte dec2[sizeof(vector)];
  12822. /* Init stack variables. */
  12823. XMEMSET(enc, 0, sizeof(enc));
  12824. XMEMSET(dec, 0, sizeof(vector));
  12825. XMEMSET(dec2, 0, sizeof(vector));
  12826. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  12827. if (ret != 0)
  12828. return ret;
  12829. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  12830. if (ret == 0) {
  12831. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  12832. if (ret == 0) {
  12833. /* Re init for decrypt and set flag. */
  12834. cbcE = 0;
  12835. wc_AesFree(&aes);
  12836. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  12837. iv, AES_DECRYPTION);
  12838. }
  12839. if (ret == 0) {
  12840. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  12841. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  12842. ret = WOLFSSL_FATAL_ERROR;
  12843. } else {
  12844. /* Set flag. */
  12845. cbcD = 0;
  12846. }
  12847. }
  12848. }
  12849. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  12850. if (ret == 0 || cbcE == 0) {
  12851. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  12852. key32, sizeof(key32)/sizeof(byte), iv);
  12853. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  12854. cbcDWK = 0;
  12855. }
  12856. }
  12857. printf(testingFmt, "wc_AesCbcEncrypt()");
  12858. /* Pass in bad args */
  12859. if (cbcE == 0) {
  12860. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  12861. if (cbcE == BAD_FUNC_ARG) {
  12862. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  12863. }
  12864. if (cbcE == BAD_FUNC_ARG) {
  12865. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  12866. }
  12867. if (cbcE == BAD_FUNC_ARG) {
  12868. cbcE = 0;
  12869. } else {
  12870. cbcE = WOLFSSL_FATAL_ERROR;
  12871. }
  12872. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  12873. if (cbcE == 0) {
  12874. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  12875. }
  12876. if (cbcE == BAD_LENGTH_E) {
  12877. cbcE = 0;
  12878. } else {
  12879. cbcE = WOLFSSL_FATAL_ERROR;
  12880. }
  12881. #endif
  12882. }
  12883. if (cbcE == 0) {
  12884. /* Test passing in size of 0 */
  12885. XMEMSET(enc, 0, sizeof(enc));
  12886. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  12887. if (cbcE == 0) {
  12888. /* Check enc was not modified */
  12889. int i;
  12890. for (i = 0; i < (int)sizeof(enc); i++)
  12891. cbcE |= enc[i];
  12892. }
  12893. }
  12894. printf(resultFmt, cbcE == 0 ? passed : failed);
  12895. if (cbcE != 0) {
  12896. wc_AesFree(&aes);
  12897. return cbcE;
  12898. }
  12899. printf(testingFmt, "wc_AesCbcDecrypt()");
  12900. if (cbcD == 0) {
  12901. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  12902. if (cbcD == BAD_FUNC_ARG) {
  12903. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  12904. }
  12905. if (cbcD == BAD_FUNC_ARG) {
  12906. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  12907. }
  12908. if (cbcD == BAD_FUNC_ARG) {
  12909. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  12910. }
  12911. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  12912. if (cbcD == BAD_LENGTH_E) {
  12913. cbcD = 0;
  12914. } else {
  12915. cbcD = WOLFSSL_FATAL_ERROR;
  12916. }
  12917. #else
  12918. if (cbcD == BAD_FUNC_ARG) {
  12919. cbcD = 0;
  12920. } else {
  12921. cbcD = WOLFSSL_FATAL_ERROR;
  12922. }
  12923. #endif
  12924. }
  12925. if (cbcD == 0) {
  12926. /* Test passing in size of 0 */
  12927. XMEMSET(dec, 0, sizeof(dec));
  12928. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  12929. if (cbcD == 0) {
  12930. /* Check dec was not modified */
  12931. int i;
  12932. for (i = 0; i < (int)sizeof(dec); i++)
  12933. cbcD |= dec[i];
  12934. }
  12935. }
  12936. printf(resultFmt, cbcD == 0 ? passed : failed);
  12937. if (cbcD != 0) {
  12938. wc_AesFree(&aes);
  12939. return cbcD;
  12940. }
  12941. printf(testingFmt, "wc_AesCbcDecryptWithKey()");
  12942. if (cbcDWK == 0) {
  12943. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  12944. key32, sizeof(key32)/sizeof(byte), iv);
  12945. if (cbcDWK == BAD_FUNC_ARG) {
  12946. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  12947. key32, sizeof(key32)/sizeof(byte), iv);
  12948. }
  12949. if (cbcDWK == BAD_FUNC_ARG) {
  12950. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  12951. NULL, sizeof(key32)/sizeof(byte), iv);
  12952. }
  12953. if (cbcDWK == BAD_FUNC_ARG) {
  12954. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  12955. key32, sizeof(key32)/sizeof(byte), NULL);
  12956. }
  12957. if (cbcDWK == BAD_FUNC_ARG) {
  12958. cbcDWK = 0;
  12959. } else {
  12960. cbcDWK = WOLFSSL_FATAL_ERROR;
  12961. }
  12962. }
  12963. wc_AesFree(&aes);
  12964. printf(resultFmt, cbcDWK == 0 ? passed : failed);
  12965. if (cbcDWK != 0) {
  12966. return cbcDWK;
  12967. }
  12968. #endif
  12969. return ret;
  12970. } /* END test_wc_AesCbcEncryptDecrypt */
  12971. /*
  12972. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  12973. */
  12974. static int test_wc_AesCtrEncryptDecrypt (void)
  12975. {
  12976. int ret = 0;
  12977. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  12978. Aes aesEnc, aesDec;
  12979. byte key32[] =
  12980. {
  12981. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12982. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  12983. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  12984. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  12985. };
  12986. byte vector[] = /* Now is the time for all w/o trailing 0 */
  12987. {
  12988. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  12989. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  12990. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  12991. };
  12992. byte iv[] = "1234567890abcdef";
  12993. byte enc[AES_BLOCK_SIZE * 2];
  12994. byte dec[AES_BLOCK_SIZE * 2];
  12995. /* Init stack variables. */
  12996. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  12997. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  12998. printf(testingFmt, "wc_AesCtrEncrypt()");
  12999. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  13000. if (ret != 0)
  13001. return ret;
  13002. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  13003. if (ret != 0) {
  13004. wc_AesFree(&aesEnc);
  13005. return ret;
  13006. }
  13007. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  13008. iv, AES_ENCRYPTION);
  13009. if (ret == 0) {
  13010. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  13011. sizeof(vector)/sizeof(byte));
  13012. if (ret == 0) {
  13013. /* Decrypt with wc_AesCtrEncrypt() */
  13014. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  13015. iv, AES_ENCRYPTION);
  13016. }
  13017. if (ret == 0) {
  13018. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  13019. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  13020. ret = WOLFSSL_FATAL_ERROR;
  13021. }
  13022. }
  13023. }
  13024. /* Test bad args. */
  13025. if (ret == 0) {
  13026. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  13027. if (ret == BAD_FUNC_ARG) {
  13028. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  13029. }
  13030. if (ret == BAD_FUNC_ARG) {
  13031. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  13032. }
  13033. if (ret == BAD_FUNC_ARG) {
  13034. ret = 0;
  13035. } else {
  13036. ret = WOLFSSL_FATAL_ERROR;
  13037. }
  13038. }
  13039. wc_AesFree(&aesEnc);
  13040. wc_AesFree(&aesDec);
  13041. printf(resultFmt, ret == 0 ? passed : failed);
  13042. #endif
  13043. return ret;
  13044. } /* END test_wc_AesCtrEncryptDecrypt */
  13045. /*
  13046. * test function for wc_AesGcmSetKey()
  13047. */
  13048. static int test_wc_AesGcmSetKey (void)
  13049. {
  13050. int ret = 0;
  13051. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  13052. Aes aes;
  13053. #ifdef WOLFSSL_AES_128
  13054. byte key16[] =
  13055. {
  13056. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13057. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  13058. };
  13059. #endif
  13060. #ifdef WOLFSSL_AES_192
  13061. byte key24[] =
  13062. {
  13063. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13064. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  13065. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  13066. };
  13067. #endif
  13068. #ifdef WOLFSSL_AES_256
  13069. byte key32[] =
  13070. {
  13071. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13072. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  13073. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13074. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  13075. };
  13076. #endif
  13077. byte badKey16[] =
  13078. {
  13079. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13080. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  13081. };
  13082. byte badKey24[] =
  13083. {
  13084. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13085. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  13086. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  13087. };
  13088. byte badKey32[] =
  13089. {
  13090. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  13091. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  13092. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13093. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  13094. };
  13095. printf(testingFmt, "wc_AesGcmSetKey()");
  13096. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  13097. if (ret != 0)
  13098. return ret;
  13099. #ifdef WOLFSSL_AES_128
  13100. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  13101. #endif
  13102. #ifdef WOLFSSL_AES_192
  13103. if (ret == 0) {
  13104. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  13105. }
  13106. #endif
  13107. #ifdef WOLFSSL_AES_256
  13108. if (ret == 0) {
  13109. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  13110. }
  13111. #endif
  13112. /* Pass in bad args. */
  13113. if (ret == 0) {
  13114. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  13115. if (ret == BAD_FUNC_ARG) {
  13116. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  13117. }
  13118. if (ret == BAD_FUNC_ARG) {
  13119. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  13120. }
  13121. if (ret == BAD_FUNC_ARG) {
  13122. ret = 0;
  13123. } else {
  13124. ret = WOLFSSL_FATAL_ERROR;
  13125. }
  13126. }
  13127. wc_AesFree(&aes);
  13128. printf(resultFmt, ret == 0 ? passed : failed);
  13129. #endif
  13130. return ret;
  13131. } /* END test_wc_AesGcmSetKey */
  13132. /*
  13133. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  13134. */
  13135. static int test_wc_AesGcmEncryptDecrypt (void)
  13136. {
  13137. int ret = 0;
  13138. /* WOLFSSL_AFALG requires 12 byte IV */
  13139. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  13140. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  13141. Aes aes;
  13142. byte key32[] =
  13143. {
  13144. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13145. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  13146. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13147. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  13148. };
  13149. byte vector[] = /* Now is the time for all w/o trailing 0 */
  13150. {
  13151. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13152. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13153. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13154. };
  13155. const byte a[] =
  13156. {
  13157. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  13158. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  13159. 0xab, 0xad, 0xda, 0xd2
  13160. };
  13161. byte iv[] = "1234567890a";
  13162. byte longIV[] = "1234567890abcdefghij";
  13163. byte enc[sizeof(vector)];
  13164. byte resultT[AES_BLOCK_SIZE];
  13165. byte dec[sizeof(vector)];
  13166. int gcmD = WOLFSSL_FATAL_ERROR;
  13167. int gcmE = WOLFSSL_FATAL_ERROR;
  13168. /* Init stack variables. */
  13169. XMEMSET(enc, 0, sizeof(vector));
  13170. XMEMSET(dec, 0, sizeof(vector));
  13171. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  13172. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  13173. if (ret != 0)
  13174. return ret;
  13175. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  13176. if (ret == 0) {
  13177. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  13178. iv, sizeof(iv)/sizeof(byte), resultT,
  13179. sizeof(resultT), a, sizeof(a));
  13180. }
  13181. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  13182. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  13183. iv, sizeof(iv)/sizeof(byte), resultT,
  13184. sizeof(resultT), a, sizeof(a));
  13185. if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  13186. gcmD = WOLFSSL_FATAL_ERROR;
  13187. }
  13188. }
  13189. printf(testingFmt, "wc_AesGcmEncrypt()");
  13190. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  13191. if (gcmE == 0) {
  13192. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  13193. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  13194. a, sizeof(a));
  13195. if (gcmE == BAD_FUNC_ARG) {
  13196. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  13197. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  13198. resultT, sizeof(resultT) + 1, a, sizeof(a));
  13199. }
  13200. if (gcmE == BAD_FUNC_ARG) {
  13201. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  13202. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  13203. resultT, sizeof(resultT) - 5, a, sizeof(a));
  13204. }
  13205. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  13206. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)
  13207. /* FIPS does not check the lower bound of ivSz */
  13208. #else
  13209. if (gcmE == BAD_FUNC_ARG) {
  13210. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  13211. sizeof(vector), iv, 0,
  13212. resultT, sizeof(resultT), a, sizeof(a));
  13213. }
  13214. #endif
  13215. if (gcmE == BAD_FUNC_ARG) {
  13216. gcmE = 0;
  13217. } else {
  13218. gcmE = WOLFSSL_FATAL_ERROR;
  13219. }
  13220. }
  13221. /* This case is now considered good. Long IVs are now allowed.
  13222. * Except for the original FIPS release, it still has an upper
  13223. * bound on the IV length. */
  13224. #if !defined(HAVE_FIPS) || \
  13225. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  13226. if (gcmE == 0) {
  13227. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  13228. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  13229. a, sizeof(a));
  13230. }
  13231. #else
  13232. (void)longIV;
  13233. #endif /* Old FIPS */
  13234. /* END wc_AesGcmEncrypt */
  13235. printf(resultFmt, gcmE == 0 ? passed : failed);
  13236. if (gcmE != 0) {
  13237. wc_AesFree(&aes);
  13238. return gcmE;
  13239. }
  13240. #ifdef HAVE_AES_DECRYPT
  13241. printf(testingFmt, "wc_AesGcmDecrypt()");
  13242. if (gcmD == 0) {
  13243. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  13244. iv, sizeof(iv)/sizeof(byte), resultT,
  13245. sizeof(resultT), a, sizeof(a));
  13246. if (gcmD == BAD_FUNC_ARG) {
  13247. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  13248. iv, sizeof(iv)/sizeof(byte), resultT,
  13249. sizeof(resultT), a, sizeof(a));
  13250. }
  13251. if (gcmD == BAD_FUNC_ARG) {
  13252. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  13253. iv, sizeof(iv)/sizeof(byte), resultT,
  13254. sizeof(resultT), a, sizeof(a));
  13255. }
  13256. if (gcmD == BAD_FUNC_ARG) {
  13257. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  13258. NULL, sizeof(iv)/sizeof(byte), resultT,
  13259. sizeof(resultT), a, sizeof(a));
  13260. }
  13261. if (gcmD == BAD_FUNC_ARG) {
  13262. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  13263. iv, sizeof(iv)/sizeof(byte), NULL,
  13264. sizeof(resultT), a, sizeof(a));
  13265. }
  13266. if (gcmD == BAD_FUNC_ARG) {
  13267. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  13268. iv, sizeof(iv)/sizeof(byte), resultT,
  13269. sizeof(resultT) + 1, a, sizeof(a));
  13270. }
  13271. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  13272. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)
  13273. /* FIPS does not check the lower bound of ivSz */
  13274. #else
  13275. if (gcmD == BAD_FUNC_ARG) {
  13276. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  13277. iv, 0, resultT,
  13278. sizeof(resultT), a, sizeof(a));
  13279. }
  13280. #endif
  13281. if (gcmD == BAD_FUNC_ARG) {
  13282. gcmD = 0;
  13283. } else {
  13284. gcmD = WOLFSSL_FATAL_ERROR;
  13285. }
  13286. } /* END wc_AesGcmDecrypt */
  13287. printf(resultFmt, gcmD == 0 ? passed : failed);
  13288. #endif /* HAVE_AES_DECRYPT */
  13289. wc_AesFree(&aes);
  13290. #endif
  13291. return ret;
  13292. } /* END test_wc_AesGcmEncryptDecrypt */
  13293. /*
  13294. * unit test for wc_GmacSetKey()
  13295. */
  13296. static int test_wc_GmacSetKey (void)
  13297. {
  13298. int ret = 0;
  13299. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  13300. Gmac gmac;
  13301. byte key16[] =
  13302. {
  13303. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13304. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  13305. };
  13306. #ifdef WOLFSSL_AES_192
  13307. byte key24[] =
  13308. {
  13309. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13310. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  13311. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  13312. };
  13313. #endif
  13314. #ifdef WOLFSSL_AES_256
  13315. byte key32[] =
  13316. {
  13317. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13318. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  13319. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13320. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  13321. };
  13322. #endif
  13323. byte badKey16[] =
  13324. {
  13325. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13326. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  13327. };
  13328. byte badKey24[] =
  13329. {
  13330. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  13331. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  13332. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  13333. };
  13334. byte badKey32[] =
  13335. {
  13336. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13337. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  13338. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  13339. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  13340. };
  13341. printf(testingFmt, "wc_GmacSetKey()");
  13342. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  13343. if (ret != 0)
  13344. return ret;
  13345. #ifdef WOLFSSL_AES_128
  13346. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  13347. #endif
  13348. #ifdef WOLFSSL_AES_192
  13349. if (ret == 0) {
  13350. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  13351. }
  13352. #endif
  13353. #ifdef WOLFSSL_AES_256
  13354. if (ret == 0) {
  13355. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  13356. }
  13357. #endif
  13358. /* Pass in bad args. */
  13359. if (ret == 0) {
  13360. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  13361. if (ret == BAD_FUNC_ARG) {
  13362. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  13363. }
  13364. if (ret == BAD_FUNC_ARG) {
  13365. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  13366. }
  13367. if (ret == BAD_FUNC_ARG) {
  13368. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  13369. }
  13370. if (ret == BAD_FUNC_ARG) {
  13371. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  13372. }
  13373. if (ret == BAD_FUNC_ARG) {
  13374. ret = 0;
  13375. } else {
  13376. ret = WOLFSSL_FATAL_ERROR;
  13377. }
  13378. }
  13379. wc_AesFree(&gmac.aes);
  13380. printf(resultFmt, ret == 0 ? passed : failed);
  13381. #endif
  13382. return ret;
  13383. } /* END test_wc_GmacSetKey */
  13384. /*
  13385. * unit test for wc_GmacUpdate
  13386. */
  13387. static int test_wc_GmacUpdate (void)
  13388. {
  13389. int ret = 0;
  13390. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  13391. Gmac gmac;
  13392. #ifdef WOLFSSL_AES_128
  13393. const byte key16[] =
  13394. {
  13395. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  13396. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  13397. };
  13398. #endif
  13399. #ifdef WOLFSSL_AES_192
  13400. byte key24[] =
  13401. {
  13402. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  13403. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  13404. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  13405. };
  13406. #endif
  13407. #ifdef WOLFSSL_AES_256
  13408. byte key32[] =
  13409. {
  13410. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  13411. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  13412. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  13413. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  13414. };
  13415. #endif
  13416. #ifdef WOLFSSL_AES_128
  13417. const byte authIn[] =
  13418. {
  13419. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  13420. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  13421. };
  13422. #endif
  13423. #ifdef WOLFSSL_AES_192
  13424. const byte authIn2[] =
  13425. {
  13426. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  13427. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  13428. };
  13429. #endif
  13430. const byte authIn3[] =
  13431. {
  13432. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  13433. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  13434. };
  13435. #ifdef WOLFSSL_AES_128
  13436. const byte tag1[] = /* Known. */
  13437. {
  13438. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  13439. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  13440. };
  13441. #endif
  13442. #ifdef WOLFSSL_AES_192
  13443. const byte tag2[] = /* Known */
  13444. {
  13445. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  13446. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  13447. };
  13448. #endif
  13449. const byte tag3[] = /* Known */
  13450. {
  13451. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  13452. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  13453. };
  13454. #ifdef WOLFSSL_AES_128
  13455. const byte iv[] =
  13456. {
  13457. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  13458. 0xe2, 0x8c, 0x8f, 0x16
  13459. };
  13460. #endif
  13461. #ifdef WOLFSSL_AES_192
  13462. const byte iv2[] =
  13463. {
  13464. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  13465. 0x7e, 0x1a, 0x6f, 0xbc
  13466. };
  13467. #endif
  13468. const byte iv3[] =
  13469. {
  13470. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  13471. 0xc3, 0xfb, 0x6c, 0x8a
  13472. };
  13473. byte tagOut[16];
  13474. byte tagOut2[24];
  13475. byte tagOut3[32];
  13476. /* Init stack variables. */
  13477. XMEMSET(tagOut, 0, sizeof(tagOut));
  13478. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  13479. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  13480. printf(testingFmt, "wc_GmacUpdate()");
  13481. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  13482. if (ret != 0)
  13483. return ret;
  13484. #ifdef WOLFSSL_AES_128
  13485. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  13486. if (ret == 0) {
  13487. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  13488. tagOut, sizeof(tag1));
  13489. if (ret == 0) {
  13490. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  13491. }
  13492. }
  13493. #endif
  13494. #ifdef WOLFSSL_AES_192
  13495. if (ret == 0) {
  13496. XMEMSET(&gmac, 0, sizeof(Gmac));
  13497. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  13498. }
  13499. if (ret == 0) {
  13500. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  13501. sizeof(authIn2), tagOut2, sizeof(tag2));
  13502. }
  13503. if (ret == 0) {
  13504. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  13505. }
  13506. #endif
  13507. #ifdef WOLFSSL_AES_256
  13508. if (ret == 0) {
  13509. XMEMSET(&gmac, 0, sizeof(Gmac));
  13510. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  13511. }
  13512. if (ret == 0) {
  13513. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  13514. sizeof(authIn3), tagOut3, sizeof(tag3));
  13515. }
  13516. if (ret == 0) {
  13517. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  13518. }
  13519. #endif
  13520. /*Pass bad args. */
  13521. if (ret == 0) {
  13522. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  13523. sizeof(authIn3), tagOut3, sizeof(tag3));
  13524. if (ret == BAD_FUNC_ARG) {
  13525. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  13526. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  13527. }
  13528. if (ret == BAD_FUNC_ARG) {
  13529. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  13530. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  13531. }
  13532. if (ret == BAD_FUNC_ARG) {
  13533. ret = 0;
  13534. } else {
  13535. ret = WOLFSSL_FATAL_ERROR;
  13536. }
  13537. }
  13538. wc_AesFree(&gmac.aes);
  13539. printf(resultFmt, ret == 0 ? passed : failed);
  13540. #endif
  13541. return ret;
  13542. } /* END test_wc_GmacUpdate */
  13543. /*
  13544. * testing wc_CamelliaSetKey
  13545. */
  13546. static int test_wc_CamelliaSetKey (void)
  13547. {
  13548. int ret = 0;
  13549. #ifdef HAVE_CAMELLIA
  13550. Camellia camellia;
  13551. /*128-bit key*/
  13552. static const byte key16[] =
  13553. {
  13554. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  13555. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  13556. };
  13557. /* 192-bit key */
  13558. static const byte key24[] =
  13559. {
  13560. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  13561. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  13562. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  13563. };
  13564. /* 256-bit key */
  13565. static const byte key32[] =
  13566. {
  13567. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  13568. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  13569. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  13570. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  13571. };
  13572. static const byte iv[] =
  13573. {
  13574. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13575. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  13576. };
  13577. printf(testingFmt, "wc_CamelliaSetKey()");
  13578. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  13579. if (ret == 0) {
  13580. ret = wc_CamelliaSetKey(&camellia, key16,
  13581. (word32)sizeof(key16), NULL);
  13582. if (ret == 0) {
  13583. ret = wc_CamelliaSetKey(&camellia, key24,
  13584. (word32)sizeof(key24), iv);
  13585. }
  13586. if (ret == 0) {
  13587. ret = wc_CamelliaSetKey(&camellia, key24,
  13588. (word32)sizeof(key24), NULL);
  13589. }
  13590. if (ret == 0) {
  13591. ret = wc_CamelliaSetKey(&camellia, key32,
  13592. (word32)sizeof(key32), iv);
  13593. }
  13594. if (ret == 0) {
  13595. ret = wc_CamelliaSetKey(&camellia, key32,
  13596. (word32)sizeof(key32), NULL);
  13597. }
  13598. }
  13599. /* Bad args. */
  13600. if (ret == 0) {
  13601. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  13602. if (ret != BAD_FUNC_ARG) {
  13603. ret = WOLFSSL_FATAL_ERROR;
  13604. } else {
  13605. ret = 0;
  13606. }
  13607. } /* END bad args. */
  13608. #endif
  13609. return ret;
  13610. } /* END test_wc_CammeliaSetKey */
  13611. /*
  13612. * Testing wc_CamelliaSetIV()
  13613. */
  13614. static int test_wc_CamelliaSetIV (void)
  13615. {
  13616. int ret = 0;
  13617. #ifdef HAVE_CAMELLIA
  13618. Camellia camellia;
  13619. static const byte iv[] =
  13620. {
  13621. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13622. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  13623. };
  13624. printf(testingFmt, "wc_CamelliaSetIV()");
  13625. ret = wc_CamelliaSetIV(&camellia, iv);
  13626. if (ret == 0) {
  13627. ret = wc_CamelliaSetIV(&camellia, NULL);
  13628. }
  13629. /* Bad args. */
  13630. if (ret == 0) {
  13631. ret = wc_CamelliaSetIV(NULL, NULL);
  13632. if (ret != BAD_FUNC_ARG) {
  13633. ret = WOLFSSL_FATAL_ERROR;
  13634. } else {
  13635. ret = 0;
  13636. }
  13637. }
  13638. printf(resultFmt, ret == 0 ? passed : failed);
  13639. #endif
  13640. return ret;
  13641. } /*END test_wc_CamelliaSetIV*/
  13642. /*
  13643. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  13644. */
  13645. static int test_wc_CamelliaEncryptDecryptDirect (void)
  13646. {
  13647. int ret = 0;
  13648. #ifdef HAVE_CAMELLIA
  13649. Camellia camellia;
  13650. static const byte key24[] =
  13651. {
  13652. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  13653. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  13654. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  13655. };
  13656. static const byte iv[] =
  13657. {
  13658. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  13659. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  13660. };
  13661. static const byte plainT[] =
  13662. {
  13663. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  13664. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  13665. };
  13666. byte enc[sizeof(plainT)];
  13667. byte dec[sizeof(enc)];
  13668. int camE = WOLFSSL_FATAL_ERROR;
  13669. int camD = WOLFSSL_FATAL_ERROR;
  13670. /*Init stack variables.*/
  13671. XMEMSET(enc, 0, 16);
  13672. XMEMSET(enc, 0, 16);
  13673. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  13674. if (ret == 0) {
  13675. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  13676. if (ret == 0) {
  13677. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  13678. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  13679. ret = WOLFSSL_FATAL_ERROR;
  13680. }
  13681. }
  13682. }
  13683. printf(testingFmt, "wc_CamelliaEncryptDirect()");
  13684. /* Pass bad args. */
  13685. if (ret == 0) {
  13686. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  13687. if (camE == BAD_FUNC_ARG) {
  13688. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  13689. }
  13690. if (camE == BAD_FUNC_ARG) {
  13691. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  13692. }
  13693. if (camE == BAD_FUNC_ARG) {
  13694. camE = 0;
  13695. } else {
  13696. camE = WOLFSSL_FATAL_ERROR;
  13697. }
  13698. }
  13699. printf(resultFmt, camE == 0 ? passed : failed);
  13700. if (camE != 0) {
  13701. return camE;
  13702. }
  13703. printf(testingFmt, "wc_CamelliaDecryptDirect()");
  13704. if (ret == 0) {
  13705. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  13706. if (camD == BAD_FUNC_ARG) {
  13707. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  13708. }
  13709. if (camD == BAD_FUNC_ARG) {
  13710. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  13711. }
  13712. if (camD == BAD_FUNC_ARG) {
  13713. camD = 0;
  13714. } else {
  13715. camD = WOLFSSL_FATAL_ERROR;
  13716. }
  13717. }
  13718. printf(resultFmt, camD == 0 ? passed : failed);
  13719. if (camD != 0) {
  13720. return camD;
  13721. }
  13722. #endif
  13723. return ret;
  13724. } /* END test-wc_CamelliaEncryptDecryptDirect */
  13725. /*
  13726. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  13727. */
  13728. static int test_wc_CamelliaCbcEncryptDecrypt (void)
  13729. {
  13730. int ret = 0;
  13731. #ifdef HAVE_CAMELLIA
  13732. Camellia camellia;
  13733. static const byte key24[] =
  13734. {
  13735. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  13736. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  13737. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  13738. };
  13739. static const byte plainT[] =
  13740. {
  13741. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  13742. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  13743. };
  13744. byte enc[CAMELLIA_BLOCK_SIZE];
  13745. byte dec[CAMELLIA_BLOCK_SIZE];
  13746. int camCbcE = WOLFSSL_FATAL_ERROR;
  13747. int camCbcD = WOLFSSL_FATAL_ERROR;
  13748. /* Init stack variables. */
  13749. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  13750. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  13751. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  13752. if (ret == 0) {
  13753. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  13754. if (ret != 0) {
  13755. ret = WOLFSSL_FATAL_ERROR;
  13756. }
  13757. }
  13758. if (ret == 0) {
  13759. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  13760. if (ret == 0) {
  13761. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  13762. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  13763. ret = WOLFSSL_FATAL_ERROR;
  13764. }
  13765. }
  13766. }
  13767. printf(testingFmt, "wc_CamelliaCbcEncrypt");
  13768. /* Pass in bad args. */
  13769. if (ret == 0) {
  13770. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  13771. if (camCbcE == BAD_FUNC_ARG) {
  13772. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  13773. CAMELLIA_BLOCK_SIZE);
  13774. }
  13775. if (camCbcE == BAD_FUNC_ARG) {
  13776. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  13777. CAMELLIA_BLOCK_SIZE);
  13778. }
  13779. if (camCbcE == BAD_FUNC_ARG) {
  13780. camCbcE = 0;
  13781. } else {
  13782. camCbcE = WOLFSSL_FATAL_ERROR;
  13783. }
  13784. }
  13785. printf(resultFmt, camCbcE == 0 ? passed : failed);
  13786. if (camCbcE != 0) {
  13787. return camCbcE;
  13788. }
  13789. printf(testingFmt, "wc_CamelliaCbcDecrypt()");
  13790. if (ret == 0) {
  13791. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  13792. if (camCbcD == BAD_FUNC_ARG) {
  13793. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  13794. CAMELLIA_BLOCK_SIZE);
  13795. }
  13796. if (camCbcD == BAD_FUNC_ARG) {
  13797. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  13798. CAMELLIA_BLOCK_SIZE);
  13799. }
  13800. if (camCbcD == BAD_FUNC_ARG) {
  13801. camCbcD = 0;
  13802. } else {
  13803. camCbcD = WOLFSSL_FATAL_ERROR;
  13804. }
  13805. } /* END bad args. */
  13806. printf(resultFmt, camCbcD == 0 ? passed : failed);
  13807. if (camCbcD != 0) {
  13808. return camCbcD;
  13809. }
  13810. #endif
  13811. return ret;
  13812. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  13813. /*
  13814. * Testing wc_RabbitSetKey()
  13815. */
  13816. static int test_wc_RabbitSetKey (void)
  13817. {
  13818. int ret = 0;
  13819. #ifndef NO_RABBIT
  13820. Rabbit rabbit;
  13821. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  13822. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  13823. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  13824. printf(testingFmt, "wc_RabbitSetKey()");
  13825. ret = wc_RabbitSetKey(&rabbit, (byte*)key, (byte*)iv);
  13826. /* Test bad args. */
  13827. if (ret == 0) {
  13828. ret = wc_RabbitSetKey(NULL, (byte*)key, (byte*)iv);
  13829. if (ret == BAD_FUNC_ARG) {
  13830. ret = wc_RabbitSetKey(&rabbit, NULL, (byte*)iv);
  13831. }
  13832. if (ret == BAD_FUNC_ARG) {
  13833. ret = wc_RabbitSetKey(&rabbit, (byte*)key, NULL);
  13834. }
  13835. }
  13836. printf(resultFmt, ret == 0 ? passed : failed);
  13837. #endif
  13838. return ret;
  13839. } /* END test_wc_RabbitSetKey */
  13840. /*
  13841. * Test wc_RabbitProcess()
  13842. */
  13843. static int test_wc_RabbitProcess (void)
  13844. {
  13845. int ret = 0;
  13846. #if !defined(NO_RABBIT) && !defined(BIG_ENDIAN_ORDER)
  13847. Rabbit enc, dec;
  13848. byte cipher[25];
  13849. byte plain[25];
  13850. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  13851. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  13852. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  13853. const char* input = TEST_STRING;
  13854. unsigned long int inlen = (unsigned long int)TEST_STRING_SZ;
  13855. /* Initialize stack variables. */
  13856. XMEMSET(cipher, 0, sizeof(cipher));
  13857. XMEMSET(plain, 0, sizeof(plain));
  13858. printf(testingFmt, "wc_RabbitProcess()");
  13859. ret = wc_RabbitSetKey(&enc, (byte*)key, (byte*)iv);
  13860. if (ret == 0) {
  13861. ret = wc_RabbitSetKey(&dec, (byte*)key, (byte*)iv);
  13862. }
  13863. if (ret == 0) {
  13864. ret = wc_RabbitProcess(&enc, cipher, (byte*)input, (word32)inlen);
  13865. }
  13866. if (ret == 0) {
  13867. ret = wc_RabbitProcess(&dec, plain, cipher, (word32)inlen);
  13868. if (ret != 0 || XMEMCMP(input, plain, inlen)) {
  13869. ret = WOLFSSL_FATAL_ERROR;
  13870. } else {
  13871. ret = 0;
  13872. }
  13873. }
  13874. /* Test bad args. */
  13875. if (ret == 0) {
  13876. ret = wc_RabbitProcess(NULL, plain, cipher, (word32)inlen);
  13877. if (ret == BAD_FUNC_ARG) {
  13878. ret = wc_RabbitProcess(&dec, NULL, cipher, (word32)inlen);
  13879. }
  13880. if (ret == BAD_FUNC_ARG) {
  13881. ret = wc_RabbitProcess(&dec, plain, NULL, (word32)inlen);
  13882. }
  13883. if (ret == BAD_FUNC_ARG) {
  13884. ret = 0;
  13885. } else {
  13886. ret = WOLFSSL_FATAL_ERROR;
  13887. }
  13888. }
  13889. printf(resultFmt, ret == 0 ? passed : failed);
  13890. #endif
  13891. return ret;
  13892. } /* END test_wc_RabbitProcess */
  13893. /*
  13894. * Testing wc_Arc4SetKey()
  13895. */
  13896. static int test_wc_Arc4SetKey (void)
  13897. {
  13898. int ret = 0;
  13899. #ifndef NO_RC4
  13900. Arc4 arc;
  13901. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  13902. int keyLen = 8;
  13903. printf(testingFmt, "wc_Arch4SetKey()");
  13904. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  13905. /* Test bad args. */
  13906. if (ret == 0) {
  13907. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  13908. if (ret == BAD_FUNC_ARG)
  13909. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  13910. if (ret == BAD_FUNC_ARG)
  13911. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  13912. if (ret == BAD_FUNC_ARG)
  13913. ret = WOLFSSL_ERROR_NONE;
  13914. else
  13915. ret = WOLFSSL_FATAL_ERROR;
  13916. } /* END test bad args. */
  13917. printf(resultFmt, ret == 0 ? passed : failed);
  13918. #endif
  13919. return ret;
  13920. } /* END test_wc_Arc4SetKey */
  13921. /*
  13922. * Testing wc_Arc4Process for ENC/DEC.
  13923. */
  13924. static int test_wc_Arc4Process (void)
  13925. {
  13926. int ret = 0;
  13927. #ifndef NO_RC4
  13928. Arc4 enc, dec;
  13929. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  13930. int keyLen = 8;
  13931. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  13932. byte cipher[8];
  13933. byte plain[8];
  13934. /* Init stack variables */
  13935. XMEMSET(cipher, 0, sizeof(cipher));
  13936. XMEMSET(plain, 0, sizeof(plain));
  13937. /* Use for async. */
  13938. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  13939. if (ret == 0) {
  13940. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  13941. }
  13942. printf(testingFmt, "wc_Arc4Process()");
  13943. if (ret == 0) {
  13944. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  13945. }
  13946. if (ret == 0) {
  13947. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  13948. }
  13949. if (ret == 0) {
  13950. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  13951. }
  13952. if (ret == 0) {
  13953. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  13954. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  13955. ret = WOLFSSL_FATAL_ERROR;
  13956. } else {
  13957. ret = 0;
  13958. }
  13959. }
  13960. /* Bad args. */
  13961. if (ret == 0) {
  13962. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  13963. if (ret == BAD_FUNC_ARG) {
  13964. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  13965. }
  13966. if (ret == BAD_FUNC_ARG) {
  13967. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  13968. }
  13969. if (ret == BAD_FUNC_ARG) {
  13970. ret = 0;
  13971. } else {
  13972. ret = WOLFSSL_FATAL_ERROR;
  13973. }
  13974. }
  13975. printf(resultFmt, ret == 0 ? passed : failed);
  13976. wc_Arc4Free(&enc);
  13977. wc_Arc4Free(&dec);
  13978. #endif
  13979. return ret;
  13980. }/* END test_wc_Arc4Process */
  13981. /*
  13982. * Testing wc_Init RsaKey()
  13983. */
  13984. static int test_wc_InitRsaKey (void)
  13985. {
  13986. int ret = 0;
  13987. #ifndef NO_RSA
  13988. RsaKey key;
  13989. printf(testingFmt, "wc_InitRsaKey()");
  13990. ret = wc_InitRsaKey(&key, NULL);
  13991. /* Test bad args. */
  13992. if (ret == 0) {
  13993. ret = wc_InitRsaKey(NULL, NULL);
  13994. #ifndef HAVE_USER_RSA
  13995. if (ret == BAD_FUNC_ARG) {
  13996. ret = 0;
  13997. } else {
  13998. #else
  13999. if (ret == USER_CRYPTO_ERROR) {
  14000. ret = 0;
  14001. } else {
  14002. #endif
  14003. ret = WOLFSSL_FATAL_ERROR;
  14004. }
  14005. } /* end if */
  14006. if (wc_FreeRsaKey(&key) || ret != 0) {
  14007. ret = WOLFSSL_FATAL_ERROR;
  14008. }
  14009. printf(resultFmt, ret == 0 ? passed : failed);
  14010. #endif
  14011. return ret;
  14012. } /* END test_wc_InitRsaKey */
  14013. /*
  14014. * Testing wc_RsaPrivateKeyDecode()
  14015. */
  14016. static int test_wc_RsaPrivateKeyDecode (void)
  14017. {
  14018. int ret = 0;
  14019. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  14020. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  14021. RsaKey key;
  14022. byte* tmp;
  14023. word32 idx = 0;
  14024. int bytes = 0;
  14025. printf(testingFmt, "wc_RsaPrivateKeyDecode()");
  14026. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  14027. if (tmp == NULL) {
  14028. ret = WOLFSSL_FATAL_ERROR;
  14029. }
  14030. if (ret == 0) {
  14031. ret = wc_InitRsaKey(&key, NULL);
  14032. }
  14033. if (ret == 0) {
  14034. #ifdef USE_CERT_BUFFERS_1024
  14035. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  14036. bytes = sizeof_client_key_der_1024;
  14037. #else
  14038. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  14039. bytes = sizeof_client_key_der_2048;
  14040. #endif /* Use cert buffers. */
  14041. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  14042. }
  14043. #ifndef HAVE_USER_RSA
  14044. /* Test bad args. */
  14045. if (ret == 0) {
  14046. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  14047. if (ret == BAD_FUNC_ARG) {
  14048. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  14049. }
  14050. if (ret == BAD_FUNC_ARG) {
  14051. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  14052. }
  14053. if (ret == BAD_FUNC_ARG) {
  14054. ret = 0;
  14055. } else {
  14056. ret = WOLFSSL_FATAL_ERROR;
  14057. }
  14058. }
  14059. #else
  14060. /* Test bad args. User RSA. */
  14061. if (ret == 0) {
  14062. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  14063. if (ret == USER_CRYPTO_ERROR) {
  14064. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  14065. }
  14066. if (ret == USER_CRYPTO_ERROR) {
  14067. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  14068. }
  14069. if (ret == USER_CRYPTO_ERROR) {
  14070. ret = 0;
  14071. } else {
  14072. ret = WOLFSSL_FATAL_ERROR;
  14073. }
  14074. }
  14075. #endif
  14076. if (tmp != NULL) {
  14077. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  14078. }
  14079. if (wc_FreeRsaKey(&key) || ret != 0) {
  14080. ret = WOLFSSL_FATAL_ERROR;
  14081. }
  14082. printf(resultFmt, ret == 0 ? passed : failed);
  14083. #endif
  14084. return ret;
  14085. } /* END test_wc_RsaPrivateKeyDecode */
  14086. /*
  14087. * Testing wc_RsaPublicKeyDecode()
  14088. */
  14089. static int test_wc_RsaPublicKeyDecode (void)
  14090. {
  14091. int ret = 0;
  14092. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  14093. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  14094. RsaKey keyPub;
  14095. byte* tmp;
  14096. word32 idx = 0;
  14097. int bytes = 0;
  14098. word32 keySz = 0;
  14099. word32 tstKeySz = 0;
  14100. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  14101. if (tmp == NULL) {
  14102. ret = WOLFSSL_FATAL_ERROR;
  14103. }
  14104. if (ret == 0) {
  14105. ret = wc_InitRsaKey(&keyPub, NULL);
  14106. }
  14107. if (ret == 0) {
  14108. #ifdef USE_CERT_BUFFERS_1024
  14109. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14110. bytes = sizeof_client_keypub_der_1024;
  14111. keySz = 1024;
  14112. #else
  14113. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14114. bytes = sizeof_client_keypub_der_2048;
  14115. keySz = 2048;
  14116. #endif
  14117. printf(testingFmt, "wc_RsaPublicKeyDecode()");
  14118. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  14119. }
  14120. #ifndef HAVE_USER_RSA
  14121. /* Pass in bad args. */
  14122. if (ret == 0) {
  14123. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  14124. if (ret == BAD_FUNC_ARG) {
  14125. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  14126. }
  14127. if (ret == BAD_FUNC_ARG) {
  14128. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  14129. }
  14130. if (ret == BAD_FUNC_ARG) {
  14131. ret = 0;
  14132. } else {
  14133. ret = WOLFSSL_FATAL_ERROR;
  14134. }
  14135. }
  14136. #else
  14137. /* Pass in bad args. */
  14138. if (ret == 0) {
  14139. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  14140. if (ret == USER_CRYPTO_ERROR) {
  14141. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  14142. }
  14143. if (ret == USER_CRYPTO_ERROR) {
  14144. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  14145. }
  14146. if (ret == USER_CRYPTO_ERROR) {
  14147. ret = 0;
  14148. } else {
  14149. ret = WOLFSSL_FATAL_ERROR;
  14150. }
  14151. }
  14152. #endif
  14153. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  14154. ret = WOLFSSL_FATAL_ERROR;
  14155. }
  14156. if (ret == 0) {
  14157. /* Test for getting modulus key size */
  14158. idx = 0;
  14159. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  14160. &tstKeySz, NULL, NULL);
  14161. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  14162. }
  14163. if (tmp != NULL) {
  14164. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  14165. }
  14166. printf(resultFmt, ret == 0 ? passed : failed);
  14167. #endif
  14168. return ret;
  14169. } /* END test_wc_RsaPublicKeyDecode */
  14170. /*
  14171. * Testing wc_RsaPublicKeyDecodeRaw()
  14172. */
  14173. static int test_wc_RsaPublicKeyDecodeRaw (void)
  14174. {
  14175. int ret = 0;
  14176. #if !defined(NO_RSA)
  14177. RsaKey key;
  14178. const byte n = 0x23;
  14179. const byte e = 0x03;
  14180. int nSz = sizeof(n);
  14181. int eSz = sizeof(e);
  14182. printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
  14183. ret = wc_InitRsaKey(&key, NULL);
  14184. if (ret == 0) {
  14185. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  14186. }
  14187. #ifndef HAVE_USER_RSA
  14188. /* Pass in bad args. */
  14189. if (ret == 0) {
  14190. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  14191. if (ret == BAD_FUNC_ARG) {
  14192. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  14193. }
  14194. if (ret == BAD_FUNC_ARG) {
  14195. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  14196. }
  14197. if (ret == BAD_FUNC_ARG) {
  14198. ret = 0;
  14199. } else {
  14200. ret = WOLFSSL_FATAL_ERROR;
  14201. }
  14202. }
  14203. #else
  14204. /* Pass in bad args. User RSA. */
  14205. if (ret == 0) {
  14206. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  14207. if (ret == USER_CRYPTO_ERROR) {
  14208. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  14209. }
  14210. if (ret == USER_CRYPTO_ERROR) {
  14211. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  14212. }
  14213. if (ret == USER_CRYPTO_ERROR) {
  14214. ret = 0;
  14215. } else {
  14216. ret = WOLFSSL_FATAL_ERROR;
  14217. }
  14218. }
  14219. #endif
  14220. if (wc_FreeRsaKey(&key) || ret != 0) {
  14221. ret = WOLFSSL_FATAL_ERROR;
  14222. }
  14223. printf(resultFmt, ret == 0 ? passed : failed);
  14224. #endif
  14225. return ret;
  14226. } /* END test_wc_RsaPublicKeyDecodeRaw */
  14227. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  14228. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  14229. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  14230. * trying until it gets a probable prime. */
  14231. #ifdef HAVE_FIPS
  14232. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  14233. {
  14234. int ret;
  14235. for (;;) {
  14236. ret = wc_MakeRsaKey(key, size, e, rng);
  14237. if (ret != PRIME_GEN_E) break;
  14238. printf("MakeRsaKey couldn't find prime; trying again.\n");
  14239. }
  14240. return ret;
  14241. }
  14242. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  14243. #else
  14244. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  14245. #endif
  14246. #endif
  14247. /*
  14248. * Testing wc_MakeRsaKey()
  14249. */
  14250. static int test_wc_MakeRsaKey (void)
  14251. {
  14252. int ret = 0;
  14253. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  14254. RsaKey genKey;
  14255. WC_RNG rng;
  14256. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  14257. int bits = 1024;
  14258. #else
  14259. int bits = 2048;
  14260. #endif
  14261. printf(testingFmt, "wc_MakeRsaKey()");
  14262. ret = wc_InitRsaKey(&genKey, NULL);
  14263. if (ret == 0) {
  14264. ret = wc_InitRng(&rng);
  14265. if (ret == 0) {
  14266. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  14267. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  14268. ret = WOLFSSL_FATAL_ERROR;
  14269. }
  14270. }
  14271. }
  14272. #ifndef HAVE_USER_RSA
  14273. /* Test bad args. */
  14274. if (ret == 0) {
  14275. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  14276. if (ret == BAD_FUNC_ARG) {
  14277. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  14278. }
  14279. if (ret == BAD_FUNC_ARG) {
  14280. /* e < 3 */
  14281. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  14282. }
  14283. if (ret == BAD_FUNC_ARG) {
  14284. /* e & 1 == 0 */
  14285. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  14286. }
  14287. if (ret == BAD_FUNC_ARG) {
  14288. ret = 0;
  14289. } else {
  14290. ret = WOLFSSL_FATAL_ERROR;
  14291. }
  14292. }
  14293. #else
  14294. /* Test bad args. */
  14295. if (ret == 0) {
  14296. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  14297. if (ret == USER_CRYPTO_ERROR) {
  14298. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  14299. }
  14300. if (ret == USER_CRYPTO_ERROR) {
  14301. /* e < 3 */
  14302. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  14303. }
  14304. if (ret == USER_CRYPTO_ERROR) {
  14305. /* e & 1 == 0 */
  14306. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  14307. }
  14308. if (ret == USER_CRYPTO_ERROR) {
  14309. ret = 0;
  14310. } else {
  14311. ret = WOLFSSL_FATAL_ERROR;
  14312. }
  14313. }
  14314. #endif
  14315. if (wc_FreeRng(&rng) || ret != 0) {
  14316. ret = WOLFSSL_FATAL_ERROR;
  14317. }
  14318. printf(resultFmt, ret == 0 ? passed : failed);
  14319. #endif
  14320. return ret;
  14321. } /* END test_wc_MakeRsaKey */
  14322. /*
  14323. * Test the bounds checking on the cipher text versus the key modulus.
  14324. * 1. Make a new RSA key.
  14325. * 2. Set c to 1.
  14326. * 3. Decrypt c into k. (error)
  14327. * 4. Copy the key modulus to c and sub 1 from the copy.
  14328. * 5. Decrypt c into k. (error)
  14329. * Valid bounds test cases are covered by all the other RSA tests.
  14330. */
  14331. static int test_RsaDecryptBoundsCheck(void)
  14332. {
  14333. int ret = 0;
  14334. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  14335. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  14336. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  14337. RsaKey key;
  14338. byte flatC[256];
  14339. word32 flatCSz;
  14340. byte out[256];
  14341. word32 outSz = sizeof(out);
  14342. WC_RNG rng;
  14343. printf(testingFmt, "RSA decrypt bounds check");
  14344. XMEMSET(&rng, 0, sizeof(rng));
  14345. ret = wc_InitRng(&rng);
  14346. if (ret == 0)
  14347. ret = wc_InitRsaKey(&key, NULL);
  14348. if (ret == 0) {
  14349. const byte* derKey;
  14350. word32 derKeySz;
  14351. word32 idx = 0;
  14352. #ifdef USE_CERT_BUFFERS_1024
  14353. derKey = server_key_der_1024;
  14354. derKeySz = (word32)sizeof_server_key_der_1024;
  14355. flatCSz = 128;
  14356. #else
  14357. derKey = server_key_der_2048;
  14358. derKeySz = (word32)sizeof_server_key_der_2048;
  14359. flatCSz = 256;
  14360. #endif
  14361. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  14362. }
  14363. if (ret == 0) {
  14364. XMEMSET(flatC, 0, flatCSz);
  14365. flatC[flatCSz-1] = 1;
  14366. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  14367. RSA_PRIVATE_DECRYPT, &rng);
  14368. }
  14369. if (ret == RSA_OUT_OF_RANGE_E) {
  14370. mp_int c;
  14371. mp_init_copy(&c, &key.n);
  14372. mp_sub_d(&c, 1, &c);
  14373. mp_to_unsigned_bin(&c, flatC);
  14374. ret = wc_RsaDirect(flatC, sizeof(flatC), out, &outSz, &key,
  14375. RSA_PRIVATE_DECRYPT, NULL);
  14376. mp_clear(&c);
  14377. }
  14378. if (ret == RSA_OUT_OF_RANGE_E)
  14379. ret = 0;
  14380. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  14381. ret = WOLFSSL_FATAL_ERROR;
  14382. printf(resultFmt, ret == 0 ? passed : failed);
  14383. #endif
  14384. return ret;
  14385. } /* END test_wc_RsaDecryptBoundsCheck */
  14386. /*
  14387. * Testing wc_SetKeyUsage()
  14388. */
  14389. static int test_wc_SetKeyUsage (void)
  14390. {
  14391. int ret = 0;
  14392. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  14393. Cert myCert;
  14394. ret = wc_InitCert(&myCert);
  14395. printf(testingFmt, "wc_SetKeyUsage()");
  14396. if (ret == 0) {
  14397. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  14398. if (ret == 0) {
  14399. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  14400. }
  14401. if (ret == 0) {
  14402. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  14403. }
  14404. if (ret == 0) {
  14405. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  14406. }
  14407. if (ret == 0) {
  14408. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  14409. }
  14410. }
  14411. /* Test bad args. */
  14412. if (ret == 0) {
  14413. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  14414. if (ret == BAD_FUNC_ARG) {
  14415. ret = wc_SetKeyUsage(&myCert, NULL);
  14416. }
  14417. if (ret == BAD_FUNC_ARG) {
  14418. ret = wc_SetKeyUsage(&myCert, "");
  14419. }
  14420. if (ret == KEYUSAGE_E) {
  14421. ret = wc_SetKeyUsage(&myCert, ",");
  14422. }
  14423. if (ret == KEYUSAGE_E) {
  14424. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  14425. }
  14426. if (ret == KEYUSAGE_E) {
  14427. ret = 0;
  14428. } else {
  14429. ret = WOLFSSL_FATAL_ERROR;
  14430. }
  14431. }
  14432. printf(resultFmt, ret == 0 ? passed : failed);
  14433. #endif
  14434. return ret;
  14435. } /* END test_wc_SetKeyUsage */
  14436. /*
  14437. * Testing wc_CheckProbablePrime()
  14438. */
  14439. static int test_wc_CheckProbablePrime (void)
  14440. {
  14441. int ret = 0;
  14442. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  14443. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  14444. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  14445. RsaKey key;
  14446. WC_RNG rng;
  14447. byte e[3];
  14448. word32 eSz = (word32)sizeof(e);
  14449. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  14450. word32 nSz = (word32)sizeof(n);
  14451. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  14452. word32 dSz = (word32)sizeof(d);
  14453. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  14454. word32 pSz = (word32)sizeof(p);
  14455. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  14456. word32 qSz = (word32)sizeof(q);
  14457. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  14458. int* isPrime;
  14459. int test[5];
  14460. isPrime = test;
  14461. printf(testingFmt, "wc_CheckProbablePrime()");
  14462. ret = wc_InitRsaKey(&key, NULL);
  14463. if (ret == 0) {
  14464. ret = wc_InitRng(&rng);
  14465. }
  14466. if (ret == 0) {
  14467. ret = wc_RsaSetRNG(&key, &rng);
  14468. }
  14469. if (ret == 0) {
  14470. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  14471. }
  14472. if (ret == 0) {
  14473. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  14474. p, &pSz, q, &qSz);
  14475. }
  14476. /* Bad cases */
  14477. if (ret == 0) {
  14478. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  14479. nlen, isPrime);
  14480. if (ret == BAD_FUNC_ARG) {
  14481. ret = 0;
  14482. }
  14483. }
  14484. if (ret == 0) {
  14485. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  14486. nlen, isPrime);
  14487. if (ret == BAD_FUNC_ARG) {
  14488. ret = 0;
  14489. }
  14490. }
  14491. if (ret == 0) {
  14492. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  14493. nlen, isPrime);
  14494. if (ret == BAD_FUNC_ARG) {
  14495. ret = 0;
  14496. }
  14497. }
  14498. if (ret == 0) {
  14499. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  14500. nlen, isPrime);
  14501. if (ret == BAD_FUNC_ARG) {
  14502. ret = 0;
  14503. }
  14504. }
  14505. if (ret == 0) {
  14506. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  14507. nlen, isPrime);
  14508. if (ret == BAD_FUNC_ARG) {
  14509. ret = 0;
  14510. }
  14511. }
  14512. if (ret == 0) {
  14513. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  14514. nlen, isPrime);
  14515. if (ret == BAD_FUNC_ARG) {
  14516. ret = 0;
  14517. }
  14518. }
  14519. if (ret == 0) {
  14520. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  14521. nlen, isPrime);
  14522. if (ret == BAD_FUNC_ARG) {
  14523. ret = 0;
  14524. }
  14525. }
  14526. /* Good case */
  14527. if (ret == 0) {
  14528. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  14529. nlen, isPrime);
  14530. }
  14531. wc_FreeRsaKey(&key);
  14532. wc_FreeRng(&rng);
  14533. printf(resultFmt, ret == 0 ? passed : failed);
  14534. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  14535. #endif
  14536. return ret;
  14537. } /* END test_wc_CheckProbablePrime */
  14538. /*
  14539. * Testing wc_RsaPSS_Verify()
  14540. */
  14541. static int test_wc_RsaPSS_Verify (void)
  14542. {
  14543. int ret = 0;
  14544. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  14545. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  14546. RsaKey key;
  14547. WC_RNG rng;
  14548. int sz = 256;
  14549. byte* pt;
  14550. const char* szMessage = "This is the string to be signed";
  14551. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  14552. unsigned char pDecrypted[2048/8];
  14553. word32 outLen = sizeof(pDecrypted);
  14554. pt = pDecrypted;
  14555. printf(testingFmt, "wc_RsaPSS_Verify()");
  14556. ret = wc_InitRsaKey(&key, NULL);
  14557. if (ret == 0) {
  14558. ret = wc_InitRng(&rng);
  14559. }
  14560. if (ret == 0) {
  14561. ret = wc_RsaSetRNG(&key, &rng);
  14562. }
  14563. if (ret == 0) {
  14564. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  14565. }
  14566. if (ret == 0) {
  14567. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  14568. pSignature, sizeof(pSignature),
  14569. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  14570. if (ret > 0 ){
  14571. sz = ret;
  14572. ret = 0;
  14573. }
  14574. }
  14575. /* Bad cases */
  14576. if (ret == 0) {
  14577. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  14578. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14579. if (ret == BAD_FUNC_ARG) {
  14580. ret = 0;
  14581. }
  14582. }
  14583. if (ret == 0) {
  14584. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  14585. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14586. if (ret == BAD_FUNC_ARG) {
  14587. ret = 0;
  14588. }
  14589. }
  14590. if (ret == 0) {
  14591. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  14592. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14593. if (ret == BAD_FUNC_ARG) {
  14594. ret = 0;
  14595. }
  14596. }
  14597. if (ret == 0) {
  14598. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  14599. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14600. if (ret == BAD_FUNC_ARG) {
  14601. ret = 0;
  14602. }
  14603. }
  14604. /* Good case */
  14605. if (ret == 0) {
  14606. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  14607. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14608. if (ret > 0) {
  14609. ret = 0;
  14610. }
  14611. }
  14612. wc_FreeRsaKey(&key);
  14613. wc_FreeRng(&rng);
  14614. printf(resultFmt, ret == 0 ? passed : failed);
  14615. #endif
  14616. return ret;
  14617. } /* END test_wc_RsaPSS_Verify */
  14618. /*
  14619. * Testing wc_RsaPSS_VerifyCheck()
  14620. */
  14621. static int test_wc_RsaPSS_VerifyCheck (void)
  14622. {
  14623. int ret = 0;
  14624. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  14625. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  14626. RsaKey key;
  14627. WC_RNG rng;
  14628. int sz = 256; /* 2048/8 */
  14629. byte* pt;
  14630. byte digest[32];
  14631. word32 digestSz;
  14632. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  14633. word32 pSignatureSz = sizeof(pSignature);
  14634. unsigned char pDecrypted[2048/8];
  14635. word32 outLen = sizeof(pDecrypted);
  14636. pt = pDecrypted;
  14637. printf(testingFmt, "wc_RsaPSS_VerifyCheck()");
  14638. XMEMSET(digest, 0, sizeof(digest));
  14639. XMEMSET(pSignature, 0, sizeof(pSignature));
  14640. ret = wc_InitRsaKey(&key, NULL);
  14641. if (ret == 0) {
  14642. ret = wc_InitRng(&rng);
  14643. }
  14644. if (ret == 0) {
  14645. ret = wc_RsaSetRNG(&key, &rng);
  14646. }
  14647. if (ret == 0) {
  14648. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  14649. }
  14650. if (ret == 0) {
  14651. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  14652. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  14653. }
  14654. if (ret == 0) {
  14655. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  14656. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  14657. if (ret > 0 ){
  14658. sz = ret;
  14659. ret = 0;
  14660. }
  14661. }
  14662. /* Bad cases */
  14663. if (ret == 0) {
  14664. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  14665. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14666. if (ret == BAD_FUNC_ARG) {
  14667. ret = 0;
  14668. }
  14669. }
  14670. if (ret == 0) {
  14671. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  14672. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14673. if (ret == BAD_FUNC_ARG) {
  14674. ret = 0;
  14675. }
  14676. }
  14677. if (ret == 0) {
  14678. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  14679. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14680. if (ret == BAD_FUNC_ARG) {
  14681. ret = 0;
  14682. }
  14683. }
  14684. if (ret == 0) {
  14685. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  14686. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14687. if (ret == BAD_FUNC_ARG) {
  14688. ret = 0;
  14689. }
  14690. }
  14691. /* Good case */
  14692. if (ret == 0) {
  14693. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  14694. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14695. if (ret > 0) {
  14696. ret = 0;
  14697. }
  14698. }
  14699. wc_FreeRsaKey(&key);
  14700. wc_FreeRng(&rng);
  14701. printf(resultFmt, ret == 0 ? passed : failed);
  14702. #endif
  14703. return ret;
  14704. } /* END test_wc_RsaPSS_VerifyCheck */
  14705. /*
  14706. * Testing wc_RsaPSS_VerifyCheckInline()
  14707. */
  14708. static int test_wc_RsaPSS_VerifyCheckInline (void)
  14709. {
  14710. int ret = 0;
  14711. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  14712. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  14713. RsaKey key;
  14714. WC_RNG rng;
  14715. int sz = 256;
  14716. byte* pt;
  14717. byte digest[32];
  14718. word32 digestSz;
  14719. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  14720. unsigned char pDecrypted[2048/8];
  14721. pt = pDecrypted;
  14722. printf(testingFmt, "wc_RsaPSS_VerifyCheckInline()");
  14723. ret = wc_InitRsaKey(&key, NULL);
  14724. XMEMSET(digest, 0, sizeof(digest));
  14725. XMEMSET(pSignature, 0, sizeof(pSignature));
  14726. if (ret == 0) {
  14727. ret = wc_InitRng(&rng);
  14728. }
  14729. if (ret == 0) {
  14730. ret = wc_RsaSetRNG(&key, &rng);
  14731. }
  14732. if (ret == 0) {
  14733. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  14734. }
  14735. if (ret == 0) {
  14736. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  14737. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  14738. }
  14739. if (ret == 0) {
  14740. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  14741. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  14742. if (ret > 0 ){
  14743. sz = ret;
  14744. ret = 0;
  14745. }
  14746. }
  14747. /* Bad Cases */
  14748. if (ret == 0) {
  14749. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  14750. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14751. if (ret == BAD_FUNC_ARG) {
  14752. ret = 0;
  14753. }
  14754. }
  14755. if (ret == 0) {
  14756. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  14757. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14758. if (ret == BAD_FUNC_ARG) {
  14759. ret = 0;
  14760. }
  14761. }
  14762. if (ret == 0) {
  14763. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  14764. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14765. if (ret == BAD_FUNC_ARG) {
  14766. ret = 0;
  14767. }
  14768. }
  14769. if (ret == 0) {
  14770. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  14771. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  14772. if (ret == BAD_FUNC_ARG) {
  14773. ret = 0;
  14774. }
  14775. }
  14776. /* Good case */
  14777. if (ret == 0) {
  14778. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  14779. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  14780. if (ret > 0) {
  14781. ret = 0;
  14782. }
  14783. }
  14784. wc_FreeRsaKey(&key);
  14785. wc_FreeRng(&rng);
  14786. printf(resultFmt, ret == 0 ? passed : failed);
  14787. #endif
  14788. return ret;
  14789. } /* END test_wc_RsaPSS_VerifyCheckInline */
  14790. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  14791. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  14792. {
  14793. (void)flag;
  14794. (void)type;
  14795. (void)file;
  14796. (void)line;
  14797. }
  14798. #endif
  14799. /*
  14800. * Testing wc_LockMutex_ex
  14801. */
  14802. static int test_wc_LockMutex_ex (void)
  14803. {
  14804. int ret = 0;
  14805. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  14806. int flag = CRYPTO_LOCK;
  14807. int type = 0;
  14808. const char* file = "./test-LockMutex_ex.txt";
  14809. int line = 0;
  14810. printf(testingFmt, "wc_LockMutex_ex()");
  14811. /*without SetMutexCb*/
  14812. ret = wc_LockMutex_ex(flag, type, file, line);
  14813. if (ret == BAD_STATE_E) {
  14814. ret = 0;
  14815. }
  14816. /*with SetMutexCb*/
  14817. if (ret == 0) {
  14818. ret = wc_SetMutexCb(sample_mutex_cb);
  14819. if (ret == 0) {
  14820. ret = wc_LockMutex_ex(flag, type, file, line);
  14821. }
  14822. }
  14823. printf(resultFmt, ret == 0 ? passed : failed);
  14824. #endif
  14825. return ret;
  14826. }/*End test_wc_LockMutex_ex*/
  14827. /*
  14828. * Testing wc_SetMutexCb
  14829. */
  14830. static int test_wc_SetMutexCb (void)
  14831. {
  14832. int ret = 0;
  14833. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  14834. printf(testingFmt, "wc_SetMutexCb()");
  14835. ret = wc_SetMutexCb(sample_mutex_cb);
  14836. printf(resultFmt, ret == 0 ? passed : failed);
  14837. #endif
  14838. return ret;
  14839. }/*End test_wc_SetMutexCb*/
  14840. /*
  14841. * Testing wc_RsaKeyToDer()
  14842. */
  14843. static int test_wc_RsaKeyToDer (void)
  14844. {
  14845. int ret = 0;
  14846. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  14847. RsaKey genKey;
  14848. WC_RNG rng;
  14849. byte* der;
  14850. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  14851. int bits = 1024;
  14852. word32 derSz = 611;
  14853. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  14854. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  14855. #else
  14856. int bits = 2048;
  14857. word32 derSz = 1196;
  14858. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  14859. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  14860. #endif
  14861. XMEMSET(&rng, 0, sizeof(rng));
  14862. XMEMSET(&genKey, 0, sizeof(genKey));
  14863. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  14864. if (der == NULL) {
  14865. ret = WOLFSSL_FATAL_ERROR;
  14866. }
  14867. /* Init structures. */
  14868. if (ret == 0) {
  14869. ret = wc_InitRsaKey(&genKey, NULL);
  14870. }
  14871. if (ret == 0) {
  14872. ret = wc_InitRng(&rng);
  14873. }
  14874. /* Make key. */
  14875. if (ret == 0) {
  14876. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  14877. if (ret != 0) {
  14878. ret = WOLFSSL_FATAL_ERROR;
  14879. }
  14880. }
  14881. printf(testingFmt, "wc_RsaKeyToDer()");
  14882. if (ret == 0) {
  14883. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  14884. if (ret > 0) {
  14885. ret = 0;
  14886. } else {
  14887. ret = WOLFSSL_FATAL_ERROR;
  14888. }
  14889. }
  14890. #ifndef HAVE_USER_RSA
  14891. /* Pass good/bad args. */
  14892. if (ret == 0) {
  14893. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  14894. if (ret == BAD_FUNC_ARG) {
  14895. /* Get just the output length */
  14896. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  14897. }
  14898. if (ret > 0) {
  14899. /* Try Public Key. */
  14900. genKey.type = 0;
  14901. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  14902. }
  14903. if (ret == BAD_FUNC_ARG) {
  14904. ret = 0;
  14905. } else {
  14906. ret = WOLFSSL_FATAL_ERROR;
  14907. }
  14908. }
  14909. #else
  14910. /* Pass good/bad args. */
  14911. if (ret == 0) {
  14912. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  14913. if (ret == USER_CRYPTO_ERROR) {
  14914. /* Get just the output length */
  14915. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  14916. }
  14917. if (ret > 0) {
  14918. /* Try Public Key. */
  14919. genKey.type = 0;
  14920. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  14921. }
  14922. if (ret == USER_CRYPTO_ERROR) {
  14923. ret = 0;
  14924. } else {
  14925. ret = WOLFSSL_FATAL_ERROR;
  14926. }
  14927. }
  14928. #endif
  14929. if (der != NULL) {
  14930. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  14931. }
  14932. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  14933. ret = WOLFSSL_FATAL_ERROR;
  14934. }
  14935. if (wc_FreeRng(&rng) || ret != 0) {
  14936. ret = WOLFSSL_FATAL_ERROR;
  14937. }
  14938. printf(resultFmt, ret == 0 ? passed : failed);
  14939. #endif
  14940. return ret;
  14941. } /* END test_wc_RsaKeyToDer */
  14942. /*
  14943. * Testing wc_RsaKeyToPublicDer()
  14944. */
  14945. static int test_wc_RsaKeyToPublicDer (void)
  14946. {
  14947. int ret = 0;
  14948. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  14949. RsaKey key;
  14950. WC_RNG rng;
  14951. byte* der;
  14952. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  14953. int bits = 1024;
  14954. word32 derLen = 162;
  14955. #else
  14956. int bits = 2048;
  14957. word32 derLen = 294;
  14958. #endif
  14959. XMEMSET(&rng, 0, sizeof(rng));
  14960. XMEMSET(&key, 0, sizeof(key));
  14961. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  14962. if (der == NULL) {
  14963. ret = WOLFSSL_FATAL_ERROR;
  14964. }
  14965. if (ret == 0) {
  14966. ret = wc_InitRsaKey(&key, NULL);
  14967. }
  14968. if (ret == 0) {
  14969. ret = wc_InitRng(&rng);
  14970. }
  14971. if (ret == 0) {
  14972. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  14973. }
  14974. printf(testingFmt, "wc_RsaKeyToPublicDer()");
  14975. if (ret == 0) {
  14976. /* test getting size only */
  14977. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  14978. if (ret >= 0)
  14979. ret = 0;
  14980. }
  14981. if (ret == 0) {
  14982. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  14983. if (ret >= 0) {
  14984. ret = 0;
  14985. } else {
  14986. ret = WOLFSSL_FATAL_ERROR;
  14987. }
  14988. }
  14989. if (ret == 0) {
  14990. /* test getting size only */
  14991. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  14992. if (ret >= 0)
  14993. ret = 0;
  14994. }
  14995. if (ret == 0) {
  14996. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  14997. if (ret >= 0) {
  14998. ret = 0;
  14999. } else {
  15000. ret = WOLFSSL_FATAL_ERROR;
  15001. }
  15002. }
  15003. #ifndef HAVE_USER_RSA
  15004. /* Pass in bad args. */
  15005. if (ret == 0) {
  15006. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  15007. if (ret == BAD_FUNC_ARG) {
  15008. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  15009. }
  15010. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  15011. ret = 0;
  15012. } else {
  15013. ret = WOLFSSL_FATAL_ERROR;
  15014. }
  15015. }
  15016. #else
  15017. /* Pass in bad args. */
  15018. if (ret == 0) {
  15019. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  15020. if (ret == USER_CRYPTO_ERROR) {
  15021. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  15022. }
  15023. if (ret == USER_CRYPTO_ERROR) {
  15024. ret = 0;
  15025. } else {
  15026. ret = WOLFSSL_FATAL_ERROR;
  15027. }
  15028. }
  15029. #endif
  15030. if (der != NULL) {
  15031. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15032. }
  15033. if (wc_FreeRsaKey(&key) || ret != 0) {
  15034. ret = WOLFSSL_FATAL_ERROR;
  15035. }
  15036. if (wc_FreeRng(&rng) || ret != 0) {
  15037. ret = WOLFSSL_FATAL_ERROR;
  15038. }
  15039. printf(resultFmt, ret == 0 ? passed : failed);
  15040. #endif
  15041. return ret;
  15042. } /* END test_wc_RsaKeyToPublicDer */
  15043. /*
  15044. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  15045. */
  15046. static int test_wc_RsaPublicEncryptDecrypt (void)
  15047. {
  15048. int ret = 0;
  15049. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15050. RsaKey key;
  15051. WC_RNG rng;
  15052. const char inStr[] = TEST_STRING;
  15053. const word32 plainLen = (word32)TEST_STRING_SZ;
  15054. const word32 inLen = (word32)TEST_STRING_SZ;
  15055. int bits = TEST_RSA_BITS;
  15056. const word32 cipherLen = TEST_RSA_BYTES;
  15057. word32 cipherLenResult = cipherLen;
  15058. DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  15059. DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  15060. DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  15061. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  15062. if (in == NULL || plain == NULL || cipher == NULL) {
  15063. printf("test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  15064. return MEMORY_E;
  15065. }
  15066. #endif
  15067. XMEMCPY(in, inStr, inLen);
  15068. ret = wc_InitRsaKey(&key, NULL);
  15069. if (ret == 0) {
  15070. ret = wc_InitRng(&rng);
  15071. }
  15072. if (ret == 0) {
  15073. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  15074. }
  15075. /* Encrypt. */
  15076. printf(testingFmt, "wc_RsaPublicEncrypt()");
  15077. if (ret == 0) {
  15078. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  15079. if (ret >= 0) {
  15080. cipherLenResult = ret;
  15081. ret = 0;
  15082. } else {
  15083. ret = WOLFSSL_FATAL_ERROR;
  15084. }
  15085. }
  15086. /* Pass bad args. */
  15087. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  15088. printf(resultFmt, ret == 0 ? passed : failed);
  15089. if (ret != 0) {
  15090. return ret;
  15091. }
  15092. /* Decrypt */
  15093. printf(testingFmt, "wc_RsaPrivateDecrypt()");
  15094. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  15095. /* Bind rng */
  15096. if (ret == 0) {
  15097. ret = wc_RsaSetRNG(&key, &rng);
  15098. }
  15099. #endif
  15100. if (ret == 0) {
  15101. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  15102. }
  15103. if (ret >= 0) {
  15104. ret = XMEMCMP(plain, inStr, plainLen);
  15105. }
  15106. /* Pass in bad args. */
  15107. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  15108. FREE_VAR(in, NULL);
  15109. FREE_VAR(plain, NULL);
  15110. FREE_VAR(cipher, NULL);
  15111. if (wc_FreeRsaKey(&key) || ret != 0) {
  15112. ret = WOLFSSL_FATAL_ERROR;
  15113. }
  15114. if (wc_FreeRng(&rng) || ret != 0) {
  15115. ret = WOLFSSL_FATAL_ERROR;
  15116. }
  15117. printf(resultFmt, ret == 0 ? passed : failed);
  15118. #endif
  15119. return ret;
  15120. } /* END test_wc_RsaPublicEncryptDecrypt */
  15121. /*
  15122. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  15123. */
  15124. static int test_wc_RsaPublicEncryptDecrypt_ex (void)
  15125. {
  15126. int ret = 0;
  15127. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  15128. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  15129. && !defined(NO_SHA)
  15130. RsaKey key;
  15131. WC_RNG rng;
  15132. const char inStr[] = TEST_STRING;
  15133. const word32 inLen = (word32)TEST_STRING_SZ;
  15134. const word32 plainSz = (word32)TEST_STRING_SZ;
  15135. byte* res = NULL;
  15136. int idx = 0;
  15137. int bits = TEST_RSA_BITS;
  15138. const word32 cipherSz = TEST_RSA_BYTES;
  15139. DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  15140. DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  15141. DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  15142. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  15143. if (in == NULL || plain == NULL || cipher == NULL) {
  15144. printf("test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  15145. return MEMORY_E;
  15146. }
  15147. #endif
  15148. XMEMCPY(in, inStr, inLen);
  15149. /* Initialize stack structures. */
  15150. XMEMSET(&rng, 0, sizeof(rng));
  15151. XMEMSET(&key, 0, sizeof(key));
  15152. ret = wc_InitRsaKey_ex(&key, NULL, INVALID_DEVID);
  15153. if (ret == 0) {
  15154. ret = wc_InitRng(&rng);
  15155. }
  15156. if (ret == 0) {
  15157. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  15158. }
  15159. /* Encrypt */
  15160. printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
  15161. if (ret == 0) {
  15162. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  15163. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  15164. if (ret >= 0) {
  15165. idx = ret;
  15166. ret = 0;
  15167. } else {
  15168. ret = WOLFSSL_FATAL_ERROR;
  15169. }
  15170. }
  15171. /*Pass bad args.*/
  15172. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  15173. printf(resultFmt, ret == 0 ? passed : failed);
  15174. if (ret != 0) {
  15175. return ret;
  15176. }
  15177. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15178. /* Decrypt */
  15179. printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
  15180. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  15181. if (ret == 0) {
  15182. ret = wc_RsaSetRNG(&key, &rng);
  15183. }
  15184. #endif
  15185. if (ret == 0) {
  15186. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  15187. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  15188. WC_MGF1SHA1, NULL, 0);
  15189. }
  15190. if (ret >= 0) {
  15191. if (!XMEMCMP(plain, inStr, plainSz)) {
  15192. ret = 0;
  15193. } else {
  15194. ret = WOLFSSL_FATAL_ERROR;
  15195. }
  15196. }
  15197. /*Pass bad args.*/
  15198. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  15199. printf(resultFmt, ret == 0 ? passed : failed);
  15200. if (ret != 0) {
  15201. return ret;
  15202. }
  15203. printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
  15204. if (ret == 0) {
  15205. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  15206. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  15207. WC_MGF1SHA1, NULL, 0);
  15208. if (ret >= 0) {
  15209. if (!XMEMCMP(inStr, res, plainSz)) {
  15210. ret = 0;
  15211. } else {
  15212. ret = WOLFSSL_FATAL_ERROR;
  15213. }
  15214. }
  15215. }
  15216. #endif
  15217. FREE_VAR(in, NULL);
  15218. FREE_VAR(plain, NULL);
  15219. FREE_VAR(cipher, NULL);
  15220. if (wc_FreeRsaKey(&key) || ret != 0) {
  15221. ret = WOLFSSL_FATAL_ERROR;
  15222. }
  15223. if (wc_FreeRng(&rng) || ret != 0) {
  15224. ret = WOLFSSL_FATAL_ERROR;
  15225. }
  15226. printf(resultFmt, ret == 0 ? passed : failed);
  15227. #endif
  15228. return ret;
  15229. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  15230. /*
  15231. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  15232. */
  15233. static int test_wc_RsaSSL_SignVerify (void)
  15234. {
  15235. int ret = 0;
  15236. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15237. RsaKey key;
  15238. WC_RNG rng;
  15239. const char inStr[] = TEST_STRING;
  15240. const word32 plainSz = (word32)TEST_STRING_SZ;
  15241. const word32 inLen = (word32)TEST_STRING_SZ;
  15242. word32 idx = 0;
  15243. int bits = TEST_RSA_BITS;
  15244. const word32 outSz = TEST_RSA_BYTES;
  15245. DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  15246. DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  15247. DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  15248. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  15249. if (in == NULL || out == NULL || plain == NULL) {
  15250. printf("test_wc_RsaSSL_SignVerify failed\n");
  15251. return MEMORY_E;
  15252. }
  15253. #endif
  15254. XMEMCPY(in, inStr, inLen);
  15255. ret = wc_InitRsaKey(&key, NULL);
  15256. if (ret == 0) {
  15257. ret = wc_InitRng(&rng);
  15258. }
  15259. if (ret == 0) {
  15260. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  15261. }
  15262. /* Sign. */
  15263. printf(testingFmt, "wc_RsaSSL_Sign()");
  15264. if (ret == 0) {
  15265. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  15266. if (ret == (int)outSz) {
  15267. idx = ret;
  15268. ret = 0;
  15269. } else {
  15270. ret = WOLFSSL_FATAL_ERROR;
  15271. }
  15272. }
  15273. #ifndef HAVE_USER_RSA
  15274. /* Test bad args. */
  15275. if (ret == 0) {
  15276. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  15277. if (ret == BAD_FUNC_ARG) {
  15278. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  15279. }
  15280. if (ret == BAD_FUNC_ARG) {
  15281. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  15282. }
  15283. if (ret == BAD_FUNC_ARG) {
  15284. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  15285. }
  15286. if (ret == BAD_FUNC_ARG) {
  15287. ret = 0;
  15288. } else {
  15289. ret = WOLFSSL_FATAL_ERROR;
  15290. }
  15291. }
  15292. #else
  15293. /* Test bad args. */
  15294. if (ret == 0) {
  15295. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  15296. if (ret == USER_CRYPTO_ERROR) {
  15297. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  15298. }
  15299. if (ret == USER_CRYPTO_ERROR) {
  15300. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  15301. }
  15302. if (ret == USER_CRYPTO_ERROR) {
  15303. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  15304. }
  15305. if (ret == USER_CRYPTO_ERROR) {
  15306. ret = 0;
  15307. } else {
  15308. ret = WOLFSSL_FATAL_ERROR;
  15309. }
  15310. }
  15311. #endif
  15312. printf(resultFmt, ret == 0 ? passed : failed);
  15313. if (ret != 0) {
  15314. return ret;
  15315. }
  15316. /* Verify. */
  15317. printf(testingFmt, "wc_RsaSSL_Verify()");
  15318. if (ret == 0) {
  15319. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  15320. if (ret == (int)inLen) {
  15321. ret = 0;
  15322. } else {
  15323. ret = WOLFSSL_FATAL_ERROR;
  15324. }
  15325. }
  15326. #ifndef HAVE_USER_RSA
  15327. /* Pass bad args. */
  15328. if (ret == 0) {
  15329. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  15330. if (ret == BAD_FUNC_ARG) {
  15331. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  15332. }
  15333. if (ret == BAD_FUNC_ARG) {
  15334. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  15335. }
  15336. if (ret == BAD_FUNC_ARG) {
  15337. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  15338. }
  15339. if (ret == BAD_FUNC_ARG) {
  15340. ret = 0;
  15341. } else {
  15342. ret = WOLFSSL_FATAL_ERROR;
  15343. }
  15344. }
  15345. #else
  15346. /* Pass bad args. */
  15347. if (ret == 0) {
  15348. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  15349. if (ret == USER_CRYPTO_ERROR) {
  15350. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  15351. }
  15352. if (ret == USER_CRYPTO_ERROR) {
  15353. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  15354. }
  15355. if (ret == USER_CRYPTO_ERROR) {
  15356. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  15357. }
  15358. if (ret == USER_CRYPTO_ERROR) {
  15359. ret = 0;
  15360. } else {
  15361. ret = WOLFSSL_FATAL_ERROR;
  15362. }
  15363. }
  15364. #endif
  15365. FREE_VAR(in, NULL);
  15366. FREE_VAR(out, NULL);
  15367. FREE_VAR(plain, NULL);
  15368. if (wc_FreeRsaKey(&key) || ret != 0) {
  15369. ret = WOLFSSL_FATAL_ERROR;
  15370. }
  15371. if (wc_FreeRng(&rng) || ret != 0) {
  15372. ret = WOLFSSL_FATAL_ERROR;
  15373. }
  15374. printf(resultFmt, ret == 0 ? passed : failed);
  15375. #endif
  15376. return ret;
  15377. } /* END test_wc_RsaSSL_SignVerify */
  15378. /*
  15379. * Testing wc_RsaEncryptSize()
  15380. */
  15381. static int test_wc_RsaEncryptSize (void)
  15382. {
  15383. int ret = 0;
  15384. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15385. RsaKey key;
  15386. WC_RNG rng;
  15387. ret = wc_InitRsaKey(&key, NULL);
  15388. if (ret == 0) {
  15389. ret = wc_InitRng(&rng);
  15390. }
  15391. printf(testingFmt, "wc_RsaEncryptSize()");
  15392. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  15393. if (ret == 0) {
  15394. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  15395. if (ret == 0) {
  15396. ret = wc_RsaEncryptSize(&key);
  15397. }
  15398. if (ret == 128) {
  15399. ret = 0;
  15400. } else {
  15401. ret = WOLFSSL_FATAL_ERROR;
  15402. }
  15403. }
  15404. if (wc_FreeRsaKey(&key) || ret != 0) {
  15405. ret = WOLFSSL_FATAL_ERROR;
  15406. } else {
  15407. ret = 0;
  15408. }
  15409. #endif
  15410. if (ret == 0) {
  15411. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  15412. if (ret == 0) {
  15413. ret = wc_RsaEncryptSize(&key);
  15414. }
  15415. if (ret == 256) {
  15416. ret = 0;
  15417. } else {
  15418. ret = WOLFSSL_FATAL_ERROR;
  15419. }
  15420. }
  15421. /* Pass in bad arg. */
  15422. if (ret == 0) {
  15423. ret = wc_RsaEncryptSize(NULL);
  15424. #ifndef HAVE_USER_RSA
  15425. if (ret == BAD_FUNC_ARG) {
  15426. ret = 0;
  15427. } else {
  15428. ret = WOLFSSL_FATAL_ERROR;
  15429. }
  15430. #endif
  15431. }
  15432. if (wc_FreeRsaKey(&key) || ret != 0) {
  15433. ret = WOLFSSL_FATAL_ERROR;
  15434. }
  15435. if (wc_FreeRng(&rng) || ret != 0) {
  15436. ret = WOLFSSL_FATAL_ERROR;
  15437. }
  15438. printf(resultFmt, ret == 0 ? passed : failed);
  15439. #endif
  15440. return ret;
  15441. } /* END test_wc_RsaEncryptSize*/
  15442. /*
  15443. * Testing wc_RsaFlattenPublicKey()
  15444. */
  15445. static int test_wc_RsaFlattenPublicKey (void)
  15446. {
  15447. int ret = 0;
  15448. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15449. RsaKey key;
  15450. WC_RNG rng;
  15451. byte e[256];
  15452. byte n[256];
  15453. word32 eSz = sizeof(e);
  15454. word32 nSz = sizeof(n);
  15455. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  15456. int bits = 1024;
  15457. #else
  15458. int bits = 2048;
  15459. #endif
  15460. ret = wc_InitRsaKey(&key, NULL);
  15461. if (ret == 0) {
  15462. ret = wc_InitRng(&rng);
  15463. }
  15464. if (ret == 0) {
  15465. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  15466. if (ret >= 0) {
  15467. ret = 0;
  15468. } else {
  15469. ret = WOLFSSL_FATAL_ERROR;
  15470. }
  15471. }
  15472. printf(testingFmt, "wc_RsaFlattenPublicKey()");
  15473. if (ret == 0) {
  15474. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  15475. }
  15476. #ifndef HAVE_USER_RSA
  15477. /* Pass bad args. */
  15478. if (ret == 0) {
  15479. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  15480. if (ret == BAD_FUNC_ARG) {
  15481. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  15482. }
  15483. if (ret == BAD_FUNC_ARG) {
  15484. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  15485. }
  15486. if (ret == BAD_FUNC_ARG) {
  15487. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  15488. }
  15489. if (ret == BAD_FUNC_ARG) {
  15490. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  15491. }
  15492. if (ret == BAD_FUNC_ARG) {
  15493. ret = 0;
  15494. } else {
  15495. ret = WOLFSSL_FATAL_ERROR;
  15496. }
  15497. }
  15498. #else
  15499. /* Pass bad args. */
  15500. if (ret == 0) {
  15501. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  15502. if (ret == USER_CRYPTO_ERROR) {
  15503. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  15504. }
  15505. if (ret == USER_CRYPTO_ERROR) {
  15506. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  15507. }
  15508. if (ret == USER_CRYPTO_ERROR) {
  15509. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  15510. }
  15511. if (ret == USER_CRYPTO_ERROR) {
  15512. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  15513. }
  15514. if (ret == USER_CRYPTO_ERROR) {
  15515. ret = 0;
  15516. } else {
  15517. ret = WOLFSSL_FATAL_ERROR;
  15518. }
  15519. }
  15520. #endif
  15521. if (wc_FreeRsaKey(&key) || ret != 0) {
  15522. ret = WOLFSSL_FATAL_ERROR;
  15523. }
  15524. if (wc_FreeRng(&rng) || ret != 0) {
  15525. ret = WOLFSSL_FATAL_ERROR;
  15526. }
  15527. printf(resultFmt, ret == 0 ? passed : failed);
  15528. #endif
  15529. return ret;
  15530. } /* END test_wc_RsaFlattenPublicKey */
  15531. /*
  15532. * unit test for wc_AesCcmSetKey
  15533. */
  15534. static int test_wc_AesCcmSetKey (void)
  15535. {
  15536. int ret = 0;
  15537. #ifdef HAVE_AESCCM
  15538. Aes aes;
  15539. const byte key16[] =
  15540. {
  15541. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  15542. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  15543. };
  15544. const byte key24[] =
  15545. {
  15546. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15547. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15548. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15549. };
  15550. const byte key32[] =
  15551. {
  15552. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15553. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15554. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15555. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15556. };
  15557. printf(testingFmt, "wc_AesCcmSetKey()");
  15558. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15559. if (ret != 0)
  15560. return ret;
  15561. #ifdef WOLFSSL_AES_128
  15562. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  15563. #endif
  15564. #ifdef WOLFSSL_AES_192
  15565. if (ret == 0) {
  15566. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  15567. }
  15568. #endif
  15569. #ifdef WOLFSSL_AES_256
  15570. if (ret == 0) {
  15571. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  15572. }
  15573. #endif
  15574. /* Test bad args. */
  15575. if (ret == 0) {
  15576. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  15577. if (ret == BAD_FUNC_ARG) {
  15578. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  15579. }
  15580. if (ret == BAD_FUNC_ARG) {
  15581. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  15582. }
  15583. if (ret != BAD_FUNC_ARG) {
  15584. ret = WOLFSSL_FATAL_ERROR;
  15585. } else {
  15586. ret = 0;
  15587. }
  15588. }
  15589. wc_AesFree(&aes);
  15590. printf(resultFmt, ret == 0 ? passed : failed);
  15591. #endif
  15592. return ret;
  15593. } /* END test_wc_AesCcmSetKey */
  15594. /*
  15595. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  15596. */
  15597. static int test_wc_AesCcmEncryptDecrypt (void)
  15598. {
  15599. int ret = 0;
  15600. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  15601. Aes aes;
  15602. const byte key16[] =
  15603. {
  15604. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  15605. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  15606. };
  15607. /* plaintext */
  15608. const byte plainT[] =
  15609. {
  15610. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  15611. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  15612. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  15613. };
  15614. /* nonce */
  15615. const byte iv[] =
  15616. {
  15617. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  15618. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  15619. };
  15620. const byte c[] = /* cipher text. */
  15621. {
  15622. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  15623. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  15624. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  15625. };
  15626. const byte t[] = /* Auth tag */
  15627. {
  15628. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  15629. };
  15630. const byte authIn[] =
  15631. {
  15632. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  15633. };
  15634. byte cipherOut[sizeof(plainT)];
  15635. byte authTag[sizeof(t)];
  15636. int ccmE = WOLFSSL_FATAL_ERROR;
  15637. #ifdef HAVE_AES_DECRYPT
  15638. int ccmD = WOLFSSL_FATAL_ERROR;
  15639. byte plainOut[sizeof(cipherOut)];
  15640. #endif
  15641. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15642. if (ret != 0)
  15643. return ret;
  15644. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  15645. if (ret == 0) {
  15646. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  15647. iv, sizeof(iv), authTag, sizeof(authTag),
  15648. authIn , sizeof(authIn));
  15649. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  15650. XMEMCMP(t, authTag, sizeof(t))) {
  15651. ccmE = WOLFSSL_FATAL_ERROR;
  15652. ret = WOLFSSL_FATAL_ERROR;
  15653. }
  15654. #ifdef HAVE_AES_DECRYPT
  15655. if (ret == 0) {
  15656. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  15657. sizeof(plainOut), iv, sizeof(iv),
  15658. authTag, sizeof(authTag),
  15659. authIn, sizeof(authIn));
  15660. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  15661. ccmD = WOLFSSL_FATAL_ERROR;
  15662. }
  15663. }
  15664. #endif
  15665. }
  15666. printf(testingFmt, "wc_AesCcmEncrypt()");
  15667. /* Pass in bad args. Encrypt*/
  15668. if (ret == 0 && ccmE == 0) {
  15669. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  15670. iv, sizeof(iv), authTag, sizeof(authTag),
  15671. authIn , sizeof(authIn));
  15672. if (ccmE == BAD_FUNC_ARG) {
  15673. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  15674. iv, sizeof(iv), authTag, sizeof(authTag),
  15675. authIn , sizeof(authIn));
  15676. }
  15677. if (ccmE == BAD_FUNC_ARG) {
  15678. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  15679. iv, sizeof(iv), authTag, sizeof(authTag),
  15680. authIn , sizeof(authIn));
  15681. }
  15682. if (ccmE == BAD_FUNC_ARG) {
  15683. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  15684. NULL, sizeof(iv), authTag, sizeof(authTag),
  15685. authIn , sizeof(authIn));
  15686. }
  15687. if (ccmE == BAD_FUNC_ARG) {
  15688. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  15689. iv, sizeof(iv), NULL, sizeof(authTag),
  15690. authIn , sizeof(authIn));
  15691. }
  15692. if (ccmE == BAD_FUNC_ARG) {
  15693. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  15694. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  15695. authIn , sizeof(authIn));
  15696. }
  15697. if (ccmE == BAD_FUNC_ARG) {
  15698. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  15699. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  15700. authIn , sizeof(authIn));
  15701. }
  15702. if (ccmE != BAD_FUNC_ARG) {
  15703. ccmE = WOLFSSL_FATAL_ERROR;
  15704. } else {
  15705. ccmE = 0;
  15706. }
  15707. } /* End Encrypt */
  15708. printf(resultFmt, ccmE == 0 ? passed : failed);
  15709. if (ccmE != 0) {
  15710. wc_AesFree(&aes);
  15711. return ccmE;
  15712. }
  15713. #ifdef HAVE_AES_DECRYPT
  15714. printf(testingFmt, "wc_AesCcmDecrypt()");
  15715. /* Pass in bad args. Decrypt*/
  15716. if (ret == 0 && ccmD == 0) {
  15717. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  15718. iv, sizeof(iv), authTag, sizeof(authTag),
  15719. authIn, sizeof(authIn));
  15720. if (ccmD == BAD_FUNC_ARG) {
  15721. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  15722. iv, sizeof(iv), authTag, sizeof(authTag),
  15723. authIn, sizeof(authIn));
  15724. }
  15725. if (ccmD == BAD_FUNC_ARG) {
  15726. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  15727. iv, sizeof(iv), authTag, sizeof(authTag),
  15728. authIn, sizeof(authIn));
  15729. }
  15730. if (ccmD == BAD_FUNC_ARG) {
  15731. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  15732. sizeof(plainOut), NULL, sizeof(iv),
  15733. authTag, sizeof(authTag),
  15734. authIn, sizeof(authIn));
  15735. }
  15736. if (ccmD == BAD_FUNC_ARG) {
  15737. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  15738. sizeof(plainOut), iv, sizeof(iv), NULL,
  15739. sizeof(authTag), authIn, sizeof(authIn));
  15740. }
  15741. if (ccmD == BAD_FUNC_ARG) {
  15742. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  15743. sizeof(plainOut), iv, sizeof(iv) + 1,
  15744. authTag, sizeof(authTag),
  15745. authIn, sizeof(authIn));
  15746. }
  15747. if (ccmD == BAD_FUNC_ARG) {
  15748. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  15749. sizeof(plainOut), iv, sizeof(iv) - 7,
  15750. authTag, sizeof(authTag),
  15751. authIn, sizeof(authIn));
  15752. }
  15753. if (ccmD != BAD_FUNC_ARG) {
  15754. ccmD = WOLFSSL_FATAL_ERROR;
  15755. } else {
  15756. ccmD = 0;
  15757. }
  15758. } /* END Decrypt */
  15759. printf(resultFmt, ccmD == 0 ? passed : failed);
  15760. if (ccmD != 0) {
  15761. return ccmD;
  15762. }
  15763. #endif
  15764. wc_AesFree(&aes);
  15765. #endif /* HAVE_AESCCM */
  15766. return ret;
  15767. } /* END test_wc_AesCcmEncryptDecrypt */
  15768. /*
  15769. * Test wc_Hc128_SetKey()
  15770. */
  15771. static int test_wc_Hc128_SetKey (void)
  15772. {
  15773. int ret = 0;
  15774. #ifdef HAVE_HC128
  15775. HC128 ctx;
  15776. const char* key = "\x80\x00\x00\x00\x00\x00\x00\x00"
  15777. "\x00\x00\x00\x00\x00\x00\x00\x00";
  15778. const char* iv = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  15779. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  15780. printf(testingFmt, "wc_Hc128_SetKey()");
  15781. ret = wc_Hc128_SetKey(&ctx, (byte*)key, (byte*)iv);
  15782. /* Test bad args. */
  15783. if (ret == 0) {
  15784. ret = wc_Hc128_SetKey(NULL, (byte*)key, (byte*)iv);
  15785. if (ret == BAD_FUNC_ARG) {
  15786. ret = wc_Hc128_SetKey(&ctx, NULL, (byte*)iv);
  15787. }
  15788. if (ret == BAD_FUNC_ARG) {
  15789. ret = wc_Hc128_SetKey(&ctx, (byte*)key, NULL);
  15790. }
  15791. }
  15792. printf(resultFmt, ret == 0 ? passed : failed);
  15793. #endif
  15794. return ret;
  15795. } /* END test_wc_Hc128_SetKey */
  15796. /*
  15797. * Testing wc_Hc128_Process()
  15798. */
  15799. static int test_wc_Hc128_Process (void)
  15800. {
  15801. int ret = 0;
  15802. #ifdef HAVE_HC128
  15803. HC128 enc;
  15804. HC128 dec;
  15805. const char* key = "\x0F\x62\xB5\x08\x5B\xAE\x01\x54"
  15806. "\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC";
  15807. const char* input = "Encrypt Hc128, and then Decrypt.";
  15808. size_t inlen = XSTRLEN(input) + 1; /* Add null terminator */
  15809. byte cipher[inlen];
  15810. byte plain[inlen];
  15811. printf(testingFmt, "wc_Hc128_Process()");
  15812. ret = wc_Hc128_SetKey(&enc, (byte*)key, NULL);
  15813. if (ret == 0) {
  15814. ret = wc_Hc128_SetKey(&dec, (byte*)key, NULL);
  15815. }
  15816. if (ret == 0) {
  15817. ret = wc_Hc128_Process(&enc, cipher, (byte*)input, (word32)inlen);
  15818. if (ret == 0) {
  15819. ret = wc_Hc128_Process(&dec, plain, cipher, (word32)inlen);
  15820. }
  15821. }
  15822. /* Bad args. */
  15823. if (ret == 0) {
  15824. ret = wc_Hc128_Process(NULL, plain, cipher, (word32)inlen);
  15825. if (ret == BAD_FUNC_ARG) {
  15826. ret = wc_Hc128_Process(&dec, NULL, cipher, (word32)inlen);
  15827. }
  15828. if (ret == BAD_FUNC_ARG) {
  15829. ret = wc_Hc128_Process(&dec, plain, NULL, (word32)inlen);
  15830. }
  15831. if (ret == BAD_FUNC_ARG) {
  15832. ret = 0;
  15833. } else {
  15834. ret = WOLFSSL_FATAL_ERROR;
  15835. }
  15836. }
  15837. printf(resultFmt, ret == 0 ? passed : failed);
  15838. #endif
  15839. return ret;
  15840. } /* END test_wc_Hc128_Process */
  15841. /*
  15842. * Testing wc_InitDsaKey()
  15843. */
  15844. static int test_wc_InitDsaKey (void)
  15845. {
  15846. int ret = 0;
  15847. #ifndef NO_DSA
  15848. DsaKey key;
  15849. printf(testingFmt, "wc_InitDsaKey()");
  15850. ret = wc_InitDsaKey(&key);
  15851. /* Pass in bad args. */
  15852. if (ret == 0) {
  15853. ret = wc_InitDsaKey(NULL);
  15854. if (ret == BAD_FUNC_ARG) {
  15855. ret = 0;
  15856. } else {
  15857. ret = WOLFSSL_FATAL_ERROR;
  15858. }
  15859. }
  15860. printf(resultFmt, ret == 0 ? passed : failed);
  15861. wc_FreeDsaKey(&key);
  15862. #endif
  15863. return ret;
  15864. } /* END test_wc_InitDsaKey */
  15865. /*
  15866. * Testing wc_DsaSign() and wc_DsaVerify()
  15867. */
  15868. static int test_wc_DsaSignVerify (void)
  15869. {
  15870. int ret = 0;
  15871. #if !defined(NO_DSA)
  15872. DsaKey key;
  15873. WC_RNG rng;
  15874. wc_Sha sha;
  15875. byte signature[DSA_SIG_SIZE];
  15876. byte hash[WC_SHA_DIGEST_SIZE];
  15877. word32 idx = 0;
  15878. word32 bytes;
  15879. int answer;
  15880. #ifdef USE_CERT_BUFFERS_1024
  15881. byte tmp[ONEK_BUF];
  15882. XMEMSET(tmp, 0, sizeof(tmp));
  15883. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  15884. bytes = sizeof_dsa_key_der_1024;
  15885. #elif defined(USE_CERT_BUFFERS_2048)
  15886. byte tmp[TWOK_BUF];
  15887. XMEMSET(tmp, 0, sizeof(tmp));
  15888. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  15889. bytes = sizeof_dsa_key_der_2048;
  15890. #else
  15891. byte tmp[TWOK_BUF];
  15892. XMEMSET(tmp, 0, sizeof(tmp));
  15893. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  15894. if (fp == XBADFILE) {
  15895. return WOLFSSL_BAD_FILE;
  15896. }
  15897. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  15898. XFCLOSE(fp);
  15899. #endif /* END USE_CERT_BUFFERS_1024 */
  15900. ret = wc_InitSha(&sha);
  15901. if (ret == 0) {
  15902. ret = wc_ShaUpdate(&sha, tmp, bytes);
  15903. if (ret == 0) {
  15904. ret = wc_ShaFinal(&sha, hash);
  15905. }
  15906. if (ret == 0) {
  15907. ret = wc_InitDsaKey(&key);
  15908. }
  15909. if (ret == 0) {
  15910. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  15911. }
  15912. if (ret == 0) {
  15913. ret = wc_InitRng(&rng);
  15914. }
  15915. }
  15916. printf(testingFmt, "wc_DsaSign()");
  15917. /* Sign. */
  15918. if (ret == 0) {
  15919. ret = wc_DsaSign(hash, signature, &key, &rng);
  15920. }
  15921. /* Test bad args. */
  15922. if (ret == 0) {
  15923. ret = wc_DsaSign(NULL, signature, &key, &rng);
  15924. if (ret == BAD_FUNC_ARG) {
  15925. ret = wc_DsaSign(hash, NULL, &key, &rng);
  15926. }
  15927. if (ret == BAD_FUNC_ARG) {
  15928. ret = wc_DsaSign(hash, signature, NULL, &rng);
  15929. }
  15930. if (ret == BAD_FUNC_ARG) {
  15931. ret = wc_DsaSign(hash, signature, &key, NULL);
  15932. }
  15933. if (ret == BAD_FUNC_ARG) {
  15934. ret = 0;
  15935. } else {
  15936. ret = WOLFSSL_FATAL_ERROR;
  15937. }
  15938. }
  15939. printf(resultFmt, ret == 0 ? passed : failed);
  15940. if (ret != 0) {
  15941. return ret;
  15942. }
  15943. /* Verify. */
  15944. printf(testingFmt, "wc_DsaVerify()");
  15945. ret = wc_DsaVerify(hash, signature, &key, &answer);
  15946. if (ret != 0 || answer != 1) {
  15947. ret = WOLFSSL_FATAL_ERROR;
  15948. } else {
  15949. ret = 0;
  15950. }
  15951. /* Pass in bad args. */
  15952. if (ret == 0) {
  15953. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  15954. if (ret == BAD_FUNC_ARG) {
  15955. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  15956. }
  15957. if (ret == BAD_FUNC_ARG) {
  15958. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  15959. }
  15960. if (ret == BAD_FUNC_ARG) {
  15961. ret = wc_DsaVerify(hash, signature, &key, NULL);
  15962. }
  15963. if (ret == BAD_FUNC_ARG) {
  15964. ret = 0;
  15965. } else {
  15966. ret = WOLFSSL_FATAL_ERROR;
  15967. }
  15968. }
  15969. if (wc_FreeRng(&rng) && ret == 0) {
  15970. ret = WOLFSSL_FATAL_ERROR;
  15971. }
  15972. printf(resultFmt, ret == 0 ? passed : failed);
  15973. wc_FreeDsaKey(&key);
  15974. wc_ShaFree(&sha);
  15975. #endif
  15976. return ret;
  15977. } /* END test_wc_DsaSign */
  15978. /*
  15979. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  15980. */
  15981. static int test_wc_DsaPublicPrivateKeyDecode (void)
  15982. {
  15983. int ret = 0;
  15984. #if !defined(NO_DSA)
  15985. DsaKey key;
  15986. word32 bytes;
  15987. word32 idx = 0;
  15988. int priv = WOLFSSL_FATAL_ERROR;
  15989. int pub = WOLFSSL_FATAL_ERROR;
  15990. #ifdef USE_CERT_BUFFERS_1024
  15991. byte tmp[ONEK_BUF];
  15992. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  15993. bytes = sizeof_dsa_key_der_1024;
  15994. #elif defined(USE_CERT_BUFFERS_2048)
  15995. byte tmp[TWOK_BUF];
  15996. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  15997. bytes = sizeof_dsa_key_der_2048;
  15998. #else
  15999. byte tmp[TWOK_BUF];
  16000. XMEMSET(tmp, 0, sizeof(tmp));
  16001. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  16002. if (fp == XBADFILE)
  16003. {
  16004. return WOLFSSL_BAD_FILE;
  16005. }
  16006. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  16007. XFCLOSE(fp);
  16008. #endif /* END USE_CERT_BUFFERS_1024 */
  16009. ret = wc_InitDsaKey(&key);
  16010. printf(testingFmt, "wc_DsaPrivateKeyDecode()");
  16011. if (ret == 0) {
  16012. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  16013. /* Test bad args. */
  16014. if (priv == 0) {
  16015. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  16016. if (priv == BAD_FUNC_ARG) {
  16017. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  16018. }
  16019. if (priv == BAD_FUNC_ARG) {
  16020. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  16021. }
  16022. if (priv == BAD_FUNC_ARG) {
  16023. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  16024. }
  16025. if (priv == ASN_PARSE_E) {
  16026. priv = 0;
  16027. } else {
  16028. priv = WOLFSSL_FATAL_ERROR;
  16029. }
  16030. }
  16031. wc_FreeDsaKey(&key);
  16032. ret = wc_InitDsaKey(&key);
  16033. }
  16034. printf(resultFmt, priv == 0 ? passed : failed);
  16035. printf(testingFmt, "wc_DsaPublicKeyDecode()");
  16036. if (ret == 0) {
  16037. idx = 0; /* Reset */
  16038. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  16039. /* Test bad args. */
  16040. if (pub == 0) {
  16041. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  16042. if (pub == BAD_FUNC_ARG) {
  16043. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  16044. }
  16045. if (pub == BAD_FUNC_ARG) {
  16046. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  16047. }
  16048. if (pub == BAD_FUNC_ARG) {
  16049. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  16050. }
  16051. if (pub == ASN_PARSE_E) {
  16052. pub = 0;
  16053. } else {
  16054. pub = WOLFSSL_FATAL_ERROR;
  16055. }
  16056. }
  16057. } /* END Public Key */
  16058. printf(resultFmt, pub == 0 ? passed : failed);
  16059. wc_FreeDsaKey(&key);
  16060. #endif
  16061. return ret;
  16062. } /* END test_wc_DsaPublicPrivateKeyDecode */
  16063. /*
  16064. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  16065. */
  16066. static int test_wc_MakeDsaKey (void)
  16067. {
  16068. int ret = 0;
  16069. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  16070. DsaKey genKey;
  16071. WC_RNG rng;
  16072. XMEMSET(&rng, 0, sizeof(rng));
  16073. XMEMSET(&genKey, 0, sizeof(genKey));
  16074. ret = wc_InitRng(&rng);
  16075. if (ret == 0) {
  16076. ret = wc_InitDsaKey(&genKey);
  16077. }
  16078. printf(testingFmt, "wc_MakeDsaParameters()");
  16079. if (ret == 0) {
  16080. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  16081. }
  16082. /* Test bad args. */
  16083. if (ret == 0) {
  16084. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  16085. if (ret == BAD_FUNC_ARG) {
  16086. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  16087. }
  16088. if (ret == BAD_FUNC_ARG) {
  16089. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  16090. }
  16091. if (ret == BAD_FUNC_ARG) {
  16092. ret = 0;
  16093. } else {
  16094. ret = WOLFSSL_FATAL_ERROR;
  16095. }
  16096. }
  16097. printf(resultFmt, ret == 0 ? passed : failed);
  16098. printf(testingFmt, "wc_MakeDsaKey()");
  16099. if (ret == 0) {
  16100. ret = wc_MakeDsaKey(&rng, &genKey);
  16101. }
  16102. /* Test bad args. */
  16103. if (ret == 0) {
  16104. ret = wc_MakeDsaKey(NULL, &genKey);
  16105. if (ret == BAD_FUNC_ARG) {
  16106. ret = wc_MakeDsaKey(&rng, NULL);
  16107. }
  16108. if (ret == BAD_FUNC_ARG) {
  16109. ret = 0;
  16110. } else {
  16111. ret = WOLFSSL_FATAL_ERROR;
  16112. }
  16113. }
  16114. if (wc_FreeRng(&rng) && ret == 0) {
  16115. ret = WOLFSSL_FAILURE;
  16116. }
  16117. printf(resultFmt, ret == 0 ? passed : failed);
  16118. wc_FreeDsaKey(&genKey);
  16119. #endif
  16120. return ret;
  16121. } /* END test_wc_MakeDsaKey */
  16122. /*
  16123. * Testing wc_DsaKeyToDer()
  16124. */
  16125. static int test_wc_DsaKeyToDer (void)
  16126. {
  16127. int ret = 0;
  16128. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  16129. DsaKey genKey;
  16130. WC_RNG rng;
  16131. word32 bytes;
  16132. word32 idx = 0;
  16133. #ifdef USE_CERT_BUFFERS_1024
  16134. byte tmp[ONEK_BUF];
  16135. byte der[ONEK_BUF];
  16136. XMEMSET(tmp, 0, sizeof(tmp));
  16137. XMEMSET(der, 0, sizeof(der));
  16138. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  16139. bytes = sizeof_dsa_key_der_1024;
  16140. #elif defined(USE_CERT_BUFFERS_2048)
  16141. byte tmp[TWOK_BUF];
  16142. byte der[TWOK_BUF];
  16143. XMEMSET(tmp, 0, sizeof(tmp));
  16144. XMEMSET(der, 0, sizeof(der));
  16145. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  16146. bytes = sizeof_dsa_key_der_2048;
  16147. #else
  16148. byte tmp[TWOK_BUF];
  16149. byte der[TWOK_BUF];
  16150. XMEMSET(tmp, 0, sizeof(tmp));
  16151. XMEMSET(der, 0, sizeof(der));
  16152. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  16153. if (fp == XBADFILE) {
  16154. return WOLFSSL_BAD_FILE;
  16155. }
  16156. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  16157. XFCLOSE(fp);
  16158. #endif /* END USE_CERT_BUFFERS_1024 */
  16159. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  16160. XMEMSET(&rng, 0, sizeof(rng));
  16161. XMEMSET(&genKey, 0, sizeof(genKey));
  16162. #endif
  16163. ret = wc_InitRng(&rng);
  16164. if (ret == 0) {
  16165. ret = wc_InitDsaKey(&genKey);
  16166. }
  16167. if (ret == 0) {
  16168. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  16169. if (ret == 0) {
  16170. wc_FreeDsaKey(&genKey);
  16171. ret = wc_InitDsaKey(&genKey);
  16172. }
  16173. }
  16174. if (ret == 0) {
  16175. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  16176. }
  16177. printf(testingFmt, "wc_DsaKeyToDer()");
  16178. if (ret == 0) {
  16179. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  16180. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  16181. ret = 0;
  16182. }
  16183. }
  16184. /* Test bad args. */
  16185. if (ret == 0) {
  16186. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  16187. if (ret == BAD_FUNC_ARG) {
  16188. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  16189. }
  16190. if (ret == BAD_FUNC_ARG) {
  16191. ret = 0;
  16192. } else {
  16193. ret = WOLFSSL_FATAL_ERROR;
  16194. }
  16195. }
  16196. if (wc_FreeRng(&rng) && ret == 0) {
  16197. ret = WOLFSSL_FATAL_ERROR;
  16198. }
  16199. printf(resultFmt, ret == 0 ? passed : failed);
  16200. wc_FreeDsaKey(&genKey);
  16201. #endif
  16202. return ret;
  16203. } /* END test_wc_DsaKeyToDer */
  16204. /*
  16205. * Testing wc_DsaKeyToPublicDer()
  16206. * (indirectly testing setDsaPublicKey())
  16207. */
  16208. static int test_wc_DsaKeyToPublicDer(void)
  16209. {
  16210. int ret = 0;
  16211. #ifndef HAVE_SELFTEST
  16212. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  16213. DsaKey genKey;
  16214. WC_RNG rng;
  16215. byte* der;
  16216. word32 sz;
  16217. printf(testingFmt, "wc_DsaKeyToPublicDer()");
  16218. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16219. if (der == NULL) {
  16220. ret = WOLFSSL_FATAL_ERROR;
  16221. }
  16222. if (ret == 0) {
  16223. ret = wc_InitDsaKey(&genKey);
  16224. }
  16225. if (ret == 0) {
  16226. ret = wc_InitRng(&rng);
  16227. }
  16228. if (ret == 0) {
  16229. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  16230. }
  16231. if (ret == 0) {
  16232. ret = wc_MakeDsaKey(&rng, &genKey);
  16233. }
  16234. if (ret == 0) {
  16235. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  16236. if (ret >= 0) {
  16237. sz = ret;
  16238. ret = 0;
  16239. } else {
  16240. ret = WOLFSSL_FATAL_ERROR;
  16241. }
  16242. }
  16243. if (ret == 0) {
  16244. word32 idx = 0;
  16245. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  16246. }
  16247. /* Test bad args. */
  16248. if (ret == 0) {
  16249. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  16250. if (ret == BAD_FUNC_ARG) {
  16251. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  16252. }
  16253. if (ret == BAD_FUNC_ARG) {
  16254. ret = 0;
  16255. } else {
  16256. ret = WOLFSSL_FATAL_ERROR;
  16257. }
  16258. }
  16259. if (wc_FreeRng(&rng) && ret == 0) {
  16260. ret = WOLFSSL_FATAL_ERROR;
  16261. }
  16262. printf(resultFmt, ret == 0 ? passed : failed);
  16263. XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
  16264. wc_FreeDsaKey(&genKey);
  16265. #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
  16266. #endif /* HAVE_SELFTEST */
  16267. return ret;
  16268. } /* END test_wc_DsaKeyToPublicDer */
  16269. /*
  16270. * Testing wc_DsaImportParamsRaw()
  16271. */
  16272. static int test_wc_DsaImportParamsRaw (void)
  16273. {
  16274. int ret = 0;
  16275. #if !defined(NO_DSA)
  16276. DsaKey key;
  16277. /* [mod = L=1024, N=160], from CAVP KeyPair */
  16278. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  16279. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  16280. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  16281. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  16282. "47123188f8dc551054ee162b634d60f097f719076640e209"
  16283. "80a0093113a8bd73";
  16284. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  16285. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  16286. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  16287. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  16288. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  16289. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  16290. "76341a7e7d9";
  16291. /* invalid p and q parameters */
  16292. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  16293. const char* invalidQ = "96c5390a";
  16294. printf(testingFmt, "wc_DsaImportParamsRaw()");
  16295. ret = wc_InitDsaKey(&key);
  16296. if (ret == 0) {
  16297. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  16298. }
  16299. /* test bad args */
  16300. if (ret == 0) {
  16301. /* null key struct */
  16302. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  16303. if (ret == BAD_FUNC_ARG) {
  16304. /* null param pointers */
  16305. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  16306. }
  16307. if (ret == BAD_FUNC_ARG) {
  16308. /* illegal p length */
  16309. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  16310. }
  16311. if (ret == BAD_FUNC_ARG) {
  16312. /* illegal q length */
  16313. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  16314. if (ret == BAD_FUNC_ARG)
  16315. ret = 0;
  16316. }
  16317. }
  16318. printf(resultFmt, ret == 0 ? passed : failed);
  16319. wc_FreeDsaKey(&key);
  16320. #endif
  16321. return ret;
  16322. } /* END test_wc_DsaImportParamsRaw */
  16323. /*
  16324. * Testing wc_DsaImportParamsRawCheck()
  16325. */
  16326. static int test_wc_DsaImportParamsRawCheck (void)
  16327. {
  16328. int ret = 0;
  16329. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  16330. DsaKey key;
  16331. int trusted = 0;
  16332. /* [mod = L=1024, N=160], from CAVP KeyPair */
  16333. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  16334. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  16335. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  16336. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  16337. "47123188f8dc551054ee162b634d60f097f719076640e209"
  16338. "80a0093113a8bd73";
  16339. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  16340. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  16341. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  16342. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  16343. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  16344. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  16345. "76341a7e7d9";
  16346. /* invalid p and q parameters */
  16347. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  16348. const char* invalidQ = "96c5390a";
  16349. printf(testingFmt, "wc_DsaImportParamsRawCheck()");
  16350. ret = wc_InitDsaKey(&key);
  16351. if (ret == 0) {
  16352. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  16353. }
  16354. /* test bad args */
  16355. if (ret == 0) {
  16356. /* null key struct */
  16357. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  16358. if (ret == BAD_FUNC_ARG) {
  16359. /* null param pointers */
  16360. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  16361. }
  16362. if (ret == BAD_FUNC_ARG) {
  16363. /* illegal p length */
  16364. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  16365. }
  16366. if (ret == BAD_FUNC_ARG) {
  16367. /* illegal q length */
  16368. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  16369. if (ret == BAD_FUNC_ARG)
  16370. ret = 0;
  16371. }
  16372. }
  16373. printf(resultFmt, ret == 0 ? passed : failed);
  16374. wc_FreeDsaKey(&key);
  16375. #endif
  16376. return ret;
  16377. } /* END test_wc_DsaImportParamsRawCheck */
  16378. /*
  16379. * Testing wc_DsaExportParamsRaw()
  16380. */
  16381. static int test_wc_DsaExportParamsRaw (void)
  16382. {
  16383. int ret = 0;
  16384. #if !defined(NO_DSA)
  16385. DsaKey key;
  16386. /* [mod = L=1024, N=160], from CAVP KeyPair */
  16387. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  16388. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  16389. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  16390. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  16391. "47123188f8dc551054ee162b634d60f097f719076640e209"
  16392. "80a0093113a8bd73";
  16393. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  16394. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  16395. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  16396. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  16397. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  16398. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  16399. "76341a7e7d9";
  16400. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  16401. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  16402. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  16403. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  16404. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  16405. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  16406. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  16407. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  16408. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  16409. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  16410. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  16411. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  16412. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  16413. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  16414. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  16415. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  16416. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  16417. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  16418. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  16419. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  16420. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  16421. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  16422. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  16423. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  16424. byte pOut[MAX_DSA_PARAM_SIZE];
  16425. byte qOut[MAX_DSA_PARAM_SIZE];
  16426. byte gOut[MAX_DSA_PARAM_SIZE];
  16427. word32 pOutSz, qOutSz, gOutSz;
  16428. printf(testingFmt, "wc_DsaExportParamsRaw()");
  16429. ret = wc_InitDsaKey(&key);
  16430. if (ret == 0) {
  16431. /* first test using imported raw parameters, for expected */
  16432. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  16433. }
  16434. if (ret == 0) {
  16435. pOutSz = sizeof(pOut);
  16436. qOutSz = sizeof(qOut);
  16437. gOutSz = sizeof(gOut);
  16438. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  16439. gOut, &gOutSz);
  16440. }
  16441. if (ret == 0) {
  16442. /* validate exported parameters are correct */
  16443. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  16444. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  16445. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  16446. ret = -1;
  16447. }
  16448. }
  16449. /* test bad args */
  16450. if (ret == 0) {
  16451. /* null key struct */
  16452. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  16453. gOut, &gOutSz);
  16454. if (ret == BAD_FUNC_ARG) {
  16455. /* null output pointers */
  16456. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  16457. NULL, &gOutSz);
  16458. }
  16459. if (ret == LENGTH_ONLY_E) {
  16460. /* null output size pointers */
  16461. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  16462. gOut, NULL);
  16463. }
  16464. if (ret == BAD_FUNC_ARG) {
  16465. /* p output buffer size too small */
  16466. pOutSz = 1;
  16467. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  16468. gOut, &gOutSz);
  16469. pOutSz = sizeof(pOut);
  16470. }
  16471. if (ret == BUFFER_E) {
  16472. /* q output buffer size too small */
  16473. qOutSz = 1;
  16474. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  16475. gOut, &gOutSz);
  16476. qOutSz = sizeof(qOut);
  16477. }
  16478. if (ret == BUFFER_E) {
  16479. /* g output buffer size too small */
  16480. gOutSz = 1;
  16481. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  16482. gOut, &gOutSz);
  16483. if (ret == BUFFER_E)
  16484. ret = 0;
  16485. }
  16486. }
  16487. printf(resultFmt, ret == 0 ? passed : failed);
  16488. wc_FreeDsaKey(&key);
  16489. #endif
  16490. return ret;
  16491. } /* END test_wc_DsaExportParamsRaw */
  16492. /*
  16493. * Testing wc_DsaExportKeyRaw()
  16494. */
  16495. static int test_wc_DsaExportKeyRaw (void)
  16496. {
  16497. int ret = 0;
  16498. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  16499. DsaKey key;
  16500. WC_RNG rng;
  16501. byte xOut[MAX_DSA_PARAM_SIZE];
  16502. byte yOut[MAX_DSA_PARAM_SIZE];
  16503. word32 xOutSz, yOutSz;
  16504. printf(testingFmt, "wc_DsaExportKeyRaw()");
  16505. XMEMSET(&rng, 0, sizeof(rng));
  16506. XMEMSET(&key, 0, sizeof(key));
  16507. ret = wc_InitRng(&rng);
  16508. if (ret == 0) {
  16509. ret = wc_InitDsaKey(&key);
  16510. }
  16511. if (ret == 0) {
  16512. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  16513. if (ret == 0) {
  16514. ret = wc_MakeDsaKey(&rng, &key);
  16515. }
  16516. }
  16517. /* try successful export */
  16518. if (ret == 0) {
  16519. xOutSz = sizeof(xOut);
  16520. yOutSz = sizeof(yOut);
  16521. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  16522. }
  16523. /* test bad args */
  16524. if (ret == 0) {
  16525. /* null key struct */
  16526. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  16527. if (ret == BAD_FUNC_ARG) {
  16528. /* null output pointers */
  16529. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  16530. }
  16531. if (ret == LENGTH_ONLY_E) {
  16532. /* null output size pointers */
  16533. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  16534. }
  16535. if (ret == BAD_FUNC_ARG) {
  16536. /* x output buffer size too small */
  16537. xOutSz = 1;
  16538. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  16539. xOutSz = sizeof(xOut);
  16540. }
  16541. if (ret == BUFFER_E) {
  16542. /* y output buffer size too small */
  16543. yOutSz = 1;
  16544. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  16545. if (ret == BUFFER_E)
  16546. ret = 0;
  16547. }
  16548. }
  16549. printf(resultFmt, ret == 0 ? passed : failed);
  16550. wc_FreeDsaKey(&key);
  16551. wc_FreeRng(&rng);
  16552. #endif
  16553. return ret;
  16554. } /* END test_wc_DsaExportParamsRaw */
  16555. /*
  16556. * Testing wc_ed25519_make_key().
  16557. */
  16558. static int test_wc_ed25519_make_key (void)
  16559. {
  16560. int ret = 0;
  16561. #if defined(HAVE_ED25519)
  16562. ed25519_key key;
  16563. WC_RNG rng;
  16564. ret = wc_InitRng(&rng);
  16565. if (ret == 0) {
  16566. ret = wc_ed25519_init(&key);
  16567. }
  16568. printf(testingFmt, "wc_ed25519_make_key()");
  16569. if (ret == 0) {
  16570. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  16571. }
  16572. /* Test bad args. */
  16573. if (ret == 0) {
  16574. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  16575. if (ret == BAD_FUNC_ARG) {
  16576. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  16577. }
  16578. if (ret == BAD_FUNC_ARG) {
  16579. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  16580. }
  16581. if (ret == BAD_FUNC_ARG) {
  16582. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  16583. }
  16584. if (ret == BAD_FUNC_ARG) {
  16585. ret = 0;
  16586. } else if (ret == 0) {
  16587. ret = SSL_FATAL_ERROR;
  16588. }
  16589. }
  16590. printf(resultFmt, ret == 0 ? passed : failed);
  16591. if (wc_FreeRng(&rng) && ret == 0) {
  16592. ret = SSL_FATAL_ERROR;
  16593. }
  16594. wc_ed25519_free(&key);
  16595. #endif
  16596. return ret;
  16597. } /* END test_wc_ed25519_make_key */
  16598. /*
  16599. * Testing wc_ed25519_init()
  16600. */
  16601. static int test_wc_ed25519_init (void)
  16602. {
  16603. int ret = 0;
  16604. #if defined(HAVE_ED25519)
  16605. ed25519_key key;
  16606. printf(testingFmt, "wc_ed25519_init()");
  16607. ret = wc_ed25519_init(&key);
  16608. /* Test bad args. */
  16609. if (ret == 0) {
  16610. ret = wc_ed25519_init(NULL);
  16611. if (ret == BAD_FUNC_ARG) {
  16612. ret = 0;
  16613. } else if (ret == 0) {
  16614. ret = SSL_FATAL_ERROR;
  16615. }
  16616. }
  16617. printf(resultFmt, ret == 0 ? passed : failed);
  16618. wc_ed25519_free(&key);
  16619. #endif
  16620. return ret;
  16621. } /* END test_wc_ed25519_init */
  16622. /*
  16623. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  16624. */
  16625. static int test_wc_ed25519_sign_msg (void)
  16626. {
  16627. int ret = 0;
  16628. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  16629. WC_RNG rng;
  16630. ed25519_key key;
  16631. byte msg[] = "Everybody gets Friday off.\n";
  16632. byte sig[ED25519_SIG_SIZE];
  16633. word32 msglen = sizeof(msg);
  16634. word32 siglen = sizeof(sig);
  16635. word32 badSigLen = sizeof(sig) - 1;
  16636. #ifdef HAVE_ED25519_VERIFY
  16637. int verify_ok = 0; /*1 = Verify success.*/
  16638. #endif
  16639. /* Initialize stack variables. */
  16640. XMEMSET(sig, 0, siglen);
  16641. /* Initialize key. */
  16642. ret = wc_InitRng(&rng);
  16643. if (ret == 0) {
  16644. ret = wc_ed25519_init(&key);
  16645. if (ret == 0) {
  16646. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  16647. }
  16648. }
  16649. printf(testingFmt, "wc_ed25519_sign_msg()");
  16650. if (ret == 0) {
  16651. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  16652. }
  16653. /* Test bad args. */
  16654. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  16655. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  16656. if (ret == BAD_FUNC_ARG) {
  16657. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  16658. }
  16659. if (ret == BAD_FUNC_ARG) {
  16660. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  16661. }
  16662. if (ret == BAD_FUNC_ARG) {
  16663. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  16664. }
  16665. if (ret == BAD_FUNC_ARG) {
  16666. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  16667. }
  16668. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  16669. badSigLen -= 1;
  16670. ret = 0;
  16671. } else if (ret == 0) {
  16672. ret = SSL_FATAL_ERROR;
  16673. }
  16674. } /* END sign */
  16675. printf(resultFmt, ret == 0 ? passed : failed);
  16676. #ifdef HAVE_ED25519_VERIFY
  16677. printf(testingFmt, "wc_ed25519_verify_msg()");
  16678. if (ret == 0) {
  16679. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  16680. if (ret == 0 && verify_ok == 1) {
  16681. ret = 0;
  16682. } else if (ret == 0) {
  16683. ret = SSL_FATAL_ERROR;
  16684. }
  16685. /* Test bad args. */
  16686. if (ret == 0) {
  16687. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  16688. msglen, &verify_ok, &key),
  16689. BAD_FUNC_ARG);
  16690. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  16691. msglen, &verify_ok, &key),
  16692. BAD_FUNC_ARG);
  16693. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  16694. &key);
  16695. if (ret == BAD_FUNC_ARG) {
  16696. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  16697. &verify_ok, &key);
  16698. }
  16699. if (ret == BAD_FUNC_ARG) {
  16700. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  16701. NULL, &key);
  16702. }
  16703. if (ret == BAD_FUNC_ARG) {
  16704. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  16705. &verify_ok, NULL);
  16706. }
  16707. if (ret == BAD_FUNC_ARG) {
  16708. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  16709. &verify_ok, &key);
  16710. }
  16711. if (ret == BAD_FUNC_ARG) {
  16712. ret = 0;
  16713. } else if (ret == 0) {
  16714. ret = SSL_FATAL_ERROR;
  16715. }
  16716. }
  16717. } /* END verify. */
  16718. printf(resultFmt, ret == 0 ? passed : failed);
  16719. #endif /* Verify. */
  16720. if (wc_FreeRng(&rng) && ret == 0) {
  16721. ret = SSL_FATAL_ERROR;
  16722. }
  16723. wc_ed25519_free(&key);
  16724. #endif
  16725. return ret;
  16726. } /* END test_wc_ed25519_sign_msg */
  16727. /*
  16728. * Testing wc_ed25519_import_public()
  16729. */
  16730. static int test_wc_ed25519_import_public (void)
  16731. {
  16732. int ret = 0;
  16733. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  16734. WC_RNG rng;
  16735. ed25519_key pubKey;
  16736. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  16737. word32 inlen = sizeof(in);
  16738. ret = wc_InitRng(&rng);
  16739. if (ret == 0) {
  16740. ret = wc_ed25519_init(&pubKey);
  16741. if (ret == 0) {
  16742. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  16743. }
  16744. }
  16745. printf(testingFmt, "wc_ed25519_import_public()");
  16746. if (ret == 0) {
  16747. ret = wc_ed25519_import_public(in, inlen, &pubKey);
  16748. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  16749. ret = 0;
  16750. } else {
  16751. ret = SSL_FATAL_ERROR;
  16752. }
  16753. /* Test bad args. */
  16754. if (ret == 0) {
  16755. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  16756. if (ret == BAD_FUNC_ARG) {
  16757. ret = wc_ed25519_import_public(in, inlen, NULL);
  16758. }
  16759. if (ret == BAD_FUNC_ARG) {
  16760. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  16761. }
  16762. if (ret == BAD_FUNC_ARG) {
  16763. ret = 0;
  16764. } else if (ret == 0) {
  16765. ret = SSL_FATAL_ERROR;
  16766. }
  16767. }
  16768. }
  16769. printf(resultFmt, ret == 0 ? passed : failed);
  16770. if (wc_FreeRng(&rng) && ret == 0) {
  16771. ret = SSL_FATAL_ERROR;
  16772. }
  16773. wc_ed25519_free(&pubKey);
  16774. #endif
  16775. return ret;
  16776. } /* END wc_ed25519_import_public */
  16777. /*
  16778. * Testing wc_ed25519_import_private_key()
  16779. */
  16780. static int test_wc_ed25519_import_private_key (void)
  16781. {
  16782. int ret = 0;
  16783. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  16784. WC_RNG rng;
  16785. ed25519_key key;
  16786. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  16787. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  16788. word32 privKeySz = sizeof(privKey);
  16789. word32 pubKeySz = sizeof(pubKey);
  16790. #ifdef HAVE_ED25519_KEY_EXPORT
  16791. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  16792. word32 bothKeysSz = sizeof(bothKeys);
  16793. #endif
  16794. ret = wc_InitRng(&rng);
  16795. if (ret != 0) {
  16796. return ret;
  16797. }
  16798. ret = wc_ed25519_init(&key);
  16799. if (ret != 0) {
  16800. wc_FreeRng(&rng);
  16801. return ret;
  16802. }
  16803. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  16804. printf(testingFmt, "wc_ed25519_import_private_key()");
  16805. if (ret == 0) {
  16806. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  16807. pubKeySz, &key);
  16808. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  16809. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  16810. ret = SSL_FATAL_ERROR;
  16811. }
  16812. }
  16813. #ifdef HAVE_ED25519_KEY_EXPORT
  16814. if (ret == 0)
  16815. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  16816. if (ret == 0) {
  16817. ret = wc_ed25519_import_private_key(bothKeys, bothKeysSz, NULL, 0, &key);
  16818. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  16819. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  16820. ret = SSL_FATAL_ERROR;
  16821. }
  16822. }
  16823. #endif
  16824. /* Test bad args. */
  16825. if (ret == 0) {
  16826. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  16827. &key);
  16828. if (ret == BAD_FUNC_ARG) {
  16829. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  16830. pubKeySz, &key);
  16831. }
  16832. if (ret == BAD_FUNC_ARG) {
  16833. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  16834. pubKeySz, NULL);
  16835. }
  16836. if (ret == BAD_FUNC_ARG) {
  16837. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  16838. pubKeySz, &key);
  16839. }
  16840. if (ret == BAD_FUNC_ARG) {
  16841. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  16842. pubKeySz - 1, &key);
  16843. }
  16844. if (ret == BAD_FUNC_ARG) {
  16845. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  16846. 0, &key);
  16847. }
  16848. if (ret == BAD_FUNC_ARG) {
  16849. ret = 0;
  16850. } else if (ret == 0) {
  16851. ret = SSL_FATAL_ERROR;
  16852. }
  16853. }
  16854. printf(resultFmt, ret == 0 ? passed : failed);
  16855. if (wc_FreeRng(&rng) && ret == 0) {
  16856. ret = SSL_FATAL_ERROR;
  16857. }
  16858. wc_ed25519_free(&key);
  16859. #endif
  16860. return ret;
  16861. } /* END test_wc_ed25519_import_private_key */
  16862. /*
  16863. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  16864. */
  16865. static int test_wc_ed25519_export (void)
  16866. {
  16867. int ret = 0;
  16868. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  16869. WC_RNG rng;
  16870. ed25519_key key;
  16871. byte priv[ED25519_PRV_KEY_SIZE];
  16872. byte pub[ED25519_PUB_KEY_SIZE];
  16873. word32 privSz = sizeof(priv);
  16874. word32 pubSz = sizeof(pub);
  16875. ret = wc_InitRng(&rng);
  16876. if (ret != 0) {
  16877. return ret;
  16878. }
  16879. ret = wc_ed25519_init(&key);
  16880. if (ret != 0) {
  16881. wc_FreeRng(&rng);
  16882. return ret;
  16883. }
  16884. if (ret == 0) {
  16885. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  16886. }
  16887. printf(testingFmt, "wc_ed25519_export_public()");
  16888. if (ret == 0) {
  16889. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  16890. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  16891. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  16892. ret = SSL_FATAL_ERROR;
  16893. }
  16894. if (ret == 0) {
  16895. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  16896. if (ret == BAD_FUNC_ARG) {
  16897. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  16898. }
  16899. if (ret == BAD_FUNC_ARG) {
  16900. ret = wc_ed25519_export_public(&key, pub, NULL);
  16901. }
  16902. if (ret == BAD_FUNC_ARG) {
  16903. ret = 0;
  16904. } else if (ret == 0) {
  16905. ret = SSL_FATAL_ERROR;
  16906. }
  16907. }
  16908. }
  16909. printf(resultFmt, ret == 0 ? passed : failed);
  16910. printf(testingFmt, "wc_ed25519_export_private_only()");
  16911. if (ret == 0) {
  16912. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  16913. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  16914. || XMEMCMP(key.k, priv, privSz) != 0)) {
  16915. ret = SSL_FATAL_ERROR;
  16916. }
  16917. if (ret == 0) {
  16918. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  16919. if (ret == BAD_FUNC_ARG) {
  16920. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  16921. }
  16922. if (ret == BAD_FUNC_ARG) {
  16923. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  16924. }
  16925. if (ret == BAD_FUNC_ARG) {
  16926. ret = 0;
  16927. } else if (ret == 0) {
  16928. ret = SSL_FATAL_ERROR;
  16929. }
  16930. }
  16931. }
  16932. printf(resultFmt, ret == 0 ? passed : failed);
  16933. if (wc_FreeRng(&rng) && ret == 0) {
  16934. ret = SSL_FATAL_ERROR;
  16935. }
  16936. wc_ed25519_free(&key);
  16937. #endif
  16938. return ret;
  16939. } /* END test_wc_ed25519_export */
  16940. /*
  16941. * Testing wc_ed25519_size()
  16942. */
  16943. static int test_wc_ed25519_size (void)
  16944. {
  16945. int ret = 0;
  16946. #if defined(HAVE_ED25519)
  16947. WC_RNG rng;
  16948. ed25519_key key;
  16949. ret = wc_InitRng(&rng);
  16950. if (ret != 0) {
  16951. return ret;
  16952. }
  16953. ret = wc_ed25519_init(&key);
  16954. if (ret != 0) {
  16955. wc_FreeRng(&rng);
  16956. return ret;
  16957. }
  16958. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  16959. if (ret != 0) {
  16960. wc_FreeRng(&rng);
  16961. wc_ed25519_free(&key);
  16962. return ret;
  16963. }
  16964. printf(testingFmt, "wc_ed25519_size()");
  16965. ret = wc_ed25519_size(&key);
  16966. /* Test bad args. */
  16967. if (ret == ED25519_KEY_SIZE) {
  16968. ret = wc_ed25519_size(NULL);
  16969. if (ret == BAD_FUNC_ARG) {
  16970. ret = 0;
  16971. }
  16972. }
  16973. printf(resultFmt, ret == 0 ? passed : failed);
  16974. if (ret == 0) {
  16975. printf(testingFmt, "wc_ed25519_sig_size()");
  16976. ret = wc_ed25519_sig_size(&key);
  16977. if (ret == ED25519_SIG_SIZE) {
  16978. ret = 0;
  16979. }
  16980. /* Test bad args. */
  16981. if (ret == 0) {
  16982. ret = wc_ed25519_sig_size(NULL);
  16983. if (ret == BAD_FUNC_ARG) {
  16984. ret = 0;
  16985. }
  16986. }
  16987. printf(resultFmt, ret == 0 ? passed : failed);
  16988. } /* END wc_ed25519_sig_size() */
  16989. if (ret == 0) {
  16990. printf(testingFmt, "wc_ed25519_pub_size");
  16991. ret = wc_ed25519_pub_size(&key);
  16992. if (ret == ED25519_PUB_KEY_SIZE) {
  16993. ret = 0;
  16994. }
  16995. if (ret == 0) {
  16996. ret = wc_ed25519_pub_size(NULL);
  16997. if (ret == BAD_FUNC_ARG) {
  16998. ret = 0;
  16999. }
  17000. }
  17001. printf(resultFmt, ret == 0 ? passed : failed);
  17002. } /* END wc_ed25519_pub_size */
  17003. if (ret == 0) {
  17004. printf(testingFmt, "wc_ed25519_priv_size");
  17005. ret = wc_ed25519_priv_size(&key);
  17006. if (ret == ED25519_PRV_KEY_SIZE) {
  17007. ret = 0;
  17008. }
  17009. if (ret == 0) {
  17010. ret = wc_ed25519_priv_size(NULL);
  17011. if (ret == BAD_FUNC_ARG) {
  17012. ret = 0;
  17013. }
  17014. }
  17015. printf(resultFmt, ret == 0 ? passed : failed);
  17016. } /* END wc_ed25519_pub_size */
  17017. if (wc_FreeRng(&rng) && ret == 0) {
  17018. ret = SSL_FATAL_ERROR;
  17019. }
  17020. wc_ed25519_free(&key);
  17021. #endif
  17022. return ret;
  17023. } /* END test_wc_ed25519_size */
  17024. /*
  17025. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  17026. */
  17027. static int test_wc_ed25519_exportKey (void)
  17028. {
  17029. int ret = 0;
  17030. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  17031. WC_RNG rng;
  17032. ed25519_key key;
  17033. byte priv[ED25519_PRV_KEY_SIZE];
  17034. byte pub[ED25519_PUB_KEY_SIZE];
  17035. byte privOnly[ED25519_PRV_KEY_SIZE];
  17036. word32 privSz = sizeof(priv);
  17037. word32 pubSz = sizeof(pub);
  17038. word32 privOnlySz = sizeof(privOnly);
  17039. ret = wc_InitRng(&rng);
  17040. if (ret != 0) {
  17041. return ret;
  17042. }
  17043. ret = wc_ed25519_init(&key);
  17044. if (ret != 0) {
  17045. wc_FreeRng(&rng);
  17046. return ret;
  17047. }
  17048. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  17049. if (ret != 0) {
  17050. wc_FreeRng(&rng);
  17051. wc_ed25519_free(&key);
  17052. return ret;
  17053. }
  17054. printf(testingFmt, "wc_ed25519_export_private()");
  17055. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  17056. if (ret == 0) {
  17057. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  17058. if (ret == BAD_FUNC_ARG) {
  17059. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  17060. }
  17061. if (ret == BAD_FUNC_ARG) {
  17062. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  17063. }
  17064. if (ret == BAD_FUNC_ARG) {
  17065. ret = 0;
  17066. } else if (ret == 0) {
  17067. ret = SSL_FATAL_ERROR;
  17068. }
  17069. }
  17070. printf(resultFmt, ret == 0 ? passed : failed);
  17071. if (ret == 0) {
  17072. printf(testingFmt, "wc_ed25519_export_key()");
  17073. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  17074. if (ret == 0) {
  17075. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  17076. if (ret == BAD_FUNC_ARG) {
  17077. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  17078. }
  17079. if (ret == BAD_FUNC_ARG) {
  17080. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  17081. }
  17082. if (ret == BAD_FUNC_ARG) {
  17083. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  17084. }
  17085. if (ret == BAD_FUNC_ARG) {
  17086. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  17087. }
  17088. if (ret == BAD_FUNC_ARG) {
  17089. ret = 0;
  17090. } else if (ret == 0) {
  17091. ret = SSL_FATAL_ERROR;
  17092. }
  17093. }
  17094. printf(resultFmt, ret == 0 ? passed : failed);
  17095. } /* END wc_ed25519_export_key() */
  17096. /* Cross check output. */
  17097. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  17098. ret = SSL_FATAL_ERROR;
  17099. }
  17100. if (wc_FreeRng(&rng) && ret == 0) {
  17101. ret = SSL_FATAL_ERROR;
  17102. }
  17103. wc_ed25519_free(&key);
  17104. #endif
  17105. return ret;
  17106. } /* END test_wc_ed25519_exportKey */
  17107. /*
  17108. * Testing wc_Ed25519PublicKeyToDer
  17109. */
  17110. static int test_wc_Ed25519PublicKeyToDer (void)
  17111. {
  17112. int ret = 0;
  17113. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  17114. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  17115. int tmp;
  17116. ed25519_key key;
  17117. byte derBuf[1024];
  17118. printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
  17119. /* Test bad args */
  17120. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  17121. if (tmp != BAD_FUNC_ARG) {
  17122. ret = SSL_FATAL_ERROR;
  17123. }
  17124. if (ret == 0) {
  17125. wc_ed25519_init(&key);
  17126. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  17127. if (tmp != BUFFER_E) {
  17128. ret = SSL_FATAL_ERROR;
  17129. }
  17130. wc_ed25519_free(&key);
  17131. }
  17132. /* Test good args */
  17133. if (ret == 0) {
  17134. WC_RNG rng;
  17135. ret = wc_InitRng(&rng);
  17136. if (ret != 0) {
  17137. return ret;
  17138. }
  17139. ret = wc_ed25519_init(&key);
  17140. if (ret != 0) {
  17141. wc_FreeRng(&rng);
  17142. return ret;
  17143. }
  17144. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  17145. if (ret != 0) {
  17146. wc_FreeRng(&rng);
  17147. wc_ed25519_free(&key);
  17148. return ret;
  17149. }
  17150. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  17151. if (tmp <= 0) {
  17152. ret = SSL_FATAL_ERROR;
  17153. }
  17154. wc_FreeRng(&rng);
  17155. wc_ed25519_free(&key);
  17156. }
  17157. printf(resultFmt, ret == 0 ? passed : failed);
  17158. #endif
  17159. return ret;
  17160. } /* END testing wc_Ed25519PublicKeyToDer */
  17161. /*
  17162. * Testing wc_curve25519_init and wc_curve25519_free.
  17163. */
  17164. static int test_wc_curve25519_init (void)
  17165. {
  17166. int ret = 0;
  17167. #if defined(HAVE_CURVE25519)
  17168. curve25519_key key;
  17169. printf(testingFmt, "wc_curve25519_init()");
  17170. ret = wc_curve25519_init(&key);
  17171. /* Test bad args for wc_curve25519_init */
  17172. if (ret == 0) {
  17173. ret = wc_curve25519_init(NULL);
  17174. if (ret == BAD_FUNC_ARG) {
  17175. ret = 0;
  17176. } else if (ret == 0) {
  17177. ret = SSL_FATAL_ERROR;
  17178. }
  17179. }
  17180. printf(resultFmt, ret == 0 ? passed : failed);
  17181. /* Test good args for wc_curve_25519_free */
  17182. wc_curve25519_free(&key);
  17183. wc_curve25519_free(NULL);
  17184. #endif
  17185. return ret;
  17186. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  17187. /*
  17188. * Testing test_wc_curve25519_size.
  17189. */
  17190. static int test_wc_curve25519_size (void)
  17191. {
  17192. int ret = 0;
  17193. #if defined(HAVE_CURVE25519)
  17194. curve25519_key key;
  17195. printf(testingFmt, "wc_curve25519_size()");
  17196. ret = wc_curve25519_init(&key);
  17197. /* Test good args for wc_curve25519_size */
  17198. if (ret == 0) {
  17199. ret = wc_curve25519_size(&key);
  17200. }
  17201. /* Test bad args for wc_curve25519_size */
  17202. if (ret != 0) {
  17203. ret = wc_curve25519_size(NULL);
  17204. }
  17205. printf(resultFmt, ret == 0 ? passed : failed);
  17206. wc_curve25519_free(&key);
  17207. #endif
  17208. return ret;
  17209. } /* END test_wc_curve25519_size*/
  17210. /*
  17211. * Testing test_wc_curve25519_export_key_raw().
  17212. */
  17213. static int test_wc_curve25519_export_key_raw (void)
  17214. {
  17215. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  17216. curve25519_key key;
  17217. WC_RNG rng;
  17218. byte privateKey[CURVE25519_KEYSIZE];
  17219. byte publicKey[CURVE25519_KEYSIZE];
  17220. word32 prvkSz;
  17221. word32 pubkSz;
  17222. byte prik[CURVE25519_KEYSIZE];
  17223. byte pubk[CURVE25519_KEYSIZE];
  17224. word32 prksz;
  17225. word32 pbksz;
  17226. printf(testingFmt, "wc_curve25519_export_key_raw()");
  17227. if(0 != wc_InitRng(&rng)){
  17228. printf(testingFmt, "failed due to wc_InitRng");
  17229. fflush( stdout );
  17230. return 1;
  17231. }
  17232. if(0 != wc_curve25519_init(&key)){
  17233. printf(testingFmt, "failed due to wc_curve25519_init");
  17234. fflush( stdout );
  17235. wc_FreeRng(&rng);
  17236. return 1;
  17237. }
  17238. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  17239. printf(testingFmt, "failed due to wc_curve25519_make_key");
  17240. fflush( stdout );
  17241. wc_curve25519_free(&key);
  17242. wc_FreeRng(&rng);
  17243. return 1;
  17244. }
  17245. /*
  17246. bad-argument-test cases
  17247. target function sould return BAD_FUNC_ARG
  17248. */
  17249. prvkSz = CURVE25519_KEYSIZE;
  17250. pubkSz = CURVE25519_KEYSIZE;
  17251. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  17252. NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
  17253. printf(testingFmt,"failed at bad-arg-case-1.");
  17254. fflush( stdout );
  17255. wc_curve25519_free(&key);
  17256. wc_FreeRng(&rng);
  17257. return 1;
  17258. }
  17259. prvkSz = CURVE25519_KEYSIZE;
  17260. pubkSz = CURVE25519_KEYSIZE;
  17261. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  17262. &key , NULL, &prvkSz, publicKey, &pubkSz)){
  17263. printf(testingFmt,"failed at bad-arg-case-2.");
  17264. fflush( stdout );
  17265. wc_curve25519_free(&key);
  17266. wc_FreeRng(&rng);
  17267. return 1;
  17268. }
  17269. prvkSz = CURVE25519_KEYSIZE;
  17270. pubkSz = CURVE25519_KEYSIZE;
  17271. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  17272. &key , privateKey, NULL, publicKey, &pubkSz)){
  17273. printf(testingFmt,"failed at bad-arg-case-3.");
  17274. fflush( stdout );
  17275. wc_curve25519_free(&key);
  17276. wc_FreeRng(&rng);
  17277. return 1;
  17278. }
  17279. /* prvkSz = CURVE25519_KEYSIZE; */
  17280. pubkSz = CURVE25519_KEYSIZE;
  17281. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  17282. &key , privateKey, &prvkSz, NULL, &pubkSz)){
  17283. printf(testingFmt,"failed at bad-arg-case-4.");
  17284. fflush( stdout );
  17285. wc_curve25519_free(&key);
  17286. wc_FreeRng(&rng);
  17287. return 1;
  17288. }
  17289. prvkSz = CURVE25519_KEYSIZE;
  17290. pubkSz = CURVE25519_KEYSIZE;
  17291. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  17292. &key , privateKey, &prvkSz, publicKey, NULL )){
  17293. printf(testingFmt,"failed at bad-arg-case-5.");
  17294. fflush( stdout );
  17295. wc_curve25519_free(&key);
  17296. wc_FreeRng(&rng);
  17297. return 1;
  17298. }
  17299. /*
  17300. cross-testing
  17301. */
  17302. prksz = CURVE25519_KEYSIZE;
  17303. if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
  17304. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  17305. fflush( stdout );
  17306. wc_curve25519_free(&key);
  17307. wc_FreeRng(&rng);
  17308. return 1;
  17309. }
  17310. pbksz = CURVE25519_KEYSIZE;
  17311. if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
  17312. printf(testingFmt,"failed due to wc_curve25519_export_public");
  17313. fflush( stdout );
  17314. wc_curve25519_free(&key);
  17315. wc_FreeRng(&rng);
  17316. return 1;
  17317. }
  17318. prvkSz = CURVE25519_KEYSIZE;
  17319. /* pubkSz = CURVE25519_KEYSIZE; */
  17320. if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  17321. publicKey, &pubkSz)){
  17322. printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
  17323. fflush( stdout );
  17324. wc_curve25519_free(&key);
  17325. wc_FreeRng(&rng);
  17326. return 1;
  17327. }
  17328. if((prksz == CURVE25519_KEYSIZE) &&
  17329. (pbksz == CURVE25519_KEYSIZE) &&
  17330. (prvkSz == CURVE25519_KEYSIZE) &&
  17331. (pubkSz == CURVE25519_KEYSIZE)){
  17332. if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) &&
  17333. 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){
  17334. printf(resultFmt,passed);
  17335. fflush( stdout );
  17336. wc_curve25519_free(&key);
  17337. wc_FreeRng(&rng);
  17338. return 0;
  17339. }
  17340. else{
  17341. printf(testingFmt,"failed due to key-contents-inconsistency.");
  17342. fflush( stdout );
  17343. wc_curve25519_free(&key);
  17344. wc_FreeRng(&rng);
  17345. return 1;
  17346. }
  17347. }
  17348. else{
  17349. printf(testingFmt,"failed due to bad-key-size.");
  17350. fflush( stdout );
  17351. wc_curve25519_free(&key);
  17352. wc_FreeRng(&rng);
  17353. return 1;
  17354. }
  17355. #endif
  17356. fflush( stdout );
  17357. return 0;
  17358. } /* end of test_wc_curve25519_export_key_raw */
  17359. /*
  17360. * Testing test_wc_curve25519_export_key_raw_ex().
  17361. */
  17362. static int test_wc_curve25519_export_key_raw_ex (void)
  17363. {
  17364. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  17365. curve25519_key key;
  17366. WC_RNG rng;
  17367. byte privateKey[CURVE25519_KEYSIZE];
  17368. byte publicKey[CURVE25519_KEYSIZE];
  17369. word32 prvkSz;
  17370. word32 pubkSz;
  17371. byte prik[CURVE25519_KEYSIZE];
  17372. byte pubk[CURVE25519_KEYSIZE];
  17373. word32 prksz;
  17374. word32 pbksz;
  17375. printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
  17376. if(0 != wc_InitRng(&rng)){
  17377. printf(testingFmt, "failed due to wc_InitRng");
  17378. fflush( stdout );
  17379. return 1;
  17380. }
  17381. if(0 != wc_curve25519_init(&key)){
  17382. printf(testingFmt, "failed due to wc_curve25519_init");
  17383. fflush( stdout );
  17384. wc_FreeRng(&rng);
  17385. return 1;
  17386. }
  17387. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  17388. printf(testingFmt, "failed due to wc_curve25519_make_key");
  17389. fflush( stdout );
  17390. wc_curve25519_free(&key);
  17391. wc_FreeRng(&rng);
  17392. return 1;
  17393. }
  17394. /*
  17395. bad-argument-test cases
  17396. target function sould return BAD_FUNC_ARG
  17397. */
  17398. prvkSz = CURVE25519_KEYSIZE;
  17399. pubkSz = CURVE25519_KEYSIZE;
  17400. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  17401. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  17402. printf(testingFmt,"failed at bad-arg-case-1.");
  17403. fflush( stdout );
  17404. wc_curve25519_free(&key);
  17405. wc_FreeRng(&rng);
  17406. return 1;
  17407. }
  17408. prvkSz = CURVE25519_KEYSIZE;
  17409. pubkSz = CURVE25519_KEYSIZE;
  17410. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  17411. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  17412. printf(testingFmt,"failed at bad-arg-case-2.");
  17413. fflush( stdout );
  17414. wc_curve25519_free(&key);
  17415. wc_FreeRng(&rng);
  17416. return 1;
  17417. }
  17418. prvkSz = CURVE25519_KEYSIZE;
  17419. pubkSz = CURVE25519_KEYSIZE;
  17420. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  17421. NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
  17422. printf(testingFmt,"failed at bad-arg-case-3.");
  17423. fflush( stdout );
  17424. wc_curve25519_free(&key);
  17425. wc_FreeRng(&rng);
  17426. return 1;
  17427. }
  17428. /* prvkSz = CURVE25519_KEYSIZE; */
  17429. pubkSz = CURVE25519_KEYSIZE;
  17430. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  17431. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
  17432. printf(testingFmt,"failed at bad-arg-case-4.");
  17433. fflush( stdout );
  17434. wc_curve25519_free(&key);
  17435. wc_FreeRng(&rng);
  17436. return 1;
  17437. }
  17438. prvkSz = CURVE25519_KEYSIZE;
  17439. pubkSz = CURVE25519_KEYSIZE;
  17440. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  17441. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
  17442. printf(testingFmt,"failed at bad-arg-case-5.");
  17443. fflush( stdout );
  17444. wc_curve25519_free(&key);
  17445. wc_FreeRng(&rng);
  17446. return 1;
  17447. }
  17448. prvkSz = CURVE25519_KEYSIZE;
  17449. /* pubkSz = CURVE25519_KEYSIZE; */
  17450. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  17451. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  17452. printf(testingFmt,"failed at bad-arg-case-6.");
  17453. fflush( stdout );
  17454. wc_curve25519_free(&key);
  17455. wc_FreeRng(&rng);
  17456. return 1;
  17457. }
  17458. prvkSz = CURVE25519_KEYSIZE;
  17459. pubkSz = CURVE25519_KEYSIZE;
  17460. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
  17461. publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  17462. printf(testingFmt,"failed at bad-arg-case-7.");
  17463. fflush( stdout );
  17464. wc_curve25519_free(&key);
  17465. wc_FreeRng(&rng);
  17466. return 1;
  17467. }
  17468. prvkSz = CURVE25519_KEYSIZE;
  17469. pubkSz = CURVE25519_KEYSIZE;
  17470. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  17471. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  17472. printf(testingFmt,"failed at bad-arg-case-8.");
  17473. fflush( stdout );
  17474. wc_curve25519_free(&key);
  17475. wc_FreeRng(&rng);
  17476. return 1;
  17477. }
  17478. /* prvkSz = CURVE25519_KEYSIZE; */
  17479. pubkSz = CURVE25519_KEYSIZE;
  17480. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  17481. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
  17482. printf(testingFmt,"failed at bad-arg-case-9.");
  17483. fflush( stdout );
  17484. wc_curve25519_free(&key);
  17485. wc_FreeRng(&rng);
  17486. return 1;
  17487. }
  17488. prvkSz = CURVE25519_KEYSIZE;
  17489. pubkSz = CURVE25519_KEYSIZE;
  17490. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  17491. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
  17492. printf(testingFmt,"failed at bad-arg-case-10.");
  17493. fflush( stdout );
  17494. wc_curve25519_free(&key);
  17495. wc_FreeRng(&rng);
  17496. return 1;
  17497. }
  17498. /* illegal value for endien */
  17499. prvkSz = CURVE25519_KEYSIZE;
  17500. /* pubkSz = CURVE25519_KEYSIZE; */
  17501. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  17502. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){
  17503. printf(testingFmt,"failed at bad-arg-case-11.");
  17504. fflush( stdout );
  17505. wc_curve25519_free(&key);
  17506. wc_FreeRng(&rng);
  17507. return 1;
  17508. }
  17509. /*
  17510. cross-testing
  17511. */
  17512. prksz = CURVE25519_KEYSIZE;
  17513. if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
  17514. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  17515. fflush( stdout );
  17516. wc_curve25519_free(&key);
  17517. wc_FreeRng(&rng);
  17518. return 1;
  17519. }
  17520. pbksz = CURVE25519_KEYSIZE;
  17521. if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
  17522. printf(testingFmt,"failed due to wc_curve25519_export_public");
  17523. fflush( stdout );
  17524. wc_curve25519_free(&key);
  17525. wc_FreeRng(&rng);
  17526. return 1;
  17527. }
  17528. prvkSz = CURVE25519_KEYSIZE;
  17529. /* pubkSz = CURVE25519_KEYSIZE; */
  17530. if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  17531. publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  17532. printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
  17533. fflush( stdout );
  17534. wc_curve25519_free(&key);
  17535. wc_FreeRng(&rng);
  17536. return 1;
  17537. }
  17538. if( prksz == CURVE25519_KEYSIZE &&
  17539. pbksz == CURVE25519_KEYSIZE &&
  17540. prvkSz == CURVE25519_KEYSIZE &&
  17541. pubkSz == CURVE25519_KEYSIZE ){
  17542. if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) &&
  17543. 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){
  17544. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  17545. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  17546. if( prvkSz == CURVE25519_KEYSIZE &&
  17547. pubkSz == CURVE25519_KEYSIZE ){
  17548. ; /* proceed to the next test */
  17549. }
  17550. else{
  17551. printf(testingFmt,"failed due to key-size-inconsistency");
  17552. fflush( stdout );
  17553. wc_curve25519_free(&key);
  17554. wc_FreeRng(&rng);
  17555. return 1;
  17556. }
  17557. }
  17558. else{
  17559. printf(testingFmt,
  17560. "failed due to wc_curve25519_export_key_raw_ex");
  17561. fflush( stdout );
  17562. wc_curve25519_free(&key);
  17563. wc_FreeRng(&rng);
  17564. return 1;
  17565. }
  17566. }
  17567. else{
  17568. printf(testingFmt,"failed due to key-contents-inconsistency");
  17569. fflush( stdout );
  17570. wc_curve25519_free(&key);
  17571. wc_FreeRng(&rng);
  17572. return 1;
  17573. }
  17574. }
  17575. else{
  17576. printf(testingFmt,"failed due to bad-key-size");
  17577. fflush( stdout );
  17578. wc_curve25519_free(&key);
  17579. wc_FreeRng(&rng);
  17580. return 1;
  17581. }
  17582. /*
  17583. try once with another endian
  17584. */
  17585. prvkSz = CURVE25519_KEYSIZE;
  17586. pubkSz = CURVE25519_KEYSIZE;
  17587. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  17588. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  17589. if( prvkSz == CURVE25519_KEYSIZE &&
  17590. pubkSz == CURVE25519_KEYSIZE ){
  17591. /* no more test*/
  17592. printf(resultFmt, passed );
  17593. fflush( stdout );
  17594. wc_curve25519_free(&key);
  17595. wc_FreeRng(&rng);
  17596. return 0;
  17597. }
  17598. else{
  17599. printf(testingFmt,"failed due to key-size-inconsistency");
  17600. fflush( stdout );
  17601. wc_curve25519_free(&key);
  17602. wc_FreeRng(&rng);
  17603. return 1;
  17604. }
  17605. }
  17606. else{
  17607. printf(testingFmt,
  17608. "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
  17609. fflush( stdout );
  17610. wc_curve25519_free(&key);
  17611. wc_FreeRng(&rng);
  17612. return 1;
  17613. }
  17614. #endif
  17615. return 0;
  17616. } /* end of test_wc_curve25519_export_key_raw_ex */
  17617. /*
  17618. * Testing wc_curve25519_make_key
  17619. */
  17620. static int test_wc_curve25519_make_key (void)
  17621. {
  17622. int ret = 0;
  17623. #if defined(HAVE_CURVE25519)
  17624. WC_RNG rng;
  17625. curve25519_key key;
  17626. int keysize;
  17627. printf(testingFmt, "wc_curve25519_make_key()");
  17628. ret = wc_curve25519_init(&key);
  17629. if (ret == 0) {
  17630. ret = wc_InitRng(&rng);
  17631. }
  17632. if (ret == 0) {
  17633. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  17634. if (ret == 0) {
  17635. keysize = wc_curve25519_size(&key);
  17636. if (keysize != CURVE25519_KEYSIZE) {
  17637. ret = SSL_FATAL_ERROR;
  17638. }
  17639. }
  17640. if (ret == 0) {
  17641. ret = wc_curve25519_make_key(&rng, keysize, &key);
  17642. }
  17643. }
  17644. /*test bad cases*/
  17645. if (ret == 0) {
  17646. ret = wc_curve25519_make_key(NULL, 0, NULL);
  17647. if (ret == BAD_FUNC_ARG) {
  17648. ret = 0;
  17649. }
  17650. }
  17651. if (ret == 0) {
  17652. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  17653. if (ret == BAD_FUNC_ARG) {
  17654. ret = 0;
  17655. }
  17656. }
  17657. if (ret == 0) {
  17658. ret = wc_curve25519_make_key(NULL, keysize, &key);
  17659. if (ret == BAD_FUNC_ARG) {
  17660. ret = 0;
  17661. }
  17662. }
  17663. if (ret == 0) {
  17664. ret = wc_curve25519_make_key(&rng, 0, &key);
  17665. if (ret == ECC_BAD_ARG_E) {
  17666. ret = 0;
  17667. }
  17668. }
  17669. printf(resultFmt, ret == 0 ? passed : failed);
  17670. wc_curve25519_free(&key);
  17671. wc_FreeRng(&rng);
  17672. #endif
  17673. return ret;
  17674. } /*END test_wc_curve25519_make_key*/
  17675. /*
  17676. * Testing wc_curve25519_shared_secret_ex
  17677. */
  17678. static int test_wc_curve25519_shared_secret_ex(void)
  17679. {
  17680. int ret = 0;
  17681. #if defined(HAVE_CURVE25519)
  17682. WC_RNG rng;
  17683. curve25519_key private_key, public_key;
  17684. byte out[CURVE25519_KEYSIZE];
  17685. word32 outLen = sizeof(out);
  17686. int endian = EC25519_BIG_ENDIAN;
  17687. printf(testingFmt, "wc_curve25519_shared_secret_ex()");
  17688. ret = wc_curve25519_init(&private_key);
  17689. if (ret == 0) {
  17690. ret = wc_curve25519_init(&public_key);
  17691. }
  17692. if (ret == 0) {
  17693. ret = wc_InitRng(&rng);
  17694. }
  17695. if (ret == 0) {
  17696. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  17697. }
  17698. if (ret == 0) {
  17699. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  17700. }
  17701. if (ret == 0) {
  17702. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  17703. &outLen, endian);
  17704. }
  17705. /*test bad cases*/
  17706. if (ret == 0) {
  17707. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  17708. 0, endian);
  17709. if (ret == 0) {
  17710. ret = -1;
  17711. }
  17712. if (ret == BAD_FUNC_ARG) {
  17713. ret = 0;
  17714. }
  17715. }
  17716. if (ret == 0) {
  17717. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  17718. &outLen, endian);
  17719. if (ret == 0) {
  17720. ret = -1;
  17721. }
  17722. else if (ret == BAD_FUNC_ARG) {
  17723. ret = 0;
  17724. }
  17725. }
  17726. if (ret == 0) {
  17727. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  17728. &outLen, endian);
  17729. if (ret == 0) {
  17730. ret = -1;
  17731. }
  17732. else if (ret == BAD_FUNC_ARG) {
  17733. ret = 0;
  17734. }
  17735. }
  17736. if (ret == 0) {
  17737. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  17738. &outLen, endian);
  17739. if (ret == 0) {
  17740. ret = -1;
  17741. }
  17742. else if (ret == BAD_FUNC_ARG) {
  17743. ret = 0;
  17744. }
  17745. }
  17746. if (ret == 0) {
  17747. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  17748. NULL, endian);
  17749. if (ret == 0) {
  17750. ret = -1;
  17751. }
  17752. else if (ret == BAD_FUNC_ARG) {
  17753. ret = 0;
  17754. }
  17755. }
  17756. if (ret == 0) {
  17757. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  17758. *increasing to 0x8f checks for error being returned*/
  17759. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  17760. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  17761. &outLen, endian);
  17762. if (ret == 0) {
  17763. ret = -1;
  17764. }
  17765. else if (ret == ECC_BAD_ARG_E) {
  17766. ret = 0;
  17767. }
  17768. }
  17769. outLen = outLen - 2;
  17770. if (ret == 0) {
  17771. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  17772. &outLen, endian);
  17773. if (ret == 0) {
  17774. ret = -1;
  17775. }
  17776. else if (ret == BAD_FUNC_ARG) {
  17777. ret = 0;
  17778. }
  17779. }
  17780. printf(resultFmt, ret == 0 ? passed : failed);
  17781. wc_curve25519_free(&private_key);
  17782. wc_curve25519_free(&public_key);
  17783. wc_FreeRng(&rng);
  17784. #endif
  17785. return ret;
  17786. } /*END test_wc_curve25519_shared_secret_ex*/
  17787. /*
  17788. * Testing wc_curve25519_make_pub
  17789. */
  17790. static int test_wc_curve25519_make_pub(void)
  17791. {
  17792. int ret = 0;
  17793. #ifdef HAVE_CURVE25519
  17794. WC_RNG rng;
  17795. curve25519_key key;
  17796. byte out[CURVE25519_KEYSIZE];
  17797. printf(testingFmt, "wc_curve25519_make_pub()");
  17798. ret = wc_curve25519_init(&key);
  17799. if (ret == 0) {
  17800. ret = wc_InitRng(&rng);
  17801. if (ret == 0) {
  17802. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  17803. }
  17804. }
  17805. if (ret == 0) {
  17806. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  17807. }
  17808. /*test bad cases*/
  17809. if (ret == 0) {
  17810. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  17811. if (ret == ECC_BAD_ARG_E) {
  17812. ret = 0;
  17813. }
  17814. }
  17815. if (ret == 0) {
  17816. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  17817. if (ret == ECC_BAD_ARG_E) {
  17818. ret = 0;
  17819. }
  17820. }
  17821. if (ret == 0) {
  17822. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  17823. if (ret == ECC_BAD_ARG_E) {
  17824. ret = 0;
  17825. }
  17826. }
  17827. if (ret == 0) {
  17828. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  17829. if (ret == ECC_BAD_ARG_E) {
  17830. ret = 0;
  17831. }
  17832. }
  17833. if (ret == 0) {
  17834. /* verify clamping test */
  17835. key.k[0] |= ~248;
  17836. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  17837. if (ret == ECC_BAD_ARG_E) {
  17838. ret = 0;
  17839. }
  17840. key.k[0] &= 248;
  17841. }
  17842. /* repeat the expected-to-succeed test. */
  17843. if (ret == 0) {
  17844. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  17845. }
  17846. printf(resultFmt, ret == 0 ? passed : failed);
  17847. wc_curve25519_free(&key);
  17848. wc_FreeRng(&rng);
  17849. #endif
  17850. return ret;
  17851. } /*END test_wc_curve25519_make_pub */
  17852. /*
  17853. * Testing test_wc_curve25519_export_public_ex
  17854. */
  17855. static int test_wc_curve25519_export_public_ex (void)
  17856. {
  17857. int ret = 0;
  17858. #if defined(HAVE_CURVE25519)
  17859. WC_RNG rng;
  17860. curve25519_key key;
  17861. byte out[CURVE25519_KEYSIZE];
  17862. word32 outLen = sizeof(out);
  17863. int endian = EC25519_BIG_ENDIAN;
  17864. printf(testingFmt, "wc_curve25519_export_public_ex()");
  17865. ret = wc_curve25519_init(&key);
  17866. if (ret == 0) {
  17867. ret = wc_InitRng(&rng);
  17868. }
  17869. if (ret == 0) {
  17870. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  17871. if (ret == 0) {
  17872. ret = wc_curve25519_export_public(&key, out, &outLen);
  17873. }
  17874. if (ret == 0) {
  17875. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  17876. }
  17877. }
  17878. /*test bad cases*/
  17879. if (ret == 0) {
  17880. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  17881. if (ret == BAD_FUNC_ARG) {
  17882. ret = 0;
  17883. }
  17884. }
  17885. if (ret == 0) {
  17886. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  17887. if (ret == BAD_FUNC_ARG) {
  17888. ret = 0;
  17889. }
  17890. }
  17891. if (ret == 0) {
  17892. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  17893. if (ret == BAD_FUNC_ARG) {
  17894. ret = 0;
  17895. }
  17896. }
  17897. if (ret == 0) {
  17898. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  17899. if (ret == BAD_FUNC_ARG) {
  17900. ret = 0;
  17901. }
  17902. }
  17903. outLen = outLen - 2;
  17904. if (ret == 0) {
  17905. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  17906. if (ret == ECC_BAD_ARG_E) {
  17907. ret = 0;
  17908. }
  17909. }
  17910. printf(resultFmt, ret == 0 ? passed : failed);
  17911. wc_curve25519_free(&key);
  17912. wc_FreeRng(&rng);
  17913. #endif
  17914. return ret;
  17915. } /*END test_wc_curve25519_export_public_ex*/
  17916. /*
  17917. * Testing test_wc_curve25519_import_private_raw_ex
  17918. */
  17919. static int test_wc_curve25519_import_private_raw_ex (void)
  17920. {
  17921. int ret = 0;
  17922. #if defined(HAVE_CURVE25519)
  17923. WC_RNG rng;
  17924. curve25519_key key;
  17925. byte priv[CURVE25519_KEYSIZE];
  17926. byte pub[CURVE25519_KEYSIZE];
  17927. word32 privSz = sizeof(priv);
  17928. word32 pubSz = sizeof(pub);
  17929. int endian = EC25519_BIG_ENDIAN;
  17930. printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
  17931. ret = wc_curve25519_init(&key);
  17932. if (ret == 0) {
  17933. ret = wc_InitRng(&rng);
  17934. }
  17935. if (ret == 0) {
  17936. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  17937. if (ret == 0) {
  17938. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  17939. }
  17940. if (ret == 0) {
  17941. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  17942. }
  17943. if (ret == 0) {
  17944. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  17945. &key, endian);
  17946. }
  17947. }
  17948. /*test bad cases*/
  17949. if (ret == 0) {
  17950. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  17951. endian);
  17952. if (ret == BAD_FUNC_ARG) {
  17953. ret = 0;
  17954. }
  17955. }
  17956. if (ret == 0) {
  17957. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  17958. &key, endian);
  17959. if (ret == BAD_FUNC_ARG) {
  17960. ret = 0;
  17961. }
  17962. }
  17963. if (ret == 0) {
  17964. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  17965. &key, endian);
  17966. if (ret == BAD_FUNC_ARG) {
  17967. ret = 0;
  17968. }
  17969. }
  17970. if (ret == 0) {
  17971. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  17972. NULL, endian);
  17973. if (ret == BAD_FUNC_ARG) {
  17974. ret = 0;
  17975. }
  17976. }
  17977. if (ret == 0) {
  17978. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  17979. &key, endian);
  17980. if (ret == ECC_BAD_ARG_E) {
  17981. ret = 0;
  17982. }
  17983. }
  17984. if (ret == 0) {
  17985. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  17986. &key, endian);
  17987. if (ret == ECC_BAD_ARG_E) {
  17988. ret = 0;
  17989. }
  17990. }
  17991. if (ret == 0) {
  17992. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  17993. &key, EC25519_LITTLE_ENDIAN);
  17994. }
  17995. printf(resultFmt, ret == 0 ? passed : failed);
  17996. wc_curve25519_free(&key);
  17997. wc_FreeRng(&rng);
  17998. #endif
  17999. return ret;
  18000. } /*END test_wc_curve25519_import_private_raw_ex*/
  18001. /*
  18002. * Testing test_wc_curve25519_import_private
  18003. */
  18004. static int test_wc_curve25519_import_private (void)
  18005. {
  18006. int ret = 0;
  18007. #if defined(HAVE_CURVE25519)
  18008. curve25519_key key;
  18009. WC_RNG rng;
  18010. byte priv[CURVE25519_KEYSIZE];
  18011. word32 privSz = sizeof(priv);
  18012. printf(testingFmt, "wc_curve25519_import_private()");
  18013. ret = wc_curve25519_init(&key);
  18014. if (ret == 0) {
  18015. ret = wc_InitRng(&rng);
  18016. }
  18017. if (ret == 0) {
  18018. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  18019. if (ret == 0) {
  18020. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  18021. }
  18022. }
  18023. if (ret == 0) {
  18024. ret = wc_curve25519_import_private(priv, privSz, &key);
  18025. }
  18026. printf(resultFmt, ret == 0 ? passed : failed);
  18027. wc_curve25519_free(&key);
  18028. wc_FreeRng(&rng);
  18029. #endif
  18030. return ret;
  18031. } /*END test_wc_curve25519_import*/
  18032. /*
  18033. * Testing test_wc_curve25519_export_private_raw_ex
  18034. */
  18035. static int test_wc_curve25519_export_private_raw_ex (void)
  18036. {
  18037. int ret = 0;
  18038. #if defined(HAVE_CURVE25519)
  18039. curve25519_key key;
  18040. byte out[CURVE25519_KEYSIZE];
  18041. word32 outLen = sizeof(out);
  18042. int endian = EC25519_BIG_ENDIAN;
  18043. printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
  18044. ret = wc_curve25519_init(&key);
  18045. if (ret == 0) {
  18046. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  18047. }
  18048. /*test bad cases*/
  18049. if (ret == 0) {
  18050. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  18051. if (ret == BAD_FUNC_ARG) {
  18052. ret = 0;
  18053. }
  18054. }
  18055. if (ret == 0) {
  18056. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  18057. if (ret == BAD_FUNC_ARG) {
  18058. ret = 0;
  18059. }
  18060. }
  18061. if (ret == 0) {
  18062. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  18063. if (ret == BAD_FUNC_ARG) {
  18064. ret = 0;
  18065. }
  18066. }
  18067. if (ret == 0) {
  18068. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  18069. if (ret == BAD_FUNC_ARG) {
  18070. ret = 0;
  18071. }
  18072. }
  18073. if (ret == 0) {
  18074. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  18075. EC25519_LITTLE_ENDIAN);
  18076. }
  18077. outLen = outLen - 2;
  18078. if (ret == 0) {
  18079. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  18080. if (ret == ECC_BAD_ARG_E) {
  18081. ret = 0;
  18082. }
  18083. }
  18084. printf(resultFmt, ret == 0 ? passed : failed);
  18085. wc_curve25519_free(&key);
  18086. #endif
  18087. return ret;
  18088. }/*END test_wc_curve25519_export_private_raw_ex*/
  18089. /*
  18090. * Testing wc_ed448_make_key().
  18091. */
  18092. static int test_wc_ed448_make_key (void)
  18093. {
  18094. int ret = 0;
  18095. #if defined(HAVE_ED448)
  18096. ed448_key key;
  18097. WC_RNG rng;
  18098. ret = wc_InitRng(&rng);
  18099. if (ret == 0) {
  18100. ret = wc_ed448_init(&key);
  18101. }
  18102. printf(testingFmt, "wc_ed448_make_key()");
  18103. if (ret == 0) {
  18104. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  18105. }
  18106. /* Test bad args. */
  18107. if (ret == 0) {
  18108. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  18109. if (ret == BAD_FUNC_ARG) {
  18110. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  18111. }
  18112. if (ret == BAD_FUNC_ARG) {
  18113. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  18114. }
  18115. if (ret == BAD_FUNC_ARG) {
  18116. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  18117. }
  18118. if (ret == BAD_FUNC_ARG) {
  18119. ret = 0;
  18120. } else if (ret == 0) {
  18121. ret = SSL_FATAL_ERROR;
  18122. }
  18123. }
  18124. printf(resultFmt, ret == 0 ? passed : failed);
  18125. if (wc_FreeRng(&rng) && ret == 0) {
  18126. ret = SSL_FATAL_ERROR;
  18127. }
  18128. wc_ed448_free(&key);
  18129. #endif
  18130. return ret;
  18131. } /* END test_wc_ed448_make_key */
  18132. /*
  18133. * Testing wc_ed448_init()
  18134. */
  18135. static int test_wc_ed448_init (void)
  18136. {
  18137. int ret = 0;
  18138. #if defined(HAVE_ED448)
  18139. ed448_key key;
  18140. printf(testingFmt, "wc_ed448_init()");
  18141. ret = wc_ed448_init(&key);
  18142. /* Test bad args. */
  18143. if (ret == 0) {
  18144. ret = wc_ed448_init(NULL);
  18145. if (ret == BAD_FUNC_ARG) {
  18146. ret = 0;
  18147. } else if (ret == 0) {
  18148. ret = SSL_FATAL_ERROR;
  18149. }
  18150. }
  18151. printf(resultFmt, ret == 0 ? passed : failed);
  18152. wc_ed448_free(&key);
  18153. #endif
  18154. return ret;
  18155. } /* END test_wc_ed448_init */
  18156. /*
  18157. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  18158. */
  18159. static int test_wc_ed448_sign_msg (void)
  18160. {
  18161. int ret = 0;
  18162. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  18163. WC_RNG rng;
  18164. ed448_key key;
  18165. byte msg[] = "Everybody gets Friday off.\n";
  18166. byte sig[ED448_SIG_SIZE];
  18167. word32 msglen = sizeof(msg);
  18168. word32 siglen = sizeof(sig);
  18169. word32 badSigLen = sizeof(sig) - 1;
  18170. #ifdef HAVE_ED448_VERIFY
  18171. int verify_ok = 0; /*1 = Verify success.*/
  18172. #endif
  18173. /* Initialize stack variables. */
  18174. XMEMSET(sig, 0, siglen);
  18175. /* Initialize key. */
  18176. ret = wc_InitRng(&rng);
  18177. if (ret == 0) {
  18178. ret = wc_ed448_init(&key);
  18179. if (ret == 0) {
  18180. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  18181. }
  18182. }
  18183. printf(testingFmt, "wc_ed448_sign_msg()");
  18184. if (ret == 0) {
  18185. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  18186. }
  18187. /* Test bad args. */
  18188. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  18189. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  18190. if (ret == BAD_FUNC_ARG) {
  18191. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  18192. }
  18193. if (ret == BAD_FUNC_ARG) {
  18194. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  18195. }
  18196. if (ret == BAD_FUNC_ARG) {
  18197. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  18198. }
  18199. if (ret == BAD_FUNC_ARG) {
  18200. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  18201. NULL, 0);
  18202. }
  18203. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  18204. badSigLen -= 1;
  18205. ret = 0;
  18206. } else if (ret == 0) {
  18207. ret = SSL_FATAL_ERROR;
  18208. }
  18209. } /* END sign */
  18210. printf(resultFmt, ret == 0 ? passed : failed);
  18211. #ifdef HAVE_ED448_VERIFY
  18212. printf(testingFmt, "wc_ed448_verify_msg()");
  18213. if (ret == 0) {
  18214. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  18215. &key, NULL, 0);
  18216. if (ret == 0 && verify_ok == 1) {
  18217. ret = 0;
  18218. } else if (ret == 0) {
  18219. ret = SSL_FATAL_ERROR;
  18220. }
  18221. /* Test bad args. */
  18222. if (ret == 0) {
  18223. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  18224. msglen, &verify_ok, &key,
  18225. NULL, 0),
  18226. BAD_FUNC_ARG);
  18227. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  18228. msglen, &verify_ok, &key,
  18229. NULL, 0),
  18230. BAD_FUNC_ARG);
  18231. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  18232. &key, NULL, 0);
  18233. if (ret == BAD_FUNC_ARG) {
  18234. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  18235. &verify_ok, &key, NULL, 0);
  18236. }
  18237. if (ret == BAD_FUNC_ARG) {
  18238. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  18239. NULL, &key, NULL, 0);
  18240. }
  18241. if (ret == BAD_FUNC_ARG) {
  18242. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  18243. &verify_ok, NULL, NULL, 0);
  18244. }
  18245. if (ret == BAD_FUNC_ARG) {
  18246. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  18247. &verify_ok, &key, NULL, 0);
  18248. }
  18249. if (ret == BAD_FUNC_ARG) {
  18250. ret = 0;
  18251. } else if (ret == 0) {
  18252. ret = SSL_FATAL_ERROR;
  18253. }
  18254. }
  18255. } /* END verify. */
  18256. printf(resultFmt, ret == 0 ? passed : failed);
  18257. #endif /* Verify. */
  18258. if (wc_FreeRng(&rng) && ret == 0) {
  18259. ret = SSL_FATAL_ERROR;
  18260. }
  18261. wc_ed448_free(&key);
  18262. #endif
  18263. return ret;
  18264. } /* END test_wc_ed448_sign_msg */
  18265. /*
  18266. * Testing wc_ed448_import_public()
  18267. */
  18268. static int test_wc_ed448_import_public (void)
  18269. {
  18270. int ret = 0;
  18271. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  18272. WC_RNG rng;
  18273. ed448_key pubKey;
  18274. const byte in[] =
  18275. "Ed448PublicKeyUnitTest.................................\n";
  18276. word32 inlen = sizeof(in);
  18277. ret = wc_InitRng(&rng);
  18278. if (ret == 0) {
  18279. ret = wc_ed448_init(&pubKey);
  18280. if (ret == 0) {
  18281. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  18282. }
  18283. }
  18284. printf(testingFmt, "wc_ed448_import_public()");
  18285. if (ret == 0) {
  18286. ret = wc_ed448_import_public(in, inlen, &pubKey);
  18287. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  18288. ret = 0;
  18289. } else {
  18290. ret = SSL_FATAL_ERROR;
  18291. }
  18292. /* Test bad args. */
  18293. if (ret == 0) {
  18294. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  18295. if (ret == BAD_FUNC_ARG) {
  18296. ret = wc_ed448_import_public(in, inlen, NULL);
  18297. }
  18298. if (ret == BAD_FUNC_ARG) {
  18299. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  18300. }
  18301. if (ret == BAD_FUNC_ARG) {
  18302. ret = 0;
  18303. } else if (ret == 0) {
  18304. ret = SSL_FATAL_ERROR;
  18305. }
  18306. }
  18307. }
  18308. printf(resultFmt, ret == 0 ? passed : failed);
  18309. if (wc_FreeRng(&rng) && ret == 0) {
  18310. ret = SSL_FATAL_ERROR;
  18311. }
  18312. wc_ed448_free(&pubKey);
  18313. #endif
  18314. return ret;
  18315. } /* END wc_ed448_import_public */
  18316. /*
  18317. * Testing wc_ed448_import_private_key()
  18318. */
  18319. static int test_wc_ed448_import_private_key (void)
  18320. {
  18321. int ret = 0;
  18322. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  18323. WC_RNG rng;
  18324. ed448_key key;
  18325. const byte privKey[] =
  18326. "Ed448PrivateKeyUnitTest................................\n";
  18327. const byte pubKey[] =
  18328. "Ed448PublicKeyUnitTest.................................\n";
  18329. word32 privKeySz = sizeof(privKey);
  18330. word32 pubKeySz = sizeof(pubKey);
  18331. #ifdef HAVE_ED448_KEY_EXPORT
  18332. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  18333. word32 bothKeysSz = sizeof(bothKeys);
  18334. #endif
  18335. ret = wc_InitRng(&rng);
  18336. if (ret != 0) {
  18337. return ret;
  18338. }
  18339. ret = wc_ed448_init(&key);
  18340. if (ret != 0) {
  18341. wc_FreeRng(&rng);
  18342. return ret;
  18343. }
  18344. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  18345. printf(testingFmt, "wc_ed448_import_private_key()");
  18346. if (ret == 0) {
  18347. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey, pubKeySz,
  18348. &key);
  18349. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  18350. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18351. ret = SSL_FATAL_ERROR;
  18352. }
  18353. }
  18354. #ifdef HAVE_ED448_KEY_EXPORT
  18355. if (ret == 0)
  18356. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  18357. if (ret == 0) {
  18358. ret = wc_ed448_import_private_key(bothKeys, bothKeysSz, NULL, 0, &key);
  18359. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  18360. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18361. ret = SSL_FATAL_ERROR;
  18362. }
  18363. }
  18364. #endif
  18365. /* Test bad args. */
  18366. if (ret == 0) {
  18367. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  18368. &key);
  18369. if (ret == BAD_FUNC_ARG) {
  18370. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  18371. pubKeySz, &key);
  18372. }
  18373. if (ret == BAD_FUNC_ARG) {
  18374. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  18375. pubKeySz, NULL);
  18376. }
  18377. if (ret == BAD_FUNC_ARG) {
  18378. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  18379. pubKeySz, &key);
  18380. }
  18381. if (ret == BAD_FUNC_ARG) {
  18382. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  18383. pubKeySz - 1, &key);
  18384. }
  18385. if (ret == BAD_FUNC_ARG) {
  18386. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  18387. 0, &key);
  18388. }
  18389. if (ret == BAD_FUNC_ARG) {
  18390. ret = 0;
  18391. } else if (ret == 0) {
  18392. ret = SSL_FATAL_ERROR;
  18393. }
  18394. }
  18395. printf(resultFmt, ret == 0 ? passed : failed);
  18396. if (wc_FreeRng(&rng) && ret == 0) {
  18397. ret = SSL_FATAL_ERROR;
  18398. }
  18399. wc_ed448_free(&key);
  18400. #endif
  18401. return ret;
  18402. } /* END test_wc_ed448_import_private_key */
  18403. /*
  18404. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  18405. */
  18406. static int test_wc_ed448_export (void)
  18407. {
  18408. int ret = 0;
  18409. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  18410. WC_RNG rng;
  18411. ed448_key key;
  18412. byte priv[ED448_PRV_KEY_SIZE];
  18413. byte pub[ED448_PUB_KEY_SIZE];
  18414. word32 privSz = sizeof(priv);
  18415. word32 pubSz = sizeof(pub);
  18416. ret = wc_InitRng(&rng);
  18417. if (ret != 0) {
  18418. return ret;
  18419. }
  18420. ret = wc_ed448_init(&key);
  18421. if (ret != 0) {
  18422. wc_FreeRng(&rng);
  18423. return ret;
  18424. }
  18425. if (ret == 0) {
  18426. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  18427. }
  18428. printf(testingFmt, "wc_ed448_export_public()");
  18429. if (ret == 0) {
  18430. ret = wc_ed448_export_public(&key, pub, &pubSz);
  18431. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  18432. XMEMCMP(key.p, pub, pubSz) != 0)) {
  18433. ret = SSL_FATAL_ERROR;
  18434. }
  18435. if (ret == 0) {
  18436. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  18437. if (ret == BAD_FUNC_ARG) {
  18438. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  18439. }
  18440. if (ret == BAD_FUNC_ARG) {
  18441. ret = wc_ed448_export_public(&key, pub, NULL);
  18442. }
  18443. if (ret == BAD_FUNC_ARG) {
  18444. ret = 0;
  18445. } else if (ret == 0) {
  18446. ret = SSL_FATAL_ERROR;
  18447. }
  18448. }
  18449. }
  18450. printf(resultFmt, ret == 0 ? passed : failed);
  18451. printf(testingFmt, "wc_ed448_export_private_only()");
  18452. if (ret == 0) {
  18453. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  18454. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  18455. XMEMCMP(key.k, priv, privSz) != 0)) {
  18456. ret = SSL_FATAL_ERROR;
  18457. }
  18458. if (ret == 0) {
  18459. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  18460. if (ret == BAD_FUNC_ARG) {
  18461. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  18462. }
  18463. if (ret == BAD_FUNC_ARG) {
  18464. ret = wc_ed448_export_private_only(&key, priv, NULL);
  18465. }
  18466. if (ret == BAD_FUNC_ARG) {
  18467. ret = 0;
  18468. } else if (ret == 0) {
  18469. ret = SSL_FATAL_ERROR;
  18470. }
  18471. }
  18472. }
  18473. printf(resultFmt, ret == 0 ? passed : failed);
  18474. if (wc_FreeRng(&rng) && ret == 0) {
  18475. ret = SSL_FATAL_ERROR;
  18476. }
  18477. wc_ed448_free(&key);
  18478. #endif
  18479. return ret;
  18480. } /* END test_wc_ed448_export */
  18481. /*
  18482. * Testing wc_ed448_size()
  18483. */
  18484. static int test_wc_ed448_size (void)
  18485. {
  18486. int ret = 0;
  18487. #if defined(HAVE_ED448)
  18488. WC_RNG rng;
  18489. ed448_key key;
  18490. ret = wc_InitRng(&rng);
  18491. if (ret != 0) {
  18492. return ret;
  18493. }
  18494. ret = wc_ed448_init(&key);
  18495. if (ret != 0) {
  18496. wc_FreeRng(&rng);
  18497. return ret;
  18498. }
  18499. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  18500. if (ret != 0) {
  18501. wc_FreeRng(&rng);
  18502. wc_ed448_free(&key);
  18503. return ret;
  18504. }
  18505. printf(testingFmt, "wc_ed448_size()");
  18506. ret = wc_ed448_size(&key);
  18507. /* Test bad args. */
  18508. if (ret == ED448_KEY_SIZE) {
  18509. ret = wc_ed448_size(NULL);
  18510. if (ret == BAD_FUNC_ARG) {
  18511. ret = 0;
  18512. }
  18513. }
  18514. printf(resultFmt, ret == 0 ? passed : failed);
  18515. if (ret == 0) {
  18516. printf(testingFmt, "wc_ed448_sig_size()");
  18517. ret = wc_ed448_sig_size(&key);
  18518. if (ret == ED448_SIG_SIZE) {
  18519. ret = 0;
  18520. }
  18521. /* Test bad args. */
  18522. if (ret == 0) {
  18523. ret = wc_ed448_sig_size(NULL);
  18524. if (ret == BAD_FUNC_ARG) {
  18525. ret = 0;
  18526. }
  18527. }
  18528. printf(resultFmt, ret == 0 ? passed : failed);
  18529. } /* END wc_ed448_sig_size() */
  18530. if (ret == 0) {
  18531. printf(testingFmt, "wc_ed448_pub_size");
  18532. ret = wc_ed448_pub_size(&key);
  18533. if (ret == ED448_PUB_KEY_SIZE) {
  18534. ret = 0;
  18535. }
  18536. if (ret == 0) {
  18537. ret = wc_ed448_pub_size(NULL);
  18538. if (ret == BAD_FUNC_ARG) {
  18539. ret = 0;
  18540. }
  18541. }
  18542. printf(resultFmt, ret == 0 ? passed : failed);
  18543. } /* END wc_ed448_pub_size */
  18544. if (ret == 0) {
  18545. printf(testingFmt, "wc_ed448_priv_size");
  18546. ret = wc_ed448_priv_size(&key);
  18547. if (ret == ED448_PRV_KEY_SIZE) {
  18548. ret = 0;
  18549. }
  18550. if (ret == 0) {
  18551. ret = wc_ed448_priv_size(NULL);
  18552. if (ret == BAD_FUNC_ARG) {
  18553. ret = 0;
  18554. }
  18555. }
  18556. printf(resultFmt, ret == 0 ? passed : failed);
  18557. } /* END wc_ed448_pub_size */
  18558. if (wc_FreeRng(&rng) && ret == 0) {
  18559. ret = SSL_FATAL_ERROR;
  18560. }
  18561. wc_ed448_free(&key);
  18562. #endif
  18563. return ret;
  18564. } /* END test_wc_ed448_size */
  18565. /*
  18566. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  18567. */
  18568. static int test_wc_ed448_exportKey (void)
  18569. {
  18570. int ret = 0;
  18571. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  18572. WC_RNG rng;
  18573. ed448_key key;
  18574. byte priv[ED448_PRV_KEY_SIZE];
  18575. byte pub[ED448_PUB_KEY_SIZE];
  18576. byte privOnly[ED448_PRV_KEY_SIZE];
  18577. word32 privSz = sizeof(priv);
  18578. word32 pubSz = sizeof(pub);
  18579. word32 privOnlySz = sizeof(privOnly);
  18580. ret = wc_InitRng(&rng);
  18581. if (ret != 0) {
  18582. return ret;
  18583. }
  18584. ret = wc_ed448_init(&key);
  18585. if (ret != 0) {
  18586. wc_FreeRng(&rng);
  18587. return ret;
  18588. }
  18589. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  18590. if (ret != 0) {
  18591. wc_FreeRng(&rng);
  18592. wc_ed448_free(&key);
  18593. return ret;
  18594. }
  18595. printf(testingFmt, "wc_ed448_export_private()");
  18596. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  18597. if (ret == 0) {
  18598. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  18599. if (ret == BAD_FUNC_ARG) {
  18600. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  18601. }
  18602. if (ret == BAD_FUNC_ARG) {
  18603. ret = wc_ed448_export_private(&key, privOnly, NULL);
  18604. }
  18605. if (ret == BAD_FUNC_ARG) {
  18606. ret = 0;
  18607. } else if (ret == 0) {
  18608. ret = SSL_FATAL_ERROR;
  18609. }
  18610. }
  18611. printf(resultFmt, ret == 0 ? passed : failed);
  18612. if (ret == 0) {
  18613. printf(testingFmt, "wc_ed448_export_key()");
  18614. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  18615. if (ret == 0) {
  18616. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  18617. if (ret == BAD_FUNC_ARG) {
  18618. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  18619. }
  18620. if (ret == BAD_FUNC_ARG) {
  18621. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  18622. }
  18623. if (ret == BAD_FUNC_ARG) {
  18624. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  18625. }
  18626. if (ret == BAD_FUNC_ARG) {
  18627. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  18628. }
  18629. if (ret == BAD_FUNC_ARG) {
  18630. ret = 0;
  18631. } else if (ret == 0) {
  18632. ret = SSL_FATAL_ERROR;
  18633. }
  18634. }
  18635. printf(resultFmt, ret == 0 ? passed : failed);
  18636. } /* END wc_ed448_export_key() */
  18637. /* Cross check output. */
  18638. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  18639. ret = SSL_FATAL_ERROR;
  18640. }
  18641. if (wc_FreeRng(&rng) && ret == 0) {
  18642. ret = SSL_FATAL_ERROR;
  18643. }
  18644. wc_ed448_free(&key);
  18645. #endif
  18646. return ret;
  18647. } /* END test_wc_ed448_exportKey */
  18648. /*
  18649. * Testing wc_Ed448PublicKeyToDer
  18650. */
  18651. static int test_wc_Ed448PublicKeyToDer (void)
  18652. {
  18653. int ret = 0;
  18654. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  18655. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  18656. int tmp;
  18657. ed448_key key;
  18658. byte derBuf[1024];
  18659. printf(testingFmt, "wc_Ed448PublicKeyToDer()");
  18660. /* Test bad args */
  18661. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  18662. if (tmp != BAD_FUNC_ARG) {
  18663. ret = SSL_FATAL_ERROR;
  18664. }
  18665. if (ret == 0) {
  18666. wc_ed448_init(&key);
  18667. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  18668. if (tmp != BUFFER_E) {
  18669. ret = SSL_FATAL_ERROR;
  18670. }
  18671. wc_ed448_free(&key);
  18672. }
  18673. /* Test good args */
  18674. if (ret == 0) {
  18675. WC_RNG rng;
  18676. ret = wc_InitRng(&rng);
  18677. if (ret != 0) {
  18678. return ret;
  18679. }
  18680. ret = wc_ed448_init(&key);
  18681. if (ret != 0) {
  18682. wc_FreeRng(&rng);
  18683. return ret;
  18684. }
  18685. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  18686. if (ret != 0) {
  18687. wc_FreeRng(&rng);
  18688. wc_ed448_free(&key);
  18689. return ret;
  18690. }
  18691. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  18692. if (tmp <= 0) {
  18693. ret = SSL_FATAL_ERROR;
  18694. }
  18695. wc_FreeRng(&rng);
  18696. wc_ed448_free(&key);
  18697. }
  18698. printf(resultFmt, ret == 0 ? passed : failed);
  18699. #endif
  18700. return ret;
  18701. } /* END testing wc_Ed448PublicKeyToDer */
  18702. /*
  18703. * Testing wc_curve448_init and wc_curve448_free.
  18704. */
  18705. static int test_wc_curve448_init (void)
  18706. {
  18707. int ret = 0;
  18708. #if defined(HAVE_CURVE448)
  18709. curve448_key key;
  18710. printf(testingFmt, "wc_curve448_init()");
  18711. ret = wc_curve448_init(&key);
  18712. /* Test bad args for wc_curve448_init */
  18713. if (ret == 0) {
  18714. ret = wc_curve448_init(NULL);
  18715. if (ret == BAD_FUNC_ARG) {
  18716. ret = 0;
  18717. } else if (ret == 0) {
  18718. ret = SSL_FATAL_ERROR;
  18719. }
  18720. }
  18721. printf(resultFmt, ret == 0 ? passed : failed);
  18722. /* Test good args for wc_curve_448_free */
  18723. wc_curve448_free(&key);
  18724. wc_curve448_free(NULL);
  18725. #endif
  18726. return ret;
  18727. } /* END test_wc_curve448_init and wc_curve_448_free*/
  18728. /*
  18729. * Testing wc_curve448_make_key
  18730. */
  18731. static int test_wc_curve448_make_key (void)
  18732. {
  18733. int ret = 0;
  18734. #if defined(HAVE_CURVE448)
  18735. WC_RNG rng;
  18736. curve448_key key;
  18737. int keysize;
  18738. printf(testingFmt, "wc_curve448_make_key()");
  18739. ret = wc_curve448_init(&key);
  18740. if (ret == 0) {
  18741. ret = wc_InitRng(&rng);
  18742. }
  18743. if (ret == 0) {
  18744. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  18745. if (ret == 0) {
  18746. keysize = wc_curve448_size(&key);
  18747. if (keysize != CURVE448_KEY_SIZE) {
  18748. ret = SSL_FATAL_ERROR;
  18749. }
  18750. }
  18751. if (ret == 0) {
  18752. ret = wc_curve448_make_key(&rng, keysize, &key);
  18753. }
  18754. }
  18755. /*test bad cases*/
  18756. if (ret == 0) {
  18757. ret = wc_curve448_make_key(NULL, 0, NULL);
  18758. if (ret == BAD_FUNC_ARG) {
  18759. ret = 0;
  18760. }
  18761. }
  18762. if (ret == 0) {
  18763. ret = wc_curve448_make_key(&rng, keysize, NULL);
  18764. if (ret == BAD_FUNC_ARG) {
  18765. ret = 0;
  18766. }
  18767. }
  18768. if (ret == 0) {
  18769. ret = wc_curve448_make_key(NULL, keysize, &key);
  18770. if (ret == BAD_FUNC_ARG) {
  18771. ret = 0;
  18772. }
  18773. }
  18774. if (ret == 0) {
  18775. ret = wc_curve448_make_key(&rng, 0, &key);
  18776. if (ret == ECC_BAD_ARG_E) {
  18777. ret = 0;
  18778. }
  18779. }
  18780. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  18781. ret = WOLFSSL_FATAL_ERROR;
  18782. }
  18783. printf(resultFmt, ret == 0 ? passed : failed);
  18784. wc_curve448_free(&key);
  18785. #endif
  18786. return ret;
  18787. } /*END test_wc_curve448_make_key*/
  18788. /*
  18789. * Testing test_wc_curve448_shared_secret_ex
  18790. */
  18791. static int test_wc_curve448_shared_secret_ex (void)
  18792. {
  18793. int ret = 0;
  18794. #if defined(HAVE_CURVE448)
  18795. WC_RNG rng;
  18796. curve448_key private_key, public_key;
  18797. byte out[CURVE448_KEY_SIZE];
  18798. word32 outLen = sizeof(out);
  18799. int endian = EC448_BIG_ENDIAN;
  18800. printf(testingFmt, "wc_curve448_shared_secret_ex()");
  18801. ret = wc_curve448_init(&private_key);
  18802. if (ret == 0) {
  18803. ret = wc_InitRng(&rng);
  18804. if (ret == 0) {
  18805. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  18806. }
  18807. }
  18808. if (ret == 0) {
  18809. ret = wc_curve448_init(&public_key);
  18810. }
  18811. if (ret == 0) {
  18812. if (ret == 0) {
  18813. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  18814. }
  18815. }
  18816. if (ret == 0) {
  18817. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  18818. &outLen, endian);
  18819. }
  18820. /*test bad cases*/
  18821. if (ret == 0) {
  18822. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL,
  18823. 0, endian);
  18824. if (ret == BAD_FUNC_ARG) {
  18825. ret = 0;
  18826. }
  18827. }
  18828. if (ret == 0) {
  18829. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  18830. &outLen, endian);
  18831. if (ret == BAD_FUNC_ARG) {
  18832. ret = 0;
  18833. }
  18834. }
  18835. if (ret == 0) {
  18836. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  18837. &outLen, endian);
  18838. if (ret == BAD_FUNC_ARG) {
  18839. ret = 0;
  18840. }
  18841. }
  18842. if (ret == 0) {
  18843. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  18844. &outLen, endian);
  18845. if (ret == BAD_FUNC_ARG) {
  18846. ret = 0;
  18847. }
  18848. }
  18849. if (ret == 0) {
  18850. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  18851. NULL, endian);
  18852. if (ret == BAD_FUNC_ARG) {
  18853. ret = 0;
  18854. }
  18855. }
  18856. outLen = outLen - 2;
  18857. if (ret == 0) {
  18858. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  18859. &outLen, endian);
  18860. if (ret == BAD_FUNC_ARG) {
  18861. ret = 0;
  18862. }
  18863. }
  18864. printf(resultFmt, ret == 0 ? passed : failed);
  18865. wc_curve448_free(&private_key);
  18866. wc_curve448_free(&public_key);
  18867. wc_FreeRng(&rng);
  18868. #endif
  18869. return ret;
  18870. } /*END test_wc_curve448_shared_secret_ex*/
  18871. /*
  18872. * Testing test_wc_curve448_export_public_ex
  18873. */
  18874. static int test_wc_curve448_export_public_ex (void)
  18875. {
  18876. int ret = 0;
  18877. #if defined(HAVE_CURVE448)
  18878. WC_RNG rng;
  18879. curve448_key key;
  18880. byte out[CURVE448_KEY_SIZE];
  18881. word32 outLen = sizeof(out);
  18882. int endian = EC448_BIG_ENDIAN;
  18883. printf(testingFmt, "wc_curve448_export_public_ex()");
  18884. ret = wc_curve448_init(&key);
  18885. if (ret == 0) {
  18886. ret = wc_InitRng(&rng);
  18887. }
  18888. if (ret == 0) {
  18889. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  18890. if (ret == 0){
  18891. ret = wc_curve448_export_public(&key, out, &outLen);
  18892. }
  18893. if (ret == 0) {
  18894. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  18895. }
  18896. }
  18897. /*test bad cases*/
  18898. if (ret == 0) {
  18899. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  18900. if (ret == BAD_FUNC_ARG) {
  18901. ret = 0;
  18902. }
  18903. }
  18904. if (ret == 0) {
  18905. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  18906. if (ret == BAD_FUNC_ARG) {
  18907. ret = 0;
  18908. }
  18909. }
  18910. if (ret == 0) {
  18911. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  18912. if (ret == BAD_FUNC_ARG) {
  18913. ret = 0;
  18914. }
  18915. }
  18916. if (ret == 0) {
  18917. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  18918. if (ret == BAD_FUNC_ARG) {
  18919. ret = 0;
  18920. }
  18921. }
  18922. outLen = outLen - 2;
  18923. if (ret == 0) {
  18924. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  18925. if (ret == ECC_BAD_ARG_E) {
  18926. ret = 0;
  18927. }
  18928. }
  18929. printf(resultFmt, ret == 0 ? passed : failed);
  18930. wc_curve448_free(&key);
  18931. wc_FreeRng(&rng);
  18932. #endif
  18933. return ret;
  18934. } /*END test_wc_curve448_export_public_ex*/
  18935. /*
  18936. * Testing test_wc_curve448_export_private_raw_ex
  18937. */
  18938. static int test_wc_curve448_export_private_raw_ex (void)
  18939. {
  18940. int ret = 0;
  18941. #if defined(HAVE_CURVE448)
  18942. curve448_key key;
  18943. byte out[CURVE448_KEY_SIZE];
  18944. word32 outLen = sizeof(out);
  18945. int endian = EC448_BIG_ENDIAN;
  18946. printf(testingFmt, "wc_curve448_export_private_raw_ex()");
  18947. ret = wc_curve448_init(&key);
  18948. if (ret == 0) {
  18949. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  18950. }
  18951. /*test bad cases*/
  18952. if (ret == 0) {
  18953. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  18954. if (ret == BAD_FUNC_ARG) {
  18955. ret = 0;
  18956. }
  18957. }
  18958. if (ret == 0) {
  18959. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  18960. if (ret == BAD_FUNC_ARG) {
  18961. ret = 0;
  18962. }
  18963. }
  18964. if (ret == 0) {
  18965. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  18966. if (ret == BAD_FUNC_ARG) {
  18967. ret = 0;
  18968. }
  18969. }
  18970. if (ret == 0) {
  18971. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  18972. if (ret == BAD_FUNC_ARG) {
  18973. ret = 0;
  18974. }
  18975. }
  18976. if (ret == 0) {
  18977. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  18978. EC448_LITTLE_ENDIAN);
  18979. }
  18980. outLen = outLen - 2;
  18981. if (ret == 0) {
  18982. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  18983. if (ret == ECC_BAD_ARG_E) {
  18984. ret = 0;
  18985. }
  18986. }
  18987. printf(resultFmt, ret == 0 ? passed : failed);
  18988. wc_curve448_free(&key);
  18989. #endif
  18990. return ret;
  18991. }/*END test_wc_curve448_export_private_raw_ex*/
  18992. /*
  18993. * Testing test_wc_curve448_import_private_raw_ex
  18994. */
  18995. static int test_wc_curve448_import_private_raw_ex (void)
  18996. {
  18997. int ret = 0;
  18998. #if defined(HAVE_CURVE448)
  18999. WC_RNG rng;
  19000. curve448_key key;
  19001. byte priv[CURVE448_KEY_SIZE];
  19002. byte pub[CURVE448_KEY_SIZE];
  19003. word32 privSz = sizeof(priv);
  19004. word32 pubSz = sizeof(pub);
  19005. int endian = EC448_BIG_ENDIAN;
  19006. printf(testingFmt, "wc_curve448_import_private_raw_ex()");
  19007. ret = wc_curve448_init(&key);
  19008. if (ret == 0) {
  19009. ret = wc_InitRng(&rng);
  19010. }
  19011. if (ret == 0) {
  19012. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  19013. if (ret == 0){
  19014. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  19015. }
  19016. if (ret == 0){
  19017. ret = wc_curve448_export_public(&key, pub, &pubSz);
  19018. }
  19019. if (ret == 0) {
  19020. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  19021. &key, endian);
  19022. }
  19023. }
  19024. /*test bad cases*/
  19025. if (ret == 0) {
  19026. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  19027. if (ret == BAD_FUNC_ARG) {
  19028. ret = 0;
  19029. }
  19030. }
  19031. if (ret == 0) {
  19032. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  19033. &key, endian);
  19034. if (ret == BAD_FUNC_ARG) {
  19035. ret = 0;
  19036. }
  19037. }
  19038. if (ret == 0) {
  19039. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  19040. &key, endian);
  19041. if (ret == BAD_FUNC_ARG) {
  19042. ret = 0;
  19043. }
  19044. }
  19045. if (ret == 0) {
  19046. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  19047. NULL, endian);
  19048. if (ret == BAD_FUNC_ARG) {
  19049. ret = 0;
  19050. }
  19051. }
  19052. if (ret == 0) {
  19053. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  19054. &key, endian);
  19055. if (ret == ECC_BAD_ARG_E) {
  19056. ret = 0;
  19057. }
  19058. }
  19059. if (ret == 0) {
  19060. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  19061. &key, endian);
  19062. if (ret == ECC_BAD_ARG_E) {
  19063. ret = 0;
  19064. }
  19065. }
  19066. if (ret == 0) {
  19067. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  19068. &key, EC448_LITTLE_ENDIAN);
  19069. }
  19070. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  19071. ret = WOLFSSL_FATAL_ERROR;
  19072. }
  19073. printf(resultFmt, ret == 0 ? passed : failed);
  19074. wc_curve448_free(&key);
  19075. #endif
  19076. return ret;
  19077. } /*END test_wc_curve448_import_private_raw_ex*/
  19078. /*
  19079. * Testing test_curve448_export_key_raw
  19080. */
  19081. static int test_wc_curve448_export_key_raw (void)
  19082. {
  19083. int ret = 0;
  19084. #if defined(HAVE_CURVE448)
  19085. WC_RNG rng;
  19086. curve448_key key;
  19087. byte priv[CURVE448_KEY_SIZE];
  19088. byte pub[CURVE448_KEY_SIZE];
  19089. word32 privSz = sizeof(priv);
  19090. word32 pubSz = sizeof(pub);
  19091. printf(testingFmt, "wc_curve448_export_key_raw()");
  19092. ret = wc_curve448_init(&key);
  19093. if (ret == 0) {
  19094. ret = wc_InitRng(&rng);
  19095. }
  19096. if (ret == 0) {
  19097. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  19098. if (ret == 0) {
  19099. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  19100. }
  19101. if (ret == 0) {
  19102. ret = wc_curve448_export_public(&key, pub, &pubSz);
  19103. }
  19104. if (ret == 0) {
  19105. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  19106. }
  19107. }
  19108. printf(resultFmt, ret == 0 ? passed : failed);
  19109. wc_curve448_free(&key);
  19110. wc_FreeRng(&rng);
  19111. #endif
  19112. return ret;
  19113. }/*END test_wc_curve448_import_private_raw_ex*/
  19114. /*
  19115. * Testing test_wc_curve448_import_private
  19116. */
  19117. static int test_wc_curve448_import_private (void)
  19118. {
  19119. int ret = 0;
  19120. #if defined(HAVE_CURVE448)
  19121. curve448_key key;
  19122. WC_RNG rng;
  19123. byte priv[CURVE448_KEY_SIZE];
  19124. word32 privSz = sizeof(priv);
  19125. printf(testingFmt, "wc_curve448_import_private()");
  19126. ret = wc_curve448_init(&key);
  19127. if (ret == 0) {
  19128. ret = wc_InitRng(&rng);
  19129. }
  19130. if (ret == 0) {
  19131. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  19132. if (ret == 0) {
  19133. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  19134. }
  19135. }
  19136. if (ret == 0) {
  19137. ret = wc_curve448_import_private(priv, privSz, &key);
  19138. }
  19139. printf(resultFmt, ret == 0 ? passed : failed);
  19140. wc_curve448_free(&key);
  19141. wc_FreeRng(&rng);
  19142. #endif
  19143. return ret;
  19144. } /*END test_wc_curve448_import*/
  19145. /*
  19146. * Testing test_wc_curve448_size.
  19147. */
  19148. static int test_wc_curve448_size (void)
  19149. {
  19150. int ret = 0;
  19151. #if defined(HAVE_CURVE448)
  19152. curve448_key key;
  19153. printf(testingFmt, "wc_curve448_size()");
  19154. ret = wc_curve448_init(&key);
  19155. /* Test good args for wc_curve448_size */
  19156. if (ret == 0) {
  19157. ret = wc_curve448_size(&key);
  19158. }
  19159. /* Test bad args for wc_curve448_size */
  19160. if (ret != 0) {
  19161. ret = wc_curve448_size(NULL);
  19162. }
  19163. printf(resultFmt, ret == 0 ? passed : failed);
  19164. wc_curve448_free(&key);
  19165. #endif
  19166. return ret;
  19167. } /* END test_wc_curve448_size*/
  19168. /*
  19169. * Testing wc_ecc_make_key.
  19170. */
  19171. static int test_wc_ecc_make_key (void)
  19172. {
  19173. int ret = 0;
  19174. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  19175. WC_RNG rng;
  19176. ecc_key key;
  19177. printf(testingFmt, "wc_ecc_make_key()");
  19178. ret = wc_InitRng(&rng);
  19179. if (ret != 0)
  19180. return ret;
  19181. ret = wc_ecc_init(&key);
  19182. if (ret == 0) {
  19183. ret = wc_ecc_make_key(&rng, KEY14, &key);
  19184. /* Pass in bad args. */
  19185. if (ret == 0) {
  19186. ret = wc_ecc_make_key(NULL, KEY14, &key);
  19187. if (ret == BAD_FUNC_ARG) {
  19188. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  19189. }
  19190. if (ret == BAD_FUNC_ARG) {
  19191. ret = 0;
  19192. } else if (ret == 0) {
  19193. ret = WOLFSSL_FATAL_ERROR;
  19194. }
  19195. }
  19196. wc_ecc_free(&key);
  19197. }
  19198. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  19199. ret = WOLFSSL_FATAL_ERROR;
  19200. }
  19201. #ifdef FP_ECC
  19202. wc_ecc_fp_free();
  19203. #endif
  19204. printf(resultFmt, ret == 0 ? passed : failed);
  19205. #endif
  19206. return ret;
  19207. } /* END test_wc_ecc_make_key */
  19208. /*
  19209. * Testing wc_ecc_init()
  19210. */
  19211. static int test_wc_ecc_init (void)
  19212. {
  19213. int ret = 0;
  19214. #ifdef HAVE_ECC
  19215. ecc_key key;
  19216. printf(testingFmt, "wc_ecc_init()");
  19217. ret = wc_ecc_init(&key);
  19218. /* Pass in bad args. */
  19219. if (ret == 0) {
  19220. ret = wc_ecc_init(NULL);
  19221. if (ret == BAD_FUNC_ARG) {
  19222. ret = 0;
  19223. } else if (ret == 0) {
  19224. ret = WOLFSSL_FATAL_ERROR;
  19225. }
  19226. }
  19227. printf(resultFmt, ret == 0 ? passed : failed);
  19228. wc_ecc_free(&key);
  19229. #endif
  19230. return ret;
  19231. } /* END test_wc_ecc_init */
  19232. /*
  19233. * Testing wc_ecc_check_key()
  19234. */
  19235. static int test_wc_ecc_check_key (void)
  19236. {
  19237. int ret = 0;
  19238. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  19239. WC_RNG rng;
  19240. ecc_key key;
  19241. XMEMSET(&rng, 0, sizeof(rng));
  19242. XMEMSET(&key, 0, sizeof(key));
  19243. ret = wc_InitRng(&rng);
  19244. if (ret == 0) {
  19245. ret = wc_ecc_init(&key);
  19246. if (ret == 0) {
  19247. ret = wc_ecc_make_key(&rng, KEY14, &key);
  19248. }
  19249. }
  19250. printf(testingFmt, "wc_ecc_check_key()");
  19251. if (ret == 0) {
  19252. ret = wc_ecc_check_key(&key);
  19253. }
  19254. /* Pass in bad args. */
  19255. if (ret == 0) {
  19256. ret = wc_ecc_check_key(NULL);
  19257. if (ret == BAD_FUNC_ARG) {
  19258. ret = 0;
  19259. } else if (ret == 0) {
  19260. ret = WOLFSSL_FATAL_ERROR;
  19261. }
  19262. }
  19263. printf(resultFmt, ret == 0 ? passed : failed);
  19264. if (wc_FreeRng(&rng) && ret == 0) {
  19265. ret = WOLFSSL_FATAL_ERROR;
  19266. }
  19267. wc_ecc_free(&key);
  19268. #ifdef FP_ECC
  19269. wc_ecc_fp_free();
  19270. #endif
  19271. #endif
  19272. return ret;
  19273. } /* END test_wc_ecc_check_key */
  19274. /*
  19275. * Testing wc_ecc_get_generator()
  19276. */
  19277. static int test_wc_ecc_get_generator(void)
  19278. {
  19279. int ret = 0;
  19280. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  19281. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  19282. ecc_point* pt;
  19283. printf(testingFmt, "wc_ecc_new_point()");
  19284. pt = wc_ecc_new_point();
  19285. if (!pt) {
  19286. ret = WOLFSSL_FATAL_ERROR;
  19287. }
  19288. printf(testingFmt, "wc_ecc_get_generator()");
  19289. if (ret == 0) {
  19290. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  19291. }
  19292. /* Test bad args. */
  19293. if (ret == MP_OKAY) {
  19294. /* Returns Zero for bad arg. */
  19295. ret = wc_ecc_get_generator(pt, -1);
  19296. if (ret != MP_OKAY)
  19297. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  19298. if (ret != MP_OKAY)
  19299. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  19300. * increase this number */
  19301. if (ret != MP_OKAY)
  19302. wc_ecc_get_generator(NULL, -1);
  19303. ret = ret == MP_OKAY ? WOLFSSL_FATAL_ERROR : 0;
  19304. }
  19305. printf(resultFmt, ret == 0 ? passed : failed);
  19306. wc_ecc_del_point(pt);
  19307. #endif
  19308. return ret;
  19309. } /* END test_wc_ecc_get_generator */
  19310. /*
  19311. * Testing wc_ecc_size()
  19312. */
  19313. static int test_wc_ecc_size (void)
  19314. {
  19315. int ret = 0;
  19316. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  19317. WC_RNG rng;
  19318. ecc_key key;
  19319. XMEMSET(&rng, 0, sizeof(rng));
  19320. XMEMSET(&key, 0, sizeof(key));
  19321. ret = wc_InitRng(&rng);
  19322. if (ret == 0) {
  19323. ret = wc_ecc_init(&key);
  19324. if (ret == 0) {
  19325. ret = wc_ecc_make_key(&rng, KEY14, &key);
  19326. }
  19327. }
  19328. printf(testingFmt, "wc_ecc_size()");
  19329. if (ret == 0) {
  19330. ret = wc_ecc_size(&key);
  19331. if (ret == KEY14) {
  19332. ret = 0;
  19333. } else if (ret == 0){
  19334. ret = WOLFSSL_FATAL_ERROR;
  19335. }
  19336. }
  19337. /* Test bad args. */
  19338. if (ret == 0) {
  19339. /* Returns Zero for bad arg. */
  19340. ret = wc_ecc_size(NULL);
  19341. }
  19342. printf(resultFmt, ret == 0 ? passed : failed);
  19343. if (wc_FreeRng(&rng) && ret == 0) {
  19344. ret = WOLFSSL_FATAL_ERROR;
  19345. }
  19346. wc_ecc_free(&key);
  19347. #endif
  19348. return ret;
  19349. } /* END test_wc_ecc_size */
  19350. static void test_wc_ecc_params(void)
  19351. {
  19352. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  19353. It was added after certifications */
  19354. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19355. const ecc_set_type* ecc_set;
  19356. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  19357. /* Test for SECP256R1 curve */
  19358. int curve_id = ECC_SECP256R1;
  19359. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  19360. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  19361. ecc_set = wc_ecc_get_curve_params(curve_idx);
  19362. AssertNotNull(ecc_set);
  19363. AssertIntEQ(ecc_set->id, curve_id);
  19364. #endif
  19365. /* Test case when SECP256R1 is not enabled */
  19366. /* Test that we get curve params for index 0 */
  19367. ecc_set = wc_ecc_get_curve_params(0);
  19368. AssertNotNull(ecc_set);
  19369. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  19370. }
  19371. /*
  19372. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  19373. */
  19374. static int test_wc_ecc_signVerify_hash (void)
  19375. {
  19376. int ret = 0;
  19377. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  19378. WC_RNG rng;
  19379. ecc_key key;
  19380. int signH = WOLFSSL_FATAL_ERROR;
  19381. #ifdef HAVE_ECC_VERIFY
  19382. int verifyH = WOLFSSL_FATAL_ERROR;
  19383. int verify = 0;
  19384. #endif
  19385. word32 siglen = ECC_BUFSIZE;
  19386. byte sig[ECC_BUFSIZE];
  19387. byte adjustedSig[ECC_BUFSIZE+1];
  19388. byte digest[] = TEST_STRING;
  19389. word32 digestlen = (word32)TEST_STRING_SZ;
  19390. /* Init stack var */
  19391. XMEMSET(sig, 0, siglen);
  19392. XMEMSET(&key, 0, sizeof(key));
  19393. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  19394. /* Init structs. */
  19395. ret = wc_InitRng(&rng);
  19396. if (ret == 0) {
  19397. ret = wc_ecc_init(&key);
  19398. if (ret == 0) {
  19399. ret = wc_ecc_make_key(&rng, KEY14, &key);
  19400. }
  19401. }
  19402. printf(testingFmt, "wc_ecc_sign_hash()");
  19403. if (ret == 0) {
  19404. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  19405. }
  19406. /* Check bad args. */
  19407. if (ret == 0) {
  19408. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  19409. if (signH == ECC_BAD_ARG_E) {
  19410. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  19411. &rng, &key);
  19412. }
  19413. if (signH == ECC_BAD_ARG_E) {
  19414. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  19415. &rng, &key);
  19416. }
  19417. if (signH == ECC_BAD_ARG_E) {
  19418. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  19419. NULL, &key);
  19420. }
  19421. if (signH == ECC_BAD_ARG_E) {
  19422. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  19423. &rng, NULL);
  19424. }
  19425. if (signH == ECC_BAD_ARG_E) {
  19426. signH = 0;
  19427. } else if (ret == 0) {
  19428. signH = WOLFSSL_FATAL_ERROR;
  19429. }
  19430. }
  19431. printf(resultFmt, signH == 0 ? passed : failed);
  19432. #ifdef HAVE_ECC_VERIFY
  19433. printf(testingFmt, "wc_ecc_verify_hash()");
  19434. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  19435. if (verify != 1 && ret == 0) {
  19436. ret = WOLFSSL_FATAL_ERROR;
  19437. }
  19438. /* test check on length of signature passed in */
  19439. XMEMCPY(adjustedSig, sig, siglen);
  19440. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  19441. #ifndef NO_STRICT_ECDSA_LEN
  19442. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  19443. &verify, &key), 0);
  19444. #else
  19445. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  19446. * is allowed */
  19447. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  19448. &verify, &key), 0);
  19449. #endif
  19450. /* Test bad args. */
  19451. if (ret == 0) {
  19452. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  19453. &verify, &key);
  19454. if (verifyH == ECC_BAD_ARG_E) {
  19455. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  19456. &verify, &key);
  19457. }
  19458. if (verifyH == ECC_BAD_ARG_E) {
  19459. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  19460. NULL, &key);
  19461. }
  19462. if (verifyH == ECC_BAD_ARG_E) {
  19463. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  19464. &verify, NULL);
  19465. }
  19466. if (verifyH == ECC_BAD_ARG_E) {
  19467. verifyH = 0;
  19468. } else if (ret == 0) {
  19469. verifyH = WOLFSSL_FATAL_ERROR;
  19470. }
  19471. }
  19472. printf(resultFmt, verifyH == 0 ? passed : failed);
  19473. #endif /* HAVE_ECC_VERIFY */
  19474. if (wc_FreeRng(&rng) && ret == 0) {
  19475. ret = WOLFSSL_FATAL_ERROR;
  19476. }
  19477. wc_ecc_free(&key);
  19478. #ifdef FP_ECC
  19479. wc_ecc_fp_free();
  19480. #endif
  19481. #endif
  19482. return ret;
  19483. } /* END test_wc_ecc_sign_hash */
  19484. /*
  19485. * Testing wc_ecc_shared_secret()
  19486. */
  19487. static int test_wc_ecc_shared_secret (void)
  19488. {
  19489. int ret = 0;
  19490. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  19491. ecc_key key, pubKey;
  19492. WC_RNG rng;
  19493. int keySz = KEY16;
  19494. byte out[KEY16];
  19495. word32 outlen = (word32)sizeof(out);
  19496. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  19497. const char* qx =
  19498. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  19499. const char* qy =
  19500. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  19501. const char* d =
  19502. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  19503. const char* curveName = "SECP256R1";
  19504. const byte expected_shared_secret[] =
  19505. {
  19506. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  19507. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  19508. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  19509. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  19510. };
  19511. #endif
  19512. /* Initialize variables. */
  19513. XMEMSET(out, 0, keySz);
  19514. XMEMSET(&rng, 0, sizeof(rng));
  19515. XMEMSET(&key, 0, sizeof(key));
  19516. XMEMSET(&pubKey, 0, sizeof(pubKey));
  19517. ret = wc_InitRng(&rng);
  19518. if (ret == 0) {
  19519. ret = wc_ecc_init(&key);
  19520. if (ret == 0) {
  19521. ret = wc_ecc_init(&pubKey);
  19522. }
  19523. }
  19524. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  19525. if (ret == 0) {
  19526. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  19527. }
  19528. if (ret == 0) {
  19529. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  19530. }
  19531. #else
  19532. if (ret == 0) {
  19533. ret = wc_ecc_make_key(&rng, keySz, &key);
  19534. }
  19535. if (ret == 0) {
  19536. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  19537. }
  19538. #endif
  19539. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19540. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19541. !defined(HAVE_SELFTEST)
  19542. if (ret == 0) {
  19543. ret = wc_ecc_set_rng(&key, &rng);
  19544. }
  19545. #endif
  19546. printf(testingFmt, "wc_ecc_shared_secret()");
  19547. if (ret == 0) {
  19548. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  19549. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  19550. if (ret == 0) {
  19551. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  19552. ret = WOLFSSL_FATAL_ERROR;
  19553. }
  19554. }
  19555. #endif
  19556. /* Test bad args. */
  19557. if (ret == 0) {
  19558. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  19559. if (ret == BAD_FUNC_ARG) {
  19560. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  19561. }
  19562. if (ret == BAD_FUNC_ARG) {
  19563. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  19564. }
  19565. if (ret == BAD_FUNC_ARG) {
  19566. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  19567. }
  19568. if (ret == BAD_FUNC_ARG) {
  19569. /* Invalid length */
  19570. outlen = 1;
  19571. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  19572. }
  19573. if (ret == BUFFER_E) {
  19574. ret = 0;
  19575. } else if (ret == 0) {
  19576. ret = WOLFSSL_FATAL_ERROR;
  19577. }
  19578. }
  19579. }
  19580. printf(resultFmt, ret == 0 ? passed : failed);
  19581. if (wc_FreeRng(&rng) && ret == 0) {
  19582. ret = WOLFSSL_FATAL_ERROR;
  19583. }
  19584. wc_ecc_free(&key);
  19585. wc_ecc_free(&pubKey);
  19586. #ifdef FP_ECC
  19587. wc_ecc_fp_free();
  19588. #endif
  19589. #endif
  19590. return ret;
  19591. } /* END tests_wc_ecc_shared_secret */
  19592. /*
  19593. * testint wc_ecc_export_x963()
  19594. */
  19595. static int test_wc_ecc_export_x963 (void)
  19596. {
  19597. int ret = 0;
  19598. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19599. ecc_key key;
  19600. WC_RNG rng;
  19601. byte out[ECC_ASN963_MAX_BUF_SZ];
  19602. word32 outlen = sizeof(out);
  19603. /* Initialize variables. */
  19604. XMEMSET(out, 0, outlen);
  19605. XMEMSET(&rng, 0, sizeof(rng));
  19606. XMEMSET(&key, 0, sizeof(key));
  19607. ret = wc_InitRng(&rng);
  19608. if (ret == 0) {
  19609. ret = wc_ecc_init(&key);
  19610. if (ret == 0) {
  19611. ret = wc_ecc_make_key(&rng, KEY20, &key);
  19612. }
  19613. }
  19614. printf(testingFmt, "wc_ecc_export_x963()");
  19615. if (ret == 0) {
  19616. ret = wc_ecc_export_x963(&key, out, &outlen);
  19617. }
  19618. /* Test bad args. */
  19619. if (ret == 0) {
  19620. ret = wc_ecc_export_x963(NULL, out, &outlen);
  19621. if (ret == ECC_BAD_ARG_E) {
  19622. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  19623. }
  19624. if (ret == LENGTH_ONLY_E) {
  19625. ret = wc_ecc_export_x963(&key, out, NULL);
  19626. }
  19627. if (ret == ECC_BAD_ARG_E) {
  19628. key.idx = -4;
  19629. ret = wc_ecc_export_x963(&key, out, &outlen);
  19630. }
  19631. if (ret == ECC_BAD_ARG_E) {
  19632. ret = 0;
  19633. } else {
  19634. ret = WOLFSSL_FATAL_ERROR;
  19635. }
  19636. }
  19637. printf(resultFmt, ret == 0 ? passed : failed);
  19638. if (wc_FreeRng(&rng) && ret == 0) {
  19639. ret = WOLFSSL_FATAL_ERROR;
  19640. }
  19641. wc_ecc_free(&key);
  19642. #ifdef FP_ECC
  19643. wc_ecc_fp_free();
  19644. #endif
  19645. #endif
  19646. return ret;
  19647. } /* END test_wc_ecc_export_x963 */
  19648. /*
  19649. * Testing wc_ecc_export_x963_ex()
  19650. * compile with --enable-compkey will use compression.
  19651. */
  19652. static int test_wc_ecc_export_x963_ex (void)
  19653. {
  19654. int ret = 0;
  19655. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19656. ecc_key key;
  19657. WC_RNG rng;
  19658. byte out[ECC_ASN963_MAX_BUF_SZ];
  19659. word32 outlen = sizeof(out);
  19660. #ifdef HAVE_COMP_KEY
  19661. word32 badOutLen = 5;
  19662. #endif
  19663. /* Init stack variables. */
  19664. XMEMSET(out, 0, outlen);
  19665. XMEMSET(&rng, 0, sizeof(rng));
  19666. XMEMSET(&key, 0, sizeof(key));
  19667. ret = wc_InitRng(&rng);
  19668. if (ret == 0) {
  19669. ret = wc_ecc_init(&key);
  19670. if (ret == 0) {
  19671. ret = wc_ecc_make_key(&rng, KEY64, &key);
  19672. }
  19673. }
  19674. printf(testingFmt, "wc_ecc_export_x963_ex()");
  19675. #ifdef HAVE_COMP_KEY
  19676. if (ret == 0) {
  19677. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  19678. }
  19679. #else
  19680. if (ret == 0) {
  19681. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  19682. }
  19683. #endif
  19684. /* Test bad args. */
  19685. #ifdef HAVE_COMP_KEY
  19686. if (ret == 0) {
  19687. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  19688. if (ret == BAD_FUNC_ARG) {
  19689. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  19690. }
  19691. if (ret == BAD_FUNC_ARG) {
  19692. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  19693. }
  19694. if (ret == BAD_FUNC_ARG) {
  19695. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  19696. }
  19697. if (ret == BUFFER_E) {
  19698. key.idx = -4;
  19699. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  19700. }
  19701. if (ret == ECC_BAD_ARG_E) {
  19702. ret = 0;
  19703. } else {
  19704. ret = WOLFSSL_FATAL_ERROR;
  19705. }
  19706. }
  19707. #else
  19708. if (ret == 0) {
  19709. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  19710. if (ret == BAD_FUNC_ARG) {
  19711. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  19712. }
  19713. if (ret == BAD_FUNC_ARG) {
  19714. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  19715. }
  19716. if (ret == NOT_COMPILED_IN) {
  19717. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  19718. }
  19719. if (ret == BAD_FUNC_ARG) {
  19720. key.idx = -4;
  19721. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  19722. }
  19723. if (ret == ECC_BAD_ARG_E) {
  19724. ret = 0;
  19725. } else if (ret == 0) {
  19726. ret = WOLFSSL_FATAL_ERROR;
  19727. }
  19728. }
  19729. #endif
  19730. printf(resultFmt, ret == 0 ? passed : failed);
  19731. if (wc_FreeRng(&rng) && ret == 0) {
  19732. ret = WOLFSSL_FATAL_ERROR;
  19733. }
  19734. wc_ecc_free(&key);
  19735. #ifdef FP_ECC
  19736. wc_ecc_fp_free();
  19737. #endif
  19738. #endif
  19739. return ret;
  19740. } /* END test_wc_ecc_export_x963_ex */
  19741. /*
  19742. * testing wc_ecc_import_x963()
  19743. */
  19744. static int test_wc_ecc_import_x963 (void)
  19745. {
  19746. int ret = 0;
  19747. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  19748. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19749. ecc_key pubKey, key;
  19750. WC_RNG rng;
  19751. byte x963[ECC_ASN963_MAX_BUF_SZ];
  19752. word32 x963Len = (word32)sizeof(x963);
  19753. /* Init stack variables. */
  19754. XMEMSET(x963, 0, x963Len);
  19755. XMEMSET(&rng, 0, sizeof(rng));
  19756. XMEMSET(&key, 0, sizeof(key));
  19757. XMEMSET(&pubKey, 0, sizeof(pubKey));
  19758. ret = wc_InitRng(&rng);
  19759. if (ret == 0) {
  19760. ret = wc_ecc_init(&pubKey);
  19761. if (ret == 0) {
  19762. ret = wc_ecc_init(&key);
  19763. }
  19764. if (ret == 0) {
  19765. ret = wc_ecc_make_key(&rng, KEY24, &key);
  19766. }
  19767. if (ret == 0) {
  19768. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  19769. }
  19770. }
  19771. printf(testingFmt, "wc_ecc_import_x963()");
  19772. if (ret == 0) {
  19773. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  19774. }
  19775. /* Test bad args. */
  19776. if (ret == 0) {
  19777. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  19778. if (ret == BAD_FUNC_ARG) {
  19779. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  19780. }
  19781. if (ret == BAD_FUNC_ARG) {
  19782. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  19783. }
  19784. if (ret == ECC_BAD_ARG_E) {
  19785. ret = 0;
  19786. } else if (ret == 0) {
  19787. ret = WOLFSSL_FATAL_ERROR;
  19788. }
  19789. }
  19790. printf(resultFmt, ret == 0 ? passed : failed);
  19791. if (wc_FreeRng(&rng) && ret == 0) {
  19792. ret = WOLFSSL_FATAL_ERROR;
  19793. }
  19794. wc_ecc_free(&key);
  19795. wc_ecc_free(&pubKey);
  19796. #ifdef FP_ECC
  19797. wc_ecc_fp_free();
  19798. #endif
  19799. #endif
  19800. return ret;
  19801. } /* END wc_ecc_import_x963 */
  19802. /*
  19803. * testing wc_ecc_import_private_key()
  19804. */
  19805. static int ecc_import_private_key (void)
  19806. {
  19807. int ret = 0;
  19808. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  19809. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19810. ecc_key key, keyImp;
  19811. WC_RNG rng;
  19812. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  19813. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  19814. word32 privKeySz = (word32)sizeof(privKey);
  19815. word32 x963KeySz = (word32)sizeof(x963Key);
  19816. /* Init stack variables. */
  19817. XMEMSET(privKey, 0, privKeySz);
  19818. XMEMSET(x963Key, 0, x963KeySz);
  19819. XMEMSET(&rng, 0, sizeof(rng));
  19820. XMEMSET(&key, 0, sizeof(key));
  19821. XMEMSET(&keyImp, 0, sizeof(keyImp));
  19822. ret = wc_InitRng(&rng);
  19823. if (ret == 0) {
  19824. ret = wc_ecc_init(&key);
  19825. if (ret == 0) {
  19826. ret = wc_ecc_init(&keyImp);
  19827. }
  19828. if (ret == 0) {
  19829. ret = wc_ecc_make_key(&rng, KEY48, &key);
  19830. }
  19831. if (ret == 0) {
  19832. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  19833. }
  19834. if (ret == 0) {
  19835. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  19836. }
  19837. }
  19838. printf(testingFmt, "wc_ecc_import_private_key()");
  19839. if (ret == 0) {
  19840. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  19841. x963KeySz, &keyImp);
  19842. }
  19843. /* Pass in bad args. */
  19844. if (ret == 0) {
  19845. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  19846. x963KeySz, NULL);
  19847. if (ret == BAD_FUNC_ARG) {
  19848. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  19849. x963KeySz, &keyImp);
  19850. }
  19851. if (ret == BAD_FUNC_ARG) {
  19852. ret = 0;
  19853. } else if (ret == 0) {
  19854. ret = WOLFSSL_FATAL_ERROR;
  19855. }
  19856. }
  19857. printf(resultFmt, ret == 0 ? passed : failed);
  19858. if (wc_FreeRng(&rng) && ret == 0) {
  19859. ret = WOLFSSL_FATAL_ERROR;
  19860. }
  19861. wc_ecc_free(&key);
  19862. wc_ecc_free(&keyImp);
  19863. #ifdef FP_ECC
  19864. wc_ecc_fp_free();
  19865. #endif
  19866. #endif
  19867. return ret;
  19868. } /* END wc_ecc_import_private_key */
  19869. /*
  19870. * Testing wc_ecc_export_private_only()
  19871. */
  19872. static int test_wc_ecc_export_private_only (void)
  19873. {
  19874. int ret = 0;
  19875. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19876. ecc_key key;
  19877. WC_RNG rng;
  19878. byte out[ECC_PRIV_KEY_BUF];
  19879. word32 outlen = sizeof(out);
  19880. /* Init stack variables. */
  19881. XMEMSET(out, 0, outlen);
  19882. XMEMSET(&rng, 0, sizeof(rng));
  19883. XMEMSET(&key, 0, sizeof(key));
  19884. ret = wc_InitRng(&rng);
  19885. if (ret == 0) {
  19886. ret = wc_ecc_init(&key);
  19887. if (ret == 0) {
  19888. ret = wc_ecc_make_key(&rng, KEY32, &key);
  19889. }
  19890. }
  19891. printf(testingFmt, "wc_ecc_export_private_only()");
  19892. if (ret == 0) {
  19893. ret = wc_ecc_export_private_only(&key, out, &outlen);
  19894. }
  19895. /* Pass in bad args. */
  19896. if (ret == 0) {
  19897. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  19898. if (ret == BAD_FUNC_ARG) {
  19899. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  19900. }
  19901. if (ret == BAD_FUNC_ARG) {
  19902. ret = wc_ecc_export_private_only(&key, out, NULL);
  19903. }
  19904. if (ret == BAD_FUNC_ARG) {
  19905. ret = 0;
  19906. } else if (ret == 0) {
  19907. ret = WOLFSSL_FATAL_ERROR;
  19908. }
  19909. }
  19910. printf(resultFmt, ret == 0 ? passed : failed);
  19911. if (wc_FreeRng(&rng) && ret == 0) {
  19912. ret = WOLFSSL_FATAL_ERROR;
  19913. }
  19914. wc_ecc_free(&key);
  19915. #ifdef FP_ECC
  19916. wc_ecc_fp_free();
  19917. #endif
  19918. #endif
  19919. return ret;
  19920. } /* END test_wc_ecc_export_private_only */
  19921. /*
  19922. * Testing wc_ecc_rs_to_sig()
  19923. */
  19924. static int test_wc_ecc_rs_to_sig (void)
  19925. {
  19926. int ret = 0;
  19927. #if defined(HAVE_ECC) && !defined(NO_ASN)
  19928. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  19929. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  19930. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  19931. const char* zeroStr = "0";
  19932. byte sig[ECC_MAX_SIG_SIZE];
  19933. word32 siglen = (word32)sizeof(sig);
  19934. /*R and S max size is the order of curve. 2^192.*/
  19935. int keySz = KEY24;
  19936. byte r[KEY24];
  19937. byte s[KEY24];
  19938. word32 rlen = (word32)sizeof(r);
  19939. word32 slen = (word32)sizeof(s);
  19940. /* Init stack variables. */
  19941. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  19942. XMEMSET(r, 0, keySz);
  19943. XMEMSET(s, 0, keySz);
  19944. printf(testingFmt, "wc_ecc_rs_to_sig()");
  19945. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  19946. /* Test bad args. */
  19947. if (ret == 0) {
  19948. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  19949. if (ret == ECC_BAD_ARG_E) {
  19950. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  19951. }
  19952. if (ret == ECC_BAD_ARG_E) {
  19953. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  19954. }
  19955. if (ret == ECC_BAD_ARG_E) {
  19956. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  19957. }
  19958. if (ret == ECC_BAD_ARG_E) {
  19959. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  19960. }
  19961. if (ret == MP_ZERO_E) {
  19962. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  19963. }
  19964. if (ret == MP_ZERO_E) {
  19965. ret = 0;
  19966. } else {
  19967. ret = WOLFSSL_FATAL_ERROR;
  19968. }
  19969. }
  19970. printf(resultFmt, ret == 0 ? passed : failed);
  19971. printf(testingFmt, "wc_ecc_sig_to_rs()");
  19972. if (ret == 0) {
  19973. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  19974. }
  19975. /* Test bad args. */
  19976. if (ret == 0) {
  19977. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  19978. if (ret == ECC_BAD_ARG_E) {
  19979. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  19980. }
  19981. if (ret == ECC_BAD_ARG_E) {
  19982. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  19983. }
  19984. if (ret == ECC_BAD_ARG_E) {
  19985. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  19986. }
  19987. if (ret == ECC_BAD_ARG_E) {
  19988. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  19989. }
  19990. if (ret == ECC_BAD_ARG_E) {
  19991. ret = 0;
  19992. } else if (ret == 0) {
  19993. ret = WOLFSSL_FATAL_ERROR;
  19994. }
  19995. }
  19996. printf(resultFmt, ret == 0 ? passed : failed);
  19997. #endif
  19998. return ret;
  19999. } /* END test_wc_ecc_rs_to_sig */
  20000. static int test_wc_ecc_import_raw(void)
  20001. {
  20002. int ret = 0;
  20003. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20004. ecc_key key;
  20005. const char* qx =
  20006. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  20007. const char* qy =
  20008. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  20009. const char* d =
  20010. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  20011. const char* curveName = "SECP256R1";
  20012. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  20013. const char* kNullStr = "";
  20014. #endif
  20015. ret = wc_ecc_init(&key);
  20016. printf(testingFmt, "wc_ecc_import_raw()");
  20017. if (ret == 0) {
  20018. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  20019. }
  20020. /* Test bad args. */
  20021. if (ret == 0) {
  20022. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  20023. if (ret == BAD_FUNC_ARG) {
  20024. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  20025. }
  20026. if (ret == BAD_FUNC_ARG) {
  20027. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  20028. }
  20029. if (ret == BAD_FUNC_ARG) {
  20030. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  20031. }
  20032. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  20033. if (ret == BAD_FUNC_ARG) {
  20034. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  20035. wc_ecc_free(&key);
  20036. #endif
  20037. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  20038. if (ret == ECC_INF_E)
  20039. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  20040. }
  20041. #endif
  20042. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  20043. if (ret == BAD_FUNC_ARG) {
  20044. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  20045. wc_ecc_free(&key);
  20046. #endif
  20047. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  20048. }
  20049. if (ret == BAD_FUNC_ARG) {
  20050. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  20051. wc_ecc_free(&key);
  20052. #endif
  20053. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  20054. }
  20055. #endif
  20056. if (ret == BAD_FUNC_ARG) {
  20057. ret = 0;
  20058. }
  20059. }
  20060. printf(resultFmt, ret == 0 ? passed : failed);
  20061. wc_ecc_free(&key);
  20062. #endif
  20063. return ret;
  20064. } /* END test_wc_ecc_import_raw */
  20065. static int test_wc_ecc_import_unsigned(void)
  20066. {
  20067. int ret = 0;
  20068. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  20069. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  20070. ecc_key key;
  20071. const byte qx[] = {
  20072. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  20073. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  20074. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  20075. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  20076. };
  20077. const byte qy[] = {
  20078. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  20079. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  20080. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  20081. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  20082. };
  20083. const byte d[] = {
  20084. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  20085. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  20086. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  20087. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  20088. };
  20089. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  20090. const byte nullBytes[32] = {0};
  20091. #endif
  20092. int curveId = ECC_SECP256R1;
  20093. ret = wc_ecc_init(&key);
  20094. printf(testingFmt, "wc_ecc_import_unsigned()");
  20095. if (ret == 0) {
  20096. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  20097. curveId);
  20098. }
  20099. /* Test bad args. */
  20100. if (ret == 0) {
  20101. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  20102. curveId);
  20103. if (ret == BAD_FUNC_ARG) {
  20104. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  20105. curveId);
  20106. }
  20107. if (ret == BAD_FUNC_ARG) {
  20108. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  20109. curveId);
  20110. }
  20111. if (ret == BAD_FUNC_ARG) {
  20112. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  20113. ECC_CURVE_INVALID);
  20114. }
  20115. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  20116. if (ret == BAD_FUNC_ARG) {
  20117. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  20118. (byte*)nullBytes, (byte*)nullBytes, curveId);
  20119. }
  20120. #endif
  20121. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  20122. ret = 0;
  20123. }
  20124. }
  20125. printf(resultFmt, ret == 0 ? passed : failed);
  20126. wc_ecc_free(&key);
  20127. #endif
  20128. return ret;
  20129. } /* END test_wc_ecc_import_unsigned */
  20130. /*
  20131. * Testing wc_ecc_sig_size()
  20132. */
  20133. static int test_wc_ecc_sig_size (void)
  20134. {
  20135. int ret = 0;
  20136. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20137. ecc_key key;
  20138. WC_RNG rng;
  20139. int keySz = KEY16;
  20140. XMEMSET(&rng, 0, sizeof(rng));
  20141. XMEMSET(&key, 0, sizeof(key));
  20142. ret = wc_InitRng(&rng);
  20143. if (ret == 0) {
  20144. ret = wc_ecc_init(&key);
  20145. if (ret == 0) {
  20146. ret = wc_ecc_make_key(&rng, keySz, &key);
  20147. }
  20148. }
  20149. printf(testingFmt, "wc_ecc_sig_size()");
  20150. if (ret == 0) {
  20151. ret = wc_ecc_sig_size(&key);
  20152. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  20153. ret = 0;
  20154. }
  20155. }
  20156. printf(resultFmt, ret == 0 ? passed : failed);
  20157. if (wc_FreeRng(&rng) && ret == 0) {
  20158. ret = WOLFSSL_FATAL_ERROR;
  20159. }
  20160. wc_ecc_free(&key);
  20161. #endif
  20162. return ret;
  20163. } /* END test_wc_ecc_sig_size */
  20164. /*
  20165. * Testing wc_ecc_ctx_new()
  20166. */
  20167. static int test_wc_ecc_ctx_new (void)
  20168. {
  20169. int ret = 0;
  20170. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  20171. WC_RNG rng;
  20172. ecEncCtx* cli = NULL;
  20173. ecEncCtx* srv = NULL;
  20174. ret = wc_InitRng(&rng);
  20175. printf(testingFmt, "wc_ecc_ctx_new()");
  20176. if (ret == 0) {
  20177. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  20178. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  20179. }
  20180. if (ret == 0 && (cli == NULL || srv == NULL)) {
  20181. ret = WOLFSSL_FATAL_ERROR;
  20182. }
  20183. wc_ecc_ctx_free(cli);
  20184. wc_ecc_ctx_free(srv);
  20185. /* Test bad args. */
  20186. if (ret == 0) {
  20187. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  20188. cli = wc_ecc_ctx_new(0, &rng);
  20189. if (cli != NULL) {
  20190. ret = WOLFSSL_FATAL_ERROR;
  20191. }
  20192. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  20193. if (cli != NULL) {
  20194. ret = WOLFSSL_FATAL_ERROR;
  20195. }
  20196. }
  20197. printf(resultFmt, ret == 0 ? passed : failed);
  20198. if (wc_FreeRng(&rng) && ret == 0) {
  20199. ret = WOLFSSL_FATAL_ERROR;
  20200. }
  20201. wc_ecc_ctx_free(cli);
  20202. #endif
  20203. return ret;
  20204. } /* END test_wc_ecc_ctx_new */
  20205. /*
  20206. * Tesing wc_ecc_reset()
  20207. */
  20208. static int test_wc_ecc_ctx_reset (void)
  20209. {
  20210. int ret = 0;
  20211. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  20212. ecEncCtx* ctx = NULL;
  20213. WC_RNG rng;
  20214. ret = wc_InitRng(&rng);
  20215. if (ret == 0) {
  20216. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  20217. ret = WOLFSSL_FATAL_ERROR;
  20218. }
  20219. }
  20220. printf(testingFmt, "wc_ecc_ctx_reset()");
  20221. if (ret == 0) {
  20222. ret = wc_ecc_ctx_reset(ctx, &rng);
  20223. }
  20224. /* Pass in bad args. */
  20225. if (ret == 0) {
  20226. ret = wc_ecc_ctx_reset(NULL, &rng);
  20227. if (ret == BAD_FUNC_ARG) {
  20228. ret = wc_ecc_ctx_reset(ctx, NULL);
  20229. }
  20230. if (ret == BAD_FUNC_ARG) {
  20231. ret = 0;
  20232. } else if (ret == 0) {
  20233. ret = WOLFSSL_FATAL_ERROR;
  20234. }
  20235. }
  20236. printf(resultFmt, ret == 0 ? passed : failed);
  20237. if (wc_FreeRng(&rng) && ret == 0) {
  20238. ret = WOLFSSL_FATAL_ERROR;
  20239. }
  20240. wc_ecc_ctx_free(ctx);
  20241. #endif
  20242. return ret;
  20243. } /* END test_wc_ecc_ctx_reset */
  20244. /*
  20245. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  20246. */
  20247. static int test_wc_ecc_ctx_set_peer_salt (void)
  20248. {
  20249. int ret = 0;
  20250. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  20251. WC_RNG rng;
  20252. ecEncCtx* cliCtx = NULL;
  20253. ecEncCtx* servCtx = NULL;
  20254. const byte* cliSalt = NULL;
  20255. const byte* servSalt = NULL;
  20256. ret = wc_InitRng(&rng);
  20257. if (ret == 0) {
  20258. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  20259. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  20260. ret = WOLFSSL_FATAL_ERROR;
  20261. }
  20262. }
  20263. printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
  20264. /* Test bad args. */
  20265. if (ret == 0) {
  20266. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  20267. if (cliSalt != NULL) {
  20268. ret = WOLFSSL_FATAL_ERROR;
  20269. }
  20270. }
  20271. if (ret == 0) {
  20272. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  20273. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  20274. if (cliSalt == NULL || servSalt == NULL) {
  20275. ret = WOLFSSL_FATAL_ERROR;
  20276. }
  20277. }
  20278. printf(resultFmt, ret == 0 ? passed : failed);
  20279. printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
  20280. if (ret == 0) {
  20281. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  20282. }
  20283. /* Test bad args. */
  20284. if (ret == 0) {
  20285. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  20286. if (ret == BAD_FUNC_ARG) {
  20287. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  20288. }
  20289. if (ret == BAD_FUNC_ARG) {
  20290. ret = 0;
  20291. } else if (ret == 0) {
  20292. ret = WOLFSSL_FATAL_ERROR;
  20293. }
  20294. }
  20295. printf(resultFmt, ret == 0 ? passed : failed);
  20296. if (wc_FreeRng(&rng) && ret == 0) {
  20297. ret = WOLFSSL_FATAL_ERROR;
  20298. }
  20299. wc_ecc_ctx_free(cliCtx);
  20300. wc_ecc_ctx_free(servCtx);
  20301. #endif
  20302. return ret;
  20303. } /* END test_wc_ecc_ctx_set_peer_salt */
  20304. /*
  20305. * Testing wc_ecc_ctx_set_info()
  20306. */
  20307. static int test_wc_ecc_ctx_set_info (void)
  20308. {
  20309. int ret = 0;
  20310. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  20311. ecEncCtx* ctx = NULL;
  20312. WC_RNG rng;
  20313. const char* optInfo = "Optional Test Info.";
  20314. int optInfoSz = (int)XSTRLEN(optInfo);
  20315. const char* badOptInfo = NULL;
  20316. ret = wc_InitRng(&rng);
  20317. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  20318. ret = WOLFSSL_FATAL_ERROR;
  20319. }
  20320. printf(testingFmt, "wc_ecc_ctx_set_info()");
  20321. if (ret == 0) {
  20322. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  20323. }
  20324. /* Test bad args. */
  20325. if (ret == 0) {
  20326. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  20327. if (ret == BAD_FUNC_ARG) {
  20328. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  20329. }
  20330. if (ret == BAD_FUNC_ARG) {
  20331. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  20332. }
  20333. if (ret == BAD_FUNC_ARG) {
  20334. ret = 0;
  20335. } else if (ret == 0) {
  20336. ret = WOLFSSL_FATAL_ERROR;
  20337. }
  20338. }
  20339. printf(resultFmt, ret == 0 ? passed : failed);
  20340. if (wc_FreeRng(&rng) && ret == 0) {
  20341. ret = WOLFSSL_FATAL_ERROR;
  20342. }
  20343. wc_ecc_ctx_free(ctx);
  20344. #endif
  20345. return ret;
  20346. } /* END test_wc_ecc_ctx_set_info */
  20347. /*
  20348. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  20349. */
  20350. static int test_wc_ecc_encryptDecrypt (void)
  20351. {
  20352. int ret = 0;
  20353. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  20354. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  20355. ecc_key srvKey, cliKey, tmpKey;
  20356. WC_RNG rng;
  20357. const char* msg = "EccBlock Size 16";
  20358. word32 msgSz = (word32)XSTRLEN(msg);
  20359. #ifdef WOLFSSL_ECIES_OLD
  20360. byte out[XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
  20361. #else
  20362. byte out[KEY20 * 2 + 1 + XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
  20363. #endif
  20364. word32 outSz = (word32)sizeof(out);
  20365. byte plain[XSTRLEN(msg) + 1];
  20366. word32 plainSz = (word32)sizeof(plain);
  20367. int keySz = KEY20;
  20368. /* Init stack variables. */
  20369. XMEMSET(out, 0, outSz);
  20370. XMEMSET(plain, 0, plainSz);
  20371. XMEMSET(&rng, 0, sizeof(rng));
  20372. XMEMSET(&srvKey, 0, sizeof(srvKey));
  20373. XMEMSET(&cliKey, 0, sizeof(cliKey));
  20374. ret = wc_InitRng(&rng);
  20375. if (ret == 0) {
  20376. ret = wc_ecc_init(&cliKey);
  20377. if (ret == 0) {
  20378. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  20379. }
  20380. if (ret == 0) {
  20381. ret = wc_ecc_init(&srvKey);
  20382. }
  20383. if (ret == 0) {
  20384. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  20385. }
  20386. if (ret == 0) {
  20387. ret = wc_ecc_init(&tmpKey);
  20388. }
  20389. }
  20390. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20391. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20392. !defined(HAVE_SELFTEST)
  20393. if (ret == 0) {
  20394. ret = wc_ecc_set_rng(&srvKey, &rng);
  20395. }
  20396. if (ret == 0) {
  20397. ret = wc_ecc_set_rng(&cliKey, &rng);
  20398. }
  20399. #endif
  20400. printf(testingFmt, "wc_ecc_encrypt()");
  20401. if (ret == 0) {
  20402. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  20403. &outSz, NULL);
  20404. }
  20405. if (ret == 0) {
  20406. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  20407. &outSz, NULL);
  20408. if (ret == BAD_FUNC_ARG) {
  20409. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  20410. &outSz, NULL);
  20411. }
  20412. if (ret == BAD_FUNC_ARG) {
  20413. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  20414. &outSz, NULL);
  20415. }
  20416. if (ret == BAD_FUNC_ARG) {
  20417. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  20418. &outSz, NULL);
  20419. }
  20420. if (ret == BAD_FUNC_ARG) {
  20421. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  20422. NULL, NULL);
  20423. }
  20424. if (ret == BAD_FUNC_ARG) {
  20425. ret = 0;
  20426. } else if (ret == 0) {
  20427. ret = WOLFSSL_FATAL_ERROR;
  20428. }
  20429. }
  20430. printf(resultFmt, ret == 0 ? passed : failed);
  20431. printf(testingFmt, "wc_ecc_decrypt()");
  20432. #ifdef WOLFSSL_ECIES_OLD
  20433. if (ret == 0) {
  20434. tmpKey.dp = cliKey.dp;
  20435. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  20436. }
  20437. #endif
  20438. if (ret == 0) {
  20439. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  20440. &plainSz, NULL);
  20441. }
  20442. if (ret == 0) {
  20443. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  20444. &plainSz, NULL);
  20445. #ifdef WOLFSSL_ECIES_OLD
  20446. /* NULL parameter allowed in new implementations - public key comes from
  20447. * the message. */
  20448. if (ret == BAD_FUNC_ARG) {
  20449. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  20450. &plainSz, NULL);
  20451. }
  20452. #endif
  20453. if (ret == BAD_FUNC_ARG) {
  20454. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  20455. &plainSz, NULL);
  20456. }
  20457. if (ret == BAD_FUNC_ARG) {
  20458. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  20459. &plainSz, NULL);
  20460. }
  20461. if (ret == BAD_FUNC_ARG) {
  20462. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  20463. plain, NULL, NULL);
  20464. }
  20465. if (ret == BAD_FUNC_ARG) {
  20466. ret = 0;
  20467. } else if (ret == 0) {
  20468. ret = WOLFSSL_FATAL_ERROR;
  20469. }
  20470. }
  20471. if (XMEMCMP(msg, plain, msgSz) != 0) {
  20472. ret = WOLFSSL_FATAL_ERROR;
  20473. }
  20474. printf(resultFmt, ret == 0 ? passed : failed);
  20475. if (wc_FreeRng(&rng) && ret == 0) {
  20476. ret = WOLFSSL_FATAL_ERROR;
  20477. }
  20478. wc_ecc_free(&tmpKey);
  20479. wc_ecc_free(&cliKey);
  20480. wc_ecc_free(&srvKey);
  20481. #endif
  20482. return ret;
  20483. } /* END test_wc_ecc_encryptDecrypt */
  20484. /*
  20485. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  20486. */
  20487. static int test_wc_ecc_del_point (void)
  20488. {
  20489. int ret = 0;
  20490. #if defined(HAVE_ECC)
  20491. ecc_point* pt;
  20492. printf(testingFmt, "wc_ecc_new_point()");
  20493. pt = wc_ecc_new_point();
  20494. if (!pt) {
  20495. ret = WOLFSSL_FATAL_ERROR;
  20496. }
  20497. printf(resultFmt, ret == 0 ? passed : failed);
  20498. wc_ecc_del_point(pt);
  20499. #endif
  20500. return ret;
  20501. } /* END test_wc_ecc_del_point */
  20502. /*
  20503. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  20504. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  20505. * and wc_ecc_cmp_point()
  20506. */
  20507. static int test_wc_ecc_pointFns (void)
  20508. {
  20509. int ret = 0;
  20510. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  20511. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  20512. !defined(WOLFSSL_ATECC608A)
  20513. ecc_key key;
  20514. WC_RNG rng;
  20515. ecc_point* point = NULL;
  20516. ecc_point* cpypt = NULL;
  20517. int idx = 0;
  20518. int keySz = KEY32;
  20519. byte der[DER_SZ(KEY32)];
  20520. word32 derlenChk = 0;
  20521. word32 derSz = DER_SZ(KEY32);
  20522. /* Init stack variables. */
  20523. XMEMSET(der, 0, derSz);
  20524. XMEMSET(&rng, 0, sizeof(rng));
  20525. XMEMSET(&key, 0, sizeof(key));
  20526. ret = wc_InitRng(&rng);
  20527. if (ret == 0) {
  20528. ret = wc_ecc_init(&key);
  20529. if (ret == 0) {
  20530. ret = wc_ecc_make_key(&rng, keySz, &key);
  20531. }
  20532. }
  20533. if (ret == 0) {
  20534. point = wc_ecc_new_point();
  20535. if (!point) {
  20536. ret = WOLFSSL_FATAL_ERROR;
  20537. }
  20538. }
  20539. if (ret == 0) {
  20540. cpypt = wc_ecc_new_point();
  20541. if (!cpypt) {
  20542. ret = WOLFSSL_FATAL_ERROR;
  20543. }
  20544. }
  20545. /* Export */
  20546. printf(testingFmt, "wc_ecc_export_point_der()");
  20547. if (ret == 0) {
  20548. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  20549. NULL, &derlenChk);
  20550. /* Check length value. */
  20551. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  20552. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  20553. der, &derSz);
  20554. }
  20555. }
  20556. /* Test bad args. */
  20557. if (ret == 0) {
  20558. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  20559. if (ret == ECC_BAD_ARG_E) {
  20560. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  20561. }
  20562. if (ret == ECC_BAD_ARG_E) {
  20563. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  20564. der, NULL);
  20565. }
  20566. if (ret == ECC_BAD_ARG_E) {
  20567. ret = 0;
  20568. } else if (ret == 0) {
  20569. ret = WOLFSSL_FATAL_ERROR;
  20570. }
  20571. }
  20572. printf(resultFmt, ret == 0 ? passed : failed);
  20573. /* Import */
  20574. printf(testingFmt, "wc_ecc_import_point_der()");
  20575. if (ret == 0) {
  20576. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  20577. /* Condition double checks wc_ecc_cmp_point(). */
  20578. if (ret == 0 && XMEMCMP(&key.pubkey, point, sizeof(key.pubkey))) {
  20579. ret = wc_ecc_cmp_point(&key.pubkey, point);
  20580. }
  20581. }
  20582. /* Test bad args. */
  20583. if (ret == 0) {
  20584. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  20585. if (ret == ECC_BAD_ARG_E) {
  20586. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  20587. }
  20588. if (ret == ECC_BAD_ARG_E) {
  20589. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  20590. }
  20591. if (ret == ECC_BAD_ARG_E) {
  20592. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  20593. }
  20594. if (ret == ECC_BAD_ARG_E) {
  20595. ret = 0;
  20596. } else if (ret == 0) {
  20597. ret = WOLFSSL_FATAL_ERROR;
  20598. }
  20599. }
  20600. printf(resultFmt, ret == 0 ? passed : failed);
  20601. /* Copy */
  20602. printf(testingFmt, "wc_ecc_copy_point()");
  20603. if (ret == 0) {
  20604. ret = wc_ecc_copy_point(point, cpypt);
  20605. }
  20606. /* Test bad args. */
  20607. if (ret == 0) {
  20608. ret = wc_ecc_copy_point(NULL, cpypt);
  20609. if (ret == ECC_BAD_ARG_E) {
  20610. ret = wc_ecc_copy_point(point, NULL);
  20611. }
  20612. if (ret == ECC_BAD_ARG_E) {
  20613. ret = 0;
  20614. } else if (ret == 0) {
  20615. ret = WOLFSSL_FATAL_ERROR;
  20616. }
  20617. }
  20618. printf(resultFmt, ret == 0 ? passed : failed);
  20619. printf(testingFmt, "wc_ecc_cmp_point()");
  20620. /* Compare point */
  20621. if (ret == 0) {
  20622. ret = wc_ecc_cmp_point(point, cpypt);
  20623. }
  20624. /* Test bad args. */
  20625. if (ret == 0) {
  20626. ret = wc_ecc_cmp_point(NULL, cpypt);
  20627. if (ret == BAD_FUNC_ARG) {
  20628. ret = wc_ecc_cmp_point(point, NULL);
  20629. }
  20630. if (ret == BAD_FUNC_ARG) {
  20631. ret = 0;
  20632. } else if (ret == 0) {
  20633. ret = WOLFSSL_FATAL_ERROR;
  20634. }
  20635. }
  20636. printf(resultFmt, ret == 0 ? passed : failed);
  20637. printf(testingFmt, "wc_ecc_point_is_at_infinity()");
  20638. /* At infinity if return == 1, otherwise return == 0. */
  20639. if (ret == 0) {
  20640. ret = wc_ecc_point_is_at_infinity(point);
  20641. }
  20642. /* Test bad args. */
  20643. if (ret == 0) {
  20644. ret = wc_ecc_point_is_at_infinity(NULL);
  20645. if (ret == BAD_FUNC_ARG) {
  20646. ret = 0;
  20647. } else if (ret == 0) {
  20648. ret = WOLFSSL_FATAL_ERROR;
  20649. }
  20650. }
  20651. printf(resultFmt, ret == 0 ? passed : failed);
  20652. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  20653. #ifdef USE_ECC_B_PARAM
  20654. printf(testingFmt, "wc_ecc_point_is_on_curve()");
  20655. /* On curve if ret == 0 */
  20656. if (ret == 0) {
  20657. ret = wc_ecc_point_is_on_curve(point, idx);
  20658. }
  20659. /* Test bad args. */
  20660. if (ret == 0) {
  20661. ret = wc_ecc_point_is_on_curve(NULL, idx);
  20662. if (ret == BAD_FUNC_ARG) {
  20663. ret = wc_ecc_point_is_on_curve(point, 1000);
  20664. }
  20665. if (ret == ECC_BAD_ARG_E) {
  20666. ret = 0;
  20667. } else if (ret == 0) {
  20668. ret = WOLFSSL_FATAL_ERROR;
  20669. }
  20670. }
  20671. printf(resultFmt, ret == 0 ? passed : failed);
  20672. #endif /* USE_ECC_B_PARAM */
  20673. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  20674. /* Free */
  20675. wc_ecc_del_point(point);
  20676. wc_ecc_del_point(cpypt);
  20677. wc_ecc_free(&key);
  20678. if (wc_FreeRng(&rng) && ret == 0) {
  20679. ret = WOLFSSL_FATAL_ERROR;
  20680. }
  20681. #endif
  20682. return ret;
  20683. } /* END test_wc_ecc_pointFns */
  20684. /*
  20685. * Testing wc_ecc_sahred_secret_ssh()
  20686. */
  20687. static int test_wc_ecc_shared_secret_ssh (void)
  20688. {
  20689. int ret = 0;
  20690. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  20691. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  20692. !defined(WOLFSSL_ATECC608A)
  20693. ecc_key key, key2;
  20694. WC_RNG rng;
  20695. int keySz = KEY32;
  20696. int key2Sz = KEY24;
  20697. byte secret[KEY32];
  20698. word32 secretLen = keySz;
  20699. /* Init stack variables. */
  20700. XMEMSET(secret, 0, secretLen);
  20701. XMEMSET(&rng, 0, sizeof(rng));
  20702. XMEMSET(&key, 0, sizeof(key));
  20703. XMEMSET(&key2, 0, sizeof(key2));
  20704. /* Make keys */
  20705. ret = wc_InitRng(&rng);
  20706. if (ret == 0) {
  20707. ret = wc_ecc_init(&key);
  20708. if (ret == 0) {
  20709. ret = wc_ecc_make_key(&rng, keySz, &key);
  20710. }
  20711. if (wc_FreeRng(&rng) && ret == 0) {
  20712. ret = WOLFSSL_FATAL_ERROR;
  20713. }
  20714. }
  20715. if (ret == 0) {
  20716. ret = wc_InitRng(&rng);
  20717. if (ret == 0) {
  20718. ret = wc_ecc_init(&key2);
  20719. }
  20720. if (ret == 0) {
  20721. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  20722. }
  20723. }
  20724. printf(testingFmt, "ecc_shared_secret_ssh()");
  20725. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20726. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20727. !defined(HAVE_SELFTEST)
  20728. if (ret == 0) {
  20729. ret = wc_ecc_set_rng(&key, &rng);
  20730. }
  20731. #endif
  20732. if (ret == 0) {
  20733. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  20734. }
  20735. /* Pass in bad args. */
  20736. if (ret == 0) {
  20737. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  20738. if (ret == BAD_FUNC_ARG) {
  20739. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  20740. }
  20741. if (ret == BAD_FUNC_ARG) {
  20742. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  20743. }
  20744. if (ret == BAD_FUNC_ARG) {
  20745. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  20746. }
  20747. if (ret == BAD_FUNC_ARG) {
  20748. key.type = ECC_PUBLICKEY;
  20749. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  20750. if (ret == ECC_BAD_ARG_E) {
  20751. ret = 0;
  20752. } else if (ret == 0) {
  20753. ret = WOLFSSL_FATAL_ERROR;
  20754. }
  20755. } else if (ret == 0) {
  20756. ret = WOLFSSL_FATAL_ERROR;
  20757. }
  20758. }
  20759. printf(resultFmt, ret == 0 ? passed : failed);
  20760. if (wc_FreeRng(&rng) && ret == 0) {
  20761. ret = WOLFSSL_FATAL_ERROR;
  20762. }
  20763. wc_ecc_free(&key);
  20764. wc_ecc_free(&key2);
  20765. #ifdef FP_ECC
  20766. wc_ecc_fp_free();
  20767. #endif
  20768. #endif
  20769. return ret;
  20770. } /* END test_wc_ecc_shared_secret_ssh */
  20771. /*
  20772. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  20773. */
  20774. static int test_wc_ecc_verify_hash_ex (void)
  20775. {
  20776. int ret = 0;
  20777. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  20778. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  20779. !defined(WOLFSSL_ATECC608A)
  20780. ecc_key key;
  20781. WC_RNG rng;
  20782. mp_int r;
  20783. mp_int s;
  20784. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  20785. unsigned char iHash[] = "Everyone gets Friday off.......";
  20786. unsigned char shortHash[] = TEST_STRING;
  20787. word32 hashlen = sizeof(hash);
  20788. word32 iHashLen = sizeof(iHash);
  20789. word32 shortHashLen = sizeof(shortHash);
  20790. int keySz = KEY32;
  20791. int sig = WOLFSSL_FATAL_ERROR;
  20792. int ver = WOLFSSL_FATAL_ERROR;
  20793. int verify_ok = 0;
  20794. /* Initialize r and s. */
  20795. ret = mp_init_multi(&r, &s, NULL, NULL, NULL, NULL);
  20796. if (ret != MP_OKAY) {
  20797. return MP_INIT_E;
  20798. }
  20799. ret = wc_InitRng(&rng);
  20800. if (ret == 0) {
  20801. ret = wc_ecc_init(&key);
  20802. if (ret == 0) {
  20803. ret = wc_ecc_make_key(&rng, keySz, &key);
  20804. }
  20805. }
  20806. if (ret == 0) {
  20807. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  20808. if (ret == 0) {
  20809. /* verify_ok should be 1. */
  20810. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  20811. if (verify_ok != 1 && ret == 0) {
  20812. ret = WOLFSSL_FATAL_ERROR;
  20813. }
  20814. }
  20815. if (ret == 0) {
  20816. /* verify_ok should be 0 */
  20817. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  20818. &verify_ok, &key);
  20819. if (verify_ok != 0 && ret == 0) {
  20820. ret = WOLFSSL_FATAL_ERROR;
  20821. }
  20822. }
  20823. if (ret == 0) {
  20824. /* verify_ok should be 0. */
  20825. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  20826. &verify_ok, &key);
  20827. if (verify_ok != 0 && ret == 0) {
  20828. ret = WOLFSSL_FATAL_ERROR;
  20829. }
  20830. }
  20831. }
  20832. printf(testingFmt, "wc_ecc_sign_hash_ex()");
  20833. /* Test bad args. */
  20834. if (ret == 0) {
  20835. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  20836. == ECC_BAD_ARG_E) {
  20837. sig = 0;
  20838. }
  20839. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  20840. != ECC_BAD_ARG_E) {
  20841. sig = WOLFSSL_FATAL_ERROR;
  20842. }
  20843. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  20844. != ECC_BAD_ARG_E) {
  20845. sig = WOLFSSL_FATAL_ERROR;
  20846. }
  20847. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  20848. != ECC_BAD_ARG_E) {
  20849. sig = WOLFSSL_FATAL_ERROR;
  20850. }
  20851. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  20852. != ECC_BAD_ARG_E) {
  20853. sig = WOLFSSL_FATAL_ERROR;
  20854. }
  20855. }
  20856. printf(resultFmt, sig == 0 ? passed : failed);
  20857. printf(testingFmt, "wc_ecc_verify_hash_ex()");
  20858. /* Test bad args. */
  20859. if (ret == 0) {
  20860. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  20861. == ECC_BAD_ARG_E) {
  20862. ver = 0;
  20863. }
  20864. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  20865. &verify_ok, &key) != ECC_BAD_ARG_E) {
  20866. ver = WOLFSSL_FATAL_ERROR;
  20867. }
  20868. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  20869. &key) != ECC_BAD_ARG_E) {
  20870. ver = WOLFSSL_FATAL_ERROR;
  20871. }
  20872. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  20873. NULL, &key) != ECC_BAD_ARG_E) {
  20874. ver = WOLFSSL_FATAL_ERROR;
  20875. }
  20876. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  20877. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  20878. ver = WOLFSSL_FATAL_ERROR;
  20879. }
  20880. }
  20881. printf(resultFmt, ver == 0 ? passed : failed);
  20882. wc_ecc_free(&key);
  20883. mp_free(&r);
  20884. mp_free(&s);
  20885. if (wc_FreeRng(&rng)) {
  20886. return WOLFSSL_FATAL_ERROR;
  20887. }
  20888. if (ret == 0 && (sig != 0 || ver != 0)) {
  20889. ret = WOLFSSL_FATAL_ERROR;
  20890. }
  20891. #endif
  20892. return ret;
  20893. } /* END test_wc_ecc_verify_hash_ex */
  20894. /*
  20895. * Testing wc_ecc_mulmod()
  20896. */
  20897. static int test_wc_ecc_mulmod (void)
  20898. {
  20899. int ret = 0;
  20900. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  20901. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  20902. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  20903. ecc_key key1, key2, key3;
  20904. WC_RNG rng;
  20905. ret = wc_InitRng(&rng);
  20906. if (ret == 0) {
  20907. ret = wc_ecc_init(&key1);
  20908. if (ret == 0) {
  20909. ret = wc_ecc_init(&key2);
  20910. }
  20911. if (ret == 0) {
  20912. ret = wc_ecc_init(&key3);
  20913. }
  20914. if (ret == 0) {
  20915. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  20916. }
  20917. wc_FreeRng(&rng);
  20918. }
  20919. if (ret == 0) {
  20920. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  20921. ECC_SECP256R1);
  20922. if (ret == 0) {
  20923. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  20924. key1.dp->prime, ECC_SECP256R1);
  20925. }
  20926. }
  20927. printf(testingFmt, "wc_ecc_mulmod()");
  20928. if (ret == 0) {
  20929. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  20930. &key3.k, 1);
  20931. }
  20932. /* Test bad args. */
  20933. if (ret == 0) {
  20934. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  20935. &key3.k, 1);
  20936. if (ret == ECC_BAD_ARG_E) {
  20937. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  20938. &key3.k, 1);
  20939. }
  20940. if (ret == ECC_BAD_ARG_E) {
  20941. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  20942. &key3.k, 1);
  20943. }
  20944. if (ret == ECC_BAD_ARG_E) {
  20945. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  20946. &key2.k, NULL, 1);
  20947. }
  20948. if (ret == ECC_BAD_ARG_E) {
  20949. ret = 0;
  20950. } else if (ret == 0) {
  20951. ret = WOLFSSL_FATAL_ERROR;
  20952. }
  20953. }
  20954. printf(resultFmt, ret == 0 ? passed : failed);
  20955. wc_ecc_free(&key1);
  20956. wc_ecc_free(&key2);
  20957. wc_ecc_free(&key3);
  20958. #ifdef FP_ECC
  20959. wc_ecc_fp_free();
  20960. #endif
  20961. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  20962. return ret;
  20963. } /* END test_wc_ecc_mulmod */
  20964. /*
  20965. * Testing wc_ecc_is_valid_idx()
  20966. */
  20967. static int test_wc_ecc_is_valid_idx (void)
  20968. {
  20969. int ret = 0;
  20970. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20971. ecc_key key;
  20972. WC_RNG rng;
  20973. int iVal = -2;
  20974. int iVal2 = 3000;
  20975. XMEMSET(&rng, 0, sizeof(rng));
  20976. XMEMSET(&key, 0, sizeof(key));
  20977. ret = wc_InitRng(&rng);
  20978. if (ret == 0) {
  20979. ret = wc_ecc_init(&key);
  20980. if (ret == 0) {
  20981. ret = wc_ecc_make_key(&rng, 32, &key);
  20982. }
  20983. }
  20984. printf(testingFmt, "wc_ecc_is_valid_idx()");
  20985. if (ret == 0) {
  20986. ret = wc_ecc_is_valid_idx(key.idx);
  20987. if (ret == 1) {
  20988. ret = 0;
  20989. } else {
  20990. ret = WOLFSSL_FATAL_ERROR;
  20991. }
  20992. }
  20993. /* Test bad args. */
  20994. if (ret == 0) {
  20995. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  20996. if (ret == 0) {
  20997. ret = wc_ecc_is_valid_idx(iVal2);
  20998. }
  20999. if (ret != 0) {
  21000. ret = WOLFSSL_FATAL_ERROR;
  21001. }
  21002. }
  21003. printf(resultFmt, ret == 0 ? passed : failed);
  21004. if (wc_FreeRng(&rng) && ret == 0) {
  21005. ret = WOLFSSL_FATAL_ERROR;
  21006. }
  21007. wc_ecc_free(&key);
  21008. #ifdef FP_ECC
  21009. wc_ecc_fp_free();
  21010. #endif
  21011. #endif
  21012. return ret;
  21013. } /* END test_wc_ecc_is_valid_idx */
  21014. /*
  21015. * Testing wc_ecc_get_curve_id_from_oid()
  21016. */
  21017. static int test_wc_ecc_get_curve_id_from_oid (void)
  21018. {
  21019. int ret = 0;
  21020. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21021. !defined(HAVE_FIPS)
  21022. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  21023. word32 len = sizeof(oid);
  21024. printf(testingFmt, "wc_ecc_get_curve_id_from_oid()");
  21025. /* Bad Cases */
  21026. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  21027. if (ret == BAD_FUNC_ARG) {
  21028. ret = 0;
  21029. }
  21030. if (ret == 0) {
  21031. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  21032. if (ret == ECC_CURVE_INVALID) {
  21033. ret = 0;
  21034. }
  21035. }
  21036. /* Good Case */
  21037. if (ret == 0) {
  21038. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  21039. if (ret == 7) {
  21040. ret = 0;
  21041. }
  21042. }
  21043. printf(resultFmt, ret == 0 ? passed : failed);
  21044. #endif
  21045. return ret;
  21046. }/* END test_wc_ecc_get_curve_id_from_oid */
  21047. /*
  21048. * Testing wc_ecc_sig_size_calc()
  21049. */
  21050. static int test_wc_ecc_sig_size_calc (void)
  21051. {
  21052. int ret = 0;
  21053. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  21054. ecc_key key;
  21055. WC_RNG rng;
  21056. int sz = 0;
  21057. printf(testingFmt, "wc_ecc_sig_size_calc()");
  21058. ret = wc_InitRng(&rng);
  21059. if (ret == 0) {
  21060. ret = wc_ecc_init(&key);
  21061. if (ret == 0) {
  21062. ret = wc_ecc_make_key(&rng, 16, &key);
  21063. }
  21064. sz = key.dp->size;
  21065. }
  21066. if (ret == 0) {
  21067. ret = wc_ecc_sig_size_calc(sz);
  21068. if (ret > 0) {
  21069. ret = 0;
  21070. }
  21071. }
  21072. printf(resultFmt, ret == 0 ? passed : failed);
  21073. wc_ecc_free(&key);
  21074. wc_FreeRng(&rng);
  21075. #endif
  21076. return ret;
  21077. } /* END test_wc_ecc_sig_size_calc */
  21078. /*
  21079. * Testing ToTraditional
  21080. */
  21081. static int test_ToTraditional (void)
  21082. {
  21083. int ret = 0;
  21084. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  21085. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  21086. defined(OPENSSL_EXTRA_X509_SMALL))
  21087. XFILE f;
  21088. byte input[TWOK_BUF];
  21089. word32 sz;
  21090. printf(testingFmt, "ToTraditional()");
  21091. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  21092. AssertTrue((f != XBADFILE));
  21093. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  21094. XFCLOSE(f);
  21095. /* Good case */
  21096. ret = ToTraditional(input, sz);
  21097. if (ret > 0) {
  21098. ret = 0;
  21099. }
  21100. /* Bad cases */
  21101. if (ret == 0) {
  21102. ret = ToTraditional(NULL, 0);
  21103. if (ret == BAD_FUNC_ARG) {
  21104. ret = 0;
  21105. }
  21106. }
  21107. if (ret == 0) {
  21108. ret = ToTraditional(NULL, sz);
  21109. if (ret == BAD_FUNC_ARG) {
  21110. ret = 0;
  21111. }
  21112. }
  21113. if (ret == 0) {
  21114. ret = ToTraditional(input, 0);
  21115. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  21116. ret = 0;
  21117. }
  21118. }
  21119. printf(resultFmt, ret == 0 ? passed : failed);
  21120. #endif
  21121. return ret;
  21122. }/* End test_ToTraditional*/
  21123. /*
  21124. * Testing wc_EccPrivateKeyToDer
  21125. */
  21126. static int test_wc_EccPrivateKeyToDer (void)
  21127. {
  21128. int ret = 0;
  21129. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21130. byte output[ONEK_BUF];
  21131. ecc_key eccKey;
  21132. WC_RNG rng;
  21133. word32 inLen;
  21134. printf(testingFmt, "wc_EccPrivateKeyToDer()");
  21135. ret = wc_InitRng(&rng);
  21136. if (ret == 0) {
  21137. ret = wc_ecc_init(&eccKey);
  21138. if (ret == 0) {
  21139. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  21140. }
  21141. inLen = (word32)sizeof(output);
  21142. /* Bad Cases */
  21143. if (ret == 0) {
  21144. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  21145. if (ret == BAD_FUNC_ARG) {
  21146. ret = 0;
  21147. }
  21148. }
  21149. if (ret == 0) {
  21150. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  21151. if (ret == BAD_FUNC_ARG) {
  21152. ret = 0;
  21153. }
  21154. }
  21155. if (ret == 0) {
  21156. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  21157. if (ret == LENGTH_ONLY_E) {
  21158. ret = 0;
  21159. }
  21160. }
  21161. if (ret == 0) {
  21162. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  21163. if (ret == BAD_FUNC_ARG) {
  21164. ret = 0;
  21165. }
  21166. }
  21167. /*Good Case */
  21168. if (ret == 0) {
  21169. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  21170. if (ret > 0) {
  21171. ret = 0;
  21172. }
  21173. }
  21174. wc_ecc_free(&eccKey);
  21175. }
  21176. wc_FreeRng(&rng);
  21177. printf(resultFmt, ret == 0 ? passed : failed);
  21178. #endif
  21179. return ret;
  21180. }/* End test_wc_EccPrivateKeyToDer*/
  21181. /*
  21182. * Testing wc_DhPublicKeyDecode
  21183. */
  21184. static int test_wc_DhPublicKeyDecode(void)
  21185. {
  21186. int ret = 0;
  21187. word32 inOutIdx;
  21188. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  21189. DhKey key;
  21190. AssertIntEQ(wc_InitDhKey(&key), 0);
  21191. printf(testingFmt, "wc_DhPublicKeyDecode()");
  21192. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  21193. BAD_FUNC_ARG);
  21194. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  21195. BAD_FUNC_ARG);
  21196. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  21197. BAD_FUNC_ARG);
  21198. inOutIdx = 0;
  21199. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  21200. BAD_FUNC_ARG);
  21201. inOutIdx = 0;
  21202. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  21203. BAD_FUNC_ARG);
  21204. inOutIdx = 0;
  21205. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  21206. sizeof_dh_pub_key_der_2048), 0);
  21207. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  21208. key.pub.used != 0 && key.priv.used == 0);
  21209. wc_FreeDhKey(&key);
  21210. printf(resultFmt, passed);
  21211. #endif
  21212. (void)inOutIdx;
  21213. return ret;
  21214. }
  21215. /*
  21216. * Testing wc_Ed25519KeyToDer
  21217. */
  21218. static int test_wc_Ed25519KeyToDer (void)
  21219. {
  21220. int ret = 0;
  21221. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  21222. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21223. byte output[ONEK_BUF];
  21224. ed25519_key ed25519Key;
  21225. WC_RNG rng;
  21226. word32 inLen;
  21227. printf(testingFmt, "wc_Ed25519KeyToDer()");
  21228. ret = wc_InitRng(&rng);
  21229. if (ret == 0) {
  21230. ret = wc_ed25519_init(&ed25519Key);
  21231. if (ret == 0) {
  21232. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  21233. }
  21234. inLen = (word32)sizeof(output);
  21235. /* Bad Cases */
  21236. if (ret == 0) {
  21237. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  21238. if (ret == BAD_FUNC_ARG) {
  21239. ret = 0;
  21240. }
  21241. }
  21242. if (ret == 0) {
  21243. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  21244. if (ret == BAD_FUNC_ARG) {
  21245. ret = 0;
  21246. }
  21247. }
  21248. if (ret == 0) {
  21249. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  21250. if (ret == BAD_FUNC_ARG) {
  21251. ret = 0;
  21252. }
  21253. }
  21254. if (ret == 0) {
  21255. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  21256. if (ret == BAD_FUNC_ARG) {
  21257. ret = 0;
  21258. }
  21259. }
  21260. /* Good Case */
  21261. if (ret == 0) {
  21262. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  21263. if (ret > 0) {
  21264. ret = 0;
  21265. }
  21266. }
  21267. wc_ed25519_free(&ed25519Key);
  21268. }
  21269. wc_FreeRng(&rng);
  21270. printf(resultFmt, ret == 0 ? passed : failed);
  21271. #endif
  21272. return ret;
  21273. }/* End test_wc_Ed25519KeyToDer*/
  21274. /*
  21275. * Testing wc_Ed25519PrivateKeyToDer
  21276. */
  21277. static int test_wc_Ed25519PrivateKeyToDer (void)
  21278. {
  21279. int ret = 0;
  21280. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  21281. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21282. byte output[ONEK_BUF];
  21283. ed25519_key ed25519PrivKey;
  21284. WC_RNG rng;
  21285. word32 inLen;
  21286. printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
  21287. ret = wc_InitRng(&rng);
  21288. if (ret == 0) {
  21289. ret = wc_ed25519_init(&ed25519PrivKey);
  21290. if (ret == 0) {
  21291. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  21292. }
  21293. inLen = (word32)sizeof(output);
  21294. /* Bad Cases */
  21295. if (ret == 0) {
  21296. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  21297. if (ret == BAD_FUNC_ARG) {
  21298. ret = 0;
  21299. }
  21300. }
  21301. if (ret == 0) {
  21302. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  21303. if (ret == BAD_FUNC_ARG) {
  21304. ret = 0;
  21305. }
  21306. }
  21307. if (ret == 0) {
  21308. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  21309. if (ret == BAD_FUNC_ARG) {
  21310. ret = 0;
  21311. }
  21312. }
  21313. if (ret == 0) {
  21314. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  21315. if (ret == BAD_FUNC_ARG) {
  21316. ret = 0;
  21317. }
  21318. }
  21319. /* Good Case */
  21320. if (ret == 0) {
  21321. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  21322. if (ret > 0) {
  21323. ret = 0;
  21324. }
  21325. }
  21326. wc_ed25519_free(&ed25519PrivKey);
  21327. }
  21328. wc_FreeRng(&rng);
  21329. printf(resultFmt, ret == 0 ? passed : failed);
  21330. #endif
  21331. return ret;
  21332. }/* End test_wc_Ed25519PrivateKeyToDer*/
  21333. /*
  21334. * Testing wc_Ed448KeyToDer
  21335. */
  21336. static int test_wc_Ed448KeyToDer (void)
  21337. {
  21338. int ret = 0;
  21339. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21340. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21341. byte output[ONEK_BUF];
  21342. ed448_key ed448Key;
  21343. WC_RNG rng;
  21344. word32 inLen;
  21345. printf(testingFmt, "wc_Ed448KeyToDer()");
  21346. ret = wc_InitRng(&rng);
  21347. if (ret == 0) {
  21348. ret = wc_ed448_init(&ed448Key);
  21349. if (ret == 0) {
  21350. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  21351. }
  21352. inLen = sizeof(output);
  21353. /* Bad Cases */
  21354. if (ret == 0) {
  21355. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  21356. if (ret == BAD_FUNC_ARG) {
  21357. ret = 0;
  21358. }
  21359. }
  21360. if (ret == 0) {
  21361. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  21362. if (ret == BAD_FUNC_ARG) {
  21363. ret = 0;
  21364. }
  21365. }
  21366. if (ret == 0) {
  21367. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  21368. if (ret == BAD_FUNC_ARG) {
  21369. ret = 0;
  21370. }
  21371. }
  21372. if (ret == 0) {
  21373. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  21374. if (ret == BAD_FUNC_ARG) {
  21375. ret = 0;
  21376. }
  21377. }
  21378. /* Good Case */
  21379. if (ret == 0) {
  21380. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  21381. if (ret > 0) {
  21382. ret = 0;
  21383. }
  21384. }
  21385. wc_ed448_free(&ed448Key);
  21386. }
  21387. wc_FreeRng(&rng);
  21388. printf(resultFmt, ret == 0 ? passed : failed);
  21389. #endif
  21390. return ret;
  21391. }/* End test_wc_Ed448KeyToDer*/
  21392. /*
  21393. * Testing wc_Ed448PrivateKeyToDer
  21394. */
  21395. static int test_wc_Ed448PrivateKeyToDer (void)
  21396. {
  21397. int ret = 0;
  21398. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21399. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21400. byte output[ONEK_BUF];
  21401. ed448_key ed448PrivKey;
  21402. WC_RNG rng;
  21403. word32 inLen;
  21404. printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
  21405. ret = wc_InitRng(&rng);
  21406. if (ret == 0) {
  21407. ret = wc_ed448_init(&ed448PrivKey);
  21408. if (ret == 0) {
  21409. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  21410. }
  21411. inLen = sizeof(output);
  21412. /* Bad Cases */
  21413. if (ret == 0) {
  21414. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  21415. if (ret == BAD_FUNC_ARG) {
  21416. ret = 0;
  21417. }
  21418. }
  21419. if (ret == 0) {
  21420. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  21421. if (ret == BAD_FUNC_ARG) {
  21422. ret = 0;
  21423. }
  21424. }
  21425. if (ret == 0) {
  21426. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  21427. if (ret == BAD_FUNC_ARG) {
  21428. ret = 0;
  21429. }
  21430. }
  21431. if (ret == 0) {
  21432. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  21433. if (ret == BAD_FUNC_ARG) {
  21434. ret = 0;
  21435. }
  21436. }
  21437. /* Good case */
  21438. if (ret == 0) {
  21439. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  21440. if (ret > 0) {
  21441. ret = 0;
  21442. }
  21443. }
  21444. wc_ed448_free(&ed448PrivKey);
  21445. }
  21446. wc_FreeRng(&rng);
  21447. printf(resultFmt, ret == 0 ? passed : failed);
  21448. #endif
  21449. return ret;
  21450. }/* End test_wc_Ed448PrivateKeyToDer*/
  21451. /*
  21452. * Testing wc_SetSubjectBuffer
  21453. */
  21454. static int test_wc_SetSubjectBuffer (void)
  21455. {
  21456. int ret = 0;
  21457. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  21458. Cert cert;
  21459. FILE* file;
  21460. byte* der;
  21461. word32 derSz;
  21462. printf(testingFmt, "wc_SetSubjectBuffer()");
  21463. derSz = FOURK_BUF;
  21464. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21465. if (der == NULL) {
  21466. ret = -1;
  21467. }
  21468. if (ret == 0) {
  21469. file = XFOPEN("./certs/ca-cert.der", "rb");
  21470. if (file != NULL) {
  21471. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  21472. XFCLOSE(file);
  21473. }
  21474. else {
  21475. ret = -1;
  21476. }
  21477. }
  21478. if (ret == 0) {
  21479. ret = wc_InitCert(&cert);
  21480. }
  21481. if (ret == 0) {
  21482. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  21483. }
  21484. if (ret == 0) {
  21485. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  21486. if (ret == BAD_FUNC_ARG) {
  21487. ret = 0;
  21488. }
  21489. }
  21490. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21491. printf(resultFmt, ret == 0 ? passed : failed);
  21492. #endif
  21493. return ret;
  21494. }/* End test_wc_SetSubjectBuffer*/
  21495. /*
  21496. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  21497. */
  21498. static int test_wc_SetSubjectKeyIdFromPublicKey_ex (void)
  21499. {
  21500. int ret = 0;
  21501. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  21502. WC_RNG rng;
  21503. Cert cert;
  21504. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  21505. ed25519_key ed25519Key;
  21506. #endif
  21507. #if !defined(NO_RSA) && defined(HAVE_RSA)
  21508. RsaKey rsaKey;
  21509. int bits = 2048;
  21510. #endif
  21511. #if defined(HAVE_ECC)
  21512. ecc_key eccKey;
  21513. #endif
  21514. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21515. ed448_key ed448Key;
  21516. #endif
  21517. printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
  21518. #ifndef HAVE_FIPS
  21519. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21520. #else
  21521. ret = wc_InitRng(&rng);
  21522. #endif
  21523. wc_InitCert(&cert);
  21524. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  21525. if (ret == 0) { /*ED25519*/
  21526. ret = wc_ed25519_init(&ed25519Key);
  21527. if (ret == 0) {
  21528. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  21529. }
  21530. if (ret == 0) {
  21531. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  21532. &ed25519Key);
  21533. }
  21534. wc_ed25519_free(&ed25519Key);
  21535. }
  21536. #endif
  21537. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  21538. if (ret == 0) { /*RSA*/
  21539. ret = wc_InitRsaKey(&rsaKey, NULL);
  21540. if (ret == 0) {
  21541. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  21542. }
  21543. if (ret == 0) {
  21544. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  21545. }
  21546. wc_FreeRsaKey(&rsaKey);
  21547. }
  21548. #endif
  21549. #if defined(HAVE_ECC)
  21550. if (ret == 0) { /*ECC*/
  21551. ret = wc_ecc_init(&eccKey);
  21552. if (ret == 0) {
  21553. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  21554. }
  21555. if (ret == 0) {
  21556. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  21557. }
  21558. wc_ecc_free(&eccKey);
  21559. }
  21560. #endif
  21561. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21562. if (ret == 0) { /*ED448*/
  21563. ret = wc_ed448_init(&ed448Key);
  21564. if (ret == 0) {
  21565. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  21566. }
  21567. if (ret == 0) {
  21568. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  21569. &ed448Key);
  21570. }
  21571. wc_ed448_free(&ed448Key);
  21572. }
  21573. #endif
  21574. printf(resultFmt, ret == 0 ? passed : failed);
  21575. wc_FreeRng(&rng);
  21576. #endif
  21577. return ret;
  21578. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  21579. /*
  21580. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  21581. */
  21582. static int test_wc_SetAuthKeyIdFromPublicKey_ex (void)
  21583. {
  21584. int ret = 0;
  21585. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  21586. WC_RNG rng;
  21587. Cert cert;
  21588. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  21589. ed25519_key ed25519Key;
  21590. #endif
  21591. #if !defined(NO_RSA) && defined(HAVE_RSA)
  21592. RsaKey rsaKey;
  21593. int bits = 2048;
  21594. #endif
  21595. #if defined(HAVE_ECC)
  21596. ecc_key eccKey;
  21597. #endif
  21598. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21599. ed448_key ed448Key;
  21600. #endif
  21601. printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
  21602. #ifndef HAVE_FIPS
  21603. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  21604. #else
  21605. ret = wc_InitRng(&rng);
  21606. #endif
  21607. wc_InitCert(&cert);
  21608. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  21609. if (ret == 0) { /*ED25519*/
  21610. ret = wc_ed25519_init(&ed25519Key);
  21611. if (ret == 0) {
  21612. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  21613. }
  21614. if (ret == 0) {
  21615. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  21616. &ed25519Key);
  21617. }
  21618. wc_ed25519_free(&ed25519Key);
  21619. }
  21620. #endif
  21621. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  21622. if (ret == 0) { /*RSA*/
  21623. ret = wc_InitRsaKey(&rsaKey, NULL);
  21624. if (ret == 0) {
  21625. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  21626. }
  21627. if (ret == 0) {
  21628. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  21629. }
  21630. wc_FreeRsaKey(&rsaKey);
  21631. }
  21632. #endif
  21633. #if defined(HAVE_ECC)
  21634. if (ret == 0) { /*ECC*/
  21635. ret = wc_ecc_init(&eccKey);
  21636. if (ret == 0) {
  21637. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  21638. }
  21639. if (ret == 0) {
  21640. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  21641. }
  21642. wc_ecc_free(&eccKey);
  21643. }
  21644. #endif
  21645. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21646. if (ret == 0) { /*ED448*/
  21647. ret = wc_ed448_init(&ed448Key);
  21648. if (ret == 0) {
  21649. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  21650. }
  21651. if (ret == 0) {
  21652. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  21653. &ed448Key);
  21654. }
  21655. wc_ed448_free(&ed448Key);
  21656. }
  21657. #endif
  21658. printf(resultFmt, ret == 0 ? passed : failed);
  21659. wc_FreeRng(&rng);
  21660. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  21661. return ret;
  21662. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  21663. /*
  21664. * Testing wc_PKCS7_New()
  21665. */
  21666. static void test_wc_PKCS7_New (void)
  21667. {
  21668. #if defined(HAVE_PKCS7)
  21669. PKCS7* pkcs7;
  21670. void* heap = NULL;
  21671. printf(testingFmt, "wc_PKCS7_New()");
  21672. pkcs7 = wc_PKCS7_New(heap, devId);
  21673. AssertNotNull(pkcs7);
  21674. printf(resultFmt, passed);
  21675. wc_PKCS7_Free(pkcs7);
  21676. #endif
  21677. } /* END test-wc_PKCS7_New */
  21678. /*
  21679. * Testing wc_PKCS7_Init()
  21680. */
  21681. static void test_wc_PKCS7_Init (void)
  21682. {
  21683. #if defined(HAVE_PKCS7)
  21684. PKCS7* pkcs7;
  21685. void* heap = NULL;
  21686. printf(testingFmt, "wc_PKCS7_Init()");
  21687. pkcs7 = wc_PKCS7_New(heap, devId);
  21688. AssertNotNull(pkcs7);
  21689. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  21690. /* Pass in bad args. */
  21691. AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
  21692. printf(resultFmt, passed);
  21693. wc_PKCS7_Free(pkcs7);
  21694. #endif
  21695. } /* END test-wc_PKCS7_Init */
  21696. /*
  21697. * Testing wc_PKCS7_InitWithCert()
  21698. */
  21699. static void test_wc_PKCS7_InitWithCert (void)
  21700. {
  21701. #if defined(HAVE_PKCS7)
  21702. PKCS7* pkcs7;
  21703. #ifndef NO_RSA
  21704. #if defined(USE_CERT_BUFFERS_2048)
  21705. unsigned char cert[sizeof(client_cert_der_2048)];
  21706. int certSz = (int)sizeof(cert);
  21707. XMEMSET(cert, 0, certSz);
  21708. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  21709. #elif defined(USE_CERT_BUFFERS_1024)
  21710. unsigned char cert[sizeof(client_cert_der_1024)];
  21711. int certSz = (int)sizeof(cert);
  21712. XMEMSET(cert, 0, certSz);
  21713. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  21714. #else
  21715. unsigned char cert[ONEK_BUF];
  21716. XFILE fp;
  21717. int certSz;
  21718. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  21719. AssertTrue(fp != XBADFILE);
  21720. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  21721. XFCLOSE(fp);
  21722. #endif
  21723. #elif defined(HAVE_ECC)
  21724. #if defined(USE_CERT_BUFFERS_256)
  21725. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21726. int certSz = (int)sizeof(cert);
  21727. XMEMSET(cert, 0, certSz);
  21728. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  21729. #else
  21730. unsigned char cert[ONEK_BUF];
  21731. XFILE fp;
  21732. int certSz;
  21733. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  21734. AssertTrue(fp != XBADFILE);
  21735. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  21736. XFCLOSE(fp);
  21737. #endif
  21738. #else
  21739. #error PKCS7 requires ECC or RSA
  21740. #endif
  21741. #ifdef HAVE_ECC
  21742. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  21743. unsigned char certWithInvalidEccKey[] = {
  21744. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  21745. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  21746. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  21747. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  21748. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  21749. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  21750. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  21751. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  21752. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  21753. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  21754. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  21755. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  21756. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  21757. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  21758. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  21759. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  21760. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  21761. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  21762. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  21763. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  21764. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  21765. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  21766. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  21767. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  21768. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  21769. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  21770. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  21771. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  21772. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  21773. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  21774. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  21775. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  21776. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  21777. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  21778. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  21779. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  21780. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  21781. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  21782. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  21783. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  21784. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  21785. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  21786. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  21787. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  21788. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  21789. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  21790. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  21791. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  21792. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  21793. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  21794. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  21795. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  21796. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  21797. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  21798. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  21799. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  21800. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  21801. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  21802. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  21803. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  21804. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  21805. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  21806. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  21807. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  21808. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  21809. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  21810. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  21811. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  21812. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  21813. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  21814. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  21815. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  21816. 0x08, 0xA8, 0xB4};
  21817. #endif
  21818. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  21819. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  21820. /* If initialization is not successful, it's free'd in init func. */
  21821. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  21822. wc_PKCS7_Free(pkcs7);
  21823. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  21824. /* Valid initialization usage. */
  21825. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  21826. /* Pass in bad args. No need free for null checks, free at end.*/
  21827. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  21828. BAD_FUNC_ARG);
  21829. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  21830. BAD_FUNC_ARG);
  21831. #ifdef HAVE_ECC
  21832. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  21833. sizeof(certWithInvalidEccKey)), 0);
  21834. #endif
  21835. printf(resultFmt, passed);
  21836. wc_PKCS7_Free(pkcs7);
  21837. #endif
  21838. } /* END test_wc_PKCS7_InitWithCert */
  21839. /*
  21840. * Testing wc_PKCS7_EncodeData()
  21841. */
  21842. static void test_wc_PKCS7_EncodeData (void)
  21843. {
  21844. #if defined(HAVE_PKCS7)
  21845. PKCS7* pkcs7;
  21846. byte output[FOURK_BUF];
  21847. byte data[] = "My encoded DER cert.";
  21848. #ifndef NO_RSA
  21849. #if defined(USE_CERT_BUFFERS_2048)
  21850. unsigned char cert[sizeof(client_cert_der_2048)];
  21851. unsigned char key[sizeof(client_key_der_2048)];
  21852. int certSz = (int)sizeof(cert);
  21853. int keySz = (int)sizeof(key);
  21854. XMEMSET(cert, 0, certSz);
  21855. XMEMSET(key, 0, keySz);
  21856. XMEMCPY(cert, client_cert_der_2048, certSz);
  21857. XMEMCPY(key, client_key_der_2048, keySz);
  21858. #elif defined(USE_CERT_BUFFERS_1024)
  21859. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  21860. unsigned char key[sizeof_client_key_der_1024];
  21861. int certSz = (int)sizeof(cert);
  21862. int keySz = (int)sizeof(key);
  21863. XMEMSET(cert, 0, certSz);
  21864. XMEMSET(key, 0, keySz);
  21865. XMEMCPY(cert, client_cert_der_1024, certSz);
  21866. XMEMCPY(key, client_key_der_1024, keySz);
  21867. #else
  21868. unsigned char cert[ONEK_BUF];
  21869. unsigned char key[ONEK_BUF];
  21870. XFILE fp;
  21871. int certSz;
  21872. int keySz;
  21873. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  21874. AssertTrue(fp != XBADFILE);
  21875. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  21876. XFCLOSE(fp);
  21877. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  21878. AssertTrue(fp != XBADFILE);
  21879. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  21880. XFCLOSE(fp);
  21881. #endif
  21882. #elif defined(HAVE_ECC)
  21883. #if defined(USE_CERT_BUFFERS_256)
  21884. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21885. unsigned char key[sizeof(ecc_clikey_der_256)];
  21886. int certSz = (int)sizeof(cert);
  21887. int keySz = (int)sizeof(key);
  21888. XMEMSET(cert, 0, certSz);
  21889. XMEMSET(key, 0, keySz);
  21890. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  21891. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  21892. #else
  21893. unsigned char cert[ONEK_BUF];
  21894. unsigned char key[ONEK_BUF];
  21895. XFILE fp;
  21896. int certSz, keySz;
  21897. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  21898. AssertTrue(fp != XBADFILE);
  21899. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  21900. XFCLOSE(fp);
  21901. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  21902. AssertTrue(fp != XBADFILE);
  21903. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  21904. XFCLOSE(fp);
  21905. #endif
  21906. #endif
  21907. XMEMSET(output, 0, sizeof(output));
  21908. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  21909. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  21910. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  21911. printf(testingFmt, "wc_PKCS7_EncodeData()");
  21912. pkcs7->content = data;
  21913. pkcs7->contentSz = sizeof(data);
  21914. pkcs7->privateKey = key;
  21915. pkcs7->privateKeySz = keySz;
  21916. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  21917. /* Test bad args. */
  21918. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  21919. BAD_FUNC_ARG);
  21920. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  21921. BAD_FUNC_ARG);
  21922. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  21923. printf(resultFmt, passed);
  21924. wc_PKCS7_Free(pkcs7);
  21925. #endif
  21926. } /* END test_wc_PKCS7_EncodeData */
  21927. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  21928. !defined(NO_RSA) && !defined(NO_SHA256)
  21929. /* RSA sign raw digest callback */
  21930. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  21931. byte* out, word32 outSz, byte* privateKey,
  21932. word32 privateKeySz, int devid, int hashOID)
  21933. {
  21934. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  21935. byte digInfoEncoding[] = {
  21936. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  21937. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  21938. 0x00, 0x04, 0x20
  21939. };
  21940. int ret;
  21941. byte digestInfo[ONEK_BUF];
  21942. byte sig[FOURK_BUF];
  21943. word32 digestInfoSz = 0;
  21944. word32 idx = 0;
  21945. RsaKey rsa;
  21946. /* SHA-256 required only for this example callback due to above
  21947. * digInfoEncoding[] */
  21948. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  21949. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  21950. (hashOID != SHA256h)) {
  21951. return -1;
  21952. }
  21953. /* build DigestInfo */
  21954. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  21955. digestInfoSz += sizeof(digInfoEncoding);
  21956. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  21957. digestInfoSz += digestSz;
  21958. /* set up RSA key */
  21959. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  21960. if (ret != 0) {
  21961. return ret;
  21962. }
  21963. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  21964. /* sign DigestInfo */
  21965. if (ret == 0) {
  21966. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  21967. &rsa, pkcs7->rng);
  21968. if (ret > 0) {
  21969. if (ret > (int)outSz) {
  21970. /* output buffer too small */
  21971. ret = -1;
  21972. } else {
  21973. /* success, ret holds sig size */
  21974. XMEMCPY(out, sig, ret);
  21975. }
  21976. }
  21977. }
  21978. wc_FreeRsaKey(&rsa);
  21979. return ret;
  21980. }
  21981. #endif
  21982. /*
  21983. * Testing wc_PKCS7_EncodeSignedData()
  21984. */
  21985. static void test_wc_PKCS7_EncodeSignedData(void)
  21986. {
  21987. #if defined(HAVE_PKCS7)
  21988. PKCS7* pkcs7;
  21989. WC_RNG rng;
  21990. byte output[FOURK_BUF];
  21991. byte badOut[1];
  21992. word32 outputSz = (word32)sizeof(output);
  21993. word32 badOutSz = 0;
  21994. byte data[] = "Test data to encode.";
  21995. #ifndef NO_RSA
  21996. #if defined(USE_CERT_BUFFERS_2048)
  21997. byte key[sizeof(client_key_der_2048)];
  21998. byte cert[sizeof(client_cert_der_2048)];
  21999. word32 keySz = (word32)sizeof(key);
  22000. word32 certSz = (word32)sizeof(cert);
  22001. XMEMSET(key, 0, keySz);
  22002. XMEMSET(cert, 0, certSz);
  22003. XMEMCPY(key, client_key_der_2048, keySz);
  22004. XMEMCPY(cert, client_cert_der_2048, certSz);
  22005. #elif defined(USE_CERT_BUFFERS_1024)
  22006. byte key[sizeof_client_key_der_1024];
  22007. byte cert[sizeof(sizeof_client_cert_der_1024)];
  22008. word32 keySz = (word32)sizeof(key);
  22009. word32 certSz = (word32)sizeof(cert);
  22010. XMEMSET(key, 0, keySz);
  22011. XMEMSET(cert, 0, certSz);
  22012. XMEMCPY(key, client_key_der_1024, keySz);
  22013. XMEMCPY(cert, client_cert_der_1024, certSz);
  22014. #else
  22015. unsigned char cert[ONEK_BUF];
  22016. unsigned char key[ONEK_BUF];
  22017. XFILE fp;
  22018. int certSz;
  22019. int keySz;
  22020. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  22021. AssertTrue(fp != XBADFILE);
  22022. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  22023. XFCLOSE(fp);
  22024. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  22025. AssertTrue(fp != XBADFILE);
  22026. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  22027. XFCLOSE(fp);
  22028. #endif
  22029. #elif defined(HAVE_ECC)
  22030. #if defined(USE_CERT_BUFFERS_256)
  22031. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22032. unsigned char key[sizeof(ecc_clikey_der_256)];
  22033. int certSz = (int)sizeof(cert);
  22034. int keySz = (int)sizeof(key);
  22035. XMEMSET(cert, 0, certSz);
  22036. XMEMSET(key, 0, keySz);
  22037. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  22038. XMEMCPY(key, ecc_clikey_der_256, keySz);
  22039. #else
  22040. unsigned char cert[ONEK_BUF];
  22041. unsigned char key[ONEK_BUF];
  22042. XFILE fp;
  22043. int certSz, keySz;
  22044. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  22045. AssertTrue(fp != XBADFILE);
  22046. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  22047. XFCLOSE(fp);
  22048. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  22049. AssertTrue(fp != XBADFILE);
  22050. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  22051. XFCLOSE(fp);
  22052. #endif
  22053. #endif
  22054. XMEMSET(output, 0, outputSz);
  22055. AssertIntEQ(wc_InitRng(&rng), 0);
  22056. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22057. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22058. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  22059. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  22060. pkcs7->content = data;
  22061. pkcs7->contentSz = (word32)sizeof(data);
  22062. pkcs7->privateKey = key;
  22063. pkcs7->privateKeySz = (word32)sizeof(key);
  22064. pkcs7->encryptOID = RSAk;
  22065. pkcs7->hashOID = SHAh;
  22066. pkcs7->rng = &rng;
  22067. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  22068. wc_PKCS7_Free(pkcs7);
  22069. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22070. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22071. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22072. /* Pass in bad args. */
  22073. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  22074. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  22075. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  22076. badOutSz), BAD_FUNC_ARG);
  22077. pkcs7->hashOID = 0; /* bad hashOID */
  22078. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  22079. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  22080. !defined(NO_RSA) && !defined(NO_SHA256)
  22081. /* test RSA sign raw digest callback, if using RSA and compiled in.
  22082. * Example callback assumes SHA-256, so only run test if compiled in. */
  22083. wc_PKCS7_Free(pkcs7);
  22084. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22085. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  22086. pkcs7->content = data;
  22087. pkcs7->contentSz = (word32)sizeof(data);
  22088. pkcs7->privateKey = key;
  22089. pkcs7->privateKeySz = (word32)sizeof(key);
  22090. pkcs7->encryptOID = RSAk;
  22091. pkcs7->hashOID = SHA256h;
  22092. pkcs7->rng = &rng;
  22093. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  22094. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  22095. #endif
  22096. printf(resultFmt, passed);
  22097. wc_PKCS7_Free(pkcs7);
  22098. wc_FreeRng(&rng);
  22099. #endif
  22100. } /* END test_wc_PKCS7_EncodeSignedData */
  22101. /*
  22102. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  22103. */
  22104. static void test_wc_PKCS7_EncodeSignedData_ex(void)
  22105. {
  22106. #if defined(HAVE_PKCS7)
  22107. int ret, i;
  22108. PKCS7* pkcs7;
  22109. WC_RNG rng;
  22110. byte outputHead[FOURK_BUF/2];
  22111. byte outputFoot[FOURK_BUF/2];
  22112. word32 outputHeadSz = (word32)sizeof(outputHead);
  22113. word32 outputFootSz = (word32)sizeof(outputFoot);
  22114. byte data[FOURK_BUF];
  22115. wc_HashAlg hash;
  22116. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  22117. byte hashBuf[WC_MAX_DIGEST_SIZE];
  22118. word32 hashSz = wc_HashGetDigestSize(hashType);
  22119. #ifndef NO_RSA
  22120. #if defined(USE_CERT_BUFFERS_2048)
  22121. byte key[sizeof(client_key_der_2048)];
  22122. byte cert[sizeof(client_cert_der_2048)];
  22123. word32 keySz = (word32)sizeof(key);
  22124. word32 certSz = (word32)sizeof(cert);
  22125. XMEMSET(key, 0, keySz);
  22126. XMEMSET(cert, 0, certSz);
  22127. XMEMCPY(key, client_key_der_2048, keySz);
  22128. XMEMCPY(cert, client_cert_der_2048, certSz);
  22129. #elif defined(USE_CERT_BUFFERS_1024)
  22130. byte key[sizeof_client_key_der_1024];
  22131. byte cert[sizeof(sizeof_client_cert_der_1024)];
  22132. word32 keySz = (word32)sizeof(key);
  22133. word32 certSz = (word32)sizeof(cert);
  22134. XMEMSET(key, 0, keySz);
  22135. XMEMSET(cert, 0, certSz);
  22136. XMEMCPY(key, client_key_der_1024, keySz);
  22137. XMEMCPY(cert, client_cert_der_1024, certSz);
  22138. #else
  22139. unsigned char cert[ONEK_BUF];
  22140. unsigned char key[ONEK_BUF];
  22141. XFILE fp;
  22142. int certSz;
  22143. int keySz;
  22144. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  22145. AssertTrue((fp != XBADFILE));
  22146. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  22147. XFCLOSE(fp);
  22148. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  22149. AssertTrue(fp != XBADFILE);
  22150. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  22151. XFCLOSE(fp);
  22152. #endif
  22153. #elif defined(HAVE_ECC)
  22154. #if defined(USE_CERT_BUFFERS_256)
  22155. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22156. unsigned char key[sizeof(ecc_clikey_der_256)];
  22157. int certSz = (int)sizeof(cert);
  22158. int keySz = (int)sizeof(key);
  22159. XMEMSET(cert, 0, certSz);
  22160. XMEMSET(key, 0, keySz);
  22161. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  22162. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  22163. #else
  22164. unsigned char cert[ONEK_BUF];
  22165. unsigned char key[ONEK_BUF];
  22166. XFILE fp;
  22167. int certSz, keySz;
  22168. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  22169. AssertTrue(fp != XBADFILE);
  22170. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  22171. XFCLOSE(fp);
  22172. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  22173. AssertTrue(fp != XBADFILE);
  22174. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  22175. XFCLOSE(fp);
  22176. #endif
  22177. #endif
  22178. /* initialize large data with sequence */
  22179. for (i=0; i<(int)sizeof(data); i++)
  22180. data[i] = i & 0xff;
  22181. XMEMSET(outputHead, 0, outputHeadSz);
  22182. XMEMSET(outputFoot, 0, outputFootSz);
  22183. AssertIntEQ(wc_InitRng(&rng), 0);
  22184. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22185. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22186. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  22187. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  22188. pkcs7->content = NULL; /* not used for ex */
  22189. pkcs7->contentSz = (word32)sizeof(data);
  22190. pkcs7->privateKey = key;
  22191. pkcs7->privateKeySz = (word32)sizeof(key);
  22192. pkcs7->encryptOID = RSAk;
  22193. pkcs7->hashOID = SHAh;
  22194. pkcs7->rng = &rng;
  22195. /* calculate hash for content */
  22196. ret = wc_HashInit(&hash, hashType);
  22197. if (ret == 0) {
  22198. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  22199. if (ret == 0) {
  22200. ret = wc_HashFinal(&hash, hashType, hashBuf);
  22201. }
  22202. wc_HashFree(&hash, hashType);
  22203. }
  22204. AssertIntEQ(ret, 0);
  22205. /* Perform PKCS7 sign using hash directly */
  22206. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  22207. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  22208. AssertIntGT(outputHeadSz, 0);
  22209. AssertIntGT(outputFootSz, 0);
  22210. wc_PKCS7_Free(pkcs7);
  22211. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22212. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22213. /* required parameter even on verify when using _ex */
  22214. pkcs7->contentSz = (word32)sizeof(data);
  22215. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22216. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  22217. wc_PKCS7_Free(pkcs7);
  22218. /* assembly complete PKCS7 sign and use normal verify */
  22219. {
  22220. byte* output = (byte*)XMALLOC(outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22221. word32 outputSz = 0;
  22222. AssertNotNull(output);
  22223. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  22224. outputSz += outputHeadSz;
  22225. XMEMCPY(&output[outputSz], data, sizeof(data));
  22226. outputSz += sizeof(data);
  22227. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  22228. outputSz += outputFootSz;
  22229. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22230. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22231. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22232. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22233. }
  22234. /* Pass in bad args. */
  22235. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  22236. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  22237. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  22238. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  22239. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  22240. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  22241. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  22242. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  22243. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  22244. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  22245. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  22246. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  22247. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  22248. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  22249. pkcs7->hashOID = 0; /* bad hashOID */
  22250. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  22251. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  22252. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  22253. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  22254. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  22255. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  22256. #ifndef NO_PKCS7_STREAM
  22257. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  22258. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  22259. #else
  22260. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  22261. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  22262. #endif
  22263. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  22264. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  22265. #ifndef NO_PKCS7_STREAM
  22266. /* can pass in 0 buffer length with streaming API */
  22267. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22268. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  22269. #else
  22270. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22271. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  22272. #endif
  22273. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22274. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  22275. #ifndef NO_PKCS7_STREAM
  22276. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22277. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  22278. #else
  22279. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22280. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  22281. #endif
  22282. printf(resultFmt, passed);
  22283. wc_PKCS7_Free(pkcs7);
  22284. wc_FreeRng(&rng);
  22285. #endif
  22286. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  22287. #if defined(HAVE_PKCS7)
  22288. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  22289. byte* data, word32 dataSz,
  22290. int withAttribs, int detachedSig)
  22291. {
  22292. PKCS7* pkcs7;
  22293. WC_RNG rng;
  22294. static byte messageTypeOid[] =
  22295. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  22296. 0x09, 0x02 };
  22297. static byte messageType[] = { 0x13, 2, '1', '9' };
  22298. PKCS7Attrib attribs[] =
  22299. {
  22300. { messageTypeOid, sizeof(messageTypeOid), messageType,
  22301. sizeof(messageType) }
  22302. };
  22303. #ifndef NO_RSA
  22304. #if defined(USE_CERT_BUFFERS_2048)
  22305. byte key[sizeof(client_key_der_2048)];
  22306. byte cert[sizeof(client_cert_der_2048)];
  22307. word32 keySz = (word32)sizeof(key);
  22308. word32 certSz = (word32)sizeof(cert);
  22309. XMEMSET(key, 0, keySz);
  22310. XMEMSET(cert, 0, certSz);
  22311. XMEMCPY(key, client_key_der_2048, keySz);
  22312. XMEMCPY(cert, client_cert_der_2048, certSz);
  22313. #elif defined(USE_CERT_BUFFERS_1024)
  22314. byte key[sizeof_client_key_der_1024];
  22315. byte cert[sizeof(sizeof_client_cert_der_1024)];
  22316. word32 keySz = (word32)sizeof(key);
  22317. word32 certSz = (word32)sizeof(cert);
  22318. XMEMSET(key, 0, keySz);
  22319. XMEMSET(cert, 0, certSz);
  22320. XMEMCPY(key, client_key_der_1024, keySz);
  22321. XMEMCPY(cert, client_cert_der_1024, certSz);
  22322. #else
  22323. unsigned char cert[ONEK_BUF];
  22324. unsigned char key[ONEK_BUF];
  22325. FILE* fp;
  22326. int certSz;
  22327. int keySz;
  22328. fp = fopen("./certs/1024/client-cert.der", "rb");
  22329. AssertNotNull(fp);
  22330. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  22331. fclose(fp);
  22332. fp = fopen("./certs/1024/client-key.der", "rb");
  22333. AssertNotNull(fp);
  22334. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  22335. fclose(fp);
  22336. #endif
  22337. #elif defined(HAVE_ECC)
  22338. #if defined(USE_CERT_BUFFERS_256)
  22339. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22340. unsigned char key[sizeof(ecc_clikey_der_256)];
  22341. int certSz = (int)sizeof(cert);
  22342. int keySz = (int)sizeof(key);
  22343. XMEMSET(cert, 0, certSz);
  22344. XMEMSET(key, 0, keySz);
  22345. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  22346. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  22347. #else
  22348. unsigned char cert[ONEK_BUF];
  22349. unsigned char key[ONEK_BUF];
  22350. FILE* fp;
  22351. int certSz, keySz;
  22352. fp = fopen("./certs/client-ecc-cert.der", "rb");
  22353. AssertNotNull(fp);
  22354. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  22355. fclose(fp);
  22356. fp = fopen("./certs/client-ecc-key.der", "rb");
  22357. AssertNotNull(fp);
  22358. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  22359. fclose(fp);
  22360. #endif
  22361. #endif
  22362. XMEMSET(output, 0, outputSz);
  22363. AssertIntEQ(wc_InitRng(&rng), 0);
  22364. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22365. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22366. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  22367. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  22368. pkcs7->content = data;
  22369. pkcs7->contentSz = dataSz;
  22370. pkcs7->privateKey = key;
  22371. pkcs7->privateKeySz = (word32)sizeof(key);
  22372. pkcs7->encryptOID = RSAk;
  22373. pkcs7->hashOID = SHAh;
  22374. pkcs7->rng = &rng;
  22375. if (withAttribs) {
  22376. /* include a signed attribute */
  22377. pkcs7->signedAttribs = attribs;
  22378. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  22379. }
  22380. if (detachedSig) {
  22381. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  22382. }
  22383. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  22384. wc_PKCS7_Free(pkcs7);
  22385. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22386. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22387. if (detachedSig) {
  22388. pkcs7->content = data;
  22389. pkcs7->contentSz = dataSz;
  22390. }
  22391. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22392. wc_PKCS7_Free(pkcs7);
  22393. wc_FreeRng(&rng);
  22394. return outputSz;
  22395. }
  22396. #endif
  22397. /*
  22398. * Testing wc_PKCS_VerifySignedData()
  22399. */
  22400. static void test_wc_PKCS7_VerifySignedData(void)
  22401. {
  22402. #if defined(HAVE_PKCS7)
  22403. PKCS7* pkcs7;
  22404. byte output[FOURK_BUF];
  22405. word32 outputSz = sizeof(output);
  22406. byte data[] = "Test data to encode.";
  22407. byte badOut[1];
  22408. word32 badOutSz = 0;
  22409. byte badContent[] = "This is different content than was signed";
  22410. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22411. (word32)sizeof(data),
  22412. 0, 0)), 0);
  22413. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22414. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22415. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22416. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22417. /* Test bad args. */
  22418. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  22419. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  22420. #ifndef NO_PKCS7_STREAM
  22421. /* can pass in 0 buffer length with streaming API */
  22422. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  22423. badOutSz), WC_PKCS7_WANT_READ_E);
  22424. #else
  22425. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  22426. badOutSz), BAD_FUNC_ARG);
  22427. #endif
  22428. wc_PKCS7_Free(pkcs7);
  22429. /* Invalid content should error, use detached signature so we can
  22430. * easily change content */
  22431. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22432. (word32)sizeof(data),
  22433. 1, 1)), 0);
  22434. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22435. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22436. pkcs7->content = badContent;
  22437. pkcs7->contentSz = sizeof(badContent);
  22438. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), SIG_VERIFY_E);
  22439. wc_PKCS7_Free(pkcs7);
  22440. /* Test success case with detached signature and valid content */
  22441. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22442. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22443. pkcs7->content = data;
  22444. pkcs7->contentSz = sizeof(data);
  22445. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22446. wc_PKCS7_Free(pkcs7);
  22447. printf(resultFmt, passed);
  22448. #endif
  22449. } /* END test_wc_PKCS7_VerifySignedData() */
  22450. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  22451. !defined(NO_AES_256)
  22452. static const byte defKey[] = {
  22453. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22454. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22455. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22456. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22457. };
  22458. static byte aesHandle[32]; /* simulated hardware key handle */
  22459. /* return 0 on success */
  22460. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  22461. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  22462. byte* in, int inSz, byte* out, void* usrCtx)
  22463. {
  22464. int ret;
  22465. Aes aes;
  22466. if (usrCtx == NULL) {
  22467. /* no simulated handle passed in */
  22468. return -1;
  22469. }
  22470. switch (encryptOID) {
  22471. case AES256CBCb:
  22472. if (ivSz != AES_BLOCK_SIZE)
  22473. return BAD_FUNC_ARG;
  22474. break;
  22475. default:
  22476. WOLFSSL_MSG("Unsupported content cipher type for test");
  22477. return ALGO_ID_E;
  22478. };
  22479. /* simulate using handle to get key */
  22480. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  22481. if (ret == 0) {
  22482. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  22483. if (ret == 0)
  22484. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  22485. wc_AesFree(&aes);
  22486. }
  22487. (void)aad;
  22488. (void)aadSz;
  22489. (void)authTag;
  22490. (void)authTagSz;
  22491. (void)pkcs7;
  22492. return ret;
  22493. }
  22494. /* returns key size on success */
  22495. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  22496. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  22497. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  22498. {
  22499. int ret = -1;
  22500. if (out == NULL)
  22501. return BAD_FUNC_ARG;
  22502. if (keyId[0] != 0x00) {
  22503. return -1;
  22504. }
  22505. if (type != (int)PKCS7_KEKRI) {
  22506. return -1;
  22507. }
  22508. switch (keyWrapAlgo) {
  22509. case AES256_WRAP:
  22510. /* simulate setting a handle for later decryption but use key
  22511. * as handle in the test case here */
  22512. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  22513. aesHandle, sizeof(aesHandle), NULL);
  22514. if (ret < 0)
  22515. return ret;
  22516. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  22517. if (ret < 0)
  22518. return ret;
  22519. /* return key size on success */
  22520. return sizeof(defKey);
  22521. default:
  22522. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  22523. return BAD_KEYWRAP_ALG_E;
  22524. };
  22525. (void)cekSz;
  22526. (void)cek;
  22527. (void)outSz;
  22528. (void)keyIdSz;
  22529. (void)direction;
  22530. (void)orginKey; /* used with KAKRI */
  22531. (void)orginKeySz;
  22532. return ret;
  22533. }
  22534. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  22535. /*
  22536. * Testing wc_PKCS7_EncodeEnvelopedData()
  22537. */
  22538. static void test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  22539. {
  22540. #if defined(HAVE_PKCS7)
  22541. PKCS7* pkcs7;
  22542. #ifdef ECC_TIMING_RESISTANT
  22543. WC_RNG rng;
  22544. #endif
  22545. word32 tempWrd32 = 0;
  22546. byte* tmpBytePtr = NULL;
  22547. const char input[] = "Test data to encode.";
  22548. int i;
  22549. int testSz = 0;
  22550. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  22551. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22552. byte* rsaCert = NULL;
  22553. byte* rsaPrivKey = NULL;
  22554. word32 rsaCertSz;
  22555. word32 rsaPrivKeySz;
  22556. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  22557. !defined(USE_CERT_BUFFERS_2048) )
  22558. static const char* rsaClientCert = "./certs/client-cert.der";
  22559. static const char* rsaClientKey = "./certs/client-key.der";
  22560. rsaCertSz = (word32)sizeof(rsaClientCert);
  22561. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  22562. #endif
  22563. #endif
  22564. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  22565. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22566. byte* eccCert = NULL;
  22567. byte* eccPrivKey = NULL;
  22568. word32 eccCertSz;
  22569. word32 eccPrivKeySz;
  22570. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  22571. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  22572. static const char* eccClientKey = "./certs/ecc-client-key.der";
  22573. #endif
  22574. #endif
  22575. /* Generic buffer size. */
  22576. byte output[ONEK_BUF];
  22577. byte decoded[sizeof(input)/sizeof(char)];
  22578. int decodedSz = 0;
  22579. #ifndef NO_FILESYSTEM
  22580. XFILE certFile;
  22581. XFILE keyFile;
  22582. #endif
  22583. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  22584. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22585. /* RSA certs and keys. */
  22586. #if defined(USE_CERT_BUFFERS_1024)
  22587. /* Allocate buffer space. */
  22588. AssertNotNull(rsaCert =
  22589. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  22590. /* Init buffer. */
  22591. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  22592. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  22593. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  22594. DYNAMIC_TYPE_TMP_BUFFER));
  22595. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  22596. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  22597. #elif defined(USE_CERT_BUFFERS_2048)
  22598. /* Allocate buffer */
  22599. AssertNotNull(rsaCert =
  22600. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  22601. /* Init buffer. */
  22602. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  22603. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  22604. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  22605. DYNAMIC_TYPE_TMP_BUFFER));
  22606. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  22607. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  22608. #else
  22609. /* File system. */
  22610. certFile = XFOPEN(rsaClientCert, "rb");
  22611. AssertTrue(certFile != XBADFILE);
  22612. rsaCertSz = (word32)FOURK_BUF;
  22613. AssertNotNull(rsaCert =
  22614. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  22615. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  22616. XFCLOSE(certFile);
  22617. keyFile = XFOPEN(rsaClientKey, "rb");
  22618. AssertTrue(keyFile != XBADFILE);
  22619. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22620. DYNAMIC_TYPE_TMP_BUFFER));
  22621. rsaPrivKeySz = (word32)FOURK_BUF;
  22622. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  22623. XFCLOSE(keyFile);
  22624. #endif /* USE_CERT_BUFFERS */
  22625. #endif /* NO_RSA */
  22626. /* ECC */
  22627. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  22628. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22629. #ifdef USE_CERT_BUFFERS_256
  22630. AssertNotNull(eccCert =
  22631. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  22632. /* Init buffer. */
  22633. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  22634. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  22635. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  22636. DYNAMIC_TYPE_TMP_BUFFER));
  22637. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  22638. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  22639. #else /* File system. */
  22640. certFile = XFOPEN(eccClientCert, "rb");
  22641. AssertTrue(certFile != XBADFILE);
  22642. eccCertSz = (word32)FOURK_BUF;
  22643. AssertNotNull(eccCert =
  22644. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  22645. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  22646. XFCLOSE(certFile);
  22647. keyFile = XFOPEN(eccClientKey, "rb");
  22648. AssertTrue(keyFile != XBADFILE);
  22649. eccPrivKeySz = (word32)FOURK_BUF;
  22650. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22651. DYNAMIC_TYPE_TMP_BUFFER));
  22652. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  22653. XFCLOSE(keyFile);
  22654. #endif /* USE_CERT_BUFFERS_256 */
  22655. #endif /* END HAVE_ECC */
  22656. /* Silence. */
  22657. (void)keyFile;
  22658. (void)certFile;
  22659. const pkcs7EnvelopedVector testVectors[] = {
  22660. /* DATA is a global variable defined in the makefile. */
  22661. #if !defined(NO_RSA)
  22662. #ifndef NO_DES3
  22663. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  22664. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22665. #endif /* NO_DES3 */
  22666. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22667. #ifndef NO_AES_128
  22668. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  22669. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22670. #endif
  22671. #ifndef NO_AES_192
  22672. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  22673. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22674. #endif
  22675. #ifndef NO_AES_256
  22676. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  22677. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22678. #endif
  22679. #endif /* NO_AES && HAVE_AES_CBC */
  22680. #endif /* NO_RSA */
  22681. #if defined(HAVE_ECC)
  22682. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22683. #if !defined(NO_SHA) && !defined(NO_AES_128)
  22684. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  22685. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  22686. eccCertSz, eccPrivKey, eccPrivKeySz},
  22687. #endif
  22688. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  22689. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  22690. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  22691. eccCertSz, eccPrivKey, eccPrivKeySz},
  22692. #endif
  22693. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  22694. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  22695. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  22696. eccCertSz, eccPrivKey, eccPrivKeySz},
  22697. #endif
  22698. #endif /* NO_AES && HAVE_AES_CBC*/
  22699. #endif /* END HAVE_ECC */
  22700. }; /* END pkcs7EnvelopedVector */
  22701. #ifdef ECC_TIMING_RESISTANT
  22702. AssertIntEQ(wc_InitRng(&rng), 0);
  22703. #endif
  22704. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  22705. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22706. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  22707. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  22708. for (i = 0; i < testSz; i++) {
  22709. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  22710. (word32)(testVectors + i)->certSz), 0);
  22711. #ifdef ECC_TIMING_RESISTANT
  22712. pkcs7->rng = &rng;
  22713. #endif
  22714. pkcs7->content = (byte*)(testVectors + i)->content;
  22715. pkcs7->contentSz = (testVectors + i)->contentSz;
  22716. pkcs7->contentOID = (testVectors + i)->contentOID;
  22717. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  22718. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  22719. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  22720. pkcs7->privateKey = (testVectors + i)->privateKey;
  22721. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  22722. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  22723. (word32)sizeof(output)), 0);
  22724. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22725. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  22726. AssertIntGE(decodedSz, 0);
  22727. /* Verify the size of each buffer. */
  22728. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  22729. /* Don't free the last time through the loop. */
  22730. if (i < testSz - 1 ){
  22731. wc_PKCS7_Free(pkcs7);
  22732. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22733. }
  22734. } /* END test loop. */
  22735. /* Test bad args. */
  22736. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  22737. (word32)sizeof(output)), BAD_FUNC_ARG);
  22738. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  22739. (word32)sizeof(output)), BAD_FUNC_ARG);
  22740. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  22741. printf(resultFmt, passed);
  22742. /* Decode. */
  22743. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  22744. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  22745. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22746. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22747. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22748. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22749. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  22750. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  22751. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22752. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  22753. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22754. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  22755. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  22756. /* only a failure for KARI test cases */
  22757. tempWrd32 = pkcs7->singleCertSz;
  22758. pkcs7->singleCertSz = 0;
  22759. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22760. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22761. pkcs7->singleCertSz = tempWrd32;
  22762. tmpBytePtr = pkcs7->singleCert;
  22763. pkcs7->singleCert = NULL;
  22764. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22765. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22766. pkcs7->singleCert = tmpBytePtr;
  22767. #endif
  22768. tempWrd32 = pkcs7->privateKeySz;
  22769. pkcs7->privateKeySz = 0;
  22770. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22771. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22772. pkcs7->privateKeySz = tempWrd32;
  22773. tmpBytePtr = pkcs7->privateKey;
  22774. pkcs7->privateKey = NULL;
  22775. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22776. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22777. pkcs7->privateKey = tmpBytePtr;
  22778. wc_PKCS7_Free(pkcs7);
  22779. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  22780. /* test of decrypt callback with KEKRI enveloped data */
  22781. {
  22782. int envelopedSz;
  22783. const byte keyId[] = { 0x00 };
  22784. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22785. pkcs7->content = (byte*)input;
  22786. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  22787. pkcs7->contentOID = DATA;
  22788. pkcs7->encryptOID = AES256CBCb;
  22789. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  22790. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  22791. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  22792. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  22793. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  22794. (word32)sizeof(output))), 0);
  22795. wc_PKCS7_Free(pkcs7);
  22796. /* decode envelopedData */
  22797. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22798. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  22799. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  22800. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22801. envelopedSz, decoded, sizeof(decoded))), 0);
  22802. wc_PKCS7_Free(pkcs7);
  22803. }
  22804. #endif /* !NO_AES && !NO_AES_256 */
  22805. printf(resultFmt, passed);
  22806. #ifndef NO_RSA
  22807. if (rsaCert) {
  22808. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22809. }
  22810. if (rsaPrivKey) {
  22811. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22812. }
  22813. #endif /*NO_RSA */
  22814. #ifdef HAVE_ECC
  22815. if (eccCert) {
  22816. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22817. }
  22818. if (eccPrivKey) {
  22819. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22820. }
  22821. #endif /* HAVE_ECC */
  22822. #ifdef ECC_TIMING_RESISTANT
  22823. wc_FreeRng(&rng);
  22824. #endif
  22825. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3)
  22826. {
  22827. byte out[7];
  22828. byte *cms;
  22829. word32 cmsSz;
  22830. XFILE cmsFile;
  22831. XMEMSET(out, 0, sizeof(out));
  22832. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22833. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  22834. AssertTrue(cmsFile != XBADFILE);
  22835. cmsSz = (word32)FOURK_BUF;
  22836. AssertNotNull(cms =
  22837. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  22838. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  22839. XFCLOSE(cmsFile);
  22840. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  22841. sizeof_client_cert_der_2048), 0);
  22842. pkcs7->privateKey = (byte*)client_key_der_2048;
  22843. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  22844. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  22845. sizeof(out)), 0);
  22846. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22847. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  22848. wc_PKCS7_Free(pkcs7);
  22849. }
  22850. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 */
  22851. #endif /* HAVE_PKCS7 */
  22852. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  22853. /*
  22854. * Testing wc_PKCS7_EncodeEncryptedData()
  22855. */
  22856. static void test_wc_PKCS7_EncodeEncryptedData (void)
  22857. {
  22858. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  22859. PKCS7* pkcs7;
  22860. byte* tmpBytePtr = NULL;
  22861. byte encrypted[TWOK_BUF];
  22862. byte decoded[TWOK_BUF];
  22863. word32 tmpWrd32 = 0;
  22864. int tmpInt = 0;
  22865. int decodedSz;
  22866. int encryptedSz;
  22867. int testSz;
  22868. int i;
  22869. const byte data[] = { /* Hello World */
  22870. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  22871. 0x72,0x6c,0x64
  22872. };
  22873. #ifndef NO_DES3
  22874. byte desKey[] = {
  22875. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  22876. };
  22877. byte des3Key[] = {
  22878. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  22879. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  22880. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  22881. };
  22882. #endif
  22883. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22884. #ifndef NO_AES_128
  22885. byte aes128Key[] = {
  22886. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22887. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22888. };
  22889. #endif
  22890. #ifndef NO_AES_192
  22891. byte aes192Key[] = {
  22892. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22893. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22894. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22895. };
  22896. #endif
  22897. #ifndef NO_AES_256
  22898. byte aes256Key[] = {
  22899. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22900. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22901. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22902. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22903. };
  22904. #endif
  22905. #endif /* !NO_AES && HAVE_AES_CBC */
  22906. const pkcs7EncryptedVector testVectors[] =
  22907. {
  22908. #ifndef NO_DES3
  22909. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  22910. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  22911. #endif /* !NO_DES3 */
  22912. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22913. #ifndef NO_AES_128
  22914. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  22915. sizeof(aes128Key)},
  22916. #endif
  22917. #ifndef NO_AES_192
  22918. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  22919. sizeof(aes192Key)},
  22920. #endif
  22921. #ifndef NO_AES_256
  22922. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  22923. sizeof(aes256Key)},
  22924. #endif
  22925. #endif /* !NO_AES && HAVE_AES_CBC */
  22926. };
  22927. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  22928. for (i = 0; i < testSz; i++) {
  22929. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22930. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  22931. pkcs7->content = (byte*)testVectors[i].content;
  22932. pkcs7->contentSz = testVectors[i].contentSz;
  22933. pkcs7->contentOID = testVectors[i].contentOID;
  22934. pkcs7->encryptOID = testVectors[i].encryptOID;
  22935. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  22936. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  22937. pkcs7->heap = HEAP_HINT;
  22938. /* encode encryptedData */
  22939. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22940. sizeof(encrypted));
  22941. AssertIntGT(encryptedSz, 0);
  22942. /* Decode encryptedData */
  22943. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  22944. decoded, sizeof(decoded));
  22945. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  22946. /* Keep values for last itr. */
  22947. if (i < testSz - 1) {
  22948. wc_PKCS7_Free(pkcs7);
  22949. }
  22950. }
  22951. if (pkcs7 == NULL || testSz == 0) {
  22952. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  22953. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  22954. }
  22955. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  22956. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  22957. sizeof(encrypted)),BAD_FUNC_ARG);
  22958. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  22959. sizeof(encrypted)), BAD_FUNC_ARG);
  22960. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22961. 0), BAD_FUNC_ARG);
  22962. /* Testing the struct. */
  22963. tmpBytePtr = pkcs7->content;
  22964. pkcs7->content = NULL;
  22965. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22966. sizeof(encrypted)), BAD_FUNC_ARG);
  22967. pkcs7->content = tmpBytePtr;
  22968. tmpWrd32 = pkcs7->contentSz;
  22969. pkcs7->contentSz = 0;
  22970. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22971. sizeof(encrypted)), BAD_FUNC_ARG);
  22972. pkcs7->contentSz = tmpWrd32;
  22973. tmpInt = pkcs7->encryptOID;
  22974. pkcs7->encryptOID = 0;
  22975. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22976. sizeof(encrypted)), BAD_FUNC_ARG);
  22977. pkcs7->encryptOID = tmpInt;
  22978. tmpBytePtr = pkcs7->encryptionKey;
  22979. pkcs7->encryptionKey = NULL;
  22980. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22981. sizeof(encrypted)), BAD_FUNC_ARG);
  22982. pkcs7->encryptionKey = tmpBytePtr;
  22983. tmpWrd32 = pkcs7->encryptionKeySz;
  22984. pkcs7->encryptionKeySz = 0;
  22985. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22986. sizeof(encrypted)), BAD_FUNC_ARG);
  22987. pkcs7->encryptionKeySz = tmpWrd32;
  22988. printf(resultFmt, passed);
  22989. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  22990. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  22991. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  22992. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  22993. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  22994. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  22995. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  22996. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  22997. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  22998. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  22999. decoded, 0), BAD_FUNC_ARG);
  23000. /* Test struct fields */
  23001. tmpBytePtr = pkcs7->encryptionKey;
  23002. pkcs7->encryptionKey = NULL;
  23003. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  23004. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  23005. pkcs7->encryptionKey = tmpBytePtr;
  23006. pkcs7->encryptionKeySz = 0;
  23007. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  23008. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  23009. printf(resultFmt, passed);
  23010. wc_PKCS7_Free(pkcs7);
  23011. #endif
  23012. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  23013. /*
  23014. * Testing wc_PKCS7_Degenerate()
  23015. */
  23016. static void test_wc_PKCS7_Degenerate(void)
  23017. {
  23018. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  23019. PKCS7* pkcs7;
  23020. char fName[] = "./certs/test-degenerate.p7b";
  23021. XFILE f;
  23022. byte der[4096];
  23023. word32 derSz;
  23024. int ret;
  23025. printf(testingFmt, "wc_PKCS7_Degenerate()");
  23026. AssertNotNull(f = XFOPEN(fName, "rb"));
  23027. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  23028. derSz = (word32)ret;
  23029. XFCLOSE(f);
  23030. /* test degenerate success */
  23031. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23032. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23033. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23034. #ifndef NO_RSA
  23035. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23036. #else
  23037. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23038. #endif
  23039. wc_PKCS7_Free(pkcs7);
  23040. /* test with turning off degenerate cases */
  23041. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23042. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23043. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23044. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  23045. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  23046. wc_PKCS7_Free(pkcs7);
  23047. printf(resultFmt, passed);
  23048. #endif
  23049. } /* END test_wc_PKCS7_Degenerate() */
  23050. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  23051. defined(ASN_BER_TO_DER) && !defined(NO_DES3)
  23052. static byte berContent[] = {
  23053. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23054. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  23055. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  23056. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  23057. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  23058. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  23059. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  23060. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  23061. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  23062. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  23063. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  23064. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  23065. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  23066. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  23067. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  23068. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  23069. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  23070. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  23071. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23072. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23073. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  23074. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23075. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23076. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  23077. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  23078. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  23079. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  23080. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  23081. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  23082. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  23083. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  23084. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  23085. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  23086. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  23087. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  23088. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  23089. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  23090. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  23091. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  23092. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  23093. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  23094. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  23095. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  23096. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  23097. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23098. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  23099. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  23100. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  23101. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  23102. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  23103. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  23104. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  23105. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  23106. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  23107. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  23108. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  23109. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  23110. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  23111. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  23112. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  23113. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  23114. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  23115. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  23116. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  23117. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  23118. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  23119. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  23120. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  23121. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  23122. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  23123. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  23124. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  23125. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  23126. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  23127. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  23128. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  23129. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  23130. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  23131. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  23132. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  23133. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  23134. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  23135. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  23136. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  23137. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  23138. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  23139. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  23140. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  23141. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  23142. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  23143. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  23144. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  23145. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  23146. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  23147. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  23148. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  23149. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  23150. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  23151. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  23152. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  23153. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  23154. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  23155. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  23156. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  23157. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  23158. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  23159. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  23160. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  23161. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  23162. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  23163. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  23164. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  23165. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  23166. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  23167. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  23168. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  23169. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  23170. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  23171. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  23172. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  23173. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  23174. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  23175. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  23176. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  23177. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  23178. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  23179. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  23180. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  23181. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  23182. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  23183. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  23184. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  23185. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  23186. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  23187. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  23188. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  23189. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  23190. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  23191. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  23192. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  23193. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  23194. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  23195. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  23196. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  23197. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  23198. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  23199. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  23200. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  23201. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  23202. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  23203. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  23204. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  23205. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  23206. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  23207. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  23208. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  23209. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  23210. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  23211. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  23212. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  23213. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  23214. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  23215. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  23216. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  23217. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  23218. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  23219. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  23220. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  23221. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  23222. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  23223. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  23224. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  23225. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  23226. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  23227. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  23228. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  23229. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  23230. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  23231. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  23232. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  23233. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  23234. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  23235. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  23236. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  23237. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  23238. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  23239. 0x00, 0x00, 0x00, 0x00, 0x00
  23240. };
  23241. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
  23242. /*
  23243. * Testing wc_PKCS7_BER()
  23244. */
  23245. static void test_wc_PKCS7_BER(void)
  23246. {
  23247. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  23248. defined(ASN_BER_TO_DER)
  23249. PKCS7* pkcs7;
  23250. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  23251. XFILE f;
  23252. byte der[4096];
  23253. #ifndef NO_DES3
  23254. byte decoded[2048];
  23255. #endif
  23256. word32 derSz;
  23257. int ret;
  23258. printf(testingFmt, "wc_PKCS7_BER()");
  23259. AssertNotNull(f = XFOPEN(fName, "rb"));
  23260. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  23261. derSz = (word32)ret;
  23262. XFCLOSE(f);
  23263. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23264. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23265. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23266. #ifndef NO_RSA
  23267. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23268. #else
  23269. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23270. #endif
  23271. wc_PKCS7_Free(pkcs7);
  23272. #ifndef NO_DES3
  23273. /* decode BER content */
  23274. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  23275. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  23276. derSz = (word32)ret;
  23277. XFCLOSE(f);
  23278. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23279. #ifndef NO_RSA
  23280. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  23281. #else
  23282. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  23283. #endif
  23284. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  23285. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  23286. derSz = (word32)ret;
  23287. XFCLOSE(f);
  23288. pkcs7->privateKey = der;
  23289. pkcs7->privateKeySz = derSz;
  23290. #ifndef NO_RSA
  23291. #ifdef WOLFSSL_SP_MATH
  23292. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  23293. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  23294. #else
  23295. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  23296. sizeof(berContent), decoded, sizeof(decoded)), 0);
  23297. #endif
  23298. #else
  23299. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  23300. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  23301. #endif
  23302. wc_PKCS7_Free(pkcs7);
  23303. #endif /* !NO_DES3 */
  23304. printf(resultFmt, passed);
  23305. #endif
  23306. } /* END test_wc_PKCS7_BER() */
  23307. static void test_PKCS7_signed_enveloped(void)
  23308. {
  23309. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  23310. !defined(NO_FILESYSTEM)
  23311. XFILE f;
  23312. PKCS7* pkcs7;
  23313. #ifdef HAVE_AES_CBC
  23314. PKCS7* inner;
  23315. #endif
  23316. void* pt;
  23317. WC_RNG rng;
  23318. unsigned char key[FOURK_BUF/2];
  23319. unsigned char cert[FOURK_BUF/2];
  23320. unsigned char env[FOURK_BUF];
  23321. int envSz = FOURK_BUF;
  23322. int keySz;
  23323. int certSz;
  23324. unsigned char sig[FOURK_BUF * 2];
  23325. int sigSz = FOURK_BUF * 2;
  23326. #ifdef HAVE_AES_CBC
  23327. unsigned char decoded[FOURK_BUF];
  23328. int decodedSz = FOURK_BUF;
  23329. #endif
  23330. printf(testingFmt, "PKCS7_signed_enveloped");
  23331. /* load cert */
  23332. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  23333. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  23334. XFCLOSE(f);
  23335. /* load key */
  23336. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  23337. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  23338. XFCLOSE(f);
  23339. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  23340. /* sign cert for envelope */
  23341. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23342. AssertIntEQ(wc_InitRng(&rng), 0);
  23343. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23344. pkcs7->content = cert;
  23345. pkcs7->contentSz = certSz;
  23346. pkcs7->contentOID = DATA;
  23347. pkcs7->privateKey = key;
  23348. pkcs7->privateKeySz = keySz;
  23349. pkcs7->encryptOID = RSAk;
  23350. pkcs7->hashOID = SHA256h;
  23351. pkcs7->rng = &rng;
  23352. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  23353. wc_PKCS7_Free(pkcs7);
  23354. wc_FreeRng(&rng);
  23355. #ifdef HAVE_AES_CBC
  23356. /* create envelope */
  23357. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23358. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23359. pkcs7->content = sig;
  23360. pkcs7->contentSz = sigSz;
  23361. pkcs7->contentOID = DATA;
  23362. pkcs7->encryptOID = AES256CBCb;
  23363. pkcs7->privateKey = key;
  23364. pkcs7->privateKeySz = keySz;
  23365. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  23366. wc_PKCS7_Free(pkcs7);
  23367. #endif
  23368. /* create bad signed enveloped data */
  23369. sigSz = FOURK_BUF * 2;
  23370. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23371. AssertIntEQ(wc_InitRng(&rng), 0);
  23372. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23373. pkcs7->content = env;
  23374. pkcs7->contentSz = envSz;
  23375. pkcs7->contentOID = DATA;
  23376. pkcs7->privateKey = key;
  23377. pkcs7->privateKeySz = keySz;
  23378. pkcs7->encryptOID = RSAk;
  23379. pkcs7->hashOID = SHA256h;
  23380. pkcs7->rng = &rng;
  23381. /* Set no certs in bundle for this test. Hang on to the pointer though to
  23382. * free it later. */
  23383. pt = (void*)pkcs7->certList;
  23384. pkcs7->certList = NULL; /* no certs in bundle */
  23385. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  23386. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  23387. wc_PKCS7_Free(pkcs7);
  23388. /* check verify fails */
  23389. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23390. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23391. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  23392. PKCS7_SIGNEEDS_CHECK);
  23393. /* try verifying the signature manually */
  23394. {
  23395. RsaKey rKey;
  23396. word32 idx = 0;
  23397. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  23398. WC_MAX_DIGEST_SIZE];
  23399. int digestSz;
  23400. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  23401. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  23402. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  23403. digest, sizeof(digest), &rKey);
  23404. AssertIntGT(digestSz, 0);
  23405. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  23406. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  23407. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  23408. /* verify was success */
  23409. }
  23410. wc_PKCS7_Free(pkcs7);
  23411. /* initializing the PKCS7 struct with the signing certificate should pass */
  23412. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23413. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23414. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  23415. wc_PKCS7_Free(pkcs7);
  23416. /* create valid degenerate bundle */
  23417. sigSz = FOURK_BUF * 2;
  23418. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23419. pkcs7->content = env;
  23420. pkcs7->contentSz = envSz;
  23421. pkcs7->contentOID = DATA;
  23422. pkcs7->privateKey = key;
  23423. pkcs7->privateKeySz = keySz;
  23424. pkcs7->encryptOID = RSAk;
  23425. pkcs7->hashOID = SHA256h;
  23426. pkcs7->rng = &rng;
  23427. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  23428. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  23429. wc_PKCS7_Free(pkcs7);
  23430. wc_FreeRng(&rng);
  23431. /* check verify */
  23432. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23433. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  23434. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  23435. AssertNotNull(pkcs7->content);
  23436. #ifdef HAVE_AES_CBC
  23437. /* check decode */
  23438. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  23439. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  23440. inner->privateKey = key;
  23441. inner->privateKeySz = keySz;
  23442. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  23443. pkcs7->contentSz, decoded, decodedSz)), 0);
  23444. wc_PKCS7_Free(inner);
  23445. #endif
  23446. wc_PKCS7_Free(pkcs7);
  23447. #ifdef HAVE_AES_CBC
  23448. /* check cert set */
  23449. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23450. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23451. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  23452. AssertNotNull(pkcs7->singleCert);
  23453. AssertIntNE(pkcs7->singleCertSz, 0);
  23454. wc_PKCS7_Free(pkcs7);
  23455. #endif
  23456. printf(resultFmt, passed);
  23457. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  23458. }
  23459. static void test_wc_PKCS7_NoDefaultSignedAttribs (void)
  23460. {
  23461. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23462. && !defined(NO_AES)
  23463. PKCS7* pkcs7;
  23464. void* heap = NULL;
  23465. printf(testingFmt, "wc_PKCS7_NoDefaultSignedAttribs()");
  23466. pkcs7 = wc_PKCS7_New(heap, devId);
  23467. AssertNotNull(pkcs7);
  23468. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  23469. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  23470. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  23471. wc_PKCS7_Free(pkcs7);
  23472. printf(resultFmt, passed);
  23473. #endif
  23474. }
  23475. static void test_wc_PKCS7_SetOriEncryptCtx (void)
  23476. {
  23477. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23478. && !defined(NO_AES)
  23479. PKCS7* pkcs7;
  23480. void* heap = NULL;
  23481. WOLFSSL_CTX* ctx;
  23482. ctx = NULL;
  23483. printf(testingFmt, "wc_PKCS7_SetOriEncryptCtx()");
  23484. pkcs7 = wc_PKCS7_New(heap, devId);
  23485. AssertNotNull(pkcs7);
  23486. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  23487. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  23488. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  23489. wc_PKCS7_Free(pkcs7);
  23490. printf(resultFmt, passed);
  23491. #endif
  23492. }
  23493. static void test_wc_PKCS7_SetOriDecryptCtx (void)
  23494. {
  23495. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23496. && !defined(NO_AES)
  23497. PKCS7* pkcs7;
  23498. void* heap = NULL;
  23499. WOLFSSL_CTX* ctx;
  23500. ctx = NULL;
  23501. printf(testingFmt, "wc_PKCS7_SetOriDecryptCtx()");
  23502. pkcs7 = wc_PKCS7_New(heap, devId);
  23503. AssertNotNull(pkcs7);
  23504. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  23505. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  23506. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  23507. wc_PKCS7_Free(pkcs7);
  23508. printf(resultFmt, passed);
  23509. #endif
  23510. }
  23511. static void test_wc_PKCS7_DecodeCompressedData(void)
  23512. {
  23513. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23514. && !defined(NO_AES) && defined(HAVE_LIBZ)
  23515. PKCS7* pkcs7;
  23516. void* heap = NULL;
  23517. byte out[4096];
  23518. byte *decompressed;
  23519. int outSz, decompressedSz;
  23520. const char* cert = "./certs/client-cert.pem";
  23521. byte* cert_buf = NULL;
  23522. size_t cert_sz = 0;
  23523. printf(testingFmt, "wc_PKCS7_DecodeCompressedData()");
  23524. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  23525. AssertNotNull((decompressed =
  23526. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  23527. decompressedSz = (int)cert_sz;
  23528. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
  23529. pkcs7->content = (byte*)cert_buf;
  23530. pkcs7->contentSz = (word32)cert_sz;
  23531. pkcs7->contentOID = DATA;
  23532. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  23533. sizeof(out))), 0);
  23534. wc_PKCS7_Free(pkcs7);
  23535. /* compressed key should be smaller than when started */
  23536. AssertIntLT(outSz, cert_sz);
  23537. /* test decompression */
  23538. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
  23539. AssertIntEQ(pkcs7->contentOID, 0);
  23540. /* fail case with out buffer too small */
  23541. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  23542. decompressed, outSz), 0);
  23543. /* success case */
  23544. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  23545. decompressed, decompressedSz), cert_sz);
  23546. AssertIntEQ(pkcs7->contentOID, DATA);
  23547. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  23548. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  23549. decompressed = NULL;
  23550. /* test decompression function with different 'max' inputs */
  23551. outSz = sizeof(out);
  23552. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  23553. 0);
  23554. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  23555. out, outSz, 0, heap), 0);
  23556. AssertNull(decompressed);
  23557. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  23558. out, outSz, 0, heap), 0);
  23559. AssertNotNull(decompressed);
  23560. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  23561. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  23562. decompressed = NULL;
  23563. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  23564. out, outSz, 0, heap), 0);
  23565. AssertNotNull(decompressed);
  23566. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  23567. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  23568. if (cert_buf)
  23569. free(cert_buf);
  23570. wc_PKCS7_Free(pkcs7);
  23571. printf(resultFmt, passed);
  23572. #endif
  23573. }
  23574. static void test_wc_i2d_PKCS12(void)
  23575. {
  23576. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  23577. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23578. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  23579. WC_PKCS12* pkcs12 = NULL;
  23580. unsigned char der[FOURK_BUF * 2];
  23581. unsigned char* pt;
  23582. int derSz;
  23583. unsigned char out[FOURK_BUF * 2];
  23584. int outSz = FOURK_BUF * 2;
  23585. const char p12_f[] = "./certs/test-servercert.p12";
  23586. XFILE f;
  23587. printf(testingFmt, "wc_i2d_PKCS12");
  23588. f = XFOPEN(p12_f, "rb");
  23589. AssertNotNull(f);
  23590. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  23591. AssertIntGT(derSz, 0);
  23592. XFCLOSE(f);
  23593. AssertNotNull(pkcs12 = wc_PKCS12_new());
  23594. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  23595. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  23596. AssertIntEQ(outSz, derSz);
  23597. outSz = derSz - 1;
  23598. pt = out;
  23599. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  23600. outSz = derSz;
  23601. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  23602. AssertIntEQ((pt == out), 0);
  23603. pt = NULL;
  23604. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  23605. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  23606. wc_PKCS12_free(pkcs12);
  23607. printf(resultFmt, passed);
  23608. #endif
  23609. }
  23610. /* Testing wc_SignatureGetSize() for signature type ECC */
  23611. static int test_wc_SignatureGetSize_ecc(void)
  23612. {
  23613. int ret = 0;
  23614. #ifndef NO_SIG_WRAPPER
  23615. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  23616. enum wc_SignatureType sig_type;
  23617. word32 key_len;
  23618. /* Initialize ECC Key */
  23619. ecc_key ecc;
  23620. const char* qx =
  23621. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  23622. const char* qy =
  23623. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  23624. const char* d =
  23625. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  23626. ret = wc_ecc_init(&ecc);
  23627. if (ret == 0) {
  23628. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  23629. }
  23630. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  23631. if (ret == 0) {
  23632. /* Input for signature type ECC */
  23633. sig_type = WC_SIGNATURE_TYPE_ECC;
  23634. key_len = sizeof(ecc_key);
  23635. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  23636. /* Test bad args */
  23637. if (ret > 0) {
  23638. sig_type = (enum wc_SignatureType) 100;
  23639. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  23640. if (ret == BAD_FUNC_ARG) {
  23641. sig_type = WC_SIGNATURE_TYPE_ECC;
  23642. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  23643. }
  23644. if (ret >= 0) {
  23645. key_len = (word32) 0;
  23646. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  23647. }
  23648. if (ret == BAD_FUNC_ARG) {
  23649. ret = SIG_TYPE_E;
  23650. }
  23651. }
  23652. } else {
  23653. ret = WOLFSSL_FATAL_ERROR;
  23654. }
  23655. wc_ecc_free(&ecc);
  23656. #else
  23657. ret = SIG_TYPE_E;
  23658. #endif
  23659. if (ret == SIG_TYPE_E) {
  23660. ret = 0;
  23661. }
  23662. else {
  23663. ret = WOLFSSL_FATAL_ERROR;
  23664. }
  23665. printf(resultFmt, ret == 0 ? passed : failed);
  23666. #endif /* NO_SIG_WRAPPER */
  23667. return ret;
  23668. }/* END test_wc_SignatureGetSize_ecc() */
  23669. /* Testing wc_SignatureGetSize() for signature type rsa */
  23670. static int test_wc_SignatureGetSize_rsa(void)
  23671. {
  23672. int ret = 0;
  23673. #ifndef NO_SIG_WRAPPER
  23674. #ifndef NO_RSA
  23675. enum wc_SignatureType sig_type;
  23676. word32 key_len;
  23677. word32 idx = 0;
  23678. /* Initialize RSA Key */
  23679. RsaKey rsa_key;
  23680. byte* tmp = NULL;
  23681. size_t bytes;
  23682. #ifdef USE_CERT_BUFFERS_1024
  23683. bytes = (size_t)sizeof_client_key_der_1024;
  23684. if (bytes < (size_t)sizeof_client_key_der_1024)
  23685. bytes = (size_t)sizeof_client_cert_der_1024;
  23686. #elif defined(USE_CERT_BUFFERS_2048)
  23687. bytes = (size_t)sizeof_client_key_der_2048;
  23688. if (bytes < (size_t)sizeof_client_cert_der_2048)
  23689. bytes = (size_t)sizeof_client_cert_der_2048;
  23690. #else
  23691. bytes = FOURK_BUF;
  23692. #endif
  23693. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23694. if (tmp != NULL) {
  23695. #ifdef USE_CERT_BUFFERS_1024
  23696. XMEMCPY(tmp, client_key_der_1024,
  23697. (size_t)sizeof_client_key_der_1024);
  23698. #elif defined(USE_CERT_BUFFERS_2048)
  23699. XMEMCPY(tmp, client_key_der_2048,
  23700. (size_t)sizeof_client_key_der_2048);
  23701. #elif !defined(NO_FILESYSTEM)
  23702. file = XFOPEN(clientKey, "rb");
  23703. if (file != XBADFILE) {
  23704. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  23705. XFCLOSE(file);
  23706. }
  23707. else {
  23708. ret = WOLFSSL_FATAL_ERROR;
  23709. }
  23710. #else
  23711. ret = WOLFSSL_FATAL_ERROR;
  23712. #endif
  23713. } else {
  23714. ret = WOLFSSL_FATAL_ERROR;
  23715. }
  23716. if (ret == 0) {
  23717. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, devId);
  23718. }
  23719. if (ret == 0) {
  23720. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  23721. }
  23722. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  23723. if (ret == 0) {
  23724. /* Input for signature type RSA */
  23725. sig_type = WC_SIGNATURE_TYPE_RSA;
  23726. key_len = sizeof(RsaKey);
  23727. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  23728. /* Test bad args */
  23729. if (ret > 0) {
  23730. sig_type = (enum wc_SignatureType) 100;
  23731. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  23732. if (ret == BAD_FUNC_ARG) {
  23733. sig_type = WC_SIGNATURE_TYPE_RSA;
  23734. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  23735. }
  23736. #ifndef HAVE_USER_RSA
  23737. if (ret == BAD_FUNC_ARG) {
  23738. #else
  23739. if (ret == 0) {
  23740. #endif
  23741. key_len = (word32)0;
  23742. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  23743. }
  23744. if (ret == BAD_FUNC_ARG) {
  23745. ret = SIG_TYPE_E;
  23746. }
  23747. }
  23748. } else {
  23749. ret = WOLFSSL_FATAL_ERROR;
  23750. }
  23751. wc_FreeRsaKey(&rsa_key);
  23752. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23753. #else
  23754. ret = SIG_TYPE_E;
  23755. #endif
  23756. if (ret == SIG_TYPE_E) {
  23757. ret = 0;
  23758. }else {
  23759. ret = WOLFSSL_FATAL_ERROR;
  23760. }
  23761. printf(resultFmt, ret == 0 ? passed : failed);
  23762. #endif /* NO_SIG_WRAPPER */
  23763. return ret;
  23764. }/* END test_wc_SignatureGetSize_rsa(void) */
  23765. /*----------------------------------------------------------------------------*
  23766. | hash.h Tests
  23767. *----------------------------------------------------------------------------*/
  23768. static int test_wc_HashInit(void)
  23769. {
  23770. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  23771. wc_HashAlg hash;
  23772. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  23773. enum wc_HashType enumArray[] = {
  23774. #ifndef NO_MD5
  23775. WC_HASH_TYPE_MD5,
  23776. #endif
  23777. #ifndef NO_SHA
  23778. WC_HASH_TYPE_SHA,
  23779. #endif
  23780. #ifndef WOLFSSL_SHA224
  23781. WC_HASH_TYPE_SHA224,
  23782. #endif
  23783. #ifndef NO_SHA256
  23784. WC_HASH_TYPE_SHA256,
  23785. #endif
  23786. #ifndef WOLFSSL_SHA384
  23787. WC_HASH_TYPE_SHA384,
  23788. #endif
  23789. #ifndef WOLFSSL_SHA512
  23790. WC_HASH_TYPE_SHA512,
  23791. #endif
  23792. };
  23793. /* dynamically finds the length */
  23794. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  23795. /* For loop to test various arguments... */
  23796. for (i = 0; i < enumlen; i++) {
  23797. /* check for bad args */
  23798. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  23799. ret = 1;
  23800. break;
  23801. }
  23802. wc_HashFree(&hash, enumArray[i]);
  23803. /* check for null ptr */
  23804. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  23805. ret = 1;
  23806. break;
  23807. }
  23808. } /* end of for loop */
  23809. printf(testingFmt, "wc_HashInit()");
  23810. if (ret==0) { /* all tests have passed */
  23811. printf(resultFmt, passed);
  23812. }
  23813. else { /* a test has failed */
  23814. printf(resultFmt, failed);
  23815. }
  23816. return ret;
  23817. } /* end of test_wc_HashInit */
  23818. /*
  23819. * Unit test function for wc_HashSetFlags()
  23820. */
  23821. static int test_wc_HashSetFlags(void)
  23822. {
  23823. int ret = 0;
  23824. #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
  23825. wc_HashAlg hash;
  23826. word32 flags = 0;
  23827. int i, j;
  23828. printf(testingFmt, "wc_HashSetFlags()");
  23829. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  23830. enum wc_HashType enumArray[] = {
  23831. #ifndef NO_MD5
  23832. WC_HASH_TYPE_MD5,
  23833. #endif
  23834. #ifndef NO_SHA
  23835. WC_HASH_TYPE_SHA,
  23836. #endif
  23837. #ifdef WOLFSSL_SHA224
  23838. WC_HASH_TYPE_SHA224,
  23839. #endif
  23840. #ifndef NO_SHA256
  23841. WC_HASH_TYPE_SHA256,
  23842. #endif
  23843. #ifdef WOLFSSL_SHA384
  23844. WC_HASH_TYPE_SHA384,
  23845. #endif
  23846. #ifdef WOLFSSL_SHA512
  23847. WC_HASH_TYPE_SHA512,
  23848. #endif
  23849. #ifdef WOLFSSL_SHA3
  23850. WC_HASH_TYPE_SHA3_224,
  23851. #endif
  23852. };
  23853. enum wc_HashType notSupported[] = {
  23854. WC_HASH_TYPE_MD5_SHA,
  23855. WC_HASH_TYPE_MD2,
  23856. WC_HASH_TYPE_MD4,
  23857. WC_HASH_TYPE_BLAKE2B,
  23858. WC_HASH_TYPE_BLAKE2S,
  23859. WC_HASH_TYPE_NONE,
  23860. };
  23861. /* dynamically finds the length */
  23862. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  23863. /* For loop to test various arguments... */
  23864. for (i = 0; i < enumlen; i++) {
  23865. ret = wc_HashInit(&hash, enumArray[i]);
  23866. if (ret == 0) {
  23867. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  23868. }
  23869. if (ret == 0) {
  23870. if (flags & WC_HASH_FLAG_ISCOPY) {
  23871. ret = 0;
  23872. }
  23873. }
  23874. if (ret == 0) {
  23875. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  23876. if (ret == BAD_FUNC_ARG) {
  23877. ret = 0;
  23878. }
  23879. }
  23880. wc_HashFree(&hash, enumArray[i]);
  23881. }
  23882. /* For loop to test not supported cases */
  23883. int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  23884. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  23885. ret = wc_HashInit(&hash, notSupported[j]);
  23886. if (ret == 0) {
  23887. ret = -1;
  23888. }
  23889. else if (ret == BAD_FUNC_ARG){
  23890. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  23891. if (ret == 0) {
  23892. ret = -1;
  23893. }
  23894. else if (ret == BAD_FUNC_ARG) {
  23895. ret = 0;
  23896. }
  23897. }
  23898. if (ret == 0) {
  23899. ret = wc_HashFree(&hash, notSupported[j]);
  23900. if (ret == 0) {
  23901. ret = -1;
  23902. }
  23903. else if (ret == BAD_FUNC_ARG) {
  23904. ret = 0;
  23905. }
  23906. }
  23907. }
  23908. printf(resultFmt, ret == 0 ? passed : failed);
  23909. #endif
  23910. return ret;
  23911. } /* END test_wc_HashSetFlags */
  23912. /*
  23913. * Unit test function for wc_HashGetFlags()
  23914. */
  23915. static int test_wc_HashGetFlags(void)
  23916. {
  23917. int ret = 0;
  23918. #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
  23919. wc_HashAlg hash;
  23920. word32 flags = 0;
  23921. int i, j;
  23922. printf(testingFmt, "wc_HashGetFlags()");
  23923. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  23924. enum wc_HashType enumArray[] = {
  23925. #ifndef NO_MD5
  23926. WC_HASH_TYPE_MD5,
  23927. #endif
  23928. #ifndef NO_SHA
  23929. WC_HASH_TYPE_SHA,
  23930. #endif
  23931. #ifdef WOLFSSL_SHA224
  23932. WC_HASH_TYPE_SHA224,
  23933. #endif
  23934. #ifndef NO_SHA256
  23935. WC_HASH_TYPE_SHA256,
  23936. #endif
  23937. #ifdef WOLFSSL_SHA384
  23938. WC_HASH_TYPE_SHA384,
  23939. #endif
  23940. #ifdef WOLFSSL_SHA512
  23941. WC_HASH_TYPE_SHA512,
  23942. #endif
  23943. #ifdef WOLFSSL_SHA3
  23944. WC_HASH_TYPE_SHA3_224,
  23945. #endif
  23946. };
  23947. enum wc_HashType notSupported[] = {
  23948. WC_HASH_TYPE_MD5_SHA,
  23949. WC_HASH_TYPE_MD2,
  23950. WC_HASH_TYPE_MD4,
  23951. WC_HASH_TYPE_BLAKE2B,
  23952. WC_HASH_TYPE_BLAKE2S,
  23953. WC_HASH_TYPE_NONE,
  23954. };
  23955. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  23956. /* For loop to test various arguments... */
  23957. for (i = 0; i < enumlen; i++) {
  23958. ret = wc_HashInit(&hash, enumArray[i]);
  23959. if (ret == 0) {
  23960. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  23961. }
  23962. if (ret == 0) {
  23963. if (flags & WC_HASH_FLAG_ISCOPY) {
  23964. ret = 0;
  23965. }
  23966. }
  23967. if (ret == 0) {
  23968. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  23969. if (ret == BAD_FUNC_ARG) {
  23970. ret = 0;
  23971. }
  23972. }
  23973. wc_HashFree(&hash, enumArray[i]);
  23974. if (ret != 0) {
  23975. break;
  23976. }
  23977. }
  23978. /* For loop to test not supported cases */
  23979. int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  23980. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  23981. ret = wc_HashInit(&hash, notSupported[j]);
  23982. if (ret == 0) {
  23983. ret = -1;
  23984. }
  23985. else if (ret == BAD_FUNC_ARG){
  23986. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  23987. if (ret == 0) {
  23988. ret = -1;
  23989. }
  23990. else if (ret == BAD_FUNC_ARG) {
  23991. ret = 0;
  23992. }
  23993. }
  23994. if (ret == 0) {
  23995. ret = wc_HashFree(&hash, notSupported[j]);
  23996. if (ret == 0) {
  23997. ret = -1;
  23998. }
  23999. if (ret == BAD_FUNC_ARG) {
  24000. ret = 0;
  24001. }
  24002. }
  24003. }
  24004. printf(resultFmt, ret == 0 ? passed : failed);
  24005. #endif
  24006. return ret;
  24007. } /* END test_wc_HashGetFlags */
  24008. /*----------------------------------------------------------------------------*
  24009. | Compatibility Tests
  24010. *----------------------------------------------------------------------------*/
  24011. static void test_wolfSSL_lhash(void)
  24012. {
  24013. #ifdef OPENSSL_ALL
  24014. const char testStr[] = "Like a true nature's child\n"
  24015. "We were born\n"
  24016. "Born to be wild";
  24017. printf(testingFmt, "wolfSSL_LH_strhash()");
  24018. AssertIntEQ(lh_strhash(testStr), 0xb1231320);
  24019. printf(resultFmt, passed);
  24020. #endif
  24021. }
  24022. static void test_wolfSSL_X509_NAME(void)
  24023. {
  24024. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  24025. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  24026. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  24027. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  24028. defined(OPENSSL_EXTRA))
  24029. X509* x509;
  24030. const unsigned char* c;
  24031. unsigned char buf[4096];
  24032. int bytes;
  24033. XFILE f;
  24034. const X509_NAME* a;
  24035. const X509_NAME* b;
  24036. X509_NAME* d2i_name = NULL;
  24037. int sz;
  24038. unsigned char* tmp;
  24039. char file[] = "./certs/ca-cert.der";
  24040. #ifndef OPENSSL_EXTRA_X509_SMALL
  24041. byte empty[] = { /* CN=empty emailAddress= */
  24042. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  24043. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  24044. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  24045. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24046. 0x01, 0x16, 0x00
  24047. };
  24048. #endif
  24049. printf(testingFmt, "wolfSSL_X509_NAME()");
  24050. #ifndef OPENSSL_EXTRA_X509_SMALL
  24051. /* test compile of deprecated function, returns 0 */
  24052. AssertIntEQ(CRYPTO_thread_id(), 0);
  24053. #endif
  24054. AssertNotNull(a = X509_NAME_new());
  24055. X509_NAME_free((X509_NAME*)a);
  24056. f = XFOPEN(file, "rb");
  24057. AssertTrue(f != XBADFILE);
  24058. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  24059. XFCLOSE(f);
  24060. c = buf;
  24061. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  24062. /* test cmp function */
  24063. AssertNotNull(a = X509_get_issuer_name(x509));
  24064. AssertNotNull(b = X509_get_subject_name(x509));
  24065. #ifndef OPENSSL_EXTRA_X509_SMALL
  24066. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  24067. #endif
  24068. tmp = buf;
  24069. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  24070. if (sz > 0 && tmp == buf) {
  24071. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  24072. printf(" Expected pointer to be incremented\n");
  24073. abort();
  24074. }
  24075. #ifndef OPENSSL_EXTRA_X509_SMALL
  24076. tmp = buf;
  24077. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  24078. #endif
  24079. /* retry but with the function creating a buffer */
  24080. tmp = NULL;
  24081. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  24082. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  24083. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  24084. #ifndef OPENSSL_EXTRA_X509_SMALL
  24085. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  24086. #endif
  24087. X509_NAME_free((X509_NAME*)b);
  24088. X509_NAME_free(d2i_name);
  24089. X509_free(x509);
  24090. #ifndef OPENSSL_EXTRA_X509_SMALL
  24091. /* test with an empty domain component */
  24092. tmp = empty;
  24093. sz = sizeof(empty);
  24094. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  24095. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  24096. /* size of empty emailAddress will be 0 */
  24097. tmp = buf;
  24098. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  24099. (char*)tmp, sizeof(buf)), 0);
  24100. /* should contain no organization name */
  24101. tmp = buf;
  24102. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  24103. (char*)tmp, sizeof(buf)), -1);
  24104. X509_NAME_free(d2i_name);
  24105. #endif
  24106. printf(resultFmt, passed);
  24107. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  24108. }
  24109. static void test_wolfSSL_X509_NAME_hash(void)
  24110. {
  24111. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_SHA)
  24112. BIO* bio;
  24113. X509* x509 = NULL;
  24114. printf(testingFmt, "wolfSSL_X509_NAME_hash");
  24115. AssertNotNull(bio = BIO_new(BIO_s_file()));
  24116. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  24117. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  24118. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0xF6CF410E);
  24119. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0x677DD39A);
  24120. X509_free(x509);
  24121. BIO_free(bio);
  24122. printf(resultFmt, passed);
  24123. #endif
  24124. }
  24125. #ifndef NO_BIO
  24126. static void test_wolfSSL_X509_INFO(void)
  24127. {
  24128. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  24129. STACK_OF(X509_INFO) *info_stack;
  24130. X509_INFO *info;
  24131. BIO *cert;
  24132. int i;
  24133. printf(testingFmt, "wolfSSL_X509_INFO");
  24134. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  24135. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  24136. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  24137. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  24138. AssertNotNull(info->x509);
  24139. AssertNull(info->crl);
  24140. }
  24141. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  24142. BIO_free(cert);
  24143. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  24144. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  24145. sk_X509_INFO_free(info_stack);
  24146. BIO_free(cert);
  24147. printf(resultFmt, passed);
  24148. #endif
  24149. }
  24150. #endif
  24151. static void test_wolfSSL_X509_subject_name_hash(void)
  24152. {
  24153. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  24154. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  24155. X509* x509;
  24156. X509_NAME* subjectName = NULL;
  24157. unsigned long ret = 0;
  24158. printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
  24159. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  24160. SSL_FILETYPE_PEM));
  24161. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  24162. ret = X509_subject_name_hash(x509);
  24163. AssertIntNE(ret, 0);
  24164. X509_free(x509);
  24165. printf(resultFmt, passed);
  24166. #endif
  24167. }
  24168. static void test_wolfSSL_X509_issuer_name_hash(void)
  24169. {
  24170. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  24171. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  24172. X509* x509;
  24173. X509_NAME* issuertName = NULL;
  24174. unsigned long ret = 0;
  24175. printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
  24176. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  24177. SSL_FILETYPE_PEM));
  24178. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  24179. ret = X509_issuer_name_hash(x509);
  24180. AssertIntNE(ret, 0);
  24181. X509_free(x509);
  24182. printf(resultFmt, passed);
  24183. #endif
  24184. }
  24185. static void test_wolfSSL_X509_check_host(void)
  24186. {
  24187. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  24188. && !defined(NO_SHA) && !defined(NO_RSA)
  24189. X509* x509;
  24190. const char altName[] = "example.com";
  24191. printf(testingFmt, "wolfSSL_X509_check_host()");
  24192. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  24193. SSL_FILETYPE_PEM));
  24194. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  24195. WOLFSSL_SUCCESS);
  24196. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  24197. WOLFSSL_FAILURE);
  24198. X509_free(x509);
  24199. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  24200. WOLFSSL_FAILURE);
  24201. printf(resultFmt, passed);
  24202. #endif
  24203. }
  24204. static void test_wolfSSL_X509_check_email(void)
  24205. {
  24206. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  24207. X509* x509;
  24208. const char goodEmail[] = "info@wolfssl.com";
  24209. const char badEmail[] = "disinfo@wolfssl.com";
  24210. printf(testingFmt, "wolfSSL_X509_check_email()");
  24211. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  24212. SSL_FILETYPE_PEM));
  24213. /* Should fail on non-matching email address */
  24214. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  24215. WOLFSSL_FAILURE);
  24216. /* Should succeed on matching email address */
  24217. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  24218. WOLFSSL_SUCCESS);
  24219. /* Should compute length internally when not provided */
  24220. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  24221. WOLFSSL_SUCCESS);
  24222. /* Should fail when email address is NULL */
  24223. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  24224. WOLFSSL_FAILURE);
  24225. X509_free(x509);
  24226. /* Should fail when x509 is NULL */
  24227. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  24228. WOLFSSL_FAILURE);
  24229. printf(resultFmt, passed);
  24230. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  24231. }
  24232. static void test_wolfSSL_DES(void)
  24233. {
  24234. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  24235. const_DES_cblock myDes;
  24236. DES_cblock iv;
  24237. DES_key_schedule key;
  24238. word32 i;
  24239. DES_LONG dl;
  24240. unsigned char msg[] = "hello wolfssl";
  24241. printf(testingFmt, "wolfSSL_DES()");
  24242. DES_check_key(1);
  24243. DES_set_key(&myDes, &key);
  24244. /* check, check of odd parity */
  24245. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  24246. XMEMSET(key, 5, sizeof(DES_key_schedule));
  24247. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  24248. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  24249. /* set odd parity for success case */
  24250. DES_set_odd_parity(&myDes);
  24251. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  24252. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  24253. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  24254. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  24255. AssertIntEQ(key[i], myDes[i]);
  24256. }
  24257. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  24258. /* check weak key */
  24259. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  24260. XMEMSET(key, 5, sizeof(DES_key_schedule));
  24261. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  24262. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  24263. /* now do unchecked copy of a weak key over */
  24264. DES_set_key_unchecked(&myDes, &key);
  24265. /* compare arrays, should be the same */
  24266. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  24267. AssertIntEQ(key[i], myDes[i]);
  24268. }
  24269. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  24270. /* check DES_key_sched API */
  24271. XMEMSET(key, 1, sizeof(DES_key_schedule));
  24272. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  24273. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  24274. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  24275. /* compare arrays, should be the same */
  24276. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  24277. AssertIntEQ(key[i], myDes[i]);
  24278. }
  24279. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  24280. * DES_cbc_encrypt on the input */
  24281. XMEMSET(iv, 0, sizeof(DES_cblock));
  24282. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  24283. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  24284. AssertIntEQ(dl, 480052723);
  24285. printf(resultFmt, passed);
  24286. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  24287. }
  24288. static void test_wc_PemToDer(void)
  24289. {
  24290. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  24291. int ret;
  24292. DerBuffer* pDer = NULL;
  24293. const char* ca_cert = "./certs/server-cert.pem";
  24294. byte* cert_buf = NULL;
  24295. size_t cert_sz = 0;
  24296. int eccKey = 0;
  24297. EncryptedInfo info;
  24298. printf(testingFmt, "wc_PemToDer()");
  24299. XMEMSET(&info, 0, sizeof(info));
  24300. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  24301. if (ret == 0) {
  24302. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  24303. &pDer, NULL, &info, &eccKey);
  24304. AssertIntEQ(ret, 0);
  24305. wc_FreeDer(&pDer);
  24306. }
  24307. if (cert_buf)
  24308. free(cert_buf);
  24309. #ifdef HAVE_ECC
  24310. {
  24311. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  24312. byte key_buf[256] = {0};
  24313. /* Test fail of loading a key with cert type */
  24314. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  24315. key_buf[0] = '\n';
  24316. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  24317. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  24318. &pDer, NULL, &info, &eccKey)), 0);
  24319. #ifdef OPENSSL_EXTRA
  24320. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  24321. &pDer, NULL, &info, &eccKey)), 0);
  24322. #endif
  24323. wc_FreeDer(&pDer);
  24324. if (cert_buf)
  24325. free(cert_buf);
  24326. }
  24327. #endif
  24328. printf(resultFmt, passed);
  24329. #endif
  24330. }
  24331. static void test_wc_AllocDer(void)
  24332. {
  24333. #if !defined(NO_CERTS)
  24334. int ret;
  24335. DerBuffer* pDer = NULL;
  24336. word32 testSize = 1024;
  24337. printf(testingFmt, "wc_AllocDer()");
  24338. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  24339. AssertIntEQ(ret, 0);
  24340. AssertNotNull(pDer);
  24341. wc_FreeDer(&pDer);
  24342. printf(resultFmt, passed);
  24343. #endif
  24344. }
  24345. static void test_wc_CertPemToDer(void)
  24346. {
  24347. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  24348. int ret;
  24349. const char* ca_cert = "./certs/ca-cert.pem";
  24350. byte* cert_buf = NULL;
  24351. size_t cert_sz = 0, cert_dersz = 0;
  24352. byte* cert_der = NULL;
  24353. printf(testingFmt, "wc_CertPemToDer()");
  24354. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  24355. if (ret == 0) {
  24356. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  24357. cert_der = (byte*)malloc(cert_dersz);
  24358. if (cert_der) {
  24359. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  24360. cert_der, (int)cert_dersz, CERT_TYPE);
  24361. AssertIntGE(ret, 0);
  24362. }
  24363. }
  24364. if (cert_der)
  24365. free(cert_der);
  24366. if (cert_buf)
  24367. free(cert_buf);
  24368. #endif
  24369. }
  24370. static void test_wc_PubKeyPemToDer(void)
  24371. {
  24372. #ifdef WOLFSSL_PEM_TO_DER
  24373. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  24374. int ret;
  24375. const char* key = "./certs/ecc-client-keyPub.pem";
  24376. byte* cert_buf = NULL;
  24377. size_t cert_sz = 0, cert_dersz = 0;
  24378. byte* cert_der = NULL;
  24379. printf(testingFmt, "wc_PubKeyPemToDer()");
  24380. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  24381. cert_der, (int)cert_dersz);
  24382. AssertIntGE(ret, BAD_FUNC_ARG);
  24383. ret = load_file(key, &cert_buf, &cert_sz);
  24384. if (ret == 0) {
  24385. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  24386. cert_der = (byte*)malloc(cert_dersz);
  24387. if (cert_der) {
  24388. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  24389. cert_der, (int)cert_dersz);
  24390. AssertIntGE(ret, 0);
  24391. }
  24392. }
  24393. if (cert_der)
  24394. free(cert_der);
  24395. if (cert_buf)
  24396. free(cert_buf);
  24397. #endif
  24398. #endif
  24399. }
  24400. static void test_wc_PemPubKeyToDer(void)
  24401. {
  24402. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  24403. int ret;
  24404. const char* key = "./certs/ecc-client-keyPub.pem";
  24405. size_t cert_dersz = 1024;
  24406. byte* cert_der = (byte*)malloc(cert_dersz);
  24407. printf(testingFmt, "wc_PemPubKeyToDer()");
  24408. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  24409. AssertIntGE(ret, BAD_FUNC_ARG);
  24410. if (cert_der) {
  24411. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  24412. AssertIntGE(ret, 0);
  24413. free(cert_der);
  24414. }
  24415. #endif
  24416. }
  24417. static void test_wolfSSL_certs(void)
  24418. {
  24419. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  24420. !defined(NO_RSA)
  24421. X509* x509ext;
  24422. #ifdef OPENSSL_ALL
  24423. X509* x509;
  24424. WOLFSSL_X509_EXTENSION* ext;
  24425. ASN1_OBJECT* obj;
  24426. #endif
  24427. WOLFSSL* ssl;
  24428. WOLFSSL_CTX* ctx;
  24429. STACK_OF(ASN1_OBJECT)* sk;
  24430. ASN1_STRING* asn1_str;
  24431. AUTHORITY_KEYID* akey;
  24432. BASIC_CONSTRAINTS* bc;
  24433. int crit;
  24434. printf(testingFmt, "wolfSSL_certs()");
  24435. #ifndef NO_WOLFSSL_SERVER
  24436. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  24437. #else
  24438. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  24439. #endif
  24440. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  24441. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  24442. #ifndef HAVE_USER_RSA
  24443. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  24444. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  24445. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  24446. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  24447. #endif
  24448. AssertNotNull(ssl = SSL_new(ctx));
  24449. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24450. #ifdef HAVE_PK_CALLBACKS
  24451. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  24452. #endif /* HAVE_PK_CALLBACKS */
  24453. /* create and use x509 */
  24454. #ifdef OPENSSL_ALL
  24455. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  24456. AssertNotNull(x509);
  24457. #endif
  24458. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  24459. AssertNotNull(x509ext);
  24460. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  24461. #ifndef HAVE_USER_RSA
  24462. /* with loading in a new cert the check on private key should now fail */
  24463. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24464. #endif
  24465. #if defined(USE_CERT_BUFFERS_2048)
  24466. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  24467. (unsigned char*)server_cert_der_2048,
  24468. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  24469. #endif
  24470. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  24471. /************* Get Digest of Certificate ******************/
  24472. {
  24473. byte digest[64]; /* max digest size */
  24474. word32 digestSz;
  24475. XMEMSET(digest, 0, sizeof(digest));
  24476. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  24477. WOLFSSL_SUCCESS);
  24478. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  24479. WOLFSSL_SUCCESS);
  24480. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  24481. WOLFSSL_FAILURE);
  24482. }
  24483. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  24484. /* test and checkout X509 extensions */
  24485. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  24486. &crit, NULL);
  24487. AssertNotNull(bc);
  24488. AssertIntEQ(crit, 0);
  24489. #ifdef OPENSSL_ALL
  24490. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  24491. AssertNotNull(ext);
  24492. X509_EXTENSION_free(ext);
  24493. AssertNotNull(ext = X509_EXTENSION_new());
  24494. X509_EXTENSION_set_critical(ext, 1);
  24495. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  24496. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  24497. ASN1_OBJECT_free(obj);
  24498. X509_EXTENSION_free(ext);
  24499. AssertNotNull(ext = X509_EXTENSION_new());
  24500. X509_EXTENSION_set_critical(ext, 0);
  24501. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  24502. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  24503. NULL);
  24504. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  24505. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  24506. * and X509_get_ext_d2i has created new */
  24507. X509_EXTENSION_free(ext);
  24508. #endif
  24509. BASIC_CONSTRAINTS_free(bc);
  24510. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  24511. AssertNotNull(asn1_str);
  24512. AssertIntEQ(crit, 1);
  24513. AssertIntEQ(asn1_str->type, NID_key_usage);
  24514. #ifdef OPENSSL_ALL
  24515. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  24516. AssertNotNull(ext);
  24517. X509_EXTENSION_free(ext);
  24518. #endif
  24519. ASN1_STRING_free(asn1_str);
  24520. #ifdef OPENSSL_ALL
  24521. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  24522. &crit, NULL);
  24523. AssertNotNull(sk);
  24524. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  24525. AssertNotNull(ext);
  24526. X509_EXTENSION_free(ext);
  24527. sk_ASN1_OBJECT_free(sk);
  24528. #else
  24529. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  24530. &crit, NULL);
  24531. AssertNull(sk);
  24532. #endif
  24533. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  24534. NID_authority_key_identifier, &crit, NULL);
  24535. AssertNotNull(akey);
  24536. #ifdef OPENSSL_ALL
  24537. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  24538. AssertNotNull(ext);
  24539. X509_EXTENSION_free(ext);
  24540. #endif
  24541. wolfSSL_AUTHORITY_KEYID_free(akey);
  24542. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  24543. NID_private_key_usage_period, &crit, NULL);
  24544. /* AssertNotNull(sk); NID not yet supported */
  24545. AssertIntEQ(crit, -1);
  24546. sk_ASN1_OBJECT_free(sk);
  24547. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  24548. &crit, NULL);
  24549. /* AssertNotNull(sk); no alt names set */
  24550. sk_GENERAL_NAME_free(sk);
  24551. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  24552. &crit, NULL);
  24553. /* AssertNotNull(sk); NID not yet supported */
  24554. AssertIntEQ(crit, -1);
  24555. sk_ASN1_OBJECT_free(sk);
  24556. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  24557. NULL);
  24558. /* AssertNotNull(sk); no auth info set */
  24559. sk_ASN1_OBJECT_free(sk);
  24560. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  24561. &crit, NULL);
  24562. /* AssertNotNull(sk); NID not yet supported */
  24563. AssertIntEQ(crit, -1);
  24564. sk_ASN1_OBJECT_free(sk);
  24565. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  24566. &crit, NULL);
  24567. /* AssertNotNull(sk); NID not yet supported */
  24568. AssertIntEQ(crit, -1);
  24569. sk_ASN1_OBJECT_free(sk);
  24570. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  24571. NID_certificate_policies, &crit, NULL);
  24572. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  24573. AssertNull(sk);
  24574. #else
  24575. /* AssertNotNull(sk); no cert policy set */
  24576. #endif
  24577. sk_ASN1_OBJECT_free(sk);
  24578. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  24579. &crit, NULL);
  24580. /* AssertNotNull(sk); NID not yet supported */
  24581. AssertIntEQ(crit, -1);
  24582. sk_ASN1_OBJECT_free(sk);
  24583. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  24584. &crit, NULL);
  24585. /* AssertNotNull(sk); NID not yet supported */
  24586. AssertIntEQ(crit, -1);
  24587. sk_ASN1_OBJECT_free(sk);
  24588. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  24589. &crit, NULL);
  24590. /* AssertNotNull(sk); NID not yet supported */
  24591. AssertIntEQ(crit, -1);
  24592. sk_ASN1_OBJECT_free(sk);
  24593. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  24594. NULL);
  24595. /* AssertNotNull(sk); NID not yet supported */
  24596. AssertIntEQ(crit, -1);
  24597. sk_ASN1_OBJECT_free(sk);
  24598. /* test invalid cases */
  24599. crit = 0;
  24600. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  24601. AssertNull(sk);
  24602. AssertIntEQ(crit, -1);
  24603. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  24604. NULL, NULL);
  24605. AssertNull(sk);
  24606. AssertIntEQ(SSL_get_hit(ssl), 0);
  24607. #ifdef OPENSSL_ALL
  24608. X509_free(x509);
  24609. #endif
  24610. X509_free(x509ext);
  24611. SSL_free(ssl);
  24612. SSL_CTX_free(ctx);
  24613. printf(resultFmt, passed);
  24614. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  24615. }
  24616. static void test_wolfSSL_X509_check_private_key(void)
  24617. {
  24618. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  24619. defined(USE_CERT_BUFFERS_2048)
  24620. X509* x509;
  24621. EVP_PKEY* pkey = NULL;
  24622. const byte* key;
  24623. printf(testingFmt, "wolfSSL_X509_check_private_key()");
  24624. /* Check with correct key */
  24625. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  24626. SSL_FILETYPE_PEM)));
  24627. key = client_key_der_2048;
  24628. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  24629. &key, (long)sizeof_client_key_der_2048));
  24630. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  24631. EVP_PKEY_free(pkey);
  24632. pkey = NULL;
  24633. /* Check with wrong key */
  24634. key = server_key_der_2048;
  24635. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  24636. &key, (long)sizeof_server_key_der_2048));
  24637. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  24638. /* test for incorrect parameter */
  24639. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  24640. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  24641. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  24642. EVP_PKEY_free(pkey);
  24643. X509_free(x509);
  24644. printf(resultFmt, passed);
  24645. #endif
  24646. }
  24647. static void test_wolfSSL_ASN1_TIME_print(void)
  24648. {
  24649. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  24650. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  24651. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048)
  24652. BIO* bio;
  24653. X509* x509;
  24654. const unsigned char* der = client_cert_der_2048;
  24655. ASN1_TIME* t;
  24656. unsigned char buf[25];
  24657. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  24658. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24659. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  24660. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  24661. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  24662. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  24663. AssertIntEQ(XMEMCMP(buf, "Feb 10 19:49:52 2021 GMT", sizeof(buf) - 1), 0);
  24664. /* create a bad time and test results */
  24665. AssertNotNull(t = X509_get_notAfter(x509));
  24666. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  24667. t->data[8] = 0;
  24668. t->data[3] = 0;
  24669. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  24670. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  24671. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  24672. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  24673. BIO_free(bio);
  24674. X509_free(x509);
  24675. printf(resultFmt, passed);
  24676. #endif
  24677. }
  24678. static void test_wolfSSL_ASN1_UTCTIME_print(void)
  24679. {
  24680. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  24681. BIO* bio;
  24682. ASN1_UTCTIME* utc = NULL;
  24683. unsigned char buf[25];
  24684. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  24685. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  24686. printf(testingFmt, "ASN1_UTCTIME_print()");
  24687. /* NULL parameter check */
  24688. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24689. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  24690. BIO_free(bio);
  24691. /* Valid date */
  24692. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24693. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  24694. DYNAMIC_TYPE_ASN1));
  24695. utc->type = ASN_UTC_TIME;
  24696. utc->length = ASN_UTC_TIME_SIZE;
  24697. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  24698. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  24699. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  24700. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  24701. XMEMSET(buf, 0, sizeof(buf));
  24702. BIO_free(bio);
  24703. /* Invalid format */
  24704. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  24705. utc->type = ASN_UTC_TIME;
  24706. utc->length = ASN_UTC_TIME_SIZE;
  24707. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  24708. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  24709. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  24710. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  24711. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  24712. BIO_free(bio);
  24713. printf(resultFmt, passed);
  24714. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  24715. }
  24716. static void test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  24717. {
  24718. #if defined(OPENSSL_EXTRA)
  24719. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  24720. unsigned char nullstr[32];
  24721. XMEMSET(nullstr, 0, 32);
  24722. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  24723. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  24724. DYNAMIC_TYPE_TMP_BUFFER);
  24725. if (asn1_gtime) {
  24726. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  24727. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  24728. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  24729. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24730. }
  24731. #endif /* OPENSSL_EXTRA */
  24732. }
  24733. static void test_wolfSSL_private_keys(void)
  24734. {
  24735. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  24736. !defined(NO_FILESYSTEM)
  24737. WOLFSSL* ssl;
  24738. WOLFSSL_CTX* ctx;
  24739. EVP_PKEY* pkey = NULL;
  24740. printf(testingFmt, "wolfSSL_private_keys()");
  24741. OpenSSL_add_all_digests();
  24742. OpenSSL_add_all_algorithms();
  24743. #ifndef NO_RSA
  24744. #ifndef NO_WOLFSSL_SERVER
  24745. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  24746. #else
  24747. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  24748. #endif
  24749. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  24750. /* Have to load a cert before you can check the private key against that
  24751. * certificates public key! */
  24752. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  24753. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  24754. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  24755. AssertNotNull(ssl = SSL_new(ctx));
  24756. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24757. #ifdef USE_CERT_BUFFERS_2048
  24758. {
  24759. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  24760. unsigned char buf[FOURK_BUF];
  24761. word32 bufSz;
  24762. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  24763. (unsigned char*)client_key_der_2048,
  24764. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  24765. #ifndef HAVE_USER_RSA
  24766. /* Should mismatch now that a different private key loaded */
  24767. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24768. #endif
  24769. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  24770. (unsigned char*)server_key,
  24771. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  24772. /* After loading back in DER format of original key, should match */
  24773. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24774. /* test loading private key to the WOLFSSL_CTX */
  24775. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  24776. (unsigned char*)client_key_der_2048,
  24777. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  24778. #ifndef NO_CHECK_PRIVATE_KEY
  24779. #ifndef HAVE_USER_RSA
  24780. /* Should mismatch now that a different private key loaded */
  24781. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  24782. #endif
  24783. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  24784. (unsigned char*)server_key,
  24785. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  24786. /* After loading back in DER format of original key, should match */
  24787. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  24788. #endif /* !NO_CHECK_PRIVATE_KEY */
  24789. /* pkey not set yet, expecting to fail */
  24790. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  24791. /* set PKEY and test again */
  24792. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  24793. &server_key, (long)sizeof_server_key_der_2048));
  24794. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  24795. /* reuse PKEY structure and test
  24796. * this should be checked with a memory management sanity checker */
  24797. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  24798. server_key = (const unsigned char*)server_key_der_2048;
  24799. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  24800. &server_key, (long)sizeof_server_key_der_2048));
  24801. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  24802. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  24803. bufSz = FOURK_BUF;
  24804. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  24805. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  24806. RSAk, NULL, 0)), 0);
  24807. server_key = (const unsigned char*)buf;
  24808. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  24809. (long)bufSz));
  24810. }
  24811. #endif
  24812. EVP_PKEY_free(pkey);
  24813. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  24814. SSL_CTX_free(ctx);
  24815. #endif /* end of RSA private key match tests */
  24816. #ifdef HAVE_ECC
  24817. #ifndef NO_WOLFSSL_SERVER
  24818. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  24819. #else
  24820. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  24821. #endif
  24822. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  24823. WOLFSSL_FILETYPE_PEM));
  24824. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  24825. WOLFSSL_FILETYPE_PEM));
  24826. AssertNotNull(ssl = SSL_new(ctx));
  24827. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24828. SSL_free(ssl);
  24829. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  24830. WOLFSSL_FILETYPE_PEM));
  24831. AssertNotNull(ssl = SSL_new(ctx));
  24832. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24833. SSL_free(ssl);
  24834. SSL_CTX_free(ctx);
  24835. #endif /* end of ECC private key match tests */
  24836. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  24837. #ifndef NO_WOLFSSL_SERVER
  24838. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  24839. #else
  24840. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  24841. #endif
  24842. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  24843. WOLFSSL_FILETYPE_PEM));
  24844. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  24845. WOLFSSL_FILETYPE_PEM));
  24846. AssertNotNull(ssl = SSL_new(ctx));
  24847. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24848. SSL_free(ssl);
  24849. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  24850. WOLFSSL_FILETYPE_PEM));
  24851. AssertNotNull(ssl = SSL_new(ctx));
  24852. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24853. SSL_free(ssl);
  24854. SSL_CTX_free(ctx);
  24855. #endif /* end of Ed25519 private key match tests */
  24856. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  24857. #ifndef NO_WOLFSSL_SERVER
  24858. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  24859. #else
  24860. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  24861. #endif
  24862. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  24863. WOLFSSL_FILETYPE_PEM));
  24864. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  24865. WOLFSSL_FILETYPE_PEM));
  24866. AssertNotNull(ssl = SSL_new(ctx));
  24867. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24868. SSL_free(ssl);
  24869. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  24870. WOLFSSL_FILETYPE_PEM));
  24871. AssertNotNull(ssl = SSL_new(ctx));
  24872. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  24873. SSL_free(ssl);
  24874. SSL_CTX_free(ctx);
  24875. #endif /* end of Ed448 private key match tests */
  24876. EVP_cleanup();
  24877. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  24878. CONF_modules_free();
  24879. ENGINE_cleanup();
  24880. CONF_modules_unload();
  24881. (void)ssl;
  24882. (void)ctx;
  24883. (void)pkey;
  24884. printf(resultFmt, passed);
  24885. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  24886. }
  24887. static void test_wolfSSL_PEM_read_PrivateKey(void)
  24888. {
  24889. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  24890. XFILE file;
  24891. const char* fname = "./certs/server-key.pem";
  24892. EVP_PKEY* pkey;
  24893. RSA* rsa;
  24894. WOLFSSL_EVP_PKEY_CTX* ctx;
  24895. unsigned char* sig;
  24896. size_t sigLen;
  24897. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  24898. size_t tbsLen = sizeof(tbs);
  24899. printf(testingFmt, "test_wolfSSL_PEM_read_PrivateKey()");
  24900. /* Check error case. */
  24901. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  24902. /* Read in an RSA key. */
  24903. file = XFOPEN(fname, "rb");
  24904. AssertTrue(file != XBADFILE);
  24905. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  24906. XFCLOSE(file);
  24907. /* Make sure the key is usable by signing some data with it. */
  24908. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  24909. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  24910. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  24911. DYNAMIC_TYPE_TMP_BUFFER));
  24912. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  24913. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  24914. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  24915. WOLFSSL_SUCCESS);
  24916. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24917. EVP_PKEY_CTX_free(ctx);
  24918. EVP_PKEY_free(pkey);
  24919. printf(resultFmt, passed);
  24920. #endif
  24921. }
  24922. static void test_wolfSSL_PEM_PrivateKey(void)
  24923. {
  24924. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  24925. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  24926. #ifndef NO_BIO
  24927. BIO* bio = NULL;
  24928. #endif
  24929. EVP_PKEY* pkey = NULL;
  24930. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  24931. #ifndef NO_BIO
  24932. /* test creating new EVP_PKEY with bad arg */
  24933. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  24934. /* test loading RSA key using BIO */
  24935. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  24936. {
  24937. XFILE file;
  24938. const char* fname = "./certs/server-key.pem";
  24939. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  24940. size_t sz;
  24941. byte* buf;
  24942. EVP_PKEY* pkey2;
  24943. EVP_PKEY* pkey3;
  24944. RSA* rsa_key = NULL;
  24945. file = XFOPEN(fname, "rb");
  24946. AssertTrue((file != XBADFILE));
  24947. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  24948. sz = XFTELL(file);
  24949. XREWIND(file);
  24950. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  24951. if (buf) {
  24952. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  24953. }
  24954. XFCLOSE(file);
  24955. /* Test using BIO new mem and loading PEM private key */
  24956. bio = BIO_new_mem_buf(buf, (int)sz);
  24957. AssertNotNull(bio);
  24958. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  24959. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  24960. BIO_free(bio);
  24961. bio = NULL;
  24962. AssertNotNull(pkey2 = EVP_PKEY_new());
  24963. pkey2->type = EVP_PKEY_RSA;
  24964. /* Test parameter copy */
  24965. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  24966. EVP_PKEY_free(pkey2);
  24967. EVP_PKEY_free(pkey);
  24968. pkey = NULL;
  24969. /* Qt unit test case : rsa pkcs8 key */
  24970. file = XFOPEN(fname_rsa_p8, "rb");
  24971. AssertTrue((file != XBADFILE));
  24972. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  24973. sz = XFTELL(file);
  24974. XREWIND(file);
  24975. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  24976. if (buf)
  24977. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  24978. XFCLOSE(file);
  24979. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  24980. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  24981. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  24982. BIO_free(bio);
  24983. bio = NULL;
  24984. AssertNotNull(pkey3 = EVP_PKEY_new());
  24985. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  24986. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  24987. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  24988. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  24989. #else
  24990. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  24991. #endif
  24992. RSA_free(rsa_key);
  24993. EVP_PKEY_free(pkey3);
  24994. EVP_PKEY_free(pkey);
  24995. pkey = NULL;
  24996. }
  24997. #endif
  24998. /* test loading ECC key using BIO */
  24999. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  25000. {
  25001. XFILE file;
  25002. const char* fname = "./certs/ecc-key.pem";
  25003. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  25004. size_t sz;
  25005. byte* buf;
  25006. EVP_PKEY* pkey2;
  25007. EVP_PKEY* pkey3;
  25008. EC_KEY* ec_key;
  25009. int nid = 0;
  25010. file = XFOPEN(fname, "rb");
  25011. AssertTrue((file != XBADFILE));
  25012. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  25013. sz = XFTELL(file);
  25014. XREWIND(file);
  25015. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  25016. if (buf)
  25017. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  25018. XFCLOSE(file);
  25019. /* Test using BIO new mem and loading PEM private key */
  25020. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  25021. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  25022. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  25023. BIO_free(bio);
  25024. bio = NULL;
  25025. AssertNotNull(pkey2 = EVP_PKEY_new());
  25026. AssertNotNull(pkey3 = EVP_PKEY_new());
  25027. pkey2->type = EVP_PKEY_EC;
  25028. /* Test parameter copy */
  25029. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  25030. /* Qt unit test case 1*/
  25031. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  25032. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  25033. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  25034. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  25035. #else
  25036. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  25037. #endif
  25038. /* Test default digest */
  25039. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  25040. AssertIntEQ(nid, NID_sha256);
  25041. EC_KEY_free(ec_key);
  25042. EVP_PKEY_free(pkey3);
  25043. EVP_PKEY_free(pkey2);
  25044. EVP_PKEY_free(pkey);
  25045. pkey = NULL;
  25046. /* Qt unit test case ec pkcs8 key */
  25047. file = XFOPEN(fname_ecc_p8, "rb");
  25048. AssertTrue((file != XBADFILE));
  25049. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  25050. sz = XFTELL(file);
  25051. XREWIND(file);
  25052. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  25053. if (buf)
  25054. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  25055. XFCLOSE(file);
  25056. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  25057. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  25058. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  25059. BIO_free(bio);
  25060. bio = NULL;
  25061. AssertNotNull(pkey3 = EVP_PKEY_new());
  25062. /* Qt unit test case */
  25063. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  25064. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  25065. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  25066. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  25067. #else
  25068. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  25069. #endif
  25070. EC_KEY_free(ec_key);
  25071. EVP_PKEY_free(pkey3);
  25072. EVP_PKEY_free(pkey);
  25073. pkey = NULL;
  25074. }
  25075. #endif
  25076. #if !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
  25077. {
  25078. #define BIO_PEM_TEST_CHAR 'a'
  25079. EVP_PKEY* pkey2 = NULL;
  25080. unsigned char extra[10];
  25081. int i;
  25082. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  25083. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  25084. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  25085. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  25086. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  25087. &server_key, (long)sizeof_server_key_der_2048));
  25088. AssertNull(pkey);
  25089. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  25090. &server_key, (long)sizeof_server_key_der_2048));
  25091. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  25092. WOLFSSL_SUCCESS);
  25093. /* test creating new EVP_PKEY with good args */
  25094. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  25095. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  25096. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  25097. /* test of reuse of EVP_PKEY */
  25098. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  25099. AssertIntEQ(BIO_pending(bio), 0);
  25100. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  25101. SSL_SUCCESS);
  25102. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  25103. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  25104. AssertNotNull(pkey);
  25105. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  25106. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  25107. }
  25108. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  25109. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  25110. for (i = 0; i < 10; i++) {
  25111. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  25112. }
  25113. BIO_free(bio);
  25114. bio = NULL;
  25115. EVP_PKEY_free(pkey);
  25116. pkey = NULL;
  25117. EVP_PKEY_free(pkey2);
  25118. }
  25119. #endif
  25120. /* key is DES encrypted */
  25121. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  25122. !defined(NO_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_MD5) && \
  25123. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  25124. {
  25125. XFILE f;
  25126. pem_password_cb* passwd_cb;
  25127. void* passwd_cb_userdata;
  25128. SSL_CTX* ctx;
  25129. char passwd[] = "bad password";
  25130. #ifndef WOLFSSL_NO_TLS12
  25131. #ifndef NO_WOLFSSL_SERVER
  25132. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  25133. #else
  25134. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  25135. #endif
  25136. #else
  25137. #ifndef NO_WOLFSSL_SERVER
  25138. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  25139. #else
  25140. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  25141. #endif
  25142. #endif
  25143. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  25144. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  25145. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  25146. AssertNull(passwd_cb_userdata =
  25147. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  25148. /* fail case with password call back */
  25149. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  25150. (void*)passwd));
  25151. BIO_free(bio);
  25152. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  25153. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  25154. (void*)passwd));
  25155. BIO_free(bio);
  25156. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  25157. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  25158. /* use callback that works */
  25159. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  25160. (void*)"yassl123"));
  25161. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  25162. EVP_PKEY_free(pkey);
  25163. pkey = NULL;
  25164. BIO_free(bio);
  25165. bio = NULL;
  25166. SSL_CTX_free(ctx);
  25167. }
  25168. #endif /* !defined(NO_DES3) */
  25169. #endif /* !NO_BIO */
  25170. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  25171. {
  25172. unsigned char buf[2048];
  25173. size_t bytes;
  25174. XFILE f;
  25175. SSL_CTX* ctx;
  25176. #ifndef WOLFSSL_NO_TLS12
  25177. #ifndef NO_WOLFSSL_SERVER
  25178. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  25179. #else
  25180. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  25181. #endif
  25182. #else
  25183. #ifndef NO_WOLFSSL_SERVER
  25184. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  25185. #else
  25186. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  25187. #endif
  25188. #endif
  25189. f = XFOPEN("./certs/ecc-key.der", "rb");
  25190. AssertTrue((f != XBADFILE));
  25191. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  25192. XFCLOSE(f);
  25193. server_key = buf;
  25194. pkey = NULL;
  25195. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  25196. AssertNull(pkey);
  25197. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  25198. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  25199. EVP_PKEY_free(pkey);
  25200. pkey = NULL;
  25201. SSL_CTX_free(ctx);
  25202. }
  25203. #endif
  25204. printf(resultFmt, passed);
  25205. #ifndef NO_BIO
  25206. (void)bio;
  25207. #endif
  25208. (void)pkey;
  25209. (void)server_key;
  25210. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  25211. }
  25212. #ifndef NO_BIO
  25213. static void test_wolfSSL_PEM_bio_RSAKey(void)
  25214. {
  25215. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  25216. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  25217. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  25218. RSA* rsa = NULL;
  25219. BIO* bio = NULL;
  25220. printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
  25221. /* PrivateKey */
  25222. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  25223. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  25224. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  25225. AssertIntEQ(RSA_size(rsa), 256);
  25226. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  25227. NULL), WOLFSSL_FAILURE);
  25228. BIO_free(bio);
  25229. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  25230. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  25231. NULL), WOLFSSL_SUCCESS);
  25232. BIO_free(bio);
  25233. RSA_free(rsa);
  25234. /* PUBKEY */
  25235. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  25236. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  25237. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  25238. AssertIntEQ(RSA_size(rsa), 256);
  25239. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  25240. BIO_free(bio);
  25241. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  25242. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  25243. BIO_free(bio);
  25244. RSA_free(rsa);
  25245. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  25246. AssertNotNull(bio = BIO_new_file("./certs/server-pub-key.pem", "rb"));
  25247. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  25248. BIO_free(bio);
  25249. RSA_free(rsa);
  25250. #ifdef HAVE_ECC
  25251. /* ensure that non-rsa keys do not work */
  25252. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  25253. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  25254. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  25255. BIO_free(bio);
  25256. RSA_free(rsa);
  25257. #endif /* HAVE_ECC */
  25258. printf(resultFmt, passed);
  25259. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  25260. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  25261. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  25262. }
  25263. static void test_wolfSSL_PEM_RSAPrivateKey(void)
  25264. {
  25265. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  25266. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  25267. RSA* rsa = NULL;
  25268. RSA* rsa_dup = NULL;
  25269. BIO* bio = NULL;
  25270. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  25271. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  25272. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  25273. AssertIntEQ(RSA_size(rsa), 256);
  25274. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  25275. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  25276. AssertPtrNE(rsa_dup, rsa);
  25277. #endif
  25278. /* test if valgrind complains about unreleased memory */
  25279. RSA_up_ref(rsa);
  25280. RSA_free(rsa);
  25281. BIO_free(bio);
  25282. RSA_free(rsa);
  25283. RSA_free(rsa_dup);
  25284. #ifdef HAVE_ECC
  25285. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  25286. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  25287. BIO_free(bio);
  25288. #endif /* HAVE_ECC */
  25289. printf(resultFmt, passed);
  25290. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  25291. }
  25292. static void test_wolfSSL_PEM_bio_DSAKey(void)
  25293. {
  25294. #ifndef HAVE_SELFTEST
  25295. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  25296. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  25297. DSA* dsa = NULL;
  25298. BIO* bio = NULL;
  25299. printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
  25300. /* PrivateKey */
  25301. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  25302. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  25303. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  25304. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  25305. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  25306. WOLFSSL_FAILURE);
  25307. BIO_free(bio);
  25308. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  25309. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  25310. WOLFSSL_SUCCESS);
  25311. BIO_free(bio);
  25312. DSA_free(dsa);
  25313. /* PUBKEY */
  25314. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  25315. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  25316. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  25317. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  25318. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  25319. BIO_free(bio);
  25320. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  25321. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  25322. BIO_free(bio);
  25323. DSA_free(dsa);
  25324. #ifdef HAVE_ECC
  25325. /* ensure that non-dsa keys do not work */
  25326. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  25327. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  25328. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  25329. BIO_free(bio);
  25330. DSA_free(dsa);
  25331. #endif /* HAVE_ECC */
  25332. printf(resultFmt, passed);
  25333. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  25334. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  25335. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  25336. #endif /* HAVE_SELFTEST */
  25337. }
  25338. static void test_wolfSSL_PEM_bio_ECKey(void)
  25339. {
  25340. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  25341. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  25342. EC_KEY* ec = NULL;
  25343. BIO* bio = NULL;
  25344. printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
  25345. /* PrivateKey */
  25346. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  25347. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  25348. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  25349. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  25350. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  25351. NULL),WOLFSSL_FAILURE);
  25352. BIO_free(bio);
  25353. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  25354. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  25355. NULL), WOLFSSL_SUCCESS);
  25356. BIO_free(bio);
  25357. EC_KEY_free(ec);
  25358. /* PUBKEY */
  25359. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  25360. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  25361. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  25362. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  25363. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  25364. BIO_free(bio);
  25365. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  25366. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  25367. BIO_free(bio);
  25368. EC_KEY_free(ec);
  25369. #ifndef NO_RSA
  25370. /* ensure that non-ec keys do not work */
  25371. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  25372. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  25373. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  25374. BIO_free(bio);
  25375. EC_KEY_free(ec);
  25376. #endif /* HAVE_ECC */
  25377. printf(resultFmt, passed);
  25378. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  25379. }
  25380. static void test_wolfSSL_PEM_PUBKEY(void)
  25381. {
  25382. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  25383. BIO* bio = NULL;
  25384. EVP_PKEY* pkey = NULL;
  25385. /* test creating new EVP_PKEY with bad arg */
  25386. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  25387. /* test loading ECC key using BIO */
  25388. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  25389. {
  25390. XFILE file;
  25391. const char* fname = "./certs/ecc-client-keyPub.pem";
  25392. size_t sz;
  25393. byte* buf;
  25394. EVP_PKEY* pkey2;
  25395. EC_KEY* ec_key;
  25396. file = XFOPEN(fname, "rb");
  25397. AssertTrue((file != XBADFILE));
  25398. XFSEEK(file, 0, XSEEK_END);
  25399. sz = XFTELL(file);
  25400. XREWIND(file);
  25401. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  25402. if (buf)
  25403. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  25404. XFCLOSE(file);
  25405. /* Test using BIO new mem and loading PEM private key */
  25406. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  25407. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  25408. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  25409. BIO_free(bio);
  25410. bio = NULL;
  25411. /* Qt unit test case*/
  25412. AssertNotNull(pkey2 = EVP_PKEY_new());
  25413. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  25414. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  25415. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  25416. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  25417. #else
  25418. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  25419. #endif
  25420. EC_KEY_free(ec_key);
  25421. EVP_PKEY_free(pkey2);
  25422. EVP_PKEY_free(pkey);
  25423. pkey = NULL;
  25424. }
  25425. #endif
  25426. (void)bio;
  25427. (void)pkey;
  25428. #endif
  25429. }
  25430. #endif /* !NO_BIO */
  25431. static void test_DSA_do_sign_verify(void)
  25432. {
  25433. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  25434. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  25435. !defined(NO_DSA)
  25436. unsigned char digest[WC_SHA_DIGEST_SIZE];
  25437. DSA_SIG* sig;
  25438. DSA* dsa;
  25439. word32 bytes;
  25440. byte sigBin[DSA_SIG_SIZE];
  25441. int dsacheck;
  25442. #ifdef USE_CERT_BUFFERS_1024
  25443. byte tmp[ONEK_BUF];
  25444. XMEMSET(tmp, 0, sizeof(tmp));
  25445. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  25446. bytes = sizeof_dsa_key_der_1024;
  25447. #elif defined(USE_CERT_BUFFERS_2048)
  25448. byte tmp[TWOK_BUF];
  25449. XMEMSET(tmp, 0, sizeof(tmp));
  25450. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  25451. bytes = sizeof_dsa_key_der_2048;
  25452. #else
  25453. byte tmp[TWOK_BUF];
  25454. XMEMSET(tmp, 0, sizeof(tmp));
  25455. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  25456. if (fp == XBADFILE) {
  25457. return WOLFSSL_BAD_FILE;
  25458. }
  25459. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  25460. XFCLOSE(fp);
  25461. #endif /* END USE_CERT_BUFFERS_1024 */
  25462. printf(testingFmt, "DSA_do_sign_verify()");
  25463. XMEMSET(digest, 202, sizeof(digest));
  25464. AssertNotNull(dsa = DSA_new());
  25465. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  25466. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  25467. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  25468. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  25469. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  25470. DSA_SIG_free(sig);
  25471. DSA_free(dsa);
  25472. #endif
  25473. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  25474. }
  25475. static void test_wolfSSL_tmp_dh(void)
  25476. {
  25477. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  25478. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  25479. byte buff[6000];
  25480. char file[] = "./certs/dsaparams.pem";
  25481. XFILE f;
  25482. int bytes;
  25483. DSA* dsa;
  25484. DH* dh;
  25485. #if defined(WOLFSSL_DH_EXTRA) && \
  25486. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  25487. DH* dh2;
  25488. #endif
  25489. BIO* bio;
  25490. SSL* ssl;
  25491. SSL_CTX* ctx;
  25492. printf(testingFmt, "wolfSSL_tmp_dh()");
  25493. #ifndef NO_WOLFSSL_SERVER
  25494. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  25495. #else
  25496. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  25497. #endif
  25498. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  25499. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  25500. AssertNotNull(ssl = SSL_new(ctx));
  25501. f = XFOPEN(file, "rb");
  25502. AssertTrue((f != XBADFILE));
  25503. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  25504. XFCLOSE(f);
  25505. bio = BIO_new_mem_buf((void*)buff, bytes);
  25506. AssertNotNull(bio);
  25507. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  25508. AssertNotNull(dsa);
  25509. dh = wolfSSL_DSA_dup_DH(dsa);
  25510. AssertNotNull(dh);
  25511. #if defined(WOLFSSL_DH_EXTRA) && \
  25512. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  25513. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  25514. #endif
  25515. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  25516. #ifndef NO_WOLFSSL_SERVER
  25517. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  25518. #else
  25519. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  25520. #endif
  25521. BIO_free(bio);
  25522. DSA_free(dsa);
  25523. DH_free(dh);
  25524. #if defined(WOLFSSL_DH_EXTRA) && \
  25525. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  25526. DH_free(dh2);
  25527. #endif
  25528. SSL_free(ssl);
  25529. SSL_CTX_free(ctx);
  25530. printf(resultFmt, passed);
  25531. #endif
  25532. }
  25533. static void test_wolfSSL_ctrl(void)
  25534. {
  25535. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  25536. byte buff[6000];
  25537. BIO* bio;
  25538. int bytes;
  25539. BUF_MEM* ptr = NULL;
  25540. printf(testingFmt, "wolfSSL_crtl()");
  25541. bytes = sizeof(buff);
  25542. bio = BIO_new_mem_buf((void*)buff, bytes);
  25543. AssertNotNull(bio);
  25544. AssertNotNull(BIO_s_socket());
  25545. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  25546. /* needs tested after stubs filled out @TODO
  25547. SSL_ctrl
  25548. SSL_CTX_ctrl
  25549. */
  25550. BIO_free(bio);
  25551. printf(resultFmt, passed);
  25552. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  25553. }
  25554. static void test_wolfSSL_EVP_PKEY_new_mac_key(void)
  25555. {
  25556. #ifdef OPENSSL_EXTRA
  25557. static const unsigned char pw[] = "password";
  25558. static const int pwSz = sizeof(pw) - 1;
  25559. size_t checkPwSz = 0;
  25560. const unsigned char* checkPw = NULL;
  25561. WOLFSSL_EVP_PKEY* key = NULL;
  25562. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  25563. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  25564. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  25565. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  25566. if (key) {
  25567. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  25568. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  25569. AssertIntEQ(key->pkey_sz, pwSz);
  25570. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  25571. }
  25572. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  25573. AssertIntEQ((int)checkPwSz, pwSz);
  25574. if (checkPw) {
  25575. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  25576. }
  25577. wolfSSL_EVP_PKEY_free(key);
  25578. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  25579. if (key) {
  25580. AssertIntEQ(key->pkey_sz, 0);
  25581. }
  25582. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  25583. (void)checkPw;
  25584. AssertIntEQ((int)checkPwSz, 0);
  25585. wolfSSL_EVP_PKEY_free(key);
  25586. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  25587. if (key) {
  25588. AssertIntEQ(key->pkey_sz, 0);
  25589. }
  25590. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  25591. (void)checkPw;
  25592. AssertIntEQ((int)checkPwSz, 0);
  25593. wolfSSL_EVP_PKEY_free(key);
  25594. printf(resultFmt, passed);
  25595. #endif /* OPENSSL_EXTRA */
  25596. }
  25597. static void test_wolfSSL_EVP_Digest(void)
  25598. {
  25599. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  25600. const char* in = "abc";
  25601. int inLen = (int)XSTRLEN(in);
  25602. byte out[WC_SHA256_DIGEST_SIZE];
  25603. unsigned int outLen;
  25604. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  25605. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  25606. "\x15\xAD";
  25607. printf(testingFmt, "wolfSSL_EVP_Digest()");
  25608. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  25609. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  25610. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  25611. printf(resultFmt, passed);
  25612. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  25613. }
  25614. static void test_wolfSSL_EVP_Digest_all(void)
  25615. {
  25616. #ifdef OPENSSL_EXTRA
  25617. const char* digests[] = {
  25618. #ifndef NO_MD5
  25619. "MD5",
  25620. #endif
  25621. #ifndef NO_SHA
  25622. "SHA",
  25623. #endif
  25624. #ifdef WOLFSSL_SHA224
  25625. "SHA224",
  25626. #endif
  25627. #ifndef NO_SHA256
  25628. "SHA256",
  25629. #endif
  25630. #ifdef WOLFSSL_SHA384
  25631. "SHA384",
  25632. #endif
  25633. #ifdef WOLFSSL_SHA512
  25634. "SHA512",
  25635. #endif
  25636. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  25637. "SHA512_224",
  25638. #endif
  25639. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  25640. "SHA512_256",
  25641. #endif
  25642. #ifdef WOLFSSL_SHA3
  25643. #ifndef WOLFSSL_NOSHA3_224
  25644. "SHA3_224",
  25645. #endif
  25646. #ifndef WOLFSSL_NOSHA3_256
  25647. "SHA3_256",
  25648. #endif
  25649. "SHA3_384",
  25650. #ifndef WOLFSSL_NOSHA3_512
  25651. "SHA3_512",
  25652. #endif
  25653. #endif /* WOLFSSL_SHA3 */
  25654. NULL
  25655. };
  25656. const char** d;
  25657. const unsigned char in[] = "abc";
  25658. int inLen = XSTR_SIZEOF(in);
  25659. byte out[WC_MAX_DIGEST_SIZE];
  25660. unsigned int outLen;
  25661. printf(testingFmt, "wolfSSL_EVP_Digest_all");
  25662. for (d = digests; *d != NULL; d++) {
  25663. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  25664. AssertIntGT(outLen, 0);
  25665. AssertIntEQ(EVP_MD_size(*d), outLen);
  25666. }
  25667. printf(resultFmt, passed);
  25668. #endif
  25669. }
  25670. static void test_wolfSSL_EVP_MD_size(void)
  25671. {
  25672. #ifdef OPENSSL_EXTRA
  25673. WOLFSSL_EVP_MD_CTX mdCtx;
  25674. printf(testingFmt, "wolfSSL_EVP_MD_size()");
  25675. #ifdef WOLFSSL_SHA3
  25676. #ifndef WOLFSSL_NOSHA3_224
  25677. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25678. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  25679. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  25680. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  25681. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25682. #endif
  25683. #ifndef WOLFSSL_NOSHA3_256
  25684. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25685. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  25686. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  25687. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  25688. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25689. #endif
  25690. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25691. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  25692. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  25693. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  25694. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25695. #ifndef WOLFSSL_NOSHA3_512
  25696. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25697. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  25698. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  25699. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  25700. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25701. #endif
  25702. #endif /* WOLFSSL_SHA3 */
  25703. #ifndef NO_SHA256
  25704. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25705. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  25706. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  25707. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  25708. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  25709. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  25710. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25711. #endif
  25712. #ifndef NO_MD5
  25713. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25714. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  25715. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  25716. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  25717. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  25718. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  25719. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25720. #endif
  25721. #ifdef WOLFSSL_SHA224
  25722. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25723. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  25724. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  25725. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  25726. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  25727. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  25728. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25729. #endif
  25730. #ifdef WOLFSSL_SHA384
  25731. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25732. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  25733. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  25734. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  25735. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  25736. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  25737. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25738. #endif
  25739. #ifdef WOLFSSL_SHA512
  25740. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25741. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  25742. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  25743. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  25744. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  25745. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  25746. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25747. #endif
  25748. #ifndef NO_SHA
  25749. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25750. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  25751. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  25752. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  25753. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  25754. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  25755. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25756. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25757. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  25758. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  25759. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  25760. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  25761. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  25762. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25763. #endif
  25764. /* error case */
  25765. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25766. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  25767. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  25768. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  25769. /* Cleanup is valid on uninit'ed struct */
  25770. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25771. printf(resultFmt, passed);
  25772. #endif /* OPENSSL_EXTRA */
  25773. }
  25774. static void test_wolfSSL_EVP_MD_pkey_type(void)
  25775. {
  25776. #ifdef OPENSSL_EXTRA
  25777. const WOLFSSL_EVP_MD* md;
  25778. printf(testingFmt, "test_wolfSSL_EVP_MD_pkey_type()");
  25779. #ifndef NO_MD5
  25780. AssertNotNull(md = EVP_md5());
  25781. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  25782. #endif
  25783. #ifndef NO_SHA
  25784. AssertNotNull(md = EVP_sha1());
  25785. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  25786. #endif
  25787. #ifdef WOLFSSL_SHA224
  25788. AssertNotNull(md = EVP_sha224());
  25789. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  25790. #endif
  25791. AssertNotNull(md = EVP_sha256());
  25792. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  25793. #ifdef WOLFSSL_SHA384
  25794. AssertNotNull(md = EVP_sha384());
  25795. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  25796. #endif
  25797. #ifdef WOLFSSL_SHA512
  25798. AssertNotNull(md = EVP_sha512());
  25799. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  25800. #endif
  25801. printf(resultFmt, passed);
  25802. #endif
  25803. }
  25804. #ifdef OPENSSL_EXTRA
  25805. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  25806. size_t testKeySz, const char* testData, size_t testDataSz,
  25807. const byte* testResult, size_t testResultSz)
  25808. {
  25809. unsigned char check[WC_MAX_DIGEST_SIZE];
  25810. size_t checkSz = -1;
  25811. WOLFSSL_EVP_PKEY* key;
  25812. WOLFSSL_EVP_MD_CTX mdCtx;
  25813. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  25814. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  25815. testKey, (int)testKeySz));
  25816. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25817. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  25818. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  25819. (unsigned int)testDataSz), 1);
  25820. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  25821. AssertIntEQ((int)checkSz, (int)testResultSz);
  25822. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  25823. AssertIntEQ((int)checkSz,(int)testResultSz);
  25824. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  25825. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25826. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  25827. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  25828. (unsigned int)testDataSz), 1);
  25829. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  25830. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25831. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  25832. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  25833. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  25834. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  25835. AssertIntEQ((int)checkSz, (int)testResultSz);
  25836. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  25837. AssertIntEQ((int)checkSz,(int)testResultSz);
  25838. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  25839. (unsigned int)testDataSz - 4), 1);
  25840. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  25841. AssertIntEQ((int)checkSz,(int)testResultSz);
  25842. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  25843. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25844. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  25845. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  25846. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  25847. (unsigned int)testDataSz - 4), 1);
  25848. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  25849. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  25850. wolfSSL_EVP_PKEY_free(key);
  25851. }
  25852. #endif
  25853. static void test_wolfSSL_EVP_MD_hmac_signing(void)
  25854. {
  25855. #ifdef OPENSSL_EXTRA
  25856. static const unsigned char testKey[] =
  25857. {
  25858. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  25859. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  25860. 0x0b, 0x0b, 0x0b, 0x0b
  25861. };
  25862. static const char testData[] = "Hi There";
  25863. #ifdef WOLFSSL_SHA224
  25864. static const unsigned char testResultSha224[] =
  25865. {
  25866. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  25867. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  25868. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  25869. 0x53, 0x68, 0x4b, 0x22
  25870. };
  25871. #endif
  25872. #ifndef NO_SHA256
  25873. static const unsigned char testResultSha256[] =
  25874. {
  25875. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  25876. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  25877. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  25878. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  25879. };
  25880. #endif
  25881. #ifdef WOLFSSL_SHA384
  25882. static const unsigned char testResultSha384[] =
  25883. {
  25884. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  25885. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  25886. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  25887. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  25888. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  25889. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  25890. };
  25891. #endif
  25892. #ifdef WOLFSSL_SHA512
  25893. static const unsigned char testResultSha512[] =
  25894. {
  25895. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  25896. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  25897. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  25898. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  25899. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  25900. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  25901. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  25902. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  25903. };
  25904. #endif
  25905. #ifdef WOLFSSL_SHA3
  25906. #ifndef WOLFSSL_NOSHA3_224
  25907. static const unsigned char testResultSha3_224[] =
  25908. {
  25909. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  25910. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  25911. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  25912. 0xf3, 0xc8, 0x60, 0xf7
  25913. };
  25914. #endif
  25915. #ifndef WOLFSSL_NOSHA3_256
  25916. static const unsigned char testResultSha3_256[] =
  25917. {
  25918. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  25919. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  25920. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  25921. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  25922. };
  25923. #endif
  25924. #ifndef WOLFSSL_NOSHA3_384
  25925. static const unsigned char testResultSha3_384[] =
  25926. {
  25927. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  25928. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  25929. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  25930. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  25931. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  25932. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  25933. };
  25934. #endif
  25935. #ifndef WOLFSSL_NOSHA3_512
  25936. static const unsigned char testResultSha3_512[] =
  25937. {
  25938. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  25939. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  25940. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  25941. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  25942. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  25943. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  25944. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  25945. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  25946. };
  25947. #endif
  25948. #endif
  25949. #ifndef NO_SHA256
  25950. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  25951. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  25952. #endif
  25953. #ifdef WOLFSSL_SHA224
  25954. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  25955. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  25956. #endif
  25957. #ifdef WOLFSSL_SHA384
  25958. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  25959. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  25960. #endif
  25961. #ifdef WOLFSSL_SHA512
  25962. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  25963. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  25964. #endif
  25965. #ifdef WOLFSSL_SHA3
  25966. #ifndef WOLFSSL_NOSHA3_224
  25967. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  25968. testData, XSTRLEN(testData), testResultSha3_224,
  25969. sizeof(testResultSha3_224));
  25970. #endif
  25971. #ifndef WOLFSSL_NOSHA3_256
  25972. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  25973. testData, XSTRLEN(testData), testResultSha3_256,
  25974. sizeof(testResultSha3_256));
  25975. #endif
  25976. #ifndef WOLFSSL_NOSHA3_384
  25977. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  25978. testData, XSTRLEN(testData), testResultSha3_384,
  25979. sizeof(testResultSha3_384));
  25980. #endif
  25981. #ifndef WOLFSSL_NOSHA3_512
  25982. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  25983. testData, XSTRLEN(testData), testResultSha3_512,
  25984. sizeof(testResultSha3_512));
  25985. #endif
  25986. #endif
  25987. printf(resultFmt, passed);
  25988. #endif /* OPENSSL_EXTRA */
  25989. }
  25990. static void test_wolfSSL_EVP_MD_rsa_signing(void)
  25991. {
  25992. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  25993. defined(USE_CERT_BUFFERS_2048)
  25994. WOLFSSL_EVP_PKEY* privKey;
  25995. WOLFSSL_EVP_PKEY* pubKey;
  25996. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  25997. const char testData[] = "Hi There";
  25998. WOLFSSL_EVP_MD_CTX mdCtx;
  25999. size_t checkSz = -1;
  26000. int sz = 2048 / 8;
  26001. const unsigned char* cp;
  26002. const unsigned char* p;
  26003. unsigned char check[2048/8];
  26004. size_t i;
  26005. int paddings[] = {
  26006. RSA_PKCS1_PADDING,
  26007. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  26008. RSA_PKCS1_PSS_PADDING,
  26009. #endif
  26010. };
  26011. printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
  26012. cp = client_key_der_2048;
  26013. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  26014. sizeof_client_key_der_2048)));
  26015. p = client_keypub_der_2048;
  26016. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  26017. sizeof_client_keypub_der_2048)));
  26018. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26019. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  26020. NULL, privKey), 1);
  26021. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  26022. (unsigned int)XSTRLEN(testData)), 1);
  26023. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  26024. AssertIntEQ((int)checkSz, sz);
  26025. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  26026. AssertIntEQ((int)checkSz,sz);
  26027. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26028. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26029. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  26030. NULL, pubKey), 1);
  26031. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  26032. (unsigned int)XSTRLEN(testData)),
  26033. 1);
  26034. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  26035. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26036. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26037. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  26038. NULL, privKey), 1);
  26039. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  26040. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  26041. AssertIntEQ((int)checkSz, sz);
  26042. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  26043. AssertIntEQ((int)checkSz, sz);
  26044. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  26045. (unsigned int)XSTRLEN(testData) - 4), 1);
  26046. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  26047. AssertIntEQ((int)checkSz, sz);
  26048. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26049. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26050. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  26051. NULL, pubKey), 1);
  26052. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  26053. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  26054. (unsigned int)XSTRLEN(testData) - 4),
  26055. 1);
  26056. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  26057. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26058. /* Check all signing padding types */
  26059. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  26060. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26061. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  26062. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  26063. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  26064. paddings[i]), 1);
  26065. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  26066. (unsigned int)XSTRLEN(testData)), 1);
  26067. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  26068. AssertIntEQ((int)checkSz, sz);
  26069. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  26070. AssertIntEQ((int)checkSz,sz);
  26071. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26072. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26073. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  26074. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  26075. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  26076. paddings[i]), 1);
  26077. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  26078. (unsigned int)XSTRLEN(testData)), 1);
  26079. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  26080. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26081. }
  26082. wolfSSL_EVP_PKEY_free(pubKey);
  26083. wolfSSL_EVP_PKEY_free(privKey);
  26084. printf(resultFmt, passed);
  26085. #endif
  26086. }
  26087. static void test_wolfSSL_EVP_MD_ecc_signing(void)
  26088. {
  26089. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  26090. WOLFSSL_EVP_PKEY* privKey;
  26091. WOLFSSL_EVP_PKEY* pubKey;
  26092. const char testData[] = "Hi There";
  26093. WOLFSSL_EVP_MD_CTX mdCtx;
  26094. size_t checkSz = -1;
  26095. const unsigned char* cp;
  26096. const unsigned char* p;
  26097. unsigned char check[2048/8];
  26098. printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
  26099. cp = ecc_clikey_der_256;
  26100. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  26101. sizeof_ecc_clikey_der_256);
  26102. AssertNotNull(privKey);
  26103. p = ecc_clikeypub_der_256;
  26104. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  26105. sizeof_ecc_clikeypub_der_256)));
  26106. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26107. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  26108. NULL, privKey), 1);
  26109. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  26110. (unsigned int)XSTRLEN(testData)), 1);
  26111. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  26112. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  26113. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26114. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26115. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  26116. NULL, pubKey), 1);
  26117. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  26118. (unsigned int)XSTRLEN(testData)),
  26119. 1);
  26120. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  26121. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26122. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26123. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  26124. NULL, privKey), 1);
  26125. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  26126. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  26127. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  26128. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  26129. (unsigned int)XSTRLEN(testData) - 4), 1);
  26130. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  26131. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26132. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  26133. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  26134. NULL, pubKey), 1);
  26135. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  26136. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  26137. (unsigned int)XSTRLEN(testData) - 4),
  26138. 1);
  26139. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  26140. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  26141. wolfSSL_EVP_PKEY_free(pubKey);
  26142. wolfSSL_EVP_PKEY_free(privKey);
  26143. printf(resultFmt, passed);
  26144. #endif
  26145. }
  26146. static void test_wolfSSL_CTX_add_extra_chain_cert(void)
  26147. {
  26148. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26149. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26150. char caFile[] = "./certs/client-ca.pem";
  26151. char clientFile[] = "./certs/client-cert.pem";
  26152. SSL_CTX* ctx;
  26153. X509* x509;
  26154. BIO *bio = NULL;
  26155. X509 *cert = NULL;
  26156. X509 *ca;
  26157. STACK_OF(X509) *chain = NULL;
  26158. STACK_OF(X509) *chain2 = NULL;
  26159. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  26160. #ifndef NO_WOLFSSL_SERVER
  26161. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26162. #else
  26163. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26164. #endif
  26165. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  26166. AssertNotNull(x509);
  26167. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  26168. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  26169. AssertNotNull(x509);
  26170. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  26171. /* additional test of getting EVP_PKEY key size from X509
  26172. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  26173. * allowed with user RSA */
  26174. {
  26175. EVP_PKEY* pkey;
  26176. #if defined(HAVE_ECC)
  26177. X509* ecX509;
  26178. #endif /* HAVE_ECC */
  26179. AssertNotNull(pkey = X509_get_pubkey(x509));
  26180. /* current RSA key is 2048 bit (256 bytes) */
  26181. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  26182. EVP_PKEY_free(pkey);
  26183. #if defined(HAVE_ECC)
  26184. #if defined(USE_CERT_BUFFERS_256)
  26185. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  26186. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  26187. SSL_FILETYPE_ASN1));
  26188. #else
  26189. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  26190. SSL_FILETYPE_PEM));
  26191. #endif
  26192. pkey = X509_get_pubkey(ecX509);
  26193. AssertNotNull(pkey);
  26194. /* current ECC key is 256 bit (32 bytes) */
  26195. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  26196. X509_free(ecX509);
  26197. EVP_PKEY_free(pkey);
  26198. #endif /* HAVE_ECC */
  26199. }
  26200. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  26201. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  26202. #ifdef WOLFSSL_ENCRYPTED_KEYS
  26203. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  26204. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  26205. #endif
  26206. SSL_CTX_free(ctx);
  26207. #ifndef NO_WOLFSSL_SERVER
  26208. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26209. #else
  26210. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26211. #endif
  26212. /* Test haproxy use case */
  26213. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  26214. /* Read Certificate */
  26215. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  26216. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  26217. AssertNotNull(chain = sk_X509_new_null());
  26218. AssertIntEQ(sk_X509_push(chain, ca), 1);
  26219. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  26220. AssertNotNull(ca = sk_X509_shift(chain2));
  26221. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  26222. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  26223. BIO_free(bio);
  26224. X509_free(cert);
  26225. sk_X509_pop_free(chain, X509_free);
  26226. sk_X509_pop_free(chain2, X509_free);
  26227. SSL_CTX_free(ctx);
  26228. printf(resultFmt, passed);
  26229. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26230. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  26231. }
  26232. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  26233. static void test_wolfSSL_ERR_peek_last_error_line(void)
  26234. {
  26235. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26236. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  26237. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  26238. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  26239. tcp_ready ready;
  26240. func_args client_args;
  26241. func_args server_args;
  26242. #ifndef SINGLE_THREADED
  26243. THREAD_TYPE serverThread;
  26244. #endif
  26245. callback_functions client_cb;
  26246. callback_functions server_cb;
  26247. int line = 0;
  26248. int flag = ERR_TXT_STRING;
  26249. const char* file = NULL;
  26250. const char* data = NULL;
  26251. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  26252. /* create a failed connection and inspect the error */
  26253. #ifdef WOLFSSL_TIRTOS
  26254. fdOpenSession(Task_self());
  26255. #endif
  26256. XMEMSET(&client_args, 0, sizeof(func_args));
  26257. XMEMSET(&server_args, 0, sizeof(func_args));
  26258. StartTCP();
  26259. InitTcpReady(&ready);
  26260. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  26261. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  26262. client_cb.method = wolfTLSv1_1_client_method;
  26263. server_cb.method = wolfTLSv1_2_server_method;
  26264. server_args.signal = &ready;
  26265. server_args.callbacks = &server_cb;
  26266. client_args.signal = &ready;
  26267. client_args.callbacks = &client_cb;
  26268. #ifndef SINGLE_THREADED
  26269. start_thread(test_server_nofail, &server_args, &serverThread);
  26270. wait_tcp_ready(&server_args);
  26271. test_client_nofail(&client_args, NULL);
  26272. join_thread(serverThread);
  26273. #endif
  26274. FreeTcpReady(&ready);
  26275. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  26276. AssertNotNull(data);
  26277. /* check clearing error state */
  26278. ERR_remove_state(0);
  26279. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  26280. ERR_peek_last_error_line(NULL, &line);
  26281. AssertIntEQ(line, 0);
  26282. ERR_peek_last_error_line(&file, NULL);
  26283. AssertNull(file);
  26284. /* retry connection to fill error queue */
  26285. XMEMSET(&client_args, 0, sizeof(func_args));
  26286. XMEMSET(&server_args, 0, sizeof(func_args));
  26287. StartTCP();
  26288. InitTcpReady(&ready);
  26289. client_cb.method = wolfTLSv1_1_client_method;
  26290. server_cb.method = wolfTLSv1_2_server_method;
  26291. server_args.signal = &ready;
  26292. server_args.callbacks = &server_cb;
  26293. client_args.signal = &ready;
  26294. client_args.callbacks = &client_cb;
  26295. start_thread(test_server_nofail, &server_args, &serverThread);
  26296. wait_tcp_ready(&server_args);
  26297. test_client_nofail(&client_args, NULL);
  26298. join_thread(serverThread);
  26299. FreeTcpReady(&ready);
  26300. /* check that error code was stored */
  26301. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  26302. ERR_peek_last_error_line(NULL, &line);
  26303. AssertIntNE(line, 0);
  26304. ERR_peek_last_error_line(&file, NULL);
  26305. AssertNotNull(file);
  26306. #ifdef WOLFSSL_TIRTOS
  26307. fdOpenSession(Task_self());
  26308. #endif
  26309. printf(resultFmt, passed);
  26310. printf("\nTesting error print out\n");
  26311. ERR_print_errors_fp(stdout);
  26312. printf("Done testing print out\n\n");
  26313. fflush(stdout);
  26314. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26315. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  26316. }
  26317. #endif
  26318. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26319. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26320. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  26321. {
  26322. (void) ok;
  26323. (void) ctx;
  26324. printf("ENTER verify_cb\n");
  26325. return SSL_SUCCESS;
  26326. }
  26327. #endif
  26328. static void test_wolfSSL_X509_Name_canon(void)
  26329. {
  26330. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  26331. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  26332. defined(WOLFSSL_CERT_GEN) && \
  26333. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  26334. const long ex_hash1 = 0x0fdb2da4;
  26335. const long ex_hash2 = 0x9f3e8c9e;
  26336. X509_NAME *name = NULL;
  26337. X509 *x509 = NULL;
  26338. FILE* file = NULL;
  26339. unsigned long hash = 0;
  26340. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  26341. byte *pbuf = NULL;
  26342. word32 len = 0;
  26343. (void) ex_hash2;
  26344. printf(testingFmt, "test_wolfSSL_X509_Name_canon()");
  26345. file = XFOPEN(caCertFile, "rb");
  26346. AssertNotNull(file);
  26347. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  26348. AssertNotNull(name = X509_get_issuer_name(x509));
  26349. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  26350. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  26351. hash = (((unsigned long)digest[3] << 24) |
  26352. ((unsigned long)digest[2] << 16) |
  26353. ((unsigned long)digest[1] << 8) |
  26354. ((unsigned long)digest[0]));
  26355. AssertIntEQ(hash, ex_hash1);
  26356. XFCLOSE(file);
  26357. X509_free(x509);
  26358. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  26359. pbuf = NULL;
  26360. file = XFOPEN(cliCertFile, "rb");
  26361. AssertNotNull(file);
  26362. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  26363. AssertNotNull(name = X509_get_issuer_name(x509));
  26364. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  26365. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  26366. hash = (((unsigned long)digest[3] << 24) |
  26367. ((unsigned long)digest[2] << 16) |
  26368. ((unsigned long)digest[1] << 8) |
  26369. ((unsigned long)digest[0]));
  26370. AssertIntEQ(hash, ex_hash2);
  26371. XFCLOSE(file);
  26372. X509_free(x509);
  26373. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  26374. printf(resultFmt, passed);
  26375. #endif
  26376. }
  26377. static void test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  26378. {
  26379. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  26380. const int MAX_DIR = 4;
  26381. const char paths[][32] = {
  26382. "./certs/ed25519",
  26383. "./certs/ecc",
  26384. "./certs/crl",
  26385. "./certs/",
  26386. };
  26387. char CertCrl_path[MAX_FILENAME_SZ];
  26388. char *p;
  26389. X509_STORE* str;
  26390. X509_LOOKUP* lookup;
  26391. WOLFSSL_STACK* sk = NULL;
  26392. int len, total_len, i;
  26393. (void) sk;
  26394. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_hash_dir()");
  26395. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  26396. /* illegal string */
  26397. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  26398. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  26399. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  26400. SSL_FILETYPE_PEM,NULL), 0);
  26401. /* free store */
  26402. X509_STORE_free(str);
  26403. /* short folder string */
  26404. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  26405. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  26406. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  26407. SSL_FILETYPE_PEM,NULL), 1);
  26408. #if defined(WOLFSSL_INT_H)
  26409. /* only available when including internal.h */
  26410. AssertNotNull(sk = lookup->dirs->dir_entry);
  26411. #endif
  26412. /* free store */
  26413. X509_STORE_free(str);
  26414. /* typical function check */
  26415. p = &CertCrl_path[0];
  26416. total_len = 0;
  26417. for(i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  26418. len = (int)XSTRLEN((const char*)&paths[i]);
  26419. total_len += len;
  26420. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  26421. p += len;
  26422. if (i != 0) *(p++) = SEPARATOR_CHAR;
  26423. }
  26424. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  26425. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  26426. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  26427. SSL_FILETYPE_PEM,NULL), 1);
  26428. #if defined(WOLFSSL_INT_H)
  26429. /* only available when including internal.h */
  26430. AssertNotNull(sk = lookup->dirs->dir_entry);
  26431. #endif
  26432. X509_STORE_free(str);
  26433. printf(resultFmt, passed);
  26434. #endif
  26435. }
  26436. static void test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  26437. {
  26438. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  26439. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  26440. defined(WOLFSSL_SIGNER_DER_CERT)
  26441. X509_STORE_CTX* ctx;
  26442. X509_STORE* str;
  26443. X509_LOOKUP* lookup;
  26444. X509* cert1;
  26445. X509* x509Ca;
  26446. X509* x509Svr;
  26447. X509* issuer;
  26448. WOLFSSL_STACK* sk = NULL;
  26449. X509_NAME* caName;
  26450. X509_NAME* issuerName;
  26451. FILE* file1 = NULL;
  26452. int i, cert_count, cmp;
  26453. char der[] = "certs/ca-cert.der";
  26454. #ifdef HAVE_CRL
  26455. char pem[][100] = {
  26456. "./certs/crl/crl.pem",
  26457. "./certs/crl/crl2.pem",
  26458. "./certs/crl/caEccCrl.pem",
  26459. "./certs/crl/eccCliCRL.pem",
  26460. "./certs/crl/eccSrvCRL.pem",
  26461. ""
  26462. };
  26463. #endif
  26464. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_file()");
  26465. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  26466. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  26467. fclose(file1);
  26468. AssertNotNull(ctx = X509_STORE_CTX_new());
  26469. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  26470. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  26471. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  26472. SSL_FILETYPE_PEM,NULL), 1);
  26473. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  26474. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  26475. /* check if CA cert is loaded into the store */
  26476. for (i = 0; i < cert_count; i++) {
  26477. x509Ca = sk_X509_value(sk, i);
  26478. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  26479. }
  26480. AssertNotNull((x509Svr =
  26481. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  26482. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  26483. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  26484. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  26485. AssertNotNull(issuer);
  26486. caName = X509_get_subject_name(x509Ca);
  26487. AssertNotNull(caName);
  26488. issuerName = X509_get_subject_name(issuer);
  26489. AssertNotNull(issuerName);
  26490. cmp = X509_NAME_cmp(caName, issuerName);
  26491. AssertIntEQ(cmp, 0);
  26492. /* load der format */
  26493. X509_free(issuer);
  26494. X509_STORE_CTX_free(ctx);
  26495. X509_STORE_free(str);
  26496. sk_X509_free(sk);
  26497. X509_free(x509Svr);
  26498. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  26499. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  26500. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  26501. SSL_FILETYPE_ASN1,NULL), 1);
  26502. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  26503. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  26504. /* check if CA cert is loaded into the store */
  26505. for (i = 0; i < cert_count; i++) {
  26506. x509Ca = sk_X509_value(sk, i);
  26507. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  26508. }
  26509. X509_STORE_free(str);
  26510. sk_X509_free(sk);
  26511. X509_free(cert1);
  26512. #ifdef HAVE_CRL
  26513. AssertNotNull(str = wolfSSL_X509_STORE_new());
  26514. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  26515. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  26516. SSL_FILETYPE_PEM,NULL), 1);
  26517. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  26518. "certs/server-revoked-cert.pem",
  26519. SSL_FILETYPE_PEM,NULL), 1);
  26520. if (str) {
  26521. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  26522. WOLFSSL_FILETYPE_PEM), 1);
  26523. /* since store hasn't yet known the revoked cert*/
  26524. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  26525. "certs/server-revoked-cert.pem",
  26526. WOLFSSL_FILETYPE_PEM), 1);
  26527. }
  26528. for (i = 0; pem[i][0] != '\0'; i++)
  26529. {
  26530. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  26531. SSL_FILETYPE_PEM, NULL), 1);
  26532. }
  26533. if (str) {
  26534. /* since store knows crl list */
  26535. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  26536. "certs/server-revoked-cert.pem",
  26537. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  26538. }
  26539. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  26540. X509_STORE_free(str);
  26541. #endif
  26542. printf(resultFmt, passed);
  26543. #endif
  26544. }
  26545. static void test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  26546. {
  26547. #if defined(OPENSSL_EXTRA)
  26548. printf(testingFmt, "test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup()");
  26549. X509_STORE_CTX_cleanup(NULL);
  26550. X509_STORE_CTX_trusted_stack(NULL, NULL);
  26551. AssertTrue(1); /* to confirm previous call gives no harm */
  26552. printf(resultFmt, passed);
  26553. #endif
  26554. }
  26555. static void test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  26556. {
  26557. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  26558. #ifdef WOLFSSL_SIGNER_DER_CERT
  26559. int cmp;
  26560. #endif
  26561. X509_STORE_CTX* ctx;
  26562. X509_STORE* str;
  26563. X509* x509Ca;
  26564. X509* x509Svr;
  26565. X509* issuer;
  26566. X509_NAME* caName;
  26567. X509_NAME* issuerName;
  26568. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
  26569. AssertNotNull(ctx = X509_STORE_CTX_new());
  26570. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  26571. AssertNotNull((x509Ca =
  26572. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  26573. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  26574. AssertNotNull((x509Svr =
  26575. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  26576. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  26577. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  26578. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  26579. AssertNotNull(issuer);
  26580. caName = X509_get_subject_name(x509Ca);
  26581. AssertNotNull(caName);
  26582. issuerName = X509_get_subject_name(issuer);
  26583. #ifdef WOLFSSL_SIGNER_DER_CERT
  26584. AssertNotNull(issuerName);
  26585. cmp = X509_NAME_cmp(caName, issuerName);
  26586. AssertIntEQ(cmp, 0);
  26587. #else
  26588. AssertNotNull(issuerName);
  26589. #endif
  26590. X509_free(issuer);
  26591. X509_STORE_CTX_free(ctx);
  26592. X509_free(x509Svr);
  26593. X509_STORE_free(str);
  26594. X509_free(x509Ca);
  26595. printf(resultFmt, passed);
  26596. #endif
  26597. }
  26598. static void test_wolfSSL_PKCS7_certs(void)
  26599. {
  26600. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  26601. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  26602. STACK_OF(X509)* sk = NULL;
  26603. STACK_OF(X509_INFO)* info_sk = NULL;
  26604. PKCS7 *p7 = NULL;
  26605. BIO* bio;
  26606. const byte* p = NULL;
  26607. int buflen = 0;
  26608. int i;
  26609. printf(testingFmt, "wolfSSL_PKCS7_certs()");
  26610. /* Test twice. Once with d2i and once without to test
  26611. * that everything is free'd correctly. */
  26612. for (i = 0; i < 2; i++) {
  26613. AssertNotNull(p7 = PKCS7_new());
  26614. p7->version = 1;
  26615. p7->hashOID = SHAh;
  26616. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26617. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26618. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  26619. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  26620. AssertNotNull(sk = sk_X509_new_null());
  26621. while (sk_X509_INFO_num(info_sk)) {
  26622. X509_INFO* info;
  26623. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  26624. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  26625. info->x509 = NULL;
  26626. X509_INFO_free(info);
  26627. }
  26628. sk_X509_INFO_free(info_sk);
  26629. BIO_free(bio);
  26630. bio = BIO_new(BIO_s_mem());
  26631. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  26632. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  26633. if (i == 0) {
  26634. PKCS7_free(p7);
  26635. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  26636. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  26637. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  26638. /* PKCS7_free free's the certs */
  26639. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  26640. }
  26641. BIO_free(bio);
  26642. PKCS7_free(p7);
  26643. }
  26644. printf(resultFmt, passed);
  26645. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  26646. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  26647. }
  26648. static void test_wolfSSL_X509_STORE_CTX(void)
  26649. {
  26650. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26651. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26652. X509_STORE_CTX* ctx;
  26653. X509_STORE* str;
  26654. X509* x509;
  26655. #ifdef OPENSSL_ALL
  26656. X509* x5092;
  26657. STACK_OF(X509) *sk, *sk2, *sk3;
  26658. #endif
  26659. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  26660. AssertNotNull(ctx = X509_STORE_CTX_new());
  26661. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  26662. AssertNotNull((x509 =
  26663. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  26664. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  26665. #ifdef OPENSSL_ALL
  26666. /* sk_X509_new only in OPENSSL_ALL */
  26667. sk = sk_X509_new();
  26668. AssertNotNull(sk);
  26669. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  26670. #else
  26671. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  26672. #endif
  26673. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  26674. X509_STORE_CTX_set_error(ctx, -5);
  26675. X509_STORE_CTX_set_error(NULL, -5);
  26676. X509_STORE_CTX_free(ctx);
  26677. #ifdef OPENSSL_ALL
  26678. sk_X509_free(sk);
  26679. #endif
  26680. X509_STORE_free(str);
  26681. X509_free(x509);
  26682. AssertNotNull(ctx = X509_STORE_CTX_new());
  26683. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  26684. X509_STORE_CTX_free(ctx);
  26685. #ifdef OPENSSL_ALL
  26686. /* test X509_STORE_CTX_get(1)_chain */
  26687. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  26688. SSL_FILETYPE_PEM)));
  26689. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  26690. SSL_FILETYPE_PEM)));
  26691. AssertNotNull((sk = sk_X509_new()));
  26692. AssertIntEQ(sk_X509_push(sk, x509), 1);
  26693. AssertNotNull((str = X509_STORE_new()));
  26694. AssertNotNull((ctx = X509_STORE_CTX_new()));
  26695. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  26696. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  26697. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  26698. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  26699. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  26700. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  26701. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  26702. X509_STORE_CTX_free(ctx);
  26703. X509_STORE_free(str);
  26704. /* CTX certs not freed yet */
  26705. X509_free(x5092);
  26706. sk_X509_free(sk);
  26707. /* sk3 is dup so free here */
  26708. sk_X509_free(sk3);
  26709. #endif
  26710. /* test X509_STORE_CTX_get/set_ex_data */
  26711. {
  26712. int i = 0, tmpData = 5;
  26713. void* tmpDataRet;
  26714. AssertNotNull(ctx = X509_STORE_CTX_new());
  26715. #if defined(HAVE_EX_DATA) || defined(FORTRESS)
  26716. for (i = 0; i < MAX_EX_DATA; i++) {
  26717. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  26718. WOLFSSL_SUCCESS);
  26719. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  26720. AssertNotNull(tmpDataRet);
  26721. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  26722. }
  26723. #else
  26724. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  26725. WOLFSSL_FAILURE);
  26726. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  26727. AssertNull(tmpDataRet);
  26728. #endif
  26729. X509_STORE_CTX_free(ctx);
  26730. }
  26731. /* test X509_STORE_get/set_ex_data */
  26732. {
  26733. int i = 0, tmpData = 99;
  26734. void* tmpDataRet;
  26735. AssertNotNull(str = X509_STORE_new());
  26736. #if defined(HAVE_EX_DATA)
  26737. for (i = 0; i < MAX_EX_DATA; i++) {
  26738. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  26739. WOLFSSL_SUCCESS);
  26740. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  26741. AssertNotNull(tmpDataRet);
  26742. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  26743. }
  26744. #else
  26745. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  26746. WOLFSSL_FAILURE);
  26747. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  26748. AssertNull(tmpDataRet);
  26749. #endif
  26750. X509_STORE_free(str);
  26751. }
  26752. printf(resultFmt, passed);
  26753. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26754. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  26755. }
  26756. static void test_wolfSSL_X509_STORE_set_flags(void)
  26757. {
  26758. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26759. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26760. X509_STORE* store;
  26761. X509* x509;
  26762. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  26763. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  26764. AssertNotNull((x509 =
  26765. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  26766. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  26767. #ifdef HAVE_CRL
  26768. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  26769. #else
  26770. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  26771. NOT_COMPILED_IN);
  26772. #endif
  26773. wolfSSL_X509_free(x509);
  26774. wolfSSL_X509_STORE_free(store);
  26775. printf(resultFmt, passed);
  26776. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26777. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  26778. }
  26779. static void test_wolfSSL_X509_LOOKUP_load_file(void)
  26780. {
  26781. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  26782. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  26783. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  26784. WOLFSSL_X509_STORE* store;
  26785. WOLFSSL_X509_LOOKUP* lookup;
  26786. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  26787. AssertNotNull(store = wolfSSL_X509_STORE_new());
  26788. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  26789. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  26790. X509_FILETYPE_PEM), 1);
  26791. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  26792. X509_FILETYPE_PEM), 1);
  26793. if (store) {
  26794. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  26795. WOLFSSL_FILETYPE_PEM), 1);
  26796. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  26797. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  26798. }
  26799. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  26800. X509_FILETYPE_PEM), 1);
  26801. if (store) {
  26802. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  26803. WOLFSSL_FILETYPE_PEM), 1);
  26804. }
  26805. wolfSSL_X509_STORE_free(store);
  26806. printf(resultFmt, passed);
  26807. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  26808. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  26809. }
  26810. static void test_wolfSSL_X509_STORE_CTX_set_time(void)
  26811. {
  26812. #if defined(OPENSSL_EXTRA)
  26813. WOLFSSL_X509_STORE_CTX* ctx;
  26814. time_t c_time;
  26815. printf(testingFmt, "wolfSSL_X509_set_time()");
  26816. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  26817. c_time = 365*24*60*60;
  26818. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  26819. AssertTrue(
  26820. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  26821. AssertTrue(ctx->param->check_time == c_time);
  26822. wolfSSL_X509_STORE_CTX_free(ctx);
  26823. printf(resultFmt, passed);
  26824. #endif /* OPENSSL_EXTRA */
  26825. }
  26826. static void test_wolfSSL_CTX_get0_set1_param(void)
  26827. {
  26828. #if defined(OPENSSL_EXTRA)
  26829. int ret;
  26830. SSL_CTX* ctx;
  26831. WOLFSSL_X509_VERIFY_PARAM* pParam;
  26832. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  26833. char testIPv4[] = "127.0.0.1";
  26834. char testhostName[] = "foo.hoge.com";
  26835. printf(testingFmt, "wolfSSL_CTX_get0_set1_param()");
  26836. #ifndef NO_WOLFSSL_SERVER
  26837. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26838. #else
  26839. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26840. #endif
  26841. AssertNull(SSL_CTX_get0_param(NULL));
  26842. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  26843. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  26844. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  26845. AssertNotNull(pvpm);
  26846. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  26847. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  26848. (int)XSTRLEN(testhostName));
  26849. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  26850. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  26851. ret = SSL_CTX_set1_param(ctx, pvpm);
  26852. AssertIntEQ(1, ret);
  26853. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  26854. (int)XSTRLEN(testhostName)));
  26855. AssertIntEQ(0x01, pParam->hostFlags);
  26856. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  26857. /* test for incorrect patameter */
  26858. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  26859. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  26860. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  26861. SSL_CTX_free(ctx);
  26862. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  26863. printf(resultFmt, passed);
  26864. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  26865. }
  26866. static void test_wolfSSL_get0_param(void)
  26867. {
  26868. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  26869. SSL_CTX* ctx;
  26870. SSL* ssl;
  26871. WOLFSSL_X509_VERIFY_PARAM* pParam;
  26872. printf(testingFmt, "wolfSSL_get0_param()");
  26873. #ifndef NO_WOLFSSL_SERVER
  26874. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26875. #else
  26876. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26877. #endif
  26878. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  26879. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  26880. AssertNotNull(ssl = SSL_new(ctx));
  26881. pParam = SSL_get0_param(ssl);
  26882. (void)pParam;
  26883. SSL_free(ssl);
  26884. SSL_CTX_free(ctx);
  26885. printf(resultFmt, passed);
  26886. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  26887. }
  26888. static void test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  26889. {
  26890. #if defined(OPENSSL_EXTRA)
  26891. const char host[] = "www.example.com";
  26892. WOLFSSL_X509_VERIFY_PARAM* pParam;
  26893. printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
  26894. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  26895. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  26896. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  26897. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  26898. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  26899. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  26900. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  26901. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  26902. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  26903. printf(resultFmt, passed);
  26904. #endif /* OPENSSL_EXTRA */
  26905. }
  26906. static void test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  26907. {
  26908. #if defined(OPENSSL_EXTRA)
  26909. unsigned char buf[16] = {0};
  26910. WOLFSSL_X509_VERIFY_PARAM* param;
  26911. printf(testingFmt, "test_wolfSSL_X509_VERIFY_PARAM_set1_ip()");
  26912. AssertNotNull(param = X509_VERIFY_PARAM_new());
  26913. /* test 127.0.0.1 */
  26914. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  26915. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  26916. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  26917. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  26918. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  26919. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  26920. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  26921. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  26922. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  26923. AssertIntEQ(XSTRNCMP(param->ipasc,
  26924. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  26925. /* test 2001:db8:: */
  26926. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  26927. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  26928. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  26929. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  26930. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  26931. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  26932. /* test ::1234:5678 */
  26933. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  26934. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  26935. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  26936. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  26937. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  26938. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  26939. /* test 2001:db8::1234:5678 */
  26940. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  26941. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  26942. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  26943. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  26944. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  26945. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  26946. sizeof(param->ipasc)), 0);
  26947. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  26948. /* 2001:db8:1::ab9:c0a8:102 */
  26949. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  26950. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  26951. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  26952. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  26953. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  26954. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  26955. sizeof(param->ipasc)), 0);
  26956. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  26957. printf(resultFmt, passed);
  26958. #endif /* OPENSSL_EXTRA */
  26959. }
  26960. static void test_wolfSSL_X509_STORE_CTX_get0_store(void)
  26961. {
  26962. #if defined(OPENSSL_EXTRA)
  26963. X509_STORE* store;
  26964. X509_STORE_CTX* ctx;
  26965. X509_STORE_CTX* ctx_no_init;
  26966. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
  26967. AssertNotNull((store = X509_STORE_new()));
  26968. AssertNotNull(ctx = X509_STORE_CTX_new());
  26969. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  26970. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  26971. AssertNull(X509_STORE_CTX_get0_store(NULL));
  26972. /* should return NULL if ctx has not bee initialized */
  26973. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  26974. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  26975. wolfSSL_X509_STORE_CTX_free(ctx);
  26976. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  26977. X509_STORE_free(store);
  26978. printf(resultFmt, passed);
  26979. #endif /* OPENSSL_EXTRA */
  26980. }
  26981. static void test_wolfSSL_CTX_set_client_CA_list(void)
  26982. {
  26983. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  26984. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  26985. WOLFSSL_CTX* ctx;
  26986. WOLFSSL* ssl;
  26987. X509_NAME* name = NULL;
  26988. STACK_OF(X509_NAME)* names = NULL;
  26989. STACK_OF(X509_NAME)* ca_list = NULL;
  26990. int i, names_len;
  26991. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  26992. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  26993. /* Send two X501 names in cert request */
  26994. names = SSL_load_client_CA_file(cliCertFile);
  26995. AssertNotNull(names);
  26996. ca_list = SSL_load_client_CA_file(caCertFile);
  26997. AssertNotNull(ca_list);
  26998. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  26999. SSL_CTX_set_client_CA_list(ctx, names);
  27000. /* This should only free the stack structure */
  27001. sk_X509_NAME_free(ca_list);
  27002. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  27003. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  27004. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  27005. for (i=0; i<names_len; i++) {
  27006. AssertNotNull(name = sk_X509_NAME_value(names, i));
  27007. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  27008. }
  27009. /* Needed to be able to create ssl object */
  27010. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  27011. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27012. AssertNotNull(ssl = wolfSSL_new(ctx));
  27013. /* load again as old names are responsibility of ctx to free*/
  27014. names = SSL_load_client_CA_file(cliCertFile);
  27015. AssertNotNull(names);
  27016. SSL_set_client_CA_list(ssl, names);
  27017. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  27018. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  27019. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  27020. for (i=0; i<names_len; i++) {
  27021. AssertNotNull(name = sk_X509_NAME_value(names, i));
  27022. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  27023. }
  27024. printf(resultFmt, passed);
  27025. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  27026. {
  27027. tcp_ready ready;
  27028. func_args server_args;
  27029. callback_functions server_cb;
  27030. THREAD_TYPE serverThread;
  27031. WOLFSSL* ssl_client;
  27032. WOLFSSL_CTX* ctx_client;
  27033. SOCKET_T sockfd = 0;
  27034. printf(testingFmt, "wolfSSL_get_client_CA_list() with handshake");
  27035. StartTCP();
  27036. InitTcpReady(&ready);
  27037. XMEMSET(&server_args, 0, sizeof(func_args));
  27038. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  27039. server_args.signal = &ready;
  27040. server_args.callbacks = &server_cb;
  27041. /* we are responsible for free'ing WOLFSSL_CTX */
  27042. server_cb.ctx = ctx;
  27043. server_cb.isSharedCtx = 1;
  27044. AssertIntEQ(WOLFSSL_SUCCESS,
  27045. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  27046. start_thread(test_server_nofail, &server_args, &serverThread);
  27047. wait_tcp_ready(&server_args);
  27048. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  27049. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  27050. AssertIntEQ(WOLFSSL_SUCCESS,
  27051. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  27052. AssertIntEQ(WOLFSSL_SUCCESS,
  27053. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  27054. AssertIntEQ(WOLFSSL_SUCCESS,
  27055. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  27056. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  27057. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  27058. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  27059. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  27060. /* We are expecting two cert names to be sent */
  27061. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  27062. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  27063. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  27064. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  27065. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  27066. }
  27067. wolfSSL_shutdown(ssl_client);
  27068. wolfSSL_free(ssl_client);
  27069. wolfSSL_CTX_free(ctx_client);
  27070. join_thread(serverThread);
  27071. FreeTcpReady(&ready);
  27072. printf(resultFmt, passed);
  27073. }
  27074. #endif
  27075. wolfSSL_free(ssl);
  27076. wolfSSL_CTX_free(ctx);
  27077. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT && !NO_BIO */
  27078. }
  27079. static void test_wolfSSL_CTX_add_client_CA(void)
  27080. {
  27081. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  27082. !defined(NO_WOLFSSL_CLIENT)
  27083. WOLFSSL_CTX* ctx;
  27084. WOLFSSL_X509* x509;
  27085. WOLFSSL_X509* x509_a;
  27086. STACK_OF(X509_NAME)* ca_list;
  27087. int ret = 0;
  27088. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  27089. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27090. /* Add client cert */
  27091. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  27092. AssertNotNull(x509);
  27093. ret = SSL_CTX_add_client_CA(ctx, x509);
  27094. AssertIntEQ(ret, SSL_SUCCESS);
  27095. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  27096. /* Add another client cert */
  27097. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  27098. SSL_FILETYPE_PEM));
  27099. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  27100. /* test for incorrect parameter */
  27101. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  27102. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  27103. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  27104. X509_free(x509);
  27105. X509_free(x509_a);
  27106. SSL_CTX_free(ctx);
  27107. printf(resultFmt, passed);
  27108. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  27109. }
  27110. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  27111. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  27112. {
  27113. callback_functions* callbacks = ((func_args*)args)->callbacks;
  27114. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  27115. WOLFSSL* ssl = NULL;
  27116. SOCKET_T sfd = 0;
  27117. SOCKET_T cfd = 0;
  27118. word16 port;
  27119. char msg[] = "I hear you fa shizzle!";
  27120. int len = (int) XSTRLEN(msg);
  27121. char input[1024];
  27122. int idx;
  27123. int ret, err = 0;
  27124. #ifdef WOLFSSL_TIRTOS
  27125. fdOpenSession(Task_self());
  27126. #endif
  27127. ((func_args*)args)->return_code = TEST_FAIL;
  27128. port = ((func_args*)args)->signal->port;
  27129. AssertIntEQ(WOLFSSL_SUCCESS,
  27130. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  27131. AssertIntEQ(WOLFSSL_SUCCESS,
  27132. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  27133. WOLFSSL_FILETYPE_PEM));
  27134. AssertIntEQ(WOLFSSL_SUCCESS,
  27135. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  27136. WOLFSSL_FILETYPE_PEM));
  27137. if (callbacks->ctx_ready)
  27138. callbacks->ctx_ready(ctx);
  27139. ssl = wolfSSL_new(ctx);
  27140. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  27141. CloseSocket(sfd);
  27142. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  27143. if (callbacks->ssl_ready)
  27144. callbacks->ssl_ready(ssl);
  27145. do {
  27146. err = 0; /* Reset error */
  27147. ret = wolfSSL_accept(ssl);
  27148. if (ret != WOLFSSL_SUCCESS) {
  27149. err = wolfSSL_get_error(ssl, 0);
  27150. }
  27151. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  27152. if (ret != WOLFSSL_SUCCESS) {
  27153. char buff[WOLFSSL_MAX_ERROR_SZ];
  27154. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  27155. }
  27156. else {
  27157. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  27158. input[idx] = 0;
  27159. printf("Client message: %s\n", input);
  27160. }
  27161. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  27162. #ifdef WOLFSSL_TIRTOS
  27163. Task_yield();
  27164. #endif
  27165. ((func_args*)args)->return_code = TEST_SUCCESS;
  27166. }
  27167. if (callbacks->on_result)
  27168. callbacks->on_result(ssl);
  27169. wolfSSL_shutdown(ssl);
  27170. wolfSSL_free(ssl);
  27171. wolfSSL_CTX_free(ctx);
  27172. CloseSocket(cfd);
  27173. #ifdef WOLFSSL_TIRTOS
  27174. fdCloseSession(Task_self());
  27175. #endif
  27176. #ifndef WOLFSSL_TIRTOS
  27177. return 0;
  27178. #endif
  27179. }
  27180. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  27181. {
  27182. AssertNotNull(ssl);
  27183. AssertNotNull(line);
  27184. XFILE fp;
  27185. const byte lf = '\n';
  27186. fp = XFOPEN("./MyKeyLog.txt", "a");
  27187. XFWRITE( line, 1, strlen(line),fp);
  27188. XFWRITE( (void*)&lf,1,1,fp);
  27189. XFCLOSE(fp);
  27190. }
  27191. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  27192. static void test_wolfSSL_CTX_set_keylog_callback(void)
  27193. {
  27194. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  27195. SSL_CTX* ctx;
  27196. printf( testingFmt, "wolfSSL_CTX_set_keylog_callback()");
  27197. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27198. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  27199. SSL_CTX_free(ctx);
  27200. SSL_CTX_set_keylog_callback(NULL, NULL);
  27201. printf(resultFmt, passed);
  27202. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  27203. }
  27204. static void test_wolfSSL_CTX_get_keylog_callback(void)
  27205. {
  27206. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  27207. SSL_CTX* ctx;
  27208. printf( testingFmt, "wolfSSL_CTX_get_keylog_callback()");
  27209. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27210. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  27211. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  27212. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  27213. SSL_CTX_set_keylog_callback(ctx, NULL );
  27214. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  27215. SSL_CTX_free(ctx);
  27216. printf(resultFmt, passed);
  27217. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  27218. }
  27219. static void test_wolfSSL_Tls12_Key_Logging_test(void)
  27220. {
  27221. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  27222. /* This test is intended for checking whether keylog callback is called
  27223. * in client during TLS handshake between the client and a server.
  27224. */
  27225. tcp_ready ready;
  27226. func_args client_args;
  27227. func_args server_args;
  27228. THREAD_TYPE serverThread;
  27229. callback_functions server_cbf;
  27230. callback_functions client_cbf;
  27231. SOCKET_T sockfd = 0;
  27232. WOLFSSL_CTX* ctx;
  27233. WOLFSSL* ssl;
  27234. XFILE fp;
  27235. char msg[64] = "hello wolfssl!";
  27236. char reply[1024];
  27237. int msgSz = (int)XSTRLEN(msg);
  27238. printf(testingFmt, "wolfSSL_Tls12_Key_Logging_test()");
  27239. #ifdef WOLFSSL_TIRTOS
  27240. fdOpenSession(Task_self());
  27241. #endif
  27242. InitTcpReady(&ready);
  27243. ready.port = 22222;
  27244. XMEMSET(&client_args, 0, sizeof(func_args));
  27245. XMEMSET(&server_args, 0, sizeof(func_args));
  27246. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  27247. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  27248. server_cbf.method = wolfTLSv1_2_server_method;
  27249. server_args.callbacks = &server_cbf;
  27250. server_args.signal = &ready;
  27251. /* clean up keylog file */
  27252. fp = XFOPEN("./MyKeyLog.txt", "w");
  27253. XFCLOSE(fp);
  27254. /* start server task */
  27255. start_thread(server_task, &server_args, &serverThread);
  27256. wait_tcp_ready(&server_args);
  27257. /* run as a TLS1.2 client */
  27258. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  27259. AssertIntEQ(WOLFSSL_SUCCESS,
  27260. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  27261. AssertIntEQ(WOLFSSL_SUCCESS,
  27262. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  27263. AssertIntEQ(WOLFSSL_SUCCESS,
  27264. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  27265. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  27266. /* set keylog callback */
  27267. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  27268. /* get connected the server task */
  27269. AssertNotNull(ssl = wolfSSL_new(ctx));
  27270. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  27271. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  27272. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  27273. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  27274. wolfSSL_shutdown(ssl);
  27275. wolfSSL_free(ssl);
  27276. wolfSSL_CTX_free(ctx);
  27277. CloseSocket(sockfd);
  27278. join_thread(serverThread);
  27279. FreeTcpReady(&ready);
  27280. #ifdef WOLFSSL_TIRTOS
  27281. fdOpenSession(Task_self());
  27282. #endif
  27283. /* check if the keylog file exists */
  27284. char buff[300] = {0};
  27285. int found = 0;
  27286. fp = XFOPEN("./MyKeyLog.txt", "r");
  27287. AssertNotNull(fp);
  27288. while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  27289. if(0 == strncmp(buff,"CLIENT_RANDOM ",
  27290. sizeof("CLIENT_RANDOM ")-1)) {
  27291. found = 1;
  27292. break;
  27293. }
  27294. }
  27295. XFCLOSE(fp);
  27296. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  27297. AssertNotNull( found );
  27298. printf(resultFmt, passed);
  27299. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  27300. }
  27301. static void test_wolfSSL_Tls13_Key_Logging_test(void)
  27302. {
  27303. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  27304. defined(HAVE_SECRET_CALLBACK)
  27305. /* This test is intended for checking whether keylog callback is called
  27306. * in client during TLS handshake between the client and a server.
  27307. */
  27308. tcp_ready ready;
  27309. func_args client_args;
  27310. func_args server_args;
  27311. THREAD_TYPE serverThread;
  27312. callback_functions server_cbf;
  27313. callback_functions client_cbf;
  27314. SOCKET_T sockfd = 0;
  27315. WOLFSSL_CTX* ctx;
  27316. WOLFSSL* ssl;
  27317. XFILE fp;
  27318. char msg[64] = "hello wolfssl!";
  27319. char reply[1024];
  27320. int msgSz = (int)XSTRLEN(msg);
  27321. printf(testingFmt, "wolfSSL_Tls13_Key_Logging_test()");
  27322. #ifdef WOLFSSL_TIRTOS
  27323. fdOpenSession(Task_self());
  27324. #endif
  27325. InitTcpReady(&ready);
  27326. ready.port = 22222;
  27327. XMEMSET(&client_args, 0, sizeof(func_args));
  27328. XMEMSET(&server_args, 0, sizeof(func_args));
  27329. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  27330. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  27331. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  27332. server_args.callbacks = &server_cbf;
  27333. server_args.signal = &ready;
  27334. /* clean up keylog file */
  27335. fp = XFOPEN("./MyKeyLog.txt", "w");
  27336. XFCLOSE(fp);
  27337. /* start server task */
  27338. start_thread(server_task, &server_args, &serverThread);
  27339. wait_tcp_ready(&server_args);
  27340. /* run as a TLS1.2 client */
  27341. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  27342. AssertIntEQ(WOLFSSL_SUCCESS,
  27343. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  27344. AssertIntEQ(WOLFSSL_SUCCESS,
  27345. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  27346. AssertIntEQ(WOLFSSL_SUCCESS,
  27347. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  27348. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  27349. /* set keylog callback */
  27350. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  27351. /* get connected the server task */
  27352. AssertNotNull(ssl = wolfSSL_new(ctx));
  27353. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  27354. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  27355. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  27356. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  27357. wolfSSL_free(ssl);
  27358. wolfSSL_CTX_free(ctx);
  27359. join_thread(serverThread);
  27360. FreeTcpReady(&ready);
  27361. #ifdef WOLFSSL_TIRTOS
  27362. fdOpenSession(Task_self());
  27363. #endif
  27364. /* check if the keylog file exists */
  27365. char buff[300] = {0};
  27366. int found[4] = {0};
  27367. fp = XFOPEN("./MyKeyLog.txt", "r");
  27368. AssertNotNull(fp);
  27369. while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  27370. if(0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  27371. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  27372. found[0] = 1;
  27373. continue;
  27374. }
  27375. else if(0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  27376. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  27377. found[1] = 1;
  27378. continue;
  27379. }
  27380. else if(0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  27381. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  27382. found[2] = 1;
  27383. continue;
  27384. }
  27385. else if(0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  27386. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  27387. found[3] = 1;
  27388. continue;
  27389. }
  27390. }
  27391. XFCLOSE(fp);
  27392. int numfnd = 0;
  27393. for( uint i = 0; i < 4; i++) {
  27394. if( found[i] != 0)
  27395. numfnd++;
  27396. }
  27397. AssertIntEQ( numfnd,4 );
  27398. printf(resultFmt, passed);
  27399. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  27400. }
  27401. static void test_wolfSSL_X509_NID(void)
  27402. {
  27403. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  27404. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  27405. int sigType;
  27406. int nameSz;
  27407. X509* cert;
  27408. EVP_PKEY* pubKeyTmp;
  27409. X509_NAME* name;
  27410. char commonName[80];
  27411. char countryName[80];
  27412. char localityName[80];
  27413. char stateName[80];
  27414. char orgName[80];
  27415. char orgUnit[80];
  27416. printf(testingFmt, "wolfSSL_X509_NID()");
  27417. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  27418. /* convert cert from DER to internal WOLFSSL_X509 struct */
  27419. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  27420. sizeof_client_cert_der_2048));
  27421. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  27422. /* extract PUBLIC KEY from cert */
  27423. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  27424. /* extract signatureType */
  27425. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  27426. /* extract subjectName info */
  27427. AssertNotNull(name = X509_get_subject_name(cert));
  27428. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  27429. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  27430. NULL, 0)), 0);
  27431. AssertIntEQ(nameSz, 15);
  27432. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  27433. commonName, sizeof(commonName))), 0);
  27434. AssertIntEQ(nameSz, 15);
  27435. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  27436. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  27437. commonName, 9)), 0);
  27438. AssertIntEQ(nameSz, 8);
  27439. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  27440. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  27441. countryName, sizeof(countryName))), 0);
  27442. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  27443. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  27444. localityName, sizeof(localityName))), 0);
  27445. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  27446. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  27447. stateName, sizeof(stateName))), 0);
  27448. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  27449. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  27450. orgName, sizeof(orgName))), 0);
  27451. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  27452. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  27453. orgUnit, sizeof(orgUnit))), 0);
  27454. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  27455. EVP_PKEY_free(pubKeyTmp);
  27456. X509_free(cert);
  27457. printf(resultFmt, passed);
  27458. #endif
  27459. }
  27460. static void test_wolfSSL_CTX_set_srp_username(void)
  27461. {
  27462. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  27463. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  27464. WOLFSSL_CTX* ctx;
  27465. WOLFSSL* ssl;
  27466. const char *username = "TESTUSER";
  27467. const char *password = "TESTPASSWORD";
  27468. int r;
  27469. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  27470. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  27471. AssertNotNull(ctx);
  27472. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  27473. AssertIntEQ(r,SSL_SUCCESS);
  27474. wolfSSL_CTX_free(ctx);
  27475. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  27476. AssertNotNull(ctx);
  27477. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  27478. AssertIntEQ(r,SSL_SUCCESS);
  27479. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  27480. AssertIntEQ(r,SSL_SUCCESS);
  27481. AssertNotNull(ssl = SSL_new(ctx));
  27482. AssertNotNull(SSL_get_srp_username(ssl));
  27483. AssertStrEQ(SSL_get_srp_username(ssl), username);
  27484. wolfSSL_free(ssl);
  27485. wolfSSL_CTX_free(ctx);
  27486. printf(resultFmt, passed);
  27487. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  27488. /* && !NO_SHA256 && !WC_NO_RNG */
  27489. }
  27490. static void test_wolfSSL_CTX_set_srp_password(void)
  27491. {
  27492. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  27493. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  27494. WOLFSSL_CTX* ctx;
  27495. const char *username = "TESTUSER";
  27496. const char *password = "TESTPASSWORD";
  27497. int r;
  27498. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  27499. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  27500. AssertNotNull(ctx);
  27501. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  27502. AssertIntEQ(r,SSL_SUCCESS);
  27503. wolfSSL_CTX_free(ctx);
  27504. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  27505. AssertNotNull(ctx);
  27506. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  27507. AssertIntEQ(r,SSL_SUCCESS);
  27508. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  27509. AssertIntEQ(r,SSL_SUCCESS);
  27510. wolfSSL_CTX_free(ctx);
  27511. printf(resultFmt, passed);
  27512. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  27513. /* && !NO_SHA256 && !WC_NO_RNG */
  27514. }
  27515. static void test_wolfSSL_X509_STORE(void)
  27516. {
  27517. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  27518. X509_STORE *store;
  27519. #ifdef HAVE_CRL
  27520. X509_STORE_CTX *storeCtx;
  27521. X509_CRL *crl;
  27522. X509 *ca, *cert;
  27523. const char crlPem[] = "./certs/crl/crl.revoked";
  27524. const char srvCert[] = "./certs/server-revoked-cert.pem";
  27525. const char caCert[] = "./certs/ca-cert.pem";
  27526. XFILE fp;
  27527. printf(testingFmt, "test_wolfSSL_X509_STORE");
  27528. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  27529. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  27530. SSL_FILETYPE_PEM)));
  27531. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  27532. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  27533. SSL_FILETYPE_PEM)));
  27534. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  27535. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  27536. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  27537. X509_STORE_free(store);
  27538. X509_STORE_CTX_free(storeCtx);
  27539. X509_free(cert);
  27540. X509_free(ca);
  27541. /* should fail to verify now after adding in CRL */
  27542. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  27543. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  27544. SSL_FILETYPE_PEM)));
  27545. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  27546. fp = XFOPEN(crlPem, "rb");
  27547. AssertTrue((fp != XBADFILE));
  27548. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  27549. NULL, NULL));
  27550. XFCLOSE(fp);
  27551. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  27552. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  27553. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  27554. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  27555. SSL_FILETYPE_PEM)));
  27556. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  27557. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  27558. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  27559. X509_CRL_free(crl);
  27560. X509_STORE_free(store);
  27561. X509_STORE_CTX_free(storeCtx);
  27562. X509_free(cert);
  27563. X509_free(ca);
  27564. #endif /* HAVE_CRL */
  27565. #ifndef WOLFCRYPT_ONLY
  27566. {
  27567. SSL_CTX* ctx;
  27568. SSL* ssl;
  27569. int i;
  27570. for (i = 0; i < 2; i++) {
  27571. #ifndef NO_WOLFSSL_SERVER
  27572. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27573. #else
  27574. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27575. #endif
  27576. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  27577. SSL_CTX_set_cert_store(ctx, store);
  27578. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  27579. SSL_CTX_set_cert_store(ctx, store);
  27580. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  27581. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  27582. SSL_FILETYPE_PEM), SSL_SUCCESS);
  27583. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  27584. SSL_FILETYPE_PEM), SSL_SUCCESS);
  27585. AssertNotNull(ssl = SSL_new(ctx));
  27586. if (i == 0) {
  27587. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  27588. }
  27589. else {
  27590. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  27591. X509_STORE_free(store);
  27592. }
  27593. SSL_free(ssl);
  27594. SSL_CTX_free(ctx);
  27595. }
  27596. }
  27597. #endif
  27598. printf(resultFmt, passed);
  27599. #endif
  27600. return;
  27601. }
  27602. static void test_wolfSSL_X509_STORE_load_locations(void)
  27603. {
  27604. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  27605. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  27606. SSL_CTX *ctx;
  27607. X509_STORE *store;
  27608. const char ca_file[] = "./certs/ca-cert.pem";
  27609. const char client_pem_file[] = "./certs/client-cert.pem";
  27610. const char client_der_file[] = "./certs/client-cert.der";
  27611. const char ecc_file[] = "./certs/ecc-key.pem";
  27612. const char certs_path[] = "./certs/";
  27613. const char bad_path[] = "./bad-path/";
  27614. #ifdef HAVE_CRL
  27615. const char crl_path[] = "./certs/crl/";
  27616. const char crl_file[] = "./certs/crl/crl.pem";
  27617. #endif
  27618. printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
  27619. #ifndef NO_WOLFSSL_SERVER
  27620. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  27621. #else
  27622. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  27623. #endif
  27624. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  27625. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  27626. /* Test bad arguments */
  27627. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  27628. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  27629. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  27630. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  27631. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  27632. #ifdef HAVE_CRL
  27633. /* Test with CRL */
  27634. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  27635. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  27636. #endif
  27637. /* Test with CA */
  27638. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  27639. /* Test with client_cert and certs path */
  27640. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  27641. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  27642. SSL_CTX_free(ctx);
  27643. printf(resultFmt, passed);
  27644. #endif
  27645. }
  27646. static void test_X509_STORE_get0_objects(void)
  27647. {
  27648. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  27649. X509_STORE *store;
  27650. X509_STORE *store_cpy;
  27651. SSL_CTX *ctx;
  27652. X509_OBJECT *obj;
  27653. STACK_OF(X509_OBJECT) *objs;
  27654. int i;
  27655. printf(testingFmt, "wolfSSL_X509_STORE_get0_objects");
  27656. /* Setup store */
  27657. #ifndef NO_WOLFSSL_SERVER
  27658. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  27659. #else
  27660. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  27661. #endif
  27662. AssertNotNull(store_cpy = X509_STORE_new());
  27663. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  27664. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  27665. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  27666. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  27667. #ifdef HAVE_CRL
  27668. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  27669. #endif
  27670. /* Store ready */
  27671. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  27672. AssertNotNull(objs = X509_STORE_get0_objects(store));
  27673. #ifdef HAVE_CRL
  27674. #ifdef WOLFSSL_SIGNER_DER_CERT
  27675. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  27676. #else
  27677. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  27678. #endif
  27679. #else
  27680. #ifdef WOLFSSL_SIGNER_DER_CERT
  27681. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  27682. #else
  27683. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  27684. #endif
  27685. #endif
  27686. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  27687. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  27688. switch (X509_OBJECT_get_type(obj)) {
  27689. case X509_LU_X509:
  27690. AssertNotNull(X509_OBJECT_get0_X509(obj));
  27691. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  27692. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  27693. break;
  27694. case X509_LU_CRL:
  27695. #ifdef HAVE_CRL
  27696. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  27697. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  27698. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  27699. break;
  27700. #endif
  27701. case X509_LU_NONE:
  27702. default:
  27703. Fail(("X509_OBJECT_get_type should return x509 or crl "
  27704. "(when built with crl support)"),
  27705. ("Unrecognized X509_OBJECT type or none"));
  27706. }
  27707. }
  27708. X509_STORE_free(store_cpy);
  27709. SSL_CTX_free(ctx);
  27710. printf(resultFmt, passed);
  27711. #endif
  27712. }
  27713. static void test_wolfSSL_BN(void)
  27714. {
  27715. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27716. BIGNUM* a;
  27717. BIGNUM* b;
  27718. BIGNUM* c;
  27719. BIGNUM* d;
  27720. ASN1_INTEGER* ai;
  27721. printf(testingFmt, "wolfSSL_BN()");
  27722. AssertNotNull(b = BN_new());
  27723. AssertNotNull(c = BN_new());
  27724. AssertNotNull(d = BN_new());
  27725. ai = ASN1_INTEGER_new();
  27726. AssertNotNull(ai);
  27727. /* at the moment hard setting since no set function */
  27728. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27729. ai->data[1] = 0x01; /* length of integer */
  27730. ai->data[2] = 0x03;
  27731. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  27732. ASN1_INTEGER_free(ai);
  27733. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  27734. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  27735. /* a + 3 = */
  27736. AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
  27737. AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
  27738. /* check result 3 + 3*/
  27739. AssertIntEQ(BN_get_word(a), 6);
  27740. /* set a back to 3 */
  27741. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  27742. /* a - 3 = */
  27743. AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
  27744. AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
  27745. /* check result 3 - 3*/
  27746. AssertIntEQ(BN_get_word(a), 0);
  27747. /* set a back to 3 */
  27748. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  27749. /* a^b mod c = */
  27750. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  27751. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  27752. /* check result 3^2 mod 5 */
  27753. AssertIntEQ(BN_get_word(d), 4);
  27754. /* a*b = */
  27755. AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
  27756. AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
  27757. /* check result 3*2 */
  27758. AssertIntEQ(BN_get_word(d), 6);
  27759. /* c/b = */
  27760. AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
  27761. AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
  27762. /* check result 5/2 */
  27763. AssertIntEQ(BN_get_word(d), 2); /* check quotient */
  27764. AssertIntEQ(BN_get_word(a), 1); /* check remainder */
  27765. /* set a back to 3 */
  27766. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  27767. /* a*b mod c = */
  27768. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  27769. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  27770. /* check result 3*2 mod 5 */
  27771. AssertIntEQ(BN_get_word(d), 1);
  27772. AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
  27773. AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
  27774. #ifdef WOLFSSL_KEY_GEN
  27775. /* gcd of a and b */
  27776. AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
  27777. AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
  27778. /* check result gcd(16, 24) */
  27779. AssertIntEQ(BN_get_word(d), 8);
  27780. #endif /* WOLFSSL_KEY_GEN */
  27781. /* set b back to 2 */
  27782. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  27783. /* BN_mod_inverse test */
  27784. BIGNUM *r = BN_new();
  27785. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  27786. AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
  27787. BN_free(val);
  27788. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  27789. defined(WOLFSSL_SP_INT_NEGATIVE))
  27790. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  27791. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  27792. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  27793. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  27794. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  27795. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  27796. {
  27797. char* ret;
  27798. AssertNotNull(ret = BN_bn2dec(c));
  27799. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  27800. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  27801. }
  27802. #endif
  27803. AssertIntEQ(BN_get_word(c), 4);
  27804. #endif
  27805. BN_free(a);
  27806. BN_free(b);
  27807. BN_free(c);
  27808. BN_clear_free(d);
  27809. /* check that converting NULL and the null string returns an error */
  27810. a = NULL;
  27811. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  27812. AssertIntLE(BN_hex2bn(&a, ""), 0);
  27813. AssertNull(a);
  27814. /* check that getting a string and a bin of the same number are equal,
  27815. * and that the comparison works EQ, LT and GT */
  27816. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  27817. AssertNotNull(b = BN_new());
  27818. AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
  27819. AssertNotNull(c = BN_new());
  27820. AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
  27821. AssertIntEQ(BN_cmp(a, b), 0);
  27822. AssertIntLT(BN_cmp(a, c), 0);
  27823. AssertIntGT(BN_cmp(c, b), 0);
  27824. AssertIntEQ(BN_set_word(a, 0), 1);
  27825. AssertIntEQ(BN_is_zero(a), 1);
  27826. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  27827. AssertIntEQ(BN_is_zero(a), 0);
  27828. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  27829. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  27830. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  27831. AssertIntEQ(BN_is_zero(a), 1);
  27832. BN_free(a);
  27833. BN_free(b);
  27834. BN_free(c);
  27835. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  27836. {
  27837. BIGNUM *ap;
  27838. BIGNUM bv;
  27839. BIGNUM cv;
  27840. BIGNUM dv;
  27841. AssertNotNull(ap = BN_new());
  27842. BN_init(&bv);
  27843. BN_init(&cv);
  27844. BN_init(&dv);
  27845. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  27846. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  27847. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  27848. /* a^b mod c = */
  27849. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  27850. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  27851. /* check result 3^2 mod 5 */
  27852. AssertIntEQ(BN_get_word(&dv), 4);
  27853. /* a*b mod c = */
  27854. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  27855. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  27856. /* check result 3*2 mod 5 */
  27857. AssertIntEQ(BN_get_word(&dv), 1);
  27858. BN_free(ap);
  27859. }
  27860. #endif
  27861. #ifdef WOLFSSL_KEY_GEN
  27862. AssertNotNull(a = BN_new());
  27863. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
  27864. SSL_SUCCESS);
  27865. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
  27866. BN_free(a);
  27867. #endif
  27868. printf(resultFmt, passed);
  27869. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  27870. }
  27871. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27872. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  27873. #define TEST_ARG 0x1234
  27874. static void msg_cb(int write_p, int version, int content_type,
  27875. const void *buf, size_t len, SSL *ssl, void *arg)
  27876. {
  27877. (void)write_p;
  27878. (void)version;
  27879. (void)content_type;
  27880. (void)buf;
  27881. (void)len;
  27882. (void)ssl;
  27883. AssertTrue(arg == (void*)TEST_ARG);
  27884. }
  27885. #endif
  27886. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27887. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  27888. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  27889. !defined(NO_WOLFSSL_SERVER)
  27890. #ifndef SINGLE_THREADED
  27891. #if defined(SESSION_CERTS)
  27892. #include "wolfssl/internal.h"
  27893. #endif
  27894. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  27895. {
  27896. (void) ctx;
  27897. (void) ssl;
  27898. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS)
  27899. STACK_OF(X509)* sk;
  27900. X509* x509;
  27901. int i, num;
  27902. BIO* bio;
  27903. #endif
  27904. printf("\n===== msgcb called ====\n");
  27905. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  27906. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  27907. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  27908. AssertNotNull(SSL_get0_verified_chain(ssl));
  27909. #endif
  27910. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS)
  27911. bio = BIO_new(BIO_s_file());
  27912. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  27913. sk = SSL_get_peer_cert_chain(ssl);
  27914. AssertNotNull(sk);
  27915. if (!sk) {
  27916. BIO_free(bio);
  27917. return SSL_FAILURE;
  27918. }
  27919. num = sk_X509_num(sk);
  27920. AssertTrue(num > 0);
  27921. for (i = 0; i < num; i++) {
  27922. x509 = sk_X509_value(sk,i);
  27923. AssertNotNull(x509);
  27924. if (!x509)
  27925. break;
  27926. printf("Certificate at index [%d] = :\n",i);
  27927. X509_print(bio,x509);
  27928. printf("\n\n");
  27929. }
  27930. BIO_free(bio);
  27931. #endif
  27932. return SSL_SUCCESS;
  27933. }
  27934. #endif
  27935. #endif
  27936. static void test_wolfSSL_msgCb(void)
  27937. {
  27938. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27939. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  27940. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  27941. !defined(NO_WOLFSSL_SERVER)
  27942. tcp_ready ready;
  27943. func_args client_args;
  27944. func_args server_args;
  27945. #ifndef SINGLE_THREADED
  27946. THREAD_TYPE serverThread;
  27947. #endif
  27948. callback_functions client_cb;
  27949. callback_functions server_cb;
  27950. printf(testingFmt, "test_wolfSSL_msgCb");
  27951. /* create a failed connection and inspect the error */
  27952. #ifdef WOLFSSL_TIRTOS
  27953. fdOpenSession(Task_self());
  27954. #endif
  27955. XMEMSET(&client_args, 0, sizeof(func_args));
  27956. XMEMSET(&server_args, 0, sizeof(func_args));
  27957. StartTCP();
  27958. InitTcpReady(&ready);
  27959. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  27960. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  27961. #ifndef WOLFSSL_NO_TLS12
  27962. client_cb.method = wolfTLSv1_2_client_method;
  27963. server_cb.method = wolfTLSv1_2_server_method;
  27964. #else
  27965. client_cb.method = wolfTLSv1_3_client_method;
  27966. server_cb.method = wolfTLSv1_3_server_method;
  27967. #endif
  27968. server_args.signal = &ready;
  27969. server_args.callbacks = &server_cb;
  27970. client_args.signal = &ready;
  27971. client_args.callbacks = &client_cb;
  27972. client_args.return_code = TEST_FAIL;
  27973. #ifndef SINGLE_THREADED
  27974. start_thread(test_server_nofail, &server_args, &serverThread);
  27975. wait_tcp_ready(&server_args);
  27976. test_client_nofail(&client_args, msgCb);
  27977. join_thread(serverThread);
  27978. #endif
  27979. FreeTcpReady(&ready);
  27980. #ifndef SINGLE_THREADED
  27981. AssertTrue(client_args.return_code);
  27982. AssertTrue(server_args.return_code);
  27983. #endif
  27984. #ifdef WOLFSSL_TIRTOS
  27985. fdOpenSession(Task_self());
  27986. #endif
  27987. printf(resultFmt, passed);
  27988. #endif
  27989. }
  27990. static void test_wolfSSL_either_side(void)
  27991. {
  27992. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  27993. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  27994. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  27995. tcp_ready ready;
  27996. func_args client_args;
  27997. func_args server_args;
  27998. #ifndef SINGLE_THREADED
  27999. THREAD_TYPE serverThread;
  28000. #endif
  28001. callback_functions client_cb;
  28002. callback_functions server_cb;
  28003. printf(testingFmt, "test_wolfSSL_either_side");
  28004. /* create a failed connection and inspect the error */
  28005. #ifdef WOLFSSL_TIRTOS
  28006. fdOpenSession(Task_self());
  28007. #endif
  28008. XMEMSET(&client_args, 0, sizeof(func_args));
  28009. XMEMSET(&server_args, 0, sizeof(func_args));
  28010. StartTCP();
  28011. InitTcpReady(&ready);
  28012. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  28013. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  28014. /* Use different CTX for client and server */
  28015. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  28016. AssertNotNull(client_cb.ctx);
  28017. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  28018. AssertNotNull(server_cb.ctx);
  28019. /* we are responsible for free'ing WOLFSSL_CTX */
  28020. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  28021. server_args.signal = &ready;
  28022. server_args.callbacks = &server_cb;
  28023. client_args.signal = &ready;
  28024. client_args.callbacks = &client_cb;
  28025. client_args.return_code = TEST_FAIL;
  28026. #ifndef SINGLE_THREADED
  28027. start_thread(test_server_nofail, &server_args, &serverThread);
  28028. wait_tcp_ready(&server_args);
  28029. test_client_nofail(&client_args, NULL);
  28030. join_thread(serverThread);
  28031. #endif
  28032. wolfSSL_CTX_free(client_cb.ctx);
  28033. wolfSSL_CTX_free(server_cb.ctx);
  28034. FreeTcpReady(&ready);
  28035. #ifndef SINGLE_THREADED
  28036. AssertTrue(client_args.return_code);
  28037. AssertTrue(server_args.return_code);
  28038. #endif
  28039. #ifdef WOLFSSL_TIRTOS
  28040. fdOpenSession(Task_self());
  28041. #endif
  28042. printf(resultFmt, passed);
  28043. #endif
  28044. }
  28045. static void test_wolfSSL_DTLS_either_side(void)
  28046. {
  28047. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  28048. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  28049. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  28050. defined(WOLFSSL_DTLS)
  28051. tcp_ready ready;
  28052. func_args client_args;
  28053. func_args server_args;
  28054. #ifndef SINGLE_THREADED
  28055. THREAD_TYPE serverThread;
  28056. #endif
  28057. callback_functions client_cb;
  28058. callback_functions server_cb;
  28059. printf(testingFmt, "test_wolfSSL_DTLS_either_side");
  28060. /* create a failed connection and inspect the error */
  28061. #ifdef WOLFSSL_TIRTOS
  28062. fdOpenSession(Task_self());
  28063. #endif
  28064. XMEMSET(&client_args, 0, sizeof(func_args));
  28065. XMEMSET(&server_args, 0, sizeof(func_args));
  28066. StartTCP();
  28067. InitTcpReady(&ready);
  28068. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  28069. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  28070. /* Use different CTX for client and server */
  28071. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  28072. AssertNotNull(client_cb.ctx);
  28073. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  28074. AssertNotNull(server_cb.ctx);
  28075. /* we are responsible for free'ing WOLFSSL_CTX */
  28076. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  28077. server_args.signal = &ready;
  28078. server_args.callbacks = &server_cb;
  28079. client_args.signal = &ready;
  28080. client_args.callbacks = &client_cb;
  28081. client_args.return_code = TEST_FAIL;
  28082. #ifndef SINGLE_THREADED
  28083. start_thread(test_server_nofail, &server_args, &serverThread);
  28084. wait_tcp_ready(&server_args);
  28085. test_client_nofail(&client_args, NULL);
  28086. join_thread(serverThread);
  28087. #endif
  28088. wolfSSL_CTX_free(client_cb.ctx);
  28089. wolfSSL_CTX_free(server_cb.ctx);
  28090. FreeTcpReady(&ready);
  28091. #ifndef SINGLE_THREADED
  28092. AssertTrue(client_args.return_code);
  28093. AssertTrue(server_args.return_code);
  28094. #endif
  28095. #ifdef WOLFSSL_TIRTOS
  28096. fdOpenSession(Task_self());
  28097. #endif
  28098. printf(resultFmt, passed);
  28099. #endif
  28100. }
  28101. static void test_generate_cookie(void)
  28102. {
  28103. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  28104. SSL_CTX* ctx;
  28105. SSL* ssl;
  28106. byte buf[FOURK_BUF] = {0};
  28107. printf(testingFmt, "test_generate_cookie");
  28108. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  28109. AssertNotNull(ssl = SSL_new(ctx));
  28110. /* Test unconnected */
  28111. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  28112. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  28113. wolfSSL_SetCookieCtx(ssl, ctx);
  28114. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  28115. AssertNull(wolfSSL_GetCookieCtx(NULL));
  28116. SSL_free(ssl);
  28117. SSL_CTX_free(ctx);
  28118. printf(resultFmt, passed);
  28119. #endif
  28120. }
  28121. static void test_wolfSSL_set_options(void)
  28122. {
  28123. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28124. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28125. SSL* ssl;
  28126. SSL_CTX* ctx;
  28127. char appData[] = "extra msg";
  28128. unsigned char protos[] = {
  28129. 7, 't', 'l', 's', '/', '1', '.', '2',
  28130. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  28131. };
  28132. unsigned int len = sizeof(protos);
  28133. void *arg = (void *)TEST_ARG;
  28134. printf(testingFmt, "wolfSSL_set_options()");
  28135. #ifndef NO_WOLFSSL_SERVER
  28136. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28137. #else
  28138. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28139. #endif
  28140. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28141. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28142. AssertTrue(SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  28143. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  28144. AssertIntGT((int)SSL_CTX_set_options(ctx, (SSL_OP_COOKIE_EXCHANGE |
  28145. SSL_OP_NO_SSLv2)), 0);
  28146. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE) &
  28147. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  28148. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2) &
  28149. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  28150. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION) &
  28151. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  28152. AssertNull((SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION) &
  28153. SSL_OP_NO_COMPRESSION));
  28154. SSL_CTX_free(ctx);
  28155. #ifndef NO_WOLFSSL_SERVER
  28156. ctx = SSL_CTX_new(wolfSSLv23_server_method());
  28157. AssertNotNull(ctx);
  28158. #else
  28159. ctx = SSL_CTX_new(wolfSSLv23_client_method());
  28160. AssertNotNull(ctx);
  28161. #endif
  28162. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28163. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28164. AssertTrue(SSL_CTX_set_msg_callback(ctx, msg_cb) == SSL_SUCCESS);
  28165. AssertNotNull(ssl = SSL_new(ctx));
  28166. #if defined(HAVE_EX_DATA) || defined(FORTRESS)
  28167. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_SUCCESS);
  28168. AssertNotNull(SSL_get_app_data((const WOLFSSL*)ssl));
  28169. if (ssl) {
  28170. AssertIntEQ(XMEMCMP(SSL_get_app_data((const WOLFSSL*)ssl),
  28171. appData, sizeof(appData)), 0);
  28172. }
  28173. #else
  28174. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_FAILURE);
  28175. AssertNull(SSL_get_app_data((const WOLFSSL*)ssl));
  28176. #endif
  28177. AssertTrue(SSL_set_options(ssl, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  28178. AssertTrue(SSL_get_options(ssl) == SSL_OP_NO_TLSv1);
  28179. AssertIntGT((int)SSL_set_options(ssl, (SSL_OP_COOKIE_EXCHANGE |
  28180. WOLFSSL_OP_NO_SSLv2)), 0);
  28181. AssertTrue((SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE) &
  28182. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  28183. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_TLSv1_2) &
  28184. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  28185. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_COMPRESSION) &
  28186. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  28187. AssertNull((SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION) &
  28188. SSL_OP_NO_COMPRESSION));
  28189. AssertTrue(SSL_set_msg_callback(ssl, msg_cb) == SSL_SUCCESS);
  28190. SSL_set_msg_callback_arg(ssl, arg);
  28191. AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == SSL_SUCCESS);
  28192. SSL_free(ssl);
  28193. SSL_CTX_free(ctx);
  28194. printf(resultFmt, passed);
  28195. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28196. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  28197. }
  28198. static void test_wolfSSL_sk_SSL_CIPHER(void)
  28199. {
  28200. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  28201. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28202. SSL* ssl;
  28203. SSL_CTX* ctx;
  28204. STACK_OF(SSL_CIPHER) *sk, *dup;
  28205. printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
  28206. #ifndef NO_WOLFSSL_SERVER
  28207. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28208. #else
  28209. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28210. #endif
  28211. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28212. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28213. AssertNotNull(ssl = SSL_new(ctx));
  28214. AssertNotNull(sk = SSL_get_ciphers(ssl));
  28215. AssertNotNull(dup = sk_SSL_CIPHER_dup(sk));
  28216. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  28217. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dup));
  28218. /* error case because connection has not been established yet */
  28219. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  28220. sk_SSL_CIPHER_free(dup);
  28221. /* sk is pointer to internal struct that should be free'd in SSL_free */
  28222. SSL_free(ssl);
  28223. SSL_CTX_free(ctx);
  28224. printf(resultFmt, passed);
  28225. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28226. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  28227. }
  28228. static void test_wolfSSL_set1_sigalgs_list(void)
  28229. {
  28230. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  28231. SSL* ssl;
  28232. SSL_CTX* ctx;
  28233. #ifndef NO_WOLFSSL_SERVER
  28234. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28235. #else
  28236. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28237. #endif
  28238. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  28239. SSL_FILETYPE_PEM));
  28240. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28241. AssertNotNull(ssl = SSL_new(ctx));
  28242. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  28243. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  28244. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  28245. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  28246. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  28247. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  28248. #ifndef NO_RSA
  28249. #ifndef NO_SHA256
  28250. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  28251. WOLFSSL_FAILURE);
  28252. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  28253. WOLFSSL_FAILURE);
  28254. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  28255. WOLFSSL_SUCCESS);
  28256. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  28257. WOLFSSL_SUCCESS);
  28258. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  28259. WOLFSSL_FAILURE);
  28260. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  28261. WOLFSSL_FAILURE);
  28262. #ifdef WC_RSA_PSS
  28263. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  28264. WOLFSSL_SUCCESS);
  28265. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  28266. WOLFSSL_SUCCESS);
  28267. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  28268. WOLFSSL_SUCCESS);
  28269. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  28270. WOLFSSL_SUCCESS);
  28271. #endif
  28272. #ifdef WOLFSSL_SHA512
  28273. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  28274. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  28275. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  28276. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  28277. #elif defined(WOLFSSL_SHA384)
  28278. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  28279. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  28280. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  28281. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  28282. #endif
  28283. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  28284. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  28285. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  28286. WOLFSSL_FAILURE);
  28287. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  28288. WOLFSSL_FAILURE);
  28289. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  28290. WOLFSSL_FAILURE);
  28291. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  28292. WOLFSSL_FAILURE);
  28293. #endif
  28294. #endif
  28295. #ifdef HAVE_ECC
  28296. #ifndef NO_SHA256
  28297. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  28298. WOLFSSL_SUCCESS);
  28299. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  28300. #ifdef WOLFSSL_SHA512
  28301. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  28302. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  28303. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  28304. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  28305. #elif defined(WOLFSSL_SHA384)
  28306. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  28307. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  28308. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  28309. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  28310. #endif
  28311. #endif
  28312. #endif
  28313. #ifdef HAVE_ED25519
  28314. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  28315. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  28316. #endif
  28317. #ifdef HAVE_ED448
  28318. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  28319. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  28320. #endif
  28321. #ifndef NO_DSA
  28322. #ifndef NO_SHA256
  28323. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  28324. WOLFSSL_SUCCESS);
  28325. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  28326. WOLFSSL_SUCCESS);
  28327. #endif
  28328. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  28329. defined(WOLFSSL_ALLOW_TLS_SHA1))
  28330. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  28331. WOLFSSL_SUCCESS);
  28332. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  28333. WOLFSSL_SUCCESS);
  28334. #endif
  28335. #endif
  28336. SSL_free(ssl);
  28337. SSL_CTX_free(ctx);
  28338. printf(resultFmt, passed);
  28339. #endif
  28340. }
  28341. /* Testing wolfSSL_set_tlsext_status_type function.
  28342. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  28343. */
  28344. static void test_wolfSSL_set_tlsext_status_type(void){
  28345. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  28346. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  28347. SSL* ssl;
  28348. SSL_CTX* ctx;
  28349. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  28350. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28351. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28352. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28353. AssertNotNull(ssl = SSL_new(ctx));
  28354. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  28355. SSL_SUCCESS);
  28356. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  28357. SSL_free(ssl);
  28358. SSL_CTX_free(ctx);
  28359. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  28360. }
  28361. #ifndef NO_BIO
  28362. static void test_wolfSSL_PEM_read_bio(void)
  28363. {
  28364. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28365. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28366. byte buff[6000];
  28367. XFILE f;
  28368. int bytes;
  28369. X509* x509;
  28370. BIO* bio = NULL;
  28371. BUF_MEM* buf;
  28372. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  28373. f = XFOPEN(cliCertFile, "rb");
  28374. AssertTrue((f != XBADFILE));
  28375. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  28376. XFCLOSE(f);
  28377. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  28378. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  28379. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  28380. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  28381. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  28382. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  28383. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  28384. BIO_free(bio);
  28385. BUF_MEM_free(buf);
  28386. X509_free(x509);
  28387. printf(resultFmt, passed);
  28388. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28389. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  28390. }
  28391. #if defined(OPENSSL_EXTRA)
  28392. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  28393. long argl, long ret)
  28394. {
  28395. (void)bio;
  28396. (void)cmd;
  28397. (void)argp;
  28398. (void)argi;
  28399. (void)argl;
  28400. return ret;
  28401. }
  28402. #endif
  28403. static void test_wolfSSL_BIO(void)
  28404. {
  28405. #if defined(OPENSSL_EXTRA)
  28406. const unsigned char* p;
  28407. byte buff[20];
  28408. BIO* bio1;
  28409. BIO* bio2;
  28410. BIO* bio3;
  28411. char* bufPt;
  28412. int i;
  28413. printf(testingFmt, "wolfSSL_BIO()");
  28414. for (i = 0; i < 20; i++) {
  28415. buff[i] = i;
  28416. }
  28417. /* test BIO_free with NULL */
  28418. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  28419. /* Creating and testing type BIO_s_bio */
  28420. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  28421. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  28422. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  28423. /* read/write before set up */
  28424. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  28425. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  28426. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  28427. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  28428. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  28429. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  28430. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  28431. XMEMCPY(bufPt, buff, 10);
  28432. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  28433. /* write buffer full */
  28434. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  28435. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  28436. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  28437. /* write the other direction with pair */
  28438. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  28439. XMEMCPY(bufPt, buff, 8);
  28440. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  28441. /* try read */
  28442. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  28443. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  28444. /* try read using ctrl function */
  28445. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  28446. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  28447. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  28448. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  28449. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  28450. for (i = 0; i < 20; i++) {
  28451. AssertIntEQ((int)bufPt[i], i);
  28452. }
  28453. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  28454. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  28455. for (i = 0; i < 8; i++) {
  28456. AssertIntEQ((int)bufPt[i], i);
  28457. }
  28458. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  28459. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  28460. /* new pair */
  28461. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  28462. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  28463. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  28464. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  28465. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  28466. /* test wrap around... */
  28467. AssertIntEQ(BIO_reset(bio1), 0);
  28468. AssertIntEQ(BIO_reset(bio3), 0);
  28469. /* fill write buffer, read only small amount then write again */
  28470. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  28471. XMEMCPY(bufPt, buff, 20);
  28472. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  28473. for (i = 0; i < 4; i++) {
  28474. AssertIntEQ(bufPt[i], i);
  28475. }
  28476. /* try writing over read index */
  28477. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  28478. XMEMSET(bufPt, 0, 4);
  28479. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  28480. /* read and write 0 bytes */
  28481. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  28482. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  28483. /* should read only to end of write buffer then need to read again */
  28484. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  28485. for (i = 0; i < 16; i++) {
  28486. AssertIntEQ(bufPt[i], buff[4 + i]);
  28487. }
  28488. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  28489. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  28490. for (i = 0; i < 4; i++) {
  28491. AssertIntEQ(bufPt[i], 0);
  28492. }
  28493. /* read index should not have advanced with nread0 */
  28494. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  28495. for (i = 0; i < 4; i++) {
  28496. AssertIntEQ(bufPt[i], 0);
  28497. }
  28498. /* write and fill up buffer checking reset of index state */
  28499. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  28500. XMEMCPY(bufPt, buff, 20);
  28501. /* test reset on data in bio1 write buffer */
  28502. AssertIntEQ(BIO_reset(bio1), 0);
  28503. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  28504. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  28505. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  28506. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  28507. AssertNotNull(p);
  28508. XMEMCPY(bufPt, buff, 20);
  28509. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  28510. for (i = 0; i < 6; i++) {
  28511. AssertIntEQ(bufPt[i], i);
  28512. }
  28513. /* test case of writing twice with offset read index */
  28514. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  28515. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  28516. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  28517. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  28518. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  28519. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  28520. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  28521. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  28522. BIO_free(bio1);
  28523. BIO_free(bio3);
  28524. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  28525. {
  28526. BIO* bioA = NULL;
  28527. BIO* bioB = NULL;
  28528. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  28529. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  28530. BIO_free(bioA);
  28531. bioA = NULL;
  28532. BIO_free(bioB);
  28533. bioB = NULL;
  28534. }
  28535. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  28536. /* BIOs with file pointers */
  28537. #if !defined(NO_FILESYSTEM)
  28538. {
  28539. XFILE f1;
  28540. XFILE f2;
  28541. BIO* f_bio1;
  28542. BIO* f_bio2;
  28543. unsigned char cert[300];
  28544. char testFile[] = "tests/bio_write_test.txt";
  28545. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  28546. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  28547. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  28548. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  28549. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  28550. f1 = XFOPEN(svrCertFile, "rwb");
  28551. AssertTrue((f1 != XBADFILE));
  28552. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  28553. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  28554. WOLFSSL_SUCCESS);
  28555. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  28556. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  28557. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  28558. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  28559. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  28560. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  28561. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  28562. AssertIntEQ(BIO_reset(f_bio2), 0);
  28563. AssertIntEQ(BIO_tell(NULL),-1);
  28564. AssertIntEQ(BIO_tell(f_bio2),0);
  28565. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  28566. AssertIntEQ(BIO_tell(f_bio2),4);
  28567. BIO_free(f_bio1);
  28568. BIO_free(f_bio2);
  28569. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  28570. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  28571. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  28572. BIO_free(f_bio1);
  28573. }
  28574. #endif /* !defined(NO_FILESYSTEM) */
  28575. /* BIO info callback */
  28576. {
  28577. const char* testArg = "test";
  28578. BIO* cb_bio;
  28579. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  28580. BIO_set_callback(cb_bio, bioCallback);
  28581. AssertNotNull(BIO_get_callback(cb_bio));
  28582. BIO_set_callback(cb_bio, NULL);
  28583. AssertNull(BIO_get_callback(cb_bio));
  28584. BIO_set_callback_arg(cb_bio, (char*)testArg);
  28585. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  28586. AssertNull(BIO_get_callback_arg(NULL));
  28587. BIO_free(cb_bio);
  28588. }
  28589. /* BIO_vfree */
  28590. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  28591. BIO_vfree(NULL);
  28592. BIO_vfree(bio1);
  28593. printf(resultFmt, passed);
  28594. #endif
  28595. }
  28596. #endif /* !NO_BIO */
  28597. static void test_wolfSSL_ASN1_STRING(void)
  28598. {
  28599. #if defined(OPENSSL_EXTRA)
  28600. ASN1_STRING* str = NULL;
  28601. const char data[] = "hello wolfSSL";
  28602. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  28603. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28604. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  28605. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  28606. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28607. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  28608. ASN1_STRING_free(str);
  28609. printf(resultFmt, passed);
  28610. #endif
  28611. }
  28612. static void test_wolfSSL_ASN1_BIT_STRING(void)
  28613. {
  28614. #ifdef OPENSSL_ALL
  28615. ASN1_BIT_STRING* str;
  28616. printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
  28617. AssertNotNull(str = ASN1_BIT_STRING_new());
  28618. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  28619. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  28620. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  28621. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  28622. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  28623. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  28624. ASN1_BIT_STRING_free(str);
  28625. printf(resultFmt, passed);
  28626. #endif
  28627. }
  28628. static void test_wolfSSL_a2i_ASN1_INTEGER(void)
  28629. {
  28630. #ifdef OPENSSL_EXTRA
  28631. BIO *bio, *out;
  28632. ASN1_INTEGER* ai;
  28633. char buf[] = "123456\n12345\n112345678912345678901234567890\n";
  28634. char tmp[1024];
  28635. int tmpSz;
  28636. const char expected1[] = "123456";
  28637. const char expected2[] = "112345678912345678901234567890";
  28638. printf(testingFmt, "test_wolfSSL_a2i_ASN1_INTEGER()");
  28639. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  28640. AssertNotNull(out = BIO_new(BIO_s_mem()));
  28641. AssertNotNull(ai = ASN1_INTEGER_new());
  28642. /* read first line */
  28643. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  28644. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  28645. XMEMSET(tmp, 0, 1024);
  28646. tmpSz = BIO_read(out, tmp, 1024);
  28647. AssertIntEQ(tmpSz, 6);
  28648. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  28649. /* fail on second line (not % 2) */
  28650. AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  28651. /* read 3rd long line */
  28652. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  28653. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  28654. XMEMSET(tmp, 0, 1024);
  28655. tmpSz = BIO_read(out, tmp, 1024);
  28656. AssertIntEQ(tmpSz, 30);
  28657. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  28658. BIO_free(out);
  28659. BIO_free(bio);
  28660. ASN1_INTEGER_free(ai);
  28661. printf(resultFmt, passed);
  28662. #endif
  28663. }
  28664. static void test_wolfSSL_a2i_IPADDRESS(void)
  28665. {
  28666. #ifdef OPENSSL_ALL
  28667. const unsigned char* data;
  28668. int dataSz = 0;
  28669. ASN1_OCTET_STRING *st;
  28670. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  28671. const unsigned char ipv6_exp[] = {
  28672. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  28673. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  28674. };
  28675. const unsigned char ipv6_home[] = {
  28676. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28677. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  28678. };
  28679. printf(testingFmt, "test_wolfSSL_a2i_IPADDRESS()");
  28680. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  28681. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  28682. data = ASN1_STRING_get0_data(st);
  28683. dataSz = ASN1_STRING_length(st);
  28684. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  28685. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  28686. ASN1_STRING_free(st);
  28687. AssertNotNull(st = a2i_IPADDRESS("::1"));
  28688. data = ASN1_STRING_get0_data(st);
  28689. dataSz = ASN1_STRING_length(st);
  28690. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  28691. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  28692. ASN1_STRING_free(st);
  28693. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  28694. data = ASN1_STRING_get0_data(st);
  28695. dataSz = ASN1_STRING_length(st);
  28696. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  28697. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  28698. ASN1_STRING_free(st);
  28699. printf(resultFmt, passed);
  28700. #endif
  28701. }
  28702. static void test_wolfSSL_DES_ecb_encrypt(void)
  28703. {
  28704. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  28705. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  28706. WOLFSSL_DES_key_schedule key;
  28707. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  28708. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  28709. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  28710. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  28711. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  28712. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  28713. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  28714. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  28715. /* Encrypt messages */
  28716. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  28717. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  28718. /* Decrypt messages */
  28719. int ret1 = 0;
  28720. int ret2 = 0;
  28721. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  28722. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  28723. AssertIntEQ(ret1,0);
  28724. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  28725. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  28726. AssertIntEQ(ret2,0);
  28727. printf(resultFmt, passed);
  28728. #endif
  28729. }
  28730. static void test_wolfSSL_ASN1_TIME_adj(void)
  28731. {
  28732. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  28733. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28734. const int year = 365*24*60*60;
  28735. const int day = 24*60*60;
  28736. const int hour = 60*60;
  28737. const int mini = 60;
  28738. const byte asn_utc_time = ASN_UTC_TIME;
  28739. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28740. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28741. #endif
  28742. WOLFSSL_ASN1_TIME *asn_time, *s;
  28743. int offset_day;
  28744. long offset_sec;
  28745. char date_str[CTC_DATE_SIZE + 1];
  28746. time_t t;
  28747. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  28748. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  28749. /* UTC notation test */
  28750. /* 2000/2/15 20:30:00 */
  28751. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28752. offset_day = 7;
  28753. offset_sec = 45 * mini;
  28754. /* offset_sec = -45 * min;*/
  28755. AssertNotNull(asn_time =
  28756. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28757. AssertTrue(asn_time->type == asn_utc_time);
  28758. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28759. date_str[CTC_DATE_SIZE] = '\0';
  28760. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28761. /* negative offset */
  28762. offset_sec = -45 * mini;
  28763. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28764. AssertTrue(asn_time->type == asn_utc_time);
  28765. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28766. date_str[CTC_DATE_SIZE] = '\0';
  28767. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28768. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28769. XMEMSET(date_str, 0, sizeof(date_str));
  28770. /* Generalized time will overflow time_t if not long */
  28771. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28772. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28773. DYNAMIC_TYPE_OPENSSL);
  28774. /* GeneralizedTime notation test */
  28775. /* 2055/03/01 09:00:00 */
  28776. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28777. offset_day = 12;
  28778. offset_sec = 10 * mini;
  28779. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28780. AssertTrue(asn_time->type == asn_gen_time);
  28781. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28782. date_str[CTC_DATE_SIZE] = '\0';
  28783. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28784. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28785. XMEMSET(date_str, 0, sizeof(date_str));
  28786. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28787. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28788. s = NULL;
  28789. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28790. offset_day = 7;
  28791. offset_sec = 45 * mini;
  28792. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28793. AssertTrue(asn_time->type == asn_utc_time);
  28794. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28795. date_str[CTC_DATE_SIZE] = '\0';
  28796. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28797. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28798. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  28799. AssertTrue(asn_time->type == asn_utc_time);
  28800. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  28801. date_str[CTC_DATE_SIZE] = '\0';
  28802. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28803. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28804. printf(resultFmt, passed);
  28805. #endif
  28806. }
  28807. static void test_wolfSSL_X509_cmp_time(void)
  28808. {
  28809. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  28810. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28811. WOLFSSL_ASN1_TIME asn_time;
  28812. time_t t;
  28813. printf(testingFmt, "wolfSSL_X509_cmp_time()");
  28814. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  28815. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  28816. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  28817. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  28818. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  28819. printf(resultFmt, passed);
  28820. #endif
  28821. }
  28822. static void test_wolfSSL_X509_time_adj(void)
  28823. {
  28824. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28825. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  28826. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  28827. !defined(NO_ASN_TIME)
  28828. X509* x509;
  28829. time_t t, not_before, not_after;
  28830. printf(testingFmt, "wolfSSL_X509_time_adj()");
  28831. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  28832. client_cert_der_2048, sizeof_client_cert_der_2048,
  28833. WOLFSSL_FILETYPE_ASN1));
  28834. t = 0;
  28835. not_before = XTIME(0);
  28836. not_after = XTIME(0) + (60 * 24 * 30); /* 30 days after */
  28837. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  28838. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  28839. /* Check X509_gmtime_adj, too. */
  28840. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  28841. X509_free(x509);
  28842. printf(resultFmt, passed);
  28843. #endif
  28844. }
  28845. static void test_wolfSSL_X509(void)
  28846. {
  28847. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  28848. && !defined(NO_RSA)
  28849. X509* x509;
  28850. #ifndef NO_BIO
  28851. BIO* bio;
  28852. X509_STORE_CTX* ctx;
  28853. X509_STORE* store;
  28854. #endif
  28855. char der[] = "certs/ca-cert.der";
  28856. XFILE fp;
  28857. printf(testingFmt, "wolfSSL_X509()");
  28858. AssertNotNull(x509 = X509_new());
  28859. X509_free(x509);
  28860. #ifndef NO_BIO
  28861. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  28862. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  28863. #ifdef WOLFSSL_CERT_GEN
  28864. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  28865. #endif
  28866. AssertNotNull(ctx = X509_STORE_CTX_new());
  28867. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  28868. AssertNotNull(store = X509_STORE_new());
  28869. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  28870. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  28871. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  28872. X509_STORE_CTX_free(ctx);
  28873. X509_STORE_free(store);
  28874. X509_free(x509);
  28875. BIO_free(bio);
  28876. #endif
  28877. /** d2i_X509_fp test **/
  28878. fp = XFOPEN(der, "rb");
  28879. AssertTrue((fp != XBADFILE));
  28880. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  28881. AssertNotNull(x509);
  28882. X509_free(x509);
  28883. XFCLOSE(fp);
  28884. fp = XFOPEN(der, "rb");
  28885. AssertTrue((fp != XBADFILE));
  28886. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  28887. AssertNotNull(x509);
  28888. X509_free(x509);
  28889. XFCLOSE(fp);
  28890. /* X509_up_ref test */
  28891. AssertIntEQ(X509_up_ref(NULL), 0);
  28892. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  28893. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  28894. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  28895. X509_free(x509); /* refCount = 2 */
  28896. X509_free(x509); /* refCount = 1 */
  28897. X509_free(x509); /* refCount = 0, free */
  28898. printf(resultFmt, passed);
  28899. #endif
  28900. }
  28901. static void test_wolfSSL_X509_get_ext_count(void)
  28902. {
  28903. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28904. !defined(NO_RSA)
  28905. int ret = 0;
  28906. WOLFSSL_X509* x509;
  28907. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  28908. FILE* f;
  28909. printf(testingFmt, "wolfSSL_X509_get_ext_count()");
  28910. /* NULL parameter check */
  28911. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  28912. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  28913. SSL_FILETYPE_PEM));
  28914. AssertIntEQ(X509_get_ext_count(x509), 5);
  28915. wolfSSL_X509_free(x509);
  28916. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  28917. SSL_FILETYPE_PEM));
  28918. AssertIntEQ(X509_get_ext_count(x509), 5);
  28919. wolfSSL_X509_free(x509);
  28920. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  28921. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  28922. fclose(f);
  28923. printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
  28924. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  28925. printf(resultFmt, ret == 4 ? passed : failed);
  28926. printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
  28927. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  28928. printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
  28929. wolfSSL_X509_free(x509);
  28930. printf(resultFmt, passed);
  28931. #endif
  28932. }
  28933. static void test_wolfSSL_X509_sign2(void)
  28934. {
  28935. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  28936. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  28937. defined(WOLFSSL_CERT_EXT) && \
  28938. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  28939. WOLFSSL_X509 *x509, *ca;
  28940. const unsigned char *der;
  28941. const unsigned char *pt;
  28942. WOLFSSL_EVP_PKEY *priv;
  28943. WOLFSSL_X509_NAME *name;
  28944. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  28945. int derSz;
  28946. const int year = 365*24*60*60;
  28947. const int day = 24*60*60;
  28948. const int hour = 60*60;
  28949. const int mini = 60;
  28950. time_t t;
  28951. const unsigned char expected[] = {
  28952. 0x30, 0x82, 0x04, 0x25, 0x30, 0x82, 0x03, 0x0D,
  28953. 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
  28954. 0xF1, 0x5C, 0x99, 0x43, 0x66, 0x3D, 0x96, 0x04,
  28955. 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  28956. 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x30,
  28957. 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
  28958. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  28959. 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08,
  28960. 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E,
  28961. 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55,
  28962. 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65,
  28963. 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06,
  28964. 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61,
  28965. 0x77, 0x74, 0x6F, 0x6F, 0x74, 0x68, 0x31, 0x13,
  28966. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C,
  28967. 0x0A, 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74,
  28968. 0x69, 0x6E, 0x67, 0x31, 0x18, 0x30, 0x16, 0x06,
  28969. 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  28970. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  28971. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30,
  28972. 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  28973. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  28974. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73,
  28975. 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E,
  28976. 0x17, 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35,
  28977. 0x32, 0x30, 0x33, 0x30, 0x30, 0x30, 0x5A, 0x17,
  28978. 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32,
  28979. 0x30, 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81,
  28980. 0x9E, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  28981. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10,
  28982. 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C,
  28983. 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  28984. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04,
  28985. 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D,
  28986. 0x61, 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03,
  28987. 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C,
  28988. 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34,
  28989. 0x38, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55,
  28990. 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67,
  28991. 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D,
  28992. 0x32, 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16,
  28993. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  28994. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73,
  28995. 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  28996. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  28997. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69,
  28998. 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  28999. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  29000. 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  29001. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01,
  29002. 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30,
  29003. 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00,
  29004. 0xC3, 0x03, 0xD1, 0x2B, 0xFE, 0x39, 0xA4, 0x32,
  29005. 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C,
  29006. 0x74, 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47,
  29007. 0xD6, 0xA6, 0x36, 0xB2, 0x07, 0x32, 0x8E, 0xD0,
  29008. 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4,
  29009. 0x81, 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67,
  29010. 0xBB, 0xA1, 0x75, 0xC8, 0x36, 0x2C, 0x4A, 0xD2,
  29011. 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF,
  29012. 0xEC, 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47,
  29013. 0x9A, 0xBF, 0x65, 0xCC, 0x7F, 0x65, 0x24, 0x69,
  29014. 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7,
  29015. 0xC5, 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A,
  29016. 0x7A, 0x78, 0xE1, 0x01, 0x56, 0x56, 0x91, 0xA6,
  29017. 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C,
  29018. 0xEF, 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C,
  29019. 0xA1, 0x3B, 0xF5, 0xF1, 0xA3, 0x4A, 0x35, 0xE4,
  29020. 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E,
  29021. 0x97, 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81,
  29022. 0xAF, 0x20, 0x0B, 0x43, 0x14, 0xC5, 0x74, 0x67,
  29023. 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88,
  29024. 0x40, 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72,
  29025. 0x22, 0x17, 0xD7, 0x52, 0x65, 0x24, 0x73, 0xB0,
  29026. 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C,
  29027. 0x7B, 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D,
  29028. 0x50, 0x6D, 0x3B, 0xA3, 0x3B, 0xA3, 0x99, 0x5E,
  29029. 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A,
  29030. 0xD9, 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB,
  29031. 0xFF, 0x25, 0x4C, 0xC4, 0xD1, 0x79, 0xF4, 0x71,
  29032. 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5,
  29033. 0x72, 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D,
  29034. 0x56, 0x2F, 0xD7, 0x15, 0xF7, 0x7F, 0xC0, 0xAE,
  29035. 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3,
  29036. 0x02, 0x03, 0x01, 0x00, 0x01, 0xA3, 0x6E, 0x30,
  29037. 0x6C, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13,
  29038. 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  29039. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15,
  29040. 0x30, 0x13, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01,
  29041. 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C,
  29042. 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1D, 0x06,
  29043. 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14,
  29044. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  29045. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  29046. 0xD7, 0x85, 0x65, 0xC0, 0x30, 0x1F, 0x06, 0x03,
  29047. 0x55, 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
  29048. 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87,
  29049. 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7,
  29050. 0x26, 0xD7, 0x85, 0x65, 0xC0, 0x30, 0x0D, 0x06,
  29051. 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
  29052. 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01,
  29053. 0x00, 0x19, 0xE7, 0xD0, 0x9A, 0xF9, 0x90, 0xAA,
  29054. 0xAD, 0x63, 0x58, 0x21, 0x38, 0xA2, 0x4D, 0x30,
  29055. 0x9A, 0x6F, 0x88, 0x9E, 0x9B, 0xFB, 0xDE, 0x73,
  29056. 0xF2, 0x38, 0xFC, 0x7E, 0x60, 0xC5, 0xFA, 0xBB,
  29057. 0x64, 0xA0, 0xD2, 0xC0, 0xBD, 0xB6, 0x4A, 0xAC,
  29058. 0x38, 0x90, 0xF5, 0xEE, 0xEC, 0x43, 0x90, 0x7D,
  29059. 0x5B, 0xF0, 0x22, 0xA0, 0xAC, 0x59, 0x10, 0xE2,
  29060. 0x8D, 0x16, 0xDA, 0x3A, 0xAB, 0x0F, 0x94, 0x11,
  29061. 0x6C, 0x0C, 0x61, 0xC1, 0xFD, 0xB5, 0xA3, 0xFC,
  29062. 0xE7, 0xFD, 0x0C, 0x63, 0x20, 0xE5, 0x00, 0xCE,
  29063. 0xFD, 0xEE, 0x21, 0xE1, 0xE1, 0x9D, 0x48, 0x9B,
  29064. 0x71, 0x9C, 0x80, 0x39, 0x5E, 0x5A, 0xD3, 0x32,
  29065. 0xA6, 0xAC, 0x3F, 0x84, 0x8C, 0xB6, 0xBC, 0x70,
  29066. 0x90, 0xE9, 0xC1, 0x0F, 0xAB, 0xA5, 0x97, 0xD4,
  29067. 0xE0, 0x8E, 0x3B, 0xBB, 0x02, 0xE0, 0xED, 0xB0,
  29068. 0x10, 0xE8, 0x3F, 0x49, 0xD2, 0x46, 0x4E, 0xE7,
  29069. 0x72, 0x0F, 0x1A, 0xFD, 0xE4, 0x59, 0x84, 0x24,
  29070. 0xA9, 0x7B, 0x9D, 0x8E, 0x8C, 0xBC, 0xEA, 0xD1,
  29071. 0x04, 0x1F, 0xC6, 0x30, 0x47, 0xBD, 0xCC, 0xD1,
  29072. 0xBC, 0x87, 0x00, 0xB5, 0x23, 0x3C, 0x60, 0x8F,
  29073. 0xB2, 0xDB, 0x71, 0xD2, 0xF5, 0xBA, 0xEB, 0xB1,
  29074. 0xD0, 0x53, 0xAC, 0x2E, 0x2C, 0xA5, 0x5D, 0x41,
  29075. 0xCD, 0x9B, 0x4F, 0x8B, 0x41, 0xA1, 0x5D, 0x8E,
  29076. 0xD9, 0x89, 0x5B, 0x5C, 0x58, 0x1C, 0x4A, 0xE6,
  29077. 0x22, 0xC8, 0x15, 0x2D, 0x8E, 0x24, 0x48, 0xF8,
  29078. 0xB2, 0x3C, 0x7A, 0x72, 0x62, 0xEC, 0xB2, 0x76,
  29079. 0xAD, 0x3D, 0x42, 0x29, 0xE9, 0x3B, 0x4E, 0x7F,
  29080. 0x06, 0xA4, 0xA4, 0x72, 0x55, 0xDD, 0x1C, 0x69,
  29081. 0x5E, 0x2B, 0x7E, 0xB7, 0x7C, 0xBD, 0xF6, 0x2F,
  29082. 0xC9, 0x9A, 0x33, 0x31, 0xD9, 0x92, 0x32, 0xB6,
  29083. 0x60, 0x4D, 0x8F, 0x5B, 0xF2, 0xAE, 0xD5, 0x72,
  29084. 0x88, 0x92, 0x75, 0xC4, 0xDC, 0xBD, 0x0B, 0xB8, 0x9D
  29085. };
  29086. printf(testingFmt, "wolfSSL_X509_sign2");
  29087. pt = ca_key_der_2048;
  29088. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  29089. sizeof_ca_key_der_2048));
  29090. pt = client_cert_der_2048;
  29091. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  29092. sizeof_client_cert_der_2048));
  29093. pt = ca_cert_der_2048;
  29094. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  29095. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  29096. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  29097. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  29098. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  29099. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  29100. AssertIntEQ(notAfter->length, 13);
  29101. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  29102. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  29103. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  29104. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  29105. AssertIntEQ(derSz, sizeof(expected));
  29106. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  29107. wolfSSL_X509_free(ca);
  29108. wolfSSL_X509_free(x509);
  29109. wolfSSL_EVP_PKEY_free(priv);
  29110. wolfSSL_ASN1_TIME_free(notBefore);
  29111. wolfSSL_ASN1_TIME_free(notAfter);
  29112. printf(resultFmt, passed);
  29113. #endif
  29114. }
  29115. static void test_wolfSSL_X509_sign(void)
  29116. {
  29117. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29118. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  29119. int ret;
  29120. char *caSubject;
  29121. X509_NAME *name;
  29122. X509 *x509, *ca;
  29123. DecodedCert dCert;
  29124. EVP_PKEY *pub;
  29125. EVP_PKEY *priv;
  29126. EVP_MD_CTX *mctx;
  29127. #if defined(USE_CERT_BUFFERS_1024)
  29128. const unsigned char* rsaPriv = client_key_der_1024;
  29129. const unsigned char* rsaPub = client_keypub_der_1024;
  29130. const unsigned char* certIssuer = client_cert_der_1024;
  29131. long clientKeySz = (long)sizeof_client_key_der_1024;
  29132. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  29133. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  29134. #elif defined(USE_CERT_BUFFERS_2048)
  29135. const unsigned char* rsaPriv = client_key_der_2048;
  29136. const unsigned char* rsaPub = client_keypub_der_2048;
  29137. const unsigned char* certIssuer = client_cert_der_2048;
  29138. long clientKeySz = (long)sizeof_client_key_der_2048;
  29139. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  29140. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  29141. #endif
  29142. byte sn[16];
  29143. int snSz = sizeof(sn);
  29144. printf(testingFmt, "wolfSSL_X509_sign");
  29145. /* Set X509_NAME fields */
  29146. AssertNotNull(name = X509_NAME_new());
  29147. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  29148. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  29149. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  29150. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  29151. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  29152. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  29153. /* Get private and public keys */
  29154. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  29155. clientKeySz));
  29156. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  29157. AssertNotNull(x509 = X509_new());
  29158. /* Set version 3 */
  29159. AssertIntNE(X509_set_version(x509, 2L), 0);
  29160. /* Set subject name, add pubkey, and sign certificate */
  29161. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  29162. X509_NAME_free(name);
  29163. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  29164. #ifdef WOLFSSL_ALT_NAMES
  29165. /* Add some subject alt names */
  29166. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  29167. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  29168. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  29169. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  29170. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  29171. "sphygmomanometer",
  29172. ASN_DNS_TYPE), SSL_SUCCESS);
  29173. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  29174. "supercalifragilisticexpialidocious",
  29175. ASN_DNS_TYPE), SSL_SUCCESS);
  29176. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  29177. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  29178. ASN_DNS_TYPE), SSL_SUCCESS);
  29179. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  29180. {
  29181. unsigned char ip4_type[] = {127,128,0,255};
  29182. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  29183. 0xff, 0xee, 0x99, 0x88,
  29184. 0x77, 0x66, 0x55, 0x44,
  29185. 0x00, 0x33, 0x22, 0x11};
  29186. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  29187. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  29188. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  29189. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  29190. }
  29191. #endif
  29192. #endif /* WOLFSSL_ALT_NAMES */
  29193. /* test valid sign case */
  29194. ret = X509_sign(x509, priv, EVP_sha256());
  29195. /* test valid X509_sign_ctx case */
  29196. AssertNotNull(mctx = EVP_MD_CTX_new());
  29197. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  29198. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  29199. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  29200. AssertIntEQ(X509_get_ext_count(x509), 1);
  29201. #endif
  29202. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  29203. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  29204. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  29205. #endif
  29206. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  29207. WOLFSSL_SUCCESS);
  29208. #if 0
  29209. /* example for writing to file */
  29210. XFILE tmpFile = XFOPEN("./signed.der", "wb");
  29211. if (tmpFile) {
  29212. int derSz = 0;
  29213. const byte* der = wolfSSL_X509_get_der(x509, &derSz);
  29214. XFWRITE(der, 1, derSz, tmpFile);
  29215. }
  29216. XFCLOSE(tmpFile);
  29217. #endif
  29218. /* Variation in size depends on ASN.1 encoding when MSB is set */
  29219. #ifndef WOLFSSL_ALT_NAMES
  29220. /* Valid case - size should be 798-797 with 16 byte serial number */
  29221. AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
  29222. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  29223. /* Valid case - size should be 955-956 with 16 byte serial number */
  29224. AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
  29225. #else
  29226. /* Valid case - size should be 926-927 with 16 byte serial number */
  29227. AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
  29228. #endif
  29229. /* check that issuer name is as expected after signature */
  29230. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  29231. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  29232. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  29233. AssertNotNull(caSubject = wolfSSL_X509_NAME_oneline(
  29234. X509_get_subject_name(ca), 0, 0));
  29235. AssertIntEQ(0, XSTRNCMP(caSubject, dCert.subject, XSTRLEN(caSubject)));
  29236. XFREE(caSubject, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29237. #ifdef WOLFSSL_MULTI_ATTRIB
  29238. /* test adding multiple OU's to the signer */
  29239. AssertNotNull(name = X509_get_subject_name(ca));
  29240. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  29241. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  29242. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  29243. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  29244. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  29245. #endif
  29246. AssertNotNull(name = X509_get_subject_name(ca));
  29247. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  29248. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  29249. AssertNotNull(caSubject = wolfSSL_X509_NAME_oneline(
  29250. X509_get_issuer_name(x509), 0, 0));
  29251. XFREE(caSubject, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29252. FreeDecodedCert(&dCert);
  29253. /* Test invalid parameters */
  29254. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  29255. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  29256. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  29257. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  29258. EVP_MD_CTX_free(mctx);
  29259. AssertNotNull(mctx = EVP_MD_CTX_new());
  29260. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  29261. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  29262. /* test invalid version number */
  29263. #if defined(OPENSSL_ALL)
  29264. AssertIntNE(X509_set_version(x509, 6L), 0);
  29265. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  29266. /* uses ParseCert which fails on bad version number */
  29267. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  29268. #endif
  29269. EVP_MD_CTX_free(mctx);
  29270. EVP_PKEY_free(priv);
  29271. EVP_PKEY_free(pub);
  29272. X509_free(x509);
  29273. X509_free(ca);
  29274. printf(resultFmt, passed);
  29275. #endif
  29276. }
  29277. static void test_wolfSSL_X509_get0_tbs_sigalg(void)
  29278. {
  29279. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  29280. X509* x509 = NULL;
  29281. const X509_ALGOR* alg;
  29282. printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
  29283. AssertNotNull(x509 = X509_new());
  29284. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  29285. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  29286. X509_free(x509);
  29287. printf(resultFmt, passed);
  29288. #endif
  29289. }
  29290. static void test_wolfSSL_X509_ALGOR_get0(void)
  29291. {
  29292. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  29293. !defined(NO_SHA256) && !defined(NO_RSA)
  29294. X509* x509 = NULL;
  29295. const ASN1_OBJECT* obj = NULL;
  29296. const X509_ALGOR* alg;
  29297. int pptype = 0;
  29298. const void *ppval = NULL;
  29299. printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
  29300. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29301. SSL_FILETYPE_PEM));
  29302. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  29303. /* Invalid case */
  29304. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  29305. AssertNull(obj);
  29306. /* Valid case */
  29307. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  29308. AssertNotNull(obj);
  29309. AssertNotNull(ppval);
  29310. AssertIntNE(pptype, 0);
  29311. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  29312. AssertIntEQ(OBJ_obj2nid(obj), CTC_SHA256wRSA);
  29313. X509_free(x509);
  29314. printf(resultFmt, passed);
  29315. #endif
  29316. }
  29317. static void test_wolfSSL_X509_VERIFY_PARAM(void)
  29318. {
  29319. #if defined(OPENSSL_EXTRA)
  29320. X509_VERIFY_PARAM *paramTo;
  29321. X509_VERIFY_PARAM *paramFrom;
  29322. int ret;
  29323. char testIPv4[] = "127.0.0.1";
  29324. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  29325. char testhostName1[] = "foo.hoge.com";
  29326. char testhostName2[] = "foobar.hoge.com";
  29327. printf(testingFmt, "wolfSSL_X509()");
  29328. paramTo = X509_VERIFY_PARAM_new();
  29329. AssertNotNull(paramTo);
  29330. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  29331. paramFrom = X509_VERIFY_PARAM_new();
  29332. AssertNotNull(paramFrom);
  29333. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  29334. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  29335. (int)XSTRLEN(testhostName1));
  29336. AssertIntEQ(1, ret);
  29337. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  29338. (int)XSTRLEN(testhostName1)));
  29339. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  29340. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  29341. AssertIntEQ(0x01, paramFrom->hostFlags);
  29342. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  29343. AssertIntEQ(0, ret);
  29344. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  29345. AssertIntEQ(1, ret);
  29346. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  29347. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  29348. AssertIntEQ(1, ret);
  29349. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  29350. AssertIntEQ(1, ret);
  29351. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  29352. /* null pointer */
  29353. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  29354. AssertIntEQ(WOLFSSL_FAILURE, ret);
  29355. /* in the case of "from" null, returns success */
  29356. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  29357. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  29358. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  29359. AssertIntEQ(WOLFSSL_FAILURE, ret);
  29360. /* inherit flags test : VPARAM_DEFAULT */
  29361. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  29362. AssertIntEQ(1, ret);
  29363. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  29364. (int)XSTRLEN(testhostName1)));
  29365. AssertIntEQ(0x01, paramTo->hostFlags);
  29366. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  29367. /* inherit flags test : VPARAM OVERWRITE */
  29368. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  29369. (int)XSTRLEN(testhostName2));
  29370. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  29371. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  29372. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  29373. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  29374. AssertIntEQ(1, ret);
  29375. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  29376. (int)XSTRLEN(testhostName1)));
  29377. AssertIntEQ(0x01, paramTo->hostFlags);
  29378. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  29379. /* inherit flags test : VPARAM_RESET_FLAGS */
  29380. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  29381. (int)XSTRLEN(testhostName2));
  29382. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  29383. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  29384. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  29385. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  29386. AssertIntEQ(1, ret);
  29387. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  29388. (int)XSTRLEN(testhostName1)));
  29389. AssertIntEQ(0x01, paramTo->hostFlags);
  29390. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  29391. /* inherit flags test : VPARAM_LOCKED */
  29392. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  29393. (int)XSTRLEN(testhostName2));
  29394. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  29395. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  29396. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  29397. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  29398. AssertIntEQ(1, ret);
  29399. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  29400. (int)XSTRLEN(testhostName2)));
  29401. AssertIntEQ(0x00, paramTo->hostFlags);
  29402. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  29403. /* test for incorrect parameters */
  29404. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  29405. AssertIntEQ(0, ret);
  29406. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  29407. AssertIntEQ(0, ret);
  29408. /* inherit flags test : VPARAM_ONCE, not testable yet */
  29409. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  29410. AssertIntEQ(1, ret);
  29411. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  29412. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  29413. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  29414. AssertIntEQ(1, ret);
  29415. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  29416. AssertIntEQ(0, ret);
  29417. X509_VERIFY_PARAM_free(paramTo);
  29418. X509_VERIFY_PARAM_free(paramFrom);
  29419. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  29420. printf(resultFmt, passed);
  29421. #endif
  29422. }
  29423. static void test_wolfSSL_X509_get_X509_PUBKEY(void)
  29424. {
  29425. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  29426. X509* x509 = NULL;
  29427. X509_PUBKEY* pubKey;
  29428. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  29429. AssertNotNull(x509 = X509_new());
  29430. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  29431. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  29432. X509_free(x509);
  29433. printf(resultFmt, passed);
  29434. #endif
  29435. }
  29436. static void test_wolfSSL_X509_PUBKEY(void)
  29437. {
  29438. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  29439. !defined(NO_SHA256) && !defined(NO_RSA)
  29440. X509* x509 = NULL;
  29441. ASN1_OBJECT* obj = NULL;
  29442. X509_PUBKEY* pubKey;
  29443. X509_PUBKEY* pubKey2;
  29444. EVP_PKEY* evpKey;
  29445. const unsigned char *pk;
  29446. int ppklen;
  29447. WOLFSSL_X509_ALGOR *pa;
  29448. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  29449. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  29450. SSL_FILETYPE_PEM));
  29451. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  29452. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  29453. AssertNotNull(pk);
  29454. AssertNotNull(pa);
  29455. AssertNotNull(pubKey);
  29456. AssertIntGT(ppklen, 0);
  29457. AssertIntEQ(OBJ_obj2nid(obj), RSAk);
  29458. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  29459. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  29460. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  29461. X509_PUBKEY_free(pubKey2);
  29462. X509_free(x509);
  29463. EVP_PKEY_free(evpKey);
  29464. printf(resultFmt, passed);
  29465. #endif
  29466. }
  29467. static void test_wolfSSL_RAND(void)
  29468. {
  29469. #if defined(OPENSSL_EXTRA)
  29470. byte seed[16];
  29471. printf(testingFmt, "wolfSSL_RAND()");
  29472. RAND_seed(seed, sizeof(seed));
  29473. AssertIntEQ(RAND_poll(), 1);
  29474. RAND_cleanup();
  29475. AssertIntEQ(RAND_egd(NULL), -1);
  29476. #ifndef NO_FILESYSTEM
  29477. {
  29478. char fname[100];
  29479. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  29480. AssertIntEQ(RAND_write_file(NULL), 0);
  29481. }
  29482. #endif
  29483. printf(resultFmt, passed);
  29484. #endif
  29485. }
  29486. static void test_wolfSSL_BUF(void)
  29487. {
  29488. #if defined(OPENSSL_EXTRA)
  29489. BUF_MEM* buf;
  29490. AssertNotNull(buf = BUF_MEM_new());
  29491. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  29492. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  29493. BUF_MEM_free(buf);
  29494. #endif /* OPENSSL_EXTRA */
  29495. }
  29496. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  29497. static int stub_rand_seed(const void *buf, int num)
  29498. {
  29499. (void)buf;
  29500. (void)num;
  29501. return 123;
  29502. }
  29503. static int stub_rand_bytes(unsigned char *buf, int num)
  29504. {
  29505. (void)buf;
  29506. (void)num;
  29507. return 456;
  29508. }
  29509. static byte* was_stub_rand_cleanup_called(void)
  29510. {
  29511. static byte was_called = 0;
  29512. return &was_called;
  29513. }
  29514. static void stub_rand_cleanup(void)
  29515. {
  29516. byte* was_called = was_stub_rand_cleanup_called();
  29517. *was_called = 1;
  29518. return;
  29519. }
  29520. static byte* was_stub_rand_add_called(void)
  29521. {
  29522. static byte was_called = 0;
  29523. return &was_called;
  29524. }
  29525. static int stub_rand_add(const void *buf, int num, double entropy)
  29526. {
  29527. byte* was_called = was_stub_rand_add_called();
  29528. (void)buf;
  29529. (void)num;
  29530. (void)entropy;
  29531. *was_called = 1;
  29532. return 0;
  29533. }
  29534. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  29535. {
  29536. (void)buf;
  29537. (void)num;
  29538. return 9876;
  29539. }
  29540. static int stub_rand_status(void)
  29541. {
  29542. return 5432;
  29543. }
  29544. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  29545. static void test_wolfSSL_RAND_set_rand_method(void)
  29546. {
  29547. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  29548. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  29549. unsigned char* buf = NULL;
  29550. int num = 0;
  29551. double entropy = 0;
  29552. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  29553. byte* was_add_called = was_stub_rand_add_called();
  29554. printf(testingFmt, "wolfSSL_RAND_set_rand_method()");
  29555. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  29556. DYNAMIC_TYPE_TMP_BUFFER);
  29557. AssertIntNE(wolfSSL_RAND_status(), 5432);
  29558. AssertIntEQ(*was_cleanup_called, 0);
  29559. RAND_cleanup();
  29560. AssertIntEQ(*was_cleanup_called, 0);
  29561. rand_methods.seed = &stub_rand_seed;
  29562. rand_methods.bytes = &stub_rand_bytes;
  29563. rand_methods.cleanup = &stub_rand_cleanup;
  29564. rand_methods.add = &stub_rand_add;
  29565. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  29566. rand_methods.status = &stub_rand_status;
  29567. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  29568. AssertIntEQ(RAND_seed(buf, num), 123);
  29569. AssertIntEQ(RAND_bytes(buf, num), 456);
  29570. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  29571. AssertIntEQ(RAND_status(), 5432);
  29572. AssertIntEQ(*was_add_called, 0);
  29573. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  29574. RAND_add(buf, num, entropy);
  29575. AssertIntEQ(*was_add_called, 1);
  29576. was_add_called = 0;
  29577. AssertIntEQ(*was_cleanup_called, 0);
  29578. RAND_cleanup();
  29579. AssertIntEQ(*was_cleanup_called, 1);
  29580. *was_cleanup_called = 0;
  29581. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  29582. AssertIntNE(RAND_status(), 5432);
  29583. AssertIntEQ(*was_cleanup_called, 0);
  29584. RAND_cleanup();
  29585. AssertIntEQ(*was_cleanup_called, 0);
  29586. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29587. printf(resultFmt, passed);
  29588. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  29589. }
  29590. static void test_wolfSSL_RAND_bytes(void)
  29591. {
  29592. #if defined(OPENSSL_EXTRA)
  29593. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  29594. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  29595. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  29596. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  29597. int max_bufsize;
  29598. byte *my_buf;
  29599. printf(testingFmt, "test_wolfSSL_RAND_bytes()");
  29600. /* sanity check */
  29601. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  29602. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  29603. max_bufsize = size4;
  29604. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  29605. DYNAMIC_TYPE_TMP_BUFFER);
  29606. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  29607. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  29608. AssertNotNull(my_buf);
  29609. XMEMSET(my_buf, 0, max_bufsize);
  29610. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  29611. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  29612. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  29613. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  29614. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29615. printf(resultFmt, passed);
  29616. #endif
  29617. }
  29618. static void test_wolfSSL_BN_rand(void)
  29619. {
  29620. #if defined(OPENSSL_EXTRA)
  29621. BIGNUM* bn;
  29622. printf(testingFmt, "wolfSSL_BN_rand()");
  29623. AssertNotNull(bn = BN_new());
  29624. AssertIntNE(BN_rand(bn, 0, 0, 0), SSL_SUCCESS);
  29625. BN_free(bn);
  29626. AssertNotNull(bn = BN_new());
  29627. AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
  29628. BN_free(bn);
  29629. AssertNotNull(bn = BN_new());
  29630. AssertIntEQ(BN_rand(bn, 64, 0, 0), SSL_SUCCESS);
  29631. BN_free(bn);
  29632. printf(resultFmt, passed);
  29633. #endif
  29634. }
  29635. static void test_wolfSSL_pseudo_rand(void)
  29636. {
  29637. #if defined(OPENSSL_EXTRA)
  29638. BIGNUM* bn;
  29639. unsigned char bin[8];
  29640. int i;
  29641. printf(testingFmt, "wolfSSL_pseudo_rand()");
  29642. /* BN_pseudo_rand returns 1 on success 0 on failure
  29643. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  29644. for (i = 0; i < 10; i++) {
  29645. AssertNotNull(bn = BN_new());
  29646. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  29647. AssertIntGT(BN_bn2bin(bn, bin),0);
  29648. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  29649. BN_free(bn);
  29650. }
  29651. for (i = 0; i < 10; i++) {
  29652. AssertNotNull(bn = BN_new());
  29653. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  29654. AssertIntGT(BN_bn2bin(bn, bin),0);
  29655. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  29656. BN_free(bn);
  29657. }
  29658. printf(resultFmt, passed);
  29659. #endif
  29660. }
  29661. static void test_wolfSSL_PKCS8_Compat(void)
  29662. {
  29663. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  29664. #ifndef NO_BIO
  29665. PKCS8_PRIV_KEY_INFO* pt;
  29666. BIO* bio;
  29667. XFILE f;
  29668. int bytes;
  29669. char pkcs8_buffer[512];
  29670. printf(testingFmt, "wolfSSL_pkcs8()");
  29671. /* file from wolfssl/certs/ directory */
  29672. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  29673. AssertTrue(f != XBADFILE);
  29674. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  29675. XFCLOSE(f);
  29676. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  29677. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  29678. BIO_free(bio);
  29679. PKCS8_PRIV_KEY_INFO_free(pt);
  29680. printf(resultFmt, passed);
  29681. #endif
  29682. #endif
  29683. }
  29684. static void test_wolfSSL_PKCS8_d2i(void)
  29685. {
  29686. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  29687. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  29688. * requires a 12 byte password in FIPS mode. This test ends up
  29689. * trying to use an 8 byte password. */
  29690. #ifndef NO_FILESYSTEM
  29691. unsigned char pkcs8_buffer[2048];
  29692. const unsigned char* p;
  29693. int bytes;
  29694. XFILE file;
  29695. WOLFSSL_EVP_PKEY* pkey = NULL;
  29696. #ifndef NO_BIO
  29697. BIO* bio;
  29698. #if defined(OPENSSL_ALL) && \
  29699. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  29700. defined(HAVE_ECC)) && \
  29701. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  29702. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  29703. #endif
  29704. #endif
  29705. #ifndef NO_RSA
  29706. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  29707. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  29708. #ifndef NO_DES3
  29709. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  29710. #endif
  29711. #endif /* NO_RSA */
  29712. #ifdef HAVE_ECC
  29713. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  29714. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  29715. #ifndef NO_DES3
  29716. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  29717. #endif
  29718. #endif /* HAVE_ECC */
  29719. #endif /* !NO_FILESYSTEM */
  29720. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  29721. #ifndef NO_RSA
  29722. #ifdef USE_CERT_BUFFERS_1024
  29723. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  29724. int rsaSz = sizeof_server_key_der_1024;
  29725. #else
  29726. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  29727. int rsaSz = sizeof_server_key_der_2048;
  29728. #endif
  29729. #endif
  29730. #ifdef HAVE_ECC
  29731. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  29732. int ecSz = sizeof_ecc_key_der_256;
  29733. #endif
  29734. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  29735. #ifndef NO_FILESYSTEM
  29736. (void)pkcs8_buffer;
  29737. (void)p;
  29738. (void)bytes;
  29739. (void)file;
  29740. #ifndef NO_BIO
  29741. (void)bio;
  29742. #endif
  29743. #endif
  29744. #ifdef OPENSSL_ALL
  29745. #ifndef NO_RSA
  29746. /* Try to auto-detect normal RSA private key */
  29747. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  29748. EVP_PKEY_free(pkey);
  29749. #endif
  29750. #ifdef HAVE_ECC
  29751. /* Try to auto-detect normal EC private key */
  29752. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  29753. EVP_PKEY_free(pkey);
  29754. #endif
  29755. #endif /* OPENSSL_ALL */
  29756. #ifndef NO_FILESYSTEM
  29757. #ifndef NO_RSA
  29758. /* Get DER encoded RSA PKCS#8 data. */
  29759. file = XFOPEN(rsaDerPkcs8File, "rb");
  29760. AssertTrue(file != XBADFILE);
  29761. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  29762. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  29763. file)), 0);
  29764. XFCLOSE(file);
  29765. p = pkcs8_buffer;
  29766. #ifdef OPENSSL_ALL
  29767. /* Try to decode - auto-detect key type. */
  29768. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  29769. #else
  29770. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  29771. #endif
  29772. /* Get PEM encoded RSA PKCS#8 data. */
  29773. file = XFOPEN(rsaPemPkcs8File, "rb");
  29774. AssertTrue(file != XBADFILE);
  29775. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  29776. file)), 0);
  29777. XFCLOSE(file);
  29778. #if defined(OPENSSL_ALL) && \
  29779. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  29780. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29781. /* Write PKCS#8 PEM to BIO. */
  29782. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  29783. NULL), bytes);
  29784. /* Compare file and written data */
  29785. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  29786. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  29787. BIO_free(bio);
  29788. #ifndef NO_DES3
  29789. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29790. /* Write Encrypted PKCS#8 PEM to BIO. */
  29791. bytes = 1834;
  29792. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  29793. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  29794. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  29795. (void*)"yassl123"));
  29796. EVP_PKEY_free(evpPkey);
  29797. BIO_free(bio);
  29798. #endif /* !NO_DES3 */
  29799. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  29800. EVP_PKEY_free(pkey);
  29801. /* PKCS#8 encrypted RSA key */
  29802. #ifndef NO_DES3
  29803. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  29804. AssertTrue(file != XBADFILE);
  29805. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  29806. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  29807. file)), 0);
  29808. XFCLOSE(file);
  29809. #if defined(OPENSSL_ALL) && \
  29810. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  29811. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  29812. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  29813. (void*)"yassl123"));
  29814. EVP_PKEY_free(pkey);
  29815. BIO_free(bio);
  29816. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  29817. #endif /* !NO_DES3 */
  29818. #endif /* NO_RSA */
  29819. #ifdef HAVE_ECC
  29820. /* PKCS#8 encode EC key */
  29821. file = XFOPEN(ecDerPkcs8File, "rb");
  29822. AssertTrue(file != XBADFILE);
  29823. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  29824. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  29825. file)), 0);
  29826. XFCLOSE(file);
  29827. p = pkcs8_buffer;
  29828. #ifdef OPENSSL_ALL
  29829. /* Try to decode - auto-detect key type. */
  29830. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  29831. #else
  29832. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  29833. #endif
  29834. /* Get PEM encoded RSA PKCS#8 data. */
  29835. file = XFOPEN(ecPemPkcs8File, "rb");
  29836. AssertTrue(file != XBADFILE);
  29837. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  29838. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  29839. file)), 0);
  29840. XFCLOSE(file);
  29841. #if defined(OPENSSL_ALL) && \
  29842. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  29843. defined(HAVE_AES_CBC)
  29844. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29845. /* Write PKCS#8 PEM to BIO. */
  29846. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  29847. NULL), bytes);
  29848. /* Compare file and written data */
  29849. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  29850. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  29851. BIO_free(bio);
  29852. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29853. /* Write Encrypted PKCS#8 PEM to BIO. */
  29854. bytes = 379;
  29855. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  29856. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  29857. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  29858. (void*)"yassl123"));
  29859. EVP_PKEY_free(evpPkey);
  29860. BIO_free(bio);
  29861. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  29862. EVP_PKEY_free(pkey);
  29863. /* PKCS#8 encrypted EC key */
  29864. #ifndef NO_DES3
  29865. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  29866. AssertTrue(file != XBADFILE);
  29867. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  29868. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  29869. file)), 0);
  29870. XFCLOSE(file);
  29871. #if defined(OPENSSL_ALL) && \
  29872. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  29873. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  29874. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  29875. (void*)"yassl123"));
  29876. EVP_PKEY_free(pkey);
  29877. BIO_free(bio);
  29878. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  29879. #endif /* !NO_DES3 */
  29880. #endif /* HAVE_ECC */
  29881. #endif /* !NO_FILESYSTEM */
  29882. printf(resultFmt, passed);
  29883. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  29884. }
  29885. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  29886. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  29887. #define LOGGING_THREADS 5
  29888. #define ERROR_COUNT 10
  29889. static volatile int loggingThreadsReady;
  29890. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  29891. {
  29892. const char* file;
  29893. int line;
  29894. int err;
  29895. int errorCount = 0;
  29896. int i;
  29897. (void)args;
  29898. while (!loggingThreadsReady);
  29899. for (i = 0; i < ERROR_COUNT; i++)
  29900. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  29901. while ((err = ERR_get_error_line(&file, &line))) {
  29902. AssertIntEQ(err, 990 + errorCount);
  29903. errorCount++;
  29904. }
  29905. AssertIntEQ(errorCount, ERROR_COUNT);
  29906. return 0;
  29907. }
  29908. #endif
  29909. static void test_error_queue_per_thread(void)
  29910. {
  29911. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  29912. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  29913. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  29914. int i;
  29915. printf(testingFmt, "error_queue_per_thread()");
  29916. ERR_clear_error(); /* clear out any error nodes */
  29917. loggingThreadsReady = 0;
  29918. for (i = 0; i < LOGGING_THREADS; i++)
  29919. start_thread(test_logging, NULL, &loggingThreads[i]);
  29920. loggingThreadsReady = 1;
  29921. for (i = 0; i < LOGGING_THREADS; i++)
  29922. join_thread(loggingThreads[i]);
  29923. printf(resultFmt, passed);
  29924. #endif
  29925. }
  29926. static void test_wolfSSL_ERR_put_error(void)
  29927. {
  29928. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  29929. defined(DEBUG_WOLFSSL)
  29930. const char* file;
  29931. int line;
  29932. printf(testingFmt, "wolfSSL_ERR_put_error()");
  29933. ERR_clear_error(); /* clear out any error nodes */
  29934. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  29935. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  29936. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  29937. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  29938. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  29939. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  29940. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  29941. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  29942. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  29943. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  29944. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  29945. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  29946. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  29947. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  29948. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  29949. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  29950. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  29951. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  29952. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  29953. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  29954. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  29955. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  29956. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  29957. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  29958. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  29959. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  29960. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  29961. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  29962. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  29963. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  29964. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  29965. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  29966. /* try reading past end of error queue */
  29967. file = NULL;
  29968. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  29969. AssertNull(file);
  29970. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  29971. PEMerr(4,4);
  29972. AssertIntEQ(ERR_get_error(), 4);
  29973. /* Empty and free up all error nodes */
  29974. ERR_clear_error();
  29975. /* Verify all nodes are cleared */
  29976. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  29977. ERR_clear_error();
  29978. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  29979. printf(resultFmt, passed);
  29980. #endif
  29981. }
  29982. #ifndef NO_BIO
  29983. static void test_wolfSSL_ERR_print_errors(void)
  29984. {
  29985. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  29986. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  29987. BIO* bio;
  29988. char buf[1024];
  29989. printf(testingFmt, "wolfSSL_ERR_print_errors()");
  29990. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  29991. ERR_clear_error(); /* clear out any error nodes */
  29992. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  29993. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  29994. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  29995. ERR_print_errors(bio);
  29996. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  29997. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  29998. buf, 55), 0);
  29999. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  30000. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  30001. buf, 56), 0);
  30002. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  30003. AssertIntEQ(buf[0], '\0');
  30004. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  30005. BIO_free(bio);
  30006. printf(resultFmt, passed);
  30007. #endif
  30008. }
  30009. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  30010. defined(DEBUG_WOLFSSL)
  30011. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  30012. {
  30013. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  30014. return 0;
  30015. }
  30016. #endif
  30017. static void test_wolfSSL_ERR_print_errors_cb(void)
  30018. {
  30019. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  30020. defined(DEBUG_WOLFSSL)
  30021. BIO* bio;
  30022. char buf[1024];
  30023. printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
  30024. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  30025. ERR_clear_error(); /* clear out any error nodes */
  30026. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  30027. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  30028. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  30029. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  30030. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  30031. buf, 53), 0);
  30032. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  30033. buf + 53, 55), 0);
  30034. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  30035. BIO_free(bio);
  30036. printf(resultFmt, passed);
  30037. #endif
  30038. }
  30039. /*
  30040. * Testing WOLFSSL_ERROR_MSG
  30041. */
  30042. static int test_WOLFSSL_ERROR_MSG (void)
  30043. {
  30044. int ret = 0;
  30045. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  30046. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  30047. const char* msg = TEST_STRING;
  30048. printf(testingFmt, "WOLFSSL_ERROR_MSG()");
  30049. WOLFSSL_ERROR_MSG(msg);
  30050. printf(resultFmt, ret == 0 ? passed : failed);
  30051. #endif
  30052. return ret;
  30053. }/*End test_WOLFSSL_ERROR_MSG*/
  30054. /*
  30055. * Testing wc_ERR_remove_state
  30056. */
  30057. static int test_wc_ERR_remove_state (void)
  30058. {
  30059. int ret = 0;
  30060. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  30061. printf(testingFmt, "wc_ERR_remove_state()");
  30062. wc_ERR_remove_state();
  30063. printf(resultFmt, ret == 0 ? passed : failed);
  30064. #endif
  30065. return ret;
  30066. }/*End test_wc_ERR_remove_state*/
  30067. /*
  30068. * Testing wc_ERR_print_errors_fp
  30069. */
  30070. static int test_wc_ERR_print_errors_fp (void)
  30071. {
  30072. int ret = 0;
  30073. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  30074. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  30075. long sz;
  30076. printf(testingFmt, "wc_ERR_print_errors_fp()");
  30077. WOLFSSL_ERROR(BAD_FUNC_ARG);
  30078. XFILE fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  30079. wc_ERR_print_errors_fp(fp);
  30080. #if defined(DEBUG_WOLFSSL)
  30081. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  30082. sz = XFTELL(fp);
  30083. if (sz == 0) {
  30084. ret = BAD_FUNC_ARG;
  30085. }
  30086. #endif
  30087. printf(resultFmt, ret == 0 ? passed : failed);
  30088. XFCLOSE(fp);
  30089. (void)sz;
  30090. #endif
  30091. return ret;
  30092. }/*End test_wc_ERR_print_errors_fp*/
  30093. #ifdef DEBUG_WOLFSSL
  30094. static void Logging_cb(const int logLevel, const char *const logMessage)
  30095. {
  30096. (void)logLevel;
  30097. (void)logMessage;
  30098. }
  30099. #endif
  30100. /*
  30101. * Testing wolfSSL_GetLoggingCb
  30102. */
  30103. static int test_wolfSSL_GetLoggingCb (void)
  30104. {
  30105. int ret = 0;
  30106. printf(testingFmt, "wolfSSL_GetLoggingCb()");
  30107. #ifdef DEBUG_WOLFSSL
  30108. /* Testing without wolfSSL_SetLoggingCb() */
  30109. if (ret == 0) {
  30110. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  30111. ret = 0;
  30112. }
  30113. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  30114. ret = -1;
  30115. }
  30116. }
  30117. /* Testing with wolfSSL_SetLoggingCb() */
  30118. if (ret == 0) {
  30119. ret = wolfSSL_SetLoggingCb(Logging_cb);
  30120. if (ret == 0){
  30121. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  30122. ret = -1;
  30123. }
  30124. if (ret == 0) {
  30125. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  30126. ret = 0;
  30127. }
  30128. }
  30129. /* reset logging callback */
  30130. wolfSSL_SetLoggingCb(NULL);
  30131. }
  30132. }
  30133. #endif
  30134. if (ret == 0) {
  30135. if (wolfSSL_GetLoggingCb() != NULL) {
  30136. ret = -1;
  30137. }
  30138. }
  30139. printf(resultFmt, ret == 0 ? passed : failed);
  30140. return ret;
  30141. }/*End test_wolfSSL_GetLoggingCb*/
  30142. #endif /* !NO_BIO */
  30143. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  30144. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  30145. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  30146. static void test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  30147. {
  30148. static const unsigned char key[] = "simple test key";
  30149. HMAC_CTX* hmac;
  30150. ENGINE* e = NULL;
  30151. unsigned char hash[WC_MAX_DIGEST_SIZE];
  30152. unsigned int len;
  30153. AssertNotNull(hmac = HMAC_CTX_new());
  30154. HMAC_CTX_init(hmac);
  30155. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  30156. SSL_SUCCESS);
  30157. /* re-using test key as data to hash */
  30158. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  30159. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  30160. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  30161. AssertIntEQ(len, md_len);
  30162. AssertIntEQ(HMAC_size(hmac), md_len);
  30163. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  30164. HMAC_cleanup(hmac);
  30165. HMAC_CTX_free(hmac);
  30166. len = 0;
  30167. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  30168. AssertIntEQ(len, md_len);
  30169. }
  30170. #endif
  30171. static void test_wolfSSL_HMAC(void)
  30172. {
  30173. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  30174. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  30175. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  30176. printf(testingFmt, "wolfSSL_HMAC()");
  30177. #ifndef NO_SHA256
  30178. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  30179. #endif
  30180. #ifdef WOLFSSL_SHA224
  30181. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  30182. #endif
  30183. #ifdef WOLFSSL_SHA384
  30184. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  30185. #endif
  30186. #ifdef WOLFSSL_SHA512
  30187. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  30188. #endif
  30189. #ifdef WOLFSSL_SHA3
  30190. #ifndef WOLFSSL_NOSHA3_224
  30191. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  30192. #endif
  30193. #ifndef WOLFSSL_NOSHA3_256
  30194. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  30195. #endif
  30196. #ifndef WOLFSSL_NOSHA3_384
  30197. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  30198. #endif
  30199. #ifndef WOLFSSL_NOSHA3_512
  30200. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  30201. #endif
  30202. #endif
  30203. printf(resultFmt, passed);
  30204. #endif
  30205. }
  30206. static void test_wolfSSL_CMAC(void)
  30207. {
  30208. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  30209. defined(WOLFSSL_AES_DIRECT)
  30210. int i;
  30211. byte key[AES_128_KEY_SIZE];
  30212. CMAC_CTX* cmacCtx = NULL;
  30213. byte out[AES_BLOCK_SIZE];
  30214. size_t outLen = AES_BLOCK_SIZE;
  30215. printf(testingFmt, "test_wolfSSL_CMAC()");
  30216. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  30217. key[i] = i;
  30218. }
  30219. AssertNotNull(cmacCtx = CMAC_CTX_new());
  30220. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  30221. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  30222. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  30223. NULL), SSL_SUCCESS);
  30224. /* re-using test key as data to hash */
  30225. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  30226. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  30227. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  30228. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  30229. CMAC_CTX_free(cmacCtx);
  30230. printf(resultFmt, passed);
  30231. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  30232. }
  30233. static void test_wolfSSL_OBJ(void)
  30234. {
  30235. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  30236. * mode
  30237. */
  30238. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  30239. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  30240. defined(WOLFSSL_CERT_GEN)
  30241. ASN1_OBJECT *obj = NULL;
  30242. char buf[50];
  30243. XFILE fp;
  30244. X509 *x509 = NULL;
  30245. X509_NAME *x509Name;
  30246. X509_NAME_ENTRY *x509NameEntry;
  30247. ASN1_OBJECT *asn1Name = NULL;
  30248. int numNames;
  30249. BIO *bio = NULL;
  30250. int nid;
  30251. int i, j;
  30252. const char *f[] = {
  30253. #ifndef NO_RSA
  30254. "./certs/ca-cert.der",
  30255. #endif
  30256. #ifdef HAVE_ECC
  30257. "./certs/ca-ecc-cert.der",
  30258. "./certs/ca-ecc384-cert.der",
  30259. #endif
  30260. NULL};
  30261. ASN1_OBJECT *field_name_obj = NULL;
  30262. int lastpos = -1;
  30263. int tmp = -1;
  30264. ASN1_STRING *asn1 = NULL;
  30265. unsigned char *buf_dyn = NULL;
  30266. printf(testingFmt, "wolfSSL_OBJ()");
  30267. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  30268. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  30269. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  30270. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  30271. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  30272. ASN1_OBJECT_free(obj);
  30273. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  30274. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  30275. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  30276. #ifdef WOLFSSL_CERT_EXT
  30277. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  30278. #endif
  30279. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  30280. ASN1_OBJECT_free(obj);
  30281. for (i = 0; f[i] != NULL; i++)
  30282. {
  30283. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  30284. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  30285. XFCLOSE(fp);
  30286. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  30287. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  30288. /* Get the Common Name by using OBJ_txt2obj */
  30289. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  30290. do
  30291. {
  30292. lastpos = tmp;
  30293. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  30294. } while (tmp > -1);
  30295. AssertIntNE(lastpos, -1);
  30296. ASN1_OBJECT_free(field_name_obj);
  30297. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  30298. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  30299. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  30300. /*
  30301. * All Common Names should be www.wolfssl.com
  30302. * This makes testing easier as we can test for the expected value.
  30303. */
  30304. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  30305. OPENSSL_free(buf_dyn);
  30306. bio = BIO_new(BIO_s_mem());
  30307. AssertTrue(bio != NULL);
  30308. for (j = 0; j < numNames; j++)
  30309. {
  30310. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  30311. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  30312. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  30313. }
  30314. BIO_free(bio);
  30315. X509_free(x509);
  30316. }
  30317. #ifdef HAVE_PKCS12
  30318. {
  30319. PKCS12 *p12;
  30320. int boolRet;
  30321. EVP_PKEY *pkey = NULL;
  30322. const char *p12_f[] = {
  30323. #if !defined(NO_DES3) && !defined(NO_RSA)
  30324. "./certs/test-servercert.p12",
  30325. #endif
  30326. NULL};
  30327. for (i = 0; p12_f[i] != NULL; i++)
  30328. {
  30329. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  30330. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  30331. XFCLOSE(fp);
  30332. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  30333. &pkey, &x509, NULL)) > 0);
  30334. wc_PKCS12_free(p12);
  30335. EVP_PKEY_free(pkey);
  30336. x509Name = X509_get_issuer_name(x509);
  30337. AssertNotNull(x509Name);
  30338. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  30339. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  30340. for (j = 0; j < numNames; j++)
  30341. {
  30342. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  30343. AssertNotNull(asn1Name =
  30344. X509_NAME_ENTRY_get_object(x509NameEntry));
  30345. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  30346. }
  30347. BIO_free(bio);
  30348. X509_free(x509);
  30349. }
  30350. }
  30351. #endif /* HAVE_PKCS12 */
  30352. printf(resultFmt, passed);
  30353. #endif
  30354. }
  30355. static void test_wolfSSL_i2a_ASN1_OBJECT(void)
  30356. {
  30357. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  30358. ASN1_OBJECT *obj = NULL;
  30359. BIO *bio = NULL;
  30360. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  30361. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  30362. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  30363. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  30364. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  30365. BIO_free(bio);
  30366. ASN1_OBJECT_free(obj);
  30367. #endif
  30368. }
  30369. static void test_wolfSSL_OBJ_cmp(void)
  30370. {
  30371. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  30372. ASN1_OBJECT *obj = NULL;
  30373. ASN1_OBJECT *obj2 = NULL;
  30374. printf(testingFmt, "wolfSSL_OBJ_cmp()");
  30375. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  30376. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  30377. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  30378. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  30379. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  30380. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  30381. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  30382. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  30383. ASN1_OBJECT_free(obj);
  30384. ASN1_OBJECT_free(obj2);
  30385. printf(resultFmt, passed);
  30386. #endif
  30387. }
  30388. static void test_wolfSSL_OBJ_txt2nid(void)
  30389. {
  30390. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  30391. int i;
  30392. static const struct {
  30393. const char* sn;
  30394. const char* ln;
  30395. const char* oid;
  30396. int nid;
  30397. } testVals[] = {
  30398. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  30399. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  30400. NID_id_on_dnsSRV },
  30401. { "msUPN", "Microsoft User Principal Name",
  30402. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  30403. { NULL, NULL, NULL, NID_undef }
  30404. };
  30405. printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
  30406. /* Invalid cases */
  30407. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  30408. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  30409. /* Valid cases */
  30410. for (i = 0; testVals[i].sn != NULL; i++) {
  30411. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  30412. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  30413. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  30414. }
  30415. printf(resultFmt, passed);
  30416. #endif
  30417. }
  30418. static void test_wolfSSL_OBJ_txt2obj(void)
  30419. {
  30420. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  30421. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  30422. int i;
  30423. char buf[50];
  30424. ASN1_OBJECT* obj;
  30425. static const struct {
  30426. const char* oidStr;
  30427. const char* sn;
  30428. const char* ln;
  30429. } objs_list[] = {
  30430. #if defined(WOLFSSL_APACHE_HTTPD)
  30431. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  30432. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  30433. #endif
  30434. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  30435. { NULL, NULL, NULL }
  30436. };
  30437. printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
  30438. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  30439. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  30440. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  30441. /* Test numerical value of oid (oidStr) */
  30442. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  30443. /* Convert object back to text to confirm oid is correct */
  30444. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  30445. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  30446. ASN1_OBJECT_free(obj);
  30447. XMEMSET(buf, 0, sizeof(buf));
  30448. /* Test short name (sn) */
  30449. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  30450. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  30451. /* Convert object back to text to confirm oid is correct */
  30452. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  30453. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  30454. ASN1_OBJECT_free(obj);
  30455. XMEMSET(buf, 0, sizeof(buf));
  30456. /* Test long name (ln) - should fail when no_name = 1 */
  30457. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  30458. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  30459. /* Convert object back to text to confirm oid is correct */
  30460. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  30461. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  30462. ASN1_OBJECT_free(obj);
  30463. XMEMSET(buf, 0, sizeof(buf));
  30464. }
  30465. printf(resultFmt, passed);
  30466. #endif
  30467. }
  30468. static void test_wolfSSL_i2t_ASN1_OBJECT(void)
  30469. {
  30470. #if defined(OPENSSL_EXTRA) && \
  30471. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  30472. char buf[50] = {0};
  30473. ASN1_OBJECT* obj;
  30474. const char* oid = "2.5.29.19";
  30475. const char* ln = "X509v3 Basic Constraints";
  30476. printf(testingFmt, "test_wolfSSL_i2t_ASN1_OBJECT()");
  30477. obj = NULL;
  30478. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
  30479. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
  30480. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
  30481. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  30482. XMEMSET(buf, 0, sizeof(buf));
  30483. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  30484. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  30485. ASN1_OBJECT_free(obj);
  30486. printf(resultFmt, passed);
  30487. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  30488. }
  30489. static void test_wolfSSL_X509_NAME_ENTRY(void)
  30490. {
  30491. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30492. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  30493. X509* x509;
  30494. #ifndef NO_BIO
  30495. BIO* bio;
  30496. #endif
  30497. X509_NAME* nm;
  30498. X509_NAME_ENTRY* entry;
  30499. unsigned char cn[] = "another name to add";
  30500. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  30501. AssertNotNull(x509 =
  30502. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  30503. #ifndef NO_BIO
  30504. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  30505. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  30506. #endif
  30507. #ifdef WOLFSSL_CERT_REQ
  30508. {
  30509. X509_REQ* req;
  30510. #ifndef NO_BIO
  30511. BIO* bReq;
  30512. #endif
  30513. AssertNotNull(req =
  30514. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  30515. #ifndef NO_BIO
  30516. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  30517. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  30518. BIO_free(bReq);
  30519. #endif
  30520. X509_free(req);
  30521. }
  30522. #endif
  30523. AssertNotNull(nm = X509_get_subject_name(x509));
  30524. /* Test add entry */
  30525. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  30526. 0x0c, cn, (int)sizeof(cn)));
  30527. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  30528. #ifdef WOLFSSL_CERT_EXT
  30529. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  30530. (byte*)"support@wolfssl.com", 19, -1,
  30531. 1), WOLFSSL_SUCCESS);
  30532. #endif
  30533. X509_NAME_ENTRY_free(entry);
  30534. /* Test add entry by text */
  30535. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  30536. 0x0c, cn, (int)sizeof(cn)));
  30537. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  30538. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  30539. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  30540. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  30541. #endif
  30542. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  30543. X509_NAME_ENTRY_free(entry);
  30544. /* Test add entry by NID */
  30545. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  30546. cn, -1, -1, 0), WOLFSSL_SUCCESS);
  30547. #ifndef NO_BIO
  30548. BIO_free(bio);
  30549. #endif
  30550. X509_free(x509); /* free's nm */
  30551. printf(resultFmt, passed);
  30552. #endif
  30553. }
  30554. static void test_wolfSSL_X509_set_name(void)
  30555. {
  30556. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30557. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  30558. X509* x509;
  30559. X509_NAME* name;
  30560. printf(testingFmt, "wolfSSL_X509_set_name()");
  30561. AssertNotNull(name = X509_NAME_new());
  30562. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  30563. (byte*)"wolfssl.com", 11, 0, 1),
  30564. WOLFSSL_SUCCESS);
  30565. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  30566. (byte*)"support@wolfssl.com", 19, -1,
  30567. 1), WOLFSSL_SUCCESS);
  30568. AssertNotNull(x509 = X509_new());
  30569. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  30570. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  30571. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  30572. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  30573. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  30574. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  30575. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  30576. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  30577. X509_free(x509);
  30578. X509_NAME_free(name);
  30579. printf(resultFmt, passed);
  30580. #endif /* OPENSSL_ALL && !NO_CERTS */
  30581. }
  30582. static void test_wolfSSL_X509_set_notAfter(void)
  30583. {
  30584. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  30585. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  30586. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  30587. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  30588. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  30589. /* Generalized time will overflow time_t if not long */
  30590. X509* x;
  30591. BIO* bio;
  30592. ASN1_TIME *asn_time, *time_check;
  30593. const int year = 365*24*60*60;
  30594. const int day = 24*60*60;
  30595. const int hour = 60*60;
  30596. const int mini = 60;
  30597. int offset_day;
  30598. unsigned char buf[25];
  30599. time_t t;
  30600. printf(testingFmt, "wolfSSL_X509_set_notAfter()");
  30601. /*
  30602. * Setup asn_time. APACHE HTTPD uses time(NULL)
  30603. */
  30604. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  30605. offset_day = 7;
  30606. /*
  30607. * Free these.
  30608. */
  30609. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  30610. AssertNotNull(asn_time);
  30611. AssertNotNull(x = X509_new());
  30612. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  30613. /*
  30614. * Tests
  30615. */
  30616. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  30617. /* time_check is simply (ANS1_TIME*)x->notAfter */
  30618. AssertNotNull(time_check = X509_get_notAfter(x));
  30619. /* ANS1_TIME_check validates by checking if argument can be parsed */
  30620. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  30621. /* Convert to human readable format and compare to intended date */
  30622. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  30623. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  30624. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  30625. /*
  30626. * Cleanup
  30627. */
  30628. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  30629. X509_free(x);
  30630. BIO_free(bio);
  30631. printf(resultFmt, passed);
  30632. #endif
  30633. }
  30634. static void test_wolfSSL_X509_set_notBefore(void)
  30635. {
  30636. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  30637. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  30638. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  30639. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  30640. X509* x;
  30641. BIO* bio;
  30642. ASN1_TIME *asn_time, *time_check;
  30643. const int year = 365*24*60*60;
  30644. const int day = 24*60*60;
  30645. const int hour = 60*60;
  30646. const int mini = 60;
  30647. int offset_day;
  30648. unsigned char buf[25];
  30649. time_t t;
  30650. printf(testingFmt, "wolfSSL_X509_set_notBefore()");
  30651. /*
  30652. * Setup asn_time. APACHE HTTPD uses time(NULL)
  30653. */
  30654. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  30655. offset_day = 7;
  30656. /*
  30657. * Free these.
  30658. */
  30659. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  30660. AssertNotNull(asn_time);
  30661. AssertNotNull(x = X509_new());
  30662. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  30663. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  30664. /*
  30665. * Main Tests
  30666. */
  30667. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  30668. /* time_check == (ANS1_TIME*)x->notBefore */
  30669. AssertNotNull(time_check = X509_get_notBefore(x));
  30670. /* ANS1_TIME_check validates by checking if argument can be parsed */
  30671. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  30672. /* Convert to human readable format and compare to intended date */
  30673. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  30674. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  30675. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  30676. /*
  30677. * Cleanup
  30678. */
  30679. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  30680. X509_free(x);
  30681. BIO_free(bio);
  30682. printf(resultFmt, passed);
  30683. #endif
  30684. }
  30685. static void test_wolfSSL_X509_set_version(void)
  30686. {
  30687. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  30688. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  30689. X509* x509;
  30690. long v = 2L;
  30691. long maxInt = INT_MAX;
  30692. AssertNotNull(x509 = X509_new());
  30693. /* These should pass. */
  30694. AssertTrue(wolfSSL_X509_set_version(x509, v));
  30695. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  30696. /* Fail Case: When v(long) is greater than x509->version(int). */
  30697. v = maxInt+1;
  30698. AssertFalse(wolfSSL_X509_set_version(x509, v));
  30699. /* Cleanup */
  30700. X509_free(x509);
  30701. printf(resultFmt, passed);
  30702. #endif
  30703. }
  30704. #ifndef NO_BIO
  30705. static void test_wolfSSL_BIO_gets(void)
  30706. {
  30707. #if defined(OPENSSL_EXTRA)
  30708. BIO* bio;
  30709. BIO* bio2;
  30710. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  30711. char emp[] = "";
  30712. char bio_buffer[20];
  30713. int bufferSz = 20;
  30714. printf(testingFmt, "wolfSSL_BIO_gets()");
  30715. /* try with bad args */
  30716. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  30717. /* try with real msg */
  30718. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  30719. XMEMSET(bio_buffer, 0, bufferSz);
  30720. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  30721. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  30722. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  30723. AssertFalse(bio2 != BIO_next(bio));
  30724. /* make buffer filled with no terminating characters */
  30725. XMEMSET(bio_buffer, 1, bufferSz);
  30726. /* BIO_gets reads a line of data */
  30727. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  30728. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  30729. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  30730. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  30731. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  30732. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  30733. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  30734. /* check not null terminated string */
  30735. BIO_free(bio);
  30736. msg[0] = 0x33;
  30737. msg[1] = 0x33;
  30738. msg[2] = 0x33;
  30739. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  30740. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  30741. AssertIntEQ(bio_buffer[0], msg[0]);
  30742. AssertIntEQ(bio_buffer[1], msg[1]);
  30743. AssertIntNE(bio_buffer[2], msg[2]);
  30744. BIO_free(bio);
  30745. msg[3] = 0x33;
  30746. bio_buffer[3] = 0x33;
  30747. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  30748. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  30749. AssertIntEQ(bio_buffer[0], msg[0]);
  30750. AssertIntEQ(bio_buffer[1], msg[1]);
  30751. AssertIntEQ(bio_buffer[2], msg[2]);
  30752. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  30753. /* check reading an empty string */
  30754. BIO_free(bio);
  30755. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  30756. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  30757. AssertStrEQ(emp, bio_buffer);
  30758. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  30759. /* check error cases */
  30760. BIO_free(bio);
  30761. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  30762. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  30763. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  30764. #if !defined(NO_FILESYSTEM)
  30765. {
  30766. BIO* f_bio;
  30767. XFILE f;
  30768. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  30769. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  30770. f = XFOPEN(svrCertFile, "rb");
  30771. AssertTrue((f != XBADFILE));
  30772. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  30773. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  30774. BIO_free(f_bio);
  30775. }
  30776. #endif /* NO_FILESYSTEM */
  30777. BIO_free(bio);
  30778. BIO_free(bio2);
  30779. /* try with type BIO */
  30780. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  30781. sizeof(msg));
  30782. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  30783. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  30784. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  30785. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  30786. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  30787. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  30788. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  30789. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  30790. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  30791. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  30792. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  30793. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  30794. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  30795. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  30796. BIO_free(bio);
  30797. BIO_free(bio2);
  30798. /* check reading an empty string */
  30799. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  30800. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  30801. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  30802. AssertStrEQ(emp, bio_buffer);
  30803. BIO_free(bio);
  30804. printf(resultFmt, passed);
  30805. #endif
  30806. }
  30807. static void test_wolfSSL_BIO_puts(void)
  30808. {
  30809. #if defined(OPENSSL_EXTRA)
  30810. BIO* bio;
  30811. char input[] = "hello\0world\n.....ok\n\0";
  30812. char output[128];
  30813. printf(testingFmt, "wolfSSL_BIO_puts()");
  30814. XMEMSET(output, 0, sizeof(output));
  30815. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  30816. AssertIntEQ(BIO_puts(bio, input), 5);
  30817. AssertIntEQ(BIO_pending(bio), 5);
  30818. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  30819. AssertIntEQ(BIO_pending(bio), 19);
  30820. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  30821. AssertStrEQ(output, "helloworld\n");
  30822. AssertIntEQ(BIO_pending(bio), 8);
  30823. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  30824. AssertStrEQ(output, ".....ok\n");
  30825. AssertIntEQ(BIO_pending(bio), 0);
  30826. AssertIntEQ(BIO_puts(bio, ""), -1);
  30827. BIO_free(bio);
  30828. printf(resultFmt, passed);
  30829. #endif
  30830. }
  30831. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  30832. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  30833. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  30834. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  30835. {
  30836. (void)ssl;
  30837. (void)buf;
  30838. (void)sz;
  30839. (void)ctx;
  30840. return WOLFSSL_CBIO_ERR_WANT_READ;
  30841. }
  30842. #endif
  30843. static void test_wolfSSL_BIO_should_retry(void)
  30844. {
  30845. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  30846. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  30847. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  30848. tcp_ready ready;
  30849. func_args server_args;
  30850. THREAD_TYPE serverThread;
  30851. SOCKET_T sockfd = 0;
  30852. WOLFSSL_CTX* ctx;
  30853. WOLFSSL* ssl;
  30854. char msg[64] = "hello wolfssl!";
  30855. char reply[1024];
  30856. int msgSz = (int)XSTRLEN(msg);
  30857. int ret;
  30858. BIO* bio;
  30859. printf(testingFmt, "wolfSSL_BIO_should_retry()");
  30860. XMEMSET(&server_args, 0, sizeof(func_args));
  30861. #ifdef WOLFSSL_TIRTOS
  30862. fdOpenSession(Task_self());
  30863. #endif
  30864. StartTCP();
  30865. InitTcpReady(&ready);
  30866. #if defined(USE_WINDOWS_API)
  30867. /* use RNG to get random port if using windows */
  30868. ready.port = GetRandomPort();
  30869. #endif
  30870. server_args.signal = &ready;
  30871. start_thread(test_server_nofail, &server_args, &serverThread);
  30872. wait_tcp_ready(&server_args);
  30873. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30874. AssertIntEQ(WOLFSSL_SUCCESS,
  30875. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30876. AssertIntEQ(WOLFSSL_SUCCESS,
  30877. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30878. AssertIntEQ(WOLFSSL_SUCCESS,
  30879. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30880. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30881. /* force retry */
  30882. ssl = wolfSSL_new(ctx);
  30883. AssertNotNull(ssl);
  30884. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30885. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  30886. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  30887. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  30888. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  30889. AssertIntNE(BIO_should_retry(bio), 0);
  30890. /* now perform successful connection */
  30891. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  30892. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  30893. BIO_read(bio, reply, sizeof(reply));
  30894. ret = wolfSSL_get_error(ssl, -1);
  30895. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  30896. AssertIntNE(BIO_should_retry(bio), 0);
  30897. }
  30898. else {
  30899. AssertIntEQ(BIO_should_retry(bio), 0);
  30900. }
  30901. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  30902. XSTRLEN("I hear you fa shizzle!")), 0);
  30903. BIO_free(bio);
  30904. wolfSSL_CTX_free(ctx);
  30905. join_thread(serverThread);
  30906. FreeTcpReady(&ready);
  30907. #ifdef WOLFSSL_TIRTOS
  30908. fdOpenSession(Task_self());
  30909. #endif
  30910. printf(resultFmt, passed);
  30911. #endif
  30912. }
  30913. static void test_wolfSSL_BIO_connect(void)
  30914. {
  30915. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  30916. tcp_ready ready;
  30917. func_args server_args;
  30918. THREAD_TYPE serverThread;
  30919. BIO *tcp_bio;
  30920. BIO *ssl_bio;
  30921. SSL_CTX* ctx;
  30922. SSL *ssl;
  30923. char msg[] = "hello wolfssl!";
  30924. char reply[30];
  30925. char buff[10] = {0};
  30926. printf(testingFmt, "wolfSSL_BIO_new_connect()");
  30927. /* Setup server */
  30928. XMEMSET(&server_args, 0, sizeof(func_args));
  30929. StartTCP();
  30930. InitTcpReady(&ready);
  30931. #if defined(USE_WINDOWS_API)
  30932. /* use RNG to get random port if using windows */
  30933. ready.port = GetRandomPort();
  30934. #endif
  30935. server_args.signal = &ready;
  30936. start_thread(test_server_nofail, &server_args, &serverThread);
  30937. wait_tcp_ready(&server_args);
  30938. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  30939. /* Start the test proper */
  30940. /* Setup the TCP BIO */
  30941. AssertNotNull(tcp_bio = BIO_new_connect(wolfSSLIP));
  30942. AssertIntEQ(BIO_set_conn_port(tcp_bio, buff), 1);
  30943. /* Setup the SSL object */
  30944. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30945. AssertIntEQ(WOLFSSL_SUCCESS,
  30946. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30947. AssertIntEQ(WOLFSSL_SUCCESS,
  30948. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30949. AssertIntEQ(WOLFSSL_SUCCESS,
  30950. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30951. AssertNotNull(ssl = SSL_new(ctx));
  30952. SSL_set_connect_state(ssl);
  30953. /* Setup the SSL BIO */
  30954. AssertNotNull(ssl_bio = BIO_new(BIO_f_ssl()));
  30955. AssertIntEQ(BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE), 1);
  30956. /* Link BIO's so that ssl_bio uses tcp_bio for IO */
  30957. AssertPtrEq(BIO_push(ssl_bio, tcp_bio), ssl_bio);
  30958. /* Do TCP connect */
  30959. AssertIntEQ(BIO_do_connect(ssl_bio), 1);
  30960. /* Do TLS handshake */
  30961. AssertIntEQ(BIO_do_handshake(ssl_bio), 1);
  30962. /* Test writing */
  30963. AssertIntEQ(BIO_write(ssl_bio, msg, sizeof(msg)), sizeof(msg));
  30964. /* Expect length of default wolfSSL reply */
  30965. AssertIntEQ(BIO_read(ssl_bio, reply, sizeof(reply)), 23);
  30966. /* Clean it all up */
  30967. BIO_free_all(ssl_bio);
  30968. SSL_CTX_free(ctx);
  30969. /* Server clean up */
  30970. join_thread(serverThread);
  30971. FreeTcpReady(&ready);
  30972. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  30973. wc_ecc_fp_free(); /* free per thread cache */
  30974. #endif
  30975. printf(resultFmt, passed);
  30976. #endif
  30977. }
  30978. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  30979. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  30980. {
  30981. BIO* clientBio;
  30982. SSL* sslClient;
  30983. SSL_CTX* ctx;
  30984. char connectAddr[20]; /* IP + port */;
  30985. (void)args;
  30986. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  30987. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  30988. AssertIntEQ(BIO_do_connect(clientBio), 1);
  30989. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  30990. AssertNotNull(sslClient = SSL_new(ctx));
  30991. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  30992. SSL_set_bio(sslClient, clientBio, clientBio);
  30993. AssertIntEQ(SSL_connect(sslClient), 1);
  30994. SSL_free(sslClient);
  30995. SSL_CTX_free(ctx);
  30996. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  30997. wc_ecc_fp_free(); /* free per thread cache */
  30998. #endif
  30999. return 0;
  31000. }
  31001. #endif
  31002. static void test_wolfSSL_BIO_accept(void)
  31003. {
  31004. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  31005. BIO* serverBindBio;
  31006. BIO* serverAcceptBio;
  31007. SSL* sslServer;
  31008. SSL_CTX* ctx;
  31009. func_args args;
  31010. THREAD_TYPE thread;
  31011. char port[10]; /* 10 bytes should be enough to store the string
  31012. * representation of the port */
  31013. printf(testingFmt, "wolfSSL_BIO_new_accept()");
  31014. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  31015. AssertNotNull(serverBindBio = BIO_new_accept(port));
  31016. /* First BIO_do_accept binds the port */
  31017. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  31018. XMEMSET(&args, 0, sizeof(func_args));
  31019. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  31020. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  31021. /* Let's plug it into SSL to test */
  31022. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  31023. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  31024. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  31025. AssertNotNull(sslServer = SSL_new(ctx));
  31026. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  31027. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  31028. AssertIntEQ(SSL_accept(sslServer), 1);
  31029. join_thread(thread);
  31030. BIO_free(serverBindBio);
  31031. SSL_free(sslServer);
  31032. SSL_CTX_free(ctx);
  31033. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  31034. wc_ecc_fp_free(); /* free per thread cache */
  31035. #endif
  31036. printf(resultFmt, passed);
  31037. #endif
  31038. }
  31039. static void test_wolfSSL_BIO_write(void)
  31040. {
  31041. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  31042. BIO* bio;
  31043. BIO* bio64;
  31044. BIO* ptr;
  31045. int sz;
  31046. char msg[] = "conversion test";
  31047. char out[40];
  31048. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  31049. void* bufPtr = NULL;
  31050. BUF_MEM* buf = NULL;
  31051. printf(testingFmt, "wolfSSL_BIO_write()");
  31052. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  31053. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  31054. /* now should convert to base64 then write to memory */
  31055. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  31056. BIO_flush(bio);
  31057. /* test BIO chain */
  31058. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  31059. AssertNotNull(buf);
  31060. AssertIntEQ(buf->length, 25);
  31061. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  31062. AssertPtrEq(buf->data, bufPtr);
  31063. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  31064. sz = sizeof(out);
  31065. XMEMSET(out, 0, sz);
  31066. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  31067. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  31068. /* write then read should return the same message */
  31069. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  31070. sz = sizeof(out);
  31071. XMEMSET(out, 0, sz);
  31072. AssertIntEQ(BIO_read(bio, out, sz), 16);
  31073. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  31074. /* now try encoding with no line ending */
  31075. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  31076. #ifdef HAVE_EX_DATA
  31077. BIO_set_ex_data(bio64, 0, (void*) "data");
  31078. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  31079. #endif
  31080. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  31081. BIO_flush(bio);
  31082. sz = sizeof(out);
  31083. XMEMSET(out, 0, sz);
  31084. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  31085. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  31086. BIO_free_all(bio); /* frees bio64 also */
  31087. /* test with more than one bio64 in list */
  31088. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  31089. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  31090. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  31091. /* now should convert to base64 when stored and then decode with read */
  31092. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  31093. BIO_flush(bio);
  31094. sz = sizeof(out);
  31095. XMEMSET(out, 0, sz);
  31096. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  31097. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  31098. BIO_clear_flags(bio64, ~0);
  31099. BIO_set_retry_read(bio);
  31100. BIO_free_all(bio); /* frees bio64s also */
  31101. printf(resultFmt, passed);
  31102. #endif
  31103. }
  31104. static void test_wolfSSL_BIO_printf(void)
  31105. {
  31106. #if defined(OPENSSL_ALL)
  31107. BIO* bio;
  31108. int sz = 7;
  31109. char msg[] = "TLS 1.3 for the world";
  31110. char out[60];
  31111. char expected[] = "TLS 1.3 for the world : sz = 7";
  31112. printf(testingFmt, "wolfSSL_BIO_printf()");
  31113. XMEMSET(out, 0, sizeof(out));
  31114. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31115. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  31116. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  31117. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  31118. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  31119. BIO_free(bio);
  31120. printf(resultFmt, passed);
  31121. #endif
  31122. }
  31123. static void test_wolfSSL_BIO_f_md(void)
  31124. {
  31125. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  31126. BIO *bio, *mem;
  31127. char msg[] = "message to hash";
  31128. char out[60];
  31129. EVP_MD_CTX* ctx;
  31130. const unsigned char testKey[] =
  31131. {
  31132. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31133. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31134. 0x0b, 0x0b, 0x0b, 0x0b
  31135. };
  31136. const char testData[] = "Hi There";
  31137. const unsigned char testResult[] =
  31138. {
  31139. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  31140. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  31141. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  31142. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  31143. };
  31144. const unsigned char expectedHash[] =
  31145. {
  31146. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  31147. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  31148. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  31149. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  31150. };
  31151. const unsigned char emptyHash[] =
  31152. {
  31153. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  31154. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  31155. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  31156. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  31157. };
  31158. unsigned char check[sizeof(testResult) + 1];
  31159. size_t checkSz = -1;
  31160. EVP_PKEY* key;
  31161. printf(testingFmt, "wolfSSL_BIO_f_md()");
  31162. XMEMSET(out, 0, sizeof(out));
  31163. AssertNotNull(bio = BIO_new(BIO_f_md()));
  31164. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  31165. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  31166. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  31167. /* should not be able to write/read yet since just digest wrapper and no
  31168. * data is passing through the bio */
  31169. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  31170. AssertIntEQ(BIO_pending(bio), 0);
  31171. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  31172. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  31173. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  31174. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  31175. BIO_reset(bio);
  31176. /* append BIO mem to bio in order to read/write */
  31177. AssertNotNull(bio = BIO_push(bio, mem));
  31178. XMEMSET(out, 0, sizeof(out));
  31179. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  31180. AssertIntEQ(BIO_pending(bio), 16);
  31181. /* this just reads the message and does not hash it (gets calls final) */
  31182. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  31183. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  31184. /* create a message digest using BIO */
  31185. XMEMSET(out, 0, sizeof(out));
  31186. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  31187. AssertIntEQ(BIO_pending(mem), 16);
  31188. AssertIntEQ(BIO_pending(bio), 16);
  31189. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  31190. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  31191. BIO_free(bio);
  31192. BIO_free(mem);
  31193. /* test with HMAC */
  31194. XMEMSET(out, 0, sizeof(out));
  31195. AssertNotNull(bio = BIO_new(BIO_f_md()));
  31196. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  31197. BIO_get_md_ctx(bio, &ctx);
  31198. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  31199. testKey, (int)sizeof(testKey)));
  31200. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  31201. AssertNotNull(bio = BIO_push(bio, mem));
  31202. BIO_write(bio, testData, (int)strlen(testData));
  31203. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  31204. EVP_DigestSignFinal(ctx, check, &checkSz);
  31205. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  31206. EVP_PKEY_free(key);
  31207. BIO_free(bio);
  31208. BIO_free(mem);
  31209. printf(resultFmt, passed);
  31210. #endif
  31211. }
  31212. #endif /* !NO_BIO */
  31213. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  31214. /* test that the callback arg is correct */
  31215. static int certCbArg = 0;
  31216. static int clientCertCb(WOLFSSL* ssl, void* arg)
  31217. {
  31218. if (ssl == NULL || arg != &certCbArg)
  31219. return 0;
  31220. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  31221. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  31222. return 0;
  31223. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  31224. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  31225. return 0;
  31226. return 1;
  31227. }
  31228. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  31229. {
  31230. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  31231. }
  31232. /**
  31233. * This is only done because test_client_nofail has no way to stop
  31234. * certificate and key loading
  31235. */
  31236. static void clientCertClearCb(WOLFSSL* ssl)
  31237. {
  31238. /* Clear the loaded certs to force the callbacks to set them up */
  31239. SSL_certs_clear(ssl);
  31240. }
  31241. static int serverCertCb(WOLFSSL* ssl, void* arg)
  31242. {
  31243. if (ssl == NULL || arg != &certCbArg)
  31244. return 0;
  31245. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  31246. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  31247. return 0;
  31248. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  31249. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  31250. return 0;
  31251. return 1;
  31252. }
  31253. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  31254. {
  31255. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  31256. }
  31257. /**
  31258. * This is only done because test_server_nofail has no way to stop
  31259. * certificate and key loading
  31260. */
  31261. static void serverCertClearCb(WOLFSSL* ssl)
  31262. {
  31263. /* Clear the loaded certs to force the callbacks to set them up */
  31264. SSL_certs_clear(ssl);
  31265. }
  31266. #endif
  31267. static void test_wolfSSL_cert_cb(void)
  31268. {
  31269. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  31270. callback_functions func_cb_client;
  31271. callback_functions func_cb_server;
  31272. tcp_ready ready;
  31273. func_args client_args;
  31274. func_args server_args;
  31275. THREAD_TYPE serverThread;
  31276. XMEMSET(&client_args, 0, sizeof(func_args));
  31277. XMEMSET(&server_args, 0, sizeof(func_args));
  31278. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  31279. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  31280. #ifdef WOLFSSL_TIRTOS
  31281. fdOpenSession(Task_self());
  31282. #endif
  31283. StartTCP();
  31284. InitTcpReady(&ready);
  31285. #if defined(USE_WINDOWS_API)
  31286. /* use RNG to get random port if using windows */
  31287. ready.port = GetRandomPort();
  31288. #endif
  31289. server_args.signal = &ready;
  31290. client_args.signal = &ready;
  31291. client_args.callbacks = &func_cb_client;
  31292. server_args.callbacks = &func_cb_server;
  31293. func_cb_client.ctx_ready = clientCertSetupCb;
  31294. func_cb_client.ssl_ready = clientCertClearCb;
  31295. func_cb_server.ctx_ready = serverCertSetupCb;
  31296. func_cb_server.ssl_ready = serverCertClearCb;
  31297. start_thread(test_server_nofail, &server_args, &serverThread);
  31298. wait_tcp_ready(&server_args);
  31299. test_client_nofail(&client_args, NULL);
  31300. join_thread(serverThread);
  31301. AssertTrue(client_args.return_code);
  31302. AssertTrue(server_args.return_code);
  31303. FreeTcpReady(&ready);
  31304. #ifdef WOLFSSL_TIRTOS
  31305. fdOpenSession(Task_self());
  31306. #endif
  31307. #endif
  31308. }
  31309. static void test_wolfSSL_SESSION(void)
  31310. {
  31311. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  31312. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  31313. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  31314. WOLFSSL* ssl;
  31315. WOLFSSL_CTX* ctx;
  31316. WOLFSSL_SESSION* sess;
  31317. WOLFSSL_SESSION* sess_copy;
  31318. unsigned char* sessDer = NULL;
  31319. unsigned char* ptr = NULL;
  31320. #ifdef OPENSSL_EXTRA
  31321. const unsigned char context[] = "user app context";
  31322. unsigned int contextSz = (unsigned int)sizeof(context);
  31323. #endif
  31324. int ret, err, sockfd, sz;
  31325. tcp_ready ready;
  31326. func_args server_args;
  31327. THREAD_TYPE serverThread;
  31328. char msg[80];
  31329. printf(testingFmt, "wolfSSL_SESSION()");
  31330. /* TLS v1.3 requires session tickets */
  31331. /* CHACHA and POLY1305 required for myTicketEncCb */
  31332. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  31333. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  31334. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  31335. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  31336. #else
  31337. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  31338. #endif
  31339. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  31340. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  31341. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  31342. #ifdef WOLFSSL_ENCRYPTED_KEYS
  31343. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31344. #endif
  31345. XMEMSET(&server_args, 0, sizeof(func_args));
  31346. #ifdef WOLFSSL_TIRTOS
  31347. fdOpenSession(Task_self());
  31348. #endif
  31349. StartTCP();
  31350. InitTcpReady(&ready);
  31351. #if defined(USE_WINDOWS_API)
  31352. /* use RNG to get random port if using windows */
  31353. ready.port = GetRandomPort();
  31354. #endif
  31355. server_args.signal = &ready;
  31356. start_thread(test_server_nofail, &server_args, &serverThread);
  31357. wait_tcp_ready(&server_args);
  31358. /* client connection */
  31359. ssl = wolfSSL_new(ctx);
  31360. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  31361. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), SSL_SUCCESS);
  31362. err = 0; /* Reset error */
  31363. do {
  31364. #ifdef WOLFSSL_ASYNC_CRYPT
  31365. if (err == WC_PENDING_E) {
  31366. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  31367. if (ret < 0) { break; } else if (ret == 0) { continue; }
  31368. }
  31369. #endif
  31370. ret = wolfSSL_connect(ssl);
  31371. if (ret != SSL_SUCCESS) {
  31372. err = wolfSSL_get_error(ssl, 0);
  31373. }
  31374. } while (ret != SSL_SUCCESS && err == WC_PENDING_E);
  31375. AssertIntEQ(ret, SSL_SUCCESS);
  31376. AssertIntEQ(wolfSSL_write(ssl, "GET", 3), 3);
  31377. AssertIntEQ(wolfSSL_read(ssl, msg, sizeof(msg)), 23);
  31378. sess = wolfSSL_get_session(ssl);
  31379. #if defined(OPENSSL_EXTRA)
  31380. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  31381. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  31382. #else
  31383. AssertIntEQ(wolfSSL_SESSION_is_resumable(NULL), 0);
  31384. AssertIntEQ(wolfSSL_SESSION_is_resumable(sess), 1);
  31385. #endif
  31386. wolfSSL_shutdown(ssl);
  31387. wolfSSL_free(ssl);
  31388. join_thread(serverThread);
  31389. FreeTcpReady(&ready);
  31390. #ifdef WOLFSSL_TIRTOS
  31391. fdOpenSession(Task_self());
  31392. #endif
  31393. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  31394. {
  31395. X509 *x509;
  31396. char buf[30];
  31397. int bufSz;
  31398. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  31399. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  31400. X509_get_subject_name(x509), NID_organizationalUnitName,
  31401. buf, sizeof(buf))), 0);
  31402. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  31403. if (bufSz == 7) {
  31404. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  31405. }
  31406. if (bufSz == 16) {
  31407. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  31408. }
  31409. }
  31410. #endif
  31411. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  31412. wolfSSL_SESSION_free(sess_copy);
  31413. /* get session from DER and update the timeout */
  31414. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  31415. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  31416. wolfSSL_SESSION_free(sess);
  31417. ptr = sessDer;
  31418. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  31419. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  31420. (const unsigned char**)&ptr, sz));
  31421. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  31422. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  31423. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  31424. /* successful set session test */
  31425. AssertNotNull(ssl = wolfSSL_new(ctx));
  31426. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_SUCCESS);
  31427. #ifdef HAVE_SESSION_TICKET
  31428. /* Test set/get session ticket */
  31429. {
  31430. const char* ticket = "This is a session ticket";
  31431. char buf[64] = {0};
  31432. word32 bufSz = (word32)sizeof(buf);
  31433. AssertIntEQ(SSL_SUCCESS,
  31434. wolfSSL_set_SessionTicket(ssl, (byte *)ticket, (word32)XSTRLEN(ticket)));
  31435. AssertIntEQ(SSL_SUCCESS,
  31436. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  31437. AssertStrEQ(ticket, buf);
  31438. }
  31439. #endif
  31440. #ifdef OPENSSL_EXTRA
  31441. /* session timeout case */
  31442. /* make the session to be expired */
  31443. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  31444. XSLEEP_MS(1200);
  31445. /* SSL_set_session should reject specified session but return success
  31446. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  31447. */
  31448. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  31449. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  31450. #else
  31451. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  31452. #endif
  31453. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  31454. /* fail case with miss match session context IDs (use compatibility API) */
  31455. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  31456. SSL_SUCCESS);
  31457. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  31458. wolfSSL_free(ssl);
  31459. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  31460. SSL_FAILURE);
  31461. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  31462. SSL_SUCCESS);
  31463. AssertNotNull(ssl = wolfSSL_new(ctx));
  31464. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  31465. #endif
  31466. wolfSSL_free(ssl);
  31467. SSL_SESSION_free(sess);
  31468. wolfSSL_CTX_free(ctx);
  31469. printf(resultFmt, passed);
  31470. #endif
  31471. }
  31472. static void test_wolfSSL_ticket_keys(void)
  31473. {
  31474. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  31475. !defined(NO_WOLFSSL_SERVER)
  31476. WOLFSSL_CTX* ctx;
  31477. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  31478. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  31479. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  31480. WOLFSSL_FAILURE);
  31481. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  31482. WOLFSSL_FAILURE);
  31483. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  31484. WOLFSSL_FAILURE);
  31485. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  31486. WOLFSSL_FAILURE);
  31487. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  31488. WOLFSSL_FAILURE);
  31489. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  31490. WOLFSSL_FAILURE);
  31491. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  31492. WOLFSSL_FAILURE);
  31493. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  31494. WOLFSSL_FAILURE);
  31495. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  31496. WOLFSSL_FAILURE);
  31497. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  31498. WOLFSSL_FAILURE);
  31499. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  31500. WOLFSSL_FAILURE);
  31501. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  31502. WOLFSSL_FAILURE);
  31503. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  31504. WOLFSSL_FAILURE);
  31505. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  31506. WOLFSSL_FAILURE);
  31507. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  31508. WOLFSSL_SUCCESS);
  31509. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  31510. WOLFSSL_SUCCESS);
  31511. wolfSSL_CTX_free(ctx);
  31512. #endif
  31513. }
  31514. #ifndef NO_BIO
  31515. static void test_wolfSSL_d2i_PUBKEY(void)
  31516. {
  31517. #if defined(OPENSSL_EXTRA)
  31518. BIO* bio;
  31519. EVP_PKEY* pkey;
  31520. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  31521. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31522. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  31523. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  31524. /* RSA PUBKEY test */
  31525. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  31526. sizeof_client_keypub_der_2048), 0);
  31527. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  31528. EVP_PKEY_free(pkey);
  31529. #endif
  31530. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  31531. /* ECC PUBKEY test */
  31532. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  31533. sizeof_ecc_clikeypub_der_256), 0);
  31534. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  31535. EVP_PKEY_free(pkey);
  31536. #endif
  31537. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  31538. /* DSA PUBKEY test */
  31539. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  31540. sizeof_dsa_pub_key_der_2048), 0);
  31541. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  31542. EVP_PKEY_free(pkey);
  31543. #endif
  31544. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  31545. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  31546. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  31547. (HAVE_FIPS_VERSION > 2))
  31548. /* DH PUBKEY test */
  31549. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  31550. sizeof_dh_pub_key_der_2048), 0);
  31551. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  31552. EVP_PKEY_free(pkey);
  31553. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  31554. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  31555. BIO_free(bio);
  31556. (void)pkey;
  31557. printf(resultFmt, passed);
  31558. #endif
  31559. }
  31560. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  31561. static void test_wolfSSL_d2i_PrivateKeys_bio(void)
  31562. {
  31563. BIO* bio = NULL;
  31564. EVP_PKEY* pkey = NULL;
  31565. #ifndef NO_RSA
  31566. #endif
  31567. WOLFSSL_CTX* ctx;
  31568. #if defined(WOLFSSL_KEY_GEN)
  31569. unsigned char buff[4096];
  31570. unsigned char* bufPtr = buff;
  31571. #endif
  31572. printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
  31573. /* test creating new EVP_PKEY with bad arg */
  31574. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  31575. /* test loading RSA key using BIO */
  31576. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  31577. {
  31578. XFILE file;
  31579. const char* fname = "./certs/server-key.der";
  31580. size_t sz;
  31581. byte* buf;
  31582. file = XFOPEN(fname, "rb");
  31583. AssertTrue((file != XBADFILE));
  31584. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31585. sz = XFTELL(file);
  31586. XREWIND(file);
  31587. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  31588. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  31589. XFCLOSE(file);
  31590. /* Test using BIO new mem and loading DER private key */
  31591. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31592. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  31593. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  31594. BIO_free(bio);
  31595. bio = NULL;
  31596. EVP_PKEY_free(pkey);
  31597. pkey = NULL;
  31598. }
  31599. #endif
  31600. /* test loading ECC key using BIO */
  31601. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31602. {
  31603. XFILE file;
  31604. const char* fname = "./certs/ecc-key.der";
  31605. size_t sz;
  31606. byte* buf;
  31607. file = XFOPEN(fname, "rb");
  31608. AssertTrue((file != XBADFILE));
  31609. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31610. sz = XFTELL(file);
  31611. XREWIND(file);
  31612. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  31613. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  31614. XFCLOSE(file);
  31615. /* Test using BIO new mem and loading DER private key */
  31616. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31617. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  31618. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  31619. BIO_free(bio);
  31620. bio = NULL;
  31621. EVP_PKEY_free(pkey);
  31622. pkey = NULL;
  31623. }
  31624. #endif
  31625. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31626. #ifndef NO_WOLFSSL_SERVER
  31627. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31628. #else
  31629. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31630. #endif
  31631. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  31632. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  31633. {
  31634. RSA* rsa = NULL;
  31635. /* Tests bad parameters */
  31636. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  31637. /* RSA not set yet, expecting to fail*/
  31638. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  31639. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  31640. /* set RSA using bio*/
  31641. AssertIntGT(BIO_write(bio, client_key_der_2048,
  31642. sizeof_client_key_der_2048), 0);
  31643. AssertNotNull(rsa = d2i_RSAPrivateKey_bio(bio, NULL));
  31644. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  31645. /*i2d RSAprivate key tests */
  31646. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  31647. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  31648. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  31649. sizeof_client_key_der_2048);
  31650. bufPtr = NULL;
  31651. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  31652. sizeof_client_key_der_2048);
  31653. AssertNotNull(bufPtr);
  31654. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  31655. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  31656. RSA_free(rsa);
  31657. }
  31658. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  31659. SSL_CTX_free(ctx);
  31660. ctx = NULL;
  31661. BIO_free(bio);
  31662. bio = NULL;
  31663. printf(resultFmt, passed);
  31664. }
  31665. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  31666. #endif /* !NO_BIO */
  31667. static void test_wolfSSL_sk_GENERAL_NAME(void)
  31668. {
  31669. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  31670. !defined(NO_RSA)
  31671. X509* x509;
  31672. GENERAL_NAME* gn;
  31673. unsigned char buf[4096];
  31674. const unsigned char* bufPt;
  31675. int bytes, i;
  31676. XFILE f;
  31677. STACK_OF(GENERAL_NAME)* sk;
  31678. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  31679. f = XFOPEN(cliCertDerFileExt, "rb");
  31680. AssertTrue((f != XBADFILE));
  31681. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  31682. XFCLOSE(f);
  31683. bufPt = buf;
  31684. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  31685. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  31686. NID_subject_alt_name, NULL, NULL));
  31687. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  31688. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  31689. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  31690. switch (gn->type) {
  31691. case GEN_DNS:
  31692. printf("found type GEN_DNS\n");
  31693. break;
  31694. case GEN_EMAIL:
  31695. printf("found type GEN_EMAIL\n");
  31696. break;
  31697. case GEN_URI:
  31698. printf("found type GEN_URI\n");
  31699. break;
  31700. }
  31701. }
  31702. X509_free(x509);
  31703. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  31704. printf(resultFmt, passed);
  31705. #endif
  31706. }
  31707. static void test_wolfSSL_GENERAL_NAME_print(void)
  31708. {
  31709. #if defined(OPENSSL_ALL)
  31710. X509* x509;
  31711. GENERAL_NAME* gn;
  31712. unsigned char buf[4096];
  31713. const unsigned char* bufPt;
  31714. int bytes;
  31715. XFILE f;
  31716. STACK_OF(GENERAL_NAME)* sk;
  31717. BIO* out;
  31718. unsigned char outbuf[128];
  31719. X509_EXTENSION* ext;
  31720. AUTHORITY_INFO_ACCESS* aia;
  31721. ACCESS_DESCRIPTION* ad;
  31722. const unsigned char v4Addr[] = {192,168,53,1};
  31723. const unsigned char v6Addr[] =
  31724. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  31725. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  31726. const unsigned char email[] =
  31727. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  31728. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  31729. const char* dnsStr = "DNS:example.com";
  31730. const char* uriStr = "URI:http://127.0.0.1:22220";
  31731. const char* v4addStr = "IP Address:192.168.53.1";
  31732. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  31733. const char* emailStr = "email:info@wolfssl.com";
  31734. const char* othrStr = "othername:<unsupported>";
  31735. const char* x400Str = "X400Name:<unsupported>";
  31736. const char* ediStr = "EdiPartyName:<unsupported>";
  31737. printf(testingFmt, "test_wolfSSL_GENERAL_NAME_print()");
  31738. /* BIO to output */
  31739. AssertNotNull(out = BIO_new(BIO_s_mem()));
  31740. /* test for NULL param */
  31741. gn = NULL;
  31742. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  31743. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  31744. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  31745. /* test for GEN_DNS */
  31746. f = XFOPEN(cliCertDerFileExt, "rb");
  31747. AssertTrue((f != XBADFILE));
  31748. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  31749. XFCLOSE(f);
  31750. bufPt = buf;
  31751. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  31752. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  31753. NID_subject_alt_name, NULL, NULL));
  31754. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  31755. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  31756. XMEMSET(outbuf,0,sizeof(outbuf));
  31757. BIO_read(out, outbuf, sizeof(outbuf));
  31758. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  31759. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  31760. X509_free(x509);
  31761. /* test for GEN_URI */
  31762. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  31763. AssertTrue((f != XBADFILE));
  31764. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  31765. XFCLOSE(f);
  31766. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  31767. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  31768. AssertNotNull(aia);
  31769. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  31770. gn = ad->location;
  31771. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  31772. XMEMSET(outbuf,0,sizeof(outbuf));
  31773. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  31774. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  31775. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  31776. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  31777. AssertNotNull(aia);
  31778. AUTHORITY_INFO_ACCESS_free(aia);
  31779. X509_free(x509);
  31780. /* test for GEN_IPADD */
  31781. /* ip v4 address */
  31782. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  31783. gn->type = GEN_IPADD;
  31784. gn->d.iPAddress->length = sizeof(v4Addr);
  31785. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  31786. sizeof(v4Addr)), 1);
  31787. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  31788. XMEMSET(outbuf,0,sizeof(outbuf));
  31789. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  31790. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  31791. GENERAL_NAME_free(gn);
  31792. /* ip v6 address */
  31793. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  31794. gn->type = GEN_IPADD;
  31795. gn->d.iPAddress->length = sizeof(v6Addr);
  31796. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  31797. sizeof(v6Addr)), 1);
  31798. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  31799. XMEMSET(outbuf,0,sizeof(outbuf));
  31800. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  31801. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  31802. GENERAL_NAME_free(gn);
  31803. /* test for GEN_EMAIL */
  31804. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  31805. gn->type = GEN_EMAIL;
  31806. gn->d.rfc822Name->length = sizeof(email);
  31807. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  31808. sizeof(email)), 1);
  31809. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  31810. XMEMSET(outbuf,0,sizeof(outbuf));
  31811. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  31812. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  31813. GENERAL_NAME_free(gn);
  31814. /* test for GEN_OTHERNAME */
  31815. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  31816. gn->type = GEN_OTHERNAME;
  31817. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  31818. XMEMSET(outbuf,0,sizeof(outbuf));
  31819. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  31820. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  31821. GENERAL_NAME_free(gn);
  31822. /* test for GEN_X400 */
  31823. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  31824. gn->type = GEN_X400;
  31825. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  31826. XMEMSET(outbuf,0,sizeof(outbuf));
  31827. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  31828. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  31829. GENERAL_NAME_free(gn);
  31830. /* test for GEN_EDIPARTY */
  31831. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  31832. gn->type = GEN_EDIPARTY;
  31833. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  31834. XMEMSET(outbuf,0,sizeof(outbuf));
  31835. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  31836. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  31837. GENERAL_NAME_free(gn);
  31838. BIO_free(out);
  31839. printf(resultFmt, passed);
  31840. #endif /* OPENSSL_ALL */
  31841. }
  31842. static void test_wolfSSL_MD4(void)
  31843. {
  31844. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  31845. MD4_CTX md4;
  31846. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  31847. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  31848. "789012345678901234567890";
  31849. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  31850. "\xcc\x05\x36";
  31851. int msgSz = (int)XSTRLEN(msg);
  31852. printf(testingFmt, "wolfSSL_MD4()");
  31853. XMEMSET(out, 0, sizeof(out));
  31854. MD4_Init(&md4);
  31855. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  31856. MD4_Final(out, &md4);
  31857. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  31858. printf(resultFmt, passed);
  31859. #endif
  31860. }
  31861. static void test_wolfSSL_RSA(void)
  31862. {
  31863. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  31864. defined(WOLFSSL_KEY_GEN)
  31865. RSA* rsa;
  31866. const BIGNUM *n;
  31867. const BIGNUM *e;
  31868. const BIGNUM *d;
  31869. const BIGNUM *p;
  31870. const BIGNUM *q;
  31871. const BIGNUM *dmp1;
  31872. const BIGNUM *dmq1;
  31873. const BIGNUM *iqmp;
  31874. printf(testingFmt, "wolfSSL_RSA()");
  31875. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  31876. AssertIntEQ(RSA_size(rsa), 256);
  31877. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(HAVE_FAST_RSA) && \
  31878. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  31879. (HAVE_FIPS_VERSION >= 2))) && !defined(HAVE_SELFTEST) && \
  31880. !defined(HAVE_INTEL_QA) && !defined(WOLFSSL_NO_RSA_KEY_CHECK)
  31881. AssertIntEQ(RSA_check_key(rsa), WOLFSSL_SUCCESS);
  31882. #endif
  31883. /* sanity check */
  31884. AssertIntEQ(RSA_bits(NULL), 0);
  31885. /* key */
  31886. AssertIntEQ(RSA_bits(rsa), 2048);
  31887. RSA_get0_key(rsa, &n, &e, &d);
  31888. AssertPtrEq(rsa->n, n);
  31889. AssertPtrEq(rsa->e, e);
  31890. AssertPtrEq(rsa->d, d);
  31891. AssertNotNull(n = BN_new());
  31892. AssertNotNull(e = BN_new());
  31893. AssertNotNull(d = BN_new());
  31894. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  31895. AssertPtrEq(rsa->n, n);
  31896. AssertPtrEq(rsa->e, e);
  31897. AssertPtrEq(rsa->d, d);
  31898. /* crt_params */
  31899. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  31900. AssertPtrEq(rsa->dmp1, dmp1);
  31901. AssertPtrEq(rsa->dmq1, dmq1);
  31902. AssertPtrEq(rsa->iqmp, iqmp);
  31903. AssertNotNull(dmp1 = BN_new());
  31904. AssertNotNull(dmq1 = BN_new());
  31905. AssertNotNull(iqmp = BN_new());
  31906. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1, (BIGNUM*)iqmp), 1);
  31907. AssertPtrEq(rsa->dmp1, dmp1);
  31908. AssertPtrEq(rsa->dmq1, dmq1);
  31909. AssertPtrEq(rsa->iqmp, iqmp);
  31910. /* factors */
  31911. RSA_get0_factors(rsa, &p, &q);
  31912. AssertPtrEq(rsa->p, p);
  31913. AssertPtrEq(rsa->q, q);
  31914. AssertNotNull(p = BN_new());
  31915. AssertNotNull(q = BN_new());
  31916. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  31917. AssertPtrEq(rsa->p, p);
  31918. AssertPtrEq(rsa->q, q);
  31919. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  31920. AssertIntEQ(RSA_bits(rsa), 21);
  31921. RSA_free(rsa);
  31922. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  31923. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  31924. AssertIntEQ(RSA_size(rsa), 384);
  31925. AssertIntEQ(RSA_bits(rsa), 3072);
  31926. RSA_free(rsa);
  31927. #endif
  31928. /* remove for now with odd key size until adjusting rsa key size check with
  31929. wc_MakeRsaKey()
  31930. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  31931. RSA_free(rsa);
  31932. */
  31933. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  31934. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  31935. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  31936. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  31937. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  31938. {
  31939. byte buff[FOURK_BUF];
  31940. byte der[FOURK_BUF];
  31941. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  31942. XFILE f;
  31943. int bytes;
  31944. /* test loading encrypted RSA private pem w/o password */
  31945. f = XFOPEN(PrivKeyPemFile, "rb");
  31946. AssertTrue((f != XBADFILE));
  31947. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  31948. XFCLOSE(f);
  31949. XMEMSET(der, 0, sizeof(der));
  31950. /* test that error value is returned with no password */
  31951. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  31952. }
  31953. #endif
  31954. printf(resultFmt, passed);
  31955. #endif
  31956. }
  31957. static void test_wolfSSL_RSA_DER(void)
  31958. {
  31959. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  31960. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  31961. RSA *rsa;
  31962. int i;
  31963. unsigned char *buff = NULL;
  31964. struct tbl_s
  31965. {
  31966. const unsigned char *der;
  31967. int sz;
  31968. } tbl[] = {
  31969. #ifdef USE_CERT_BUFFERS_1024
  31970. {client_key_der_1024, sizeof_client_key_der_1024},
  31971. {server_key_der_1024, sizeof_server_key_der_1024},
  31972. #endif
  31973. #ifdef USE_CERT_BUFFERS_2048
  31974. {client_key_der_2048, sizeof_client_key_der_2048},
  31975. {server_key_der_2048, sizeof_server_key_der_2048},
  31976. #endif
  31977. {NULL, 0}
  31978. };
  31979. /* Public Key DER */
  31980. struct tbl_s pub[] = {
  31981. #ifdef USE_CERT_BUFFERS_1024
  31982. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  31983. #endif
  31984. #ifdef USE_CERT_BUFFERS_2048
  31985. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  31986. #endif
  31987. {NULL, 0}
  31988. };
  31989. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  31990. for (i = 0; tbl[i].der != NULL; i++)
  31991. {
  31992. AssertNotNull(d2i_RSAPublicKey(&rsa, &tbl[i].der, tbl[i].sz));
  31993. AssertNotNull(rsa);
  31994. RSA_free(rsa);
  31995. }
  31996. for (i = 0; tbl[i].der != NULL; i++)
  31997. {
  31998. AssertNotNull(d2i_RSAPrivateKey(&rsa, &tbl[i].der, tbl[i].sz));
  31999. AssertNotNull(rsa);
  32000. RSA_free(rsa);
  32001. }
  32002. for (i = 0; pub[i].der != NULL; i++)
  32003. {
  32004. AssertNotNull(d2i_RSAPublicKey(&rsa, &pub[i].der, pub[i].sz));
  32005. AssertNotNull(rsa);
  32006. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  32007. buff = NULL;
  32008. AssertIntEQ(i2d_RSAPublicKey(rsa, &buff), pub[i].sz);
  32009. AssertNotNull(buff);
  32010. AssertIntEQ(0, memcmp((void *)buff, (void *)pub[i].der, pub[i].sz));
  32011. XFREE((void *)buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  32012. RSA_free(rsa);
  32013. }
  32014. printf(resultFmt, passed);
  32015. #endif
  32016. }
  32017. static void test_wolfSSL_RSA_get0_key(void)
  32018. {
  32019. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  32020. RSA *rsa = NULL;
  32021. const BIGNUM* n = NULL;
  32022. const BIGNUM* e = NULL;
  32023. const BIGNUM* d = NULL;
  32024. const unsigned char* der;
  32025. int derSz;
  32026. #ifdef USE_CERT_BUFFERS_1024
  32027. der = client_key_der_1024;
  32028. derSz = sizeof_client_key_der_1024;
  32029. #elif defined(USE_CERT_BUFFERS_2048)
  32030. der = client_key_der_2048;
  32031. derSz = sizeof_client_key_der_2048;
  32032. #else
  32033. der = NULL;
  32034. derSz = 0;
  32035. #endif
  32036. printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
  32037. if (der != NULL) {
  32038. RSA_get0_key(NULL, NULL, NULL, NULL);
  32039. RSA_get0_key(rsa, NULL, NULL, NULL);
  32040. RSA_get0_key(NULL, &n, &e, &d);
  32041. AssertNull(n);
  32042. AssertNull(e);
  32043. AssertNull(d);
  32044. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  32045. AssertNotNull(rsa);
  32046. RSA_get0_key(rsa, NULL, NULL, NULL);
  32047. RSA_get0_key(rsa, &n, NULL, NULL);
  32048. AssertNotNull(n);
  32049. RSA_get0_key(rsa, NULL, &e, NULL);
  32050. AssertNotNull(e);
  32051. RSA_get0_key(rsa, NULL, NULL, &d);
  32052. AssertNotNull(d);
  32053. RSA_get0_key(rsa, &n, &e, &d);
  32054. AssertNotNull(n);
  32055. AssertNotNull(e);
  32056. AssertNotNull(d);
  32057. RSA_free(rsa);
  32058. }
  32059. printf(resultFmt, passed);
  32060. #endif
  32061. }
  32062. static void test_wolfSSL_RSA_meth(void)
  32063. {
  32064. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  32065. RSA *rsa;
  32066. RSA_METHOD *rsa_meth;
  32067. printf(testingFmt, "test_wolfSSL_RSA_meth");
  32068. #ifdef WOLFSSL_KEY_GEN
  32069. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  32070. RSA_free(rsa);
  32071. #else
  32072. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  32073. #endif
  32074. AssertNotNull(rsa_meth =
  32075. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  32076. #ifndef NO_WOLFSSL_STUB
  32077. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  32078. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  32079. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  32080. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  32081. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  32082. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  32083. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  32084. #endif
  32085. AssertNotNull(rsa = RSA_new());
  32086. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  32087. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  32088. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  32089. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  32090. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  32091. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC | RSA_METHOD_FLAG_NO_CHECK);
  32092. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  32093. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  32094. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  32095. /* rsa_meth is freed here */
  32096. RSA_free(rsa);
  32097. printf(resultFmt, passed);
  32098. #endif
  32099. }
  32100. static void test_wolfSSL_verify_mode(void)
  32101. {
  32102. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  32103. WOLFSSL* ssl;
  32104. WOLFSSL_CTX* ctx;
  32105. printf(testingFmt, "test_wolfSSL_verify()");
  32106. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32107. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  32108. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  32109. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  32110. AssertNotNull(ssl = SSL_new(ctx));
  32111. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  32112. SSL_free(ssl);
  32113. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  32114. AssertNotNull(ssl = SSL_new(ctx));
  32115. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  32116. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  32117. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  32118. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  32119. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  32120. SSL_free(ssl);
  32121. wolfSSL_CTX_set_verify(ctx,
  32122. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  32123. AssertNotNull(ssl = SSL_new(ctx));
  32124. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  32125. AssertIntEQ(SSL_get_verify_mode(ssl),
  32126. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  32127. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  32128. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  32129. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  32130. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  32131. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  32132. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  32133. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  32134. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  32135. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  32136. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  32137. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32138. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  32139. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  32140. #endif
  32141. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  32142. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  32143. SSL_free(ssl);
  32144. SSL_CTX_free(ctx);
  32145. printf(resultFmt, passed);
  32146. #endif
  32147. }
  32148. static void test_wolfSSL_verify_depth(void)
  32149. {
  32150. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  32151. WOLFSSL* ssl;
  32152. WOLFSSL_CTX* ctx;
  32153. long depth;
  32154. printf(testingFmt, "test_wolfSSL_verify_depth()");
  32155. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32156. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  32157. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  32158. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  32159. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  32160. AssertNotNull(ssl = SSL_new(ctx));
  32161. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  32162. SSL_free(ssl);
  32163. SSL_CTX_set_verify_depth(ctx, -1);
  32164. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  32165. SSL_CTX_set_verify_depth(ctx, 2);
  32166. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  32167. AssertNotNull(ssl = SSL_new(ctx));
  32168. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  32169. SSL_free(ssl);
  32170. SSL_CTX_free(ctx);
  32171. printf(resultFmt, passed);
  32172. #endif
  32173. }
  32174. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  32175. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  32176. * buffer of 64 bytes.
  32177. *
  32178. * returns the size of the digest buffer on success and a negative value on
  32179. * failure.
  32180. */
  32181. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  32182. {
  32183. HMAC_CTX ctx1;
  32184. HMAC_CTX ctx2;
  32185. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  32186. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  32187. unsigned char long_key[] =
  32188. "0123456789012345678901234567890123456789"
  32189. "0123456789012345678901234567890123456789"
  32190. "0123456789012345678901234567890123456789"
  32191. "0123456789012345678901234567890123456789";
  32192. unsigned char msg[] = "message to hash";
  32193. unsigned int digestSz = 64;
  32194. int keySz = sizeof(key);
  32195. int long_keySz = sizeof(long_key);
  32196. int msgSz = sizeof(msg);
  32197. unsigned char digest2[64];
  32198. unsigned int digestSz2 = 64;
  32199. HMAC_CTX_init(&ctx1);
  32200. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  32201. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32202. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  32203. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32204. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  32205. HMAC_CTX_cleanup(&ctx1);
  32206. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  32207. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  32208. HMAC_CTX_cleanup(&ctx2);
  32209. AssertIntEQ(digestSz, digestSz2);
  32210. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  32211. /* test HMAC_Init with NULL key */
  32212. /* init after copy */
  32213. printf("test HMAC_Init with NULL key (0)\n");
  32214. HMAC_CTX_init(&ctx1);
  32215. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  32216. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32217. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  32218. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  32219. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32220. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32221. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  32222. HMAC_CTX_cleanup(&ctx1);
  32223. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  32224. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  32225. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  32226. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  32227. HMAC_CTX_cleanup(&ctx2);
  32228. AssertIntEQ(digestSz, digestSz2);
  32229. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  32230. /* long key */
  32231. printf("test HMAC_Init with NULL key (1)\n");
  32232. HMAC_CTX_init(&ctx1);
  32233. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  32234. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32235. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  32236. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  32237. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32238. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32239. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  32240. HMAC_CTX_cleanup(&ctx1);
  32241. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  32242. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  32243. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  32244. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  32245. HMAC_CTX_cleanup(&ctx2);
  32246. AssertIntEQ(digestSz, digestSz2);
  32247. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  32248. /* init before copy */
  32249. printf("test HMAC_Init with NULL key (2)\n");
  32250. HMAC_CTX_init(&ctx1);
  32251. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  32252. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32253. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  32254. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  32255. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32256. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  32257. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  32258. HMAC_CTX_cleanup(&ctx1);
  32259. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  32260. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  32261. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  32262. HMAC_CTX_cleanup(&ctx2);
  32263. AssertIntEQ(digestSz, digestSz2);
  32264. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  32265. return digestSz;
  32266. }
  32267. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  32268. static void test_wolfSSL_HMAC_CTX(void)
  32269. {
  32270. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  32271. unsigned char digest[64];
  32272. int digestSz;
  32273. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  32274. #ifndef NO_SHA
  32275. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  32276. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  32277. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  32278. #endif /* !NO_SHA */
  32279. #ifdef WOLFSSL_SHA224
  32280. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  32281. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  32282. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  32283. "\x02\x0E", digest, digestSz), 0);
  32284. #endif /* WOLFSSL_SHA224 */
  32285. #ifndef NO_SHA256
  32286. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  32287. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  32288. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  32289. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  32290. #endif /* !NO_SHA256 */
  32291. #ifdef WOLFSSL_SHA384
  32292. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  32293. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  32294. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  32295. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  32296. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  32297. digest, digestSz), 0);
  32298. #endif /* WOLFSSL_SHA384 */
  32299. #ifdef WOLFSSL_SHA512
  32300. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  32301. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  32302. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  32303. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  32304. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  32305. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  32306. digest, digestSz), 0);
  32307. #endif /* WOLFSSL_SHA512 */
  32308. #ifndef NO_MD5
  32309. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  32310. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  32311. "\xE4\x98\xDD", digest, digestSz), 0);
  32312. #endif /* !NO_MD5 */
  32313. printf(resultFmt, passed);
  32314. #endif
  32315. }
  32316. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  32317. static void sslMsgCb(int w, int version, int type, const void* buf,
  32318. size_t sz, SSL* ssl, void* arg)
  32319. {
  32320. int i;
  32321. unsigned char* pt = (unsigned char*)buf;
  32322. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  32323. (int)sz, version, type);
  32324. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  32325. printf("\n");
  32326. (void)ssl;
  32327. (void)arg;
  32328. }
  32329. #endif /* OPENSSL_EXTRA */
  32330. static void test_wolfSSL_msg_callback(void)
  32331. {
  32332. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  32333. WOLFSSL* ssl;
  32334. WOLFSSL_CTX* ctx;
  32335. printf(testingFmt, "wolfSSL_msg_callback()");
  32336. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32337. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  32338. SSL_FILETYPE_PEM));
  32339. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  32340. SSL_FILETYPE_PEM));
  32341. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  32342. SSL_SUCCESS);
  32343. AssertNotNull(ssl = SSL_new(ctx));
  32344. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  32345. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  32346. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  32347. SSL_free(ssl);
  32348. SSL_CTX_free(ctx);
  32349. printf(resultFmt, passed);
  32350. #endif
  32351. }
  32352. static void test_wolfSSL_SHA(void)
  32353. {
  32354. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  32355. printf(testingFmt, "wolfSSL_SHA()");
  32356. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES)
  32357. {
  32358. const unsigned char in[] = "abc";
  32359. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  32360. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  32361. unsigned char out[WC_SHA_DIGEST_SIZE];
  32362. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  32363. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  32364. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  32365. /* SHA interface test */
  32366. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  32367. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  32368. AssertNotNull(SHA(in, 0, out));
  32369. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  32370. AssertNotNull(SHA(NULL, 0, out));
  32371. AssertNotNull(SHA(NULL, 0, NULL));
  32372. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  32373. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  32374. }
  32375. #endif
  32376. #if !defined(NO_SHA256)
  32377. {
  32378. const unsigned char in[] = "abc";
  32379. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  32380. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  32381. "\x15\xAD";
  32382. unsigned char out[WC_SHA256_DIGEST_SIZE];
  32383. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  32384. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  32385. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  32386. #else
  32387. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  32388. #endif
  32389. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  32390. }
  32391. #endif
  32392. #if defined(WOLFSSL_SHA384)
  32393. {
  32394. const unsigned char in[] = "abc";
  32395. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  32396. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  32397. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  32398. "\xc8\x25\xa7";
  32399. unsigned char out[WC_SHA384_DIGEST_SIZE];
  32400. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  32401. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  32402. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  32403. #else
  32404. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  32405. #endif
  32406. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  32407. }
  32408. #endif
  32409. #if defined(WOLFSSL_SHA512)
  32410. {
  32411. const unsigned char in[] = "abc";
  32412. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  32413. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  32414. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  32415. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  32416. "\xa5\x4c\xa4\x9f";
  32417. unsigned char out[WC_SHA512_DIGEST_SIZE];
  32418. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  32419. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  32420. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  32421. #else
  32422. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  32423. #endif
  32424. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  32425. }
  32426. #endif
  32427. printf(resultFmt, passed);
  32428. #endif
  32429. }
  32430. static void test_wolfSSL_DH_1536_prime(void)
  32431. {
  32432. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  32433. BIGNUM* bn;
  32434. unsigned char bits[200];
  32435. int sz = 192; /* known binary size */
  32436. const byte expected[] = {
  32437. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  32438. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  32439. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  32440. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  32441. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  32442. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  32443. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  32444. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  32445. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  32446. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  32447. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  32448. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  32449. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  32450. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  32451. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  32452. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  32453. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  32454. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  32455. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  32456. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  32457. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  32458. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  32459. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  32460. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  32461. };
  32462. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  32463. bn = get_rfc3526_prime_1536(NULL);
  32464. AssertNotNull(bn);
  32465. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  32466. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  32467. BN_free(bn);
  32468. printf(resultFmt, passed);
  32469. #endif
  32470. }
  32471. static void test_wolfSSL_PEM_write_DHparams(void)
  32472. {
  32473. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  32474. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  32475. DH* dh;
  32476. BIO* bio;
  32477. XFILE fp;
  32478. byte pem[2048];
  32479. int pemSz;
  32480. const char expected[] =
  32481. "-----BEGIN DH PARAMETERS-----\n\
  32482. MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n\
  32483. v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n\
  32484. nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n\
  32485. joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n\
  32486. wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n\
  32487. tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n\
  32488. -----END DH PARAMETERS-----\n";
  32489. printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
  32490. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  32491. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  32492. XFCLOSE(fp);
  32493. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32494. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  32495. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  32496. BIO_free(bio);
  32497. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  32498. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  32499. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  32500. XFCLOSE(fp);
  32501. DH_free(dh);
  32502. /* check results */
  32503. XMEMSET(pem, 0, sizeof(pem));
  32504. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  32505. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  32506. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  32507. XFCLOSE(fp);
  32508. printf(resultFmt, passed);
  32509. #endif
  32510. }
  32511. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  32512. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  32513. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  32514. static void binary_dump(void *ptr, int size)
  32515. {
  32516. #ifdef WOLFSSL_EVP_PRINT
  32517. int i = 0;
  32518. unsigned char *p = (unsigned char *) ptr;
  32519. printf("{");
  32520. while((p != NULL) && (i < size)) {
  32521. if((i % 8) == 0) {
  32522. printf("\n");
  32523. printf(" ");
  32524. }
  32525. printf("0x%02x, ", p[i]);
  32526. i++;
  32527. }
  32528. printf("\n};\n");
  32529. #else
  32530. (void) ptr;
  32531. (void) size;
  32532. #endif
  32533. }
  32534. static int last_val = 0x0f;
  32535. static int check_result(unsigned char *data, int len)
  32536. {
  32537. int i;
  32538. for( ; len; ) {
  32539. last_val = (last_val + 1) % 16;
  32540. for(i = 0; i < 16; len--, i++, data++)
  32541. if(*data != last_val) {
  32542. return -1;
  32543. }
  32544. }
  32545. return 0;
  32546. }
  32547. static int r_offset;
  32548. static int w_offset;
  32549. static void init_offset(void)
  32550. {
  32551. r_offset = 0;
  32552. w_offset = 0;
  32553. }
  32554. static void get_record(unsigned char *data, unsigned char *buf, int len)
  32555. {
  32556. XMEMCPY(buf, data+r_offset, len);
  32557. r_offset += len;
  32558. }
  32559. static void set_record(unsigned char *data, unsigned char *buf, int len)
  32560. {
  32561. XMEMCPY(data+w_offset, buf, len);
  32562. w_offset += len;
  32563. }
  32564. static void set_plain(unsigned char *plain, int rec)
  32565. {
  32566. int i, j;
  32567. unsigned char *p = plain;
  32568. #define BLOCKSZ 16
  32569. for(i=0; i<(rec/BLOCKSZ); i++){
  32570. for(j=0; j<BLOCKSZ; j++)
  32571. *p++ = (i % 16);
  32572. }
  32573. }
  32574. #endif
  32575. static int test_wolfSSL_EVP_Cipher_extra(void)
  32576. {
  32577. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  32578. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  32579. /* aes128-cbc, keylen=16, ivlen=16 */
  32580. byte aes128_cbc_key[] = {
  32581. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  32582. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  32583. };
  32584. byte aes128_cbc_iv[] = {
  32585. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  32586. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  32587. };
  32588. /* teset data size table */
  32589. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  32590. int test_drive2[] = {8, 3, 8, 512, 0};
  32591. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  32592. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  32593. int test_drive_len[100];
  32594. int drive_len;
  32595. int ret = 0;
  32596. EVP_CIPHER_CTX *evp = NULL;
  32597. int ilen = 0;
  32598. int klen = 0;
  32599. int i, j;
  32600. const EVP_CIPHER *type;
  32601. byte *iv;
  32602. byte *key;
  32603. int ivlen;
  32604. int keylen;
  32605. #define RECORDS 16
  32606. #define BUFFSZ 512
  32607. byte plain [BUFFSZ * RECORDS];
  32608. byte cipher[BUFFSZ * RECORDS];
  32609. byte inb[BUFFSZ];
  32610. byte outb[BUFFSZ+16];
  32611. int outl, inl;
  32612. iv = aes128_cbc_iv;
  32613. ivlen = sizeof(aes128_cbc_iv);
  32614. key = aes128_cbc_key;
  32615. keylen = sizeof(aes128_cbc_key);
  32616. type = EVP_aes_128_cbc();
  32617. set_plain(plain, BUFFSZ * RECORDS);
  32618. SSL_library_init();
  32619. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  32620. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  32621. klen = EVP_CIPHER_CTX_key_length(evp);
  32622. if (klen > 0 && keylen != klen) {
  32623. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  32624. }
  32625. ilen = EVP_CIPHER_CTX_iv_length(evp);
  32626. if (ilen > 0 && ivlen != ilen) {
  32627. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  32628. }
  32629. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  32630. for (j = 0; j<RECORDS; j++)
  32631. {
  32632. inl = BUFFSZ;
  32633. get_record(plain, inb, inl);
  32634. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  32635. set_record(cipher, outb, outl);
  32636. }
  32637. for (i = 0; test_drive[i]; i++) {
  32638. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  32639. init_offset();
  32640. test_drive_len[i] = 0;
  32641. for (j = 0; test_drive[i][j]; j++)
  32642. {
  32643. inl = test_drive[i][j];
  32644. test_drive_len[i] += inl;
  32645. get_record(plain, inb, inl);
  32646. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  32647. /* output to cipher buffer, so that following Dec test can detect
  32648. if any error */
  32649. set_record(cipher, outb, outl);
  32650. }
  32651. EVP_CipherFinal(evp, outb, &outl);
  32652. if(outl > 0)
  32653. set_record(cipher, outb, outl);
  32654. }
  32655. for (i = 0; test_drive[i]; i++) {
  32656. last_val = 0x0f;
  32657. drive_len = 0;
  32658. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  32659. init_offset();
  32660. for (j = 0; test_drive[i][j]; j++){
  32661. inl = test_drive[i][j];
  32662. get_record(cipher, inb, inl);
  32663. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  32664. binary_dump(outb, outl);
  32665. AssertIntEQ((ret = check_result(outb, outl)), 0);
  32666. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  32667. drive_len += outl;
  32668. }
  32669. ret = EVP_CipherFinal(evp, outb, &outl);
  32670. binary_dump(outb, outl);
  32671. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  32672. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  32673. AssertTrue(ret);
  32674. }
  32675. EVP_CIPHER_CTX_free(evp);
  32676. #endif /* test_EVP_Cipher */
  32677. return 0;
  32678. }
  32679. static void test_wolfSSL_AES_ecb_encrypt(void)
  32680. {
  32681. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  32682. AES_KEY aes;
  32683. const byte msg[] =
  32684. {
  32685. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  32686. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  32687. };
  32688. const byte verify[] =
  32689. {
  32690. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  32691. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  32692. };
  32693. const byte key[] =
  32694. {
  32695. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  32696. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  32697. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  32698. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  32699. };
  32700. byte out[AES_BLOCK_SIZE];
  32701. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  32702. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  32703. XMEMSET(out, 0, AES_BLOCK_SIZE);
  32704. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  32705. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  32706. #ifdef HAVE_AES_DECRYPT
  32707. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  32708. XMEMSET(out, 0, AES_BLOCK_SIZE);
  32709. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  32710. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  32711. #endif
  32712. /* test bad arguments */
  32713. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  32714. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  32715. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  32716. printf(resultFmt, passed);
  32717. #endif
  32718. }
  32719. static void test_wolfSSL_MD5(void)
  32720. {
  32721. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  32722. byte input1[] = "";
  32723. byte input2[] = "message digest";
  32724. byte hash[WC_MD5_DIGEST_SIZE];
  32725. unsigned char output1[] =
  32726. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  32727. unsigned char output2[] =
  32728. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  32729. WOLFSSL_MD5_CTX md5;
  32730. printf(testingFmt, "wolfSSL_MD5()");
  32731. XMEMSET(&md5, 0, sizeof(md5));
  32732. /* Test cases for illegal parameters */
  32733. AssertIntEQ(MD5_Init(NULL), 0);
  32734. AssertIntEQ(MD5_Init(&md5), 1);
  32735. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  32736. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  32737. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  32738. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  32739. AssertIntEQ(MD5_Final(hash, NULL), 0);
  32740. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  32741. /* Init MD5 CTX */
  32742. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  32743. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  32744. XSTRLEN((const char*)&input1)), 1);
  32745. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  32746. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  32747. /* Init MD5 CTX */
  32748. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  32749. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  32750. (int)XSTRLEN((const char*)input2)), 1);
  32751. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  32752. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  32753. #if !defined(NO_OLD_NAMES) && \
  32754. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  32755. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  32756. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  32757. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  32758. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  32759. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  32760. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  32761. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  32762. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  32763. {
  32764. byte data[] = "Data to be hashed.";
  32765. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  32766. AssertNotNull(MD5(data, sizeof(data), NULL));
  32767. AssertNotNull(MD5(data, sizeof(data), hash));
  32768. AssertNotNull(MD5(NULL, 0, hash));
  32769. AssertNull(MD5(NULL, sizeof(data), hash));
  32770. }
  32771. #endif
  32772. printf(resultFmt, passed);
  32773. #endif
  32774. }
  32775. static void test_wolfSSL_MD5_Transform(void)
  32776. {
  32777. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  32778. byte input1[] = "";
  32779. byte input2[] = "abc";
  32780. byte local[WC_MD5_BLOCK_SIZE];
  32781. word32 sLen = 0;
  32782. #ifdef BIG_ENDIAN_ORDER
  32783. unsigned char output1[] =
  32784. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  32785. unsigned char output2[] =
  32786. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  32787. #else
  32788. unsigned char output1[] =
  32789. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  32790. unsigned char output2[] =
  32791. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  32792. #endif
  32793. MD5_CTX md5;
  32794. printf(testingFmt, "wolfSSL_MD5_Transform()");
  32795. XMEMSET(&md5, 0, sizeof(md5));
  32796. XMEMSET(&local, 0, sizeof(local));
  32797. /* sanity check */
  32798. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  32799. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  32800. AssertIntEQ(MD5_Transform(&md5, NULL), 0);
  32801. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  32802. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  32803. AssertIntEQ(wc_Md5Transform((wc_Md5*)&md5, NULL), BAD_FUNC_ARG);
  32804. /* Init MD5 CTX */
  32805. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  32806. /* Do Transform*/
  32807. sLen = (word32)XSTRLEN((char*)input1);
  32808. XMEMCPY(local, input1, sLen);
  32809. AssertIntEQ(MD5_Transform(&md5, (const byte*)&local[0]), 1);
  32810. AssertIntEQ(XMEMCMP(&((wc_Md5*)&md5)->digest[0], output1,
  32811. WC_MD5_DIGEST_SIZE), 0);
  32812. /* Init MD5 CTX */
  32813. AssertIntEQ(MD5_Init(&md5), 1);
  32814. sLen = (word32)XSTRLEN((char*)input2);
  32815. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  32816. XMEMCPY(local, input2, sLen);
  32817. AssertIntEQ(MD5_Transform(&md5, (const byte*)&local[0]), 1);
  32818. AssertIntEQ(XMEMCMP(&((wc_Md5*)&md5)->digest[0], output2,
  32819. WC_MD5_DIGEST_SIZE), 0);
  32820. printf(resultFmt, passed);
  32821. #endif
  32822. }
  32823. static void test_wolfSSL_SHA224(void)
  32824. {
  32825. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  32826. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  32827. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  32828. unsigned char input[] =
  32829. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  32830. unsigned char output[] =
  32831. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  32832. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  32833. size_t inLen;
  32834. byte hash[WC_SHA224_DIGEST_SIZE];
  32835. printf(testingFmt, "wolfSSL_SHA224()");
  32836. inLen = XSTRLEN((char*)input);
  32837. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  32838. AssertNull(SHA224(NULL, inLen, hash));
  32839. AssertNotNull(SHA224(input, 0, hash));
  32840. AssertNotNull(SHA224(input, inLen, NULL));
  32841. AssertNotNull(SHA224(NULL, 0, hash));
  32842. AssertNotNull(SHA224(NULL, 0, NULL));
  32843. AssertNotNull(SHA224(input, inLen, hash));
  32844. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  32845. printf(resultFmt, passed);
  32846. #endif
  32847. }
  32848. static void test_wolfSSL_SHA_Transform(void)
  32849. {
  32850. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  32851. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  32852. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  32853. byte input1[] = "";
  32854. byte input2[] = "abc";
  32855. byte local[WC_SHA_BLOCK_SIZE];
  32856. word32 sLen = 0;
  32857. #ifdef BIG_ENDIAN_ORDER
  32858. unsigned char output1[] =
  32859. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  32860. "\x09\xf7\x3a\x93";
  32861. unsigned char output2[] =
  32862. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  32863. "\x7c\x6e\x08\xb8";
  32864. #else
  32865. unsigned char output1[] =
  32866. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  32867. "\x93\x3a\xf7\x09";
  32868. unsigned char output2[] =
  32869. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  32870. "\xb8\x08\x6e\x7c";
  32871. #endif
  32872. SHA_CTX sha;
  32873. SHA_CTX sha1;
  32874. printf(testingFmt, "wolfSSL_SHA_Transform()");
  32875. XMEMSET(&sha, 0, sizeof(sha));
  32876. XMEMSET(&local, 0, sizeof(local));
  32877. /* sanity check */
  32878. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  32879. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  32880. AssertIntEQ(SHA_Transform(&sha, NULL), 0);
  32881. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  32882. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  32883. AssertIntEQ(SHA1_Transform(&sha, NULL), 0);
  32884. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  32885. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  32886. AssertIntEQ(wc_ShaTransform((wc_Sha*)&sha, NULL), BAD_FUNC_ARG);
  32887. /* Init SHA CTX */
  32888. AssertIntEQ(SHA_Init(&sha), 1);
  32889. /* Do Transform*/
  32890. sLen = (word32)XSTRLEN((char*)input1);
  32891. XMEMCPY(local, input1, sLen);
  32892. AssertIntEQ(SHA_Transform(&sha, (const byte*)&local[0]), 1);
  32893. AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha)->digest[0], output1,
  32894. WC_SHA_DIGEST_SIZE), 0);
  32895. AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
  32896. /* Init SHA CTX */
  32897. AssertIntEQ(SHA_Init(&sha), 1);
  32898. sLen = (word32)XSTRLEN((char*)input2);
  32899. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  32900. XMEMCPY(local, input2, sLen);
  32901. AssertIntEQ(SHA_Transform(&sha, (const byte*)&local[0]), 1);
  32902. AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha)->digest[0], output2,
  32903. WC_SHA_DIGEST_SIZE), 0);
  32904. AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
  32905. /* SHA1 */
  32906. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  32907. /* Init SHA CTX */
  32908. AssertIntEQ(SHA1_Init(&sha1), 1);
  32909. /* Do Transform*/
  32910. sLen = (word32)XSTRLEN((char*)input1);
  32911. XMEMCPY(local, input1, sLen);
  32912. AssertIntEQ(SHA1_Transform(&sha1, (const byte*)&local[0]), 1);
  32913. AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha1)->digest[0], output1,
  32914. WC_SHA_DIGEST_SIZE), 0);
  32915. AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
  32916. /* Init SHA CTX */
  32917. AssertIntEQ(SHA1_Init(&sha1), 1);
  32918. sLen = (word32)XSTRLEN((char*)input2);
  32919. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  32920. XMEMCPY(local, input2, sLen);
  32921. AssertIntEQ(SHA1_Transform(&sha1, (const byte*)&local[0]), 1);
  32922. AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha1)->digest[0], output2,
  32923. WC_SHA_DIGEST_SIZE), 0);
  32924. AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
  32925. printf(resultFmt, passed);
  32926. #endif
  32927. #endif
  32928. }
  32929. static void test_wolfSSL_SHA256_Transform(void)
  32930. {
  32931. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  32932. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  32933. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  32934. byte input1[] = "";
  32935. byte input2[] = "abc";
  32936. byte local[WC_SHA256_BLOCK_SIZE];
  32937. word32 sLen = 0;
  32938. #ifdef BIG_ENDIAN_ORDER
  32939. unsigned char output1[] =
  32940. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  32941. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  32942. unsigned char output2[] =
  32943. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  32944. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  32945. #else
  32946. unsigned char output1[] =
  32947. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  32948. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  32949. unsigned char output2[] =
  32950. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  32951. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  32952. #endif
  32953. SHA256_CTX sha256;
  32954. printf(testingFmt, "wolfSSL_SHA256_Transform()");
  32955. XMEMSET(&sha256, 0, sizeof(sha256));
  32956. XMEMSET(&local, 0, sizeof(local));
  32957. /* sanity check */
  32958. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  32959. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  32960. AssertIntEQ(SHA256_Transform(&sha256, NULL), 0);
  32961. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  32962. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  32963. AssertIntEQ(wc_Sha256Transform((wc_Sha256*)&sha256, NULL), BAD_FUNC_ARG);
  32964. /* Init SHA256 CTX */
  32965. AssertIntEQ(SHA256_Init(&sha256), 1);
  32966. /* Do Transform*/
  32967. sLen = (word32)XSTRLEN((char*)input1);
  32968. XMEMCPY(local, input1, sLen);
  32969. AssertIntEQ(SHA256_Transform(&sha256, (const byte*)&local[0]), 1);
  32970. AssertIntEQ(XMEMCMP(&((wc_Sha256*)&sha256)->digest[0], output1,
  32971. WC_SHA256_DIGEST_SIZE), 0);
  32972. AssertIntEQ(SHA256_Final(local, &sha256), 1); /* frees resources */
  32973. /* Init SHA256 CTX */
  32974. AssertIntEQ(SHA256_Init(&sha256), 1);
  32975. sLen = (word32)XSTRLEN((char*)input2);
  32976. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  32977. XMEMCPY(local, input2, sLen);
  32978. AssertIntEQ(SHA256_Transform(&sha256, (const byte*)&local[0]), 1);
  32979. AssertIntEQ(XMEMCMP(&((wc_Sha256*)&sha256)->digest[0], output2,
  32980. WC_SHA256_DIGEST_SIZE), 0);
  32981. AssertIntEQ(SHA256_Final(local, &sha256), 1); /* frees resources */
  32982. printf(resultFmt, passed);
  32983. #endif
  32984. #endif
  32985. }
  32986. static void test_wolfSSL_SHA256(void)
  32987. {
  32988. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  32989. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  32990. unsigned char input[] =
  32991. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  32992. unsigned char output[] =
  32993. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  32994. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  32995. "\x06\xC1";
  32996. size_t inLen;
  32997. byte hash[WC_SHA256_DIGEST_SIZE];
  32998. printf(testingFmt, "wolfSSL_SHA256()");
  32999. inLen = XSTRLEN((char*)input);
  33000. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  33001. AssertNotNull(SHA256(input, inLen, hash));
  33002. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  33003. printf(resultFmt, passed);
  33004. #endif
  33005. }
  33006. static void test_wolfSSL_SHA512_Transform(void)
  33007. {
  33008. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  33009. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  33010. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  33011. byte input1[] = "";
  33012. byte input2[] = "abc";
  33013. byte local[WC_SHA512_BLOCK_SIZE];
  33014. word32 sLen = 0;
  33015. #ifdef BIG_ENDIAN_ORDER
  33016. unsigned char output1[] =
  33017. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  33018. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  33019. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  33020. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  33021. unsigned char output2[] =
  33022. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  33023. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  33024. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  33025. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  33026. #else
  33027. unsigned char output1[] =
  33028. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  33029. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  33030. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  33031. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  33032. unsigned char output2[] =
  33033. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  33034. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  33035. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  33036. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  33037. #endif
  33038. SHA512_CTX sha512;
  33039. printf(testingFmt, "wolfSSL_SHA512_Transform()");
  33040. XMEMSET(&sha512, 0, sizeof(sha512));
  33041. XMEMSET(&local, 0, sizeof(local));
  33042. /* sanity check */
  33043. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  33044. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  33045. AssertIntEQ(SHA512_Transform(&sha512, NULL), 0);
  33046. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  33047. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  33048. AssertIntEQ(wc_Sha512Transform((wc_Sha512*)&sha512, NULL), BAD_FUNC_ARG);
  33049. /* Init SHA512 CTX */
  33050. AssertIntEQ(wolfSSL_SHA512_Init(&sha512), 1);
  33051. /* Do Transform*/
  33052. sLen = (word32)XSTRLEN((char*)input1);
  33053. XMEMCPY(local, input1, sLen);
  33054. AssertIntEQ(SHA512_Transform(&sha512, (const byte*)&local[0]), 1);
  33055. AssertIntEQ(XMEMCMP(&((wc_Sha512*)&sha512)->digest[0], output1,
  33056. WC_SHA512_DIGEST_SIZE), 0);
  33057. AssertIntEQ(SHA512_Final(local, &sha512), 1); /* frees resources */
  33058. /* Init SHA512 CTX */
  33059. AssertIntEQ(SHA512_Init(&sha512), 1);
  33060. sLen = (word32)XSTRLEN((char*)input2);
  33061. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  33062. XMEMCPY(local, input2, sLen);
  33063. AssertIntEQ(SHA512_Transform(&sha512, (const byte*)&local[0]), 1);
  33064. AssertIntEQ(XMEMCMP(&((wc_Sha512*)&sha512)->digest[0], output2,
  33065. WC_SHA512_DIGEST_SIZE), 0);
  33066. AssertIntEQ(SHA512_Final(local, &sha512), 1); /* frees resources */
  33067. (void)input1;
  33068. printf(resultFmt, passed);
  33069. #endif
  33070. #endif
  33071. }
  33072. static void test_wolfSSL_X509_get_serialNumber(void)
  33073. {
  33074. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  33075. ASN1_INTEGER* a;
  33076. BIGNUM* bn;
  33077. X509* x509;
  33078. char *serialHex;
  33079. byte serial[3];
  33080. int serialSz;
  33081. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  33082. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33083. SSL_FILETYPE_PEM));
  33084. AssertNotNull(a = X509_get_serialNumber(x509));
  33085. /* check on value of ASN1 Integer */
  33086. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  33087. /* test setting serial number and then retrieving it */
  33088. AssertNotNull(a = ASN1_INTEGER_new());
  33089. ASN1_INTEGER_set(a, 3);
  33090. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  33091. serialSz = sizeof(serial);
  33092. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  33093. WOLFSSL_SUCCESS);
  33094. AssertIntEQ(serialSz, 1);
  33095. AssertIntEQ(serial[0], 3);
  33096. ASN1_INTEGER_free(a);
  33097. /* test setting serial number with 0's in it */
  33098. serial[0] = 0x01;
  33099. serial[1] = 0x00;
  33100. serial[2] = 0x02;
  33101. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  33102. a->data[0] = ASN_INTEGER;
  33103. a->data[1] = sizeof(serial);
  33104. XMEMCPY(&a->data[2], serial, sizeof(serial));
  33105. a->length = sizeof(serial) + 2;
  33106. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  33107. XMEMSET(serial, 0, sizeof(serial));
  33108. serialSz = sizeof(serial);
  33109. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  33110. WOLFSSL_SUCCESS);
  33111. AssertIntEQ(serialSz, 3);
  33112. AssertIntEQ(serial[0], 0x01);
  33113. AssertIntEQ(serial[1], 0x00);
  33114. AssertIntEQ(serial[2], 0x02);
  33115. ASN1_INTEGER_free(a);
  33116. X509_free(x509); /* free's a */
  33117. AssertNotNull(serialHex = BN_bn2hex(bn));
  33118. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  33119. AssertStrEQ(serialHex, "01");
  33120. #else
  33121. AssertStrEQ(serialHex, "1");
  33122. #endif
  33123. OPENSSL_free(serialHex);
  33124. AssertIntEQ(BN_get_word(bn), 1);
  33125. BN_free(bn);
  33126. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  33127. a = ASN1_INTEGER_new();
  33128. if (a) {
  33129. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  33130. DYNAMIC_TYPE_OPENSSL));
  33131. a->isDynamic = 1;
  33132. ASN1_INTEGER_free(a);
  33133. }
  33134. printf(resultFmt, passed);
  33135. #endif
  33136. }
  33137. static void test_wolfSSL_OpenSSL_add_all_algorithms(void){
  33138. #if defined(OPENSSL_EXTRA)
  33139. printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
  33140. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  33141. wolfSSL_Cleanup();
  33142. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  33143. wolfSSL_Cleanup();
  33144. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  33145. wolfSSL_Cleanup();
  33146. printf(resultFmt, passed);
  33147. #endif
  33148. }
  33149. static void test_wolfSSL_OPENSSL_hexstr2buf(void)
  33150. {
  33151. #if defined(OPENSSL_EXTRA)
  33152. #define MAX_HEXSTR_BUFSZ 9
  33153. #define NUM_CASES 5
  33154. struct Output {
  33155. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  33156. long ret;
  33157. };
  33158. int i;
  33159. int j;
  33160. const char* inputs[NUM_CASES] = {
  33161. "aabcd1357e",
  33162. "01:12:23:34:a5:b6:c7:d8:e9",
  33163. ":01:02",
  33164. "012",
  33165. ":ab:ac:d"
  33166. };
  33167. struct Output expectedOutputs[NUM_CASES] = {
  33168. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  33169. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  33170. {{0x01, 0x02}, 2},
  33171. {{0x00}, 0},
  33172. {{0x00}, 0}
  33173. };
  33174. long len = 0;
  33175. unsigned char* returnedBuf = NULL;
  33176. printf(testingFmt, "test_wolfSSL_OPENSSL_hexstr2buf()");
  33177. for (i = 0; i < NUM_CASES; ++i) {
  33178. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  33179. if (returnedBuf == NULL) {
  33180. AssertIntEQ(expectedOutputs[i].ret, 0);
  33181. continue;
  33182. }
  33183. AssertIntEQ(expectedOutputs[i].ret, len);
  33184. for (j = 0; j < len; ++j) {
  33185. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  33186. }
  33187. OPENSSL_free(returnedBuf);
  33188. }
  33189. printf(resultFmt, passed);
  33190. #endif
  33191. }
  33192. static void test_wolfSSL_ASN1_STRING_print_ex(void){
  33193. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  33194. #ifndef NO_BIO
  33195. ASN1_STRING* asn_str;
  33196. const char data[] = "Hello wolfSSL!";
  33197. ASN1_STRING* esc_str;
  33198. const char esc_data[] = "a+;<>";
  33199. BIO *bio;
  33200. unsigned long flags;
  33201. int p_len;
  33202. unsigned char rbuf[255];
  33203. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  33204. /* setup */
  33205. XMEMSET(rbuf, 0, 255);
  33206. bio = BIO_new(BIO_s_mem());
  33207. BIO_set_write_buf_size(bio,255);
  33208. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  33209. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  33210. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  33211. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  33212. /* no flags */
  33213. XMEMSET(rbuf, 0, 255);
  33214. flags = 0;
  33215. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  33216. AssertIntEQ(p_len, 15);
  33217. BIO_read(bio, (void*)rbuf, 15);
  33218. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  33219. /* RFC2253 Escape */
  33220. XMEMSET(rbuf, 0, 255);
  33221. flags = ASN1_STRFLGS_ESC_2253;
  33222. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  33223. AssertIntEQ(p_len, 9);
  33224. BIO_read(bio, (void*)rbuf, 9);
  33225. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  33226. /* Show type */
  33227. XMEMSET(rbuf, 0, 255);
  33228. flags = ASN1_STRFLGS_SHOW_TYPE;
  33229. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  33230. AssertIntEQ(p_len, 28);
  33231. BIO_read(bio, (void*)rbuf, 28);
  33232. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  33233. /* Dump All */
  33234. XMEMSET(rbuf, 0, 255);
  33235. flags = ASN1_STRFLGS_DUMP_ALL;
  33236. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  33237. AssertIntEQ(p_len, 31);
  33238. BIO_read(bio, (void*)rbuf, 31);
  33239. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  33240. /* Dump Der */
  33241. XMEMSET(rbuf, 0, 255);
  33242. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  33243. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  33244. AssertIntEQ(p_len, 35);
  33245. BIO_read(bio, (void*)rbuf, 35);
  33246. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  33247. /* Dump All + Show type */
  33248. XMEMSET(rbuf, 0, 255);
  33249. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  33250. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  33251. AssertIntEQ(p_len, 44);
  33252. BIO_read(bio, (void*)rbuf, 44);
  33253. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  33254. BIO_free(bio);
  33255. ASN1_STRING_free(asn_str);
  33256. ASN1_STRING_free(esc_str);
  33257. printf(resultFmt, passed);
  33258. #endif /* !NO_BIO */
  33259. #endif
  33260. }
  33261. static void test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  33262. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  33263. WOLFSSL_ASN1_TIME *t;
  33264. WOLFSSL_ASN1_TIME *out;
  33265. WOLFSSL_ASN1_TIME *gtime;
  33266. int tlen = 0;
  33267. unsigned char *data;
  33268. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  33269. /* UTC Time test */
  33270. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  33271. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  33272. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  33273. t->type = ASN_UTC_TIME;
  33274. t->length = ASN_UTC_TIME_SIZE;
  33275. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  33276. tlen = wolfSSL_ASN1_TIME_get_length(t);
  33277. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  33278. data = wolfSSL_ASN1_TIME_get_data(t);
  33279. AssertStrEQ((char*)data, "050727123456Z");
  33280. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  33281. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  33282. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  33283. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  33284. /* Generalized Time test */
  33285. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  33286. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  33287. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  33288. t->type = ASN_GENERALIZED_TIME;
  33289. t->length = ASN_GENERALIZED_TIME_SIZE;
  33290. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  33291. tlen = wolfSSL_ASN1_TIME_get_length(t);
  33292. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  33293. data = wolfSSL_ASN1_TIME_get_data(t);
  33294. AssertStrEQ((char*)data, "20050727123456Z");
  33295. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  33296. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  33297. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  33298. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  33299. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33300. /* Null parameter test */
  33301. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  33302. gtime = NULL;
  33303. out = NULL;
  33304. t->type = ASN_UTC_TIME;
  33305. t->length = ASN_UTC_TIME_SIZE;
  33306. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  33307. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  33308. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  33309. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  33310. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  33311. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33312. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33313. printf(resultFmt, passed);
  33314. #endif
  33315. }
  33316. static void test_wolfSSL_X509_CA_num(void){
  33317. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  33318. defined(HAVE_ECC) && !defined(NO_RSA)
  33319. WOLFSSL_X509_STORE *store;
  33320. WOLFSSL_X509 *x509_1, *x509_2;
  33321. int ca_num = 0;
  33322. printf(testingFmt, "wolfSSL_X509_CA_num()");
  33323. store = wolfSSL_X509_STORE_new();
  33324. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  33325. wolfSSL_X509_STORE_add_cert(store, x509_1);
  33326. ca_num = wolfSSL_X509_CA_num(store);
  33327. AssertIntEQ(ca_num, 1);
  33328. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  33329. wolfSSL_X509_STORE_add_cert(store, x509_2);
  33330. ca_num = wolfSSL_X509_CA_num(store);
  33331. AssertIntEQ(ca_num, 2);
  33332. wolfSSL_X509_free(x509_1);
  33333. wolfSSL_X509_free(x509_2);
  33334. wolfSSL_X509_STORE_free(store);
  33335. printf(resultFmt, passed);
  33336. #endif
  33337. }
  33338. static void test_wolfSSL_X509_check_ca(void){
  33339. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  33340. WOLFSSL_X509 *x509;
  33341. printf(testingFmt, "wolfSSL_X509_check_ca()");
  33342. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  33343. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  33344. wolfSSL_X509_free(x509);
  33345. x509 = wolfSSL_X509_load_certificate_file(ntruCertFile, WOLFSSL_FILETYPE_PEM);
  33346. AssertIntEQ(wolfSSL_X509_check_ca(x509), 0);
  33347. wolfSSL_X509_free(x509);
  33348. printf(resultFmt, passed);
  33349. #endif
  33350. }
  33351. static void test_wolfSSL_X509_check_ip_asc(void){
  33352. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  33353. WOLFSSL_X509 *x509;
  33354. printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
  33355. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  33356. #if 0
  33357. /* TODO: add cert gen for testing positive case */
  33358. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  33359. #endif
  33360. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  33361. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  33362. wolfSSL_X509_free(x509);
  33363. printf(resultFmt, passed);
  33364. #endif
  33365. }
  33366. static void test_wolfSSL_DC_cert(void)
  33367. {
  33368. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  33369. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_KEY_GEN) && \
  33370. defined(WOLFSSL_CERT_EXT)
  33371. Cert cert;
  33372. RsaKey key;
  33373. WC_RNG rng;
  33374. byte der[FOURK_BUF];
  33375. int certSz;
  33376. int ret, idx;
  33377. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  33378. const unsigned char* pt;
  33379. X509* x509;
  33380. X509_NAME* x509name;
  33381. X509_NAME_ENTRY* entry;
  33382. ASN1_STRING* entryValue;
  33383. CertName name;
  33384. printf(testingFmt, "wolfSSL Certs with DC");
  33385. XMEMSET(&name, 0, sizeof(CertName));
  33386. /* set up cert name */
  33387. XMEMCPY(name.country, "US", sizeof("US"));
  33388. name.countryEnc = CTC_PRINTABLE;
  33389. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  33390. name.stateEnc = CTC_UTF8;
  33391. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  33392. name.localityEnc = CTC_UTF8;
  33393. XMEMCPY(name.sur, "Test", sizeof("Test"));
  33394. name.surEnc = CTC_UTF8;
  33395. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  33396. name.orgEnc = CTC_UTF8;
  33397. XMEMCPY(name.unit, "Development", sizeof("Development"));
  33398. name.unitEnc = CTC_UTF8;
  33399. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  33400. name.commonNameEnc = CTC_UTF8;
  33401. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  33402. name.serialDevEnc = CTC_PRINTABLE;
  33403. #ifdef WOLFSSL_MULTI_ATTRIB
  33404. #if CTC_MAX_ATTRIB > 2
  33405. {
  33406. NameAttrib* n;
  33407. n = &name.name[0];
  33408. n->id = ASN_DOMAIN_COMPONENT;
  33409. n->type = CTC_UTF8;
  33410. n->sz = sizeof("com");
  33411. XMEMCPY(n->value, "com", sizeof("com"));
  33412. n = &name.name[1];
  33413. n->id = ASN_DOMAIN_COMPONENT;
  33414. n->type = CTC_UTF8;
  33415. n->sz = sizeof("wolfssl");
  33416. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  33417. }
  33418. #endif
  33419. #endif /* WOLFSSL_MULTI_ATTRIB */
  33420. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  33421. #ifndef HAVE_FIPS
  33422. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, devId), 0);
  33423. #else
  33424. AssertIntEQ(wc_InitRng(&rng), 0);
  33425. #endif
  33426. AssertIntEQ(wc_MakeRsaKey(&key, 2048, 3, &rng), 0);
  33427. XMEMSET(&cert, 0 , sizeof(Cert));
  33428. AssertIntEQ(wc_InitCert(&cert), 0);
  33429. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  33430. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  33431. cert.serialSz = (int)sizeof(mySerial);
  33432. cert.isCA = 1;
  33433. #ifndef NO_SHA256
  33434. cert.sigType = CTC_SHA256wRSA;
  33435. #else
  33436. cert.sigType = CTC_SHAwRSA;
  33437. #endif
  33438. /* add SKID from the Public Key */
  33439. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  33440. /* add AKID from the Public Key */
  33441. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  33442. ret = 0;
  33443. do {
  33444. #if defined(WOLFSSL_ASYNC_CRYPT)
  33445. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  33446. #endif
  33447. if (ret >= 0) {
  33448. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  33449. }
  33450. } while (ret == WC_PENDING_E);
  33451. AssertIntGT(ret, 0);
  33452. certSz = ret;
  33453. /* der holds a certificate with DC's now check X509 parsing of it */
  33454. pt = der;
  33455. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  33456. AssertNotNull(x509name = X509_get_subject_name(x509));
  33457. #ifdef WOLFSSL_MULTI_ATTRIB
  33458. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  33459. -1)), 5);
  33460. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  33461. idx)), 6);
  33462. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  33463. idx)), -1);
  33464. #endif /* WOLFSSL_MULTI_ATTRIB */
  33465. /* compare DN at index 0 */
  33466. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  33467. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  33468. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  33469. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  33470. #ifdef WOLFSSL_MULTI_ATTRIB
  33471. /* get first and second DC and compare result */
  33472. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  33473. -1)), 5);
  33474. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  33475. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  33476. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  33477. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  33478. idx)), 6);
  33479. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  33480. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  33481. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  33482. #endif /* WOLFSSL_MULTI_ATTRIB */
  33483. /* try invalid index locations for regression test and sanity check */
  33484. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  33485. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  33486. (void)idx;
  33487. X509_free(x509);
  33488. wc_FreeRsaKey(&key);
  33489. wc_FreeRng(&rng);
  33490. printf(resultFmt, passed);
  33491. #endif
  33492. }
  33493. static void test_wolfSSL_X509_get_version(void){
  33494. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33495. WOLFSSL_X509 *x509;
  33496. printf(testingFmt, "wolfSSL_X509_get_version()");
  33497. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  33498. AssertNotNull(x509);
  33499. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  33500. wolfSSL_X509_free(x509);
  33501. printf(resultFmt, passed);
  33502. #endif
  33503. }
  33504. static void test_wolfSSL_DES_ncbc(void){
  33505. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  33506. const_DES_cblock myDes;
  33507. DES_cblock iv = {1};
  33508. DES_key_schedule key = {0};
  33509. unsigned char msg[] = "hello wolfssl";
  33510. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  33511. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  33512. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  33513. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  33514. printf(testingFmt, "wolfSSL_DES_ncbc()");
  33515. /* partial block test */
  33516. DES_set_key(&key, &myDes);
  33517. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  33518. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  33519. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  33520. DES_set_key(&key, &myDes);
  33521. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  33522. *((byte*)&iv) = 1;
  33523. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  33524. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  33525. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  33526. /* full block test */
  33527. DES_set_key(&key, &myDes);
  33528. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  33529. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  33530. *((byte*)&iv) = 1;
  33531. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  33532. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  33533. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  33534. DES_set_key(&key, &myDes);
  33535. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  33536. *((byte*)&iv) = 1;
  33537. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  33538. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  33539. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  33540. printf(resultFmt, passed);
  33541. #endif
  33542. }
  33543. static void test_wolfSSL_AES_cbc_encrypt(void)
  33544. {
  33545. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  33546. AES_KEY aes;
  33547. AES_KEY* aesN = NULL;
  33548. size_t len = 0;
  33549. size_t lenB = 0;
  33550. int keySz0 = 0;
  33551. int keySzN = -1;
  33552. byte out[AES_BLOCK_SIZE] = {0};
  33553. byte* outN = NULL;
  33554. const int enc1 = AES_ENCRYPT;
  33555. const int enc2 = AES_DECRYPT;
  33556. /* Test vectors retrieved from:
  33557. * <begin URL>
  33558. * https://csrc.nist.gov/
  33559. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  33560. * documents/aes/KAT_AES.zip
  33561. * </end URL>
  33562. */
  33563. const byte* pt128N = NULL;
  33564. byte* key128N = NULL;
  33565. byte* iv128N = NULL;
  33566. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  33567. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  33568. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  33569. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  33570. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  33571. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  33572. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  33573. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  33574. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  33575. len = sizeof(pt128);
  33576. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  33577. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  33578. AssertIntNE(XMEMCMP(b, g, h), i)
  33579. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  33580. printf(testingFmt, "Stressing wolfSSL_AES_cbc_encrypt()");
  33581. STRESS_T(pt128N, out, len, &aes, iv128tmp, enc1, ct128, AES_BLOCK_SIZE, 0);
  33582. STRESS_T(pt128, out, len, &aes, iv128N, enc1, ct128, AES_BLOCK_SIZE, 0);
  33583. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, enc1);
  33584. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  33585. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, enc1);
  33586. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  33587. STRESS_T(pt128, out, lenB, &aes, iv128tmp, enc1, ct128, AES_BLOCK_SIZE, 0);
  33588. printf(resultFmt, "Stress Tests: passed");
  33589. printf(testingFmt, "Stressing wolfSSL_AES_set_encrypt_key");
  33590. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  33591. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  33592. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  33593. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  33594. printf(resultFmt, "Stress Tests: passed");
  33595. printf(testingFmt, "Stressing wolfSSL_AES_set_decrypt_key");
  33596. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  33597. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  33598. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  33599. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  33600. printf(resultFmt, "Stress Tests: passed");
  33601. #ifdef WOLFSSL_AES_128
  33602. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit");
  33603. XMEMSET(out, 0, AES_BLOCK_SIZE);
  33604. RESET_IV(iv128tmp, iv128);
  33605. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  33606. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, enc1);
  33607. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  33608. printf(resultFmt, "passed");
  33609. #ifdef HAVE_AES_DECRYPT
  33610. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode");
  33611. XMEMSET(out, 0, AES_BLOCK_SIZE);
  33612. RESET_IV(iv128tmp, iv128);
  33613. len = sizeof(ct128);
  33614. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  33615. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, enc2);
  33616. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  33617. printf(resultFmt, "passed");
  33618. #endif
  33619. #endif /* WOLFSSL_AES_128 */
  33620. #ifdef WOLFSSL_AES_192
  33621. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  33622. * Appendix F.2.3 */
  33623. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  33624. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  33625. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  33626. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  33627. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  33628. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  33629. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  33630. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  33631. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  33632. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  33633. len = sizeof(pt192);
  33634. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit");
  33635. XMEMSET(out, 0, AES_BLOCK_SIZE);
  33636. RESET_IV(iv192tmp, iv192);
  33637. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  33638. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, enc1);
  33639. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  33640. printf(resultFmt, "passed");
  33641. #ifdef HAVE_AES_DECRYPT
  33642. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode");
  33643. len = sizeof(ct192);
  33644. RESET_IV(iv192tmp, iv192);
  33645. XMEMSET(out, 0, AES_BLOCK_SIZE);
  33646. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  33647. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, enc2);
  33648. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  33649. printf(resultFmt, "passed");
  33650. #endif
  33651. #endif /* WOLFSSL_AES_192 */
  33652. #ifdef WOLFSSL_AES_256
  33653. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  33654. * Appendix F.2.5 */
  33655. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  33656. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  33657. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  33658. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  33659. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  33660. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  33661. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  33662. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  33663. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  33664. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  33665. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  33666. len = sizeof(pt256);
  33667. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit");
  33668. XMEMSET(out, 0, AES_BLOCK_SIZE);
  33669. RESET_IV(iv256tmp, iv256);
  33670. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  33671. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, enc1);
  33672. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  33673. printf(resultFmt, "passed");
  33674. #ifdef HAVE_AES_DECRYPT
  33675. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode");
  33676. len = sizeof(ct256);
  33677. RESET_IV(iv256tmp, iv256);
  33678. XMEMSET(out, 0, AES_BLOCK_SIZE);
  33679. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  33680. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, enc2);
  33681. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  33682. printf(resultFmt, "passed");
  33683. #endif
  33684. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  33685. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  33686. byte wrapPlain[sizeof(key256)] = { 0 };
  33687. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  33688. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit NULL iv");
  33689. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  33690. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  33691. sizeof(key256)), sizeof(wrapCipher));
  33692. printf(resultFmt, "passed");
  33693. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit NULL iv");
  33694. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  33695. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  33696. sizeof(wrapCipher)), sizeof(wrapPlain));
  33697. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  33698. printf(resultFmt, "passed");
  33699. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  33700. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  33701. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit custom iv");
  33702. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  33703. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  33704. sizeof(key256)), sizeof(wrapCipher));
  33705. printf(resultFmt, "passed");
  33706. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit custom iv");
  33707. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  33708. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  33709. sizeof(wrapCipher)), sizeof(wrapPlain));
  33710. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  33711. printf(resultFmt, "passed");
  33712. #endif /* HAVE_AES_KEYWRAP */
  33713. #endif /* WOLFSSL_AES_256 */
  33714. #endif
  33715. }
  33716. #if defined(OPENSSL_ALL)
  33717. #if !defined(NO_ASN)
  33718. static void test_wolfSSL_ASN1_STRING_to_UTF8(void)
  33719. {
  33720. #if !defined(NO_RSA)
  33721. WOLFSSL_X509* x509;
  33722. WOLFSSL_X509_NAME* subject;
  33723. WOLFSSL_X509_NAME_ENTRY* e;
  33724. WOLFSSL_ASN1_STRING* a;
  33725. FILE* file;
  33726. int idx = 0;
  33727. char targetOutput[16] = "www.wolfssl.com";
  33728. unsigned char* actual_output;
  33729. int len = 0;
  33730. int result = 0;
  33731. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  33732. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  33733. fclose(file);
  33734. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName");
  33735. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  33736. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  33737. NID_commonName, -1)), 5);
  33738. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  33739. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  33740. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  33741. result = strncmp((const char*)actual_output, targetOutput, len);
  33742. AssertIntEQ(result, 0);
  33743. printf(resultFmt, result == 0 ? passed : failed);
  33744. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
  33745. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  33746. WOLFSSL_FATAL_ERROR);
  33747. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  33748. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
  33749. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  33750. WOLFSSL_FATAL_ERROR);
  33751. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  33752. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
  33753. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  33754. WOLFSSL_FATAL_ERROR);
  33755. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  33756. wolfSSL_X509_free(x509);
  33757. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33758. #endif
  33759. }
  33760. static void test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  33761. {
  33762. ASN1_STRING* asn1str_test;
  33763. ASN1_STRING* asn1str_answer;
  33764. /* Each character is encoded using 4 bytes */
  33765. char input[] = {
  33766. 0, 0, 0, 'T',
  33767. 0, 0, 0, 'e',
  33768. 0, 0, 0, 's',
  33769. 0, 0, 0, 't',
  33770. };
  33771. char output[] = "Test";
  33772. printf(testingFmt, "test_wolfSSL_ASN1_UNIVERSALSTRING_to_string()");
  33773. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  33774. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  33775. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  33776. AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  33777. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  33778. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  33779. ASN1_STRING_free(asn1str_test);
  33780. ASN1_STRING_free(asn1str_answer);
  33781. printf(resultFmt, "passed");
  33782. }
  33783. #endif /* !defined(NO_ASN) */
  33784. static void test_wolfSSL_sk_CIPHER_description(void)
  33785. {
  33786. #if !defined(NO_RSA)
  33787. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  33788. int i,j,k;
  33789. int numCiphers = 0;
  33790. const SSL_METHOD *method = NULL;
  33791. const SSL_CIPHER *cipher = NULL;
  33792. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  33793. SSL_CTX *ctx = NULL;
  33794. SSL *ssl = NULL;
  33795. char buf[256];
  33796. char test_str[9] = "0000000";
  33797. const char badStr[] = "unknown";
  33798. const char certPath[] = "./certs/client-cert.pem";
  33799. XMEMSET(buf, 0, sizeof(buf));
  33800. printf(testingFmt, "wolfSSL_sk_CIPHER_description");
  33801. AssertNotNull(method = TLSv1_2_client_method());
  33802. AssertNotNull(ctx = SSL_CTX_new(method));
  33803. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  33804. SSL_CTX_set_verify_depth(ctx, 4);
  33805. SSL_CTX_set_options(ctx, flags);
  33806. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  33807. WOLFSSL_SUCCESS);
  33808. AssertNotNull(ssl = SSL_new(ctx));
  33809. /* SSL_get_ciphers returns a stack of all configured ciphers
  33810. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  33811. */
  33812. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  33813. /* loop through the amount of supportedCiphers */
  33814. numCiphers = sk_num(supportedCiphers);
  33815. for (i = 0; i < numCiphers; ++i) {
  33816. /* sk_value increments "sk->data.cipher->cipherOffset".
  33817. * wolfSSL_sk_CIPHER_description sets the description for
  33818. * the cipher based on the provided offset.
  33819. */
  33820. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  33821. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  33822. }
  33823. /* Search cipher description string for "unknown" descriptor */
  33824. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  33825. k = 0;
  33826. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  33827. test_str[k] = badStr[k];
  33828. j++;
  33829. k++;
  33830. }
  33831. }
  33832. /* Fail if test_str == badStr == "unknown" */
  33833. AssertStrNE(test_str,badStr);
  33834. }
  33835. SSL_free(ssl);
  33836. SSL_CTX_free(ctx);
  33837. printf(resultFmt, passed);
  33838. #endif
  33839. }
  33840. static void test_wolfSSL_get_ciphers_compat(void)
  33841. {
  33842. #if !defined(NO_RSA)
  33843. const SSL_METHOD *method = NULL;
  33844. const char certPath[] = "./certs/client-cert.pem";
  33845. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  33846. SSL_CTX *ctx = NULL;
  33847. WOLFSSL *ssl = NULL;
  33848. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  33849. printf(testingFmt, "wolfSSL_get_ciphers_compat");
  33850. method = SSLv23_client_method();
  33851. AssertNotNull(method);
  33852. ctx = SSL_CTX_new(method);
  33853. AssertNotNull(ctx);
  33854. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  33855. SSL_CTX_set_verify_depth(ctx, 4);
  33856. SSL_CTX_set_options(ctx, flags);
  33857. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  33858. WOLFSSL_SUCCESS);
  33859. AssertNotNull(ssl = SSL_new(ctx));
  33860. /* Test Bad NULL input */
  33861. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  33862. /* Test for Good input */
  33863. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  33864. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  33865. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  33866. SSL_free(ssl);
  33867. SSL_CTX_free(ctx);
  33868. printf(resultFmt, passed);
  33869. #endif
  33870. }
  33871. static void test_wolfSSL_X509_PUBKEY_get(void)
  33872. {
  33873. WOLFSSL_X509_PUBKEY pubkey;
  33874. WOLFSSL_X509_PUBKEY* key;
  33875. WOLFSSL_EVP_PKEY evpkey ;
  33876. WOLFSSL_EVP_PKEY* evpPkey;
  33877. WOLFSSL_EVP_PKEY* retEvpPkey;
  33878. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  33879. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  33880. key = &pubkey;
  33881. evpPkey = &evpkey;
  33882. evpPkey->type = WOLFSSL_SUCCESS;
  33883. key->pkey = evpPkey;
  33884. printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
  33885. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  33886. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  33887. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  33888. key->pkey = NULL;
  33889. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  33890. printf(resultFmt,retEvpPkey == NULL ? passed : failed);
  33891. }
  33892. static void test_wolfSSL_d2i_DHparams(void)
  33893. {
  33894. #if !defined(NO_DH)
  33895. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  33896. FILE* f = NULL;
  33897. unsigned char buf[4096];
  33898. const unsigned char* pt = buf;
  33899. const char* params1 = "./certs/dh2048.der";
  33900. const char* params2 = "./certs/dh3072.der";
  33901. long len = 0;
  33902. WOLFSSL_DH* dh = NULL;
  33903. XMEMSET(buf, 0, sizeof(buf));
  33904. /* Test 2048 bit parameters */
  33905. printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
  33906. f = XFOPEN(params1, "rb");
  33907. AssertTrue(f != XBADFILE);
  33908. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  33909. XFCLOSE(f);
  33910. /* Valid case */
  33911. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  33912. AssertNotNull(dh->p);
  33913. AssertNotNull(dh->g);
  33914. AssertTrue(pt != buf);
  33915. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  33916. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), WOLFSSL_SUCCESS);
  33917. #endif
  33918. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  33919. /* Invalid cases */
  33920. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  33921. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  33922. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  33923. DH_free(dh);
  33924. printf(resultFmt, passed);
  33925. *buf = 0;
  33926. pt = buf;
  33927. /* Test 3072 bit parameters */
  33928. printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
  33929. f = XFOPEN(params2, "rb");
  33930. AssertTrue(f != XBADFILE);
  33931. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  33932. XFCLOSE(f);
  33933. /* Valid case */
  33934. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  33935. AssertNotNull(dh->p);
  33936. AssertNotNull(dh->g);
  33937. AssertTrue(pt != buf);
  33938. AssertIntEQ(DH_generate_key(dh), 1);
  33939. /* Invalid cases */
  33940. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  33941. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  33942. DH_free(dh);
  33943. printf(resultFmt, passed);
  33944. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  33945. #endif /* !NO_DH */
  33946. }
  33947. static void test_wolfSSL_i2d_DHparams(void)
  33948. {
  33949. #if !defined(NO_DH)
  33950. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  33951. FILE* f;
  33952. unsigned char buf[4096];
  33953. const unsigned char* pt = buf;
  33954. unsigned char* pt2 = buf;
  33955. const char* params1 = "./certs/dh2048.der";
  33956. const char* params2 = "./certs/dh3072.der";
  33957. long len;
  33958. WOLFSSL_DH* dh;
  33959. /* Test 2048 bit parameters */
  33960. printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
  33961. f = XFOPEN(params1, "rb");
  33962. AssertTrue(f != XBADFILE);
  33963. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  33964. XFCLOSE(f);
  33965. /* Valid case */
  33966. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  33967. AssertTrue(pt != buf);
  33968. AssertIntEQ(DH_generate_key(dh), 1);
  33969. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  33970. /* Invalid case */
  33971. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  33972. /* Return length only */
  33973. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  33974. DH_free(dh);
  33975. printf(resultFmt, passed);
  33976. *buf = 0;
  33977. pt = buf;
  33978. pt2 = buf;
  33979. /* Test 3072 bit parameters */
  33980. printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
  33981. f = XFOPEN(params2, "rb");
  33982. AssertTrue(f != XBADFILE);
  33983. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  33984. XFCLOSE(f);
  33985. /* Valid case */
  33986. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  33987. AssertTrue(pt != buf);
  33988. AssertIntEQ(DH_generate_key(dh), 1);
  33989. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  33990. /* Invalid case */
  33991. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  33992. /* Return length only */
  33993. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  33994. DH_free(dh);
  33995. printf(resultFmt, passed);
  33996. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  33997. #endif
  33998. }
  33999. static void test_wolfSSL_EC_KEY_dup(void)
  34000. {
  34001. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  34002. defined(OPENSSL_EXTRA_X509_SMALL))
  34003. WOLFSSL_EC_KEY* ecKey;
  34004. WOLFSSL_EC_KEY* dupKey;
  34005. ecc_key* srcKey;
  34006. ecc_key* destKey;
  34007. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  34008. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  34009. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  34010. /* Valid cases */
  34011. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  34012. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  34013. /* Compare pubkey */
  34014. srcKey = (ecc_key*)ecKey->internal;
  34015. destKey = (ecc_key*)dupKey->internal;
  34016. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  34017. /* compare EC_GROUP */
  34018. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  34019. /* compare EC_POINT */
  34020. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  34021. dupKey->pub_key, NULL), MP_EQ);
  34022. /* compare BIGNUM */
  34023. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  34024. wolfSSL_EC_KEY_free(dupKey);
  34025. /* Invalid cases */
  34026. /* NULL key */
  34027. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  34028. /* NULL ecc_key */
  34029. wc_ecc_free((ecc_key*)ecKey->internal);
  34030. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  34031. ecKey->internal = NULL; /* Set ecc_key to NULL */
  34032. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  34033. wolfSSL_EC_KEY_free(ecKey);
  34034. wolfSSL_EC_KEY_free(dupKey);
  34035. /* NULL Group */
  34036. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  34037. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  34038. wolfSSL_EC_GROUP_free(ecKey->group);
  34039. ecKey->group = NULL; /* Set group to NULL */
  34040. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  34041. wolfSSL_EC_KEY_free(ecKey);
  34042. wolfSSL_EC_KEY_free(dupKey);
  34043. /* NULL public key */
  34044. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  34045. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  34046. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  34047. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  34048. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  34049. wolfSSL_EC_POINT_free(ecKey->pub_key);
  34050. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  34051. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  34052. wolfSSL_EC_KEY_free(ecKey);
  34053. wolfSSL_EC_KEY_free(dupKey);
  34054. /* NULL private key */
  34055. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  34056. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  34057. wolfSSL_BN_free(ecKey->priv_key);
  34058. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  34059. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  34060. wolfSSL_EC_KEY_free(ecKey);
  34061. wolfSSL_EC_KEY_free(dupKey);
  34062. printf(resultFmt, passed);
  34063. #endif
  34064. }
  34065. static void test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  34066. {
  34067. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  34068. DSA *dsa = NULL;
  34069. DSA *setDsa = NULL;
  34070. EVP_PKEY *pkey = NULL;
  34071. EVP_PKEY *set1Pkey = NULL;
  34072. SHA_CTX sha;
  34073. byte signature[DSA_SIG_SIZE];
  34074. byte hash[WC_SHA_DIGEST_SIZE];
  34075. word32 bytes;
  34076. int answer;
  34077. #ifdef USE_CERT_BUFFERS_1024
  34078. const unsigned char* dsaKeyDer = dsa_key_der1024;
  34079. int dsaKeySz = sizeof_dsa_key_der_1024;
  34080. byte tmp[ONEK_BUF];
  34081. XMEMSET(tmp, 0, sizeof(tmp));
  34082. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  34083. bytes = dsa_key_der_sz;
  34084. #elif defined(USE_CERT_BUFFERS_2048)
  34085. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  34086. int dsaKeySz = sizeof_dsa_key_der_2048;
  34087. byte tmp[TWOK_BUF];
  34088. XMEMSET(tmp, 0, sizeof(tmp));
  34089. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  34090. bytes = dsaKeySz;
  34091. #else
  34092. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  34093. int dsaKeySz = sizeof_dsa_key_der_2048;
  34094. byte tmp[TWOK_BUF];
  34095. XMEMSET(tmp, 0, sizeof(tmp));
  34096. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  34097. XFILE fp = XOPEN("./certs/dsa2048.der", "rb");
  34098. if (fp == XBADFILE) {
  34099. return WOLFSSL_BAD_FILE;
  34100. }
  34101. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  34102. XFCLOSE(fp);
  34103. #endif /* END USE_CERT_BUFFERS_1024 */
  34104. printf(testingFmt,
  34105. "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
  34106. /* Create hash to later Sign and Verify */
  34107. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  34108. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  34109. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  34110. /* Initialize pkey with der format dsa key */
  34111. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  34112. &dsaKeyDer ,(long)dsaKeySz));
  34113. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  34114. /* Should Fail: NULL argument */
  34115. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  34116. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  34117. /* Should Pass: Initialized pkey argument */
  34118. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  34119. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  34120. AssertIntEQ(DSA_bits(dsa), 2048);
  34121. /* Sign */
  34122. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  34123. /* Verify. */
  34124. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  34125. WOLFSSL_SUCCESS);
  34126. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  34127. /* Should Fail: set1Pkey not initialized */
  34128. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  34129. /* Initialize set1Pkey */
  34130. set1Pkey = EVP_PKEY_new();
  34131. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  34132. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  34133. WOLFSSL_SUCCESS);
  34134. /* Should Pass: set dsa into set1Pkey */
  34135. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  34136. printf(resultFmt, passed);
  34137. DSA_free(dsa);
  34138. DSA_free(setDsa);
  34139. EVP_PKEY_free(pkey);
  34140. EVP_PKEY_free(set1Pkey);
  34141. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  34142. } /* END test_EVP_PKEY_set1_get1_DSA */
  34143. static void test_wolfSSL_DSA_SIG(void)
  34144. {
  34145. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  34146. !defined(HAVE_FIPS)
  34147. DSA *dsa = NULL;
  34148. DSA *dsa2 = NULL;
  34149. DSA_SIG *sig = NULL;
  34150. const BIGNUM *p = NULL;
  34151. const BIGNUM *q = NULL;
  34152. const BIGNUM *g = NULL;
  34153. const BIGNUM *pub = NULL;
  34154. const BIGNUM *priv = NULL;
  34155. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  34156. printf(testingFmt, "wolfSSL_DSA_SIG");
  34157. AssertNotNull(dsa = DSA_generate_parameters(2048,
  34158. NULL, 0, NULL, NULL, NULL, NULL));
  34159. DSA_free(dsa);
  34160. AssertNotNull(dsa = DSA_new());
  34161. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  34162. NULL, 0, NULL, NULL, NULL), 1);
  34163. AssertIntEQ(DSA_generate_key(dsa), 1);
  34164. DSA_get0_pqg(dsa, &p, &q, &g);
  34165. DSA_get0_key(dsa, &pub, &priv);
  34166. AssertNotNull(p = BN_dup(p));
  34167. AssertNotNull(q = BN_dup(q));
  34168. AssertNotNull(g = BN_dup(g));
  34169. AssertNotNull(pub = BN_dup(pub));
  34170. AssertNotNull(priv = BN_dup(priv));
  34171. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  34172. AssertNotNull(dsa2 = DSA_new());
  34173. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  34174. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  34175. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  34176. printf(resultFmt, passed);
  34177. DSA_free(dsa);
  34178. DSA_free(dsa2);
  34179. DSA_SIG_free(sig);
  34180. #endif
  34181. }
  34182. static void test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  34183. {
  34184. #ifdef HAVE_ECC
  34185. WOLFSSL_EC_KEY *ecKey = NULL;
  34186. WOLFSSL_EC_KEY *ecGet1 = NULL;
  34187. EVP_PKEY *pkey = NULL;
  34188. printf(testingFmt,
  34189. "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
  34190. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  34191. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34192. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  34193. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  34194. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  34195. /* Should fail since ecKey is empty */
  34196. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  34197. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  34198. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  34199. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  34200. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  34201. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  34202. wolfSSL_EC_KEY_free(ecKey);
  34203. wolfSSL_EC_KEY_free(ecGet1);
  34204. EVP_PKEY_free(pkey);
  34205. /* PASSED */
  34206. printf(resultFmt, passed);
  34207. #endif /* HAVE_ECC */
  34208. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  34209. static void test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  34210. {
  34211. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  34212. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  34213. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  34214. DH *dh = NULL;
  34215. DH *setDh = NULL;
  34216. EVP_PKEY *pkey = NULL;
  34217. FILE* f = NULL;
  34218. unsigned char buf[4096];
  34219. const unsigned char* pt = buf;
  34220. const char* dh2048 = "./certs/dh2048.der";
  34221. long len = 0;
  34222. int code = -1;
  34223. printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
  34224. XMEMSET(buf, 0, sizeof(buf));
  34225. f = XFOPEN(dh2048, "rb");
  34226. AssertTrue(f != XBADFILE);
  34227. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  34228. XFCLOSE(f);
  34229. /* Load dh2048.der into DH with internal format */
  34230. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  34231. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  34232. AssertIntEQ(code, 0);
  34233. code = -1;
  34234. pkey = wolfSSL_EVP_PKEY_new();
  34235. /* Set DH into PKEY */
  34236. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  34237. /* Get DH from PKEY */
  34238. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  34239. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  34240. AssertIntEQ(code, 0);
  34241. EVP_PKEY_free(pkey);
  34242. DH_free(setDh);
  34243. DH_free(dh);
  34244. printf(resultFmt, passed);
  34245. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  34246. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  34247. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  34248. } /* END test_EVP_PKEY_set1_get1_DH */
  34249. static void test_wolfSSL_CTX_ctrl(void)
  34250. {
  34251. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34252. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34253. char caFile[] = "./certs/client-ca.pem";
  34254. char clientFile[] = "./certs/client-cert.pem";
  34255. SSL_CTX* ctx;
  34256. X509* x509 = NULL;
  34257. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  34258. byte buf[6000];
  34259. char file[] = "./certs/dsaparams.pem";
  34260. XFILE f;
  34261. int bytes;
  34262. BIO* bio;
  34263. DSA* dsa;
  34264. DH* dh;
  34265. #endif
  34266. #ifdef HAVE_ECC
  34267. WOLFSSL_EC_KEY* ecKey;
  34268. #endif
  34269. printf(testingFmt, "wolfSSL_CTX_ctrl");
  34270. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34271. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  34272. AssertNotNull(x509);
  34273. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  34274. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  34275. AssertNotNull(x509);
  34276. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  34277. /* Initialize DH */
  34278. f = XFOPEN(file, "rb");
  34279. AssertTrue((f != XBADFILE));
  34280. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  34281. XFCLOSE(f);
  34282. bio = BIO_new_mem_buf((void*)buf, bytes);
  34283. AssertNotNull(bio);
  34284. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  34285. AssertNotNull(dsa);
  34286. dh = wolfSSL_DSA_dup_DH(dsa);
  34287. AssertNotNull(dh);
  34288. #endif
  34289. #ifdef HAVE_ECC
  34290. /* Initialize WOLFSSL_EC_KEY */
  34291. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  34292. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  34293. #endif
  34294. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  34295. /* additional test of getting EVP_PKEY key size from X509
  34296. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  34297. * allowed with user RSA */
  34298. {
  34299. EVP_PKEY* pkey;
  34300. #if defined(HAVE_ECC)
  34301. X509* ecX509;
  34302. #endif /* HAVE_ECC */
  34303. AssertNotNull(pkey = X509_get_pubkey(x509));
  34304. /* current RSA key is 2048 bit (256 bytes) */
  34305. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  34306. EVP_PKEY_free(pkey);
  34307. #if defined(HAVE_ECC)
  34308. #if defined(USE_CERT_BUFFERS_256)
  34309. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  34310. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  34311. SSL_FILETYPE_ASN1));
  34312. #else
  34313. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  34314. cliEccCertFile, SSL_FILETYPE_PEM));
  34315. #endif
  34316. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  34317. /* current ECC key is 256 bit (32 bytes) */
  34318. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  34319. X509_free(ecX509);
  34320. EVP_PKEY_free(pkey);
  34321. #endif /* HAVE_ECC */
  34322. }
  34323. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  34324. /* Tests should fail with passed in NULL pointer */
  34325. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  34326. SSL_FAILURE);
  34327. #if !defined(NO_DH) && !defined(NO_DSA)
  34328. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  34329. SSL_FAILURE);
  34330. #endif
  34331. #ifdef HAVE_ECC
  34332. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  34333. SSL_FAILURE);
  34334. #endif
  34335. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  34336. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  34337. */
  34338. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  34339. SSL_SUCCESS);
  34340. /* Test with SSL_CTRL_OPTIONS
  34341. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  34342. */
  34343. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  34344. == SSL_OP_NO_TLSv1);
  34345. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  34346. /* Test with SSL_CTRL_SET_TMP_DH
  34347. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  34348. */
  34349. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  34350. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  34351. SSL_SUCCESS);
  34352. #endif
  34353. /* Test with SSL_CTRL_SET_TMP_ECDH
  34354. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  34355. */
  34356. #ifdef HAVE_ECC
  34357. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  34358. SSL_SUCCESS);
  34359. #endif
  34360. #ifdef WOLFSSL_ENCRYPTED_KEYS
  34361. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  34362. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  34363. #endif
  34364. /* Test for min/max proto */
  34365. #ifndef WOLFSSL_NO_TLS12
  34366. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  34367. 0, NULL), SSL_SUCCESS);
  34368. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  34369. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  34370. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  34371. #endif
  34372. #ifdef WOLFSSL_TLS13
  34373. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  34374. 0, NULL), SSL_SUCCESS);
  34375. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  34376. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  34377. #endif
  34378. /* Cleanup and Pass */
  34379. #if !defined(NO_DH) && !defined(NO_DSA)
  34380. #ifndef NO_BIO
  34381. BIO_free(bio);
  34382. DSA_free(dsa);
  34383. DH_free(dh);
  34384. #endif
  34385. #endif
  34386. #ifdef HAVE_ECC
  34387. wolfSSL_EC_KEY_free(ecKey);
  34388. #endif
  34389. SSL_CTX_free(ctx);
  34390. printf(resultFmt, passed);
  34391. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34392. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34393. }
  34394. static void test_wolfSSL_DH_check(void)
  34395. {
  34396. #if !defined(NO_DH) && !defined(NO_DSA)
  34397. #ifndef NO_BIO
  34398. byte buf[6000];
  34399. char file[] = "./certs/dsaparams.pem";
  34400. XFILE f;
  34401. int bytes;
  34402. BIO* bio;
  34403. DSA* dsa;
  34404. DH* dh = NULL;
  34405. WOLFSSL_BIGNUM* pTmp = NULL;
  34406. WOLFSSL_BIGNUM* gTmp = NULL;
  34407. int codes = -1;
  34408. printf(testingFmt, "wolfSSL_DH_check");
  34409. /* Initialize DH */
  34410. f = XFOPEN(file, "rb");
  34411. AssertTrue((f != XBADFILE));
  34412. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  34413. XFCLOSE(f);
  34414. bio = BIO_new_mem_buf((void*)buf, bytes);
  34415. AssertNotNull(bio);
  34416. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  34417. AssertNotNull(dsa);
  34418. dh = wolfSSL_DSA_dup_DH(dsa);
  34419. AssertNotNull(dh);
  34420. /* Test assumed to be valid dh.
  34421. * Should return WOLFSSL_SUCCESS
  34422. * codes should be 0
  34423. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  34424. */
  34425. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_SUCCESS);
  34426. AssertIntEQ(codes, 0);
  34427. /* Test NULL dh: expected BAD_FUNC_ARG */
  34428. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), WOLFSSL_FAILURE);
  34429. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  34430. pTmp = dh->p;
  34431. dh->p = NULL;
  34432. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  34433. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  34434. /* set dh->p back to normal so it wont fail on next tests */
  34435. dh->p = pTmp;
  34436. pTmp = NULL;
  34437. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  34438. gTmp = dh->g;
  34439. dh->g = NULL;
  34440. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  34441. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  34442. dh->g = gTmp;
  34443. gTmp = NULL;
  34444. /* Cleanup and Pass Test */
  34445. BIO_free(bio);
  34446. DSA_free(dsa);
  34447. DH_free(dh);
  34448. printf(resultFmt, passed);
  34449. #endif
  34450. #endif /* !NO_DH && !NO_DSA */
  34451. }
  34452. static void test_wolfSSL_EVP_PKEY_assign(void)
  34453. {
  34454. #if defined(OPENSSL_ALL)
  34455. int type;
  34456. WOLFSSL_EVP_PKEY* pkey;
  34457. #ifndef NO_RSA
  34458. WOLFSSL_RSA* rsa;
  34459. #endif
  34460. #ifndef NO_DSA
  34461. WOLFSSL_DSA* dsa;
  34462. #endif
  34463. #ifdef HAVE_ECC
  34464. WOLFSSL_EC_KEY* ecKey;
  34465. #endif
  34466. (void)pkey;
  34467. printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
  34468. #ifndef NO_RSA
  34469. type = EVP_PKEY_RSA;
  34470. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34471. AssertNotNull(rsa = wolfSSL_RSA_new());
  34472. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  34473. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  34474. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  34475. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  34476. wolfSSL_EVP_PKEY_free(pkey);
  34477. #endif /* NO_RSA */
  34478. #ifndef NO_DSA
  34479. type = EVP_PKEY_DSA;
  34480. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34481. AssertNotNull(dsa = wolfSSL_DSA_new());
  34482. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  34483. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  34484. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  34485. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  34486. wolfSSL_EVP_PKEY_free(pkey);
  34487. #endif /* NO_DSA */
  34488. #ifdef HAVE_ECC
  34489. type = EVP_PKEY_EC;
  34490. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34491. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  34492. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  34493. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  34494. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  34495. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  34496. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  34497. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  34498. wolfSSL_EVP_PKEY_free(pkey);
  34499. #endif /* HAVE_ECC */
  34500. (void)type;
  34501. printf(resultFmt, passed);
  34502. #endif /* OPENSSL_ALL */
  34503. }
  34504. static void test_wolfSSL_EVP_PKEY_base_id(void)
  34505. {
  34506. #if defined(OPENSSL_ALL)
  34507. WOLFSSL_EVP_PKEY* pkey;
  34508. printf(testingFmt, "wolfSSL_EVP_PKEY_base_id");
  34509. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34510. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  34511. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  34512. EVP_PKEY_free(pkey);
  34513. printf(resultFmt, passed);
  34514. #endif
  34515. }
  34516. static void test_wolfSSL_EVP_PKEY_id(void)
  34517. {
  34518. #if defined(OPENSSL_ALL)
  34519. WOLFSSL_EVP_PKEY* pkey;
  34520. printf(testingFmt, "wolfSSL_EVP_PKEY_id");
  34521. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34522. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  34523. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  34524. EVP_PKEY_free(pkey);
  34525. printf(resultFmt, passed);
  34526. #endif
  34527. }
  34528. static void test_wolfSSL_EVP_PKEY_keygen(void)
  34529. {
  34530. #if defined(OPENSSL_ALL)
  34531. WOLFSSL_EVP_PKEY* pkey;
  34532. EVP_PKEY_CTX *ctx;
  34533. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen");
  34534. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34535. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  34536. /* Bad cases */
  34537. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  34538. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  34539. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  34540. /* Good case */
  34541. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  34542. EVP_PKEY_CTX_free(ctx);
  34543. EVP_PKEY_free(pkey);
  34544. printf(resultFmt, passed);
  34545. #endif
  34546. }
  34547. static void test_wolfSSL_EVP_PKEY_keygen_init(void)
  34548. {
  34549. #if defined(OPENSSL_ALL)
  34550. WOLFSSL_EVP_PKEY* pkey;
  34551. EVP_PKEY_CTX *ctx;
  34552. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen_init");
  34553. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34554. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  34555. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  34556. EVP_PKEY_CTX_free(ctx);
  34557. EVP_PKEY_free(pkey);
  34558. printf(resultFmt, passed);
  34559. #endif
  34560. }
  34561. static void test_wolfSSL_EVP_PKEY_missing_parameters(void)
  34562. {
  34563. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  34564. WOLFSSL_EVP_PKEY* pkey;
  34565. printf(testingFmt, "wolfSSL_EVP_PKEY_missing_parameters");
  34566. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34567. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  34568. EVP_PKEY_free(pkey);
  34569. printf(resultFmt, passed);
  34570. #endif
  34571. }
  34572. static void test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  34573. {
  34574. #if defined(OPENSSL_ALL)
  34575. WOLFSSL_EVP_PKEY* pkey;
  34576. EVP_PKEY_CTX *ctx;
  34577. int bits = 2048;
  34578. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits");
  34579. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  34580. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  34581. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  34582. WOLFSSL_SUCCESS);
  34583. EVP_PKEY_CTX_free(ctx);
  34584. EVP_PKEY_free(pkey);
  34585. printf(resultFmt, passed);
  34586. #endif
  34587. }
  34588. static void test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  34589. {
  34590. #if defined(OPENSSL_ALL)
  34591. /* This is large enough to be used for all key sizes */
  34592. byte key[AES_256_KEY_SIZE] = {0};
  34593. byte iv[AES_BLOCK_SIZE] = {0};
  34594. int i, enumlen;
  34595. EVP_CIPHER_CTX *ctx;
  34596. const EVP_CIPHER *init;
  34597. int enumArray[] = {
  34598. #ifdef HAVE_AES_CBC
  34599. NID_aes_128_cbc,
  34600. #endif
  34601. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  34602. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  34603. #ifdef HAVE_AESGCM
  34604. NID_aes_128_gcm,
  34605. #endif
  34606. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  34607. #ifdef WOLFSSL_AES_COUNTER
  34608. NID_aes_128_ctr,
  34609. #endif
  34610. #ifndef NO_DES3
  34611. NID_des_cbc,
  34612. NID_des_ede3_cbc,
  34613. #endif
  34614. #ifdef HAVE_IDEA
  34615. NID_idea_cbc,
  34616. #endif
  34617. };
  34618. int iv_lengths[] = {
  34619. #ifdef HAVE_AES_CBC
  34620. AES_BLOCK_SIZE,
  34621. #endif
  34622. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  34623. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  34624. #ifdef HAVE_AESGCM
  34625. GCM_NONCE_MID_SZ,
  34626. #endif
  34627. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  34628. #ifdef WOLFSSL_AES_COUNTER
  34629. AES_BLOCK_SIZE,
  34630. #endif
  34631. #ifndef NO_DES3
  34632. DES_BLOCK_SIZE,
  34633. DES_BLOCK_SIZE,
  34634. #endif
  34635. #ifdef HAVE_IDEA
  34636. IDEA_BLOCK_SIZE,
  34637. #endif
  34638. };
  34639. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_iv_length");
  34640. enumlen = (sizeof(enumArray)/sizeof(int));
  34641. for(i = 0; i < enumlen; i++)
  34642. {
  34643. ctx = EVP_CIPHER_CTX_new();
  34644. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  34645. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  34646. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  34647. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  34648. EVP_CIPHER_CTX_free(ctx);
  34649. }
  34650. printf(resultFmt, passed);
  34651. #endif
  34652. }
  34653. static void test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  34654. {
  34655. #if defined(OPENSSL_ALL) && !defined(NO_DES3)
  34656. byte key[AES_256_KEY_SIZE] = {0};
  34657. byte iv[AES_BLOCK_SIZE] = {0};
  34658. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  34659. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  34660. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_key_length");
  34661. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  34662. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  34663. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  34664. EVP_CIPHER_CTX_free(ctx);
  34665. printf(resultFmt, passed);
  34666. #endif
  34667. }
  34668. static void test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  34669. {
  34670. #if defined(OPENSSL_ALL) && !defined(NO_DES3)
  34671. byte key[AES_256_KEY_SIZE] = {0};
  34672. byte iv[AES_BLOCK_SIZE] = {0};
  34673. int keylen;
  34674. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  34675. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  34676. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_key_length");
  34677. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  34678. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  34679. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  34680. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  34681. WOLFSSL_SUCCESS);
  34682. EVP_CIPHER_CTX_free(ctx);
  34683. printf(resultFmt, passed);
  34684. #endif
  34685. }
  34686. static void test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  34687. {
  34688. #if defined(OPENSSL_ALL) && defined(HAVE_AESGCM) && !defined(NO_DES3)
  34689. byte key[DES3_KEY_SIZE] = {0};
  34690. byte iv[DES_BLOCK_SIZE] = {0};
  34691. int ivLen, keyLen;
  34692. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  34693. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  34694. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_iv");
  34695. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  34696. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  34697. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  34698. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  34699. /* Bad cases */
  34700. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  34701. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  34702. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  34703. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  34704. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  34705. /* Good case */
  34706. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  34707. EVP_CIPHER_CTX_free(ctx);
  34708. printf(resultFmt, passed);
  34709. #endif
  34710. }
  34711. static void test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  34712. {
  34713. #if defined(OPENSSL_ALL)
  34714. WOLFSSL_ENGINE* e = NULL;
  34715. int id = 0;
  34716. EVP_PKEY_CTX *ctx;
  34717. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_new_id");
  34718. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  34719. EVP_PKEY_CTX_free(ctx);
  34720. printf(resultFmt, passed);
  34721. #endif
  34722. }
  34723. static void test_wolfSSL_EVP_rc4(void)
  34724. {
  34725. #if defined(OPENSSL_ALL) && !defined(NO_RC4)
  34726. printf(testingFmt, "wolfSSL_EVP_rc4");
  34727. AssertNotNull(wolfSSL_EVP_rc4());
  34728. printf(resultFmt, passed);
  34729. #endif
  34730. }
  34731. static void test_wolfSSL_EVP_enc_null(void)
  34732. {
  34733. #if defined(OPENSSL_ALL)
  34734. printf(testingFmt, "wolfSSL_EVP_enc_null");
  34735. AssertNotNull(wolfSSL_EVP_enc_null());
  34736. printf(resultFmt, passed);
  34737. #endif
  34738. }
  34739. static void test_wolfSSL_EVP_rc2_cbc(void)
  34740. {
  34741. #if defined(OPENSSL_ALL) && defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  34742. printf(testingFmt, "wolfSSL_EVP_rc2_cbc");
  34743. AssertNull(wolfSSL_EVP_rc2_cbc());
  34744. printf(resultFmt, passed);
  34745. #endif
  34746. }
  34747. static void test_wolfSSL_EVP_mdc2(void)
  34748. {
  34749. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  34750. printf(testingFmt, "wolfSSL_EVP_mdc2");
  34751. AssertNull(wolfSSL_EVP_mdc2());
  34752. printf(resultFmt, passed);
  34753. #endif
  34754. }
  34755. static void test_wolfSSL_EVP_md4(void)
  34756. {
  34757. #if defined(OPENSSL_ALL) && !defined(NO_MD4)
  34758. printf(testingFmt, "wolfSSL_EVP_md4");
  34759. AssertNotNull(wolfSSL_EVP_md4());
  34760. printf(resultFmt, passed);
  34761. #endif
  34762. }
  34763. static void test_wolfSSL_EVP_aes_256_gcm(void)
  34764. {
  34765. #if defined(OPENSSL_ALL)
  34766. printf(testingFmt, "wolfSSL_EVP_aes_256_gcm");
  34767. AssertNotNull(wolfSSL_EVP_aes_256_gcm());
  34768. printf(resultFmt, passed);
  34769. #endif
  34770. }
  34771. static void test_wolfSSL_EVP_aes_192_gcm(void)
  34772. {
  34773. #if defined(OPENSSL_ALL)
  34774. printf(testingFmt, "wolfSSL_EVP_aes_192_gcm");
  34775. AssertNotNull(wolfSSL_EVP_aes_192_gcm());
  34776. printf(resultFmt, passed);
  34777. #endif
  34778. }
  34779. static void test_wolfSSL_EVP_ripemd160(void)
  34780. {
  34781. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  34782. printf(testingFmt, "wolfSSL_EVP_ripemd160");
  34783. AssertNull(wolfSSL_EVP_ripemd160());
  34784. printf(resultFmt, passed);
  34785. #endif
  34786. }
  34787. static void test_wolfSSL_EVP_get_digestbynid(void)
  34788. {
  34789. #if defined(OPENSSL_ALL)
  34790. printf(testingFmt, "wolfSSL_EVP_get_digestbynid");
  34791. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  34792. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  34793. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  34794. printf(resultFmt, passed);
  34795. #endif
  34796. }
  34797. static void test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  34798. {
  34799. #if defined(HAVE_ECC) && defined(OPENSSL_ALL)
  34800. WOLFSSL_EVP_PKEY* pkey;
  34801. printf(testingFmt, "wolfSSL_EVP_PKEY_get0_EC_KEY");
  34802. AssertNotNull(pkey = EVP_PKEY_new());
  34803. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  34804. EVP_PKEY_free(pkey);
  34805. printf(resultFmt, passed);
  34806. #endif
  34807. }
  34808. static void test_wolfSSL_EVP_X_STATE(void)
  34809. {
  34810. #if defined(OPENSSL_ALL) && !defined(NO_DES3) && !defined(NO_RC4)
  34811. byte key[DES3_KEY_SIZE] = {0};
  34812. byte iv[DES_IV_SIZE] = {0};
  34813. EVP_CIPHER_CTX *ctx;
  34814. const EVP_CIPHER *init;
  34815. printf(testingFmt, "wolfSSL_EVP_X_STATE");
  34816. /* Bad test cases */
  34817. ctx = EVP_CIPHER_CTX_new();
  34818. init = EVP_des_ede3_cbc();
  34819. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  34820. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  34821. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  34822. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  34823. EVP_CIPHER_CTX_free(ctx);
  34824. /* Good test case */
  34825. ctx = EVP_CIPHER_CTX_new();
  34826. init = wolfSSL_EVP_rc4();
  34827. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  34828. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  34829. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  34830. EVP_CIPHER_CTX_free(ctx);
  34831. printf(resultFmt, passed);
  34832. #endif
  34833. }
  34834. static void test_wolfSSL_EVP_X_STATE_LEN(void)
  34835. {
  34836. #if defined(OPENSSL_ALL) && !defined(NO_DES3) && !defined(NO_RC4)
  34837. byte key[DES3_KEY_SIZE] = {0};
  34838. byte iv[DES_IV_SIZE] = {0};
  34839. EVP_CIPHER_CTX *ctx;
  34840. const EVP_CIPHER *init;
  34841. printf(testingFmt, "wolfSSL_EVP_X_STATE_LEN");
  34842. /* Bad test cases */
  34843. ctx = EVP_CIPHER_CTX_new();
  34844. init = EVP_des_ede3_cbc();
  34845. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  34846. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  34847. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  34848. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  34849. EVP_CIPHER_CTX_free(ctx);
  34850. /* Good test case */
  34851. ctx = EVP_CIPHER_CTX_new();
  34852. init = wolfSSL_EVP_rc4();
  34853. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  34854. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  34855. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  34856. EVP_CIPHER_CTX_free(ctx);
  34857. printf(resultFmt, passed);
  34858. #endif
  34859. }
  34860. static void test_wolfSSL_EVP_CIPHER_block_size(void)
  34861. {
  34862. #if defined(OPENSSL_ALL)
  34863. #ifdef HAVE_AES_CBC
  34864. #ifdef WOLFSSL_AES_128
  34865. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  34866. #endif
  34867. #ifdef WOLFSSL_AES_192
  34868. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  34869. #endif
  34870. #ifdef WOLFSSL_AES_256
  34871. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  34872. #endif
  34873. #endif
  34874. #ifdef HAVE_AES_GCM
  34875. #ifdef WOLFSSL_AES_128
  34876. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  34877. #endif
  34878. #ifdef WOLFSSL_AES_192
  34879. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  34880. #endif
  34881. #ifdef WOLFSSL_AES_256
  34882. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  34883. #endif
  34884. #endif
  34885. #ifdef WOLFSSL_AES_COUNTER
  34886. #ifdef WOLFSSL_AES_128
  34887. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  34888. #endif
  34889. #ifdef WOLFSSL_AES_192
  34890. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  34891. #endif
  34892. #ifdef WOLFSSL_AES_256
  34893. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  34894. #endif
  34895. #endif
  34896. #ifdef HAVE_AES_ECB
  34897. #ifdef WOLFSSL_AES_128
  34898. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  34899. #endif
  34900. #ifdef WOLFSSL_AES_192
  34901. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  34902. #endif
  34903. #ifdef WOLFSSL_AES_256
  34904. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  34905. #endif
  34906. #endif
  34907. #ifdef WOLFSSL_AES_OFB
  34908. #ifdef WOLFSSL_AES_128
  34909. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  34910. #endif
  34911. #ifdef WOLFSSL_AES_192
  34912. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  34913. #endif
  34914. #ifdef WOLFSSL_AES_256
  34915. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  34916. #endif
  34917. #endif
  34918. #ifndef NO_RC4
  34919. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  34920. #endif
  34921. #endif /* OPENSSL_ALL */
  34922. }
  34923. static void test_wolfSSL_EVP_CIPHER_iv_length(void)
  34924. {
  34925. #if defined(OPENSSL_ALL)
  34926. int i, enumlen;
  34927. int enumArray[] = {
  34928. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  34929. #ifdef WOLFSSL_AES_128
  34930. NID_aes_128_cbc,
  34931. #endif
  34932. #ifdef WOLFSSL_AES_192
  34933. NID_aes_192_cbc,
  34934. #endif
  34935. #ifdef WOLFSSL_AES_256
  34936. NID_aes_256_cbc,
  34937. #endif
  34938. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  34939. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  34940. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  34941. #ifdef HAVE_AESGCM
  34942. #ifdef WOLFSSL_AES_128
  34943. NID_aes_128_gcm,
  34944. #endif
  34945. #ifdef WOLFSSL_AES_192
  34946. NID_aes_192_gcm,
  34947. #endif
  34948. #ifdef WOLFSSL_AES_256
  34949. NID_aes_256_gcm,
  34950. #endif
  34951. #endif /* HAVE_AESGCM */
  34952. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  34953. #ifdef WOLFSSL_AES_COUNTER
  34954. #ifdef WOLFSSL_AES_128
  34955. NID_aes_128_ctr,
  34956. #endif
  34957. #ifdef WOLFSSL_AES_192
  34958. NID_aes_192_ctr,
  34959. #endif
  34960. #ifdef WOLFSSL_AES_256
  34961. NID_aes_256_ctr,
  34962. #endif
  34963. #endif
  34964. #ifndef NO_DES3
  34965. NID_des_cbc,
  34966. NID_des_ede3_cbc,
  34967. #endif
  34968. #ifdef HAVE_IDEA
  34969. NID_idea_cbc,
  34970. #endif
  34971. };
  34972. int iv_lengths[] = {
  34973. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  34974. #ifdef WOLFSSL_AES_128
  34975. AES_BLOCK_SIZE,
  34976. #endif
  34977. #ifdef WOLFSSL_AES_192
  34978. AES_BLOCK_SIZE,
  34979. #endif
  34980. #ifdef WOLFSSL_AES_256
  34981. AES_BLOCK_SIZE,
  34982. #endif
  34983. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  34984. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  34985. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  34986. #ifdef HAVE_AESGCM
  34987. #ifdef WOLFSSL_AES_128
  34988. GCM_NONCE_MID_SZ,
  34989. #endif
  34990. #ifdef WOLFSSL_AES_192
  34991. GCM_NONCE_MID_SZ,
  34992. #endif
  34993. #ifdef WOLFSSL_AES_256
  34994. GCM_NONCE_MID_SZ,
  34995. #endif
  34996. #endif /* HAVE_AESGCM */
  34997. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  34998. #ifdef WOLFSSL_AES_COUNTER
  34999. #ifdef WOLFSSL_AES_128
  35000. AES_BLOCK_SIZE,
  35001. #endif
  35002. #ifdef WOLFSSL_AES_192
  35003. AES_BLOCK_SIZE,
  35004. #endif
  35005. #ifdef WOLFSSL_AES_256
  35006. AES_BLOCK_SIZE,
  35007. #endif
  35008. #endif
  35009. #ifndef NO_DES3
  35010. DES_BLOCK_SIZE,
  35011. DES_BLOCK_SIZE,
  35012. #endif
  35013. #ifdef HAVE_IDEA
  35014. IDEA_BLOCK_SIZE,
  35015. #endif
  35016. };
  35017. printf(testingFmt, "wolfSSL_EVP_CIPHER_iv_length");
  35018. enumlen = (sizeof(enumArray)/sizeof(int));
  35019. for(i = 0; i < enumlen; i++)
  35020. {
  35021. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  35022. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  35023. }
  35024. printf(resultFmt, passed);
  35025. #endif
  35026. }
  35027. static void test_wolfSSL_EVP_SignInit_ex(void)
  35028. {
  35029. #if defined(OPENSSL_ALL)
  35030. WOLFSSL_EVP_MD_CTX mdCtx;
  35031. WOLFSSL_ENGINE* e = 0;
  35032. const EVP_MD* md;
  35033. md = "SHA256";
  35034. printf(testingFmt, "wolfSSL_EVP_SignInit_ex");
  35035. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  35036. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  35037. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  35038. printf(resultFmt, passed);
  35039. #endif
  35040. }
  35041. static void test_wolfSSL_EVP_DigestFinal_ex(void)
  35042. {
  35043. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  35044. WOLFSSL_EVP_MD_CTX mdCtx;
  35045. unsigned int s = 0;
  35046. unsigned char md[WC_SHA256_DIGEST_SIZE];
  35047. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  35048. printf(testingFmt, "wolfSSL_EVP_DigestFinal_ex");
  35049. /* Bad Case */
  35050. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  35051. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  35052. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  35053. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  35054. #else
  35055. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  35056. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  35057. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  35058. #endif
  35059. /* Good Case */
  35060. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  35061. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  35062. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  35063. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  35064. printf(resultFmt, passed);
  35065. #endif
  35066. }
  35067. static void test_wolfSSL_EVP_PKEY_assign_DH(void)
  35068. {
  35069. #if defined(OPENSSL_ALL) && !defined(NO_DH) && \
  35070. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  35071. FILE* f = NULL;
  35072. unsigned char buf[4096];
  35073. const unsigned char* pt = buf;
  35074. const char* params1 = "./certs/dh2048.der";
  35075. long len = 0;
  35076. WOLFSSL_DH* dh = NULL;
  35077. WOLFSSL_EVP_PKEY* pkey;
  35078. XMEMSET(buf, 0, sizeof(buf));
  35079. f = XFOPEN(params1, "rb");
  35080. AssertTrue(f != XBADFILE);
  35081. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  35082. XFCLOSE(f);
  35083. printf(testingFmt, "wolfSSL_EVP_PKEY_assign_DH");
  35084. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  35085. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  35086. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  35087. /* Bad cases */
  35088. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  35089. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  35090. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  35091. /* Good case */
  35092. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  35093. EVP_PKEY_free(pkey);
  35094. printf(resultFmt, passed);
  35095. #endif
  35096. }
  35097. static void test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  35098. {
  35099. #if defined(OPENSSL_EXTRA)
  35100. EVP_PKEY* pkey;
  35101. EVP_PKEY_CTX* ctx;
  35102. printf(testingFmt, "test_wolfSSL_QT_EVP_PKEY_CTX_free");
  35103. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  35104. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  35105. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  35106. /* void */
  35107. EVP_PKEY_CTX_free(ctx);
  35108. AssertTrue(1);
  35109. #else
  35110. /* int */
  35111. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  35112. #endif
  35113. EVP_PKEY_free(pkey);
  35114. printf(resultFmt, passed);
  35115. #endif
  35116. }
  35117. static void test_wolfSSL_EVP_PKEY_param_check(void)
  35118. {
  35119. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  35120. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  35121. DH *dh = NULL;
  35122. DH *setDh = NULL;
  35123. EVP_PKEY *pkey = NULL;
  35124. EVP_PKEY_CTX* ctx = NULL;
  35125. FILE* f = NULL;
  35126. unsigned char buf[512];
  35127. const unsigned char* pt = buf;
  35128. const char* dh2048 = "./certs/dh2048.der";
  35129. long len = 0;
  35130. int code = -1;
  35131. printf(testingFmt, "test_wolfSSL_EVP_PKEY_param_check");
  35132. XMEMSET(buf, 0, sizeof(buf));
  35133. f = XFOPEN(dh2048, "rb");
  35134. AssertTrue(f != XBADFILE);
  35135. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  35136. XFCLOSE(f);
  35137. /* Load dh2048.der into DH with internal format */
  35138. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  35139. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  35140. AssertIntEQ(code, 0);
  35141. code = -1;
  35142. pkey = wolfSSL_EVP_PKEY_new();
  35143. /* Set DH into PKEY */
  35144. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  35145. /* create ctx from pkey */
  35146. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  35147. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  35148. /* */
  35149. /* TO DO invlaid case */
  35150. /* */
  35151. EVP_PKEY_CTX_free(ctx);
  35152. EVP_PKEY_free(pkey);
  35153. DH_free(setDh);
  35154. DH_free(dh);
  35155. printf(resultFmt, passed);
  35156. #endif
  35157. #endif
  35158. }
  35159. static void test_wolfSSL_EVP_BytesToKey(void)
  35160. {
  35161. #if defined(OPENSSL_ALL) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  35162. byte key[AES_BLOCK_SIZE] = {0};
  35163. byte iv[AES_BLOCK_SIZE] = {0};
  35164. int sz = 5;
  35165. int count = 0;
  35166. const EVP_MD* md;
  35167. md = "SHA256";
  35168. const EVP_CIPHER *type;
  35169. const unsigned char *salt = (unsigned char *)"salt1234";
  35170. const byte data[] = {
  35171. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35172. 0x72,0x6c,0x64
  35173. };
  35174. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  35175. printf(testingFmt, "EVP_BytesToKey");
  35176. /* Bad cases */
  35177. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  35178. 0);
  35179. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  35180. 16);
  35181. md = "2";
  35182. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  35183. WOLFSSL_FAILURE);
  35184. /* Good case */
  35185. md = "SHA256";
  35186. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  35187. 16);
  35188. printf(resultFmt, passed);
  35189. #endif
  35190. }
  35191. static void test_IncCtr(void)
  35192. {
  35193. #if defined(OPENSSL_ALL) && defined(HAVE_AESGCM) && !defined(HAVE_FIPS)
  35194. byte key[AES_128_KEY_SIZE] = {0};
  35195. byte iv[GCM_NONCE_MID_SZ] = {0};
  35196. int type = EVP_CTRL_GCM_IV_GEN;
  35197. int arg = 0;
  35198. void *ptr = NULL;
  35199. printf(testingFmt, "IncCtr");
  35200. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  35201. const EVP_CIPHER *init = EVP_aes_128_gcm();
  35202. AssertNotNull(ctx);
  35203. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  35204. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  35205. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_ctrl(ctx, type, arg, ptr), WOLFSSL_SUCCESS);
  35206. EVP_CIPHER_CTX_free(ctx);
  35207. printf(resultFmt, passed);
  35208. #endif
  35209. }
  35210. static void test_wolfSSL_OBJ_ln(void)
  35211. {
  35212. const int nid_set[] = {
  35213. NID_commonName,
  35214. NID_serialNumber,
  35215. NID_countryName,
  35216. NID_localityName,
  35217. NID_stateOrProvinceName,
  35218. NID_organizationName,
  35219. NID_organizationalUnitName,
  35220. NID_domainComponent,
  35221. NID_businessCategory,
  35222. NID_jurisdictionCountryName,
  35223. NID_jurisdictionStateOrProvinceName,
  35224. NID_emailAddress
  35225. };
  35226. const char* ln_set[] = {
  35227. "commonName",
  35228. "serialNumber",
  35229. "countryName",
  35230. "localityName",
  35231. "stateOrProvinceName",
  35232. "organizationName",
  35233. "organizationalUnitName",
  35234. "domainComponent",
  35235. "businessCategory",
  35236. "jurisdictionCountryName",
  35237. "jurisdictionStateOrProvinceName",
  35238. "emailAddress",
  35239. };
  35240. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  35241. printf(testingFmt, "wolfSSL_OBJ_ln");
  35242. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  35243. #ifdef HAVE_ECC
  35244. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  35245. {
  35246. size_t nCurves = 27;
  35247. EC_builtin_curve r[nCurves];
  35248. nCurves = EC_get_builtin_curves(r,nCurves);
  35249. for (i = 0; i < nCurves; i++) {
  35250. /* skip ECC_CURVE_INVALID */
  35251. if (r[i].nid != ECC_CURVE_INVALID) {
  35252. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  35253. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  35254. }
  35255. }
  35256. }
  35257. #endif
  35258. #endif
  35259. for (i = 0; i < maxIdx; i++) {
  35260. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  35261. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  35262. }
  35263. printf(resultFmt, passed);
  35264. }
  35265. static void test_wolfSSL_OBJ_sn(void)
  35266. {
  35267. int i = 0, maxIdx = 7;
  35268. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  35269. NID_stateOrProvinceName,NID_organizationName,
  35270. NID_organizationalUnitName,NID_emailAddress};
  35271. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  35272. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  35273. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  35274. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  35275. WOLFSSL_EMAIL_ADDR};
  35276. printf(testingFmt, "wolfSSL_OBJ_sn");
  35277. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  35278. for (i = 0; i < maxIdx; i++) {
  35279. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  35280. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  35281. }
  35282. printf(resultFmt, passed);
  35283. }
  35284. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  35285. {
  35286. return lh_strhash(s[3]);
  35287. }
  35288. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  35289. {
  35290. return XSTRCMP(a[3], b[3]);
  35291. }
  35292. static void test_wolfSSL_TXT_DB(void)
  35293. {
  35294. #if !defined(NO_FILESYSTEM)
  35295. BIO *bio;
  35296. TXT_DB *db = NULL;
  35297. const int columns = 6;
  35298. const char *fields[6] = {
  35299. "V",
  35300. "320926161116Z",
  35301. "",
  35302. "12BD",
  35303. "unknown",
  35304. "/CN=rsa doe",
  35305. };
  35306. char** fields_copy;
  35307. printf(testingFmt, "wolfSSL_TXT_DB");
  35308. /* Test read */
  35309. AssertNotNull(bio = BIO_new(BIO_s_file()));
  35310. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  35311. AssertNotNull(db = TXT_DB_read(bio, columns));
  35312. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  35313. DYNAMIC_TYPE_OPENSSL));
  35314. XMEMCPY(fields_copy, fields, sizeof(fields));
  35315. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  35316. BIO_free(bio);
  35317. /* Test write */
  35318. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35319. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  35320. BIO_free(bio);
  35321. /* Test index */
  35322. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  35323. (wolf_sk_compare_cb)TXT_DB_cmp), 1);
  35324. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  35325. fields[3] = "12DA";
  35326. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  35327. fields[3] = "FFFF";
  35328. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  35329. fields[3] = "";
  35330. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  35331. TXT_DB_free(db);
  35332. printf(resultFmt, passed);
  35333. #endif
  35334. }
  35335. static void test_wolfSSL_NCONF(void)
  35336. {
  35337. #if !defined(NO_FILESYSTEM)
  35338. const char* confFile = "./tests/NCONF_test.cnf";
  35339. CONF* conf = NULL;
  35340. long eline = 0;
  35341. long num = 0;
  35342. printf(testingFmt, "wolfSSL_NCONF");
  35343. AssertNotNull(conf = NCONF_new(NULL));
  35344. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  35345. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  35346. AssertIntEQ(num, 1234);
  35347. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  35348. AssertIntEQ(num, 4321);
  35349. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  35350. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  35351. "./test-dir/file1");
  35352. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  35353. "./test-dir/file1:./test-dir/file2:./section1:file2");
  35354. NCONF_free(conf);
  35355. printf(resultFmt, passed);
  35356. #endif
  35357. }
  35358. #endif /* OPENSSL_ALL */
  35359. static void test_wolfSSL_EC_KEY_set_group(void)
  35360. {
  35361. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  35362. defined(OPENSSL_EXTRA)
  35363. EC_KEY *key = NULL;
  35364. EC_GROUP *group = NULL;
  35365. const EC_GROUP *group2 = NULL;
  35366. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  35367. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  35368. AssertNotNull(key = EC_KEY_new());
  35369. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  35370. AssertNotNull(group2 = EC_KEY_get0_group(key));
  35371. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  35372. EC_GROUP_free(group);
  35373. EC_KEY_free(key);
  35374. printf(resultFmt, passed);
  35375. #endif
  35376. }
  35377. static void test_wolfSSL_X509V3_EXT_get(void) {
  35378. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  35379. FILE* f;
  35380. int numOfExt =0;
  35381. int extNid = 0;
  35382. int i = 0;
  35383. WOLFSSL_X509* x509;
  35384. WOLFSSL_X509_EXTENSION* ext;
  35385. const WOLFSSL_v3_ext_method* method;
  35386. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  35387. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  35388. fclose(f);
  35389. printf(testingFmt, "wolfSSL_X509V3_EXT_get() return struct and nid test");
  35390. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  35391. for (i = 0; i < numOfExt; i++) {
  35392. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  35393. AssertNotNull(extNid = ext->obj->nid);
  35394. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  35395. AssertIntEQ(method->ext_nid, extNid);
  35396. }
  35397. printf(resultFmt, "passed");
  35398. printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
  35399. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  35400. printf(resultFmt, "passed");
  35401. wolfSSL_X509_free(x509);
  35402. #endif
  35403. }
  35404. static void test_wolfSSL_X509V3_EXT_nconf(void)
  35405. {
  35406. #if defined (OPENSSL_ALL)
  35407. const char *ext_names[] = {
  35408. "subjectKeyIdentifier",
  35409. "authorityKeyIdentifier",
  35410. "subjectAltName",
  35411. "keyUsage",
  35412. };
  35413. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  35414. int ext_nids[] = {
  35415. NID_subject_key_identifier,
  35416. NID_authority_key_identifier,
  35417. NID_subject_alt_name,
  35418. NID_key_usage,
  35419. };
  35420. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  35421. const char *ext_values[] = {
  35422. "hash",
  35423. "hash",
  35424. "DNS:example.com, IP:127.0.0.1",
  35425. "digitalSignature,keyEncipherment,dataEncipherment",
  35426. };
  35427. size_t i;
  35428. printf(testingFmt, "wolfSSL_X509V3_EXT_nconf()");
  35429. for (i = 0; i < ext_names_count; i++) {
  35430. X509_EXTENSION* ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  35431. ext_values[i]);
  35432. AssertNotNull(ext);
  35433. X509_EXTENSION_free(ext);
  35434. }
  35435. for (i = 0; i < ext_nids_count; i++) {
  35436. X509_EXTENSION* ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  35437. ext_values[i]);
  35438. AssertNotNull(ext);
  35439. X509_EXTENSION_free(ext);
  35440. }
  35441. printf(resultFmt, "passed");
  35442. #endif
  35443. }
  35444. static void test_wolfSSL_X509V3_EXT(void) {
  35445. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  35446. FILE* f;
  35447. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  35448. char* str;
  35449. unsigned char* data;
  35450. const WOLFSSL_v3_ext_method* method;
  35451. WOLFSSL_X509* x509;
  35452. WOLFSSL_X509_EXTENSION* ext;
  35453. WOLFSSL_X509_EXTENSION* ext2;
  35454. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  35455. WOLFSSL_ASN1_STRING* asn1str;
  35456. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  35457. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  35458. WOLFSSL_BASIC_CONSTRAINTS* bc;
  35459. WOLFSSL_ACCESS_DESCRIPTION* ad;
  35460. WOLFSSL_GENERAL_NAME* gn;
  35461. printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
  35462. /* Check NULL argument */
  35463. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  35464. /* Using OCSP cert with X509V3 extensions */
  35465. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  35466. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  35467. fclose(f);
  35468. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  35469. /* Basic Constraints */
  35470. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  35471. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  35472. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  35473. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  35474. AssertIntEQ(bc->ca, 1);
  35475. AssertNull(bc->pathlen);
  35476. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  35477. i++;
  35478. /* Subject Key Identifier */
  35479. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  35480. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  35481. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  35482. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  35483. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  35484. asn1str));
  35485. X509_EXTENSION_free(ext2);
  35486. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  35487. AssertNotNull(method->i2s);
  35488. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  35489. wolfSSL_ASN1_STRING_free(asn1str);
  35490. actual = strcmp(str,
  35491. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  35492. AssertIntEQ(actual, 0);
  35493. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35494. i++;
  35495. /* Authority Key Identifier */
  35496. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  35497. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  35498. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  35499. AssertNotNull(aKeyId =
  35500. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  35501. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  35502. AssertNotNull(asn1str = aKeyId->keyid);
  35503. AssertNotNull(str =
  35504. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  35505. actual = strcmp(str,
  35506. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  35507. AssertIntEQ(actual, 0);
  35508. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35509. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  35510. i++;
  35511. /* Key Usage */
  35512. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  35513. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  35514. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  35515. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  35516. #if defined(WOLFSSL_QT)
  35517. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  35518. #else
  35519. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  35520. #endif
  35521. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  35522. #ifdef BIG_ENDIAN_ORDER
  35523. actual = data[1];
  35524. #else
  35525. actual = data[0];
  35526. #endif
  35527. AssertIntEQ(actual, expected);
  35528. wolfSSL_ASN1_STRING_free(asn1str);
  35529. #if 1
  35530. i++;
  35531. /* Authority Info Access */
  35532. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  35533. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  35534. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  35535. AssertNotNull(aia =
  35536. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  35537. #if defined(WOLFSSL_QT)
  35538. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  35539. #else
  35540. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  35541. #endif
  35542. /* URI entry is an ACCESS_DESCRIPTION type */
  35543. #if defined(WOLFSSL_QT)
  35544. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  35545. #else
  35546. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  35547. #endif
  35548. AssertNotNull(adObj = ad->method);
  35549. /* Make sure nid is OCSP */
  35550. #ifdef HAVE_OCSP
  35551. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), AIA_OCSP_OID);
  35552. #else
  35553. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), -1);
  35554. #endif
  35555. /* GENERAL_NAME stores URI as an ASN1_STRING */
  35556. AssertNotNull(gn = ad->location);
  35557. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  35558. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  35559. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  35560. #if defined(WOLFSSL_QT)
  35561. str = (char*)ASN1_STRING_get0_data(asn1str);
  35562. #else
  35563. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  35564. #endif
  35565. actual = strcmp(str, "http://127.0.0.1:22220");
  35566. AssertIntEQ(actual, 0);
  35567. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  35568. #else
  35569. (void) aia; (void) ad; (void) adObj; (void) gn;
  35570. #endif
  35571. wolfSSL_X509_free(x509);
  35572. printf(resultFmt, "passed");
  35573. #endif
  35574. }
  35575. static void test_wolfSSL_X509_get_ext(void){
  35576. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  35577. int ret = 0;
  35578. FILE* f;
  35579. WOLFSSL_X509* x509;
  35580. WOLFSSL_X509_EXTENSION* foundExtension;
  35581. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  35582. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  35583. fclose(f);
  35584. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  35585. printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
  35586. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  35587. printf(resultFmt, "passed");
  35588. printf(testingFmt, "wolfSSL_X509_get_ext() valid x509, idx out of bounds");
  35589. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  35590. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  35591. printf(resultFmt, "passed");
  35592. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, idx out of bounds");
  35593. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  35594. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  35595. printf(resultFmt, "passed");
  35596. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
  35597. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  35598. printf(resultFmt, "passed");
  35599. wolfSSL_X509_free(x509);
  35600. #endif
  35601. }
  35602. static void test_wolfSSL_X509_get_ext_by_NID(void)
  35603. {
  35604. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  35605. int rc;
  35606. FILE* f;
  35607. WOLFSSL_X509* x509;
  35608. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  35609. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  35610. fclose(f);
  35611. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  35612. AssertIntGE(rc, 0);
  35613. /* Start search from last location (should fail) */
  35614. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  35615. AssertIntGE(rc, -1);
  35616. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  35617. AssertIntGE(rc, -1);
  35618. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  35619. AssertIntEQ(rc, -1);
  35620. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  35621. AssertIntEQ(rc, -1);
  35622. wolfSSL_X509_free(x509);
  35623. #endif
  35624. }
  35625. static void test_wolfSSL_X509_get_ext_subj_alt_name(void)
  35626. {
  35627. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  35628. int rc;
  35629. XFILE f;
  35630. WOLFSSL_X509* x509;
  35631. WOLFSSL_X509_EXTENSION* ext;
  35632. WOLFSSL_ASN1_STRING* sanString;
  35633. byte* sanDer;
  35634. const byte expectedDer[] = {
  35635. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  35636. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  35637. printf(testingFmt, "test_wolfSSL_X509_get_ext_subj_alt_name");
  35638. f = XFOPEN("./certs/server-cert.pem", "rb");
  35639. AssertTrue(f != XBADFILE);
  35640. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  35641. fclose(f);
  35642. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  35643. AssertIntNE(rc, -1);
  35644. AssertNotNull(ext = X509_get_ext(x509, rc));
  35645. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  35646. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  35647. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  35648. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  35649. X509_free(x509);
  35650. printf(resultFmt, passed);
  35651. #endif
  35652. }
  35653. static void test_wolfSSL_X509_EXTENSION_new(void)
  35654. {
  35655. #if defined (OPENSSL_ALL)
  35656. WOLFSSL_X509_EXTENSION* ext;
  35657. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  35658. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  35659. ext->obj->nid = WOLFSSL_SUCCESS;
  35660. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  35661. wolfSSL_X509_EXTENSION_free(ext);
  35662. #endif
  35663. }
  35664. static void test_wolfSSL_X509_EXTENSION_get_object(void)
  35665. {
  35666. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  35667. WOLFSSL_X509* x509;
  35668. WOLFSSL_X509_EXTENSION* ext;
  35669. WOLFSSL_ASN1_OBJECT* o;
  35670. FILE* file;
  35671. int nid = 0;
  35672. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  35673. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  35674. fclose(file);
  35675. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() testing ext idx 0");
  35676. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  35677. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  35678. AssertIntEQ(o->nid, 128);
  35679. nid = o->nid;
  35680. printf(resultFmt, nid == 128 ? passed : failed);
  35681. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
  35682. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  35683. printf(resultFmt, passed);
  35684. wolfSSL_X509_free(x509);
  35685. #endif
  35686. }
  35687. static void test_wolfSSL_X509_EXTENSION_get_data(void)
  35688. {
  35689. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  35690. WOLFSSL_X509* x509;
  35691. WOLFSSL_X509_EXTENSION* ext;
  35692. WOLFSSL_ASN1_STRING* str;
  35693. FILE* file;
  35694. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
  35695. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  35696. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  35697. fclose(file);
  35698. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  35699. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  35700. printf(resultFmt, passed);
  35701. wolfSSL_X509_free(x509);
  35702. #endif
  35703. }
  35704. static void test_wolfSSL_X509_EXTENSION_get_critical(void)
  35705. {
  35706. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  35707. WOLFSSL_X509* x509;
  35708. WOLFSSL_X509_EXTENSION* ext;
  35709. FILE* file;
  35710. int crit;
  35711. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
  35712. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  35713. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  35714. fclose(file);
  35715. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  35716. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  35717. AssertIntEQ(crit, 0);
  35718. printf(resultFmt, passed);
  35719. wolfSSL_X509_free(x509);
  35720. #endif
  35721. }
  35722. static void test_wolfSSL_X509V3_EXT_print(void)
  35723. {
  35724. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  35725. !defined(NO_RSA)
  35726. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  35727. {
  35728. FILE* f;
  35729. WOLFSSL_X509* x509;
  35730. X509_EXTENSION * ext = NULL;
  35731. int loc;
  35732. BIO *bio = NULL;
  35733. AssertNotNull(f = fopen(svrCertFile, "rb"));
  35734. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  35735. fclose(f);
  35736. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  35737. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  35738. AssertIntGT(loc, -1);
  35739. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  35740. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  35741. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  35742. AssertIntGT(loc, -1);
  35743. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  35744. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  35745. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  35746. AssertIntGT(loc, -1);
  35747. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  35748. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  35749. wolfSSL_BIO_free(bio);
  35750. wolfSSL_X509_free(x509);
  35751. }
  35752. {
  35753. X509 *x509;
  35754. BIO *bio;
  35755. X509_EXTENSION *ext;
  35756. unsigned int i;
  35757. unsigned int idx;
  35758. /* Some NIDs to test with */
  35759. int nids[] = {
  35760. /* NID_key_usage, currently X509_get_ext returns this as a bit
  35761. * string, which messes up X509V3_EXT_print */
  35762. /* NID_ext_key_usage, */
  35763. NID_subject_alt_name,
  35764. };
  35765. int* n;
  35766. AssertNotNull(bio = BIO_new_fp(stdout, BIO_NOCLOSE));
  35767. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  35768. WOLFSSL_FILETYPE_PEM));
  35769. printf("\nPrinting extension values:\n");
  35770. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  35771. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  35772. * update the index */
  35773. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  35774. AssertNotNull(ext = X509_get_ext(x509, idx));
  35775. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  35776. printf("\n");
  35777. }
  35778. BIO_free(bio);
  35779. X509_free(x509);
  35780. }
  35781. printf(resultFmt, passed);
  35782. #endif
  35783. }
  35784. static void test_wolfSSL_X509_cmp(void)
  35785. {
  35786. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  35787. FILE* file1;
  35788. FILE* file2;
  35789. WOLFSSL_X509* cert1;
  35790. WOLFSSL_X509* cert2;
  35791. int ret = 0;
  35792. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  35793. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  35794. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  35795. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  35796. fclose(file1);
  35797. fclose(file2);
  35798. printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
  35799. ret = wolfSSL_X509_cmp(cert1, cert1);
  35800. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  35801. printf(resultFmt, ret == 0 ? passed : failed);
  35802. printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
  35803. ret = wolfSSL_X509_cmp(cert1, cert2);
  35804. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  35805. printf(resultFmt, ret == -1 ? passed : failed);
  35806. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
  35807. ret = wolfSSL_X509_cmp(NULL, cert2);
  35808. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  35809. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  35810. printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
  35811. ret = wolfSSL_X509_cmp(cert1, NULL);
  35812. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  35813. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  35814. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
  35815. ret = wolfSSL_X509_cmp(NULL, NULL);
  35816. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  35817. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  35818. wolfSSL_X509_free(cert1);
  35819. wolfSSL_X509_free(cert2);
  35820. #endif
  35821. }
  35822. static void test_wolfSSL_PKEY_up_ref(void)
  35823. {
  35824. #if defined(OPENSSL_ALL)
  35825. EVP_PKEY* pkey;
  35826. printf(testingFmt, "wolfSSL_PKEY_up_ref()");
  35827. pkey = EVP_PKEY_new();
  35828. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  35829. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  35830. EVP_PKEY_free(pkey);
  35831. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  35832. EVP_PKEY_free(pkey);
  35833. EVP_PKEY_free(pkey);
  35834. printf(resultFmt, "passed");
  35835. #endif
  35836. }
  35837. static void test_wolfSSL_d2i_and_i2d_PublicKey(void)
  35838. {
  35839. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  35840. EVP_PKEY* pkey;
  35841. const unsigned char* p;
  35842. unsigned char* der = NULL;
  35843. int derLen;
  35844. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_PublicKey()");
  35845. p = client_keypub_der_2048;
  35846. /* Check that key can be successfully decoded. */
  35847. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  35848. sizeof_client_keypub_der_2048));
  35849. /* Check that key can be successfully encoded. */
  35850. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  35851. /* Ensure that the encoded version matches the original. */
  35852. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  35853. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  35854. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  35855. EVP_PKEY_free(pkey);
  35856. printf(resultFmt, passed);
  35857. #endif
  35858. }
  35859. static void test_wolfSSL_d2i_and_i2d_DSAparams(void)
  35860. {
  35861. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  35862. DSA* dsa;
  35863. char file[] = "./certs/dsaparams.der";
  35864. XFILE f;
  35865. int derInLen;
  35866. byte* derIn;
  35867. int derOutLen;
  35868. byte* derOut = NULL;
  35869. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_DSAparams()");
  35870. f = XFOPEN(file, "rb");
  35871. AssertTrue(f != XBADFILE);
  35872. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  35873. derInLen = (int)XFTELL(f);
  35874. XREWIND(f);
  35875. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  35876. DYNAMIC_TYPE_TMP_BUFFER));
  35877. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  35878. XFCLOSE(f);
  35879. /* Check that params can be successfully decoded. */
  35880. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  35881. /* Check that params can be successfully encoded. */
  35882. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  35883. /* Ensure that the encoded version matches the original. */
  35884. AssertIntEQ(derInLen, derOutLen);
  35885. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  35886. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35887. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  35888. DSA_free(dsa);
  35889. printf(resultFmt, passed);
  35890. #endif
  35891. }
  35892. static void test_wolfSSL_i2d_PrivateKey(void)
  35893. {
  35894. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  35895. printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
  35896. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  35897. {
  35898. EVP_PKEY* pkey;
  35899. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  35900. unsigned char buf[FOURK_BUF];
  35901. unsigned char* pt = NULL;
  35902. int bufSz;
  35903. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  35904. (long)sizeof_server_key_der_2048));
  35905. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  35906. pt = buf;
  35907. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  35908. AssertIntNE((pt - buf), 0);
  35909. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  35910. EVP_PKEY_free(pkey);
  35911. }
  35912. #endif
  35913. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  35914. {
  35915. EVP_PKEY* pkey;
  35916. const unsigned char* client_key =
  35917. (const unsigned char*)ecc_clikey_der_256;
  35918. unsigned char buf[FOURK_BUF];
  35919. unsigned char* pt = NULL;
  35920. int bufSz;
  35921. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  35922. sizeof_ecc_clikey_der_256)));
  35923. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  35924. pt = buf;
  35925. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  35926. AssertIntNE((pt - buf), 0);
  35927. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  35928. EVP_PKEY_free(pkey);
  35929. }
  35930. #endif
  35931. printf(resultFmt, "passed");
  35932. #endif
  35933. }
  35934. static void test_wolfSSL_OCSP_id_get0_info(void)
  35935. {
  35936. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  35937. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35938. X509* cert;
  35939. X509* issuer;
  35940. OCSP_CERTID* id;
  35941. OCSP_CERTID* id2;
  35942. ASN1_STRING* name = NULL;
  35943. ASN1_OBJECT* pmd = NULL;
  35944. ASN1_STRING* keyHash = NULL;
  35945. ASN1_INTEGER* serial = NULL;
  35946. ASN1_INTEGER* x509Int;
  35947. printf(testingFmt, "wolfSSL_OCSP_id_get0_info()");
  35948. AssertNotNull(cert =
  35949. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  35950. AssertNotNull(issuer =
  35951. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  35952. id = OCSP_cert_to_id(NULL, cert, issuer);
  35953. AssertNotNull(id);
  35954. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  35955. AssertNotNull(id2);
  35956. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  35957. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  35958. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  35959. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  35960. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  35961. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  35962. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  35963. AssertNotNull(serial);
  35964. /* compare serial number to one in cert, should be equal */
  35965. x509Int = X509_get_serialNumber(cert);
  35966. AssertNotNull(x509Int);
  35967. AssertIntEQ(x509Int->length, serial->length);
  35968. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  35969. /* test OCSP_id_cmp */
  35970. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  35971. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  35972. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  35973. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  35974. id->issuerHash[0] = ~id->issuerHash[0];
  35975. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  35976. OCSP_CERTID_free(id);
  35977. OCSP_CERTID_free(id2);
  35978. X509_free(cert); /* free's x509Int */
  35979. X509_free(issuer);
  35980. printf(resultFmt, "passed");
  35981. #endif
  35982. }
  35983. static void test_wolfSSL_i2d_OCSP_CERTID(void)
  35984. {
  35985. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  35986. WOLFSSL_OCSP_CERTID certId;
  35987. byte* targetBuffer;
  35988. byte* beginTargetBuffer;
  35989. /* OCSP CertID bytes taken from PCAP */
  35990. byte rawCertId[] = {
  35991. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  35992. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  35993. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  35994. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  35995. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  35996. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  35997. 0xcf, 0xbc, 0x91
  35998. };
  35999. int ret, i;
  36000. printf(testingFmt, "wolfSSL_i2d_OCSP_CERTID()");
  36001. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  36002. certId.rawCertId = rawCertId;
  36003. certId.rawCertIdSize = sizeof(rawCertId);
  36004. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36005. beginTargetBuffer = targetBuffer;
  36006. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  36007. /* If target buffer is not null, function increments targetBuffer to point
  36008. just past the end of the encoded data. */
  36009. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  36010. /* Function returns the size of the encoded data. */
  36011. AssertIntEQ(ret, sizeof(rawCertId));
  36012. for (i = 0; i < ret; ++i)
  36013. {
  36014. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  36015. }
  36016. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36017. targetBuffer = NULL;
  36018. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  36019. /* If target buffer is null, function allocates memory for a buffer and
  36020. copies the encoded data into it. targetBuffer then points to the start of
  36021. this newly allocate buffer. */
  36022. AssertIntEQ(ret, sizeof(rawCertId));
  36023. for (i = 0; i < ret; ++i)
  36024. {
  36025. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  36026. }
  36027. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  36028. printf(resultFmt, passed);
  36029. #endif
  36030. }
  36031. static void test_wolfSSL_OCSP_id_cmp(void)
  36032. {
  36033. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  36034. OCSP_CERTID id1;
  36035. OCSP_CERTID id2;
  36036. printf(testingFmt, "wolfSSL_OCSP_id_cmp()");
  36037. XMEMSET(&id1, 0, sizeof(id1));
  36038. XMEMSET(&id2, 0, sizeof(id2));
  36039. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  36040. printf(resultFmt, passed);
  36041. #endif
  36042. }
  36043. static void test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  36044. {
  36045. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  36046. WOLFSSL_OCSP_SINGLERESP single;
  36047. const WOLFSSL_OCSP_CERTID* certId;
  36048. XMEMSET(&single, 0, sizeof(single));
  36049. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  36050. printf(testingFmt, "wolfSSL_OCSP_SINGLERESP_get0_id()");
  36051. AssertPtrEq(&single, certId);
  36052. printf(resultFmt, passed);
  36053. #endif
  36054. }
  36055. static void test_wolfSSL_OCSP_single_get0_status(void)
  36056. {
  36057. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  36058. WOLFSSL_OCSP_SINGLERESP single;
  36059. CertStatus certStatus;
  36060. WOLFSSL_ASN1_TIME* thisDate;
  36061. WOLFSSL_ASN1_TIME* nextDate;
  36062. int ret, i;
  36063. printf(testingFmt, "wolfSSL_OCSP_single_get0_status()");
  36064. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  36065. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  36066. /* Fill the date fields with some dummy data. */
  36067. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  36068. certStatus.thisDateParsed.data[i] = i;
  36069. certStatus.nextDateParsed.data[i] = i;
  36070. }
  36071. certStatus.status = CERT_GOOD;
  36072. single.status = &certStatus;
  36073. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  36074. &nextDate);
  36075. AssertIntEQ(ret, CERT_GOOD);
  36076. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  36077. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  36078. printf(resultFmt, passed);
  36079. #endif
  36080. }
  36081. static void test_wolfSSL_OCSP_resp_count(void)
  36082. {
  36083. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  36084. WOLFSSL_OCSP_BASICRESP basicResp;
  36085. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  36086. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  36087. int count;
  36088. printf(testingFmt, "wolfSSL_OCSP_resp_count()");
  36089. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  36090. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  36091. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  36092. count = wolfSSL_OCSP_resp_count(&basicResp);
  36093. AssertIntEQ(count, 0);
  36094. basicResp.single = &singleRespOne;
  36095. count = wolfSSL_OCSP_resp_count(&basicResp);
  36096. AssertIntEQ(count, 1);
  36097. singleRespOne.next = &singleRespTwo;
  36098. count = wolfSSL_OCSP_resp_count(&basicResp);
  36099. AssertIntEQ(count, 2);
  36100. printf(resultFmt, passed);
  36101. #endif
  36102. }
  36103. static void test_wolfSSL_OCSP_resp_get0(void)
  36104. {
  36105. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  36106. WOLFSSL_OCSP_BASICRESP basicResp;
  36107. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  36108. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  36109. WOLFSSL_OCSP_SINGLERESP* ret;
  36110. printf(testingFmt, "wolfSSL_OCSP_resp_get0()");
  36111. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  36112. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  36113. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  36114. basicResp.single = &singleRespOne;
  36115. singleRespOne.next = &singleRespTwo;
  36116. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  36117. AssertPtrEq(ret, &singleRespOne);
  36118. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  36119. AssertPtrEq(ret, &singleRespTwo);
  36120. printf(resultFmt, passed);
  36121. #endif
  36122. }
  36123. static void test_wolfSSL_EVP_PKEY_derive(void)
  36124. {
  36125. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  36126. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  36127. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  36128. printf(testingFmt, "wolfSSL_EVP_PKEY_derive()");
  36129. EVP_PKEY_CTX *ctx;
  36130. unsigned char *skey;
  36131. size_t skeylen;
  36132. EVP_PKEY *pkey, *peerkey;
  36133. const unsigned char* key;
  36134. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  36135. /* DH */
  36136. key = dh_key_der_2048;
  36137. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  36138. sizeof_dh_key_der_2048)));
  36139. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  36140. key = dh_key_der_2048;
  36141. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  36142. sizeof_dh_key_der_2048)));
  36143. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  36144. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  36145. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  36146. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  36147. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  36148. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  36149. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  36150. EVP_PKEY_CTX_free(ctx);
  36151. EVP_PKEY_free(peerkey);
  36152. EVP_PKEY_free(pkey);
  36153. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  36154. #endif
  36155. #ifdef HAVE_ECC
  36156. /* ECDH */
  36157. key = ecc_clikey_der_256;
  36158. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  36159. sizeof_ecc_clikey_der_256)));
  36160. key = ecc_clikeypub_der_256;
  36161. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  36162. sizeof_ecc_clikeypub_der_256)));
  36163. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  36164. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  36165. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  36166. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  36167. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  36168. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  36169. EVP_PKEY_CTX_free(ctx);
  36170. EVP_PKEY_free(peerkey);
  36171. EVP_PKEY_free(pkey);
  36172. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  36173. #endif /* HAVE_ECC */
  36174. printf(resultFmt, "passed");
  36175. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  36176. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36177. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  36178. }
  36179. #ifndef NO_RSA
  36180. static void test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  36181. {
  36182. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  36183. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  36184. RSA *rsa;
  36185. const unsigned char *derBuf = client_key_der_2048;
  36186. unsigned char em[256] = {0}; /* len = 2048/8 */
  36187. /* Random data simulating a hash */
  36188. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  36189. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  36190. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  36191. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  36192. };
  36193. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  36194. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -1), 1);
  36195. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -1), 1);
  36196. RSA_free(rsa);
  36197. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36198. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  36199. }
  36200. #endif
  36201. static void test_wolfSSL_RSA_sign_sha3(void)
  36202. {
  36203. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  36204. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  36205. RSA *rsa;
  36206. const unsigned char *derBuf = client_key_der_2048;
  36207. unsigned char sigRet[256] = {0};
  36208. unsigned int sigLen = sizeof(sigRet);
  36209. /* Random data simulating a hash */
  36210. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  36211. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  36212. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  36213. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  36214. };
  36215. printf(testingFmt, "wolfSSL_RSA_sign_sha3");
  36216. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  36217. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  36218. &sigLen, rsa), 1);
  36219. RSA_free(rsa);
  36220. printf(resultFmt, passed);
  36221. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  36222. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  36223. }
  36224. static void test_wolfSSL_EC_get_builtin_curves(void)
  36225. {
  36226. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  36227. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  36228. EC_builtin_curve* curves = NULL;
  36229. size_t crv_len = 0;
  36230. size_t i = 0;
  36231. printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
  36232. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  36233. AssertNotNull(curves = (EC_builtin_curve*)
  36234. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  36235. DYNAMIC_TYPE_TMP_BUFFER));
  36236. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  36237. for (i = 0; i < crv_len; i++)
  36238. {
  36239. if (curves[i].comment != NULL)
  36240. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  36241. }
  36242. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36243. printf(resultFmt, passed);
  36244. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36245. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  36246. }
  36247. static void test_no_op_functions(void)
  36248. {
  36249. #if defined(OPENSSL_EXTRA)
  36250. printf(testingFmt, "no_op_functions()");
  36251. /* this makes sure wolfSSL can compile and run these no-op functions */
  36252. SSL_load_error_strings();
  36253. ENGINE_load_builtin_engines();
  36254. OpenSSL_add_all_ciphers();
  36255. AssertIntEQ(CRYPTO_malloc_init(), 0);
  36256. printf(resultFmt, passed);
  36257. #endif
  36258. }
  36259. static void test_wolfSSL_CRYPTO_memcmp(void)
  36260. {
  36261. #ifdef OPENSSL_EXTRA
  36262. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  36263. "implementation of TLS/SSL for embedded devices to the cloud.";
  36264. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  36265. "implementation of TLS/SSL for embedded devices to the cloud.";
  36266. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  36267. "implementation of TLS/SSL for embedded devices to the cloud!";
  36268. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  36269. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  36270. #endif
  36271. }
  36272. /*----------------------------------------------------------------------------*
  36273. | wolfCrypt ASN
  36274. *----------------------------------------------------------------------------*/
  36275. static void test_wc_CreateEncryptedPKCS8Key(void)
  36276. {
  36277. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  36278. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  36279. WC_RNG rng;
  36280. byte* encKey = NULL;
  36281. word32 encKeySz = 0;
  36282. word32 decKeySz = 0;
  36283. const char password[] = "Lorem ipsum dolor sit amet";
  36284. word32 passwordSz = (word32)XSTRLEN(password);
  36285. word32 tradIdx = 0;
  36286. printf(testingFmt, "test_wc_CreateEncryptedPKCS8Key");
  36287. AssertIntEQ(wc_InitRng(&rng), 0);
  36288. /* Call with NULL for out buffer to get necessary length. */
  36289. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  36290. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  36291. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  36292. LENGTH_ONLY_E);
  36293. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  36294. DYNAMIC_TYPE_TMP_BUFFER));
  36295. /* Call with the allocated out buffer. */
  36296. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  36297. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  36298. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  36299. 0);
  36300. /* Decrypt the encrypted PKCS8 key we just made. */
  36301. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  36302. passwordSz)), 0);
  36303. /* encKey now holds the decrypted key (decrypted in place). */
  36304. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  36305. /* Check that the decrypted key matches the key prior to encryption. */
  36306. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  36307. sizeof_server_key_der_2048), 0);
  36308. if (encKey != NULL)
  36309. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36310. wc_FreeRng(&rng);
  36311. printf(resultFmt, passed);
  36312. #endif
  36313. }
  36314. static void test_wc_GetPkcs8TraditionalOffset(void)
  36315. {
  36316. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  36317. int length, derSz;
  36318. word32 inOutIdx;
  36319. const char* path = "./certs/server-keyPkcs8.der";
  36320. XFILE file;
  36321. byte der[2048];
  36322. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  36323. file = XFOPEN(path, "rb");
  36324. AssertTrue(file != XBADFILE);
  36325. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  36326. XFCLOSE(file);
  36327. /* valid case */
  36328. inOutIdx = 0;
  36329. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  36330. AssertIntGT(length, 0);
  36331. /* inOutIdx > sz */
  36332. inOutIdx = 4000;
  36333. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  36334. AssertIntEQ(length, BAD_FUNC_ARG);
  36335. /* null input */
  36336. inOutIdx = 0;
  36337. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  36338. AssertIntEQ(length, BAD_FUNC_ARG);
  36339. /* invalid input, fill buffer with 1's */
  36340. XMEMSET(der, 1, sizeof(der));
  36341. inOutIdx = 0;
  36342. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  36343. AssertIntEQ(length, ASN_PARSE_E);
  36344. printf(resultFmt, passed);
  36345. #endif /* NO_ASN */
  36346. }
  36347. static void test_wc_SetSubjectRaw(void)
  36348. {
  36349. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  36350. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  36351. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  36352. WOLFSSL_X509* x509;
  36353. int peerCertSz;
  36354. const byte* peerCertBuf;
  36355. Cert forgedCert;
  36356. printf(testingFmt, "test_wc_SetSubjectRaw()");
  36357. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  36358. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  36359. AssertIntEQ(0, wc_InitCert(&forgedCert));
  36360. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  36361. wolfSSL_FreeX509(x509);
  36362. printf(resultFmt, passed);
  36363. #endif
  36364. }
  36365. static void test_wc_GetSubjectRaw(void)
  36366. {
  36367. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  36368. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  36369. Cert cert;
  36370. byte *subjectRaw;
  36371. printf(testingFmt, "test_wc_GetSubjectRaw()");
  36372. AssertIntEQ(0, wc_InitCert(&cert));
  36373. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  36374. printf(resultFmt, passed);
  36375. #endif
  36376. }
  36377. static void test_wc_SetIssuerRaw(void)
  36378. {
  36379. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  36380. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  36381. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  36382. WOLFSSL_X509* x509;
  36383. int peerCertSz;
  36384. const byte* peerCertBuf;
  36385. Cert forgedCert;
  36386. printf(testingFmt, "test_wc_SetIssuerRaw()");
  36387. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  36388. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  36389. AssertIntEQ(0, wc_InitCert(&forgedCert));
  36390. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  36391. wolfSSL_FreeX509(x509);
  36392. printf(resultFmt, passed);
  36393. #endif
  36394. }
  36395. static void test_wc_SetIssueBuffer(void)
  36396. {
  36397. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  36398. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  36399. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  36400. WOLFSSL_X509* x509;
  36401. int peerCertSz;
  36402. const byte* peerCertBuf;
  36403. Cert forgedCert;
  36404. printf(testingFmt, "test_wc_SetIssuerBuffer()");
  36405. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  36406. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  36407. AssertIntEQ(0, wc_InitCert(&forgedCert));
  36408. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  36409. wolfSSL_FreeX509(x509);
  36410. printf(resultFmt, passed);
  36411. #endif
  36412. }
  36413. /*
  36414. * Testing wc_SetSubjectKeyId
  36415. */
  36416. static void test_wc_SetSubjectKeyId(void)
  36417. {
  36418. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  36419. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  36420. Cert cert;
  36421. const char* file = "certs/ecc-client-keyPub.pem";
  36422. printf(testingFmt, "wc_SetSubjectKeyId()");
  36423. AssertIntEQ(0, wc_InitCert(&cert));
  36424. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  36425. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  36426. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  36427. printf(resultFmt, passed);
  36428. #endif
  36429. } /* END test_wc_SetSubjectKeyId */
  36430. /*
  36431. * Testing wc_SetSubject
  36432. */
  36433. static void test_wc_SetSubject(void)
  36434. {
  36435. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  36436. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  36437. Cert cert;
  36438. const char* file = "./certs/ca-ecc-cert.pem";
  36439. printf(testingFmt, "wc_SetSubject()");
  36440. AssertIntEQ(0, wc_InitCert(&cert));
  36441. AssertIntEQ(0, wc_SetSubject(&cert, file));
  36442. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  36443. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  36444. printf(resultFmt, passed);
  36445. #endif
  36446. } /* END test_wc_SetSubject */
  36447. static void test_CheckCertSignature(void)
  36448. {
  36449. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  36450. WOLFSSL_CERT_MANAGER* cm = NULL;
  36451. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  36452. FILE* fp;
  36453. byte cert[4096];
  36454. int certSz;
  36455. #endif
  36456. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  36457. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  36458. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  36459. #ifndef NO_RSA
  36460. #ifdef USE_CERT_BUFFERS_1024
  36461. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  36462. sizeof_server_cert_der_1024, NULL, cm));
  36463. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  36464. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  36465. WOLFSSL_FILETYPE_ASN1));
  36466. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  36467. sizeof_server_cert_der_1024, NULL, cm));
  36468. #elif defined(USE_CERT_BUFFERS_2048)
  36469. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  36470. sizeof_server_cert_der_2048, NULL, cm));
  36471. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  36472. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  36473. WOLFSSL_FILETYPE_ASN1));
  36474. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  36475. sizeof_server_cert_der_2048, NULL, cm));
  36476. #endif
  36477. #endif
  36478. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  36479. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  36480. sizeof_serv_ecc_der_256, NULL, cm));
  36481. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  36482. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  36483. WOLFSSL_FILETYPE_ASN1));
  36484. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  36485. NULL, cm));
  36486. #endif
  36487. #if !defined(NO_FILESYSTEM)
  36488. wolfSSL_CertManagerFree(cm);
  36489. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  36490. #ifndef NO_RSA
  36491. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  36492. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  36493. XFCLOSE(fp);
  36494. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  36495. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  36496. "./certs/ca-cert.pem", NULL));
  36497. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  36498. #endif
  36499. #ifdef HAVE_ECC
  36500. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  36501. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  36502. XFCLOSE(fp);
  36503. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  36504. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  36505. "./certs/ca-ecc-cert.pem", NULL));
  36506. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  36507. #endif
  36508. #endif
  36509. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  36510. (void)fp;
  36511. (void)cert;
  36512. (void)certSz;
  36513. #endif
  36514. wolfSSL_CertManagerFree(cm);
  36515. #endif
  36516. }
  36517. /*----------------------------------------------------------------------------*
  36518. | wolfCrypt ECC
  36519. *----------------------------------------------------------------------------*/
  36520. static void test_wc_ecc_get_curve_size_from_name(void)
  36521. {
  36522. #ifdef HAVE_ECC
  36523. int ret;
  36524. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  36525. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  36526. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  36527. AssertIntEQ(ret, 32);
  36528. #endif
  36529. /* invalid case */
  36530. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  36531. AssertIntEQ(ret, -1);
  36532. /* NULL input */
  36533. ret = wc_ecc_get_curve_size_from_name(NULL);
  36534. AssertIntEQ(ret, BAD_FUNC_ARG);
  36535. printf(resultFmt, passed);
  36536. #endif /* HAVE_ECC */
  36537. }
  36538. static void test_wc_ecc_get_curve_id_from_name(void)
  36539. {
  36540. #ifdef HAVE_ECC
  36541. int id;
  36542. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  36543. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  36544. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  36545. AssertIntEQ(id, ECC_SECP256R1);
  36546. #endif
  36547. /* invalid case */
  36548. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  36549. AssertIntEQ(id, -1);
  36550. /* NULL input */
  36551. id = wc_ecc_get_curve_id_from_name(NULL);
  36552. AssertIntEQ(id, BAD_FUNC_ARG);
  36553. printf(resultFmt, passed);
  36554. #endif /* HAVE_ECC */
  36555. }
  36556. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  36557. !defined(HAVE_SELFTEST) && \
  36558. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  36559. static void test_wc_ecc_get_curve_id_from_dp_params(void)
  36560. {
  36561. int id;
  36562. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  36563. int curve_id;
  36564. ecc_key* key;
  36565. const ecc_set_type* params;
  36566. int ret;
  36567. #endif
  36568. WOLFSSL_EC_KEY *ecKey = NULL;
  36569. printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
  36570. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  36571. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  36572. AssertIntEQ(id, ECC_SECP256R1);
  36573. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  36574. AssertNotNull(ecKey);
  36575. ret = EC_KEY_generate_key(ecKey);
  36576. if (ret == 0) {
  36577. /* normal test */
  36578. key = (ecc_key*)ecKey->internal;
  36579. params = key->dp;
  36580. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  36581. AssertIntEQ(curve_id, id);
  36582. }
  36583. #endif
  36584. /* invalid case, NULL input*/
  36585. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  36586. AssertIntEQ(id, BAD_FUNC_ARG);
  36587. wolfSSL_EC_KEY_free(ecKey);
  36588. printf(resultFmt, passed);
  36589. }
  36590. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  36591. static void test_wc_ecc_get_curve_id_from_params(void)
  36592. {
  36593. #ifdef HAVE_ECC
  36594. int id;
  36595. const byte prime[] =
  36596. {
  36597. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  36598. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  36599. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  36600. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  36601. };
  36602. const byte primeInvalid[] =
  36603. {
  36604. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  36605. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  36606. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  36607. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  36608. };
  36609. const byte Af[] =
  36610. {
  36611. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  36612. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  36613. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  36614. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  36615. };
  36616. const byte Bf[] =
  36617. {
  36618. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  36619. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  36620. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  36621. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  36622. };
  36623. const byte order[] =
  36624. {
  36625. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  36626. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  36627. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  36628. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  36629. };
  36630. const byte Gx[] =
  36631. {
  36632. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  36633. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  36634. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  36635. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  36636. };
  36637. const byte Gy[] =
  36638. {
  36639. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  36640. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  36641. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  36642. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  36643. };
  36644. int cofactor = 1;
  36645. int fieldSize = 256;
  36646. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  36647. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  36648. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  36649. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  36650. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  36651. AssertIntEQ(id, ECC_SECP256R1);
  36652. #endif
  36653. /* invalid case, fieldSize = 0 */
  36654. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  36655. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  36656. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  36657. AssertIntEQ(id, ECC_CURVE_INVALID);
  36658. /* invalid case, NULL prime */
  36659. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  36660. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  36661. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  36662. AssertIntEQ(id, BAD_FUNC_ARG);
  36663. /* invalid case, invalid prime */
  36664. id = wc_ecc_get_curve_id_from_params(fieldSize,
  36665. primeInvalid, sizeof(primeInvalid),
  36666. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  36667. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  36668. AssertIntEQ(id, ECC_CURVE_INVALID);
  36669. printf(resultFmt, passed);
  36670. #endif
  36671. }
  36672. static void test_wolfSSL_EVP_PKEY_encrypt(void)
  36673. {
  36674. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  36675. !defined(HAVE_FAST_RSA)
  36676. WOLFSSL_RSA* rsa = NULL;
  36677. WOLFSSL_EVP_PKEY* pkey = NULL;
  36678. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  36679. const char* in = "What is easy to do is easy not to do.";
  36680. size_t inlen = XSTRLEN(in);
  36681. size_t outEncLen = 0;
  36682. byte* outEnc = NULL;
  36683. byte* outDec = NULL;
  36684. size_t outDecLen = 0;
  36685. size_t rsaKeySz = 2048/8; /* Bytes */
  36686. #ifdef WC_RSA_NO_PADDING
  36687. byte* inTmp = NULL;
  36688. byte* outEncTmp = NULL;
  36689. byte* outDecTmp = NULL;
  36690. #endif
  36691. printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
  36692. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  36693. XMEMSET(outEnc, 0, rsaKeySz);
  36694. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  36695. XMEMSET(outDec, 0, rsaKeySz);
  36696. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  36697. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36698. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  36699. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  36700. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  36701. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  36702. WOLFSSL_SUCCESS);
  36703. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  36704. since ctx uses it.*/
  36705. AssertIntEQ(pkey->references, 2);
  36706. EVP_PKEY_free(pkey);
  36707. AssertIntEQ(pkey->references, 1);
  36708. /* Encrypt data */
  36709. /* Check that we can get the required output buffer length by passing in a
  36710. * NULL output buffer. */
  36711. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  36712. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  36713. AssertIntEQ(rsaKeySz, outEncLen);
  36714. /* Now do the actual encryption. */
  36715. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  36716. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  36717. /* Decrypt data */
  36718. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  36719. /* Check that we can get the required output buffer length by passing in a
  36720. * NULL output buffer. */
  36721. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  36722. WOLFSSL_SUCCESS);
  36723. AssertIntEQ(rsaKeySz, outDecLen);
  36724. /* Now do the actual decryption. */
  36725. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  36726. WOLFSSL_SUCCESS);
  36727. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  36728. #ifdef WC_RSA_NO_PADDING
  36729. /* The input length must be the same size as the RSA key.*/
  36730. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  36731. XMEMSET(inTmp, 9, rsaKeySz);
  36732. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  36733. XMEMSET(outEncTmp, 0, rsaKeySz);
  36734. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  36735. XMEMSET(outDecTmp, 0, rsaKeySz);
  36736. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  36737. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  36738. WOLFSSL_SUCCESS);
  36739. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  36740. WOLFSSL_SUCCESS);
  36741. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  36742. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  36743. WOLFSSL_SUCCESS);
  36744. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  36745. #endif
  36746. EVP_PKEY_CTX_free(ctx);
  36747. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36748. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36749. #ifdef WC_RSA_NO_PADDING
  36750. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36751. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36752. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36753. #endif
  36754. printf(resultFmt, passed);
  36755. #endif
  36756. }
  36757. static void test_wolfSSL_EVP_PKEY_sign(void)
  36758. {
  36759. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  36760. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  36761. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  36762. WOLFSSL_RSA* rsa = NULL;
  36763. WOLFSSL_EVP_PKEY* pkey = NULL;
  36764. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  36765. const char* in = "What is easy to do is easy not to do.";
  36766. size_t inlen = XSTRLEN(in);
  36767. byte hash[SHA256_DIGEST_LENGTH] = {0};
  36768. SHA256_CTX c;
  36769. byte* sig = NULL;
  36770. byte* sigVerify = NULL;
  36771. size_t siglen = 0;
  36772. size_t rsaKeySz = 2048/8; /* Bytes */
  36773. printf(testingFmt, "wolfSSL_EVP_PKEY_sign()");
  36774. sig = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36775. AssertNotNull(sig);
  36776. XMEMSET(sig, 0, rsaKeySz);
  36777. AssertNotNull(sigVerify = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  36778. XMEMSET(sigVerify, 0, rsaKeySz);
  36779. /* Generate hash */
  36780. SHA256_Init(&c);
  36781. SHA256_Update(&c, in, inlen);
  36782. SHA256_Final(hash, &c);
  36783. #ifdef WOLFSSL_SMALL_STACK_CACHE
  36784. /* workaround for small stack cache case */
  36785. wc_Sha256Free((wc_Sha256*)&c);
  36786. #endif
  36787. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  36788. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36789. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  36790. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  36791. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  36792. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  36793. WOLFSSL_SUCCESS);
  36794. /* Sign data */
  36795. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash, SHA256_DIGEST_LENGTH),
  36796. WOLFSSL_SUCCESS);
  36797. /* Verify signature.
  36798. EVP_PKEY_verify() doesn't exist yet, so use RSA_public_decrypt(). */
  36799. AssertIntEQ(RSA_public_decrypt((int)siglen, sig, sigVerify,
  36800. rsa, RSA_PKCS1_PADDING), SHA256_DIGEST_LENGTH);
  36801. AssertIntEQ(XMEMCMP(hash, sigVerify, SHA256_DIGEST_LENGTH), 0);
  36802. /* error cases */
  36803. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  36804. ctx->pkey->type = EVP_PKEY_RSA;
  36805. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  36806. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  36807. WOLFSSL_SUCCESS);
  36808. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  36809. WOLFSSL_SUCCESS);
  36810. EVP_PKEY_free(pkey);
  36811. EVP_PKEY_CTX_free(ctx);
  36812. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36813. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36814. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36815. printf(resultFmt, passed);
  36816. #endif
  36817. }
  36818. static void test_EVP_PKEY_rsa(void)
  36819. {
  36820. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  36821. WOLFSSL_RSA* rsa;
  36822. WOLFSSL_EVP_PKEY* pkey;
  36823. AssertNotNull(rsa = wolfSSL_RSA_new());
  36824. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36825. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  36826. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  36827. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  36828. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  36829. wolfSSL_EVP_PKEY_free(pkey);
  36830. printf(resultFmt, passed);
  36831. #endif
  36832. }
  36833. static void test_EVP_PKEY_ec(void)
  36834. {
  36835. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  36836. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  36837. WOLFSSL_EC_KEY* ecKey;
  36838. WOLFSSL_EVP_PKEY* pkey;
  36839. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  36840. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36841. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  36842. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  36843. /* Should fail since ecKey is empty */
  36844. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  36845. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  36846. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  36847. wolfSSL_EVP_PKEY_free(pkey);
  36848. printf(resultFmt, passed);
  36849. #endif
  36850. #endif
  36851. }
  36852. static void test_EVP_PKEY_cmp(void)
  36853. {
  36854. #if defined(OPENSSL_EXTRA)
  36855. EVP_PKEY *a, *b;
  36856. const unsigned char *in;
  36857. printf(testingFmt, "wolfSSL_EVP_PKEY_cmp()");
  36858. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  36859. in = client_key_der_2048;
  36860. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  36861. &in, (long)sizeof_client_key_der_2048));
  36862. in = client_key_der_2048;
  36863. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  36864. &in, (long)sizeof_client_key_der_2048));
  36865. /* Test success case RSA */
  36866. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  36867. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  36868. #else
  36869. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  36870. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  36871. EVP_PKEY_free(b);
  36872. EVP_PKEY_free(a);
  36873. #endif
  36874. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  36875. in = ecc_clikey_der_256;
  36876. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  36877. &in, (long)sizeof_ecc_clikey_der_256));
  36878. in = ecc_clikey_der_256;
  36879. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  36880. &in, (long)sizeof_ecc_clikey_der_256));
  36881. /* Test success case ECC */
  36882. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  36883. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  36884. #else
  36885. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  36886. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  36887. EVP_PKEY_free(b);
  36888. EVP_PKEY_free(a);
  36889. #endif
  36890. /* Test failure cases */
  36891. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  36892. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  36893. in = client_key_der_2048;
  36894. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  36895. &in, (long)sizeof_client_key_der_2048));
  36896. in = ecc_clikey_der_256;
  36897. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  36898. &in, (long)sizeof_ecc_clikey_der_256));
  36899. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  36900. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  36901. #else
  36902. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  36903. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  36904. EVP_PKEY_free(b);
  36905. EVP_PKEY_free(a);
  36906. #endif
  36907. /* invalid or empty failure cases */
  36908. a = EVP_PKEY_new();
  36909. b = EVP_PKEY_new();
  36910. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  36911. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  36912. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  36913. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  36914. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  36915. #else
  36916. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  36917. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  36918. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  36919. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  36920. #endif
  36921. EVP_PKEY_free(b);
  36922. EVP_PKEY_free(a);
  36923. (void)in;
  36924. printf(resultFmt, passed);
  36925. #endif
  36926. }
  36927. static void test_ERR_load_crypto_strings(void)
  36928. {
  36929. #if defined(OPENSSL_ALL)
  36930. ERR_load_crypto_strings();
  36931. printf(resultFmt, passed);
  36932. #endif
  36933. }
  36934. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  36935. static void free_x509(X509* x)
  36936. {
  36937. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  36938. }
  36939. #endif
  36940. static void test_sk_X509(void)
  36941. {
  36942. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  36943. STACK_OF(X509)* s;
  36944. AssertNotNull(s = sk_X509_new());
  36945. AssertIntEQ(sk_X509_num(s), 0);
  36946. sk_X509_free(s);
  36947. AssertNotNull(s = sk_X509_new_null());
  36948. AssertIntEQ(sk_X509_num(s), 0);
  36949. sk_X509_free(s);
  36950. AssertNotNull(s = sk_X509_new());
  36951. sk_X509_push(s, (X509*)1);
  36952. AssertIntEQ(sk_X509_num(s), 1);
  36953. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  36954. sk_X509_push(s, (X509*)2);
  36955. AssertIntEQ(sk_X509_num(s), 2);
  36956. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  36957. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  36958. sk_X509_push(s, (X509*)2);
  36959. sk_X509_pop_free(s, free_x509);
  36960. printf(resultFmt, passed);
  36961. #endif
  36962. }
  36963. static void test_X509_get_signature_nid(void)
  36964. {
  36965. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36966. X509* x509;
  36967. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  36968. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36969. SSL_FILETYPE_PEM));
  36970. AssertIntEQ(X509_get_signature_nid(x509), CTC_SHA256wRSA);
  36971. X509_free(x509);
  36972. printf(resultFmt, passed);
  36973. #endif
  36974. }
  36975. static void test_X509_REQ(void)
  36976. {
  36977. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  36978. defined(WOLFSSL_CERT_REQ)
  36979. X509_NAME* name;
  36980. #if !defined(NO_RSA) || defined(HAVE_ECC)
  36981. X509_REQ* req;
  36982. EVP_PKEY* priv;
  36983. EVP_PKEY* pub;
  36984. unsigned char* der = NULL;
  36985. #endif
  36986. #ifndef NO_RSA
  36987. EVP_MD_CTX *mctx = NULL;
  36988. EVP_PKEY_CTX *pkctx = NULL;
  36989. #ifdef USE_CERT_BUFFERS_1024
  36990. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  36991. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  36992. #elif defined(USE_CERT_BUFFERS_2048)
  36993. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  36994. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  36995. #endif
  36996. #endif
  36997. #ifdef HAVE_ECC
  36998. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  36999. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  37000. int len;
  37001. #endif
  37002. AssertNotNull(name = X509_NAME_new());
  37003. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  37004. (byte*)"wolfssl.com", 11, 0, 1),
  37005. WOLFSSL_SUCCESS);
  37006. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  37007. (byte*)"support@wolfssl.com", 19, -1,
  37008. 1), WOLFSSL_SUCCESS);
  37009. #ifndef NO_RSA
  37010. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  37011. (long)sizeof_client_key_der_2048));
  37012. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  37013. (long)sizeof_client_keypub_der_2048));
  37014. AssertNotNull(req = X509_REQ_new());
  37015. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  37016. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  37017. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  37018. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  37019. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  37020. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  37021. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  37022. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  37023. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  37024. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  37025. AssertIntEQ(i2d_X509_REQ(req, &der), 643);
  37026. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  37027. der = NULL;
  37028. mctx = EVP_MD_CTX_new();
  37029. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  37030. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  37031. EVP_MD_CTX_free(mctx);
  37032. X509_REQ_free(NULL);
  37033. X509_REQ_free(req);
  37034. EVP_PKEY_free(pub);
  37035. EVP_PKEY_free(priv);
  37036. #endif
  37037. #ifdef HAVE_ECC
  37038. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  37039. sizeof_ecc_clikey_der_256));
  37040. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  37041. sizeof_ecc_clikeypub_der_256));
  37042. AssertNotNull(req = X509_REQ_new());
  37043. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  37044. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  37045. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  37046. /* Signature is random and may be shorter or longer. */
  37047. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  37048. AssertIntLE(len, 253);
  37049. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  37050. X509_REQ_free(req);
  37051. EVP_PKEY_free(pub);
  37052. EVP_PKEY_free(priv);
  37053. #ifdef FP_ECC
  37054. wc_ecc_fp_free();
  37055. #endif
  37056. #endif /* HAVE_ECC */
  37057. X509_NAME_free(name);
  37058. printf(resultFmt, passed);
  37059. #endif
  37060. }
  37061. static void test_wolfssl_PKCS7(void)
  37062. {
  37063. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  37064. PKCS7* pkcs7;
  37065. byte data[FOURK_BUF];
  37066. word32 len = sizeof(data);
  37067. const byte* p = data;
  37068. byte content[] = "Test data to encode.";
  37069. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  37070. BIO* bio;
  37071. byte key[sizeof(client_key_der_2048)];
  37072. word32 keySz = (word32)sizeof(key);
  37073. #endif
  37074. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  37075. (word32)sizeof(content),
  37076. 0, 0)), 0);
  37077. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  37078. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  37079. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  37080. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  37081. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  37082. PKCS7_free(pkcs7);
  37083. /* fail case, without PKCS7_NOVERIFY */
  37084. p = data;
  37085. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  37086. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  37087. 0), WOLFSSL_FAILURE);
  37088. PKCS7_free(pkcs7);
  37089. /* success case, with PKCS7_NOVERIFY */
  37090. p = data;
  37091. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  37092. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  37093. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  37094. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  37095. /* test i2d */
  37096. XMEMCPY(key, client_key_der_2048, keySz);
  37097. pkcs7->privateKey = key;
  37098. pkcs7->privateKeySz = (word32)sizeof(key);
  37099. pkcs7->encryptOID = RSAk;
  37100. pkcs7->hashOID = SHAh;
  37101. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37102. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  37103. BIO_free(bio);
  37104. #endif
  37105. PKCS7_free(NULL);
  37106. PKCS7_free(pkcs7);
  37107. printf(resultFmt, passed);
  37108. #endif
  37109. }
  37110. static void test_wolfSSL_PKCS7_SIGNED_new(void)
  37111. {
  37112. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  37113. PKCS7_SIGNED* pkcs7;
  37114. printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
  37115. pkcs7 = PKCS7_SIGNED_new();
  37116. AssertNotNull(pkcs7);
  37117. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  37118. PKCS7_SIGNED_free(pkcs7);
  37119. printf(resultFmt, passed);
  37120. #endif
  37121. }
  37122. #ifndef NO_BIO
  37123. static void test_wolfSSL_PEM_write_bio_PKCS7(void)
  37124. {
  37125. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  37126. PKCS7* pkcs7 = NULL;
  37127. BIO* bio = NULL;
  37128. const byte* cert_buf = NULL;
  37129. int ret = 0;
  37130. WC_RNG rng;
  37131. const byte data[] = { /* Hello World */
  37132. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  37133. 0x72,0x6c,0x64
  37134. };
  37135. #ifndef NO_RSA
  37136. #if defined(USE_CERT_BUFFERS_2048)
  37137. byte key[sizeof(client_key_der_2048)];
  37138. byte cert[sizeof(client_cert_der_2048)];
  37139. word32 keySz = (word32)sizeof(key);
  37140. word32 certSz = (word32)sizeof(cert);
  37141. XMEMSET(key, 0, keySz);
  37142. XMEMSET(cert, 0, certSz);
  37143. XMEMCPY(key, client_key_der_2048, keySz);
  37144. XMEMCPY(cert, client_cert_der_2048, certSz);
  37145. #elif defined(USE_CERT_BUFFERS_1024)
  37146. byte key[sizeof_client_key_der_1024];
  37147. byte cert[sizeof(sizeof_client_cert_der_1024)];
  37148. word32 keySz = (word32)sizeof(key);
  37149. word32 certSz = (word32)sizeof(cert);
  37150. XMEMSET(key, 0, keySz);
  37151. XMEMSET(cert, 0, certSz);
  37152. XMEMCPY(key, client_key_der_1024, keySz);
  37153. XMEMCPY(cert, client_cert_der_1024, certSz);
  37154. #else
  37155. unsigned char cert[ONEK_BUF];
  37156. unsigned char key[ONEK_BUF];
  37157. XFILE fp;
  37158. int certSz;
  37159. int keySz;
  37160. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  37161. AssertTrue((fp != XBADFILE));
  37162. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  37163. XFCLOSE(fp);
  37164. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  37165. AssertTrue(fp != XBADFILE);
  37166. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  37167. XFCLOSE(fp);
  37168. #endif
  37169. #elif defined(HAVE_ECC)
  37170. #if defined(USE_CERT_BUFFERS_256)
  37171. unsigned char cert[sizeof(cliecc_cert_der_256)];
  37172. unsigned char key[sizeof(ecc_clikey_der_256)];
  37173. int certSz = (int)sizeof(cert);
  37174. int keySz = (int)sizeof(key);
  37175. XMEMSET(cert, 0, certSz);
  37176. XMEMSET(key, 0, keySz);
  37177. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  37178. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  37179. #else
  37180. unsigned char cert[ONEK_BUF];
  37181. unsigned char key[ONEK_BUF];
  37182. XFILE fp;
  37183. int certSz, keySz;
  37184. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  37185. AssertTrue(fp != XBADFILE);
  37186. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  37187. XFCLOSE(fp);
  37188. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  37189. AssertTrue(fp != XBADFILE);
  37190. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  37191. XFCLOSE(fp);
  37192. #endif
  37193. #else
  37194. #error PKCS7 requires ECC or RSA
  37195. #endif
  37196. printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
  37197. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  37198. /* initialize with DER encoded cert */
  37199. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  37200. /* init rng */
  37201. AssertIntEQ(wc_InitRng(&rng), 0);
  37202. pkcs7->rng = &rng;
  37203. pkcs7->content = (byte*)data; /* not used for ex */
  37204. pkcs7->contentSz = (word32)sizeof(data);
  37205. pkcs7->contentOID = SIGNED_DATA;
  37206. pkcs7->privateKey = key;
  37207. pkcs7->privateKeySz = (word32)sizeof(key);
  37208. pkcs7->encryptOID = RSAk;
  37209. pkcs7->hashOID = SHAh;
  37210. pkcs7->signedAttribs = NULL;
  37211. pkcs7->signedAttribsSz = 0;
  37212. #ifndef NO_BIO
  37213. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37214. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  37215. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  37216. /* Read PKCS#7 PEM from BIO */
  37217. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  37218. AssertIntGE(ret, 0);
  37219. BIO_free(bio);
  37220. #endif
  37221. wc_PKCS7_Free(pkcs7);
  37222. wc_FreeRng(&rng);
  37223. printf(resultFmt, passed);
  37224. #endif
  37225. }
  37226. #ifdef HAVE_SMIME
  37227. static void test_wolfSSL_SMIME_read_PKCS7(void)
  37228. {
  37229. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  37230. !defined(NO_RSA)
  37231. PKCS7* pkcs7 = NULL;
  37232. BIO* bio = NULL;
  37233. BIO* bcont = NULL;
  37234. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  37235. printf(testingFmt, "wolfSSL_SMIME_read_PKCS7()");
  37236. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  37237. AssertNotNull(bio);
  37238. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  37239. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  37240. AssertNotNull(pkcs7);
  37241. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_SUCCESS);
  37242. XFCLOSE(smimeTestFile);
  37243. if (bcont) BIO_free(bcont);
  37244. wolfSSL_PKCS7_free(pkcs7);
  37245. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  37246. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  37247. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  37248. AssertNotNull(pkcs7);
  37249. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_SUCCESS);
  37250. XFCLOSE(smimeTestFile);
  37251. if (bcont) BIO_free(bcont);
  37252. wolfSSL_PKCS7_free(pkcs7);
  37253. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  37254. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  37255. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  37256. AssertNull(pkcs7);
  37257. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_FAILURE);
  37258. XFCLOSE(smimeTestFile);
  37259. if (bcont) BIO_free(bcont);
  37260. wolfSSL_PKCS7_free(pkcs7);
  37261. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  37262. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  37263. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  37264. AssertNotNull(pkcs7);
  37265. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_SUCCESS);
  37266. BIO_free(bio);
  37267. if (bcont) BIO_free(bcont);
  37268. wolfSSL_PKCS7_free(pkcs7);
  37269. printf(resultFmt, passed);
  37270. #endif
  37271. }
  37272. #endif /* HAVE_SMIME*/
  37273. #endif /* !NO_BIO */
  37274. /*----------------------------------------------------------------------------*
  37275. | Certificate Failure Checks
  37276. *----------------------------------------------------------------------------*/
  37277. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  37278. !defined(WOLFSSL_NO_CLIENT_AUTH))
  37279. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  37280. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  37281. int type)
  37282. {
  37283. int ret;
  37284. WOLFSSL_CERT_MANAGER* cm = NULL;
  37285. switch (type) {
  37286. case TESTING_RSA:
  37287. #ifdef NO_RSA
  37288. printf("RSA disabled, skipping test\n");
  37289. return ASN_SIG_CONFIRM_E;
  37290. #else
  37291. break;
  37292. #endif
  37293. case TESTING_ECC:
  37294. #ifndef HAVE_ECC
  37295. printf("ECC disabled, skipping test\n");
  37296. return ASN_SIG_CONFIRM_E;
  37297. #else
  37298. break;
  37299. #endif
  37300. default:
  37301. printf("Bad function argument\n");
  37302. return BAD_FUNC_ARG;
  37303. }
  37304. cm = wolfSSL_CertManagerNew();
  37305. if (cm == NULL) {
  37306. printf("wolfSSL_CertManagerNew failed\n");
  37307. return -1;
  37308. }
  37309. #ifndef NO_FILESYSTEM
  37310. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  37311. if (ret != WOLFSSL_SUCCESS) {
  37312. printf("wolfSSL_CertManagerLoadCA failed\n");
  37313. wolfSSL_CertManagerFree(cm);
  37314. return ret;
  37315. }
  37316. #else
  37317. (void)ca;
  37318. #endif
  37319. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  37320. /* Let AssertIntEQ handle return code */
  37321. wolfSSL_CertManagerFree(cm);
  37322. return ret;
  37323. }
  37324. static int test_RsaSigFailure_cm(void)
  37325. {
  37326. int ret = 0;
  37327. const char* ca_cert = "./certs/ca-cert.pem";
  37328. const char* server_cert = "./certs/server-cert.der";
  37329. byte* cert_buf = NULL;
  37330. size_t cert_sz = 0;
  37331. ret = load_file(server_cert, &cert_buf, &cert_sz);
  37332. if (ret == 0) {
  37333. /* corrupt DER - invert last byte, which is signature */
  37334. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  37335. /* test bad cert */
  37336. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  37337. }
  37338. printf("Signature failure test: RSA: Ret %d\n", ret);
  37339. if (cert_buf)
  37340. free(cert_buf);
  37341. return ret;
  37342. }
  37343. static int test_EccSigFailure_cm(void)
  37344. {
  37345. int ret = 0;
  37346. /* self-signed ECC cert, so use server cert as CA */
  37347. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  37348. const char* server_cert = "./certs/server-ecc.der";
  37349. byte* cert_buf = NULL;
  37350. size_t cert_sz = 0;
  37351. ret = load_file(server_cert, &cert_buf, &cert_sz);
  37352. if (ret == 0) {
  37353. /* corrupt DER - invert last byte, which is signature */
  37354. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  37355. /* test bad cert */
  37356. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  37357. }
  37358. printf("Signature failure test: ECC: Ret %d\n", ret);
  37359. if (cert_buf)
  37360. free(cert_buf);
  37361. #ifdef FP_ECC
  37362. wc_ecc_fp_free();
  37363. #endif
  37364. return ret;
  37365. }
  37366. #endif /* NO_CERTS */
  37367. #ifdef WOLFSSL_TLS13
  37368. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  37369. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  37370. #endif
  37371. #ifdef WOLFSSL_EARLY_DATA
  37372. static const char earlyData[] = "Early Data";
  37373. static char earlyDataBuffer[1];
  37374. #endif
  37375. static int test_tls13_apis(void)
  37376. {
  37377. int ret = 0;
  37378. #ifndef WOLFSSL_NO_TLS12
  37379. #ifndef NO_WOLFSSL_CLIENT
  37380. WOLFSSL_CTX* clientTls12Ctx;
  37381. WOLFSSL* clientTls12Ssl;
  37382. #endif
  37383. #ifndef NO_WOLFSSL_SERVER
  37384. WOLFSSL_CTX* serverTls12Ctx;
  37385. WOLFSSL* serverTls12Ssl;
  37386. #endif
  37387. #endif
  37388. #ifndef NO_WOLFSSL_CLIENT
  37389. WOLFSSL_CTX* clientCtx;
  37390. WOLFSSL* clientSsl;
  37391. #endif
  37392. #ifndef NO_WOLFSSL_SERVER
  37393. WOLFSSL_CTX* serverCtx;
  37394. WOLFSSL* serverSsl;
  37395. #ifndef NO_CERTS
  37396. const char* ourCert = svrCertFile;
  37397. const char* ourKey = svrKeyFile;
  37398. #endif
  37399. #endif
  37400. int required;
  37401. #ifdef WOLFSSL_EARLY_DATA
  37402. int outSz;
  37403. #endif
  37404. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  37405. int groups[2] = { WOLFSSL_ECC_X25519, WOLFSSL_ECC_X448 };
  37406. int numGroups = 2;
  37407. #endif
  37408. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  37409. char groupList[] =
  37410. #ifndef NO_ECC_SECP
  37411. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  37412. "P-521:"
  37413. #endif
  37414. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  37415. "P-384:"
  37416. #endif
  37417. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  37418. "P-256"
  37419. #endif
  37420. "";
  37421. #endif /* !defined(NO_ECC_SECP) */
  37422. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  37423. #ifndef WOLFSSL_NO_TLS12
  37424. #ifndef NO_WOLFSSL_CLIENT
  37425. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  37426. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  37427. #endif
  37428. #ifndef NO_WOLFSSL_SERVER
  37429. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  37430. #ifndef NO_CERTS
  37431. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  37432. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  37433. #endif
  37434. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  37435. #endif
  37436. #endif
  37437. #ifndef NO_WOLFSSL_CLIENT
  37438. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  37439. clientSsl = wolfSSL_new(clientCtx);
  37440. #endif
  37441. #ifndef NO_WOLFSSL_SERVER
  37442. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  37443. #ifndef NO_CERTS
  37444. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  37445. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  37446. #endif
  37447. serverSsl = wolfSSL_new(serverCtx);
  37448. #endif
  37449. #ifdef WOLFSSL_SEND_HRR_COOKIE
  37450. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  37451. #ifndef NO_WOLFSSL_CLIENT
  37452. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  37453. #endif
  37454. #ifndef NO_WOLFSSL_SERVER
  37455. #ifndef WOLFSSL_NO_TLS12
  37456. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  37457. #endif
  37458. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  37459. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  37460. WOLFSSL_SUCCESS);
  37461. #endif
  37462. #endif
  37463. #ifdef HAVE_SUPPORTED_CURVES
  37464. #ifdef HAVE_ECC
  37465. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  37466. #ifndef NO_WOLFSSL_SERVER
  37467. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1),
  37468. WOLFSSL_SUCCESS);
  37469. #endif
  37470. #ifndef NO_WOLFSSL_CLIENT
  37471. #ifndef WOLFSSL_NO_TLS12
  37472. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  37473. WOLFSSL_SUCCESS);
  37474. #endif
  37475. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  37476. WOLFSSL_SUCCESS);
  37477. #endif
  37478. #elif defined(HAVE_CURVE25519)
  37479. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  37480. #ifndef NO_WOLFSSL_SERVER
  37481. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  37482. WOLFSSL_SUCCESS);
  37483. #endif
  37484. #ifndef NO_WOLFSSL_CLIENT
  37485. #ifndef WOLFSSL_NO_TLS12
  37486. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  37487. WOLFSSL_SUCCESS);
  37488. #endif
  37489. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  37490. WOLFSSL_SUCCESS);
  37491. #endif
  37492. #elif defined(HAVE_CURVE448)
  37493. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  37494. #ifndef NO_WOLFSSL_SERVER
  37495. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  37496. WOLFSSL_SUCCESS);
  37497. #endif
  37498. #ifndef NO_WOLFSSL_CLIENT
  37499. #ifndef WOLFSSL_NO_TLS12
  37500. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  37501. WOLFSSL_SUCCESS);
  37502. #endif
  37503. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  37504. WOLFSSL_SUCCESS);
  37505. #endif
  37506. #else
  37507. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  37508. #ifndef NO_WOLFSSL_CLIENT
  37509. #ifndef WOLFSSL_NO_TLS12
  37510. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  37511. NOT_COMPILED_IN);
  37512. #endif
  37513. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  37514. NOT_COMPILED_IN);
  37515. #endif
  37516. #endif
  37517. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  37518. #ifndef NO_WOLFSSL_SERVER
  37519. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  37520. #endif
  37521. #ifndef NO_WOLFSSL_CLIENT
  37522. #ifndef WOLFSSL_NO_TLS12
  37523. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  37524. #endif
  37525. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  37526. #endif
  37527. #endif /* HAVE_SUPPORTED_CURVES */
  37528. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  37529. #ifndef NO_WOLFSSL_CLIENT
  37530. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  37531. #endif
  37532. #ifndef NO_WOLFSSL_SERVER
  37533. #ifndef WOLFSSL_NO_TLS12
  37534. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  37535. #endif
  37536. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  37537. #endif
  37538. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  37539. #ifndef NO_WOLFSSL_CLIENT
  37540. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  37541. #endif
  37542. #ifndef NO_WOLFSSL_SERVER
  37543. #ifndef WOLFSSL_NO_TLS12
  37544. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  37545. #endif
  37546. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  37547. #endif
  37548. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  37549. #ifndef NO_WOLFSSL_CLIENT
  37550. #ifndef WOLFSSL_NO_TLS12
  37551. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  37552. #endif
  37553. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  37554. #endif
  37555. #ifndef NO_WOLFSSL_SERVER
  37556. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  37557. #endif
  37558. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  37559. #ifndef NO_WOLFSSL_CLIENT
  37560. #ifndef WOLFSSL_NO_TLS12
  37561. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  37562. #endif
  37563. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  37564. #endif
  37565. #ifndef NO_WOLFSSL_SERVER
  37566. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  37567. #endif
  37568. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  37569. #ifndef NO_WOLFSSL_CLIENT
  37570. #ifndef WOLFSSL_NO_TLS12
  37571. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  37572. #endif
  37573. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  37574. #endif
  37575. #ifndef NO_WOLFSSL_SERVER
  37576. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  37577. #endif
  37578. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  37579. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  37580. #ifndef NO_WOLFSSL_CLIENT
  37581. #ifndef WOLFSSL_NO_TLS12
  37582. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  37583. BAD_FUNC_ARG);
  37584. #endif
  37585. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  37586. #endif
  37587. #ifndef NO_WOLFSSL_SERVER
  37588. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  37589. #endif
  37590. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  37591. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  37592. #ifndef NO_WOLFSSL_SERVER
  37593. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  37594. #endif
  37595. #ifndef NO_WOLFSSL_CLIENT
  37596. #ifndef WOLFSSL_NO_TLS12
  37597. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  37598. BAD_FUNC_ARG);
  37599. #endif
  37600. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  37601. #endif
  37602. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  37603. #ifndef NO_WOLFSSL_SERVER
  37604. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  37605. #endif
  37606. #ifndef NO_WOLFSSL_CLIENT
  37607. #ifndef WOLFSSL_NO_TLS12
  37608. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  37609. BAD_FUNC_ARG);
  37610. #endif
  37611. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  37612. #endif
  37613. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  37614. #ifndef NO_WOLFSSL_CLIENT
  37615. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  37616. #endif
  37617. #ifndef NO_WOLFSSL_SERVER
  37618. #ifndef WOLFSSL_NO_TLS12
  37619. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  37620. BAD_FUNC_ARG);
  37621. #endif
  37622. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  37623. #endif
  37624. #endif
  37625. #ifdef HAVE_ECC
  37626. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  37627. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  37628. #ifndef NO_WOLFSSL_SERVER
  37629. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  37630. #endif
  37631. #ifndef NO_WOLFSSL_CLIENT
  37632. #ifndef WOLFSSL_NO_TLS12
  37633. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  37634. #endif
  37635. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  37636. #endif
  37637. #endif
  37638. #ifdef HAVE_SUPPORTED_CURVES
  37639. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  37640. #ifndef NO_WOLFSSL_CLIENT
  37641. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  37642. #endif
  37643. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  37644. #ifndef NO_WOLFSSL_CLIENT
  37645. #ifndef WOLFSSL_NO_TLS12
  37646. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  37647. BAD_FUNC_ARG);
  37648. #endif
  37649. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  37650. WOLFSSL_MAX_GROUP_COUNT + 1),
  37651. BAD_FUNC_ARG);
  37652. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  37653. WOLFSSL_SUCCESS);
  37654. #endif
  37655. #ifndef NO_WOLFSSL_SERVER
  37656. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  37657. WOLFSSL_SUCCESS);
  37658. #endif
  37659. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  37660. #ifndef NO_WOLFSSL_CLIENT
  37661. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  37662. #endif
  37663. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  37664. #ifndef NO_WOLFSSL_CLIENT
  37665. #ifndef WOLFSSL_NO_TLS12
  37666. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  37667. BAD_FUNC_ARG);
  37668. #endif
  37669. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  37670. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  37671. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  37672. WOLFSSL_SUCCESS);
  37673. #endif
  37674. #ifndef NO_WOLFSSL_SERVER
  37675. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  37676. WOLFSSL_SUCCESS);
  37677. #endif
  37678. #ifdef OPENSSL_EXTRA
  37679. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  37680. #ifndef NO_WOLFSSL_CLIENT
  37681. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  37682. #endif
  37683. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  37684. #ifndef NO_WOLFSSL_CLIENT
  37685. #ifndef WOLFSSL_NO_TLS12
  37686. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  37687. WOLFSSL_FAILURE);
  37688. #endif
  37689. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  37690. WOLFSSL_SUCCESS);
  37691. #endif
  37692. #ifndef NO_WOLFSSL_SERVER
  37693. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  37694. WOLFSSL_SUCCESS);
  37695. #endif
  37696. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  37697. #ifndef NO_WOLFSSL_CLIENT
  37698. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  37699. #endif
  37700. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  37701. #ifndef NO_WOLFSSL_CLIENT
  37702. #ifndef WOLFSSL_NO_TLS12
  37703. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  37704. WOLFSSL_FAILURE);
  37705. #endif
  37706. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  37707. WOLFSSL_SUCCESS);
  37708. #endif
  37709. #ifndef NO_WOLFSSL_SERVER
  37710. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  37711. WOLFSSL_SUCCESS);
  37712. #endif
  37713. #endif /* OPENSSL_EXTRA */
  37714. #endif /* HAVE_SUPPORTED_CURVES */
  37715. #endif /* HAVE_ECC */
  37716. #ifdef WOLFSSL_EARLY_DATA
  37717. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  37718. #ifndef NO_WOLFSSL_CLIENT
  37719. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  37720. #endif
  37721. #ifndef NO_WOLFSSL_SERVER
  37722. #ifndef WOLFSSL_NO_TLS12
  37723. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  37724. BAD_FUNC_ARG);
  37725. #endif
  37726. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 0), 0);
  37727. #endif
  37728. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  37729. #ifndef NO_WOLFSSL_CLIENT
  37730. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  37731. #endif
  37732. #ifndef NO_WOLFSSL_SERVER
  37733. #ifndef WOLFSSL_NO_TLS12
  37734. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  37735. #endif
  37736. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 0), 0);
  37737. #endif
  37738. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  37739. &outSz), BAD_FUNC_ARG);
  37740. #ifndef NO_WOLFSSL_CLIENT
  37741. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  37742. &outSz), BAD_FUNC_ARG);
  37743. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  37744. BAD_FUNC_ARG);
  37745. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  37746. sizeof(earlyData), NULL),
  37747. BAD_FUNC_ARG);
  37748. #endif
  37749. #ifndef NO_WOLFSSL_SERVER
  37750. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  37751. sizeof(earlyData), &outSz),
  37752. SIDE_ERROR);
  37753. #endif
  37754. #ifndef NO_WOLFSSL_CLIENT
  37755. #ifndef WOLFSSL_NO_TLS12
  37756. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  37757. sizeof(earlyData), &outSz),
  37758. BAD_FUNC_ARG);
  37759. #endif
  37760. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  37761. sizeof(earlyData), &outSz),
  37762. WOLFSSL_FATAL_ERROR);
  37763. #endif
  37764. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  37765. sizeof(earlyDataBuffer), &outSz),
  37766. BAD_FUNC_ARG);
  37767. #ifndef NO_WOLFSSL_SERVER
  37768. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  37769. sizeof(earlyDataBuffer), &outSz),
  37770. BAD_FUNC_ARG);
  37771. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  37772. BAD_FUNC_ARG);
  37773. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  37774. sizeof(earlyDataBuffer), NULL),
  37775. BAD_FUNC_ARG);
  37776. #endif
  37777. #ifndef NO_WOLFSSL_CLIENT
  37778. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  37779. sizeof(earlyDataBuffer), &outSz),
  37780. SIDE_ERROR);
  37781. #endif
  37782. #ifndef NO_WOLFSSL_SERVER
  37783. #ifndef WOLFSSL_NO_TLS12
  37784. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  37785. sizeof(earlyDataBuffer), &outSz),
  37786. BAD_FUNC_ARG);
  37787. #endif
  37788. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  37789. sizeof(earlyDataBuffer), &outSz),
  37790. WOLFSSL_FATAL_ERROR);
  37791. #endif
  37792. #endif
  37793. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  37794. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  37795. #endif
  37796. #ifndef NO_WOLFSSL_SERVER
  37797. wolfSSL_free(serverSsl);
  37798. wolfSSL_CTX_free(serverCtx);
  37799. #endif
  37800. #ifndef NO_WOLFSSL_CLIENT
  37801. wolfSSL_free(clientSsl);
  37802. wolfSSL_CTX_free(clientCtx);
  37803. #endif
  37804. #ifndef WOLFSSL_NO_TLS12
  37805. #ifndef NO_WOLFSSL_SERVER
  37806. wolfSSL_free(serverTls12Ssl);
  37807. wolfSSL_CTX_free(serverTls12Ctx);
  37808. #endif
  37809. #ifndef NO_WOLFSSL_CLIENT
  37810. wolfSSL_free(clientTls12Ssl);
  37811. wolfSSL_CTX_free(clientTls12Ctx);
  37812. #endif
  37813. #endif
  37814. return ret;
  37815. }
  37816. #endif
  37817. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  37818. !defined(NO_OLD_TLS))
  37819. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  37820. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  37821. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  37822. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  37823. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  37824. const unsigned char* priv, unsigned int privSz,
  37825. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  37826. unsigned char* out, unsigned int* outlen,
  37827. void* ctx)
  37828. {
  37829. /* Test fail when context associated with WOLFSSL is NULL */
  37830. if (ctx == NULL) {
  37831. return -1;
  37832. }
  37833. (void)ssl;
  37834. /* return 0 on success */
  37835. return wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  37836. }
  37837. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  37838. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  37839. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  37840. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  37841. WOLFSSL_SUCCESS);
  37842. #endif
  37843. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  37844. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  37845. WOLFSSL_SUCCESS);
  37846. #endif
  37847. }
  37848. static void test_dh_ssl_setup(WOLFSSL* ssl)
  37849. {
  37850. static int dh_test_ctx = 1;
  37851. int ret;
  37852. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  37853. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  37854. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  37855. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  37856. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  37857. }
  37858. }
  37859. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  37860. {
  37861. int ret;
  37862. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  37863. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  37864. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  37865. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  37866. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  37867. }
  37868. }
  37869. #endif
  37870. static void test_DhCallbacks(void)
  37871. {
  37872. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  37873. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  37874. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  37875. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  37876. WOLFSSL_CTX *ctx;
  37877. WOLFSSL *ssl;
  37878. tcp_ready ready;
  37879. func_args server_args;
  37880. func_args client_args;
  37881. THREAD_TYPE serverThread;
  37882. callback_functions func_cb_client;
  37883. callback_functions func_cb_server;
  37884. int test;
  37885. printf(testingFmt, "test_DhCallbacks");
  37886. #ifndef NO_WOLFSSL_CLIENT
  37887. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  37888. #else
  37889. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  37890. #endif
  37891. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  37892. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  37893. /* load client ca cert */
  37894. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  37895. WOLFSSL_SUCCESS);
  37896. /* test with NULL arguments */
  37897. wolfSSL_SetDhAgreeCtx(NULL, &test);
  37898. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  37899. /* test success case */
  37900. test = 1;
  37901. AssertNotNull(ssl = wolfSSL_new(ctx));
  37902. wolfSSL_SetDhAgreeCtx(ssl, &test);
  37903. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  37904. wolfSSL_free(ssl);
  37905. wolfSSL_CTX_free(ctx);
  37906. /* test a connection where callback is used */
  37907. #ifdef WOLFSSL_TIRTOS
  37908. fdOpenSession(Task_self());
  37909. #endif
  37910. XMEMSET(&server_args, 0, sizeof(func_args));
  37911. XMEMSET(&client_args, 0, sizeof(func_args));
  37912. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  37913. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  37914. StartTCP();
  37915. InitTcpReady(&ready);
  37916. #if defined(USE_WINDOWS_API)
  37917. /* use RNG to get random port if using windows */
  37918. ready.port = GetRandomPort();
  37919. #endif
  37920. server_args.signal = &ready;
  37921. client_args.signal = &ready;
  37922. server_args.return_code = TEST_FAIL;
  37923. client_args.return_code = TEST_FAIL;
  37924. /* set callbacks to use DH functions */
  37925. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  37926. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  37927. #ifndef WOLFSSL_NO_TLS12
  37928. func_cb_client.method = wolfTLSv1_2_client_method;
  37929. #else
  37930. func_cb_client.method = wolfTLSv1_3_client_method;
  37931. #endif
  37932. client_args.callbacks = &func_cb_client;
  37933. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  37934. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  37935. #ifndef WOLFSSL_NO_TLS12
  37936. func_cb_server.method = wolfTLSv1_2_server_method;
  37937. #else
  37938. func_cb_server.method = wolfTLSv1_3_server_method;
  37939. #endif
  37940. server_args.callbacks = &func_cb_server;
  37941. start_thread(test_server_nofail, &server_args, &serverThread);
  37942. wait_tcp_ready(&server_args);
  37943. test_client_nofail(&client_args, NULL);
  37944. join_thread(serverThread);
  37945. AssertTrue(client_args.return_code);
  37946. AssertTrue(server_args.return_code);
  37947. FreeTcpReady(&ready);
  37948. #ifdef WOLFSSL_TIRTOS
  37949. fdOpenSession(Task_self());
  37950. #endif
  37951. /* now set user ctx to not be 1 so that the callback returns fail case */
  37952. #ifdef WOLFSSL_TIRTOS
  37953. fdOpenSession(Task_self());
  37954. #endif
  37955. XMEMSET(&server_args, 0, sizeof(func_args));
  37956. XMEMSET(&client_args, 0, sizeof(func_args));
  37957. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  37958. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  37959. StartTCP();
  37960. InitTcpReady(&ready);
  37961. #if defined(USE_WINDOWS_API)
  37962. /* use RNG to get random port if using windows */
  37963. ready.port = GetRandomPort();
  37964. #endif
  37965. server_args.signal = &ready;
  37966. client_args.signal = &ready;
  37967. server_args.return_code = TEST_FAIL;
  37968. client_args.return_code = TEST_FAIL;
  37969. /* set callbacks to use DH functions */
  37970. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  37971. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  37972. #ifndef WOLFSSL_NO_TLS12
  37973. func_cb_client.method = wolfTLSv1_2_client_method;
  37974. #else
  37975. func_cb_client.method = wolfTLSv1_3_client_method;
  37976. #endif
  37977. client_args.callbacks = &func_cb_client;
  37978. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  37979. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  37980. #ifndef WOLFSSL_NO_TLS12
  37981. func_cb_server.method = wolfTLSv1_2_server_method;
  37982. #else
  37983. func_cb_server.method = wolfTLSv1_3_server_method;
  37984. #endif
  37985. server_args.callbacks = &func_cb_server;
  37986. start_thread(test_server_nofail, &server_args, &serverThread);
  37987. wait_tcp_ready(&server_args);
  37988. test_client_nofail(&client_args, NULL);
  37989. join_thread(serverThread);
  37990. AssertIntEQ(client_args.return_code, TEST_FAIL);
  37991. AssertIntEQ(server_args.return_code, TEST_FAIL);
  37992. FreeTcpReady(&ready);
  37993. #ifdef WOLFSSL_TIRTOS
  37994. fdOpenSession(Task_self());
  37995. #endif
  37996. printf(resultFmt, passed);
  37997. #endif
  37998. }
  37999. #endif /* HAVE_PK_CALLBACKS */
  38000. #ifdef HAVE_HASHDRBG
  38001. #ifdef TEST_RESEED_INTERVAL
  38002. static int test_wc_RNG_GenerateBlock_Reseed(void)
  38003. {
  38004. int i, ret;
  38005. WC_RNG rng;
  38006. byte key[32];
  38007. ret = wc_InitRng(&rng);
  38008. if (ret == 0) {
  38009. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  38010. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  38011. if (ret != 0) {
  38012. break;
  38013. }
  38014. }
  38015. }
  38016. wc_FreeRng(&rng);
  38017. return ret;
  38018. }
  38019. #endif /* TEST_RESEED_INTERVAL */
  38020. static int test_wc_RNG_GenerateBlock(void)
  38021. {
  38022. int i, ret;
  38023. WC_RNG rng;
  38024. byte key[32];
  38025. ret = wc_InitRng(&rng);
  38026. if (ret == 0) {
  38027. for(i = 0; i < 10; i++) {
  38028. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  38029. if (ret != 0) {
  38030. break;
  38031. }
  38032. }
  38033. }
  38034. wc_FreeRng(&rng);
  38035. (void)rng; /* for WC_NO_RNG case */
  38036. (void)key;
  38037. return ret;
  38038. }
  38039. #endif
  38040. /*
  38041. * Testing get_rand_digit
  38042. */
  38043. static int test_get_rand_digit (void)
  38044. {
  38045. int ret = 0;
  38046. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  38047. WC_RNG rng;
  38048. mp_digit d;
  38049. printf(testingFmt, "get_rand_digit()");
  38050. ret = wc_InitRng(&rng);
  38051. if (ret == 0) {
  38052. ret = get_rand_digit(&rng, &d);
  38053. }
  38054. if (ret == 0) {
  38055. ret = get_rand_digit(NULL, NULL);
  38056. if (ret == BAD_FUNC_ARG) {
  38057. ret = 0;
  38058. }
  38059. }
  38060. if (ret == 0) {
  38061. ret = get_rand_digit(NULL, &d);
  38062. if (ret == BAD_FUNC_ARG) {
  38063. ret = 0;
  38064. }
  38065. }
  38066. if (ret == 0) {
  38067. ret = get_rand_digit(&rng, NULL);
  38068. if (ret == BAD_FUNC_ARG) {
  38069. ret = 0;
  38070. }
  38071. }
  38072. if (ret == 0) {
  38073. ret = wc_FreeRng(&rng);
  38074. }
  38075. printf(resultFmt, ret == 0 ? passed : failed);
  38076. #endif
  38077. return ret;
  38078. }/* End test_get_rand_digit*/
  38079. /*
  38080. * Testing get_digit_count
  38081. */
  38082. static int test_get_digit_count (void)
  38083. {
  38084. int ret = 0;
  38085. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  38086. mp_int a;
  38087. printf(testingFmt, "get_digit_count()");
  38088. if (mp_init(&a) != MP_OKAY) {
  38089. ret = -1;
  38090. }
  38091. if (ret == 0) {
  38092. ret = get_digit_count(NULL);
  38093. }
  38094. if (ret == 0) {
  38095. ret = get_digit_count(&a);
  38096. }
  38097. printf(resultFmt, ret == 0 ? passed : failed);
  38098. mp_clear(&a);
  38099. #endif
  38100. return ret;
  38101. }/* End test_get_digit_count*/
  38102. /*
  38103. * Testing mp_cond_copy
  38104. */
  38105. static int test_mp_cond_copy (void)
  38106. {
  38107. int ret = 0;
  38108. #if defined(WOLFSSL_PUBLIC_MP)
  38109. mp_int a;
  38110. mp_int b;
  38111. int copy = 0;
  38112. printf(testingFmt, "mp_cond_copy()");
  38113. if (mp_init(&a) != MP_OKAY) {
  38114. ret = -1;
  38115. }
  38116. if (ret == 0) {
  38117. if (mp_init(&b) != MP_OKAY) {
  38118. ret = -1;
  38119. }
  38120. }
  38121. if (ret == 0) {
  38122. ret = mp_cond_copy(NULL, copy, NULL);
  38123. if (ret == BAD_FUNC_ARG) {
  38124. ret = 0;
  38125. }
  38126. }
  38127. if (ret == 0) {
  38128. ret = mp_cond_copy(NULL, copy, &b);
  38129. if (ret == BAD_FUNC_ARG) {
  38130. ret = 0;
  38131. }
  38132. }
  38133. if (ret == 0) {
  38134. ret = mp_cond_copy(&a, copy, NULL);
  38135. if (ret == BAD_FUNC_ARG) {
  38136. ret = 0;
  38137. }
  38138. }
  38139. if (ret == 0) {
  38140. ret = mp_cond_copy(&a, copy, &b);
  38141. }
  38142. printf(resultFmt, ret == 0 ? passed : failed);
  38143. mp_clear(&a);
  38144. mp_clear(&b);
  38145. #endif
  38146. return ret;
  38147. }/* End test_mp_cond_copy*/
  38148. /*
  38149. * Testing mp_rand
  38150. */
  38151. static int test_mp_rand (void)
  38152. {
  38153. int ret = 0;
  38154. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  38155. mp_int a;
  38156. int digits = 1;
  38157. WC_RNG rng;
  38158. printf(testingFmt, "mp_rand()");
  38159. if (mp_init(&a) != MP_OKAY) {
  38160. ret = -1;
  38161. }
  38162. if (ret == 0) {
  38163. ret = wc_InitRng(&rng);
  38164. }
  38165. if (ret == 0) {
  38166. ret = mp_rand(&a, digits, NULL);
  38167. if (ret == MISSING_RNG_E) {
  38168. ret = 0;
  38169. }
  38170. }
  38171. if (ret == 0) {
  38172. ret = mp_rand(NULL, digits, &rng);
  38173. if (ret == BAD_FUNC_ARG) {
  38174. ret = 0;
  38175. }
  38176. }
  38177. if (ret == 0) {
  38178. ret = mp_rand(&a, 0, &rng);
  38179. if (ret == BAD_FUNC_ARG) {
  38180. ret = 0;
  38181. }
  38182. }
  38183. if (ret == 0) {
  38184. ret = mp_rand(&a, digits, &rng);
  38185. }
  38186. printf(resultFmt, ret == 0 ? passed : failed);
  38187. mp_clear(&a);
  38188. wc_FreeRng(&rng);
  38189. #endif
  38190. return ret;
  38191. }/* End test_mp_rand*/
  38192. /*
  38193. * Testing get_digit
  38194. */
  38195. static int test_get_digit (void)
  38196. {
  38197. int ret = 0;
  38198. #if defined(WOLFSSL_PUBLIC_MP)
  38199. mp_int a;
  38200. int n = 0;
  38201. printf(testingFmt, "get_digit()");
  38202. if (mp_init(&a) != MP_OKAY) {
  38203. ret = -1;
  38204. }
  38205. if (ret == 0) {
  38206. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  38207. ret = -1;
  38208. }
  38209. }
  38210. if (ret == 0) {
  38211. if (get_digit(NULL, n) == 0) { /* Should hit this */
  38212. ret = 0;
  38213. }
  38214. }
  38215. if (ret == 0) {
  38216. if (get_digit(&a, n) != 0) { /* Should not hit this */
  38217. ret = -1;
  38218. }
  38219. }
  38220. if (ret == 0) {
  38221. if (get_digit(&a, n) == 0) { /* Should hit this */
  38222. ret = 0;
  38223. }
  38224. }
  38225. printf(resultFmt, ret == 0 ? passed : failed);
  38226. mp_clear(&a);
  38227. #endif
  38228. return ret;
  38229. }/* End test_get_digit*/
  38230. /*
  38231. * Testing wc_export_int
  38232. */
  38233. static int test_wc_export_int(void)
  38234. {
  38235. int ret = 0;
  38236. #if defined(WOLFSSL_PUBLIC_MP)
  38237. mp_int mp;
  38238. byte buf[32];
  38239. word32 keySz = (word32)sizeof(buf);
  38240. word32 len = (word32)sizeof(buf);
  38241. printf(testingFmt, "wc_export_int()");
  38242. if (mp_init(&mp) != MP_OKAY) {
  38243. ret = -1;
  38244. }
  38245. if (ret == 0) {
  38246. ret = mp_set_int(&mp, 1234);
  38247. }
  38248. if (ret == 0) {
  38249. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  38250. if (ret == BAD_FUNC_ARG) {
  38251. ret = 0;
  38252. }
  38253. }
  38254. if (ret == 0) {
  38255. len = sizeof(buf)-1;
  38256. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  38257. if (ret == BUFFER_E) {
  38258. ret = 0;
  38259. }
  38260. }
  38261. if (ret == 0) {
  38262. len = sizeof(buf);
  38263. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  38264. }
  38265. if (ret == 0) {
  38266. len = 4; /* test input too small */
  38267. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  38268. if (ret == BUFFER_E) {
  38269. ret = 0;
  38270. }
  38271. }
  38272. if (ret == 0) {
  38273. len = sizeof(buf);
  38274. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  38275. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  38276. if (ret == 0 && len != 5) {
  38277. ret = BAD_FUNC_ARG;
  38278. }
  38279. }
  38280. printf(resultFmt, ret == 0 ? passed : failed);
  38281. mp_clear(&mp);
  38282. #endif
  38283. return ret;
  38284. }/* End test_wc_export_int*/
  38285. static int test_wc_InitRngNonce(void)
  38286. {
  38287. int ret=0;
  38288. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  38289. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  38290. WC_RNG rng;
  38291. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  38292. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  38293. word32 nonceSz = sizeof(nonce);
  38294. printf(testingFmt, "wc_InitRngNonce()");
  38295. if (ret == 0){
  38296. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  38297. }
  38298. wc_FreeRng(&rng);
  38299. printf(resultFmt, ret == 0 ? passed : failed);
  38300. #endif
  38301. return ret;
  38302. }/* End test_wc_InitRngNonce*/
  38303. /*
  38304. * Testing wc_InitRngNonce_ex
  38305. */
  38306. static int test_wc_InitRngNonce_ex(void)
  38307. {
  38308. int ret=0;
  38309. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  38310. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  38311. WC_RNG rng;
  38312. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  38313. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  38314. word32 nonceSz = sizeof(nonce);
  38315. printf(testingFmt, "wc_InitRngNonce_ex()");
  38316. if (ret == 0){
  38317. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, devId);
  38318. }
  38319. wc_FreeRng(&rng);
  38320. printf(resultFmt, ret == 0 ? passed : failed);
  38321. #endif
  38322. return ret;
  38323. }/*End test_wc_InitRngNonce_ex*/
  38324. static void test_wolfSSL_X509_CRL(void)
  38325. {
  38326. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  38327. X509_CRL *crl;
  38328. char pem[][100] = {
  38329. "./certs/crl/crl.pem",
  38330. "./certs/crl/crl2.pem",
  38331. "./certs/crl/caEccCrl.pem",
  38332. "./certs/crl/eccCliCRL.pem",
  38333. "./certs/crl/eccSrvCRL.pem",
  38334. ""
  38335. };
  38336. #ifndef NO_BIO
  38337. BIO *bio;
  38338. #endif
  38339. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  38340. char der[][100] = {
  38341. "./certs/crl/crl.der",
  38342. "./certs/crl/crl2.der",
  38343. ""};
  38344. #endif
  38345. XFILE fp;
  38346. int i;
  38347. printf(testingFmt, "test_wolfSSL_X509_CRL");
  38348. for (i = 0; pem[i][0] != '\0'; i++)
  38349. {
  38350. fp = XFOPEN(pem[i], "rb");
  38351. AssertTrue((fp != XBADFILE));
  38352. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  38353. AssertNotNull(crl);
  38354. X509_CRL_free(crl);
  38355. XFCLOSE(fp);
  38356. fp = XFOPEN(pem[i], "rb");
  38357. AssertTrue((fp != XBADFILE));
  38358. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  38359. AssertNotNull(crl);
  38360. X509_CRL_free(crl);
  38361. XFCLOSE(fp);
  38362. }
  38363. #ifndef NO_BIO
  38364. for (i = 0; pem[i][0] != '\0'; i++)
  38365. {
  38366. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  38367. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  38368. X509_CRL_free(crl);
  38369. BIO_free(bio);
  38370. }
  38371. #endif
  38372. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  38373. for(i = 0; der[i][0] != '\0'; i++){
  38374. fp = XFOPEN(der[i], "rb");
  38375. AssertTrue((fp != XBADFILE));
  38376. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  38377. AssertNotNull(crl);
  38378. X509_CRL_free(crl);
  38379. XFCLOSE(fp);
  38380. fp = XFOPEN(der[i], "rb");
  38381. AssertTrue((fp != XBADFILE));
  38382. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  38383. AssertNotNull(crl);
  38384. X509_CRL_free(crl);
  38385. XFCLOSE(fp);
  38386. }
  38387. #endif
  38388. printf(resultFmt, passed);
  38389. #endif
  38390. return;
  38391. }
  38392. static void test_wolfSSL_X509_load_crl_file(void)
  38393. {
  38394. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  38395. !defined(NO_RSA)
  38396. int i;
  38397. char pem[][100] = {
  38398. "./certs/crl/crl.pem",
  38399. "./certs/crl/crl2.pem",
  38400. "./certs/crl/caEccCrl.pem",
  38401. "./certs/crl/eccCliCRL.pem",
  38402. "./certs/crl/eccSrvCRL.pem",
  38403. ""
  38404. };
  38405. char der[][100] = {
  38406. "./certs/crl/crl.der",
  38407. "./certs/crl/crl2.der",
  38408. ""
  38409. };
  38410. WOLFSSL_X509_STORE* store;
  38411. WOLFSSL_X509_LOOKUP* lookup;
  38412. printf(testingFmt, "wolfSSL_X509_load_crl_file");
  38413. AssertNotNull(store = wolfSSL_X509_STORE_new());
  38414. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  38415. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  38416. X509_FILETYPE_PEM), 1);
  38417. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  38418. X509_FILETYPE_PEM), 1);
  38419. if (store) {
  38420. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  38421. WOLFSSL_FILETYPE_PEM), 1);
  38422. /* since store hasn't yet known the revoked cert*/
  38423. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  38424. WOLFSSL_FILETYPE_PEM), 1);
  38425. }
  38426. for (i = 0; pem[i][0] != '\0'; i++)
  38427. {
  38428. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  38429. }
  38430. if (store) {
  38431. /* since store knows crl list */
  38432. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  38433. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  38434. }
  38435. /* once feeing store */
  38436. X509_STORE_free(store);
  38437. store = NULL;
  38438. AssertNotNull(store = wolfSSL_X509_STORE_new());
  38439. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  38440. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  38441. X509_FILETYPE_PEM), 1);
  38442. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  38443. X509_FILETYPE_PEM), 1);
  38444. if (store) {
  38445. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  38446. WOLFSSL_FILETYPE_PEM), 1);
  38447. /* since store hasn't yet known the revoked cert*/
  38448. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  38449. WOLFSSL_FILETYPE_PEM), 1);
  38450. }
  38451. for (i = 0; der[i][0] != '\0'; i++)
  38452. {
  38453. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  38454. }
  38455. if (store) {
  38456. /* since store knows crl list */
  38457. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  38458. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  38459. }
  38460. /* test for incorrect parameter */
  38461. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  38462. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  38463. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  38464. X509_STORE_free(store);
  38465. store = NULL;
  38466. printf(resultFmt, passed);
  38467. #endif
  38468. }
  38469. static void test_wolfSSL_d2i_X509_REQ(void)
  38470. {
  38471. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && \
  38472. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA))
  38473. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  38474. * generated by libest
  38475. * ./certs/csr.attr.der contains sample attributes
  38476. * ./certs/csr.ext.der contains sample extensions */
  38477. const char* csrFile = "./certs/csr.signed.der";
  38478. const char* csrPopFile = "./certs/csr.attr.der";
  38479. const char* csrExtFile = "./certs/csr.ext.der";
  38480. /* ./certs/csr.dsa.pem is generated using
  38481. * openssl req -newkey dsa:certs/dsaparams.pem \
  38482. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  38483. * -outform PEM
  38484. * with the passphrase "wolfSSL"
  38485. */
  38486. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  38487. const char* csrDsaFile = "./certs/csr.dsa.pem";
  38488. #endif
  38489. BIO* bio = NULL;
  38490. X509* req = NULL;
  38491. EVP_PKEY *pub_key = NULL;
  38492. {
  38493. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  38494. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  38495. /*
  38496. * Extract the public key from the CSR
  38497. */
  38498. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  38499. /*
  38500. * Verify the signature in the CSR
  38501. */
  38502. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  38503. X509_free(req);
  38504. BIO_free(bio);
  38505. EVP_PKEY_free(pub_key);
  38506. }
  38507. {
  38508. #ifdef OPENSSL_ALL
  38509. X509_ATTRIBUTE* attr;
  38510. ASN1_TYPE *at;
  38511. #endif
  38512. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  38513. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  38514. /*
  38515. * Extract the public key from the CSR
  38516. */
  38517. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  38518. /*
  38519. * Verify the signature in the CSR
  38520. */
  38521. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  38522. #ifdef OPENSSL_ALL
  38523. /*
  38524. * Obtain the challenge password from the CSR
  38525. */
  38526. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  38527. NID_pkcs9_challengePassword);
  38528. AssertNotNull(attr = X509_REQ_get_attr(req, NID_pkcs9_challengePassword));
  38529. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  38530. AssertNotNull(at->value.asn1_string);
  38531. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  38532. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  38533. #endif
  38534. X509_free(req);
  38535. BIO_free(bio);
  38536. EVP_PKEY_free(pub_key);
  38537. }
  38538. {
  38539. #ifdef OPENSSL_ALL
  38540. X509_ATTRIBUTE* attr;
  38541. ASN1_TYPE *at;
  38542. STACK_OF(X509_EXTENSION) *exts = NULL;
  38543. #endif
  38544. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  38545. /* This CSR contains an Extension Request attribute so
  38546. * we test extension parsing in a CSR attribute here. */
  38547. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  38548. /*
  38549. * Extract the public key from the CSR
  38550. */
  38551. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  38552. /*
  38553. * Verify the signature in the CSR
  38554. */
  38555. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  38556. #ifdef OPENSSL_ALL
  38557. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  38558. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  38559. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  38560. /*
  38561. * Obtain the challenge password from the CSR
  38562. */
  38563. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  38564. NID_pkcs9_challengePassword);
  38565. AssertNotNull(attr = X509_REQ_get_attr(req, NID_pkcs9_challengePassword));
  38566. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  38567. AssertNotNull(at->value.asn1_string);
  38568. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  38569. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  38570. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  38571. #endif
  38572. X509_free(req);
  38573. BIO_free(bio);
  38574. EVP_PKEY_free(pub_key);
  38575. }
  38576. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  38577. {
  38578. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  38579. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  38580. /*
  38581. * Extract the public key from the CSR
  38582. */
  38583. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  38584. /*
  38585. * Verify the signature in the CSR
  38586. */
  38587. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  38588. X509_free(req);
  38589. BIO_free(bio);
  38590. EVP_PKEY_free(pub_key);
  38591. }
  38592. #endif /* !NO_DSA && !HAVE_SELFTEST */
  38593. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  38594. }
  38595. static void test_wolfSSL_PEM_read_X509(void)
  38596. {
  38597. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  38598. !defined(NO_RSA)
  38599. X509 *x509 = NULL;
  38600. XFILE fp;
  38601. printf(testingFmt, "wolfSSL_PEM_read_X509");
  38602. fp = XFOPEN(svrCertFile, "rb");
  38603. AssertTrue((fp != XBADFILE));
  38604. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  38605. X509_free(x509);
  38606. XFCLOSE(fp);
  38607. printf(resultFmt, passed);
  38608. #endif
  38609. }
  38610. static void test_wolfSSL_PEM_read(void)
  38611. {
  38612. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  38613. const char* filename = "./certs/server-keyEnc.pem";
  38614. XFILE fp;
  38615. char* name = NULL;
  38616. char* header = NULL;
  38617. byte* data = NULL;
  38618. long len;
  38619. EVP_CIPHER_INFO cipher;
  38620. WOLFSSL_BIO* bio;
  38621. byte* fileData;
  38622. size_t fileDataSz;
  38623. byte* out;
  38624. printf(testingFmt, "wolfSSL_PEM_read");
  38625. fp = XFOPEN(filename, "rb");
  38626. AssertTrue((fp != XBADFILE));
  38627. /* Fail cases. */
  38628. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  38629. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  38630. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  38631. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  38632. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  38633. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  38634. AssertIntGT(XSTRLEN(header), 0);
  38635. AssertIntGT(len, 0);
  38636. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  38637. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  38638. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  38639. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  38640. DYNAMIC_TYPE_TMP_BUFFER));
  38641. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  38642. XFCLOSE(fp);
  38643. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  38644. /* Fail cases. */
  38645. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  38646. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  38647. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  38648. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  38649. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  38650. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  38651. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  38652. /* Fail cases. */
  38653. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  38654. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  38655. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  38656. #ifndef NO_DES3
  38657. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  38658. #endif
  38659. /* Fail cases. */
  38660. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  38661. (void*)"yassl123"), WOLFSSL_FAILURE);
  38662. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  38663. (void*)"yassl123"), WOLFSSL_FAILURE);
  38664. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  38665. (void*)"yassl123"), WOLFSSL_FAILURE);
  38666. #if !defined(NO_DES3) && !defined(NO_MD5)
  38667. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  38668. (void*)"yassl123"), WOLFSSL_SUCCESS);
  38669. #endif
  38670. BIO_free(bio);
  38671. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38672. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38673. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38674. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38675. name = NULL;
  38676. header = NULL;
  38677. data = NULL;
  38678. fp = XFOPEN(svrKeyFile, "rb");
  38679. AssertTrue((fp != XBADFILE));
  38680. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  38681. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  38682. AssertIntEQ(XSTRLEN(header), 0);
  38683. AssertIntGT(len, 0);
  38684. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  38685. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  38686. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  38687. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  38688. DYNAMIC_TYPE_TMP_BUFFER));
  38689. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  38690. XFCLOSE(fp);
  38691. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  38692. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  38693. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  38694. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  38695. BIO_free(bio);
  38696. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38697. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38698. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38699. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38700. printf(resultFmt, passed);
  38701. #endif
  38702. }
  38703. static void test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  38704. {
  38705. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  38706. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  38707. const byte iv[12] = { 0 };
  38708. const byte key[16] = { 0 };
  38709. const byte cleartext[16] = { 0 };
  38710. const byte aad[] = {
  38711. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  38712. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  38713. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  38714. 0x4f
  38715. };
  38716. byte out1Part[16];
  38717. byte outTag1Part[16];
  38718. byte out2Part[16];
  38719. byte outTag2Part[16];
  38720. byte decryptBuf[16];
  38721. int len;
  38722. int tlen;
  38723. EVP_CIPHER_CTX* ctx = NULL;
  38724. printf(testingFmt, "wolfssl_EVP_aes_gcm_AAD_2_parts");
  38725. /* ENCRYPT */
  38726. /* Send AAD and data in 1 part */
  38727. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  38728. tlen = 0;
  38729. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  38730. 1);
  38731. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  38732. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  38733. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  38734. sizeof(cleartext)), 1);
  38735. tlen += len;
  38736. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  38737. tlen += len;
  38738. AssertIntEQ(tlen, sizeof(cleartext));
  38739. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  38740. outTag1Part), 1);
  38741. EVP_CIPHER_CTX_free(ctx);
  38742. /* DECRYPT */
  38743. /* Send AAD and data in 1 part */
  38744. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  38745. tlen = 0;
  38746. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  38747. 1);
  38748. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  38749. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  38750. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  38751. sizeof(cleartext)), 1);
  38752. tlen += len;
  38753. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  38754. outTag1Part), 1);
  38755. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  38756. tlen += len;
  38757. AssertIntEQ(tlen, sizeof(cleartext));
  38758. EVP_CIPHER_CTX_free(ctx);
  38759. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  38760. /* ENCRYPT */
  38761. /* Send AAD and data in 2 parts */
  38762. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  38763. tlen = 0;
  38764. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  38765. 1);
  38766. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  38767. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  38768. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  38769. 1);
  38770. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  38771. tlen += len;
  38772. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  38773. sizeof(cleartext) - 1), 1);
  38774. tlen += len;
  38775. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  38776. tlen += len;
  38777. AssertIntEQ(tlen, sizeof(cleartext));
  38778. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  38779. outTag2Part), 1);
  38780. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  38781. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  38782. EVP_CIPHER_CTX_free(ctx);
  38783. /* DECRYPT */
  38784. /* Send AAD and data in 2 parts */
  38785. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  38786. tlen = 0;
  38787. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  38788. 1);
  38789. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  38790. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  38791. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  38792. 1);
  38793. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  38794. tlen += len;
  38795. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  38796. sizeof(cleartext) - 1), 1);
  38797. tlen += len;
  38798. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  38799. outTag1Part), 1);
  38800. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  38801. tlen += len;
  38802. AssertIntEQ(tlen, sizeof(cleartext));
  38803. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  38804. /* Test AAD re-use */
  38805. EVP_CIPHER_CTX_free(ctx);
  38806. printf(resultFmt, passed);
  38807. #endif
  38808. }
  38809. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  38810. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  38811. static void test_wolfssl_EVP_aes_gcm_zeroLen(void)
  38812. {
  38813. /* Zero length plain text */
  38814. byte key[] = {
  38815. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38816. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38817. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38818. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  38819. }; /* align */
  38820. byte iv[] = {
  38821. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  38822. }; /* align */
  38823. byte plaintxt[1];
  38824. int ivSz = 12;
  38825. int plaintxtSz = 0;
  38826. unsigned char tag[16];
  38827. unsigned char tag_kat[] =
  38828. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  38829. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  38830. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  38831. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  38832. int ciphertxtSz = 0;
  38833. int decryptedtxtSz = 0;
  38834. int len = 0;
  38835. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  38836. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  38837. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  38838. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  38839. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  38840. plaintxtSz));
  38841. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  38842. ciphertxtSz += len;
  38843. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  38844. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  38845. AssertIntEQ(0, ciphertxtSz);
  38846. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  38847. EVP_CIPHER_CTX_init(de);
  38848. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  38849. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  38850. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  38851. decryptedtxtSz = len;
  38852. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  38853. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  38854. decryptedtxtSz += len;
  38855. AssertIntEQ(0, decryptedtxtSz);
  38856. EVP_CIPHER_CTX_free(en);
  38857. EVP_CIPHER_CTX_free(de);
  38858. }
  38859. #endif
  38860. static void test_wolfssl_EVP_aes_gcm(void)
  38861. {
  38862. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  38863. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  38864. /* A 256 bit key, AES_128 will use the first 128 bit*/
  38865. byte *key = (byte*)"01234567890123456789012345678901";
  38866. /* A 128 bit IV */
  38867. byte *iv = (byte*)"0123456789012345";
  38868. int ivSz = AES_BLOCK_SIZE;
  38869. /* Message to be encrypted */
  38870. byte *plaintxt = (byte*)"for things to change you have to change";
  38871. /* Additional non-confidential data */
  38872. byte *aad = (byte*)"Don't spend major time on minor things.";
  38873. unsigned char tag[AES_BLOCK_SIZE] = {0};
  38874. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  38875. int aadSz = (int)XSTRLEN((char*)aad);
  38876. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  38877. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  38878. int ciphertxtSz = 0;
  38879. int decryptedtxtSz = 0;
  38880. int len = 0;
  38881. int i = 0;
  38882. EVP_CIPHER_CTX en[2];
  38883. EVP_CIPHER_CTX de[2];
  38884. printf(testingFmt, "wolfssl_EVP_aes_gcm");
  38885. for (i = 0; i < 2; i++) {
  38886. EVP_CIPHER_CTX_init(&en[i]);
  38887. if (i == 0) {
  38888. /* Default uses 96-bits IV length */
  38889. #ifdef WOLFSSL_AES_128
  38890. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  38891. #elif defined(WOLFSSL_AES_192)
  38892. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  38893. #elif defined(WOLFSSL_AES_256)
  38894. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  38895. #endif
  38896. }
  38897. else {
  38898. #ifdef WOLFSSL_AES_128
  38899. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  38900. #elif defined(WOLFSSL_AES_192)
  38901. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  38902. #elif defined(WOLFSSL_AES_256)
  38903. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  38904. #endif
  38905. /* non-default must to set the IV length first */
  38906. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  38907. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  38908. }
  38909. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  38910. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  38911. ciphertxtSz = len;
  38912. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  38913. ciphertxtSz += len;
  38914. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  38915. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  38916. EVP_CIPHER_CTX_init(&de[i]);
  38917. if (i == 0) {
  38918. /* Default uses 96-bits IV length */
  38919. #ifdef WOLFSSL_AES_128
  38920. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  38921. #elif defined(WOLFSSL_AES_192)
  38922. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  38923. #elif defined(WOLFSSL_AES_256)
  38924. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  38925. #endif
  38926. }
  38927. else {
  38928. #ifdef WOLFSSL_AES_128
  38929. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  38930. #elif defined(WOLFSSL_AES_192)
  38931. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  38932. #elif defined(WOLFSSL_AES_256)
  38933. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  38934. #endif
  38935. /* non-default must to set the IV length first */
  38936. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  38937. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  38938. }
  38939. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  38940. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  38941. decryptedtxtSz = len;
  38942. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  38943. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  38944. decryptedtxtSz += len;
  38945. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  38946. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  38947. /* modify tag*/
  38948. tag[AES_BLOCK_SIZE-1]+=0xBB;
  38949. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  38950. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  38951. /* fail due to wrong tag */
  38952. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  38953. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  38954. AssertIntEQ(0, len);
  38955. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  38956. }
  38957. test_wolfssl_EVP_aes_gcm_zeroLen();
  38958. printf(resultFmt, passed);
  38959. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  38960. }
  38961. #ifndef NO_BIO
  38962. static void test_wolfSSL_PEM_X509_INFO_read_bio(void)
  38963. {
  38964. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38965. BIO* bio;
  38966. X509_INFO* info;
  38967. STACK_OF(X509_INFO)* sk;
  38968. char* subject;
  38969. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  38970. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  38971. printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
  38972. AssertNotNull(bio = BIO_new(BIO_s_file()));
  38973. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  38974. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  38975. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  38976. /* using dereference to maintain testing for Apache port*/
  38977. AssertNotNull(info = sk_X509_INFO_pop(sk));
  38978. AssertNotNull(info->x_pkey);
  38979. AssertNotNull(info->x_pkey->dec_pkey);
  38980. AssertIntEQ(EVP_PKEY_bits(info->x_pkey->dec_pkey), 2048);
  38981. AssertNotNull(subject =
  38982. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  38983. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  38984. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  38985. X509_INFO_free(info);
  38986. AssertNotNull(info = sk_X509_INFO_pop(sk));
  38987. AssertNotNull(subject =
  38988. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  38989. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  38990. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  38991. X509_INFO_free(info);
  38992. AssertNull(info = sk_X509_INFO_pop(sk));
  38993. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  38994. BIO_free(bio);
  38995. printf(resultFmt, passed);
  38996. #endif
  38997. }
  38998. #endif /* !NO_BIO */
  38999. static void test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  39000. {
  39001. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  39002. X509 *x509;
  39003. X509_NAME* name;
  39004. int idx = 0;
  39005. X509_NAME_ENTRY *ne;
  39006. ASN1_OBJECT *object = NULL;
  39007. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
  39008. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  39009. AssertNotNull(x509);
  39010. name = X509_get_subject_name(x509);
  39011. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  39012. AssertIntGE(idx, 0);
  39013. ne = X509_NAME_get_entry(name, idx);
  39014. AssertNotNull(ne);
  39015. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  39016. X509_free(x509);
  39017. printf(resultFmt, passed);
  39018. #endif
  39019. }
  39020. static void test_wolfSSL_ASN1_INTEGER_get_set(void)
  39021. {
  39022. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  39023. ASN1_INTEGER *a;
  39024. long val;
  39025. int ret;
  39026. printf(testingFmt, "test_wolfSSL_ASN1_INTEGER_get_set");
  39027. a = ASN1_INTEGER_new();
  39028. val = 0;
  39029. ret = ASN1_INTEGER_set(NULL, val);
  39030. AssertIntEQ(ret, 0);
  39031. ASN1_INTEGER_free(a);
  39032. /* 0 */
  39033. a = ASN1_INTEGER_new();
  39034. val = 0;
  39035. ret = ASN1_INTEGER_set(a, val);
  39036. AssertIntEQ(ret, 1);
  39037. AssertIntEQ(ASN1_INTEGER_get(a), val);
  39038. ASN1_INTEGER_free(a);
  39039. /* 40 */
  39040. a = ASN1_INTEGER_new();
  39041. val = 40;
  39042. ret = ASN1_INTEGER_set(a, val);
  39043. AssertIntEQ(ret, 1);
  39044. AssertIntEQ(ASN1_INTEGER_get(a), val);
  39045. ASN1_INTEGER_free(a);
  39046. /* -40 */
  39047. a = ASN1_INTEGER_new();
  39048. val = -40;
  39049. ret = ASN1_INTEGER_set(a, val);
  39050. AssertIntEQ(ret, 1);
  39051. AssertIntEQ(ASN1_INTEGER_get(a), val);
  39052. ASN1_INTEGER_free(a);
  39053. /* 128 */
  39054. a = ASN1_INTEGER_new();
  39055. val = 128;
  39056. ret = ASN1_INTEGER_set(a, val);
  39057. AssertIntEQ(ret, 1);
  39058. AssertIntEQ(ASN1_INTEGER_get(a), val);
  39059. ASN1_INTEGER_free(a);
  39060. /* -128 */
  39061. a = ASN1_INTEGER_new();
  39062. val = -128;
  39063. ret = ASN1_INTEGER_set(a, val);
  39064. AssertIntEQ(ret, 1);
  39065. AssertIntEQ(ASN1_INTEGER_get(a), val);
  39066. ASN1_INTEGER_free(a);
  39067. /* 200 */
  39068. a = ASN1_INTEGER_new();
  39069. val = 200;
  39070. ret = ASN1_INTEGER_set(a, val);
  39071. AssertIntEQ(ret, 1);
  39072. AssertIntEQ(ASN1_INTEGER_get(a), val);
  39073. ASN1_INTEGER_free(a);
  39074. #ifndef TIME_T_NOT_64BIT
  39075. /* int max (2147483647) */
  39076. a = ASN1_INTEGER_new();
  39077. val = 2147483647;
  39078. ret = ASN1_INTEGER_set(a, val);
  39079. AssertIntEQ(ret, 1);
  39080. AssertIntEQ(ASN1_INTEGER_get(a), val);
  39081. ASN1_INTEGER_free(a);
  39082. /* int min (-2147483648) */
  39083. a = ASN1_INTEGER_new();
  39084. val = -2147483647 - 1;
  39085. ret = ASN1_INTEGER_set(a, val);
  39086. AssertIntEQ(ret, 1);
  39087. AssertIntEQ(ASN1_INTEGER_get(a), val);
  39088. ASN1_INTEGER_free(a);
  39089. #endif
  39090. printf(resultFmt, passed);
  39091. #endif
  39092. }
  39093. static void test_wolfSSL_X509_STORE_get1_certs(void)
  39094. {
  39095. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  39096. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  39097. X509_STORE_CTX *storeCtx;
  39098. X509_STORE *store;
  39099. X509 *caX509;
  39100. X509 *svrX509;
  39101. X509_NAME *subject;
  39102. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  39103. printf(testingFmt, "wolfSSL_X509_STORE_get1_certs()");
  39104. AssertNotNull(caX509 =
  39105. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  39106. AssertNotNull((svrX509 =
  39107. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  39108. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  39109. AssertNotNull(store = X509_STORE_new());
  39110. AssertNotNull(subject = X509_get_subject_name(caX509));
  39111. /* Errors */
  39112. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  39113. AssertNull(X509_STORE_get1_certs(NULL, subject));
  39114. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  39115. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  39116. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  39117. /* Should find the cert */
  39118. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  39119. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  39120. sk_X509_free(certs);
  39121. /* Should not find the cert */
  39122. AssertNotNull(subject = X509_get_subject_name(svrX509));
  39123. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  39124. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  39125. sk_X509_free(certs);
  39126. X509_STORE_free(store);
  39127. X509_STORE_CTX_free(storeCtx);
  39128. X509_free(svrX509);
  39129. X509_free(caX509);
  39130. printf(resultFmt, passed);
  39131. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  39132. }
  39133. /* Testing code used in dpp.c in hostap */
  39134. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  39135. typedef struct {
  39136. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  39137. * as an OID identifying the curve */
  39138. X509_ALGOR *alg;
  39139. /* Compressed format public key per ANSI X9.63 */
  39140. ASN1_BIT_STRING *pub_key;
  39141. } DPP_BOOTSTRAPPING_KEY;
  39142. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  39143. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  39144. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  39145. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  39146. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  39147. #endif
  39148. static void test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  39149. {
  39150. /* Testing code used in dpp.c in hostap */
  39151. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  39152. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  39153. EC_KEY *eckey;
  39154. EVP_PKEY *key;
  39155. size_t len;
  39156. unsigned char *der = NULL;
  39157. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  39158. const unsigned char *in = ecc_clikey_der_256;
  39159. const EC_GROUP *group;
  39160. const EC_POINT *point;
  39161. int nid;
  39162. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  39163. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  39164. (long)sizeof_ecc_clikey_der_256));
  39165. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  39166. AssertNotNull(group = EC_KEY_get0_group(eckey));
  39167. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  39168. nid = EC_GROUP_get_curve_name(group);
  39169. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  39170. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  39171. #ifdef HAVE_COMP_KEY
  39172. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  39173. NULL, 0, NULL)), 0);
  39174. #else
  39175. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  39176. NULL, 0, NULL)), 0);
  39177. #endif
  39178. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  39179. #ifdef HAVE_COMP_KEY
  39180. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  39181. der, len, NULL), len);
  39182. #else
  39183. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  39184. der, len, NULL), len);
  39185. #endif
  39186. bootstrap->pub_key->data = der;
  39187. bootstrap->pub_key->length = (int)len;
  39188. /* Not actually used */
  39189. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  39190. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  39191. der = NULL;
  39192. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  39193. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  39194. EVP_PKEY_free(key);
  39195. EC_KEY_free(eckey);
  39196. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  39197. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  39198. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  39199. }
  39200. static void test_wolfSSL_i2c_ASN1_INTEGER(void)
  39201. {
  39202. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  39203. ASN1_INTEGER *a;
  39204. unsigned char *pp,*tpp;
  39205. int ret;
  39206. printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
  39207. a = wolfSSL_ASN1_INTEGER_new();
  39208. /* 40 */
  39209. a->intData[0] = ASN_INTEGER;
  39210. a->intData[1] = 1;
  39211. a->intData[2] = 40;
  39212. ret = i2c_ASN1_INTEGER(a, NULL);
  39213. AssertIntEQ(ret, 1);
  39214. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  39215. DYNAMIC_TYPE_TMP_BUFFER));
  39216. tpp = pp;
  39217. XMEMSET(pp, 0, ret + 1);
  39218. i2c_ASN1_INTEGER(a, &pp);
  39219. pp--;
  39220. AssertIntEQ(*pp, 40);
  39221. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39222. /* 128 */
  39223. a->intData[0] = ASN_INTEGER;
  39224. a->intData[1] = 1;
  39225. a->intData[2] = 128;
  39226. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  39227. AssertIntEQ(ret, 2);
  39228. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  39229. DYNAMIC_TYPE_TMP_BUFFER));
  39230. tpp = pp;
  39231. XMEMSET(pp, 0, ret + 1);
  39232. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  39233. pp--;
  39234. AssertIntEQ(*(pp--), 128);
  39235. AssertIntEQ(*pp, 0);
  39236. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39237. /* -40 */
  39238. a->intData[0] = ASN_INTEGER;
  39239. a->intData[1] = 1;
  39240. a->intData[2] = 40;
  39241. a->negative = 1;
  39242. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  39243. AssertIntEQ(ret, 1);
  39244. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  39245. DYNAMIC_TYPE_TMP_BUFFER));
  39246. tpp = pp;
  39247. XMEMSET(pp, 0, ret + 1);
  39248. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  39249. pp--;
  39250. AssertIntEQ(*pp, 216);
  39251. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39252. /* -128 */
  39253. a->intData[0] = ASN_INTEGER;
  39254. a->intData[1] = 1;
  39255. a->intData[2] = 128;
  39256. a->negative = 1;
  39257. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  39258. AssertIntEQ(ret, 1);
  39259. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  39260. DYNAMIC_TYPE_TMP_BUFFER));
  39261. tpp = pp;
  39262. XMEMSET(pp, 0, ret + 1);
  39263. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  39264. pp--;
  39265. AssertIntEQ(*pp, 128);
  39266. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39267. /* -200 */
  39268. a->intData[0] = ASN_INTEGER;
  39269. a->intData[1] = 1;
  39270. a->intData[2] = 200;
  39271. a->negative = 1;
  39272. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  39273. AssertIntEQ(ret, 2);
  39274. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  39275. DYNAMIC_TYPE_TMP_BUFFER));
  39276. tpp = pp;
  39277. XMEMSET(pp, 0, ret + 1);
  39278. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  39279. pp--;
  39280. AssertIntEQ(*(pp--), 56);
  39281. AssertIntEQ(*pp, 255);
  39282. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39283. wolfSSL_ASN1_INTEGER_free(a);
  39284. printf(resultFmt, passed);
  39285. #endif /* OPENSSL_EXTRA && !NO_ASN */
  39286. }
  39287. #ifndef NO_INLINE
  39288. #define WOLFSSL_MISC_INCLUDED
  39289. #include <wolfcrypt/src/misc.c>
  39290. #else
  39291. #include <wolfssl/wolfcrypt/misc.h>
  39292. #endif
  39293. static int test_ForceZero(void)
  39294. {
  39295. unsigned char data[32];
  39296. unsigned int i, j, len;
  39297. /* Test case with 0 length */
  39298. ForceZero(data, 0);
  39299. /* Test ForceZero */
  39300. for (i = 0; i < sizeof(data); i++) {
  39301. for (len = 1; len < sizeof(data) - i; len++) {
  39302. for (j = 0; j < sizeof(data); j++)
  39303. data[j] = j + 1;
  39304. ForceZero(data + i, len);
  39305. for (j = 0; j < sizeof(data); j++) {
  39306. if (j < i || j >= i + len) {
  39307. if (data[j] == 0x00)
  39308. return -10200;
  39309. }
  39310. else if (data[j] != 0x00)
  39311. return -10201;
  39312. }
  39313. }
  39314. }
  39315. return 0;
  39316. }
  39317. #ifndef NO_BIO
  39318. static void test_wolfSSL_X509_print(void)
  39319. {
  39320. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  39321. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  39322. X509 *x509;
  39323. BIO *bio;
  39324. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  39325. const X509_ALGOR *cert_sig_alg;
  39326. #endif
  39327. printf(testingFmt, "wolfSSL_X509_print");
  39328. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  39329. AssertNotNull(x509);
  39330. /* print to memory */
  39331. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39332. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  39333. #if defined(WOLFSSL_QT)
  39334. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3113);
  39335. #else
  39336. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3103);
  39337. #endif
  39338. BIO_free(bio);
  39339. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  39340. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  39341. /* Print signature */
  39342. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  39343. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  39344. #endif
  39345. /* print to stdout */
  39346. #if !defined(NO_WOLFSSL_DIR)
  39347. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  39348. #endif
  39349. /* print again */
  39350. AssertIntEQ(X509_print_fp(stdout, x509), SSL_SUCCESS);
  39351. X509_free(x509);
  39352. BIO_free(bio);
  39353. printf(resultFmt, passed);
  39354. #endif
  39355. }
  39356. static void test_wolfSSL_RSA_print(void)
  39357. {
  39358. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  39359. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  39360. !defined(HAVE_FAST_RSA) && !defined(NO_BIO)
  39361. BIO *bio;
  39362. WOLFSSL_RSA* rsa = NULL;
  39363. printf(testingFmt, "wolfSSL_RSA_print");
  39364. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  39365. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  39366. AssertIntEQ(RSA_print(bio, rsa, 0), SSL_SUCCESS);
  39367. BIO_free(bio);
  39368. RSA_free(rsa);
  39369. printf(resultFmt, passed);
  39370. #endif
  39371. }
  39372. static void test_wolfSSL_BIO_get_len(void)
  39373. {
  39374. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  39375. BIO *bio;
  39376. const char txt[] = "Some example text to push to the BIO.";
  39377. printf(testingFmt, "wolfSSL_BIO_get_len");
  39378. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  39379. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  39380. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  39381. BIO_free(bio);
  39382. printf(resultFmt, passed);
  39383. #endif
  39384. }
  39385. static void test_wolfSSL_ASN1_STRING_print(void){
  39386. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  39387. ASN1_STRING* asnStr = NULL;
  39388. const char HELLO_DATA[]= \
  39389. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  39390. #define MAX_UNPRINTABLE_CHAR 32
  39391. #define MAX_BUF 255
  39392. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  39393. unsigned char expected[sizeof(unprintableData)+1];
  39394. unsigned char rbuf[MAX_BUF];
  39395. BIO *bio;
  39396. int p_len, i;
  39397. printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
  39398. /* setup */
  39399. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  39400. unprintableData[i] = HELLO_DATA[i];
  39401. expected[i] = HELLO_DATA[i];
  39402. }
  39403. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  39404. unprintableData[sizeof(HELLO_DATA)+i] = i;
  39405. if (i == (int)'\n' || i == (int)'\r')
  39406. expected[sizeof(HELLO_DATA)+i] = i;
  39407. else
  39408. expected[sizeof(HELLO_DATA)+i] = '.';
  39409. }
  39410. unprintableData[sizeof(unprintableData)-1] = '\0';
  39411. expected[sizeof(expected)-1] = '\0';
  39412. XMEMSET(rbuf, 0, MAX_BUF);
  39413. bio = BIO_new(BIO_s_mem());
  39414. BIO_set_write_buf_size(bio, MAX_BUF);
  39415. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  39416. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  39417. (int)sizeof(unprintableData));
  39418. /* test */
  39419. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  39420. AssertIntEQ(p_len, 46);
  39421. BIO_read(bio, (void*)rbuf, 46);
  39422. AssertStrEQ((char*)rbuf, (const char*)expected);
  39423. BIO_free(bio);
  39424. ASN1_STRING_free(asnStr);
  39425. printf(resultFmt, passed);
  39426. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  39427. }
  39428. #endif /* !NO_BIO */
  39429. static void test_wolfSSL_ASN1_get_object(void)
  39430. {
  39431. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  39432. const unsigned char* derBuf = cliecc_cert_der_256;
  39433. int len = sizeof_cliecc_cert_der_256;
  39434. long asnLen = 0;
  39435. int tag = 0, cls = 0;
  39436. ASN1_OBJECT *a;
  39437. printf(testingFmt, "wolfSSL_ASN1_get_object()");
  39438. /* Read a couple TLV triplets and make sure they match the expected values */
  39439. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  39440. AssertIntEQ(asnLen, 841);
  39441. AssertIntEQ(tag, 0x10);
  39442. AssertIntEQ(cls, 0);
  39443. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  39444. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  39445. AssertIntEQ(asnLen, 750);
  39446. AssertIntEQ(tag, 0x10);
  39447. AssertIntEQ(cls, 0);
  39448. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  39449. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  39450. AssertIntEQ(asnLen, 3);
  39451. AssertIntEQ(tag, 0);
  39452. AssertIntEQ(cls, 0x80);
  39453. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  39454. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  39455. AssertIntEQ(asnLen, 1);
  39456. AssertIntEQ(tag, 0x2);
  39457. AssertIntEQ(cls, 0);
  39458. derBuf += asnLen;
  39459. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  39460. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  39461. AssertIntEQ(asnLen, 9);
  39462. AssertIntEQ(tag, 0x2);
  39463. AssertIntEQ(cls, 0);
  39464. derBuf += asnLen;
  39465. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  39466. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  39467. AssertIntEQ(asnLen, 10);
  39468. AssertIntEQ(tag, 0x10);
  39469. AssertIntEQ(cls, 0);
  39470. /* Read an ASN OBJECT */
  39471. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  39472. ASN1_OBJECT_free(a);
  39473. printf(resultFmt, passed);
  39474. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  39475. }
  39476. static void test_wolfSSL_RSA_verify(void)
  39477. {
  39478. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  39479. !defined(NO_FILESYSTEM) && defined(HAVE_CRL)
  39480. #ifndef NO_BIO
  39481. XFILE fp;
  39482. RSA *pKey, *pubKey;
  39483. X509 *cert;
  39484. const char *text = "Hello wolfSSL !";
  39485. unsigned char hash[SHA256_DIGEST_LENGTH];
  39486. unsigned char signature[2048/8];
  39487. unsigned int signatureLength;
  39488. byte *buf;
  39489. BIO *bio;
  39490. SHA256_CTX c;
  39491. EVP_PKEY *evpPkey, *evpPubkey;
  39492. size_t sz;
  39493. printf(testingFmt, "wolfSSL_RSA_verify");
  39494. /* generate hash */
  39495. SHA256_Init(&c);
  39496. SHA256_Update(&c, text, strlen(text));
  39497. SHA256_Final(hash, &c);
  39498. #ifdef WOLFSSL_SMALL_STACK_CACHE
  39499. /* workaround for small stack cache case */
  39500. wc_Sha256Free((wc_Sha256*)&c);
  39501. #endif
  39502. /* read privete key file */
  39503. fp = XFOPEN(svrKeyFile, "rb");
  39504. AssertTrue((fp != XBADFILE));
  39505. XFSEEK(fp, 0, XSEEK_END);
  39506. sz = XFTELL(fp);
  39507. XREWIND(fp);
  39508. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  39509. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  39510. XFCLOSE(fp);
  39511. /* read private key and sign hash data */
  39512. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  39513. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  39514. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  39515. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  39516. signature, &signatureLength, pKey), SSL_SUCCESS);
  39517. /* read public key and verify signed data */
  39518. fp = XFOPEN(svrCertFile,"rb");
  39519. AssertTrue((fp != XBADFILE));
  39520. cert = PEM_read_X509(fp, 0, 0, 0 );
  39521. XFCLOSE(fp);
  39522. evpPubkey = X509_get_pubkey(cert);
  39523. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  39524. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  39525. signatureLength, pubKey), SSL_SUCCESS);
  39526. RSA_free(pKey);
  39527. EVP_PKEY_free(evpPkey);
  39528. RSA_free(pubKey);
  39529. EVP_PKEY_free(evpPubkey);
  39530. X509_free(cert);
  39531. BIO_free(bio);
  39532. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  39533. printf(resultFmt, passed);
  39534. #endif
  39535. #endif
  39536. }
  39537. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39538. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  39539. static void test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  39540. {
  39541. X509* x509 = NULL;
  39542. BIGNUM* serial_number = NULL;
  39543. X509_NAME* name = NULL;
  39544. time_t epoch_off = 0;
  39545. ASN1_INTEGER* asn1_serial_number;
  39546. long not_before, not_after;
  39547. AssertNotNull(x509 = X509_new());
  39548. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  39549. AssertNotNull(serial_number = BN_new());
  39550. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  39551. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  39552. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  39553. /* version 3 */
  39554. AssertIntNE(X509_set_version(x509, 2L), 0);
  39555. AssertNotNull(name = X509_NAME_new());
  39556. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  39557. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  39558. AssertIntNE(X509_set_subject_name(x509, name), 0);
  39559. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  39560. not_before = (long)XTIME(NULL);
  39561. not_after = not_before + (365 * 24 * 60 * 60);
  39562. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  39563. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  39564. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  39565. BN_free(serial_number);
  39566. X509_NAME_free(name);
  39567. X509_free(x509);
  39568. }
  39569. #endif
  39570. static void test_openssl_generate_key_and_cert(void)
  39571. {
  39572. #if defined(OPENSSL_EXTRA)
  39573. #if !defined(NO_RSA)
  39574. {
  39575. EVP_PKEY* pkey = EVP_PKEY_new();
  39576. int key_length = 2048;
  39577. BIGNUM* exponent = BN_new();
  39578. RSA* rsa = RSA_new();
  39579. AssertNotNull(pkey);
  39580. AssertNotNull(exponent);
  39581. AssertNotNull(rsa);
  39582. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  39583. #ifndef WOLFSSL_KEY_GEN
  39584. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), WOLFSSL_FAILURE);
  39585. #if defined(USE_CERT_BUFFERS_1024)
  39586. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  39587. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  39588. key_length = 1024;
  39589. #elif defined(USE_CERT_BUFFERS_2048)
  39590. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  39591. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  39592. #else
  39593. RSA_free(rsa);
  39594. rsa = NULL;
  39595. #endif
  39596. #else
  39597. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  39598. #endif
  39599. if (rsa) {
  39600. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  39601. BN_free(exponent);
  39602. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  39603. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  39604. test_openssl_make_self_signed_certificate(pkey);
  39605. #endif
  39606. }
  39607. EVP_PKEY_free(pkey);
  39608. }
  39609. #endif /* !NO_RSA */
  39610. #ifdef HAVE_ECC
  39611. {
  39612. EVP_PKEY* pkey = EVP_PKEY_new();
  39613. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  39614. AssertNotNull(pkey);
  39615. AssertNotNull(ec_key);
  39616. #ifndef NO_WOLFSSL_STUB
  39617. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  39618. #endif
  39619. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  39620. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  39621. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  39622. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  39623. test_openssl_make_self_signed_certificate(pkey);
  39624. #endif
  39625. EVP_PKEY_free(pkey);
  39626. }
  39627. #endif /* HAVE_ECC */
  39628. #endif /* OPENSSL_EXTRA */
  39629. }
  39630. static void test_stubs_are_stubs(void)
  39631. {
  39632. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  39633. WOLFSSL_CTX* ctx = NULL;
  39634. WOLFSSL_CTX* ctxN = NULL;
  39635. #ifndef NO_WOLFSSL_CLIENT
  39636. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  39637. AssertNotNull(ctx);
  39638. #elif !defined(NO_WOLFSSL_SERVER)
  39639. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  39640. AssertNotNull(ctx);
  39641. #else
  39642. return;
  39643. #endif
  39644. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  39645. AssertIntEQ((int) x(z), 0)
  39646. /* test logic, all stubs return same result regardless of ctx being NULL
  39647. * as there are no sanity checks, it's just a stub! If at some
  39648. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  39649. * if invalid inputs are supplied. Test calling both
  39650. * with and without valid inputs, if a stub functionality remains unchanged.
  39651. */
  39652. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  39653. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  39654. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  39655. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  39656. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  39657. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  39658. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  39659. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  39660. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  39661. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  39662. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  39663. wolfSSL_CTX_free(ctx);
  39664. ctx = NULL;
  39665. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  39666. }
  39667. static void test_CONF_modules_xxx(void)
  39668. {
  39669. #if defined(OPENSSL_EXTRA)
  39670. CONF_modules_free();
  39671. AssertTrue(1); /* to confirm previous call gives no harm */
  39672. CONF_modules_unload(0);
  39673. AssertTrue(1);
  39674. CONF_modules_unload(1);
  39675. AssertTrue(1);
  39676. CONF_modules_unload(-1);
  39677. AssertTrue(1);
  39678. #endif /* OPENSSL_EXTRA */
  39679. }
  39680. static void test_CRYPTO_set_dynlock_xxx(void)
  39681. {
  39682. #if defined(OPENSSL_EXTRA)
  39683. printf(testingFmt, "CRYPTO_set_dynlock_xxx()");
  39684. CRYPTO_set_dynlock_create_callback(
  39685. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  39686. CRYPTO_set_dynlock_create_callback(
  39687. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  39688. CRYPTO_set_dynlock_destroy_callback(
  39689. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  39690. CRYPTO_set_dynlock_destroy_callback(
  39691. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  39692. CRYPTO_set_dynlock_lock_callback(
  39693. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  39694. CRYPTO_set_dynlock_lock_callback(
  39695. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  39696. AssertTrue(1); /* to confirm previous call gives no harm */
  39697. printf(resultFmt, passed);
  39698. #endif /* OPENSSL_EXTRA */
  39699. }
  39700. static void test_CRYPTO_THREADID_xxx(void)
  39701. {
  39702. #if defined(OPENSSL_EXTRA)
  39703. printf(testingFmt, "CRYPTO_THREADID_xxx()");
  39704. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  39705. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  39706. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  39707. printf(resultFmt, passed);
  39708. #endif /* OPENSSL_EXTRA */
  39709. }
  39710. static void test_ENGINE_cleanup(void)
  39711. {
  39712. #if defined(OPENSSL_EXTRA)
  39713. printf(testingFmt, "ENGINE_cleanup()");
  39714. ENGINE_cleanup();
  39715. AssertTrue(1); /* to confirm previous call gives no harm */
  39716. printf(resultFmt, passed);
  39717. #endif /* OPENSSL_EXTRA */
  39718. }
  39719. static void test_wolfSSL_CTX_LoadCRL(void)
  39720. {
  39721. #ifdef HAVE_CRL
  39722. WOLFSSL_CTX* ctx = NULL;
  39723. const char* badPath = "dummypath";
  39724. const char* validPath = "./certs/crl";
  39725. int derType = WOLFSSL_FILETYPE_ASN1;
  39726. int rawType = WOLFSSL_FILETYPE_RAW;
  39727. int pemType = WOLFSSL_FILETYPE_PEM;
  39728. int monitor = WOLFSSL_CRL_MONITOR;
  39729. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  39730. BAD_FUNC_ARG)
  39731. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  39732. WOLFSSL_SUCCESS)
  39733. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  39734. #ifndef NO_WOLFSSL_CLIENT
  39735. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39736. #elif !defined(NO_WOLFSSL_SERVER)
  39737. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  39738. #else
  39739. return;
  39740. #endif
  39741. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  39742. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  39743. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  39744. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, rawType, monitor);
  39745. wolfSSL_CTX_free(ctx);
  39746. ctx = NULL;
  39747. #endif
  39748. }
  39749. static void test_SetTmpEC_DHE_Sz(void)
  39750. {
  39751. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  39752. WOLFSSL_CTX *ctx;
  39753. WOLFSSL *ssl;
  39754. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  39755. AssertNotNull(ctx);
  39756. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  39757. ssl = wolfSSL_new(ctx);
  39758. AssertNotNull(ssl);
  39759. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  39760. wolfSSL_free(ssl);
  39761. wolfSSL_CTX_free(ctx);
  39762. #endif
  39763. }
  39764. static void test_wolfSSL_CTX_get0_privatekey(void)
  39765. {
  39766. #ifdef OPENSSL_ALL
  39767. WOLFSSL_CTX* ctx = NULL;
  39768. printf(testingFmt, "wolfSSL_CTX_get0_privatekey()");
  39769. #ifndef NO_RSA
  39770. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  39771. AssertNull(SSL_CTX_get0_privatekey(ctx));
  39772. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  39773. WOLFSSL_FILETYPE_PEM));
  39774. AssertNull(SSL_CTX_get0_privatekey(ctx));
  39775. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  39776. WOLFSSL_FILETYPE_PEM));
  39777. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  39778. wolfSSL_CTX_free(ctx);
  39779. #endif
  39780. #ifdef HAVE_ECC
  39781. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  39782. AssertNull(SSL_CTX_get0_privatekey(ctx));
  39783. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  39784. WOLFSSL_FILETYPE_PEM));
  39785. AssertNull(SSL_CTX_get0_privatekey(ctx));
  39786. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  39787. WOLFSSL_FILETYPE_PEM));
  39788. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  39789. wolfSSL_CTX_free(ctx);
  39790. #endif
  39791. printf(resultFmt, passed);
  39792. #endif
  39793. }
  39794. static void test_wolfSSL_dtls_set_mtu(void)
  39795. {
  39796. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  39797. defined(WOLFSSL_DTLS)
  39798. WOLFSSL_CTX* ctx = NULL;
  39799. WOLFSSL* ssl = NULL;
  39800. const char* testCertFile;
  39801. const char* testKeyFile;
  39802. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  39803. #ifndef NO_RSA
  39804. testCertFile = svrCertFile;
  39805. testKeyFile = svrKeyFile;
  39806. #elif defined(HAVE_ECC)
  39807. testCertFile = eccCertFile;
  39808. testKeyFile = eccKeyFile;
  39809. #endif
  39810. if (testCertFile != NULL && testKeyFile != NULL) {
  39811. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  39812. WOLFSSL_FILETYPE_PEM));
  39813. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  39814. WOLFSSL_FILETYPE_PEM));
  39815. }
  39816. AssertNotNull(ssl = wolfSSL_new(ctx));
  39817. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  39818. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  39819. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  39820. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  39821. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  39822. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  39823. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  39824. wolfSSL_free(ssl);
  39825. wolfSSL_CTX_free(ctx);
  39826. printf(testingFmt, "wolfSSL_dtls_set_mtu()");
  39827. printf(resultFmt, passed);
  39828. #endif
  39829. }
  39830. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  39831. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  39832. !defined(WOLFSSL_NO_CLIENT_AUTH))
  39833. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  39834. {
  39835. int ret;
  39836. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  39837. printf("loading cert %s failed\n", certA);
  39838. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  39839. return -1;
  39840. }
  39841. return 0;
  39842. }
  39843. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  39844. {
  39845. int ret;
  39846. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  39847. != WOLFSSL_SUCCESS) {
  39848. printf("could not verify the cert: %s\n", certA);
  39849. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  39850. return -1;
  39851. } else {
  39852. printf("successfully verified: %s\n", certA);
  39853. }
  39854. return 0;
  39855. }
  39856. #define LOAD_ONE_CA(a, b, c, d) \
  39857. do { \
  39858. a = load_ca_into_cm(c, d); \
  39859. if (a != 0) \
  39860. return b; \
  39861. else \
  39862. b--; \
  39863. } while(0)
  39864. #define VERIFY_ONE_CERT(a, b, c, d) \
  39865. do { \
  39866. a = verify_cert_with_cm(c, d); \
  39867. if (a != 0) \
  39868. return b; \
  39869. else \
  39870. b--; \
  39871. } while(0)
  39872. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  39873. {
  39874. int ret;
  39875. int i = -1;
  39876. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  39877. char chainGArr[9][50] = {"certs/ca-cert.pem",
  39878. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  39879. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  39880. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  39881. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  39882. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  39883. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  39884. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  39885. "certs/test-pathlen/chainG-entity.pem"};
  39886. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  39887. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  39888. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  39889. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  39890. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  39891. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  39892. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  39893. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  39894. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  39895. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  39896. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  39897. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  39898. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  39899. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  39900. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  39901. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  39902. /* test validating the entity twice, should have no effect on pathLen since
  39903. * entity/leaf cert */
  39904. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  39905. return ret;
  39906. }
  39907. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  39908. {
  39909. int ret;
  39910. int i = -1;
  39911. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  39912. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  39913. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  39914. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  39915. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  39916. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  39917. */
  39918. char chainHArr[6][50] = {"certs/ca-cert.pem",
  39919. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  39920. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  39921. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  39922. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  39923. "certs/test-pathlen/chainH-entity.pem"};
  39924. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  39925. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  39926. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  39927. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  39928. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  39929. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  39930. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  39931. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  39932. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  39933. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  39934. return ret;
  39935. }
  39936. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  39937. {
  39938. int ret;
  39939. int i = -1;
  39940. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  39941. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  39942. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  39943. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  39944. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  39945. */
  39946. char chainIArr[5][50] = {"certs/ca-cert.pem",
  39947. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  39948. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  39949. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  39950. "certs/test-pathlen/chainI-entity.pem"};
  39951. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  39952. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  39953. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  39954. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  39955. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  39956. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  39957. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  39958. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  39959. return ret;
  39960. }
  39961. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  39962. {
  39963. int ret;
  39964. int i = -1;
  39965. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  39966. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  39967. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  39968. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  39969. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  39970. */
  39971. char chainJArr[6][50] = {"certs/ca-cert.pem",
  39972. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  39973. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  39974. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  39975. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  39976. "certs/test-pathlen/chainJ-entity.pem"};
  39977. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  39978. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  39979. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  39980. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  39981. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  39982. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  39983. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  39984. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  39985. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  39986. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  39987. return ret;
  39988. }
  39989. static int test_various_pathlen_chains(void)
  39990. {
  39991. int ret;
  39992. WOLFSSL_CERT_MANAGER* cm;
  39993. /* Test chain G (large chain with varying pathLens) */
  39994. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  39995. printf("cert manager new failed\n");
  39996. return -1;
  39997. }
  39998. AssertIntEQ(test_chainG(cm), 0);
  39999. ret = wolfSSL_CertManagerUnloadCAs(cm);
  40000. if (ret != WOLFSSL_SUCCESS)
  40001. return -1;
  40002. wolfSSL_CertManagerFree(cm);
  40003. /* end test chain G */
  40004. /* Test chain H (5 chain with same pathLens) */
  40005. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  40006. printf("cert manager new failed\n");
  40007. return -1;
  40008. }
  40009. AssertIntLT(test_chainH(cm), 0);
  40010. wolfSSL_CertManagerUnloadCAs(cm);
  40011. wolfSSL_CertManagerFree(cm);
  40012. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  40013. printf("cert manager new failed\n");
  40014. return -1;
  40015. }
  40016. ret = wolfSSL_CertManagerUnloadCAs(cm);
  40017. if (ret != WOLFSSL_SUCCESS)
  40018. return -1;
  40019. wolfSSL_CertManagerFree(cm);
  40020. /* end test chain H */
  40021. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  40022. * followed by 2 ICA's, should pass) */
  40023. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  40024. printf("cert manager new failed\n");
  40025. return -1;
  40026. }
  40027. AssertIntEQ(test_chainI(cm), 0);
  40028. wolfSSL_CertManagerUnloadCAs(cm);
  40029. wolfSSL_CertManagerFree(cm);
  40030. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  40031. printf("cert manager new failed\n");
  40032. return -1;
  40033. }
  40034. ret = wolfSSL_CertManagerUnloadCAs(cm);
  40035. if (ret != WOLFSSL_SUCCESS)
  40036. return -1;
  40037. wolfSSL_CertManagerFree(cm);
  40038. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  40039. * this time followed by 3 ICA's, should fail */
  40040. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  40041. printf("cert manager new failed\n");
  40042. return -1;
  40043. }
  40044. AssertIntLT(test_chainJ(cm), 0);
  40045. wolfSSL_CertManagerUnloadCAs(cm);
  40046. wolfSSL_CertManagerFree(cm);
  40047. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  40048. printf("cert manager new failed\n");
  40049. return -1;
  40050. }
  40051. ret = wolfSSL_CertManagerUnloadCAs(cm);
  40052. wolfSSL_CertManagerFree(cm);
  40053. return ret;
  40054. }
  40055. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  40056. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  40057. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  40058. {
  40059. byte ekm[100] = {0};
  40060. (void)ctx;
  40061. /* Succes Cases */
  40062. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  40063. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  40064. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  40065. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  40066. /* Use some random context */
  40067. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  40068. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  40069. /* Failure cases */
  40070. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  40071. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  40072. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  40073. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  40074. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  40075. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  40076. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  40077. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  40078. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  40079. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  40080. return 0;
  40081. }
  40082. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  40083. {
  40084. wolfSSL_KeepArrays(ssl);
  40085. }
  40086. static void test_export_keying_material(void)
  40087. {
  40088. #ifndef SINGLE_THREADED
  40089. tcp_ready ready;
  40090. callback_functions clientCb;
  40091. func_args client_args;
  40092. func_args server_args;
  40093. THREAD_TYPE serverThread;
  40094. XMEMSET(&client_args, 0, sizeof(func_args));
  40095. XMEMSET(&server_args, 0, sizeof(func_args));
  40096. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  40097. #ifdef WOLFSSL_TIRTOS
  40098. fdOpenSession(Task_self());
  40099. #endif
  40100. StartTCP();
  40101. InitTcpReady(&ready);
  40102. #if defined(USE_WINDOWS_API)
  40103. /* use RNG to get random port if using windows */
  40104. ready.port = GetRandomPort();
  40105. #endif
  40106. server_args.signal = &ready;
  40107. client_args.signal = &ready;
  40108. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  40109. client_args.callbacks = &clientCb;
  40110. start_thread(test_server_nofail, &server_args, &serverThread);
  40111. wait_tcp_ready(&server_args);
  40112. test_client_nofail(&client_args, test_export_keying_material_cb);
  40113. join_thread(serverThread);
  40114. AssertTrue(client_args.return_code);
  40115. AssertTrue(server_args.return_code);
  40116. FreeTcpReady(&ready);
  40117. #ifdef WOLFSSL_TIRTOS
  40118. fdOpenSession(Task_self());
  40119. #endif
  40120. #endif /* !SINGLE_THREADED */
  40121. }
  40122. #endif /* HAVE_KEYING_MATERIAL */
  40123. static int test_wolfSSL_THREADID_hash(void)
  40124. {
  40125. int ret = 0;
  40126. CRYPTO_THREADID id;
  40127. unsigned long res;
  40128. #if defined(OPENSSL_EXTRA)
  40129. printf(testingFmt, "wolfSSL_THREADID_hash");
  40130. CRYPTO_THREADID_current(NULL);
  40131. AssertTrue(1);
  40132. res = CRYPTO_THREADID_hash(NULL);
  40133. AssertTrue( res == 0UL);
  40134. XMEMSET(&id, 0, sizeof(id));
  40135. res = CRYPTO_THREADID_hash(&id);
  40136. AssertTrue( res == 0UL);
  40137. printf(resultFmt, passed);
  40138. #endif /* OPENSSL_EXTRA */
  40139. (void)id;
  40140. (void)res;
  40141. return ret;
  40142. }
  40143. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  40144. {
  40145. int ret = 0;
  40146. WOLFSSL_CTX* ctx = NULL;
  40147. #if defined(OPENSSL_EXTRA)
  40148. printf(testingFmt, "SSL_CTX_set_ecdh_auto");
  40149. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  40150. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  40151. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  40152. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  40153. printf(resultFmt, passed);
  40154. #endif /* OPENSSL_EXTRA */
  40155. (void)ctx;
  40156. return ret;
  40157. }
  40158. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40159. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  40160. {
  40161. callback_functions* callbacks = NULL;
  40162. WOLFSSL_CTX* ctx = NULL;
  40163. WOLFSSL* ssl = NULL;
  40164. SOCKET_T sfd = 0;
  40165. SOCKET_T cfd = 0;
  40166. word16 port;
  40167. char msg[] = "I hear you fa shizzle!";
  40168. int len = (int) XSTRLEN(msg);
  40169. char input[1024];
  40170. int ret, err;
  40171. if (!args)
  40172. return 0;
  40173. ((func_args*)args)->return_code = TEST_FAIL;
  40174. callbacks = ((func_args*)args)->callbacks;
  40175. ctx = wolfSSL_CTX_new(callbacks->method());
  40176. #if defined(USE_WINDOWS_API)
  40177. port = ((func_args*)args)->signal->port;
  40178. #else
  40179. /* Let tcp_listen assign port */
  40180. port = 0;
  40181. #endif
  40182. #ifdef WOLFSSL_TIRTOS
  40183. fdOpenSession(Task_self());
  40184. #endif
  40185. AssertIntEQ(WOLFSSL_SUCCESS,
  40186. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40187. AssertIntEQ(WOLFSSL_SUCCESS,
  40188. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  40189. WOLFSSL_FILETYPE_PEM));
  40190. AssertIntEQ(WOLFSSL_SUCCESS,
  40191. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  40192. WOLFSSL_FILETYPE_PEM));
  40193. if (callbacks->ctx_ready)
  40194. callbacks->ctx_ready(ctx);
  40195. ssl = wolfSSL_new(ctx);
  40196. AssertNotNull(ssl);
  40197. /* listen and accept */
  40198. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  40199. CloseSocket(sfd);
  40200. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  40201. if (callbacks->ssl_ready)
  40202. callbacks->ssl_ready(ssl);
  40203. do {
  40204. err = 0; /* Reset error */
  40205. ret = wolfSSL_accept(ssl);
  40206. if (ret != WOLFSSL_SUCCESS) {
  40207. err = wolfSSL_get_error(ssl, 0);
  40208. }
  40209. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  40210. if (ret != WOLFSSL_SUCCESS) {
  40211. wolfSSL_free(ssl);
  40212. wolfSSL_CTX_free(ctx);
  40213. CloseSocket(cfd);
  40214. ((func_args*)args)->return_code = TEST_FAIL;
  40215. return 0;
  40216. }
  40217. /* read and write data */
  40218. XMEMSET( input, 0, sizeof(input));
  40219. while (1) {
  40220. ret = wolfSSL_read(ssl, input, sizeof(input));
  40221. if (ret > 0) {
  40222. break;
  40223. }
  40224. else {
  40225. err = wolfSSL_get_error(ssl,ret);
  40226. if (err == WOLFSSL_ERROR_WANT_READ) {
  40227. continue;
  40228. }
  40229. break;
  40230. }
  40231. }
  40232. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  40233. do {
  40234. ret = wolfSSL_write(ssl, msg, len);
  40235. if (ret > 0) {
  40236. break;
  40237. }
  40238. } while (ret < 0);
  40239. }
  40240. /* bidirectional shutdown */
  40241. while ((ret = wolfSSL_shutdown(ssl)) != WOLFSSL_SUCCESS) {
  40242. continue;
  40243. }
  40244. /* wait for the peer to disconnect the tcp connection */
  40245. do {
  40246. ret = wolfSSL_read(ssl, input, sizeof(input));
  40247. err = wolfSSL_get_error(ssl, ret);
  40248. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  40249. /* detect TCP disconnect */
  40250. AssertIntLE(ret,WOLFSSL_FAILURE);
  40251. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  40252. ((func_args*)args)->return_code = TEST_SUCCESS;
  40253. wolfSSL_free(ssl);
  40254. wolfSSL_CTX_free(ctx);
  40255. CloseSocket(cfd);
  40256. return 0;
  40257. }
  40258. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  40259. {
  40260. callback_functions* callbacks = NULL;
  40261. WOLFSSL_CTX* ctx = NULL;
  40262. WOLFSSL* ssl = NULL;
  40263. SOCKET_T sfd = 0;
  40264. char msg[] = "hello wolfssl server!";
  40265. int len = (int) XSTRLEN(msg);
  40266. char input[1024];
  40267. int idx;
  40268. int ret, err;
  40269. if (!args)
  40270. return 0;
  40271. ((func_args*)args)->return_code = TEST_FAIL;
  40272. callbacks = ((func_args*)args)->callbacks;
  40273. ctx = wolfSSL_CTX_new(callbacks->method());
  40274. #ifdef WOLFSSL_TIRTOS
  40275. fdOpenSession(Task_self());
  40276. #endif
  40277. AssertIntEQ(WOLFSSL_SUCCESS,
  40278. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40279. AssertIntEQ(WOLFSSL_SUCCESS,
  40280. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  40281. WOLFSSL_FILETYPE_PEM));
  40282. AssertIntEQ(WOLFSSL_SUCCESS,
  40283. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  40284. WOLFSSL_FILETYPE_PEM));
  40285. AssertNotNull((ssl = wolfSSL_new(ctx)));
  40286. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  40287. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  40288. do {
  40289. err = 0; /* Reset error */
  40290. ret = wolfSSL_connect(ssl);
  40291. if (ret != WOLFSSL_SUCCESS) {
  40292. err = wolfSSL_get_error(ssl, 0);
  40293. }
  40294. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  40295. ret = wolfSSL_write(ssl, msg, len);
  40296. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  40297. input[idx] = 0;
  40298. }
  40299. ret = wolfSSL_shutdown(ssl);
  40300. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  40301. ret = wolfSSL_shutdown(ssl);
  40302. }
  40303. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  40304. ((func_args*)args)->return_code = TEST_SUCCESS;
  40305. wolfSSL_free(ssl);
  40306. wolfSSL_CTX_free(ctx);
  40307. CloseSocket(sfd);
  40308. return 0;
  40309. }
  40310. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL */
  40311. /* This test is to check wolfSSL_read behaves as same as
  40312. * openSSL when it is called after SSL_shutdown completes.
  40313. */
  40314. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  40315. {
  40316. int ret = 0;
  40317. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40318. tcp_ready ready;
  40319. func_args client_args;
  40320. func_args server_args;
  40321. THREAD_TYPE serverThread;
  40322. THREAD_TYPE clientThread;
  40323. callback_functions server_cbf;
  40324. callback_functions client_cbf;
  40325. printf(testingFmt, "wolfSSL_read_detect_TCP_disconnect()");
  40326. #ifdef WOLFSSL_TIRTOS
  40327. fdOpenSession(Task_self());
  40328. #endif
  40329. StartTCP();
  40330. InitTcpReady(&ready);
  40331. #if defined(USE_WINDOWS_API)
  40332. /* use RNG to get random port if using windows */
  40333. ready.port = GetRandomPort();
  40334. #endif
  40335. XMEMSET(&client_args, 0, sizeof(func_args));
  40336. XMEMSET(&server_args, 0, sizeof(func_args));
  40337. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  40338. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  40339. server_cbf.method = wolfTLSv1_2_server_method;
  40340. client_cbf.method = wolfTLSv1_2_client_method;
  40341. server_args.callbacks = &server_cbf;
  40342. client_args.callbacks = &client_cbf;
  40343. server_args.signal = &ready;
  40344. client_args.signal = &ready;
  40345. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  40346. wait_tcp_ready(&server_args);
  40347. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  40348. join_thread(clientThread);
  40349. join_thread(serverThread);
  40350. AssertTrue(client_args.return_code);
  40351. AssertTrue(server_args.return_code);
  40352. FreeTcpReady(&ready);
  40353. printf(resultFmt, passed);
  40354. #endif
  40355. return ret;
  40356. }
  40357. static void test_wolfSSL_CTX_get_min_proto_version(void)
  40358. {
  40359. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  40360. WOLFSSL_CTX *ctx;
  40361. (void)ctx;
  40362. printf(testingFmt, "wolfSSL_CTX_get_min_proto_version()");
  40363. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  40364. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  40365. #ifdef WOLFSSL_ALLOW_SSLV3
  40366. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  40367. #else
  40368. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  40369. #endif
  40370. wolfSSL_CTX_free(ctx);
  40371. #ifdef WOLFSSL_ALLOW_TLSV10
  40372. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  40373. #else
  40374. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  40375. #endif
  40376. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  40377. #ifdef WOLFSSL_ALLOW_TLSV10
  40378. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  40379. #else
  40380. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  40381. #endif
  40382. wolfSSL_CTX_free(ctx);
  40383. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  40384. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  40385. #ifndef NO_OLD_TLS
  40386. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  40387. #else
  40388. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  40389. #endif
  40390. wolfSSL_CTX_free(ctx);
  40391. #ifndef WOLFSSL_NO_TLS12
  40392. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  40393. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  40394. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  40395. wolfSSL_CTX_free(ctx);
  40396. #endif
  40397. #ifdef WOLFSSL_TLS13
  40398. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  40399. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  40400. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  40401. wolfSSL_CTX_free(ctx);
  40402. #endif
  40403. printf(resultFmt, passed);
  40404. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  40405. }
  40406. static void test_wolfSSL_security_level(void)
  40407. {
  40408. #if defined(OPENSSL_EXTRA)
  40409. printf(testingFmt, "test_wolfSSL_security_level()");
  40410. SSL_CTX *ctx;
  40411. #ifdef WOLFSSL_TLS13
  40412. #ifdef NO_WOLFSSL_SERVER
  40413. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  40414. #else
  40415. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  40416. #endif
  40417. SSL_CTX_set_security_level(ctx, 1);
  40418. AssertTrue(1);
  40419. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  40420. SSL_CTX_free(ctx);
  40421. #else
  40422. (void)ctx;
  40423. #endif
  40424. printf(resultFmt, passed);
  40425. #endif
  40426. }
  40427. static void test_wolfSSL_SSL_in_init(void)
  40428. {
  40429. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  40430. printf(testingFmt, "test_wolfSSL_SSL_in_init()");
  40431. SSL_CTX* ctx;
  40432. SSL* ssl;
  40433. const char* testCertFile;
  40434. const char* testKeyFile;
  40435. #ifdef WOLFSSL_TLS13
  40436. #ifdef NO_WOLFSSL_SERVER
  40437. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  40438. #else
  40439. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  40440. #endif
  40441. #ifndef NO_RSA
  40442. testCertFile = svrCertFile;
  40443. testKeyFile = svrKeyFile;
  40444. #elif defined(HAVE_ECC)
  40445. testCertFile = eccCertFile;
  40446. testKeyFile = eccKeyFile;
  40447. #else
  40448. testCertFile = NULL;
  40449. testKeyFile = NULL;
  40450. #endif
  40451. if (testCertFile != NULL && testKeyFile != NULL) {
  40452. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  40453. SSL_FILETYPE_PEM));
  40454. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  40455. SSL_FILETYPE_PEM));
  40456. }
  40457. ssl = SSL_new(ctx);
  40458. AssertNotNull(ssl);
  40459. AssertIntEQ(SSL_in_init(ssl), 1);
  40460. SSL_CTX_free(ctx);
  40461. SSL_free(ssl);
  40462. #else
  40463. (void)ctx;
  40464. (void)ssl;
  40465. (void)testCertFile;
  40466. (void)testKeyFile;
  40467. #endif
  40468. printf(resultFmt, passed);
  40469. #endif
  40470. }
  40471. static void test_wolfSSL_EC_curve(void)
  40472. {
  40473. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  40474. printf(testingFmt, "test_wolfSSL_EC_curve()");
  40475. int nid = NID_secp160k1;
  40476. const char* nid_name;
  40477. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  40478. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  40479. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  40480. printf(resultFmt, passed);
  40481. #endif
  40482. }
  40483. static void test_wolfSSL_CTX_set_timeout(void)
  40484. {
  40485. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  40486. int timeout;
  40487. (void)timeout;
  40488. printf(testingFmt, "test_wolfSSL_CTX_set_timeout()");
  40489. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  40490. AssertNotNull(ctx);
  40491. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40492. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  40493. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  40494. */
  40495. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  40496. /* giving 0 as timeout value sets default timeout */
  40497. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  40498. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  40499. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  40500. #else
  40501. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  40502. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  40503. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  40504. #endif
  40505. wolfSSL_CTX_free(ctx);
  40506. printf(resultFmt, passed);
  40507. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  40508. }
  40509. static void test_wolfSSL_OpenSSL_version(void)
  40510. {
  40511. #if defined(OPENSSL_EXTRA)
  40512. printf(testingFmt, "test_wolfSSL_OpenSSL_version()");
  40513. const char* ver;
  40514. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  40515. AssertNotNull(ver = OpenSSL_version(0));
  40516. #else
  40517. AssertNotNull(ver = OpenSSL_version());
  40518. #endif
  40519. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  40520. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  40521. printf(resultFmt, passed);
  40522. #endif
  40523. }
  40524. static void test_CONF_CTX_CMDLINE(void)
  40525. {
  40526. #if defined(OPENSSL_ALL)
  40527. printf(testingFmt, "test_CONF_CTX_CMDLINE");
  40528. SSL_CTX* ctx = NULL;
  40529. SSL_CONF_CTX* cctx = NULL;
  40530. AssertNotNull(cctx = SSL_CONF_CTX_new());
  40531. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  40532. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  40533. AssertTrue(1);
  40534. /* set flags */
  40535. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  40536. WOLFSSL_CONF_FLAG_CMDLINE);
  40537. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  40538. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  40539. /* cmd invalid command */
  40540. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  40541. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  40542. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  40543. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  40544. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  40545. /* cmd Certificate and Private Key*/
  40546. {
  40547. #if !defined(NO_CERTS) && !defined(NO_RSA)
  40548. const char* ourCert = svrCertFile;
  40549. const char* ourKey = svrKeyFile;
  40550. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  40551. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  40552. WOLFSSL_SUCCESS);
  40553. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  40554. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  40555. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  40556. #endif
  40557. }
  40558. /* cmd curves */
  40559. {
  40560. #if defined(HAVE_ECC)
  40561. const char* curve = "secp256r1";
  40562. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  40563. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  40564. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  40565. #endif
  40566. }
  40567. /* cmd CipherString */
  40568. {
  40569. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  40570. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  40571. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  40572. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  40573. }
  40574. /* cmd DH parameter */
  40575. {
  40576. #if !defined(NO_DH) && !defined(NO_BIO)
  40577. const char* ourdhcert = "./certs/dh2048.pem";
  40578. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  40579. -3);
  40580. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  40581. WOLFSSL_SUCCESS);
  40582. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  40583. #endif
  40584. }
  40585. SSL_CTX_free(ctx);
  40586. SSL_CONF_CTX_free(cctx);
  40587. printf(resultFmt, passed);
  40588. #endif /* OPENSSL_EXTRA */
  40589. }
  40590. static void test_CONF_CTX_FILE(void)
  40591. {
  40592. #if defined(OPENSSL_ALL)
  40593. printf(testingFmt, "test_CONF_CTX_FILE");
  40594. SSL_CTX* ctx = NULL;
  40595. SSL_CONF_CTX* cctx = NULL;
  40596. AssertNotNull(cctx = SSL_CONF_CTX_new());
  40597. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  40598. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  40599. AssertTrue(1);
  40600. /* set flags */
  40601. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  40602. WOLFSSL_CONF_FLAG_FILE);
  40603. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  40604. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  40605. /* sanity check */
  40606. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  40607. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  40608. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  40609. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  40610. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  40611. /* cmd Certificate and Private Key*/
  40612. {
  40613. #if !defined(NO_CERTS) && !defined(NO_RSA)
  40614. const char* ourCert = svrCertFile;
  40615. const char* ourKey = svrKeyFile;
  40616. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  40617. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  40618. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  40619. WOLFSSL_SUCCESS);
  40620. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  40621. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  40622. #endif
  40623. }
  40624. /* cmd curves */
  40625. {
  40626. #if defined(HAVE_ECC)
  40627. const char* curve = "secp256r1";
  40628. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  40629. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  40630. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  40631. #endif
  40632. }
  40633. /* cmd CipherString */
  40634. {
  40635. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  40636. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  40637. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  40638. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  40639. }
  40640. /* cmd DH parameter */
  40641. {
  40642. #if !defined(NO_DH) && !defined(NO_BIO)
  40643. const char* ourdhcert = "./certs/dh3072.pem";
  40644. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  40645. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  40646. WOLFSSL_SUCCESS);
  40647. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  40648. #endif
  40649. }
  40650. SSL_CTX_free(ctx);
  40651. SSL_CONF_CTX_free(cctx);
  40652. printf(resultFmt, passed);
  40653. #endif /* OPENSSL_EXTRA */
  40654. }
  40655. static void test_wolfSSL_CRYPTO_get_ex_new_index(void)
  40656. {
  40657. #if defined(HAVE_EX_DATA) || defined(FORTRESS)
  40658. int idx1,idx2;
  40659. printf(testingFmt, "test_wolfSSL_CRYPTO_get_ex_new_index()");
  40660. /* test for unsupported flass index */
  40661. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION,
  40662. 0,NULL, NULL, NULL, NULL ), -1);
  40663. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE,
  40664. 0,NULL, NULL, NULL, NULL ), -1);
  40665. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX,
  40666. 0,NULL, NULL, NULL, NULL ), -1);
  40667. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH,
  40668. 0,NULL, NULL, NULL, NULL ), -1);
  40669. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA,
  40670. 0,NULL, NULL, NULL, NULL ), -1);
  40671. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY,
  40672. 0,NULL, NULL, NULL, NULL ), -1);
  40673. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA,
  40674. 0,NULL, NULL, NULL, NULL ), -1);
  40675. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE,
  40676. 0,NULL, NULL, NULL, NULL ), -1);
  40677. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI,
  40678. 0,NULL, NULL, NULL, NULL ), -1);
  40679. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO,
  40680. 0,NULL, NULL, NULL, NULL ), -1);
  40681. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  40682. 0,NULL, NULL, NULL, NULL ), -1);
  40683. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD,
  40684. 0,NULL, NULL, NULL, NULL ), -1);
  40685. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DRBG,
  40686. 0,NULL, NULL, NULL, NULL ), -1);
  40687. AssertIntEQ(CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  40688. /* test for supported class index */
  40689. idx1 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL,
  40690. 0,NULL, NULL, NULL, NULL );
  40691. idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL,
  40692. 0,NULL, NULL, NULL, NULL );
  40693. AssertIntNE(idx1, -1);
  40694. AssertIntNE(idx2, -1);
  40695. AssertIntNE(idx1, idx2);
  40696. idx1 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX,
  40697. 0,NULL, NULL, NULL, NULL );
  40698. idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX,
  40699. 0,NULL, NULL, NULL, NULL );
  40700. AssertIntNE(idx1, -1);
  40701. AssertIntNE(idx2, -1);
  40702. AssertIntNE(idx1, idx2);
  40703. idx1 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509,
  40704. 0,NULL, NULL, NULL, NULL );
  40705. idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509,
  40706. 0,NULL, NULL, NULL, NULL );
  40707. AssertIntNE(idx1, -1);
  40708. AssertIntNE(idx2, -1);
  40709. AssertIntNE(idx1, idx2);
  40710. printf(resultFmt, "passed");
  40711. #endif /* HAVE_EX_DATA || FORTRESS */
  40712. }
  40713. static void test_wolfSSL_set_psk_use_session_callback(void)
  40714. {
  40715. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  40716. printf(testingFmt, "test_wolfSSL_set_psk_use_session_callback()");
  40717. SSL_CTX* ctx;
  40718. SSL* ssl;
  40719. const char* testCertFile;
  40720. const char* testKeyFile;
  40721. #ifdef WOLFSSL_TLS13
  40722. #ifdef NO_WOLFSSL_SERVER
  40723. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  40724. #else
  40725. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  40726. #endif
  40727. #ifndef NO_RSA
  40728. testCertFile = svrCertFile;
  40729. testKeyFile = svrKeyFile;
  40730. #elif defined(HAVE_ECC)
  40731. testCertFile = eccCertFile;
  40732. testKeyFile = eccKeyFile;
  40733. #else
  40734. testCertFile = NULL;
  40735. testKeyFile = NULL;
  40736. #endif
  40737. if (testCertFile != NULL && testKeyFile != NULL) {
  40738. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  40739. SSL_FILETYPE_PEM));
  40740. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  40741. SSL_FILETYPE_PEM));
  40742. }
  40743. ssl = SSL_new(ctx);
  40744. AssertNotNull(ssl);
  40745. SSL_set_psk_use_session_callback(ssl,
  40746. my_psk_use_session_cb);
  40747. AssertTrue(1);
  40748. SSL_CTX_free(ctx);
  40749. SSL_free(ssl);
  40750. #else
  40751. (void)ctx;
  40752. (void)ssl;
  40753. (void)testCertFile;
  40754. (void)testKeyFile;
  40755. #endif
  40756. printf(resultFmt, passed);
  40757. #endif
  40758. }
  40759. static void test_wolfSSL_DH(void)
  40760. {
  40761. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  40762. DH *dh = NULL;
  40763. BIGNUM* p;
  40764. BIGNUM* q;
  40765. BIGNUM* g;
  40766. BIGNUM* pub;
  40767. BIGNUM* priv;
  40768. (void)dh;
  40769. (void)p;
  40770. (void)q;
  40771. (void)g;
  40772. (void)pub;
  40773. (void)priv;
  40774. #if defined(OPENSSL_ALL) && defined(WOLFSSL_KEY_GEN)
  40775. #if !defined(HAVE_FIPS) || \
  40776. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  40777. FILE* f = NULL;
  40778. unsigned char buf[268];
  40779. const unsigned char* pt = buf;
  40780. long len = 0;
  40781. dh = NULL;
  40782. XMEMSET(buf, 0, sizeof(buf));
  40783. /* Test 2048 bit parameters */
  40784. f = XFOPEN("./certs/dh2048.der", "rb");
  40785. AssertTrue(f != XBADFILE);
  40786. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  40787. XFCLOSE(f);
  40788. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  40789. AssertNotNull(dh->p);
  40790. AssertNotNull(dh->g);
  40791. AssertTrue(pt != buf);
  40792. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  40793. DH_get0_pqg(dh, (const BIGNUM**)&p,
  40794. (const BIGNUM**)&q,
  40795. (const BIGNUM**) &g);
  40796. AssertPtrEq(p, dh->p);
  40797. AssertPtrEq(q, dh->q);
  40798. AssertPtrEq(g, dh->g);
  40799. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  40800. AssertPtrEq(pub, dh->pub_key);
  40801. AssertPtrEq(priv, dh->priv_key);
  40802. AssertNotNull(pub = BN_new());
  40803. AssertNotNull(priv = BN_new());
  40804. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  40805. AssertPtrEq(pub, dh->pub_key);
  40806. AssertPtrEq(priv, dh->priv_key);
  40807. DH_free(dh);
  40808. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  40809. DH_free(dh);
  40810. #endif
  40811. #endif
  40812. printf(testingFmt, "test_wolfSSL_DH");
  40813. dh = wolfSSL_DH_new();
  40814. AssertNotNull(dh);
  40815. /* invalid parameters test */
  40816. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  40817. (const BIGNUM**)&q,
  40818. (const BIGNUM**)&g);
  40819. DH_get0_pqg(dh, NULL,
  40820. (const BIGNUM**)&q,
  40821. (const BIGNUM**)&g);
  40822. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  40823. DH_get0_pqg(dh, NULL, NULL, NULL);
  40824. AssertTrue(1);
  40825. DH_get0_pqg(dh, (const BIGNUM**)&p,
  40826. (const BIGNUM**)&q,
  40827. (const BIGNUM**)&g);
  40828. AssertPtrEq(p, NULL);
  40829. AssertPtrEq(q, NULL);
  40830. AssertPtrEq(g, NULL);
  40831. DH_free(dh);
  40832. printf(resultFmt, passed);
  40833. #endif /* OPENSSL_EXTRA && !NO_DH */
  40834. }
  40835. static void test_wolfSSL_ERR_strings(void)
  40836. {
  40837. const char* err1 = "unsupported cipher suite";
  40838. const char* err2 = "wolfSSL PEM routines";
  40839. const char* err = NULL;
  40840. (void)err;
  40841. (void)err1;
  40842. (void)err2;
  40843. #if !defined(NO_ERROR_STRINGS)
  40844. printf(testingFmt, "test_wolfSSL_ERR_strings");
  40845. #if defined(OPENSSL_EXTRA)
  40846. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  40847. AssertTrue(err != NULL);
  40848. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  40849. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  40850. AssertTrue(err != NULL);
  40851. AssertIntEQ((*err == '\0'), 1);
  40852. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  40853. AssertTrue(err != NULL);
  40854. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  40855. #else
  40856. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  40857. AssertTrue(err != NULL);
  40858. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  40859. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  40860. AssertTrue(err != NULL);
  40861. AssertIntEQ((*err == '\0'), 1);
  40862. err = wolfSSL_ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  40863. AssertTrue(err != NULL);
  40864. AssertIntEQ((*err == ('\0')), 1);
  40865. #endif
  40866. printf(resultFmt, passed);
  40867. #endif
  40868. }
  40869. static void test_wolfSSL_EVP_shake128(void)
  40870. {
  40871. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  40872. defined(WOLFSSL_SHAKE128)
  40873. printf(testingFmt, "test_wolfSSL_EVP_shake128");
  40874. const EVP_MD* md = NULL;
  40875. md = EVP_shake128();
  40876. AssertTrue(md != NULL);
  40877. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  40878. printf(resultFmt, passed);
  40879. #endif
  40880. }
  40881. static void test_wolfSSL_EVP_shake256(void)
  40882. {
  40883. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  40884. defined(WOLFSSL_SHAKE256)
  40885. printf(testingFmt, "test_wolfSSL_EVP_shake256");
  40886. const EVP_MD* md = NULL;
  40887. md = EVP_shake256();
  40888. AssertTrue(md != NULL);
  40889. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  40890. printf(resultFmt, passed);
  40891. #endif
  40892. }
  40893. static void test_EVP_blake2(void)
  40894. {
  40895. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  40896. printf(testingFmt, "test_EVP_blake2");
  40897. const EVP_MD* md = NULL;
  40898. (void)md;
  40899. #if defined(HAVE_BLAKE2)
  40900. md = EVP_blake2b512();
  40901. AssertTrue(md != NULL);
  40902. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  40903. #endif
  40904. #if defined(HAVE_BLAKE2S)
  40905. md = EVP_blake2s256();
  40906. AssertTrue(md != NULL);
  40907. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  40908. #endif
  40909. printf(resultFmt, passed);
  40910. #endif
  40911. }
  40912. #if defined(OPENSSL_EXTRA)
  40913. static void list_md_fn(const EVP_MD* m, const char* from,
  40914. const char* to, void* arg)
  40915. {
  40916. const char* mn;
  40917. BIO *bio;
  40918. (void) from;
  40919. (void) to;
  40920. (void) arg;
  40921. (void) mn;
  40922. (void) bio;
  40923. if (!m) {
  40924. /* alias */
  40925. AssertNull(m);
  40926. AssertNotNull(to);
  40927. }
  40928. else {
  40929. AssertNotNull(m);
  40930. AssertNull(to);
  40931. }
  40932. AssertNotNull(from);
  40933. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  40934. mn = EVP_get_digestbyname(from);
  40935. /* print to stdout */
  40936. AssertNotNull(arg);
  40937. bio = BIO_new(BIO_s_file());
  40938. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  40939. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  40940. BIO_free(bio);
  40941. #endif
  40942. }
  40943. #endif
  40944. static void test_EVP_MD_do_all(void)
  40945. {
  40946. #if defined(OPENSSL_EXTRA)
  40947. printf(testingFmt, "test_EVP_MD_do_all");
  40948. EVP_MD_do_all(NULL, stdout);
  40949. /* to confirm previous call gives no harm */
  40950. AssertTrue(1);
  40951. EVP_MD_do_all(list_md_fn, stdout);
  40952. /* to confirm previous call gives no harm */
  40953. AssertTrue(1);
  40954. printf(resultFmt, passed);
  40955. #endif
  40956. }
  40957. #if defined(OPENSSL_EXTRA)
  40958. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  40959. {
  40960. (void)arg;
  40961. (void)nm;
  40962. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  40963. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  40964. /* print to stdout */
  40965. AssertNotNull(arg);
  40966. bio = BIO_new(BIO_s_file());
  40967. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  40968. BIO_printf(bio, "%s\n", mn);
  40969. BIO_free(bio);
  40970. #endif
  40971. }
  40972. #endif
  40973. static void test_OBJ_NAME_do_all(void)
  40974. {
  40975. #if defined(OPENSSL_EXTRA)
  40976. printf(testingFmt, "test_OBJ_NAME_do_all");
  40977. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  40978. /* to confirm previous call gives no harm */
  40979. AssertTrue(1);
  40980. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stdout);
  40981. /* to confirm previous call gives no harm */
  40982. AssertTrue(1);
  40983. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stdout);
  40984. AssertTrue(1);
  40985. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stdout);
  40986. AssertTrue(1);
  40987. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stdout);
  40988. AssertTrue(1);
  40989. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stdout);
  40990. AssertTrue(1);
  40991. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stdout);
  40992. AssertTrue(1);
  40993. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stdout);
  40994. AssertTrue(1);
  40995. OBJ_NAME_do_all(-1, obj_name_t, stdout);
  40996. AssertTrue(1);
  40997. printf(resultFmt, passed);
  40998. #endif
  40999. }
  41000. static void test_SSL_CIPHER_get_xxx(void)
  41001. {
  41002. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  41003. !defined(NO_FILESYSTEM)
  41004. printf(testingFmt, "test_SSL_CIPHER_get_xxx");
  41005. const SSL_CIPHER* cipher = NULL;
  41006. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  41007. int i, numCiphers = 0;
  41008. SSL_CTX* ctx = NULL;
  41009. SSL* ssl = NULL;
  41010. const char* testCertFile;
  41011. const char* testKeyFile;
  41012. char buf[256] = {0};
  41013. const char* cipher_id = NULL;
  41014. int expect_nid1 = NID_undef;
  41015. int expect_nid2 = NID_undef;
  41016. int expect_nid3 = NID_undef;
  41017. int expect_nid4 = NID_undef;
  41018. int expect_nid5 = 0;
  41019. const char* cipher_id2 = NULL;
  41020. int expect_nid21 = NID_undef;
  41021. int expect_nid22 = NID_undef;
  41022. int expect_nid23 = NID_undef;
  41023. int expect_nid24 = NID_undef;
  41024. int expect_nid25 = 0;
  41025. (void)cipher;
  41026. (void)supportedCiphers;
  41027. (void)i;
  41028. (void)numCiphers;
  41029. (void)ctx;
  41030. (void)ssl;
  41031. (void)testCertFile;
  41032. (void)testKeyFile;
  41033. #if defined(WOLFSSL_TLS13)
  41034. cipher_id = "TLS13-AES128-GCM-SHA256";
  41035. expect_nid1 = NID_auth_rsa;
  41036. expect_nid2 = NID_aes_128_gcm;
  41037. expect_nid3 = NID_sha256;
  41038. expect_nid4 = NID_kx_any;
  41039. expect_nid5 = 1;
  41040. #if !defined(WOLFSSL_NO_TLS12)
  41041. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  41042. expect_nid21 = NID_auth_rsa;
  41043. expect_nid22 = NID_aes_256_gcm;
  41044. expect_nid23 = NID_sha384;
  41045. expect_nid24 = NID_kx_ecdhe;
  41046. expect_nid25 = 1;
  41047. #endif
  41048. #endif
  41049. #ifdef NO_WOLFSSL_SERVER
  41050. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41051. #else
  41052. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  41053. #endif
  41054. if (cipher_id) {
  41055. #ifndef NO_RSA
  41056. testCertFile = svrCertFile;
  41057. testKeyFile = svrKeyFile;
  41058. #elif defined(HAVE_ECC)
  41059. testCertFile = eccCertFile;
  41060. testKeyFile = eccKeyFile;
  41061. #else
  41062. testCertFile = NULL;
  41063. testKeyFile = NULL;
  41064. #endif
  41065. if (testCertFile != NULL && testKeyFile != NULL) {
  41066. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  41067. SSL_FILETYPE_PEM));
  41068. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  41069. SSL_FILETYPE_PEM));
  41070. }
  41071. ssl = SSL_new(ctx);
  41072. AssertNotNull(ssl);
  41073. AssertIntEQ(SSL_in_init(ssl), 1);
  41074. supportedCiphers = SSL_get_ciphers(ssl);
  41075. numCiphers = sk_num(supportedCiphers);
  41076. for (i = 0; i < numCiphers; ++i) {
  41077. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  41078. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  41079. }
  41080. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  41081. break;
  41082. }
  41083. }
  41084. /* test case for */
  41085. if (i != numCiphers) {
  41086. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  41087. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  41088. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  41089. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  41090. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  41091. }
  41092. if (cipher_id2) {
  41093. for (i = 0; i < numCiphers; ++i) {
  41094. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  41095. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  41096. }
  41097. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  41098. break;
  41099. }
  41100. }
  41101. /* test case for */
  41102. if (i != numCiphers) {
  41103. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  41104. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  41105. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  41106. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  41107. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  41108. }
  41109. }
  41110. }
  41111. if (ctx)
  41112. SSL_CTX_free(ctx);
  41113. if(ssl)
  41114. SSL_free(ssl);
  41115. printf(resultFmt, passed);
  41116. #endif
  41117. }
  41118. /*----------------------------------------------------------------------------*
  41119. | Main
  41120. *----------------------------------------------------------------------------*/
  41121. void ApiTest(void)
  41122. {
  41123. printf("\n-----------------Porting tests------------------\n");
  41124. AssertTrue(test_fileAccess());
  41125. printf(" Begin API Tests\n");
  41126. AssertIntEQ(test_wolfSSL_Init(), WOLFSSL_SUCCESS);
  41127. /* wolfcrypt initialization tests */
  41128. test_wolfSSL_Method_Allocators();
  41129. #ifndef NO_WOLFSSL_SERVER
  41130. test_wolfSSL_CTX_new(wolfSSLv23_server_method());
  41131. #endif
  41132. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  41133. (!defined(NO_RSA) || defined(HAVE_ECC))
  41134. test_for_double_Free();
  41135. #endif
  41136. test_SSL_CIPHER_get_xxx();
  41137. test_wolfSSL_ERR_strings();
  41138. test_wolfSSL_EVP_shake128();
  41139. test_wolfSSL_EVP_shake256();
  41140. test_EVP_blake2();
  41141. test_EVP_MD_do_all();
  41142. test_OBJ_NAME_do_all();
  41143. test_wolfSSL_CTX_use_certificate_file();
  41144. AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS);
  41145. test_wolfSSL_CTX_use_PrivateKey_file();
  41146. test_wolfSSL_CTX_load_verify_locations();
  41147. test_wolfSSL_CertManagerCheckOCSPResponse();
  41148. test_wolfSSL_CertManagerLoadCABuffer();
  41149. test_wolfSSL_CertManagerGetCerts();
  41150. test_wolfSSL_CertManagerSetVerify();
  41151. test_wolfSSL_CertManagerNameConstraint();
  41152. test_wolfSSL_CertManagerNameConstraint2();
  41153. test_wolfSSL_CertManagerCRL();
  41154. test_wolfSSL_CTX_load_verify_locations_ex();
  41155. test_wolfSSL_CTX_load_verify_buffer_ex();
  41156. test_wolfSSL_CTX_load_verify_chain_buffer_format();
  41157. test_wolfSSL_CTX_add1_chain_cert();
  41158. test_wolfSSL_CTX_use_certificate_chain_file_format();
  41159. test_wolfSSL_CTX_trust_peer_cert();
  41160. test_wolfSSL_CTX_SetTmpDH_file();
  41161. test_wolfSSL_CTX_SetTmpDH_buffer();
  41162. test_wolfSSL_CTX_SetMinMaxDhKey_Sz();
  41163. test_wolfSSL_CTX_der_load_verify_locations();
  41164. test_wolfSSL_CTX_enable_disable();
  41165. test_wolfSSL_CTX_ticket_API();
  41166. test_server_wolfSSL_new();
  41167. test_client_wolfSSL_new();
  41168. test_wolfSSL_SetTmpDH_file();
  41169. test_wolfSSL_SetTmpDH_buffer();
  41170. test_wolfSSL_SetMinMaxDhKey_Sz();
  41171. test_SetTmpEC_DHE_Sz();
  41172. test_wolfSSL_CTX_get0_privatekey();
  41173. test_wolfSSL_dtls_set_mtu();
  41174. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  41175. defined(HAVE_IO_TESTS_DEPENDENCIES)
  41176. test_wolfSSL_read_write();
  41177. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  41178. test_wolfSSL_reuse_WOLFSSLobj();
  41179. #endif
  41180. test_wolfSSL_CTX_verifyDepth_ServerClient();
  41181. test_wolfSSL_dtls_export();
  41182. #endif
  41183. AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS);
  41184. AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);
  41185. /* TLS extensions tests */
  41186. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  41187. test_wolfSSL_UseSNI();
  41188. #endif
  41189. test_wolfSSL_UseTrustedCA();
  41190. test_wolfSSL_UseMaxFragment();
  41191. test_wolfSSL_UseTruncatedHMAC();
  41192. test_wolfSSL_UseSupportedCurve();
  41193. test_wolfSSL_UseALPN();
  41194. test_wolfSSL_DisableExtendedMasterSecret();
  41195. test_wolfSSL_wolfSSL_UseSecureRenegotiation();
  41196. /* X509 tests */
  41197. test_wolfSSL_X509_NAME_get_entry();
  41198. test_wolfSSL_PKCS12();
  41199. test_wolfSSL_no_password_cb();
  41200. test_wolfSSL_PKCS8();
  41201. test_wolfSSL_PKCS8_ED25519();
  41202. test_wolfSSL_PKCS8_ED448();
  41203. test_wolfSSL_PKCS5();
  41204. test_wolfSSL_URI();
  41205. test_wolfSSL_TBS();
  41206. test_wolfSSL_X509_verify();
  41207. test_wolfSSL_X509_TLS_version();
  41208. test_wc_PemToDer();
  41209. test_wc_AllocDer();
  41210. test_wc_CertPemToDer();
  41211. test_wc_PubKeyPemToDer();
  41212. test_wc_PemPubKeyToDer();
  41213. /*OCSP Stapling. */
  41214. AssertIntEQ(test_wolfSSL_UseOCSPStapling(), WOLFSSL_SUCCESS);
  41215. AssertIntEQ(test_wolfSSL_UseOCSPStaplingV2(), WOLFSSL_SUCCESS);
  41216. /* Multicast */
  41217. test_wolfSSL_mcast();
  41218. /* compatibility tests */
  41219. test_wolfSSL_lhash();
  41220. test_wolfSSL_X509_NAME();
  41221. test_wolfSSL_X509_NAME_hash();
  41222. #ifndef NO_BIO
  41223. test_wolfSSL_X509_INFO();
  41224. #endif
  41225. test_wolfSSL_X509_subject_name_hash();
  41226. test_wolfSSL_X509_issuer_name_hash();
  41227. test_wolfSSL_X509_check_host();
  41228. test_wolfSSL_X509_check_email();
  41229. test_wolfSSL_DES();
  41230. test_wolfSSL_certs();
  41231. test_wolfSSL_X509_check_private_key();
  41232. test_wolfSSL_ASN1_TIME_print();
  41233. test_wolfSSL_ASN1_UTCTIME_print();
  41234. test_wolfSSL_ASN1_GENERALIZEDTIME_free();
  41235. test_wolfSSL_private_keys();
  41236. test_wolfSSL_PEM_read_PrivateKey();
  41237. test_wolfSSL_PEM_PrivateKey();
  41238. #ifndef NO_BIO
  41239. test_wolfSSL_PEM_bio_RSAKey();
  41240. test_wolfSSL_PEM_bio_DSAKey();
  41241. test_wolfSSL_PEM_bio_ECKey();
  41242. test_wolfSSL_PEM_RSAPrivateKey();
  41243. test_wolfSSL_PEM_PUBKEY();
  41244. #endif
  41245. test_DSA_do_sign_verify();
  41246. test_wolfSSL_tmp_dh();
  41247. test_wolfSSL_ctrl();
  41248. test_wolfSSL_EVP_MD_size();
  41249. test_wolfSSL_EVP_MD_pkey_type();
  41250. test_wolfSSL_EVP_Digest();
  41251. test_wolfSSL_EVP_Digest_all();
  41252. test_wolfSSL_EVP_PKEY_new_mac_key();
  41253. test_wolfSSL_EVP_MD_hmac_signing();
  41254. test_wolfSSL_EVP_MD_rsa_signing();
  41255. test_wolfSSL_EVP_MD_ecc_signing();
  41256. test_wolfSSL_EVP_PKEY_print_public();
  41257. test_wolfSSL_EVP_ENCODE_CTX_new();
  41258. test_wolfSSL_EVP_ENCODE_CTX_free();
  41259. test_wolfSSL_EVP_EncodeInit();
  41260. test_wolfSSL_EVP_EncodeUpdate();
  41261. test_wolfSSL_EVP_EncodeFinal();
  41262. test_wolfSSL_EVP_DecodeInit();
  41263. test_wolfSSL_EVP_DecodeUpdate();
  41264. test_wolfSSL_EVP_DecodeFinal();
  41265. test_wolfSSL_CTX_add_extra_chain_cert();
  41266. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  41267. test_wolfSSL_ERR_peek_last_error_line();
  41268. #endif
  41269. #ifndef NO_BIO
  41270. test_wolfSSL_ERR_print_errors_cb();
  41271. AssertFalse(test_wolfSSL_GetLoggingCb());
  41272. AssertFalse(test_WOLFSSL_ERROR_MSG());
  41273. AssertFalse(test_wc_ERR_remove_state());
  41274. AssertFalse(test_wc_ERR_print_errors_fp());
  41275. #endif
  41276. test_wolfSSL_set_options();
  41277. test_wolfSSL_sk_SSL_CIPHER();
  41278. test_wolfSSL_set1_sigalgs_list();
  41279. test_wolfSSL_PKCS7_certs();
  41280. test_wolfSSL_X509_STORE_CTX();
  41281. test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup();
  41282. test_wolfSSL_X509_STORE_CTX_get0_current_issuer();
  41283. test_wolfSSL_msgCb();
  41284. test_wolfSSL_either_side();
  41285. test_wolfSSL_DTLS_either_side();
  41286. test_generate_cookie();
  41287. test_wolfSSL_X509_STORE_set_flags();
  41288. test_wolfSSL_X509_LOOKUP_load_file();
  41289. test_wolfSSL_X509_Name_canon();
  41290. test_wolfSSL_X509_LOOKUP_ctrl_file();
  41291. test_wolfSSL_X509_LOOKUP_ctrl_hash_dir();
  41292. test_wolfSSL_X509_NID();
  41293. test_wolfSSL_X509_STORE_CTX_set_time();
  41294. test_wolfSSL_get0_param();
  41295. test_wolfSSL_X509_VERIFY_PARAM_set1_host();
  41296. test_wolfSSL_X509_VERIFY_PARAM_set1_ip();
  41297. test_wolfSSL_X509_STORE_CTX_get0_store();
  41298. test_wolfSSL_X509_STORE();
  41299. test_wolfSSL_X509_STORE_load_locations();
  41300. test_X509_STORE_get0_objects();
  41301. test_wolfSSL_X509_load_crl_file();
  41302. test_wolfSSL_BN();
  41303. test_wolfSSL_CTX_get0_set1_param();
  41304. #ifndef NO_BIO
  41305. test_wolfSSL_PEM_read_bio();
  41306. test_wolfSSL_BIO();
  41307. #endif
  41308. test_wolfSSL_ASN1_STRING();
  41309. test_wolfSSL_ASN1_BIT_STRING();
  41310. test_wolfSSL_a2i_ASN1_INTEGER();
  41311. test_wolfSSL_a2i_IPADDRESS();
  41312. test_wolfSSL_X509();
  41313. test_wolfSSL_X509_VERIFY_PARAM();
  41314. test_wolfSSL_X509_sign();
  41315. test_wolfSSL_X509_sign2();
  41316. test_wolfSSL_X509_get0_tbs_sigalg();
  41317. test_wolfSSL_X509_ALGOR_get0();
  41318. test_wolfSSL_X509_get_X509_PUBKEY();
  41319. test_wolfSSL_X509_PUBKEY();
  41320. test_wolfSSL_RAND();
  41321. test_wolfSSL_BUF();
  41322. test_wolfSSL_set_tlsext_status_type();
  41323. test_wolfSSL_ASN1_TIME_adj();
  41324. test_wolfSSL_X509_cmp_time();
  41325. test_wolfSSL_X509_time_adj();
  41326. test_wolfSSL_CTX_set_client_CA_list();
  41327. test_wolfSSL_CTX_add_client_CA();
  41328. test_wolfSSL_CTX_set_srp_username();
  41329. test_wolfSSL_CTX_set_srp_password();
  41330. test_wolfSSL_CTX_set_keylog_callback();
  41331. test_wolfSSL_CTX_get_keylog_callback();
  41332. test_wolfSSL_Tls12_Key_Logging_test();
  41333. test_wolfSSL_Tls13_Key_Logging_test();
  41334. test_wolfSSL_CTX_set_ecdh_auto();
  41335. test_wolfSSL_THREADID_hash();
  41336. test_wolfSSL_RAND_set_rand_method();
  41337. test_wolfSSL_RAND_bytes();
  41338. test_wolfSSL_BN_rand();
  41339. test_wolfSSL_pseudo_rand();
  41340. test_wolfSSL_PKCS8_Compat();
  41341. test_wolfSSL_PKCS8_d2i();
  41342. test_error_queue_per_thread();
  41343. test_wolfSSL_ERR_put_error();
  41344. #ifndef NO_BIO
  41345. test_wolfSSL_ERR_print_errors();
  41346. #endif
  41347. test_wolfSSL_HMAC();
  41348. test_wolfSSL_CMAC();
  41349. test_wolfSSL_OBJ();
  41350. test_wolfSSL_i2a_ASN1_OBJECT();
  41351. test_wolfSSL_OBJ_cmp();
  41352. test_wolfSSL_OBJ_txt2nid();
  41353. test_wolfSSL_OBJ_txt2obj();
  41354. test_wolfSSL_i2t_ASN1_OBJECT();
  41355. test_wolfSSL_X509_NAME_ENTRY();
  41356. test_wolfSSL_X509_set_name();
  41357. test_wolfSSL_X509_set_notAfter();
  41358. test_wolfSSL_X509_set_notBefore();
  41359. test_wolfSSL_X509_set_version();
  41360. #ifndef NO_BIO
  41361. test_wolfSSL_BIO_gets();
  41362. test_wolfSSL_BIO_puts();
  41363. test_wolfSSL_BIO_should_retry();
  41364. test_wolfSSL_d2i_PUBKEY();
  41365. test_wolfSSL_BIO_write();
  41366. test_wolfSSL_BIO_connect();
  41367. test_wolfSSL_BIO_accept();
  41368. test_wolfSSL_BIO_printf();
  41369. test_wolfSSL_BIO_f_md();
  41370. #endif
  41371. test_wolfSSL_cert_cb();
  41372. test_wolfSSL_SESSION();
  41373. test_wolfSSL_ticket_keys();
  41374. test_wolfSSL_DES_ecb_encrypt();
  41375. test_wolfSSL_sk_GENERAL_NAME();
  41376. test_wolfSSL_GENERAL_NAME_print();
  41377. test_wolfSSL_MD4();
  41378. test_wolfSSL_RSA();
  41379. test_wolfSSL_RSA_DER();
  41380. test_wolfSSL_RSA_get0_key();
  41381. test_wolfSSL_RSA_meth();
  41382. test_wolfSSL_verify_mode();
  41383. test_wolfSSL_verify_depth();
  41384. test_wolfSSL_HMAC_CTX();
  41385. test_wolfSSL_msg_callback();
  41386. test_wolfSSL_SHA();
  41387. test_wolfSSL_DH_1536_prime();
  41388. test_wolfSSL_PEM_write_DHparams();
  41389. test_wolfSSL_AES_ecb_encrypt();
  41390. test_wolfSSL_MD5();
  41391. test_wolfSSL_MD5_Transform();
  41392. test_wolfSSL_SHA_Transform();
  41393. test_wolfSSL_SHA256();
  41394. test_wolfSSL_SHA256_Transform();
  41395. test_wolfSSL_SHA224();
  41396. test_wolfSSL_SHA512_Transform();
  41397. test_wolfSSL_X509_get_serialNumber();
  41398. test_wolfSSL_X509_CRL();
  41399. test_wolfSSL_d2i_X509_REQ();
  41400. test_wolfSSL_PEM_read_X509();
  41401. test_wolfSSL_PEM_read();
  41402. #ifndef NO_BIO
  41403. test_wolfSSL_PEM_X509_INFO_read_bio();
  41404. test_wolfSSL_PEM_read_bio_ECPKParameters();
  41405. #endif
  41406. test_wolfSSL_X509_STORE_get1_certs();
  41407. test_wolfSSL_X509_NAME_ENTRY_get_object();
  41408. test_wolfSSL_OpenSSL_add_all_algorithms();
  41409. test_wolfSSL_OPENSSL_hexstr2buf();
  41410. test_wolfSSL_ASN1_STRING_print_ex();
  41411. test_wolfSSL_ASN1_TIME_to_generalizedtime();
  41412. test_wolfSSL_ASN1_INTEGER_get_set();
  41413. test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS();
  41414. test_wolfSSL_i2c_ASN1_INTEGER();
  41415. test_wolfSSL_X509_check_ca();
  41416. test_wolfSSL_X509_check_ip_asc();
  41417. test_wolfSSL_DC_cert();
  41418. test_wolfSSL_DES_ncbc();
  41419. test_wolfSSL_AES_cbc_encrypt();
  41420. test_wolfssl_EVP_aes_gcm_AAD_2_parts();
  41421. test_wolfssl_EVP_aes_gcm();
  41422. test_wolfSSL_PKEY_up_ref();
  41423. test_wolfSSL_EVP_Cipher_extra();
  41424. test_wolfSSL_d2i_and_i2d_PublicKey();
  41425. test_wolfSSL_d2i_and_i2d_DSAparams();
  41426. test_wolfSSL_i2d_PrivateKey();
  41427. test_wolfSSL_OCSP_id_get0_info();
  41428. test_wolfSSL_i2d_OCSP_CERTID();
  41429. test_wolfSSL_OCSP_id_cmp();
  41430. test_wolfSSL_OCSP_SINGLERESP_get0_id();
  41431. test_wolfSSL_OCSP_single_get0_status();
  41432. test_wolfSSL_OCSP_resp_count();
  41433. test_wolfSSL_OCSP_resp_get0();
  41434. test_wolfSSL_EVP_PKEY_derive();
  41435. #ifndef NO_RSA
  41436. test_wolfSSL_RSA_padding_add_PKCS1_PSS();
  41437. #endif
  41438. test_wolfSSL_RSA_sign_sha3();
  41439. test_CONF_modules_xxx();
  41440. test_CRYPTO_set_dynlock_xxx();
  41441. test_CRYPTO_THREADID_xxx();
  41442. test_ENGINE_cleanup();
  41443. test_wolfSSL_EC_KEY_set_group();
  41444. #if defined(OPENSSL_ALL)
  41445. test_wolfSSL_X509_PUBKEY_get();
  41446. test_wolfSSL_sk_CIPHER_description();
  41447. test_wolfSSL_get_ciphers_compat();
  41448. test_wolfSSL_d2i_DHparams();
  41449. test_wolfSSL_i2d_DHparams();
  41450. test_wolfSSL_ASN1_STRING_to_UTF8();
  41451. test_wolfSSL_ASN1_UNIVERSALSTRING_to_string();
  41452. test_wolfSSL_EC_KEY_dup();
  41453. test_wolfSSL_EVP_PKEY_set1_get1_DSA();
  41454. test_wolfSSL_DSA_SIG();
  41455. test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY();
  41456. test_wolfSSL_EVP_PKEY_set1_get1_DH();
  41457. test_wolfSSL_CTX_ctrl();
  41458. test_wolfSSL_DH_check();
  41459. test_wolfSSL_EVP_PKEY_assign();
  41460. test_wolfSSL_EVP_PKEY_base_id();
  41461. test_wolfSSL_EVP_PKEY_id();
  41462. test_wolfSSL_EVP_PKEY_keygen();
  41463. test_wolfSSL_EVP_PKEY_keygen_init();
  41464. test_wolfSSL_EVP_PKEY_missing_parameters();
  41465. test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits();
  41466. test_wolfSSL_EVP_CIPHER_CTX_iv_length();
  41467. test_wolfSSL_EVP_CIPHER_CTX_key_length();
  41468. test_wolfSSL_EVP_CIPHER_CTX_set_key_length();
  41469. test_wolfSSL_EVP_CIPHER_CTX_set_iv();
  41470. test_wolfSSL_EVP_PKEY_CTX_new_id();
  41471. test_wolfSSL_EVP_rc4();
  41472. test_wolfSSL_EVP_enc_null();
  41473. test_wolfSSL_EVP_rc2_cbc();
  41474. test_wolfSSL_EVP_mdc2();
  41475. test_wolfSSL_EVP_md4();
  41476. test_wolfSSL_EVP_aes_256_gcm();
  41477. test_wolfSSL_EVP_aes_192_gcm();
  41478. test_wolfSSL_EVP_ripemd160();
  41479. test_wolfSSL_EVP_get_digestbynid();
  41480. test_wolfSSL_EVP_PKEY_get0_EC_KEY();
  41481. test_wolfSSL_EVP_X_STATE();
  41482. test_wolfSSL_EVP_X_STATE_LEN();
  41483. test_wolfSSL_EVP_CIPHER_block_size();
  41484. test_wolfSSL_EVP_CIPHER_iv_length();
  41485. test_wolfSSL_EVP_SignInit_ex();
  41486. test_wolfSSL_EVP_DigestFinal_ex();
  41487. test_wolfSSL_EVP_PKEY_assign_DH();
  41488. test_wolfSSL_EVP_BytesToKey();
  41489. test_wolfSSL_EVP_PKEY_param_check();
  41490. test_wolfSSL_QT_EVP_PKEY_CTX_free();
  41491. test_IncCtr();
  41492. test_wolfSSL_OBJ_ln();
  41493. test_wolfSSL_OBJ_sn();
  41494. test_wolfSSL_TXT_DB();
  41495. test_wolfSSL_NCONF();
  41496. #endif /* OPENSSL_ALL */
  41497. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  41498. AssertIntEQ(test_wolfSSL_CTX_use_certificate_ASN1(), WOLFSSL_SUCCESS);
  41499. #ifndef NO_BIO
  41500. test_wolfSSL_d2i_PrivateKeys_bio();
  41501. #endif
  41502. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  41503. test_wolfSSL_X509_CA_num();
  41504. test_wolfSSL_X509_get_version();
  41505. #ifndef NO_BIO
  41506. test_wolfSSL_X509_print();
  41507. test_wolfSSL_BIO_get_len();
  41508. #endif
  41509. test_wolfSSL_RSA_verify();
  41510. test_wolfSSL_X509V3_EXT_get();
  41511. test_wolfSSL_X509V3_EXT_nconf();
  41512. test_wolfSSL_X509V3_EXT();
  41513. test_wolfSSL_X509_get_ext();
  41514. test_wolfSSL_X509_get_ext_by_NID();
  41515. test_wolfSSL_X509_get_ext_subj_alt_name();
  41516. test_wolfSSL_X509_get_ext_count();
  41517. test_wolfSSL_X509_EXTENSION_new();
  41518. test_wolfSSL_X509_EXTENSION_get_object();
  41519. test_wolfSSL_X509_EXTENSION_get_data();
  41520. test_wolfSSL_X509_EXTENSION_get_critical();
  41521. test_wolfSSL_X509V3_EXT_print();
  41522. test_wolfSSL_X509_cmp();
  41523. #ifndef NO_BIO
  41524. test_wolfSSL_RSA_print();
  41525. test_wolfSSL_ASN1_STRING_print();
  41526. #endif
  41527. test_wolfSSL_ASN1_get_object();
  41528. test_openssl_generate_key_and_cert();
  41529. test_wolfSSL_EC_get_builtin_curves();
  41530. test_wolfSSL_CRYPTO_memcmp();
  41531. test_wolfSSL_read_detect_TCP_disconnect();
  41532. /* test the no op functions for compatibility */
  41533. test_no_op_functions();
  41534. /* OpenSSL EVP_PKEY API tests */
  41535. test_EVP_PKEY_rsa();
  41536. test_wolfSSL_EVP_PKEY_encrypt();
  41537. test_wolfSSL_EVP_PKEY_sign();
  41538. test_EVP_PKEY_ec();
  41539. test_EVP_PKEY_cmp();
  41540. /* OpenSSL error API tests */
  41541. test_ERR_load_crypto_strings();
  41542. /* OpenSSL sk_X509 API test */
  41543. test_sk_X509();
  41544. /* OpenSSL X509 API test */
  41545. test_X509_get_signature_nid();
  41546. /* OpenSSL X509 REQ API test */
  41547. test_X509_REQ();
  41548. /* OpenSSL PKCS7 API test */
  41549. test_wolfssl_PKCS7();
  41550. test_wolfSSL_PKCS7_SIGNED_new();
  41551. #ifndef NO_BIO
  41552. test_wolfSSL_PEM_write_bio_PKCS7();
  41553. #ifdef HAVE_SMIME
  41554. test_wolfSSL_SMIME_read_PKCS7();
  41555. #endif
  41556. #endif
  41557. /* wolfCrypt ASN tests */
  41558. test_wc_CreateEncryptedPKCS8Key();
  41559. test_wc_GetPkcs8TraditionalOffset();
  41560. test_wc_SetSubjectRaw();
  41561. test_wc_GetSubjectRaw();
  41562. test_wc_SetIssuerRaw();
  41563. test_wc_SetIssueBuffer();
  41564. test_wc_SetSubjectKeyId();
  41565. test_wc_SetSubject();
  41566. test_CheckCertSignature();
  41567. /* wolfCrypt ECC tests */
  41568. test_wc_ecc_get_curve_size_from_name();
  41569. test_wc_ecc_get_curve_id_from_name();
  41570. test_wc_ecc_get_curve_id_from_params();
  41571. #ifdef WOLFSSL_TLS13
  41572. /* TLS v1.3 API tests */
  41573. test_tls13_apis();
  41574. #endif
  41575. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  41576. !defined(WOLFSSL_NO_CLIENT_AUTH))
  41577. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  41578. /* Bad certificate signature tests */
  41579. AssertIntEQ(test_EccSigFailure_cm(), ASN_SIG_CONFIRM_E);
  41580. AssertIntEQ(test_RsaSigFailure_cm(), ASN_SIG_CONFIRM_E);
  41581. #endif /* NO_CERTS */
  41582. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  41583. !defined(NO_OLD_TLS))
  41584. /* public key callback tests */
  41585. test_DhCallbacks();
  41586. #endif
  41587. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  41588. test_export_keying_material();
  41589. #endif /* HAVE_KEYING_MATERIAL */
  41590. test_wolfSSL_CTX_get_min_proto_version();
  41591. test_wolfSSL_security_level();
  41592. test_wolfSSL_SSL_in_init();
  41593. test_wolfSSL_EC_curve();
  41594. test_wolfSSL_CTX_set_timeout();
  41595. test_wolfSSL_OpenSSL_version();
  41596. test_wolfSSL_set_psk_use_session_callback();
  41597. test_CONF_CTX_FILE();
  41598. test_CONF_CTX_CMDLINE();
  41599. test_wolfSSL_CRYPTO_get_ex_new_index();
  41600. test_wolfSSL_DH();
  41601. /*wolfcrypt */
  41602. printf("\n-----------------wolfcrypt unit tests------------------\n");
  41603. AssertFalse(test_wolfCrypt_Init());
  41604. AssertFalse(test_wc_InitMd5());
  41605. AssertFalse(test_wc_Md5Update());
  41606. AssertFalse(test_wc_Md5Final());
  41607. AssertFalse(test_wc_InitSha());
  41608. AssertFalse(test_wc_ShaUpdate());
  41609. AssertFalse(test_wc_ShaFinal());
  41610. AssertFalse(test_wc_InitSha256());
  41611. AssertFalse(test_wc_Sha256Update());
  41612. AssertFalse(test_wc_Sha256Final());
  41613. AssertFalse(test_wc_Sha256FinalRaw());
  41614. AssertFalse(test_wc_Sha256GetFlags());
  41615. AssertFalse(test_wc_Sha256Free());
  41616. AssertFalse(test_wc_Sha256GetHash());
  41617. AssertFalse(test_wc_Sha256Copy());
  41618. AssertFalse(test_wc_InitSha512());
  41619. AssertFalse(test_wc_Sha512Update());
  41620. AssertFalse(test_wc_Sha512Final());
  41621. AssertFalse(test_wc_Sha512GetFlags());
  41622. AssertFalse(test_wc_Sha512FinalRaw());
  41623. AssertFalse(test_wc_Sha512Free());
  41624. AssertFalse(test_wc_Sha512GetHash());
  41625. AssertFalse(test_wc_Sha512Copy());
  41626. AssertFalse(test_wc_InitSha512_224());
  41627. AssertFalse(test_wc_Sha512_224Update());
  41628. AssertFalse(test_wc_Sha512_224Final());
  41629. AssertFalse(test_wc_Sha512_224GetFlags());
  41630. AssertFalse(test_wc_Sha512_224FinalRaw());
  41631. AssertFalse(test_wc_Sha512_224Free());
  41632. AssertFalse(test_wc_Sha512_224GetHash());
  41633. AssertFalse(test_wc_Sha512_224Copy());
  41634. AssertFalse(test_wc_InitSha512_256());
  41635. AssertFalse(test_wc_Sha512_256Update());
  41636. AssertFalse(test_wc_Sha512_256Final());
  41637. AssertFalse(test_wc_Sha512_256GetFlags());
  41638. AssertFalse(test_wc_Sha512_256FinalRaw());
  41639. AssertFalse(test_wc_Sha512_256Free());
  41640. AssertFalse(test_wc_Sha512_256GetHash());
  41641. AssertFalse(test_wc_Sha512_256Copy());
  41642. AssertFalse(test_wc_InitSha384());
  41643. AssertFalse(test_wc_Sha384Update());
  41644. AssertFalse(test_wc_Sha384Final());
  41645. AssertFalse(test_wc_Sha384GetFlags());
  41646. AssertFalse(test_wc_Sha384FinalRaw());
  41647. AssertFalse(test_wc_Sha384Free());
  41648. AssertFalse(test_wc_Sha384GetHash());
  41649. AssertFalse(test_wc_Sha384Copy());
  41650. AssertFalse(test_wc_InitSha224());
  41651. AssertFalse(test_wc_Sha224Update());
  41652. AssertFalse(test_wc_Sha224Final());
  41653. AssertFalse(test_wc_Sha224SetFlags());
  41654. AssertFalse(test_wc_Sha224GetFlags());
  41655. AssertFalse(test_wc_Sha224Free());
  41656. AssertFalse(test_wc_Sha224GetHash());
  41657. AssertFalse(test_wc_Sha224Copy());
  41658. AssertFalse(test_wc_InitBlake2b());
  41659. AssertFalse(test_wc_InitBlake2b_WithKey());
  41660. AssertFalse(test_wc_InitBlake2s_WithKey());
  41661. AssertFalse(test_wc_InitRipeMd());
  41662. AssertFalse(test_wc_RipeMdUpdate());
  41663. AssertFalse(test_wc_RipeMdFinal());
  41664. AssertIntEQ(test_wc_InitSha3(), 0);
  41665. AssertIntEQ(testing_wc_Sha3_Update(), 0);
  41666. AssertIntEQ(test_wc_Sha3_224_Final(), 0);
  41667. AssertIntEQ(test_wc_Sha3_256_Final(), 0);
  41668. AssertIntEQ(test_wc_Sha3_384_Final(), 0);
  41669. AssertIntEQ(test_wc_Sha3_512_Final(), 0);
  41670. AssertIntEQ(test_wc_Sha3_224_Copy(), 0);
  41671. AssertIntEQ(test_wc_Sha3_256_Copy(), 0);
  41672. AssertIntEQ(test_wc_Sha3_384_Copy(), 0);
  41673. AssertIntEQ(test_wc_Sha3_512_Copy(), 0);
  41674. AssertIntEQ(test_wc_Sha3_GetFlags(), 0);
  41675. AssertIntEQ(test_wc_InitShake256(), 0);
  41676. AssertIntEQ(testing_wc_Shake256_Update(), 0);
  41677. AssertIntEQ(test_wc_Shake256_Final(), 0);
  41678. AssertIntEQ(test_wc_Shake256_Copy(), 0);
  41679. AssertIntEQ(test_wc_Shake256Hash(), 0);
  41680. AssertFalse(test_wc_Md5HmacSetKey());
  41681. AssertFalse(test_wc_Md5HmacUpdate());
  41682. AssertFalse(test_wc_Md5HmacFinal());
  41683. AssertFalse(test_wc_ShaHmacSetKey());
  41684. AssertFalse(test_wc_ShaHmacUpdate());
  41685. AssertFalse(test_wc_ShaHmacFinal());
  41686. AssertFalse(test_wc_Sha224HmacSetKey());
  41687. AssertFalse(test_wc_Sha224HmacUpdate());
  41688. AssertFalse(test_wc_Sha224HmacFinal());
  41689. AssertFalse(test_wc_Sha256HmacSetKey());
  41690. AssertFalse(test_wc_Sha256HmacUpdate());
  41691. AssertFalse(test_wc_Sha256HmacFinal());
  41692. AssertFalse(test_wc_Sha384HmacSetKey());
  41693. AssertFalse(test_wc_Sha384HmacUpdate());
  41694. AssertFalse(test_wc_Sha384HmacFinal());
  41695. AssertIntEQ(test_wc_HashInit(), 0);
  41696. AssertIntEQ(test_wc_HashSetFlags(), 0);
  41697. AssertIntEQ(test_wc_HashGetFlags(), 0);
  41698. AssertIntEQ(test_wc_InitCmac(), 0);
  41699. AssertIntEQ(test_wc_CmacUpdate(), 0);
  41700. AssertIntEQ(test_wc_CmacFinal(), 0);
  41701. AssertIntEQ(test_wc_AesCmacGenerate(), 0);
  41702. AssertIntEQ(test_wc_AesGcmStream(), 0);
  41703. AssertIntEQ(test_wc_Des3_SetIV(), 0);
  41704. AssertIntEQ(test_wc_Des3_SetKey(), 0);
  41705. AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
  41706. AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
  41707. AssertIntEQ(test_wc_Des3_EcbEncrypt(), 0);
  41708. AssertIntEQ(test_wc_IdeaSetKey(), 0);
  41709. AssertIntEQ(test_wc_IdeaSetIV(), 0);
  41710. AssertIntEQ(test_wc_IdeaCipher(), 0);
  41711. AssertIntEQ(test_wc_IdeaCbcEncyptDecrypt(), 0);
  41712. AssertIntEQ(test_wc_Chacha_SetKey(), 0);
  41713. AssertIntEQ(test_wc_Chacha_Process(), 0);
  41714. AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0);
  41715. AssertIntEQ(test_wc_Poly1305SetKey(), 0);
  41716. AssertIntEQ(test_wc_CamelliaSetKey(), 0);
  41717. AssertIntEQ(test_wc_CamelliaSetIV(), 0);
  41718. AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0);
  41719. AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0);
  41720. AssertIntEQ(test_wc_RabbitSetKey(), 0);
  41721. AssertIntEQ(test_wc_RabbitProcess(), 0);
  41722. AssertIntEQ(test_wc_Arc4SetKey(), 0);
  41723. AssertIntEQ(test_wc_Arc4Process(), 0);
  41724. AssertIntEQ(test_wc_Rc2SetKey(), 0);
  41725. AssertIntEQ(test_wc_Rc2SetIV(), 0);
  41726. AssertIntEQ(test_wc_Rc2EcbEncryptDecrypt(), 0);
  41727. AssertIntEQ(test_wc_Rc2CbcEncryptDecrypt(), 0);
  41728. AssertIntEQ(test_wc_AesSetKey(), 0);
  41729. AssertIntEQ(test_wc_AesSetIV(), 0);
  41730. AssertIntEQ(test_wc_AesCbcEncryptDecrypt(), 0);
  41731. AssertIntEQ(test_wc_AesCtrEncryptDecrypt(), 0);
  41732. AssertIntEQ(test_wc_AesGcmSetKey(), 0);
  41733. AssertIntEQ(test_wc_AesGcmEncryptDecrypt(), 0);
  41734. AssertIntEQ(test_wc_GmacSetKey(), 0);
  41735. AssertIntEQ(test_wc_GmacUpdate(), 0);
  41736. AssertIntEQ(test_wc_InitRsaKey(), 0);
  41737. AssertIntEQ(test_wc_RsaPrivateKeyDecode(), 0);
  41738. AssertIntEQ(test_wc_RsaPublicKeyDecode(), 0);
  41739. AssertIntEQ(test_wc_RsaPublicKeyDecodeRaw(), 0);
  41740. AssertIntEQ(test_wc_MakeRsaKey(), 0);
  41741. AssertIntEQ(test_wc_SetKeyUsage (), 0);
  41742. AssertIntEQ(test_wc_CheckProbablePrime (), 0);
  41743. AssertIntEQ(test_wc_RsaPSS_Verify (), 0);
  41744. AssertIntEQ(test_wc_RsaPSS_VerifyCheck (), 0);
  41745. AssertIntEQ(test_wc_RsaPSS_VerifyCheckInline (), 0);
  41746. AssertIntEQ(test_wc_SetMutexCb(), 0);
  41747. AssertIntEQ(test_wc_LockMutex_ex(), 0);
  41748. AssertIntEQ(test_wc_RsaKeyToDer(), 0);
  41749. AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0);
  41750. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0);
  41751. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt_ex(), 0);
  41752. AssertIntEQ(test_wc_RsaEncryptSize(), 0);
  41753. AssertIntEQ(test_wc_RsaSSL_SignVerify(), 0);
  41754. AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0);
  41755. AssertIntEQ(test_RsaDecryptBoundsCheck(), 0);
  41756. AssertIntEQ(test_wc_AesCcmSetKey(), 0);
  41757. AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0);
  41758. AssertIntEQ(test_wc_Hc128_SetKey(), 0);
  41759. AssertIntEQ(test_wc_Hc128_Process(), 0);
  41760. AssertIntEQ(test_wc_InitDsaKey(), 0);
  41761. AssertIntEQ(test_wc_DsaSignVerify(), 0);
  41762. AssertIntEQ(test_wc_DsaPublicPrivateKeyDecode(), 0);
  41763. AssertIntEQ(test_wc_MakeDsaKey(), 0);
  41764. AssertIntEQ(test_wc_DsaKeyToDer(), 0);
  41765. AssertIntEQ(test_wc_DsaKeyToPublicDer(), 0);
  41766. AssertIntEQ(test_wc_DsaImportParamsRaw(), 0);
  41767. AssertIntEQ(test_wc_DsaImportParamsRawCheck(), 0);
  41768. AssertIntEQ(test_wc_DsaExportParamsRaw(), 0);
  41769. AssertIntEQ(test_wc_DsaExportKeyRaw(), 0);
  41770. AssertIntEQ(test_wc_SignatureGetSize_ecc(), 0);
  41771. AssertIntEQ(test_wc_SignatureGetSize_rsa(), 0);
  41772. wolfCrypt_Cleanup();
  41773. #ifdef OPENSSL_EXTRA
  41774. /*wolfSSL_EVP_get_cipherbynid test*/
  41775. test_wolfSSL_EVP_get_cipherbynid();
  41776. test_wolfSSL_EVP_CIPHER_CTX();
  41777. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41778. test_wolfSSL_EC();
  41779. #endif
  41780. test_wolfSSL_ECDSA_SIG();
  41781. test_ECDSA_size_sign();
  41782. test_ED25519();
  41783. test_ED448();
  41784. test_EC_i2d();
  41785. #endif
  41786. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  41787. !defined(HAVE_SELFTEST) && \
  41788. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  41789. test_wc_ecc_get_curve_id_from_dp_params();
  41790. #endif
  41791. #ifdef HAVE_HASHDRBG
  41792. #ifdef TEST_RESEED_INTERVAL
  41793. AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0);
  41794. #endif
  41795. AssertIntEQ(test_wc_RNG_GenerateBlock(), 0);
  41796. #endif
  41797. AssertIntEQ(test_get_rand_digit(), 0);
  41798. AssertIntEQ(test_get_digit_count(), 0);
  41799. AssertIntEQ(test_mp_cond_copy(), 0);
  41800. AssertIntEQ(test_mp_rand(), 0);
  41801. AssertIntEQ(test_get_digit(), 0);
  41802. AssertIntEQ(test_wc_export_int(), 0);
  41803. AssertIntEQ(test_wc_InitRngNonce(), 0);
  41804. AssertIntEQ(test_wc_InitRngNonce_ex(), 0);
  41805. AssertIntEQ(test_wc_ed25519_make_key(), 0);
  41806. AssertIntEQ(test_wc_ed25519_init(), 0);
  41807. AssertIntEQ(test_wc_ed25519_sign_msg(), 0);
  41808. AssertIntEQ(test_wc_ed25519_import_public(), 0);
  41809. AssertIntEQ(test_wc_ed25519_import_private_key(), 0);
  41810. AssertIntEQ(test_wc_ed25519_export(), 0);
  41811. AssertIntEQ(test_wc_ed25519_size(), 0);
  41812. AssertIntEQ(test_wc_ed25519_exportKey(), 0);
  41813. AssertIntEQ(test_wc_Ed25519PublicKeyToDer(), 0);
  41814. AssertIntEQ(test_wc_curve25519_init(), 0);
  41815. AssertIntEQ(test_wc_curve25519_size(), 0);
  41816. AssertIntEQ(test_wc_curve25519_export_key_raw(), 0);
  41817. AssertIntEQ(test_wc_curve25519_export_key_raw_ex(), 0);
  41818. AssertIntEQ(test_wc_curve25519_size (), 0);
  41819. AssertIntEQ(test_wc_curve25519_make_key (), 0);
  41820. AssertIntEQ(test_wc_curve25519_shared_secret_ex (), 0);
  41821. AssertIntEQ(test_wc_curve25519_make_pub (), 0);
  41822. AssertIntEQ(test_wc_curve25519_export_public_ex (), 0);
  41823. AssertIntEQ(test_wc_curve25519_export_private_raw_ex (), 0);
  41824. AssertIntEQ(test_wc_curve25519_import_private_raw_ex (), 0);
  41825. AssertIntEQ(test_wc_curve25519_import_private (), 0);
  41826. AssertIntEQ(test_wc_ed448_make_key(), 0);
  41827. AssertIntEQ(test_wc_ed448_init(), 0);
  41828. AssertIntEQ(test_wc_ed448_sign_msg(), 0);
  41829. AssertIntEQ(test_wc_ed448_import_public(), 0);
  41830. AssertIntEQ(test_wc_ed448_import_private_key(), 0);
  41831. AssertIntEQ(test_wc_ed448_export(), 0);
  41832. AssertIntEQ(test_wc_ed448_size(), 0);
  41833. AssertIntEQ(test_wc_ed448_exportKey(), 0);
  41834. AssertIntEQ(test_wc_Ed448PublicKeyToDer(), 0);
  41835. AssertIntEQ(test_wc_curve448_make_key (), 0);
  41836. AssertIntEQ(test_wc_curve448_shared_secret_ex (), 0);
  41837. AssertIntEQ(test_wc_curve448_export_public_ex (), 0);
  41838. AssertIntEQ(test_wc_curve448_export_private_raw_ex (), 0);
  41839. AssertIntEQ(test_wc_curve448_export_key_raw (), 0);
  41840. AssertIntEQ(test_wc_curve448_import_private_raw_ex (), 0);
  41841. AssertIntEQ(test_wc_curve448_import_private (), 0);
  41842. AssertIntEQ(test_wc_curve448_init(), 0);
  41843. AssertIntEQ(test_wc_curve448_size (), 0);
  41844. AssertIntEQ(test_wc_ecc_make_key(), 0);
  41845. AssertIntEQ(test_wc_ecc_init(), 0);
  41846. AssertIntEQ(test_wc_ecc_check_key(), 0);
  41847. AssertIntEQ(test_wc_ecc_get_generator(), 0);
  41848. AssertIntEQ(test_wc_ecc_size(), 0);
  41849. test_wc_ecc_params();
  41850. AssertIntEQ(test_wc_ecc_signVerify_hash(), 0);
  41851. AssertIntEQ(test_wc_ecc_shared_secret(), 0);
  41852. AssertIntEQ(test_wc_ecc_export_x963(), 0);
  41853. AssertIntEQ(test_wc_ecc_export_x963_ex(), 0);
  41854. AssertIntEQ(test_wc_ecc_import_x963(), 0);
  41855. AssertIntEQ(ecc_import_private_key(), 0);
  41856. AssertIntEQ(test_wc_ecc_export_private_only(), 0);
  41857. AssertIntEQ(test_wc_ecc_rs_to_sig(), 0);
  41858. AssertIntEQ(test_wc_ecc_import_raw(), 0);
  41859. AssertIntEQ(test_wc_ecc_import_unsigned(), 0);
  41860. AssertIntEQ(test_wc_ecc_sig_size(), 0);
  41861. AssertIntEQ(test_wc_ecc_ctx_new(), 0);
  41862. AssertIntEQ(test_wc_ecc_ctx_reset(), 0);
  41863. AssertIntEQ(test_wc_ecc_ctx_set_peer_salt(), 0);
  41864. AssertIntEQ(test_wc_ecc_ctx_set_info(), 0);
  41865. AssertIntEQ(test_wc_ecc_encryptDecrypt(), 0);
  41866. AssertIntEQ(test_wc_ecc_del_point(), 0);
  41867. AssertIntEQ(test_wc_ecc_pointFns(), 0);
  41868. AssertIntEQ(test_wc_ecc_shared_secret_ssh(), 0);
  41869. AssertIntEQ(test_wc_ecc_verify_hash_ex(), 0);
  41870. AssertIntEQ(test_wc_ecc_mulmod(), 0);
  41871. AssertIntEQ(test_wc_ecc_is_valid_idx(), 0);
  41872. AssertIntEQ(test_wc_ecc_get_curve_id_from_oid(), 0);
  41873. AssertIntEQ(test_wc_ecc_sig_size_calc(), 0);
  41874. AssertIntEQ(test_ToTraditional(), 0);
  41875. AssertIntEQ(test_wc_EccPrivateKeyToDer(), 0);
  41876. AssertIntEQ(test_wc_DhPublicKeyDecode(), 0);
  41877. AssertIntEQ(test_wc_Ed25519KeyToDer(), 0);
  41878. AssertIntEQ(test_wc_Ed25519PrivateKeyToDer(), 0);
  41879. AssertIntEQ(test_wc_Ed448KeyToDer(), 0);
  41880. AssertIntEQ(test_wc_Ed448PrivateKeyToDer(), 0);
  41881. AssertIntEQ(test_wc_SetAuthKeyIdFromPublicKey_ex(), 0);
  41882. AssertIntEQ(test_wc_SetSubjectBuffer(), 0);
  41883. AssertIntEQ(test_wc_SetSubjectKeyIdFromPublicKey_ex(), 0);
  41884. test_wc_PKCS7_New();
  41885. test_wc_PKCS7_Init();
  41886. test_wc_PKCS7_InitWithCert();
  41887. test_wc_PKCS7_EncodeData();
  41888. test_wc_PKCS7_EncodeSignedData();
  41889. test_wc_PKCS7_EncodeSignedData_ex();
  41890. test_wc_PKCS7_VerifySignedData();
  41891. test_wc_PKCS7_EncodeDecodeEnvelopedData();
  41892. test_wc_PKCS7_EncodeEncryptedData();
  41893. test_wc_PKCS7_Degenerate();
  41894. test_wc_PKCS7_BER();
  41895. test_PKCS7_signed_enveloped();
  41896. test_wc_PKCS7_NoDefaultSignedAttribs();
  41897. test_wc_PKCS7_SetOriEncryptCtx();
  41898. test_wc_PKCS7_SetOriDecryptCtx();
  41899. test_wc_PKCS7_DecodeCompressedData();
  41900. test_wc_i2d_PKCS12();
  41901. test_wolfSSL_CTX_LoadCRL();
  41902. AssertIntEQ(test_ForceZero(), 0);
  41903. AssertIntEQ(test_wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  41904. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  41905. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  41906. !defined(WOLFSSL_NO_CLIENT_AUTH))
  41907. AssertIntEQ(test_various_pathlen_chains(), WOLFSSL_SUCCESS);
  41908. #endif
  41909. /* If at some point a stub get implemented this test should fail indicating
  41910. * a need to implement a new test case
  41911. */
  41912. test_stubs_are_stubs();
  41913. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  41914. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  41915. wc_ecc_fp_free(); /* free per thread cache */
  41916. #endif
  41917. wolfSSL_Cleanup();
  41918. (void)devId;
  41919. printf(" End API Tests\n");
  41920. }