api.c 1.6 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506
  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/hmac.h>
  281. #include <wolfssl/openssl/objects.h>
  282. #include <wolfssl/openssl/rand.h>
  283. #ifdef OPENSSL_ALL
  284. #include <wolfssl/openssl/txt_db.h>
  285. #include <wolfssl/openssl/lhash.h>
  286. #endif
  287. #ifndef NO_AES
  288. #include <wolfssl/openssl/aes.h>
  289. #endif
  290. #ifndef NO_DES3
  291. #include <wolfssl/openssl/des.h>
  292. #endif
  293. #ifdef HAVE_ECC
  294. #include <wolfssl/openssl/ecdsa.h>
  295. #endif
  296. #ifdef HAVE_PKCS7
  297. #include <wolfssl/openssl/pkcs7.h>
  298. #endif
  299. #ifdef HAVE_ED25519
  300. #include <wolfssl/openssl/ed25519.h>
  301. #endif
  302. #ifdef HAVE_ED448
  303. #include <wolfssl/openssl/ed448.h>
  304. #endif
  305. #endif /* OPENSSL_EXTRA */
  306. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  307. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  308. #include <wolfssl/wolfcrypt/srp.h>
  309. #endif
  310. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  311. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  312. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && \
  313. !defined(WOLFSSL_ASN_TEMPLATE))
  314. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  315. * or for setting authKeyIdSrc in WOLFSSL_X509 */
  316. #include "wolfssl/internal.h"
  317. #endif
  318. /* force enable test buffers */
  319. #ifndef USE_CERT_BUFFERS_2048
  320. #define USE_CERT_BUFFERS_2048
  321. #endif
  322. #ifndef USE_CERT_BUFFERS_256
  323. #define USE_CERT_BUFFERS_256
  324. #endif
  325. #include <wolfssl/certs_test.h>
  326. typedef struct testVector {
  327. const char* input;
  328. const char* output;
  329. size_t inLen;
  330. size_t outLen;
  331. } testVector;
  332. #if defined(HAVE_PKCS7)
  333. typedef struct {
  334. const byte* content;
  335. word32 contentSz;
  336. int contentOID;
  337. int encryptOID;
  338. int keyWrapOID;
  339. int keyAgreeOID;
  340. byte* cert;
  341. size_t certSz;
  342. byte* privateKey;
  343. word32 privateKeySz;
  344. } pkcs7EnvelopedVector;
  345. #ifndef NO_PKCS7_ENCRYPTED_DATA
  346. typedef struct {
  347. const byte* content;
  348. word32 contentSz;
  349. int contentOID;
  350. int encryptOID;
  351. byte* encryptionKey;
  352. word32 encryptionKeySz;
  353. } pkcs7EncryptedVector;
  354. #endif
  355. #endif /* HAVE_PKCS7 */
  356. /*----------------------------------------------------------------------------*
  357. | Constants
  358. *----------------------------------------------------------------------------*/
  359. #define TEST_SUCCESS (1)
  360. #define TEST_FAIL (0)
  361. #define testingFmt " %s:"
  362. #define resultFmt " %s\n"
  363. static const char* passed = "passed";
  364. static const char* failed = "failed";
  365. #define TEST_STRING "Everyone gets Friday off."
  366. #define TEST_STRING_SZ 25
  367. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  368. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  369. #define TEST_RSA_BITS 1024
  370. #else
  371. #define TEST_RSA_BITS 2048
  372. #endif
  373. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  374. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  375. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  376. static const char* bogusFile =
  377. #ifdef _WIN32
  378. "NUL"
  379. #else
  380. "/dev/null"
  381. #endif
  382. ;
  383. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  384. enum {
  385. TESTING_RSA = 1,
  386. TESTING_ECC = 2
  387. };
  388. #ifdef WOLFSSL_QNX_CAAM
  389. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  390. static int devId = WOLFSSL_CAAM_DEVID;
  391. #else
  392. static int devId = INVALID_DEVID;
  393. #endif
  394. /*----------------------------------------------------------------------------*
  395. | Setup
  396. *----------------------------------------------------------------------------*/
  397. static int test_wolfSSL_Init(void)
  398. {
  399. int result;
  400. printf(testingFmt, "wolfSSL_Init()");
  401. result = wolfSSL_Init();
  402. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  403. return result;
  404. }
  405. static int test_wolfSSL_Cleanup(void)
  406. {
  407. int result;
  408. printf(testingFmt, "wolfSSL_Cleanup()");
  409. result = wolfSSL_Cleanup();
  410. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  411. return result;
  412. }
  413. /* Initialize the wolfCrypt state.
  414. * POST: 0 success.
  415. */
  416. static int test_wolfCrypt_Init(void)
  417. {
  418. int result;
  419. printf(testingFmt, "wolfCrypt_Init()");
  420. result = wolfCrypt_Init();
  421. printf(resultFmt, result == 0 ? passed : failed);
  422. return result;
  423. } /* END test_wolfCrypt_Init */
  424. /*----------------------------------------------------------------------------*
  425. | Platform dependent function test
  426. *----------------------------------------------------------------------------*/
  427. static int test_fileAccess(void)
  428. {
  429. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  430. const char *fname[] = {
  431. svrCertFile, svrKeyFile, caCertFile,
  432. eccCertFile, eccKeyFile, eccRsaCertFile,
  433. cliCertFile, cliCertDerFile, cliKeyFile,
  434. dhParamFile,
  435. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  436. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  437. NULL
  438. };
  439. const char derfile[] = "./certs/server-cert.der";
  440. XFILE f;
  441. size_t sz;
  442. byte *buff;
  443. int i;
  444. printf(testingFmt, "test_fileAccess()");
  445. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  446. for(i=0; fname[i] != NULL ; i++){
  447. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  448. XFCLOSE(f);
  449. }
  450. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  451. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  452. sz = (size_t) XFTELL(f);
  453. XREWIND(f);
  454. AssertTrue(sz == sizeof_server_cert_der_2048);
  455. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  456. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  457. XMEMCMP(server_cert_der_2048, buff, sz);
  458. printf(resultFmt, passed);
  459. #endif
  460. return WOLFSSL_SUCCESS;
  461. }
  462. /*----------------------------------------------------------------------------*
  463. | Method Allocators
  464. *----------------------------------------------------------------------------*/
  465. static void test_wolfSSL_Method_Allocators(void)
  466. {
  467. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  468. do { \
  469. WOLFSSL_METHOD *method; \
  470. condition(method = allocator()); \
  471. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  472. } while(0)
  473. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  474. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  475. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  476. TEST_METHOD_ALLOCATOR(a, AssertNull)
  477. #ifndef NO_OLD_TLS
  478. #ifdef WOLFSSL_ALLOW_SSLV3
  479. #ifndef NO_WOLFSSL_SERVER
  480. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  481. #endif
  482. #ifndef NO_WOLFSSL_CLIENT
  483. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  484. #endif
  485. #endif
  486. #ifdef WOLFSSL_ALLOW_TLSV10
  487. #ifndef NO_WOLFSSL_SERVER
  488. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  489. #endif
  490. #ifndef NO_WOLFSSL_CLIENT
  491. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  492. #endif
  493. #endif
  494. #ifndef NO_WOLFSSL_SERVER
  495. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  496. #endif
  497. #ifndef NO_WOLFSSL_CLIENT
  498. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  499. #endif
  500. #endif /* !NO_OLD_TLS */
  501. #ifndef WOLFSSL_NO_TLS12
  502. #ifndef NO_WOLFSSL_SERVER
  503. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  504. #endif
  505. #ifndef NO_WOLFSSL_CLIENT
  506. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  507. #endif
  508. #endif /* !WOLFSSL_NO_TLS12 */
  509. #ifdef WOLFSSL_TLS13
  510. #ifndef NO_WOLFSSL_SERVER
  511. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  512. #endif
  513. #ifndef NO_WOLFSSL_CLIENT
  514. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  515. #endif
  516. #endif /* WOLFSSL_TLS13 */
  517. #ifndef NO_WOLFSSL_SERVER
  518. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  519. #endif
  520. #ifndef NO_WOLFSSL_CLIENT
  521. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  522. #endif
  523. #ifdef WOLFSSL_DTLS
  524. #ifndef NO_OLD_TLS
  525. #ifndef NO_WOLFSSL_SERVER
  526. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  527. #endif
  528. #ifndef NO_WOLFSSL_CLIENT
  529. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  530. #endif
  531. #endif
  532. #ifndef WOLFSSL_NO_TLS12
  533. #ifndef NO_WOLFSSL_SERVER
  534. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  535. #endif
  536. #ifndef NO_WOLFSSL_CLIENT
  537. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  538. #endif
  539. #endif
  540. #endif /* WOLFSSL_DTLS */
  541. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  542. /* Stubs */
  543. #ifndef NO_WOLFSSL_SERVER
  544. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  545. #endif
  546. #ifndef NO_WOLFSSL_CLIENT
  547. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  548. #endif
  549. #endif
  550. /* Test Either Method (client or server) */
  551. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  552. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  553. #ifndef NO_OLD_TLS
  554. #ifdef WOLFSSL_ALLOW_TLSV10
  555. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  556. #endif
  557. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  558. #endif /* !NO_OLD_TLS */
  559. #ifndef WOLFSSL_NO_TLS12
  560. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  561. #endif /* !WOLFSSL_NO_TLS12 */
  562. #ifdef WOLFSSL_TLS13
  563. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  564. #endif /* WOLFSSL_TLS13 */
  565. #ifdef WOLFSSL_DTLS
  566. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  567. #ifndef NO_OLD_TLS
  568. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  569. #endif /* !NO_OLD_TLS */
  570. #ifndef WOLFSSL_NO_TLS12
  571. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  572. #endif /* !WOLFSSL_NO_TLS12 */
  573. #endif /* WOLFSSL_DTLS */
  574. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  575. }
  576. /*----------------------------------------------------------------------------*
  577. | Context
  578. *----------------------------------------------------------------------------*/
  579. #ifndef NO_WOLFSSL_SERVER
  580. static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
  581. {
  582. WOLFSSL_CTX *ctx;
  583. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  584. AssertNotNull(method);
  585. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  586. wolfSSL_CTX_free(ctx);
  587. }
  588. #endif
  589. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  590. (!defined(NO_RSA) || defined(HAVE_ECC))
  591. static void test_for_double_Free(void)
  592. {
  593. WOLFSSL_CTX* ctx;
  594. WOLFSSL* ssl;
  595. int skipTest = 0;
  596. const char* testCertFile;
  597. const char* testKeyFile;
  598. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  599. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  600. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  601. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  602. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  603. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  604. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  605. "NULL-SHA:HC128-MD5:HC128-SHA:RABBIT-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  606. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  607. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  608. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  609. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  610. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  611. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  612. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  613. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  614. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  615. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  616. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  617. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  618. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  619. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  620. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  621. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  622. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  623. "LY1305-OLD:IDEA-CBC-SHA:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  624. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  625. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  626. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  627. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  628. #ifndef NO_RSA
  629. testCertFile = svrCertFile;
  630. testKeyFile = svrKeyFile;
  631. #elif defined(HAVE_ECC)
  632. testCertFile = eccCertFile;
  633. testKeyFile = eccKeyFile;
  634. #else
  635. skipTest = 1;
  636. #endif
  637. if (skipTest != 1) {
  638. #ifndef NO_WOLFSSL_SERVER
  639. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  640. AssertNotNull(ctx);
  641. #else
  642. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  643. AssertNotNull(ctx);
  644. #endif
  645. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  646. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  647. ssl = wolfSSL_new(ctx);
  648. AssertNotNull(ssl);
  649. /* First test freeing SSL, then CTX */
  650. wolfSSL_free(ssl);
  651. wolfSSL_CTX_free(ctx);
  652. #ifndef NO_WOLFSSL_CLIENT
  653. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  654. AssertNotNull(ctx);
  655. #else
  656. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  657. AssertNotNull(ctx);
  658. #endif
  659. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  660. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  661. ssl = wolfSSL_new(ctx);
  662. AssertNotNull(ssl);
  663. /* Next test freeing CTX then SSL */
  664. wolfSSL_CTX_free(ctx);
  665. wolfSSL_free(ssl);
  666. #ifndef NO_WOLFSSL_SERVER
  667. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  668. AssertNotNull(ctx);
  669. #else
  670. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  671. AssertNotNull(ctx);
  672. #endif
  673. /* Test setting ciphers at ctx level */
  674. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  675. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  676. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  677. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  678. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  679. /* only update TLSv13 suites */
  680. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  681. #endif
  682. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  683. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  684. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  685. /* only update pre-TLSv13 suites */
  686. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
  687. #endif
  688. AssertNotNull(ssl = wolfSSL_new(ctx));
  689. wolfSSL_CTX_free(ctx);
  690. wolfSSL_free(ssl);
  691. #ifndef NO_WOLFSSL_CLIENT
  692. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  693. AssertNotNull(ctx);
  694. #else
  695. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  696. AssertNotNull(ctx);
  697. #endif
  698. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  699. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  700. ssl = wolfSSL_new(ctx);
  701. AssertNotNull(ssl);
  702. /* test setting ciphers at SSL level */
  703. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  704. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  705. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  706. /* only update TLSv13 suites */
  707. AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  708. #endif
  709. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  710. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  711. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  712. /* only update pre-TLSv13 suites */
  713. AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  714. #endif
  715. wolfSSL_CTX_free(ctx);
  716. wolfSSL_free(ssl);
  717. }
  718. }
  719. #endif
  720. static void test_wolfSSL_CTX_use_certificate_file(void)
  721. {
  722. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  723. WOLFSSL_CTX *ctx;
  724. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  725. /* invalid context */
  726. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  727. WOLFSSL_FILETYPE_PEM));
  728. /* invalid cert file */
  729. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  730. WOLFSSL_FILETYPE_PEM));
  731. /* invalid cert type */
  732. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  733. #ifdef NO_RSA
  734. /* rsa needed */
  735. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  736. #else
  737. /* success */
  738. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  739. #endif
  740. wolfSSL_CTX_free(ctx);
  741. #endif
  742. }
  743. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  744. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  745. {
  746. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  747. WOLFSSL_CTX* ctx;
  748. int ret;
  749. printf(testingFmt, "wolfSSL_CTX_use_certificate_ASN1()");
  750. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  751. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  752. server_cert_der_2048);
  753. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  754. wolfSSL_CTX_free(ctx);
  755. return ret;
  756. #else
  757. return WOLFSSL_SUCCESS;
  758. #endif
  759. }
  760. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  761. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  762. * context using buffer.
  763. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  764. * --enable-testcert flag.
  765. */
  766. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  767. {
  768. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  769. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  770. WOLFSSL_CTX* ctx;
  771. int ret;
  772. printf(testingFmt, "wolfSSL_CTX_use_certificate_buffer()");
  773. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  774. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  775. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  776. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  777. wolfSSL_CTX_free(ctx);
  778. return ret;
  779. #else
  780. return WOLFSSL_SUCCESS;
  781. #endif
  782. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  783. static void test_wolfSSL_CTX_use_PrivateKey_file(void)
  784. {
  785. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  786. WOLFSSL_CTX *ctx;
  787. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  788. /* invalid context */
  789. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  790. WOLFSSL_FILETYPE_PEM));
  791. /* invalid key file */
  792. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  793. WOLFSSL_FILETYPE_PEM));
  794. /* invalid key type */
  795. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  796. /* success */
  797. #ifdef NO_RSA
  798. /* rsa needed */
  799. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  800. #else
  801. /* success */
  802. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  803. #endif
  804. wolfSSL_CTX_free(ctx);
  805. #endif
  806. }
  807. /* test both file and buffer versions along with unloading trusted peer certs */
  808. static void test_wolfSSL_CTX_trust_peer_cert(void)
  809. {
  810. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  811. !defined(NO_WOLFSSL_CLIENT)
  812. WOLFSSL_CTX *ctx;
  813. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  814. #if !defined(NO_FILESYSTEM)
  815. /* invalid file */
  816. assert(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  817. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
  818. assert(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  819. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
  820. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  821. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
  822. /* success */
  823. assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM)
  824. == WOLFSSL_SUCCESS);
  825. /* unload cert */
  826. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
  827. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
  828. #endif
  829. /* Test of loading certs from buffers */
  830. /* invalid buffer */
  831. assert(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  832. WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
  833. /* success */
  834. #ifdef USE_CERT_BUFFERS_1024
  835. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  836. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
  837. #endif
  838. #ifdef USE_CERT_BUFFERS_2048
  839. assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  840. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
  841. #endif
  842. /* unload cert */
  843. assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
  844. assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
  845. wolfSSL_CTX_free(ctx);
  846. #endif
  847. }
  848. static void test_wolfSSL_CTX_load_verify_locations(void)
  849. {
  850. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  851. WOLFSSL_CTX *ctx;
  852. #ifndef NO_RSA
  853. WOLFSSL_CERT_MANAGER* cm;
  854. #ifdef PERSIST_CERT_CACHE
  855. int cacheSz;
  856. #endif
  857. #endif
  858. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  859. const char* load_certs_path = "./certs/external";
  860. const char* load_no_certs_path = "./examples";
  861. const char* load_expired_path = "./certs/test/expired";
  862. #endif
  863. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  864. /* invalid arguments */
  865. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  866. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  867. /* invalid ca file */
  868. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  869. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  870. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  871. (defined(WOLFSSL_QT) && \
  872. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  873. /* invalid path */
  874. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  875. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  876. #endif
  877. /* load ca cert */
  878. #ifdef NO_RSA
  879. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  880. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  881. #else /* Skip the following test without RSA certs. */
  882. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  883. #ifdef PERSIST_CERT_CACHE
  884. /* Get cert cache size */
  885. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  886. #endif
  887. /* Test unloading CA's */
  888. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  889. #ifdef PERSIST_CERT_CACHE
  890. /* Verify no certs (result is less than cacheSz) */
  891. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  892. #endif
  893. /* load ca cert again */
  894. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  895. /* Test getting CERT_MANAGER */
  896. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  897. /* Test unloading CA's using CM */
  898. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  899. #ifdef PERSIST_CERT_CACHE
  900. /* Verify no certs (result is less than cacheSz) */
  901. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  902. #endif
  903. #endif
  904. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  905. /* Test loading CA certificates using a path */
  906. #ifdef NO_RSA
  907. /* failure here okay since certs in external directory are RSA */
  908. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  909. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  910. #else
  911. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  912. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  913. #endif
  914. /* Test loading path with no files */
  915. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  916. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  917. /* Test loading expired CA certificates */
  918. #ifdef NO_RSA
  919. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  920. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  921. WOLFSSL_SUCCESS);
  922. #else
  923. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  924. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  925. WOLFSSL_SUCCESS);
  926. #endif
  927. /* Test loading CA certificates and ignoring all errors */
  928. #ifdef NO_RSA
  929. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  930. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  931. #else
  932. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  933. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  934. #endif
  935. #endif
  936. wolfSSL_CTX_free(ctx);
  937. #endif
  938. }
  939. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  940. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  941. {
  942. int ret;
  943. WOLFSSL_CERT_MANAGER* cm;
  944. cm = wolfSSL_CertManagerNew();
  945. if (cm == NULL) {
  946. printf("test_cm_load_ca failed\n");
  947. return -1;
  948. }
  949. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  950. wolfSSL_CertManagerFree(cm);
  951. return ret;
  952. }
  953. static int test_cm_load_ca_file(const char* ca_cert_file)
  954. {
  955. int ret = 0;
  956. byte* cert_buf = NULL;
  957. size_t cert_sz = 0;
  958. #if defined(WOLFSSL_PEM_TO_DER)
  959. DerBuffer* pDer = NULL;
  960. #endif
  961. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  962. if (ret == 0) {
  963. /* normal test */
  964. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  965. if (ret == WOLFSSL_SUCCESS) {
  966. /* test including null terminator in length */
  967. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  968. if (tmp == NULL) {
  969. ret = MEMORY_E;
  970. }
  971. else {
  972. cert_buf = tmp;
  973. cert_buf[cert_sz] = '\0';
  974. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  975. WOLFSSL_FILETYPE_PEM);
  976. }
  977. }
  978. #if defined(WOLFSSL_PEM_TO_DER)
  979. if (ret == WOLFSSL_SUCCESS) {
  980. /* test loading DER */
  981. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  982. if (ret == 0 && pDer != NULL) {
  983. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  984. WOLFSSL_FILETYPE_ASN1);
  985. wc_FreeDer(&pDer);
  986. }
  987. }
  988. #endif
  989. }
  990. free(cert_buf);
  991. return ret;
  992. }
  993. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  994. static void test_wolfSSL_CertManagerCheckOCSPResponse(void)
  995. {
  996. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  997. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  998. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  999. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1000. defined(HAVE_LIGHTY)
  1001. WOLFSSL_CERT_MANAGER* cm = NULL;
  1002. /* Raw OCSP response bytes captured using the following setup:
  1003. * - Run responder with
  1004. * openssl ocsp -port 9999 -ndays 9999
  1005. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1006. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1007. * -rkey certs/ocsp/ocsp-responder-key.pem
  1008. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1009. * - Run client with
  1010. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1011. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1012. * -cert certs/ocsp/server1-cert.pem
  1013. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1014. * - Copy raw response from Wireshark.
  1015. */
  1016. byte response[] = {
  1017. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1018. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1019. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1020. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1021. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1022. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1023. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1024. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1025. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1026. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1027. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1028. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1029. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1030. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1031. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1032. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1033. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1034. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1035. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1036. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1037. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1038. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1039. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1040. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1041. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1042. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1043. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1044. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1045. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1046. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1047. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1048. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1049. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1050. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1051. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1052. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1053. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1054. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1055. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1056. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1057. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1058. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1059. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1060. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1061. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1062. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1063. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1064. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1065. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1066. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1067. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1068. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1069. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1070. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1071. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1072. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1073. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1074. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1075. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1076. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1077. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1078. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1079. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1080. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1081. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1082. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1083. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1084. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1085. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1086. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1087. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1088. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1089. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1090. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1091. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1092. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1093. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1094. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1095. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1096. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1097. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1098. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1099. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1100. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1101. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1102. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1103. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1104. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1105. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1106. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1107. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1108. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1109. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1110. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1111. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1112. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1113. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1114. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1115. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1116. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1117. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1118. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1119. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1120. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1121. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1122. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1123. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1124. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1125. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1126. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1127. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1128. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1129. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1130. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1131. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1132. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1133. 0x59, 0x59, 0xa0, 0x07
  1134. };
  1135. OcspEntry entry[1];
  1136. CertStatus status[1];
  1137. OcspRequest* request;
  1138. byte serial[] = {0x05};
  1139. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1140. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1141. printf(testingFmt, "wolfSSL_CertManagerCheckOCSPResponse()");
  1142. XMEMSET(entry, 0, sizeof(OcspEntry));
  1143. XMEMSET(status, 0, sizeof(CertStatus));
  1144. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1145. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1146. DYNAMIC_TYPE_OCSP_REQUEST);
  1147. AssertNotNull(request->serial);
  1148. request->serialSz = sizeof(serial);
  1149. XMEMCPY(request->serial, serial, sizeof(serial));
  1150. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1151. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1152. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1153. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1154. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1155. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1156. /* Response should be valid. */
  1157. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1158. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1159. /* Flip a byte in the request serial number, response should be invalid
  1160. * now. */
  1161. request->serial[0] ^= request->serial[0];
  1162. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1163. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1164. wolfSSL_OCSP_REQUEST_free(request);
  1165. wolfSSL_CertManagerFree(cm);
  1166. printf(resultFmt, passed);
  1167. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1168. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1169. #endif /* HAVE_OCSP */
  1170. }
  1171. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1172. {
  1173. int ret = 0;
  1174. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1175. const char* ca_cert = "./certs/ca-cert.pem";
  1176. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1177. ret = test_cm_load_ca_file(ca_cert);
  1178. #ifdef NO_RSA
  1179. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1180. #else
  1181. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1182. #endif
  1183. ret = test_cm_load_ca_file(ca_expired_cert);
  1184. #ifdef NO_RSA
  1185. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1186. #else
  1187. #if !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1188. !defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1189. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1190. #else
  1191. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1192. #endif
  1193. #endif
  1194. #endif
  1195. return ret;
  1196. }
  1197. static void test_wolfSSL_CertManagerGetCerts(void)
  1198. {
  1199. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1200. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1201. defined(WOLFSSL_SIGNER_DER_CERT)
  1202. WOLFSSL_CERT_MANAGER* cm = NULL;
  1203. WOLFSSL_STACK* sk = NULL;
  1204. X509* x509 = NULL;
  1205. X509* cert1 = NULL;
  1206. FILE* file1 = NULL;
  1207. #ifdef DEBUG_WOLFSSL_VERBOSE
  1208. WOLFSSL_BIO* bio = NULL;
  1209. #endif
  1210. int i = 0;
  1211. printf(testingFmt, "wolfSSL_CertManagerGetCerts()");
  1212. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1213. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1214. fclose(file1);
  1215. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1216. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1217. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1218. "./certs/ca-cert.pem", NULL));
  1219. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1220. for (i = 0; i < sk_X509_num(sk); i++) {
  1221. x509 = sk_X509_value(sk, i);
  1222. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1223. #ifdef DEBUG_WOLFSSL_VERBOSE
  1224. bio = BIO_new(wolfSSL_BIO_s_file());
  1225. if (bio != NULL) {
  1226. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  1227. X509_print(bio, x509);
  1228. BIO_free(bio);
  1229. }
  1230. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1231. }
  1232. wolfSSL_X509_free(cert1);
  1233. sk_X509_pop_free(sk, NULL);
  1234. wolfSSL_CertManagerFree(cm);
  1235. printf(resultFmt, passed);
  1236. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1237. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1238. defined(WOLFSSL_SIGNER_DER_CERT) */
  1239. }
  1240. static int test_wolfSSL_CertManagerSetVerify(void)
  1241. {
  1242. int ret = 0;
  1243. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1244. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1245. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1246. WOLFSSL_CERT_MANAGER* cm;
  1247. int tmp = myVerifyAction;
  1248. const char* ca_cert = "./certs/ca-cert.pem";
  1249. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1250. cm = wolfSSL_CertManagerNew();
  1251. AssertNotNull(cm);
  1252. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1253. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1254. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1255. /* Use the test CB that always accepts certs */
  1256. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1257. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1258. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1259. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1260. {
  1261. const char* verifyCert = "./certs/server-cert.pem";
  1262. /* Use the test CB that always fails certs */
  1263. myVerifyAction = VERIFY_FORCE_FAIL;
  1264. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1265. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1266. }
  1267. #endif
  1268. wolfSSL_CertManagerFree(cm);
  1269. myVerifyAction = tmp;
  1270. #endif
  1271. return ret;
  1272. }
  1273. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1274. defined(DEBUG_UNIT_TEST_CERTS)
  1275. /* used when debugging name constraint tests */
  1276. static void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1277. {
  1278. BIO* out = BIO_new(BIO_s_file());
  1279. if (out != NULL) {
  1280. FILE* f = fopen(fileName, "wb");
  1281. BIO_set_fp(out, f, BIO_CLOSE);
  1282. PEM_write_bio_X509(out, x509);
  1283. BIO_free(out);
  1284. }
  1285. }
  1286. static void DEBUG_WRITE_CERT_DER(const byte* der, int derSz, const char* fileName)
  1287. {
  1288. BIO* out = BIO_new(BIO_s_file());
  1289. if (out != NULL) {
  1290. FILE* f = fopen(fileName, "wb");
  1291. BIO_set_fp(out, f, BIO_CLOSE);
  1292. BIO_write(out, der, derSz);
  1293. BIO_free(out);
  1294. }
  1295. }
  1296. #else
  1297. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1298. #define DEBUG_WRITE_CERT_DER(der, derSz, fileName)
  1299. #endif
  1300. static void test_wolfSSL_CertManagerNameConstraint(void)
  1301. {
  1302. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1303. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1304. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1305. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1306. !defined(NO_SHA256)
  1307. WOLFSSL_CERT_MANAGER* cm;
  1308. WOLFSSL_EVP_PKEY *priv;
  1309. WOLFSSL_X509_NAME* name;
  1310. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1311. const char* server_cert = "./certs/test/server-goodcn.pem";
  1312. int i = 0;
  1313. static const byte extNameConsOid[] = {85, 29, 30};
  1314. RsaKey key;
  1315. WC_RNG rng;
  1316. byte *der;
  1317. int derSz;
  1318. word32 idx = 0;
  1319. byte *pt;
  1320. WOLFSSL_X509 *x509, *ca;
  1321. wc_InitRng(&rng);
  1322. /* load in CA private key for signing */
  1323. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, devId), 0);
  1324. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1325. sizeof_server_key_der_2048), 0);
  1326. /* get ca certificate then alter it */
  1327. AssertNotNull(der =
  1328. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1329. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1330. WOLFSSL_FILETYPE_ASN1));
  1331. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1332. XMEMCPY(der, pt, derSz);
  1333. /* find the name constraint extension and alter it */
  1334. pt = der;
  1335. for (i = 0; i < derSz - 3; i++) {
  1336. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1337. pt += 3;
  1338. break;
  1339. }
  1340. pt++;
  1341. }
  1342. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1343. /* go to the length value and set it to 0 */
  1344. while (i < derSz && *pt != 0x81) {
  1345. pt++;
  1346. i++;
  1347. }
  1348. AssertIntNE(i, derSz); /* did not place to alter */
  1349. pt++;
  1350. *pt = 0x00;
  1351. /* resign the altered certificate */
  1352. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1353. FOURK_BUF, &key, NULL, &rng)), 0);
  1354. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1355. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1356. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1357. wolfSSL_CertManagerFree(cm);
  1358. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1359. wolfSSL_X509_free(x509);
  1360. wc_FreeRsaKey(&key);
  1361. wc_FreeRng(&rng);
  1362. /* add email alt name to satisfy constraint */
  1363. pt = (byte*)server_key_der_2048;
  1364. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1365. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1366. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1367. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1368. WOLFSSL_FILETYPE_ASN1));
  1369. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1370. DEBUG_WRITE_CERT_DER(der, derSz, "ca.der");
  1371. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1372. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1373. /* Good cert test with proper alt email name */
  1374. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1375. WOLFSSL_FILETYPE_PEM));
  1376. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1377. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1378. AssertNotNull(name = X509_NAME_new());
  1379. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1380. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1381. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1382. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1383. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1384. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1385. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1386. X509_NAME_free(name);
  1387. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1388. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1389. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1390. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1391. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1392. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1393. wolfSSL_X509_free(x509);
  1394. /* Cert with bad alt name list */
  1395. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1396. WOLFSSL_FILETYPE_PEM));
  1397. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1398. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1399. AssertNotNull(name = X509_NAME_new());
  1400. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1401. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1402. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1403. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1404. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1405. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1406. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1407. X509_NAME_free(name);
  1408. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1409. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1410. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1411. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1412. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1413. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1414. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1415. wolfSSL_CertManagerFree(cm);
  1416. wolfSSL_X509_free(x509);
  1417. wolfSSL_X509_free(ca);
  1418. wolfSSL_EVP_PKEY_free(priv);
  1419. #endif
  1420. }
  1421. static void test_wolfSSL_CertManagerNameConstraint2(void)
  1422. {
  1423. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1424. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1425. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1426. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1427. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  1428. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  1429. const char* server_cert = "./certs/server-cert.pem";
  1430. WOLFSSL_CERT_MANAGER* cm;
  1431. WOLFSSL_X509 *x509, *ca;
  1432. const unsigned char *der;
  1433. const unsigned char *pt;
  1434. WOLFSSL_EVP_PKEY *priv;
  1435. WOLFSSL_X509_NAME* name;
  1436. int derSz;
  1437. /* C=US*/
  1438. char altName[] = {
  1439. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1440. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  1441. };
  1442. /* C=ID */
  1443. char altNameFail[] = {
  1444. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1445. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  1446. };
  1447. /* C=US ST=California*/
  1448. char altNameExc[] = {
  1449. 0x30, 0x22,
  1450. 0x31, 0x0B,
  1451. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  1452. 0x31, 0x13,
  1453. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  1454. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  1455. };
  1456. /* load in CA private key for signing */
  1457. pt = ca_key_der_2048;
  1458. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  1459. sizeof_ca_key_der_2048));
  1460. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1461. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1462. WOLFSSL_FILETYPE_ASN1));
  1463. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1464. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1465. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1466. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1467. WOLFSSL_FILETYPE_PEM));
  1468. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1469. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1470. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1471. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1472. #else
  1473. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1474. #endif
  1475. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1476. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1477. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1478. /* add in matching DIR alt name and resign */
  1479. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1480. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1481. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1482. #else
  1483. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1484. #endif
  1485. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1486. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1487. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1488. wolfSSL_X509_free(x509);
  1489. /* check verify fail */
  1490. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1491. WOLFSSL_FILETYPE_PEM));
  1492. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1493. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1494. /* add in miss matching DIR alt name and resign */
  1495. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1496. ASN_DIR_TYPE);
  1497. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1498. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1499. #else
  1500. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1501. #endif
  1502. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1503. #ifndef WOLFSSL_NO_ASN_STRICT
  1504. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1505. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1506. #else
  1507. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1508. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1509. #endif
  1510. /* check that it still fails if one bad altname and one good altname is in
  1511. * the certificate */
  1512. wolfSSL_X509_free(x509);
  1513. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1514. WOLFSSL_FILETYPE_PEM));
  1515. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1516. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1517. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1518. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1519. ASN_DIR_TYPE);
  1520. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1521. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1522. #else
  1523. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1524. #endif
  1525. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1526. #ifndef WOLFSSL_NO_ASN_STRICT
  1527. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1528. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1529. #else
  1530. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1531. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1532. #endif
  1533. /* check it fails with switching position of bad altname */
  1534. wolfSSL_X509_free(x509);
  1535. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1536. WOLFSSL_FILETYPE_PEM));
  1537. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1538. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1539. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1540. ASN_DIR_TYPE);
  1541. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1542. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1543. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1544. #else
  1545. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1546. #endif
  1547. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1548. #ifndef WOLFSSL_NO_ASN_STRICT
  1549. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1550. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1551. #else
  1552. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1553. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1554. #endif
  1555. wolfSSL_CertManagerFree(cm);
  1556. wolfSSL_X509_free(x509);
  1557. wolfSSL_X509_free(ca);
  1558. /* now test with excluded name constraint */
  1559. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1560. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  1561. WOLFSSL_FILETYPE_ASN1));
  1562. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1563. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1564. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1565. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1566. WOLFSSL_FILETYPE_PEM));
  1567. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  1568. ASN_DIR_TYPE);
  1569. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1570. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1571. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1572. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1573. #else
  1574. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1575. #endif
  1576. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1577. #ifndef WOLFSSL_NO_ASN_STRICT
  1578. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1579. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1580. #else
  1581. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1582. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1583. #endif
  1584. wolfSSL_CertManagerFree(cm);
  1585. wolfSSL_X509_free(x509);
  1586. wolfSSL_X509_free(ca);
  1587. wolfSSL_EVP_PKEY_free(priv);
  1588. #endif
  1589. }
  1590. static void test_wolfSSL_CertManagerNameConstraint3(void)
  1591. {
  1592. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1593. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1594. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1595. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1596. !defined(NO_SHA256)
  1597. WOLFSSL_CERT_MANAGER* cm;
  1598. WOLFSSL_EVP_PKEY *priv;
  1599. WOLFSSL_X509_NAME* name;
  1600. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  1601. const char* server_cert = "./certs/test/server-goodcn.pem";
  1602. byte *der;
  1603. int derSz;
  1604. byte *pt;
  1605. WOLFSSL_X509 *x509, *ca;
  1606. pt = (byte*)server_key_der_2048;
  1607. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1608. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1609. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1610. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1611. WOLFSSL_FILETYPE_ASN1));
  1612. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1613. DEBUG_WRITE_CERT_DER(der, derSz, "ca.der");
  1614. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1615. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1616. /* check satisfying .wolfssl.com constraint passes */
  1617. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1618. WOLFSSL_FILETYPE_PEM));
  1619. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1620. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1621. AssertNotNull(name = X509_NAME_new());
  1622. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1623. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1624. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1625. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1626. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1627. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1628. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1629. X509_NAME_free(name);
  1630. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1631. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1632. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  1633. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1634. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1635. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1636. wolfSSL_X509_free(x509);
  1637. /* check satisfying .random.com constraint passes */
  1638. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1639. WOLFSSL_FILETYPE_PEM));
  1640. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1641. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1642. AssertNotNull(name = X509_NAME_new());
  1643. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1644. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1645. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1646. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1647. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1648. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  1649. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1650. X509_NAME_free(name);
  1651. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  1652. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1653. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  1654. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1655. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1656. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1657. wolfSSL_X509_free(x509);
  1658. /* check fail case when neither constraint is matched */
  1659. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1660. WOLFSSL_FILETYPE_PEM));
  1661. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1662. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1663. AssertNotNull(name = X509_NAME_new());
  1664. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1665. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1666. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1667. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1668. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1669. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  1670. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1671. X509_NAME_free(name);
  1672. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1673. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1674. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1675. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1676. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1677. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1678. wolfSSL_CertManagerFree(cm);
  1679. wolfSSL_X509_free(x509);
  1680. wolfSSL_X509_free(ca);
  1681. wolfSSL_EVP_PKEY_free(priv);
  1682. #endif
  1683. }
  1684. static void test_wolfSSL_CertManagerNameConstraint4(void)
  1685. {
  1686. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1687. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1688. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1689. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1690. !defined(NO_SHA256)
  1691. WOLFSSL_CERT_MANAGER* cm;
  1692. WOLFSSL_EVP_PKEY *priv;
  1693. WOLFSSL_X509_NAME* name;
  1694. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  1695. const char* server_cert = "./certs/test/server-goodcn.pem";
  1696. byte *der;
  1697. int derSz;
  1698. byte *pt;
  1699. WOLFSSL_X509 *x509, *ca;
  1700. pt = (byte*)server_key_der_2048;
  1701. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1702. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1703. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1704. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1705. WOLFSSL_FILETYPE_ASN1));
  1706. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1707. DEBUG_WRITE_CERT_DER(der, derSz, "ca.der");
  1708. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1709. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1710. /* check satisfying wolfssl.com constraint passes */
  1711. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1712. WOLFSSL_FILETYPE_PEM));
  1713. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1714. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1715. AssertNotNull(name = X509_NAME_new());
  1716. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1717. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1718. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1719. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1720. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1721. X509_NAME_free(name);
  1722. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1723. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1724. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  1725. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1726. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1727. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1728. wolfSSL_X509_free(x509);
  1729. /* check satisfying example.com constraint passes */
  1730. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1731. WOLFSSL_FILETYPE_PEM));
  1732. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1733. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1734. AssertNotNull(name = X509_NAME_new());
  1735. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1736. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1737. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1738. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  1739. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1740. X509_NAME_free(name);
  1741. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  1742. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1743. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  1744. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1745. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1746. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1747. wolfSSL_X509_free(x509);
  1748. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  1749. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1750. WOLFSSL_FILETYPE_PEM));
  1751. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1752. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1753. AssertNotNull(name = X509_NAME_new());
  1754. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1755. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1756. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1757. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1758. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1759. X509_NAME_free(name);
  1760. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1761. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  1762. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  1763. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1764. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  1765. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1766. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1767. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1768. wolfSSL_X509_free(x509);
  1769. /* check fail when one DNS in the list is bad */
  1770. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1771. WOLFSSL_FILETYPE_PEM));
  1772. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1773. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1774. AssertNotNull(name = X509_NAME_new());
  1775. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1776. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1777. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1778. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1779. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1780. X509_NAME_free(name);
  1781. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1782. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  1783. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  1784. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1785. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  1786. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1787. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1788. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1789. wolfSSL_X509_free(x509);
  1790. /* check fail case when neither constraint is matched */
  1791. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1792. WOLFSSL_FILETYPE_PEM));
  1793. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1794. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1795. AssertNotNull(name = X509_NAME_new());
  1796. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1797. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1798. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1799. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  1800. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1801. X509_NAME_free(name);
  1802. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  1803. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1804. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1805. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1806. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1807. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1808. wolfSSL_CertManagerFree(cm);
  1809. wolfSSL_X509_free(x509);
  1810. wolfSSL_X509_free(ca);
  1811. wolfSSL_EVP_PKEY_free(priv);
  1812. #endif
  1813. }
  1814. static void test_wolfSSL_CertManagerNameConstraint5(void)
  1815. {
  1816. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1817. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1818. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1819. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1820. !defined(NO_SHA256)
  1821. WOLFSSL_CERT_MANAGER* cm;
  1822. WOLFSSL_EVP_PKEY *priv;
  1823. WOLFSSL_X509_NAME* name;
  1824. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  1825. const char* server_cert = "./certs/test/server-goodcn.pem";
  1826. byte *der;
  1827. int derSz;
  1828. byte *pt;
  1829. WOLFSSL_X509 *x509, *ca;
  1830. pt = (byte*)server_key_der_2048;
  1831. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1832. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1833. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1834. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1835. WOLFSSL_FILETYPE_ASN1));
  1836. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1837. DEBUG_WRITE_CERT_DER(der, derSz, "ca.der");
  1838. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1839. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1840. /* check satisfying wolfssl.com constraint passes */
  1841. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1842. WOLFSSL_FILETYPE_PEM));
  1843. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1844. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1845. AssertNotNull(name = X509_NAME_new());
  1846. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1847. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1848. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1849. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  1850. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1851. X509_NAME_free(name);
  1852. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  1853. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  1854. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1855. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1856. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1857. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1858. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1859. wolfSSL_X509_free(x509);
  1860. /* fail with DNS check because of common name */
  1861. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1862. WOLFSSL_FILETYPE_PEM));
  1863. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1864. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1865. AssertNotNull(name = X509_NAME_new());
  1866. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1867. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1868. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1869. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1870. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1871. X509_NAME_free(name);
  1872. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1873. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  1874. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1875. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  1876. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1877. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1878. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1879. wolfSSL_X509_free(x509);
  1880. /* fail on permitted DNS name constraint */
  1881. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1882. WOLFSSL_FILETYPE_PEM));
  1883. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1884. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1885. AssertNotNull(name = X509_NAME_new());
  1886. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1887. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1888. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1889. X509_NAME_free(name);
  1890. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  1891. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  1892. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  1893. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1894. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  1895. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1896. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1897. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1898. wolfSSL_X509_free(x509);
  1899. /* fail on permitted email name constraint */
  1900. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1901. WOLFSSL_FILETYPE_PEM));
  1902. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1903. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1904. AssertNotNull(name = X509_NAME_new());
  1905. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1906. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1907. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1908. X509_NAME_free(name);
  1909. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1910. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  1911. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  1912. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1913. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  1914. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1915. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1916. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1917. wolfSSL_X509_free(x509);
  1918. /* success with empty email name */
  1919. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1920. WOLFSSL_FILETYPE_PEM));
  1921. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1922. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1923. AssertNotNull(name = X509_NAME_new());
  1924. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1925. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1926. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1927. X509_NAME_free(name);
  1928. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1929. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1930. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  1931. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1932. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1933. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1934. wolfSSL_X509_free(x509);
  1935. wolfSSL_CertManagerFree(cm);
  1936. wolfSSL_X509_free(ca);
  1937. wolfSSL_EVP_PKEY_free(priv);
  1938. #endif
  1939. }
  1940. static void test_wolfSSL_CertManagerCRL(void)
  1941. {
  1942. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  1943. !defined(NO_RSA)
  1944. const char* ca_cert = "./certs/ca-cert.pem";
  1945. const char* crl1 = "./certs/crl/crl.pem";
  1946. const char* crl2 = "./certs/crl/crl2.pem";
  1947. WOLFSSL_CERT_MANAGER* cm = NULL;
  1948. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1949. AssertIntEQ(WOLFSSL_SUCCESS,
  1950. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1951. AssertIntEQ(WOLFSSL_SUCCESS,
  1952. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1953. AssertIntEQ(WOLFSSL_SUCCESS,
  1954. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  1955. wolfSSL_CertManagerFreeCRL(cm);
  1956. AssertIntEQ(WOLFSSL_SUCCESS,
  1957. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  1958. AssertIntEQ(WOLFSSL_SUCCESS,
  1959. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  1960. wolfSSL_CertManagerFree(cm);
  1961. #endif
  1962. }
  1963. static void test_wolfSSL_CTX_load_verify_locations_ex(void)
  1964. {
  1965. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1966. !defined(NO_WOLFSSL_CLIENT)
  1967. WOLFSSL_CTX* ctx;
  1968. const char* ca_cert = "./certs/ca-cert.pem";
  1969. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1970. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1971. AssertNotNull(ctx);
  1972. /* test good CA */
  1973. AssertTrue(WOLFSSL_SUCCESS ==
  1974. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  1975. WOLFSSL_LOAD_FLAG_NONE));
  1976. /* test expired CA */
  1977. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  1978. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1979. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  1980. #else
  1981. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1982. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  1983. #endif
  1984. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  1985. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  1986. wolfSSL_CTX_free(ctx);
  1987. #endif
  1988. }
  1989. static void test_wolfSSL_CTX_load_verify_buffer_ex(void)
  1990. {
  1991. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  1992. defined(USE_CERT_BUFFERS_2048)
  1993. WOLFSSL_CTX* ctx;
  1994. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  1995. byte ca_expired_cert[TWOK_BUF];
  1996. word32 sizeof_ca_expired_cert;
  1997. XFILE fp;
  1998. #ifndef NO_WOLFSSL_CLIENT
  1999. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2000. #else
  2001. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2002. #endif
  2003. AssertNotNull(ctx);
  2004. /* test good CA */
  2005. AssertTrue(WOLFSSL_SUCCESS ==
  2006. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2007. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2008. WOLFSSL_LOAD_FLAG_NONE));
  2009. /* load expired CA */
  2010. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2011. fp = XFOPEN(ca_expired_cert_file, "rb");
  2012. AssertTrue(fp != XBADFILE);
  2013. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2014. sizeof(ca_expired_cert), fp);
  2015. XFCLOSE(fp);
  2016. /* test expired CA failure */
  2017. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2018. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2019. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2020. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2021. #else
  2022. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2023. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2024. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2025. #endif
  2026. /* test expired CA success */
  2027. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2028. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2029. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2030. wolfSSL_CTX_free(ctx);
  2031. #endif
  2032. }
  2033. static void test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2034. {
  2035. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2036. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048)
  2037. WOLFSSL_CTX* ctx;
  2038. #ifndef NO_WOLFSSL_CLIENT
  2039. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2040. #else
  2041. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2042. #endif
  2043. AssertTrue(WOLFSSL_SUCCESS ==
  2044. wolfSSL_CTX_load_verify_chain_buffer_format(ctx, ca_cert_chain_der,
  2045. sizeof_ca_cert_chain_der,
  2046. WOLFSSL_FILETYPE_ASN1));
  2047. wolfSSL_CTX_free(ctx);
  2048. #endif
  2049. }
  2050. static void test_wolfSSL_CTX_add1_chain_cert(void)
  2051. {
  2052. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2053. defined(KEEP_OUR_CERT)
  2054. WOLFSSL_CTX* ctx;
  2055. WOLFSSL* ssl;
  2056. const char *certChain[] = {
  2057. "./certs/intermediate/client-int-cert.pem",
  2058. "./certs/intermediate/ca-int2-cert.pem",
  2059. "./certs/intermediate/ca-int-cert.pem",
  2060. "./certs/ca-cert.pem",
  2061. NULL
  2062. };
  2063. const char** cert;
  2064. WOLFSSL_X509* x509;
  2065. WOLF_STACK_OF(X509)* chain = NULL;
  2066. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2067. AssertNotNull(ssl = wolfSSL_new(ctx));
  2068. for (cert = certChain; *cert != NULL; cert++) {
  2069. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2070. AssertNotNull(x509);
  2071. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2072. X509_free(x509);
  2073. }
  2074. for (cert = certChain; *cert != NULL; cert++) {
  2075. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2076. AssertNotNull(x509);
  2077. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2078. X509_free(x509);
  2079. }
  2080. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2081. AssertIntEQ(sk_X509_num(chain), 3);
  2082. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2083. AssertIntEQ(sk_X509_num(chain), 3);
  2084. SSL_free(ssl);
  2085. SSL_CTX_free(ctx);
  2086. #endif
  2087. }
  2088. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2089. {
  2090. int ret = 0;
  2091. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  2092. const char* server_chain_der = "./certs/server-cert-chain.der";
  2093. WOLFSSL_CTX* ctx;
  2094. #ifndef NO_WOLFSSL_CLIENT
  2095. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2096. AssertNotNull(ctx);
  2097. #else
  2098. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2099. AssertNotNull(ctx);
  2100. #endif
  2101. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2102. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2103. wolfSSL_CTX_free(ctx);
  2104. #endif
  2105. return ret;
  2106. }
  2107. static void test_wolfSSL_CTX_SetTmpDH_file(void)
  2108. {
  2109. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH)
  2110. WOLFSSL_CTX *ctx;
  2111. #ifndef NO_WOLFSSL_CLIENT
  2112. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2113. #else
  2114. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2115. #endif
  2116. /* invalid context */
  2117. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2118. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2119. /* invalid dhParamFile file */
  2120. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2121. NULL, WOLFSSL_FILETYPE_PEM));
  2122. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2123. bogusFile, WOLFSSL_FILETYPE_PEM));
  2124. /* success */
  2125. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2126. WOLFSSL_FILETYPE_PEM));
  2127. wolfSSL_CTX_free(ctx);
  2128. #endif
  2129. }
  2130. static void test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2131. {
  2132. #if !defined(NO_CERTS) && !defined(NO_DH)
  2133. WOLFSSL_CTX *ctx;
  2134. #ifndef NO_WOLFSSL_CLIENT
  2135. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2136. #else
  2137. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2138. #endif
  2139. /* invalid context */
  2140. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2141. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2142. /* invalid dhParamFile file */
  2143. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2144. 0, WOLFSSL_FILETYPE_ASN1));
  2145. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2146. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2147. /* success */
  2148. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2149. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2150. wolfSSL_CTX_free(ctx);
  2151. #endif
  2152. }
  2153. static void test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2154. {
  2155. #if !defined(NO_CERTS) && !defined(NO_DH)
  2156. WOLFSSL_CTX *ctx;
  2157. #ifndef NO_WOLFSSL_CLIENT
  2158. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2159. AssertNotNull(ctx);
  2160. #else
  2161. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2162. AssertNotNull(ctx);
  2163. #endif
  2164. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2165. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2166. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2167. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2168. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2169. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2170. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2171. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2172. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2173. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2174. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2175. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2176. wolfSSL_CTX_free(ctx);
  2177. #endif
  2178. }
  2179. static void test_wolfSSL_CTX_der_load_verify_locations(void)
  2180. {
  2181. #ifdef WOLFSSL_DER_LOAD
  2182. WOLFSSL_CTX* ctx = NULL;
  2183. const char* derCert = "./certs/server-cert.der";
  2184. const char* nullPath = NULL;
  2185. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2186. const char* emptyPath = "";
  2187. /* der load Case 1 ctx NULL */
  2188. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2189. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2190. #ifndef NO_WOLFSSL_CLIENT
  2191. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2192. #else
  2193. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2194. #endif
  2195. /* Case 2 filePath NULL */
  2196. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2197. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2198. /* Case 3 invalid format */
  2199. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2200. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2201. /* Case 4 filePath not valid */
  2202. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2203. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2204. /* Case 5 filePath empty */
  2205. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2206. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2207. #ifndef NO_RSA
  2208. /* Case 6 success case */
  2209. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2210. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2211. #endif
  2212. wolfSSL_CTX_free(ctx);
  2213. #endif
  2214. }
  2215. static void test_wolfSSL_CTX_enable_disable(void)
  2216. {
  2217. #ifndef NO_CERTS
  2218. WOLFSSL_CTX* ctx = NULL;
  2219. #ifdef HAVE_CRL
  2220. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2221. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2222. #endif
  2223. #ifdef HAVE_OCSP
  2224. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2225. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2226. #endif
  2227. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2228. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2229. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  2230. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  2231. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2232. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2233. #endif
  2234. #ifndef NO_WOLFSSL_CLIENT
  2235. #ifdef HAVE_EXTENDED_MASTER
  2236. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  2237. #endif
  2238. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2239. AssertNotNull(ctx);
  2240. #ifdef HAVE_EXTENDED_MASTER
  2241. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  2242. #endif
  2243. #elif !defined(NO_WOLFSSL_SERVER)
  2244. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2245. #else
  2246. return;
  2247. #endif
  2248. #ifdef HAVE_CRL
  2249. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  2250. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  2251. #endif
  2252. #ifdef HAVE_OCSP
  2253. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  2254. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  2255. WOLFSSL_SUCCESS);
  2256. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  2257. WOLFSSL_SUCCESS);
  2258. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  2259. WOLFSSL_SUCCESS);
  2260. #endif
  2261. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2262. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2263. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2264. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2265. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2266. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2267. #endif
  2268. wolfSSL_CTX_free(ctx);
  2269. #endif /* NO_CERTS */
  2270. }
  2271. static void test_wolfSSL_CTX_ticket_API(void)
  2272. {
  2273. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  2274. WOLFSSL_CTX* ctx = NULL;
  2275. void *userCtx = (void*)"this is my ctx";
  2276. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2277. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  2278. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  2279. wolfSSL_CTX_free(ctx);
  2280. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  2281. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  2282. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  2283. }
  2284. static void test_wolfSSL_set_minmax_proto_version(void)
  2285. {
  2286. #ifdef OPENSSL_EXTRA
  2287. WOLFSSL_CTX *ctx;
  2288. WOLFSSL *ssl;
  2289. int ret;
  2290. (void)ret;
  2291. (void)ssl;
  2292. printf(testingFmt, "test_wolfSSL_set_minmax_proto_version");
  2293. #ifndef NO_WOLFSSL_CLIENT
  2294. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2295. AssertNotNull(ssl = wolfSSL_new(ctx));
  2296. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2297. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2298. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2299. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2300. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2301. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  2302. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2303. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  2304. wolfSSL_free(ssl);
  2305. wolfSSL_CTX_free(ctx);
  2306. #else
  2307. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2308. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2309. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2310. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2311. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2312. wolfSSL_CTX_free(ctx);
  2313. #endif
  2314. printf(resultFmt, passed);
  2315. #endif
  2316. }
  2317. /*----------------------------------------------------------------------------*
  2318. | SSL
  2319. *----------------------------------------------------------------------------*/
  2320. static void test_server_wolfSSL_new(void)
  2321. {
  2322. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2323. !defined(NO_WOLFSSL_SERVER)
  2324. WOLFSSL_CTX *ctx;
  2325. WOLFSSL_CTX *ctx_nocert;
  2326. WOLFSSL *ssl;
  2327. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2328. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2329. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  2330. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  2331. /* invalid context */
  2332. AssertNull(ssl = wolfSSL_new(NULL));
  2333. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  2334. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  2335. #endif
  2336. /* success */
  2337. AssertNotNull(ssl = wolfSSL_new(ctx));
  2338. wolfSSL_free(ssl);
  2339. wolfSSL_CTX_free(ctx);
  2340. wolfSSL_CTX_free(ctx_nocert);
  2341. #endif
  2342. }
  2343. static void test_client_wolfSSL_new(void)
  2344. {
  2345. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2346. !defined(NO_WOLFSSL_CLIENT)
  2347. WOLFSSL_CTX *ctx;
  2348. WOLFSSL_CTX *ctx_nocert;
  2349. WOLFSSL *ssl;
  2350. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2351. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2352. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  2353. /* invalid context */
  2354. AssertNull(ssl = wolfSSL_new(NULL));
  2355. /* success */
  2356. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  2357. wolfSSL_free(ssl);
  2358. /* success */
  2359. AssertNotNull(ssl = wolfSSL_new(ctx));
  2360. wolfSSL_free(ssl);
  2361. wolfSSL_CTX_free(ctx);
  2362. wolfSSL_CTX_free(ctx_nocert);
  2363. #endif
  2364. }
  2365. static void test_wolfSSL_SetTmpDH_file(void)
  2366. {
  2367. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2368. !defined(NO_WOLFSSL_SERVER)
  2369. WOLFSSL_CTX *ctx;
  2370. WOLFSSL *ssl;
  2371. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2372. #ifndef NO_RSA
  2373. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2374. WOLFSSL_FILETYPE_PEM));
  2375. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2376. WOLFSSL_FILETYPE_PEM));
  2377. #elif defined(HAVE_ECC)
  2378. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  2379. WOLFSSL_FILETYPE_PEM));
  2380. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  2381. WOLFSSL_FILETYPE_PEM));
  2382. #elif defined(HAVE_ED25519)
  2383. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  2384. WOLFSSL_FILETYPE_PEM));
  2385. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  2386. WOLFSSL_FILETYPE_PEM));
  2387. #elif defined(HAVE_ED448)
  2388. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  2389. WOLFSSL_FILETYPE_PEM));
  2390. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  2391. WOLFSSL_FILETYPE_PEM));
  2392. #endif
  2393. AssertNotNull(ssl = wolfSSL_new(ctx));
  2394. /* invalid ssl */
  2395. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  2396. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2397. /* invalid dhParamFile file */
  2398. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2399. NULL, WOLFSSL_FILETYPE_PEM));
  2400. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2401. bogusFile, WOLFSSL_FILETYPE_PEM));
  2402. /* success */
  2403. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  2404. WOLFSSL_FILETYPE_PEM));
  2405. wolfSSL_free(ssl);
  2406. wolfSSL_CTX_free(ctx);
  2407. #endif
  2408. }
  2409. static void test_wolfSSL_SetTmpDH_buffer(void)
  2410. {
  2411. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  2412. WOLFSSL_CTX *ctx;
  2413. WOLFSSL *ssl;
  2414. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2415. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  2416. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2417. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2418. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2419. AssertNotNull(ssl = wolfSSL_new(ctx));
  2420. /* invalid ssl */
  2421. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2422. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2423. /* invalid dhParamFile file */
  2424. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  2425. 0, WOLFSSL_FILETYPE_ASN1));
  2426. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  2427. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2428. /* success */
  2429. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2430. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2431. wolfSSL_free(ssl);
  2432. wolfSSL_CTX_free(ctx);
  2433. #endif
  2434. }
  2435. static void test_wolfSSL_SetMinMaxDhKey_Sz(void)
  2436. {
  2437. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  2438. WOLFSSL_CTX *ctx, *ctx2;
  2439. WOLFSSL *ssl, *ssl2;
  2440. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2441. AssertNotNull(ctx);
  2442. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  2443. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2444. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2445. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2446. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2447. ssl = wolfSSL_new(ctx);
  2448. AssertNotNull(ssl);
  2449. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2450. AssertNotNull(ctx2);
  2451. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  2452. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2453. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  2454. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2455. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2456. ssl2 = wolfSSL_new(ctx2);
  2457. AssertNotNull(ssl2);
  2458. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2459. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2460. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  2461. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2462. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2463. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  2464. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2465. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2466. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  2467. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2468. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  2469. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  2470. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2471. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  2472. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2473. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2474. wolfSSL_free(ssl2);
  2475. wolfSSL_CTX_free(ctx2);
  2476. wolfSSL_free(ssl);
  2477. wolfSSL_CTX_free(ctx);
  2478. #endif
  2479. }
  2480. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  2481. * allowed.
  2482. * POST: return 1 on success.
  2483. */
  2484. static int test_wolfSSL_SetMinVersion(void)
  2485. {
  2486. int failFlag = WOLFSSL_SUCCESS;
  2487. #ifndef NO_WOLFSSL_CLIENT
  2488. WOLFSSL_CTX* ctx;
  2489. WOLFSSL* ssl;
  2490. int itr;
  2491. #ifndef NO_OLD_TLS
  2492. const int versions[] = {
  2493. #ifdef WOLFSSL_ALLOW_TLSV10
  2494. WOLFSSL_TLSV1,
  2495. #endif
  2496. WOLFSSL_TLSV1_1,
  2497. WOLFSSL_TLSV1_2};
  2498. #elif !defined(WOLFSSL_NO_TLS12)
  2499. const int versions[] = { WOLFSSL_TLSV1_2 };
  2500. #else
  2501. const int versions[] = { WOLFSSL_TLSV1_3 };
  2502. #endif
  2503. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2504. ssl = wolfSSL_new(ctx);
  2505. printf(testingFmt, "wolfSSL_SetMinVersion()");
  2506. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  2507. if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
  2508. failFlag = WOLFSSL_FAILURE;
  2509. }
  2510. }
  2511. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  2512. wolfSSL_free(ssl);
  2513. wolfSSL_CTX_free(ctx);
  2514. #endif
  2515. return failFlag;
  2516. } /* END test_wolfSSL_SetMinVersion */
  2517. /*----------------------------------------------------------------------------*
  2518. | EC
  2519. *----------------------------------------------------------------------------*/
  2520. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  2521. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  2522. */
  2523. # if defined(OPENSSL_EXTRA) && \
  2524. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  2525. static void test_wolfSSL_EC(void)
  2526. {
  2527. #if defined(HAVE_ECC)
  2528. BN_CTX *ctx;
  2529. EC_GROUP *group;
  2530. EC_GROUP *group2;
  2531. EC_POINT *Gxy, *new_point, *set_point;
  2532. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  2533. BIGNUM *X, *Y;
  2534. BIGNUM *set_point_bn;
  2535. char* hexStr;
  2536. int group_bits;
  2537. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  2538. /* NISTP256R1 Gx/Gy */
  2539. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2540. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2541. #ifndef HAVE_SELFTEST
  2542. EC_POINT *tmp;
  2543. size_t bin_len;
  2544. unsigned char* buf = NULL;
  2545. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2546. const unsigned char binUncompG[] = {
  2547. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2548. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2549. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2550. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  2551. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  2552. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  2553. };
  2554. #ifdef HAVE_COMP_KEY
  2555. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2556. const unsigned char binCompG[] = {
  2557. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2558. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2559. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2560. };
  2561. #endif
  2562. #endif
  2563. AssertNotNull(ctx = BN_CTX_new());
  2564. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  2565. AssertNotNull(group2 = EC_GROUP_dup(group));
  2566. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  2567. AssertNotNull(Gxy = EC_POINT_new(group));
  2568. AssertNotNull(new_point = EC_POINT_new(group));
  2569. AssertNotNull(set_point = EC_POINT_new(group));
  2570. AssertNotNull(X = BN_new());
  2571. AssertNotNull(Y = BN_new());
  2572. AssertNotNull(set_point_bn = BN_new());
  2573. /* load test values */
  2574. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  2575. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  2576. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  2577. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  2578. /* populate coordinates for input point */
  2579. Gxy->X = Gx;
  2580. Gxy->Y = Gy;
  2581. Gxy->Z = Gz;
  2582. #ifndef HAVE_SELFTEST
  2583. /* perform point multiplication */
  2584. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
  2585. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
  2586. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2587. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2588. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2589. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  2590. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2591. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2592. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2593. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
  2594. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2595. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2596. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2597. #else
  2598. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  2599. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2600. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2601. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2602. #endif
  2603. /* check if point X coordinate is zero */
  2604. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2605. #ifdef USE_ECC_B_PARAM
  2606. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  2607. #endif /* USE_ECC_B_PARAM */
  2608. /* Force non-affine coordinates */
  2609. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2610. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  2611. new_point->inSet = 0;
  2612. /* extract the coordinates from point */
  2613. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2614. /* check if point X coordinate is zero */
  2615. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  2616. /* set the same X and Y points in another object */
  2617. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2618. /* compare points as they should be the same */
  2619. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  2620. /* Test copying */
  2621. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  2622. /* Test inverting */
  2623. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  2624. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  2625. set_point_bn, ctx), set_point_bn);
  2626. /* check bn2hex */
  2627. hexStr = BN_bn2hex(k);
  2628. AssertStrEQ(hexStr, kTest);
  2629. #ifndef NO_FILESYSTEM
  2630. BN_print_fp(stdout, k);
  2631. printf("\n");
  2632. #endif
  2633. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2634. hexStr = BN_bn2hex(Gx);
  2635. AssertStrEQ(hexStr, kGx);
  2636. #ifndef NO_FILESYSTEM
  2637. BN_print_fp(stdout, Gx);
  2638. printf("\n");
  2639. #endif
  2640. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2641. hexStr = BN_bn2hex(Gy);
  2642. AssertStrEQ(hexStr, kGy);
  2643. #ifndef NO_FILESYSTEM
  2644. BN_print_fp(stdout, Gy);
  2645. printf("\n");
  2646. #endif
  2647. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2648. #ifndef HAVE_SELFTEST
  2649. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  2650. AssertStrEQ(hexStr, uncompG);
  2651. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2652. #ifdef HAVE_COMP_KEY
  2653. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  2654. AssertStrEQ(hexStr, compG);
  2655. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2656. #endif
  2657. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  2658. AssertIntEQ(bin_len, sizeof(binUncompG));
  2659. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2660. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  2661. bin_len, ctx), bin_len);
  2662. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  2663. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2664. #ifdef HAVE_COMP_KEY
  2665. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  2666. AssertIntEQ(bin_len, sizeof(binCompG));
  2667. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2668. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  2669. bin_len, ctx), bin_len);
  2670. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  2671. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2672. #endif
  2673. AssertNotNull(tmp = EC_POINT_new(group));
  2674. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  2675. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2676. EC_POINT_free(tmp);
  2677. #ifdef HAVE_COMP_KEY
  2678. AssertNotNull(tmp = EC_POINT_new(group));
  2679. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  2680. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2681. EC_POINT_free(tmp);
  2682. #endif
  2683. #endif
  2684. /* test BN_mod_add */
  2685. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2686. (WOLFSSL_BIGNUM*)BN_value_one(),
  2687. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  2688. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  2689. /* cleanup */
  2690. BN_free(X);
  2691. BN_free(Y);
  2692. BN_free(k);
  2693. BN_free(set_point_bn);
  2694. EC_POINT_free(new_point);
  2695. EC_POINT_free(set_point);
  2696. EC_POINT_free(Gxy);
  2697. EC_GROUP_free(group);
  2698. EC_GROUP_free(group2);
  2699. BN_CTX_free(ctx);
  2700. #endif /* HAVE_ECC */
  2701. }
  2702. #endif /* OPENSSL_EXTRA && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  2703. #ifndef NO_BIO
  2704. static void test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  2705. {
  2706. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  2707. EC_GROUP *group;
  2708. BIO* bio;
  2709. AssertNotNull(bio = BIO_new(BIO_s_file()));
  2710. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  2711. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  2712. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  2713. EC_GROUP_free(group);
  2714. BIO_free(bio);
  2715. #endif /* HAVE_ECC */
  2716. }
  2717. #endif /* !NO_BIO */
  2718. # if defined(OPENSSL_EXTRA)
  2719. static void test_wolfSSL_ECDSA_SIG(void)
  2720. {
  2721. #ifdef HAVE_ECC
  2722. WOLFSSL_ECDSA_SIG* sig = NULL;
  2723. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  2724. const unsigned char* cp;
  2725. unsigned char* p;
  2726. unsigned char outSig[8];
  2727. unsigned char sigData[8] =
  2728. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  2729. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  2730. AssertNull(sig);
  2731. cp = sigData;
  2732. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  2733. AssertIntEQ((cp == sigData + 8), 1);
  2734. cp = sigData;
  2735. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  2736. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  2737. AssertIntEQ((sig == sig2), 1);
  2738. cp = outSig;
  2739. p = outSig;
  2740. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  2741. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  2742. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  2743. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  2744. AssertIntEQ((p == outSig + 8), 1);
  2745. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  2746. wolfSSL_ECDSA_SIG_free(sig);
  2747. #endif /* HAVE_ECC */
  2748. }
  2749. static void test_EC_i2d(void)
  2750. {
  2751. #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
  2752. EC_KEY *key;
  2753. EC_KEY *copy;
  2754. int len;
  2755. unsigned char *buf = NULL;
  2756. const unsigned char *tmp = NULL;
  2757. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2758. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2759. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  2760. AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
  2761. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2762. buf = NULL;
  2763. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  2764. AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
  2765. tmp = buf;
  2766. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  2767. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2768. buf = NULL;
  2769. AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
  2770. tmp = buf;
  2771. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  2772. AssertIntEQ(EC_KEY_check_key(key), 1);
  2773. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  2774. EC_KEY_free(key);
  2775. EC_KEY_free(copy);
  2776. #endif /* HAVE_ECC */
  2777. }
  2778. static void test_ECDSA_size_sign(void)
  2779. {
  2780. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  2781. EC_KEY *key;
  2782. int id;
  2783. byte hash[WC_MAX_DIGEST_SIZE];
  2784. byte sig[ECC_MAX_SIG_SIZE];
  2785. unsigned int sigSz = sizeof(sig);
  2786. XMEMSET(hash, 123, sizeof(hash));
  2787. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  2788. AssertIntEQ(id, ECC_SECP256R1);
  2789. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2790. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2791. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  2792. AssertIntGE(ECDSA_size(key), sigSz);
  2793. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  2794. EC_KEY_free(key);
  2795. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  2796. }
  2797. static void test_ED25519(void)
  2798. {
  2799. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  2800. defined(WOLFSSL_KEY_GEN)
  2801. byte priv[ED25519_PRV_KEY_SIZE];
  2802. unsigned int privSz = (unsigned int)sizeof(priv);
  2803. byte pub[ED25519_PUB_KEY_SIZE];
  2804. unsigned int pubSz = (unsigned int)sizeof(pub);
  2805. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  2806. const char* msg = TEST_STRING;
  2807. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  2808. byte sig[ED25519_SIG_SIZE];
  2809. unsigned int sigSz = (unsigned int)sizeof(sig);
  2810. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  2811. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  2812. WOLFSSL_SUCCESS);
  2813. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  2814. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  2815. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  2816. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  2817. &sigSz), WOLFSSL_SUCCESS);
  2818. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  2819. #ifdef HAVE_ED25519_VERIFY
  2820. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  2821. sigSz), WOLFSSL_SUCCESS);
  2822. #endif /* HAVE_ED25519_VERIFY */
  2823. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  2824. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  2825. }
  2826. static void test_ED448(void)
  2827. {
  2828. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  2829. defined(WOLFSSL_KEY_GEN)
  2830. byte priv[ED448_PRV_KEY_SIZE];
  2831. unsigned int privSz = (unsigned int)sizeof(priv);
  2832. byte pub[ED448_PUB_KEY_SIZE];
  2833. unsigned int pubSz = (unsigned int)sizeof(pub);
  2834. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  2835. const char* msg = TEST_STRING;
  2836. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  2837. byte sig[ED448_SIG_SIZE];
  2838. unsigned int sigSz = (unsigned int)sizeof(sig);
  2839. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  2840. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  2841. WOLFSSL_SUCCESS);
  2842. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  2843. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  2844. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  2845. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  2846. &sigSz), WOLFSSL_SUCCESS);
  2847. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  2848. #ifdef HAVE_ED448_VERIFY
  2849. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  2850. sigSz), WOLFSSL_SUCCESS);
  2851. #endif /* HAVE_ED448_VERIFY */
  2852. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  2853. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  2854. }
  2855. #endif /* OPENSSL_EXTRA */
  2856. #include <wolfssl/openssl/pem.h>
  2857. /*----------------------------------------------------------------------------*
  2858. | EVP
  2859. *----------------------------------------------------------------------------*/
  2860. static void test_wolfSSL_EVP_PKEY_print_public(void)
  2861. {
  2862. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  2863. WOLFSSL_BIO* rbio = NULL;
  2864. WOLFSSL_BIO* wbio = NULL;
  2865. WOLFSSL_EVP_PKEY* pkey = NULL;
  2866. char line[256] = { 0 };
  2867. char line1[256] = { 0 };
  2868. int i;
  2869. printf(testingFmt, "EVP_PKEY_print_public()");
  2870. /* test error cases */
  2871. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  2872. /*
  2873. * test RSA public key print
  2874. * in this test, pass '3' for indent
  2875. */
  2876. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  2877. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  2878. sizeof_client_keypub_der_1024);
  2879. AssertNotNull(rbio);
  2880. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2881. AssertNotNull(pkey);
  2882. wbio = BIO_new(BIO_s_mem());
  2883. AssertNotNull(wbio);
  2884. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  2885. BIO_gets(wbio, line, sizeof(line));
  2886. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  2887. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2888. BIO_gets(wbio, line, sizeof(line));
  2889. strcpy(line1, " Modulus:\n");
  2890. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2891. BIO_gets(wbio, line, sizeof(line));
  2892. strcpy(line1, " 00:BC:73:0E:A8:49:F3:74:A2:A9:EF:18:A5:DA:55:\n");
  2893. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2894. /* skip to the end of modulus element*/
  2895. for( i = 0; i < 8 ;i++) {
  2896. BIO_gets(wbio, line, sizeof(line));
  2897. }
  2898. BIO_gets(wbio, line, sizeof(line));
  2899. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  2900. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2901. /* should reach EOF */
  2902. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  2903. EVP_PKEY_free(pkey);
  2904. pkey = NULL;
  2905. BIO_free(rbio);
  2906. BIO_free(wbio);
  2907. rbio = NULL;
  2908. wbio = NULL;
  2909. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  2910. /*
  2911. * test DSA public key print
  2912. */
  2913. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  2914. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  2915. sizeof_dsa_pub_key_der_2048);
  2916. AssertNotNull(rbio);
  2917. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2918. AssertNotNull(pkey);
  2919. wbio = BIO_new(BIO_s_mem());
  2920. AssertNotNull(wbio);
  2921. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  2922. BIO_gets(wbio, line, sizeof(line));
  2923. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  2924. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2925. BIO_gets(wbio, line, sizeof(line));
  2926. strcpy(line1, "pub:\n");
  2927. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2928. BIO_gets(wbio, line, sizeof(line));
  2929. strcpy(line1,
  2930. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  2931. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2932. /* skip to the end of pub element*/
  2933. for( i = 0; i < 17 ;i++) {
  2934. BIO_gets(wbio, line, sizeof(line));
  2935. }
  2936. BIO_gets(wbio, line, sizeof(line));
  2937. strcpy(line1, "P:\n");
  2938. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2939. /* skip to the end of P element*/
  2940. for( i = 0; i < 18 ;i++) {
  2941. BIO_gets(wbio, line, sizeof(line));
  2942. }
  2943. BIO_gets(wbio, line, sizeof(line));
  2944. strcpy(line1, "Q:\n");
  2945. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2946. /* skip to the end of Q element*/
  2947. for( i = 0; i < 3 ;i++) {
  2948. BIO_gets(wbio, line, sizeof(line));
  2949. }
  2950. BIO_gets(wbio, line, sizeof(line));
  2951. strcpy(line1, "G:\n");
  2952. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2953. /* skip to the end of G element*/
  2954. for( i = 0; i < 18 ;i++) {
  2955. BIO_gets(wbio, line, sizeof(line));
  2956. }
  2957. /* should reach EOF */
  2958. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  2959. EVP_PKEY_free(pkey);
  2960. pkey = NULL;
  2961. BIO_free(rbio);
  2962. BIO_free(wbio);
  2963. rbio = NULL;
  2964. wbio = NULL;
  2965. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  2966. /*
  2967. * test ECC public key print
  2968. */
  2969. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  2970. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  2971. sizeof_ecc_clikeypub_der_256);
  2972. AssertNotNull(rbio);
  2973. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  2974. AssertNotNull(pkey);
  2975. wbio = BIO_new(BIO_s_mem());
  2976. AssertNotNull(wbio);
  2977. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  2978. BIO_gets(wbio, line, sizeof(line));
  2979. strcpy(line1, "Public-Key: (256 bit)\n");
  2980. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2981. BIO_gets(wbio, line, sizeof(line));
  2982. strcpy(line1, "pub:\n");
  2983. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2984. BIO_gets(wbio, line, sizeof(line));
  2985. strcpy(line1,
  2986. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  2987. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2988. /* skip to the end of pub element*/
  2989. for( i = 0; i < 4 ;i++) {
  2990. BIO_gets(wbio, line, sizeof(line));
  2991. }
  2992. BIO_gets(wbio, line, sizeof(line));
  2993. strcpy(line1, "ASN1 OID: prime256v1\n");
  2994. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2995. BIO_gets(wbio, line, sizeof(line));
  2996. strcpy(line1, "NIST CURVE: P-256\n");
  2997. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  2998. /* should reach EOF */
  2999. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3000. EVP_PKEY_free(pkey);
  3001. pkey = NULL;
  3002. BIO_free(rbio);
  3003. BIO_free(wbio);
  3004. rbio = NULL;
  3005. wbio = NULL;
  3006. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3007. /*
  3008. * test DH public key print
  3009. */
  3010. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3011. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3012. sizeof_dh_pub_key_der_2048);
  3013. AssertNotNull(rbio);
  3014. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3015. AssertNotNull(pkey);
  3016. wbio = BIO_new(BIO_s_mem());
  3017. AssertNotNull(wbio);
  3018. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3019. BIO_gets(wbio, line, sizeof(line));
  3020. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3021. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3022. BIO_gets(wbio, line, sizeof(line));
  3023. strcpy(line1, "public-key:\n");
  3024. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3025. BIO_gets(wbio, line, sizeof(line));
  3026. strcpy(line1,
  3027. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3028. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3029. /* skip to the end of public-key element*/
  3030. for( i = 0; i < 17 ;i++) {
  3031. BIO_gets(wbio, line, sizeof(line));
  3032. }
  3033. BIO_gets(wbio, line, sizeof(line));
  3034. strcpy(line1, "prime:\n");
  3035. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3036. BIO_gets(wbio, line, sizeof(line));
  3037. strcpy(line1,
  3038. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3039. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3040. /* skip to the end of prime element*/
  3041. for( i = 0; i < 17 ;i++) {
  3042. BIO_gets(wbio, line, sizeof(line));
  3043. }
  3044. BIO_gets(wbio, line, sizeof(line));
  3045. strcpy(line1, "generator: 2 (0x02)\n");
  3046. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3047. /* should reach EOF */
  3048. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3049. EVP_PKEY_free(pkey);
  3050. pkey = NULL;
  3051. BIO_free(rbio);
  3052. BIO_free(wbio);
  3053. rbio = NULL;
  3054. wbio = NULL;
  3055. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3056. /* to prevent "unused variable" warning */
  3057. (void)pkey;
  3058. (void)wbio;
  3059. (void)rbio;
  3060. (void)line;
  3061. (void)line1;
  3062. (void)i;
  3063. printf(resultFmt, passed);
  3064. #endif /* OPENSSL_EXTRA */
  3065. }
  3066. /* Test functions for base64 encode/decode */
  3067. static void test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3068. {
  3069. #if defined(OPENSSL_EXTRA) && \
  3070. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3071. printf(testingFmt, "EVP_ENCODE_CTX_new()");
  3072. EVP_ENCODE_CTX* ctx = NULL;
  3073. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3074. AssertIntEQ( ctx->remaining,0);
  3075. AssertIntEQ( ctx->data[0],0);
  3076. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3077. EVP_ENCODE_CTX_free(ctx);
  3078. printf(resultFmt, passed);
  3079. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3080. }
  3081. static void test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3082. {
  3083. #if defined(OPENSSL_EXTRA) && \
  3084. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3085. printf(testingFmt, "EVP_ENCODE_CTX_free()");
  3086. EVP_ENCODE_CTX* ctx = NULL;
  3087. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3088. EVP_ENCODE_CTX_free(ctx);
  3089. printf(resultFmt, passed);
  3090. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3091. }
  3092. static void test_wolfSSL_EVP_EncodeInit(void)
  3093. {
  3094. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3095. printf(testingFmt, "EVP_EncodeInit()");
  3096. EVP_ENCODE_CTX* ctx = NULL;
  3097. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3098. AssertIntEQ( ctx->remaining,0);
  3099. AssertIntEQ( ctx->data[0],0);
  3100. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3101. /* make ctx dirty */
  3102. ctx->remaining = 10;
  3103. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3104. EVP_EncodeInit(ctx);
  3105. AssertIntEQ( ctx->remaining,0);
  3106. AssertIntEQ( ctx->data[0],0);
  3107. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3108. EVP_ENCODE_CTX_free(ctx);
  3109. printf(resultFmt, passed);
  3110. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3111. }
  3112. static void test_wolfSSL_EVP_EncodeUpdate(void)
  3113. {
  3114. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3115. printf(testingFmt, "EVP_EncodeUpdate()");
  3116. int outl;
  3117. int total;
  3118. const unsigned char plain0[] = {"Th"};
  3119. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3120. const unsigned char plain2[] = {"This is additional data."};
  3121. const unsigned char enc0[] = {"VGg=\n"};
  3122. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3123. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3124. const unsigned char enc2[] =
  3125. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3126. unsigned char encOutBuff[300];
  3127. EVP_ENCODE_CTX* ctx = NULL;
  3128. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3129. EVP_EncodeInit(ctx);
  3130. /* illegal parameter test */
  3131. AssertIntEQ(
  3132. EVP_EncodeUpdate(
  3133. NULL, /* pass NULL as ctx */
  3134. encOutBuff,
  3135. &outl,
  3136. plain1,
  3137. sizeof(plain1)-1),
  3138. 0 /* expected result code 0: fail */
  3139. );
  3140. AssertIntEQ(
  3141. EVP_EncodeUpdate(
  3142. ctx,
  3143. NULL, /* pass NULL as out buff */
  3144. &outl,
  3145. plain1,
  3146. sizeof(plain1)-1),
  3147. 0 /* expected result code 0: fail */
  3148. );
  3149. AssertIntEQ(
  3150. EVP_EncodeUpdate(
  3151. ctx,
  3152. encOutBuff,
  3153. NULL, /* pass NULL as outl */
  3154. plain1,
  3155. sizeof(plain1)-1),
  3156. 0 /* expected result code 0: fail */
  3157. );
  3158. AssertIntEQ(
  3159. EVP_EncodeUpdate(
  3160. ctx,
  3161. encOutBuff,
  3162. &outl,
  3163. NULL, /* pass NULL as in */
  3164. sizeof(plain1)-1),
  3165. 0 /* expected result code 0: fail */
  3166. );
  3167. /* meaningless parameter test */
  3168. AssertIntEQ(
  3169. EVP_EncodeUpdate(
  3170. ctx,
  3171. encOutBuff,
  3172. &outl,
  3173. plain1,
  3174. 0), /* pass zero input */
  3175. 1 /* expected result code 1: success */
  3176. );
  3177. /* very small data encoding test */
  3178. EVP_EncodeInit(ctx);
  3179. AssertIntEQ(
  3180. EVP_EncodeUpdate(
  3181. ctx,
  3182. encOutBuff,
  3183. &outl,
  3184. plain0,
  3185. sizeof(plain0)-1),
  3186. 1 /* expected result code 1: success */
  3187. );
  3188. AssertIntEQ(outl,0);
  3189. EVP_EncodeFinal(
  3190. ctx,
  3191. encOutBuff + outl,
  3192. &outl);
  3193. AssertIntEQ( outl, sizeof(enc0)-1);
  3194. AssertIntEQ(
  3195. XSTRNCMP(
  3196. (const char*)encOutBuff,
  3197. (const char*)enc0,sizeof(enc0) ),
  3198. 0);
  3199. /* pass small size( < 48bytes ) input, then make sure they are not
  3200. * encoded and just stored in ctx
  3201. */
  3202. EVP_EncodeInit(ctx);
  3203. total = 0;
  3204. outl = 0;
  3205. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3206. AssertIntEQ(
  3207. EVP_EncodeUpdate(
  3208. ctx,
  3209. encOutBuff, /* buffer for output */
  3210. &outl, /* size of output */
  3211. plain1, /* input */
  3212. sizeof(plain1)-1), /* size of input */
  3213. 1); /* expected result code 1:success */
  3214. total += outl;
  3215. AssertIntEQ(outl, 0); /* no output expected */
  3216. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3217. AssertTrue(
  3218. XSTRNCMP((const char*)(ctx->data),
  3219. (const char*)plain1,
  3220. ctx->remaining) ==0 );
  3221. AssertTrue(encOutBuff[0] == 0);
  3222. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3223. * the stored data and the new input together
  3224. */
  3225. AssertIntEQ(
  3226. EVP_EncodeUpdate(
  3227. ctx,
  3228. encOutBuff + outl, /* buffer for output */
  3229. &outl, /* size of output */
  3230. plain2, /* additional input */
  3231. sizeof(plain2) -1), /* size of additional input */
  3232. 1); /* expected result code 1:success */
  3233. total += outl;
  3234. AssertIntNE(outl, 0); /* some output is expected this time*/
  3235. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3236. AssertIntEQ(
  3237. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3238. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3239. EVP_EncodeFinal(
  3240. ctx,
  3241. encOutBuff + outl,
  3242. &outl);
  3243. total += outl;
  3244. AssertIntNE(total,0);
  3245. AssertIntNE(outl,0);
  3246. AssertIntEQ(XSTRNCMP(
  3247. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3248. /* test with illeagal parameters */
  3249. outl = 1;
  3250. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3251. AssertIntEQ(outl, 0);
  3252. outl = 1;
  3253. EVP_EncodeFinal(ctx, NULL, &outl);
  3254. AssertIntEQ(outl, 0);
  3255. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3256. EVP_EncodeFinal(NULL, NULL, NULL);
  3257. EVP_ENCODE_CTX_free(ctx);
  3258. printf(resultFmt, passed);
  3259. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3260. }
  3261. static void test_wolfSSL_EVP_EncodeFinal(void)
  3262. {
  3263. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3264. printf(testingFmt, "wolfSSL_EVP_EncodeFinal()");
  3265. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3266. * test_wolfSSL_EVP_EncodeUpdate
  3267. */
  3268. printf(resultFmt, passed);
  3269. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3270. }
  3271. static void test_wolfSSL_EVP_DecodeInit(void)
  3272. {
  3273. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3274. printf(testingFmt, "EVP_DecodeInit()");
  3275. EVP_ENCODE_CTX* ctx = NULL;
  3276. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3277. AssertIntEQ( ctx->remaining,0);
  3278. AssertIntEQ( ctx->data[0],0);
  3279. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3280. /* make ctx dirty */
  3281. ctx->remaining = 10;
  3282. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3283. EVP_DecodeInit(ctx);
  3284. AssertIntEQ( ctx->remaining,0);
  3285. AssertIntEQ( ctx->data[0],0);
  3286. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3287. EVP_ENCODE_CTX_free(ctx);
  3288. printf(resultFmt, passed);
  3289. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3290. }
  3291. static void test_wolfSSL_EVP_DecodeUpdate(void)
  3292. {
  3293. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3294. printf(testingFmt, "EVP_DecodeUpdate()");
  3295. int outl;
  3296. unsigned char decOutBuff[300];
  3297. EVP_ENCODE_CTX* ctx = EVP_ENCODE_CTX_new();
  3298. EVP_DecodeInit(ctx);
  3299. const unsigned char enc1[] =
  3300. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3301. /* const unsigned char plain1[] =
  3302. {"This is a base64 decoding test."} */
  3303. /* illegal parameter tests */
  3304. /* pass NULL as ctx */
  3305. AssertIntEQ(
  3306. EVP_DecodeUpdate(
  3307. NULL, /* pass NULL as ctx */
  3308. decOutBuff,
  3309. &outl,
  3310. enc1,
  3311. sizeof(enc1)-1),
  3312. -1 /* expected result code -1: fail */
  3313. );
  3314. AssertIntEQ( outl, 0);
  3315. /* pass NULL as output */
  3316. AssertIntEQ(
  3317. EVP_DecodeUpdate(
  3318. ctx,
  3319. NULL, /* pass NULL as out buff */
  3320. &outl,
  3321. enc1,
  3322. sizeof(enc1)-1),
  3323. -1 /* expected result code -1: fail */
  3324. );
  3325. AssertIntEQ( outl, 0);
  3326. /* pass NULL as outl */
  3327. AssertIntEQ(
  3328. EVP_DecodeUpdate(
  3329. ctx,
  3330. decOutBuff,
  3331. NULL, /* pass NULL as outl */
  3332. enc1,
  3333. sizeof(enc1)-1),
  3334. -1 /* expected result code -1: fail */
  3335. );
  3336. /* pass NULL as input */
  3337. AssertIntEQ(
  3338. EVP_DecodeUpdate(
  3339. ctx,
  3340. decOutBuff,
  3341. &outl,
  3342. NULL, /* pass NULL as in */
  3343. sizeof(enc1)-1),
  3344. -1 /* expected result code -1: fail */
  3345. );
  3346. AssertIntEQ( outl, 0);
  3347. /* pass zero length input */
  3348. AssertIntEQ(
  3349. EVP_DecodeUpdate(
  3350. ctx,
  3351. decOutBuff,
  3352. &outl,
  3353. enc1,
  3354. 0), /* pass zero as input len */
  3355. 1 /* expected result code 1: success */
  3356. );
  3357. /* decode correct base64 string */
  3358. const unsigned char enc2[] =
  3359. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3360. const unsigned char plain2[] =
  3361. {"This is a base64 decoding test."};
  3362. EVP_EncodeInit(ctx);
  3363. AssertIntEQ(
  3364. EVP_DecodeUpdate(
  3365. ctx,
  3366. decOutBuff,
  3367. &outl,
  3368. enc2,
  3369. sizeof(enc2)-1),
  3370. 0 /* expected result code 0: success */
  3371. );
  3372. AssertIntEQ(outl,sizeof(plain2) -1);
  3373. AssertIntEQ(
  3374. EVP_DecodeFinal(
  3375. ctx,
  3376. decOutBuff + outl,
  3377. &outl),
  3378. 1 /* expected result code 1: success */
  3379. );
  3380. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  3381. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3382. sizeof(plain2) -1 ),0);
  3383. /* decode correct base64 string which does not have '\n' in its last*/
  3384. const unsigned char enc3[] =
  3385. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  3386. const unsigned char plain3[] =
  3387. {"This is a base64 decoding test."}; /* 31 chars */
  3388. EVP_EncodeInit(ctx);
  3389. AssertIntEQ(
  3390. EVP_DecodeUpdate(
  3391. ctx,
  3392. decOutBuff,
  3393. &outl,
  3394. enc3,
  3395. sizeof(enc3)-1),
  3396. 0 /* expected result code 0: success */
  3397. );
  3398. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  3399. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3400. sizeof(plain3) -1 ),0);
  3401. AssertIntEQ(
  3402. EVP_DecodeFinal(
  3403. ctx,
  3404. decOutBuff + outl,
  3405. &outl),
  3406. 1 /* expected result code 1: success */
  3407. );
  3408. AssertIntEQ(outl,0 );
  3409. /* decode string which has a padding char ('=') in the illegal position*/
  3410. const unsigned char enc4[] =
  3411. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  3412. EVP_EncodeInit(ctx);
  3413. AssertIntEQ(
  3414. EVP_DecodeUpdate(
  3415. ctx,
  3416. decOutBuff,
  3417. &outl,
  3418. enc4,
  3419. sizeof(enc4)-1),
  3420. -1 /* expected result code -1: error */
  3421. );
  3422. AssertIntEQ(outl,0);
  3423. /* small data decode test */
  3424. const unsigned char enc00[] = {"VG"};
  3425. const unsigned char enc01[] = {"g=\n"};
  3426. const unsigned char plain4[] = {"Th"};
  3427. EVP_EncodeInit(ctx);
  3428. AssertIntEQ(
  3429. EVP_DecodeUpdate(
  3430. ctx,
  3431. decOutBuff,
  3432. &outl,
  3433. enc00,
  3434. sizeof(enc00)-1),
  3435. 1 /* expected result code 1: success */
  3436. );
  3437. AssertIntEQ(outl,0);
  3438. AssertIntEQ(
  3439. EVP_DecodeUpdate(
  3440. ctx,
  3441. decOutBuff + outl,
  3442. &outl,
  3443. enc01,
  3444. sizeof(enc01)-1),
  3445. 0 /* expected result code 0: success */
  3446. );
  3447. AssertIntEQ(outl,sizeof(plain4)-1);
  3448. /* test with illegal parameters */
  3449. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  3450. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  3451. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  3452. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  3453. EVP_DecodeFinal(
  3454. ctx,
  3455. decOutBuff + outl,
  3456. &outl);
  3457. AssertIntEQ( outl, 0);
  3458. AssertIntEQ(
  3459. XSTRNCMP(
  3460. (const char*)decOutBuff,
  3461. (const char*)plain4,sizeof(plain4)-1 ),
  3462. 0);
  3463. EVP_ENCODE_CTX_free(ctx);
  3464. printf(resultFmt, passed);
  3465. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3466. }
  3467. static void test_wolfSSL_EVP_DecodeFinal(void)
  3468. {
  3469. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3470. printf(testingFmt, "EVP_DecodeFinal()");
  3471. /* tests for wolfSSL_EVP_DecodeFinal are included in
  3472. * test_wolfSSL_EVP_DecodeUpdate
  3473. */
  3474. printf(resultFmt, passed);
  3475. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3476. }
  3477. /* Test function for wolfSSL_EVP_get_cipherbynid.
  3478. */
  3479. #ifdef OPENSSL_EXTRA
  3480. static void test_wolfSSL_EVP_get_cipherbynid(void)
  3481. {
  3482. #ifndef NO_AES
  3483. const WOLFSSL_EVP_CIPHER* c;
  3484. c = wolfSSL_EVP_get_cipherbynid(419);
  3485. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3486. defined(WOLFSSL_AES_128)
  3487. AssertNotNull(c);
  3488. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  3489. #else
  3490. AssertNull(c);
  3491. #endif
  3492. c = wolfSSL_EVP_get_cipherbynid(423);
  3493. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3494. defined(WOLFSSL_AES_192)
  3495. AssertNotNull(c);
  3496. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  3497. #else
  3498. AssertNull(c);
  3499. #endif
  3500. c = wolfSSL_EVP_get_cipherbynid(427);
  3501. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3502. defined(WOLFSSL_AES_256)
  3503. AssertNotNull(c);
  3504. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  3505. #else
  3506. AssertNull(c);
  3507. #endif
  3508. c = wolfSSL_EVP_get_cipherbynid(904);
  3509. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  3510. AssertNotNull(c);
  3511. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  3512. #else
  3513. AssertNull(c);
  3514. #endif
  3515. c = wolfSSL_EVP_get_cipherbynid(905);
  3516. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  3517. AssertNotNull(c);
  3518. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  3519. #else
  3520. AssertNull(c);
  3521. #endif
  3522. c = wolfSSL_EVP_get_cipherbynid(906);
  3523. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  3524. AssertNotNull(c);
  3525. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  3526. #else
  3527. AssertNull(c);
  3528. #endif
  3529. c = wolfSSL_EVP_get_cipherbynid(418);
  3530. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  3531. AssertNotNull(c);
  3532. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  3533. #else
  3534. AssertNull(c);
  3535. #endif
  3536. c = wolfSSL_EVP_get_cipherbynid(422);
  3537. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  3538. AssertNotNull(c);
  3539. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  3540. #else
  3541. AssertNull(c);
  3542. #endif
  3543. c = wolfSSL_EVP_get_cipherbynid(426);
  3544. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  3545. AssertNotNull(c);
  3546. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  3547. #else
  3548. AssertNull(c);
  3549. #endif
  3550. #endif /* !NO_AES */
  3551. #ifndef NO_DES3
  3552. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  3553. #ifdef WOLFSSL_DES_ECB
  3554. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  3555. #endif
  3556. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  3557. #ifdef WOLFSSL_DES_ECB
  3558. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  3559. #endif
  3560. #endif /* !NO_DES3 */
  3561. #ifdef HAVE_IDEA
  3562. AssertNotNull(strcmp("EVP_IDEA_CBC", wolfSSL_EVP_get_cipherbynid(34)));
  3563. #endif
  3564. /* test for nid is out of range */
  3565. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  3566. }
  3567. static void test_wolfSSL_EVP_CIPHER_CTX(void)
  3568. {
  3569. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  3570. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  3571. const EVP_CIPHER *init = EVP_aes_128_cbc();
  3572. const EVP_CIPHER *test;
  3573. byte key[AES_BLOCK_SIZE] = {0};
  3574. byte iv[AES_BLOCK_SIZE] = {0};
  3575. AssertNotNull(ctx);
  3576. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  3577. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  3578. test = EVP_CIPHER_CTX_cipher(ctx);
  3579. AssertTrue(init == test);
  3580. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  3581. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  3582. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  3583. EVP_CIPHER_CTX_free(ctx);
  3584. /* test EVP_CIPHER_CTX_cleanup with NULL */
  3585. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  3586. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  3587. }
  3588. #endif /* OPENSSL_EXTRA */
  3589. /*----------------------------------------------------------------------------*
  3590. | IO
  3591. *----------------------------------------------------------------------------*/
  3592. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  3593. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  3594. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  3595. #define HAVE_IO_TESTS_DEPENDENCIES
  3596. #endif
  3597. /* helper functions */
  3598. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  3599. #ifdef WOLFSSL_SESSION_EXPORT
  3600. #ifdef WOLFSSL_DTLS
  3601. /* set up function for sending session information */
  3602. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  3603. {
  3604. WOLFSSL_CTX* ctx = NULL;
  3605. WOLFSSL* ssl = NULL;
  3606. AssertNotNull(inSsl);
  3607. AssertNotNull(buf);
  3608. AssertIntNE(0, sz);
  3609. /* Set ctx to DTLS 1.2 */
  3610. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  3611. AssertNotNull(ctx);
  3612. ssl = wolfSSL_new(ctx);
  3613. AssertNotNull(ssl);
  3614. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  3615. wolfSSL_free(ssl);
  3616. wolfSSL_CTX_free(ctx);
  3617. (void)userCtx;
  3618. return WOLFSSL_SUCCESS;
  3619. }
  3620. #endif
  3621. /* returns negative value on fail and positive (including 0) on success */
  3622. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  3623. {
  3624. int ret, err, loop_count, count, timeout = 10;
  3625. char msg[] = "I hear you fa shizzle!";
  3626. char input[1024];
  3627. loop_count = ((func_args*)args)->argc;
  3628. err = 0; /* Reset error */
  3629. do {
  3630. #ifdef WOLFSSL_ASYNC_CRYPT
  3631. if (err == WC_PENDING_E) {
  3632. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3633. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3634. }
  3635. #endif
  3636. ret = wolfSSL_accept(ssl);
  3637. err = wolfSSL_get_error(ssl, 0);
  3638. if (err == WOLFSSL_ERROR_WANT_READ ||
  3639. err == WOLFSSL_ERROR_WANT_WRITE) {
  3640. int select_ret;
  3641. err = WC_PENDING_E;
  3642. select_ret = tcp_select(*sockfd, timeout);
  3643. if (select_ret == TEST_TIMEOUT) {
  3644. return WOLFSSL_FATAL_ERROR;
  3645. }
  3646. }
  3647. } while (err == WC_PENDING_E);
  3648. if (ret != WOLFSSL_SUCCESS) {
  3649. char buff[WOLFSSL_MAX_ERROR_SZ];
  3650. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3651. return ret;
  3652. }
  3653. for (count = 0; count < loop_count; count++) {
  3654. int select_ret;
  3655. select_ret = tcp_select(*sockfd, timeout);
  3656. if (select_ret == TEST_TIMEOUT) {
  3657. ret = WOLFSSL_FATAL_ERROR;
  3658. break;
  3659. }
  3660. do {
  3661. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  3662. if (ret > 0) {
  3663. input[ret] = '\0';
  3664. printf("Client message: %s\n", input);
  3665. }
  3666. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3667. do {
  3668. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  3669. return WOLFSSL_FATAL_ERROR;
  3670. }
  3671. err = wolfSSL_get_error(ssl, ret);
  3672. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3673. }
  3674. return ret;
  3675. }
  3676. #endif /* WOLFSSL_SESSION_EXPORT */
  3677. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  3678. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  3679. defined(HAVE_AES_CBC)
  3680. typedef struct openssl_key_ctx {
  3681. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  3682. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  3683. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  3684. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  3685. } openssl_key_ctx;
  3686. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  3687. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  3688. static WC_INLINE int OpenSSLTicketInit(void)
  3689. {
  3690. int ret = wc_InitRng(&myOpenSSLKey_rng);
  3691. if (ret != 0) return ret;
  3692. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  3693. sizeof(myOpenSSLKey_ctx.name));
  3694. if (ret != 0) return ret;
  3695. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  3696. sizeof(myOpenSSLKey_ctx.key));
  3697. if (ret != 0) return ret;
  3698. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  3699. sizeof(myOpenSSLKey_ctx.hmacKey));
  3700. if (ret != 0) return ret;
  3701. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  3702. sizeof(myOpenSSLKey_ctx.iv));
  3703. if (ret != 0) return ret;
  3704. return 0;
  3705. }
  3706. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  3707. byte name[WOLFSSL_TICKET_NAME_SZ],
  3708. byte iv[WOLFSSL_TICKET_IV_SZ],
  3709. WOLFSSL_EVP_CIPHER_CTX *ectx,
  3710. WOLFSSL_HMAC_CTX *hctx, int enc) {
  3711. (void)ssl;
  3712. if (enc) {
  3713. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  3714. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  3715. }
  3716. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  3717. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  3718. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  3719. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  3720. return 0;
  3721. }
  3722. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  3723. if (enc)
  3724. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3725. else
  3726. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3727. return 1;
  3728. }
  3729. static WC_INLINE void OpenSSLTicketCleanup(void)
  3730. {
  3731. wc_FreeRng(&myOpenSSLKey_rng);
  3732. }
  3733. #endif
  3734. #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  3735. #ifdef WC_SHA512_DIGEST_SIZE
  3736. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  3737. #else
  3738. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  3739. #endif
  3740. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  3741. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  3742. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  3743. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  3744. #endif
  3745. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  3746. {
  3747. SOCKET_T sockfd = 0;
  3748. SOCKET_T clientfd = 0;
  3749. word16 port;
  3750. callback_functions* cbf;
  3751. WOLFSSL_CTX* ctx = 0;
  3752. WOLFSSL* ssl = 0;
  3753. char msg[] = "I hear you fa shizzle!";
  3754. char input[1024];
  3755. int idx;
  3756. int ret, err = 0;
  3757. int sharedCtx = 0;
  3758. #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  3759. size_t msg_len = 0;
  3760. #endif
  3761. #ifdef WOLFSSL_TIRTOS
  3762. fdOpenSession(Task_self());
  3763. #endif
  3764. ((func_args*)args)->return_code = TEST_FAIL;
  3765. cbf = ((func_args*)args)->callbacks;
  3766. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3767. if (cbf != NULL && cbf->ctx) {
  3768. ctx = cbf->ctx;
  3769. sharedCtx = 1;
  3770. }
  3771. else
  3772. #endif
  3773. {
  3774. WOLFSSL_METHOD* method = NULL;
  3775. if (cbf != NULL && cbf->method != NULL) {
  3776. method = cbf->method();
  3777. }
  3778. else {
  3779. method = wolfSSLv23_server_method();
  3780. }
  3781. ctx = wolfSSL_CTX_new(method);
  3782. }
  3783. if (ctx == NULL) {
  3784. goto done;
  3785. }
  3786. #if defined(HAVE_SESSION_TICKET) && \
  3787. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  3788. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  3789. OpenSSLTicketInit();
  3790. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  3791. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  3792. TicketInit();
  3793. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  3794. #endif
  3795. #endif
  3796. #if defined(USE_WINDOWS_API)
  3797. port = ((func_args*)args)->signal->port;
  3798. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  3799. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  3800. /* Let tcp_listen assign port */
  3801. port = 0;
  3802. #else
  3803. /* Use default port */
  3804. port = wolfSSLPort;
  3805. #endif
  3806. /* do it here to detect failure */
  3807. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  3808. CloseSocket(sockfd);
  3809. wolfSSL_CTX_set_verify(ctx,
  3810. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  3811. #ifdef WOLFSSL_ENCRYPTED_KEYS
  3812. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  3813. #endif
  3814. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  3815. != WOLFSSL_SUCCESS) {
  3816. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  3817. goto done;
  3818. }
  3819. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3820. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3821. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3822. #else
  3823. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3824. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3825. #endif
  3826. /*err_sys("can't load server cert chain file, "
  3827. "Please run from wolfSSL home dir");*/
  3828. goto done;
  3829. }
  3830. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3831. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3832. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3833. #else
  3834. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3835. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3836. #endif
  3837. /*err_sys("can't load server key file, "
  3838. "Please run from wolfSSL home dir");*/
  3839. goto done;
  3840. }
  3841. /* call ctx setup callback */
  3842. if (cbf != NULL && cbf->ctx_ready != NULL) {
  3843. cbf->ctx_ready(ctx);
  3844. }
  3845. ssl = wolfSSL_new(ctx);
  3846. if (ssl == NULL) {
  3847. goto done;
  3848. }
  3849. #ifdef WOLFSSL_SESSION_EXPORT
  3850. /* only add in more complex nonblocking case with session export tests */
  3851. if (args && ((func_args*)args)->argc > 0) {
  3852. /* set as nonblock and time out for waiting on read/write */
  3853. tcp_set_nonblocking(&clientfd);
  3854. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  3855. }
  3856. #endif
  3857. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3858. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  3859. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3860. #else
  3861. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  3862. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3863. #endif
  3864. /*err_sys("can't load server cert chain file, "
  3865. "Please run from wolfSSL home dir");*/
  3866. goto done;
  3867. }
  3868. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3869. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  3870. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3871. #else
  3872. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  3873. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  3874. #endif
  3875. /*err_sys("can't load server key file, "
  3876. "Please run from wolfSSL home dir");*/
  3877. goto done;
  3878. }
  3879. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  3880. /*err_sys("SSL_set_fd failed");*/
  3881. goto done;
  3882. }
  3883. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  3884. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  3885. #elif !defined(NO_DH)
  3886. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  3887. #endif
  3888. /* call ssl setup callback */
  3889. if (cbf != NULL && cbf->ssl_ready != NULL) {
  3890. cbf->ssl_ready(ssl);
  3891. }
  3892. #ifdef WOLFSSL_SESSION_EXPORT
  3893. /* only add in more complex nonblocking case with session export tests */
  3894. if (((func_args*)args)->argc > 0) {
  3895. ret = nonblocking_accept_read(args, ssl, &clientfd);
  3896. if (ret >= 0) {
  3897. ((func_args*)args)->return_code = TEST_SUCCESS;
  3898. }
  3899. #ifdef WOLFSSL_TIRTOS
  3900. Task_yield();
  3901. #endif
  3902. goto done;
  3903. }
  3904. #endif
  3905. #ifdef WOLFSSL_ASYNC_CRYPT
  3906. err = 0; /* Reset error */
  3907. #endif
  3908. do {
  3909. #ifdef WOLFSSL_ASYNC_CRYPT
  3910. if (err == WC_PENDING_E) {
  3911. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3912. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3913. }
  3914. #endif
  3915. ret = wolfSSL_accept(ssl);
  3916. err = wolfSSL_get_error(ssl, 0);
  3917. } while (err == WC_PENDING_E);
  3918. if (ret != WOLFSSL_SUCCESS) {
  3919. char buff[WOLFSSL_MAX_ERROR_SZ];
  3920. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3921. /*err_sys("SSL_accept failed");*/
  3922. goto done;
  3923. }
  3924. #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  3925. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  3926. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  3927. AssertIntGE(msg_len, 0);
  3928. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  3929. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  3930. AssertIntGE(msg_len, 0);
  3931. #endif
  3932. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  3933. if (idx > 0) {
  3934. input[idx] = '\0';
  3935. printf("Client message: %s\n", input);
  3936. }
  3937. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  3938. /*err_sys("SSL_write failed");*/
  3939. #ifdef WOLFSSL_TIRTOS
  3940. return;
  3941. #else
  3942. return 0;
  3943. #endif
  3944. }
  3945. if (cbf != NULL && cbf->on_result != NULL)
  3946. cbf->on_result(ssl);
  3947. #ifdef WOLFSSL_TIRTOS
  3948. Task_yield();
  3949. #endif
  3950. ((func_args*)args)->return_code = TEST_SUCCESS;
  3951. done:
  3952. wolfSSL_shutdown(ssl);
  3953. wolfSSL_free(ssl);
  3954. if (!sharedCtx)
  3955. wolfSSL_CTX_free(ctx);
  3956. CloseSocket(clientfd);
  3957. #ifdef WOLFSSL_TIRTOS
  3958. fdCloseSession(Task_self());
  3959. #endif
  3960. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  3961. && defined(HAVE_THREAD_LS)
  3962. wc_ecc_fp_free(); /* free per thread cache */
  3963. #endif
  3964. #if defined(HAVE_SESSION_TICKET) && \
  3965. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  3966. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  3967. OpenSSLTicketCleanup();
  3968. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  3969. TicketCleanup();
  3970. #endif
  3971. #endif
  3972. #ifndef WOLFSSL_TIRTOS
  3973. return 0;
  3974. #endif
  3975. }
  3976. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  3977. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  3978. {
  3979. SOCKET_T sockfd = 0;
  3980. SOCKET_T clientfd = 0;
  3981. word16 port;
  3982. callback_functions* cbf;
  3983. WOLFSSL_CTX* ctx = 0;
  3984. WOLFSSL* ssl = 0;
  3985. char msg[] = "I hear you fa shizzle!";
  3986. char input[1024];
  3987. int idx;
  3988. int ret, err = 0;
  3989. int sharedCtx = 0;
  3990. int loop_count = ((func_args*)args)->argc;
  3991. int count = 0;
  3992. #ifdef WOLFSSL_TIRTOS
  3993. fdOpenSession(Task_self());
  3994. #endif
  3995. ((func_args*)args)->return_code = TEST_FAIL;
  3996. cbf = ((func_args*)args)->callbacks;
  3997. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  3998. if (cbf != NULL && cbf->ctx) {
  3999. ctx = cbf->ctx;
  4000. sharedCtx = 1;
  4001. }
  4002. else
  4003. #endif
  4004. {
  4005. WOLFSSL_METHOD* method = NULL;
  4006. if (cbf != NULL && cbf->method != NULL) {
  4007. method = cbf->method();
  4008. }
  4009. else {
  4010. method = wolfSSLv23_server_method();
  4011. }
  4012. ctx = wolfSSL_CTX_new(method);
  4013. }
  4014. #if defined(USE_WINDOWS_API)
  4015. port = ((func_args*)args)->signal->port;
  4016. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4017. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4018. /* Let tcp_listen assign port */
  4019. port = 0;
  4020. #else
  4021. /* Use default port */
  4022. port = wolfSSLPort;
  4023. #endif
  4024. wolfSSL_CTX_set_verify(ctx,
  4025. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4026. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4027. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4028. #endif
  4029. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4030. != WOLFSSL_SUCCESS) {
  4031. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4032. goto done;
  4033. }
  4034. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4035. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4036. /*err_sys("can't load server cert chain file, "
  4037. "Please run from wolfSSL home dir");*/
  4038. goto done;
  4039. }
  4040. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4041. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4042. /*err_sys("can't load server key file, "
  4043. "Please run from wolfSSL home dir");*/
  4044. goto done;
  4045. }
  4046. /* call ctx setup callback */
  4047. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4048. cbf->ctx_ready(ctx);
  4049. }
  4050. while(count != loop_count) {
  4051. ssl = wolfSSL_new(ctx);
  4052. if (ssl == NULL) {
  4053. goto done;
  4054. }
  4055. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4056. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4057. /*err_sys("can't load server cert chain file, "
  4058. "Please run from wolfSSL home dir");*/
  4059. goto done;
  4060. }
  4061. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4062. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4063. /*err_sys("can't load server key file, "
  4064. "Please run from wolfSSL home dir");*/
  4065. goto done;
  4066. }
  4067. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4068. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4069. #elif !defined(NO_DH)
  4070. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4071. #endif
  4072. /* call ssl setup callback */
  4073. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4074. cbf->ssl_ready(ssl);
  4075. }
  4076. /* do it here to detect failure */
  4077. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4078. CloseSocket(sockfd);
  4079. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4080. /*err_sys("SSL_set_fd failed");*/
  4081. goto done;
  4082. }
  4083. #ifdef WOLFSSL_ASYNC_CRYPT
  4084. err = 0; /* Reset error */
  4085. #endif
  4086. do {
  4087. #ifdef WOLFSSL_ASYNC_CRYPT
  4088. if (err == WC_PENDING_E) {
  4089. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4090. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4091. }
  4092. #endif
  4093. ret = wolfSSL_accept(ssl);
  4094. err = wolfSSL_get_error(ssl, 0);
  4095. } while (err == WC_PENDING_E);
  4096. if (ret != WOLFSSL_SUCCESS) {
  4097. char buff[WOLFSSL_MAX_ERROR_SZ];
  4098. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4099. /*err_sys("SSL_accept failed");*/
  4100. goto done;
  4101. }
  4102. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4103. if (idx > 0) {
  4104. input[idx] = '\0';
  4105. printf("Client message: %s\n", input);
  4106. }
  4107. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4108. /*err_sys("SSL_write failed");*/
  4109. #ifdef WOLFSSL_TIRTOS
  4110. return;
  4111. #else
  4112. return 0;
  4113. #endif
  4114. }
  4115. /* free ssl for this connection */
  4116. wolfSSL_shutdown(ssl);
  4117. wolfSSL_free(ssl); ssl = NULL;
  4118. CloseSocket(clientfd);
  4119. count++;
  4120. }
  4121. #ifdef WOLFSSL_TIRTOS
  4122. Task_yield();
  4123. #endif
  4124. ((func_args*)args)->return_code = TEST_SUCCESS;
  4125. done:
  4126. if(ssl != NULL) {
  4127. wolfSSL_shutdown(ssl);
  4128. wolfSSL_free(ssl);
  4129. }
  4130. if (!sharedCtx)
  4131. wolfSSL_CTX_free(ctx);
  4132. CloseSocket(clientfd);
  4133. #ifdef WOLFSSL_TIRTOS
  4134. fdCloseSession(Task_self());
  4135. #endif
  4136. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4137. && defined(HAVE_THREAD_LS)
  4138. wc_ecc_fp_free(); /* free per thread cache */
  4139. #endif
  4140. #ifndef WOLFSSL_TIRTOS
  4141. return 0;
  4142. #endif
  4143. }
  4144. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4145. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4146. static void test_client_nofail(void* args, cbType cb)
  4147. {
  4148. SOCKET_T sockfd = 0;
  4149. callback_functions* cbf;
  4150. WOLFSSL_CTX* ctx = 0;
  4151. WOLFSSL* ssl = 0;
  4152. WOLFSSL_CIPHER* cipher;
  4153. char msg[64] = "hello wolfssl!";
  4154. char reply[1024];
  4155. int input;
  4156. int msgSz = (int)XSTRLEN(msg);
  4157. int ret, err = 0;
  4158. int cipherSuite;
  4159. int sharedCtx = 0;
  4160. const char* cipherName1, *cipherName2;
  4161. #ifdef WOLFSSL_TIRTOS
  4162. fdOpenSession(Task_self());
  4163. #endif
  4164. ((func_args*)args)->return_code = TEST_FAIL;
  4165. cbf = ((func_args*)args)->callbacks;
  4166. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4167. if (cbf != NULL && cbf->ctx) {
  4168. ctx = cbf->ctx;
  4169. sharedCtx = cbf->isSharedCtx;
  4170. }
  4171. else
  4172. #endif
  4173. {
  4174. WOLFSSL_METHOD* method = NULL;
  4175. if (cbf != NULL && cbf->method != NULL) {
  4176. method = cbf->method();
  4177. }
  4178. else {
  4179. method = wolfSSLv23_client_method();
  4180. }
  4181. ctx = wolfSSL_CTX_new(method);
  4182. }
  4183. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4184. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4185. #endif
  4186. /* Do connect here so server detects failures */
  4187. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4188. 0, 0, NULL);
  4189. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4190. {
  4191. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4192. goto done;
  4193. }
  4194. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4195. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4196. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4197. #else
  4198. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4199. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4200. #endif
  4201. /*err_sys("can't load client cert file, "
  4202. "Please run from wolfSSL home dir");*/
  4203. goto done;
  4204. }
  4205. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4206. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4207. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4208. #else
  4209. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4210. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4211. #endif
  4212. /*err_sys("can't load client key file, "
  4213. "Please run from wolfSSL home dir");*/
  4214. goto done;
  4215. }
  4216. /* call ctx setup callback */
  4217. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4218. cbf->ctx_ready(ctx);
  4219. }
  4220. ssl = wolfSSL_new(ctx);
  4221. if (ssl == NULL) {
  4222. goto done;
  4223. }
  4224. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4225. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4226. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4227. #else
  4228. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4229. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4230. #endif
  4231. /*err_sys("can't load client cert file, "
  4232. "Please run from wolfSSL home dir");*/
  4233. goto done;
  4234. }
  4235. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4236. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4237. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4238. #else
  4239. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4240. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4241. #endif
  4242. /*err_sys("can't load client key file, "
  4243. "Please run from wolfSSL home dir");*/
  4244. goto done;
  4245. }
  4246. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4247. /*err_sys("SSL_set_fd failed");*/
  4248. goto done;
  4249. }
  4250. /* call ssl setup callback */
  4251. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4252. cbf->ssl_ready(ssl);
  4253. }
  4254. #ifdef WOLFSSL_ASYNC_CRYPT
  4255. err = 0; /* Reset error */
  4256. #endif
  4257. do {
  4258. #ifdef WOLFSSL_ASYNC_CRYPT
  4259. if (err == WC_PENDING_E) {
  4260. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4261. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4262. }
  4263. #endif
  4264. ret = wolfSSL_connect(ssl);
  4265. err = wolfSSL_get_error(ssl, 0);
  4266. } while (err == WC_PENDING_E);
  4267. if (ret != WOLFSSL_SUCCESS) {
  4268. char buff[WOLFSSL_MAX_ERROR_SZ];
  4269. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4270. /*err_sys("SSL_connect failed");*/
  4271. goto done;
  4272. }
  4273. /* test the various get cipher methods */
  4274. /* Internal cipher suite names */
  4275. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  4276. cipherName1 = wolfSSL_get_cipher_name(ssl);
  4277. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  4278. (cipherSuite >> 8), cipherSuite & 0xFF);
  4279. AssertStrEQ(cipherName1, cipherName2);
  4280. /* IANA Cipher Suites Names */
  4281. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  4282. then it's the internal cipher suite name */
  4283. cipher = wolfSSL_get_current_cipher(ssl);
  4284. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  4285. cipherName2 = wolfSSL_get_cipher(ssl);
  4286. AssertStrEQ(cipherName1, cipherName2);
  4287. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  4288. !defined(WOLFSSL_QT)
  4289. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  4290. (cipherSuite >> 8), cipherSuite & 0xFF);
  4291. AssertStrEQ(cipherName1, cipherName2);
  4292. #endif
  4293. if (cb != NULL)
  4294. (cb)(ctx, ssl);
  4295. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4296. /*err_sys("SSL_write failed");*/
  4297. goto done;
  4298. }
  4299. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4300. if (input > 0) {
  4301. reply[input] = '\0';
  4302. printf("Server response: %s\n", reply);
  4303. }
  4304. ((func_args*)args)->return_code = TEST_SUCCESS;
  4305. done:
  4306. wolfSSL_free(ssl);
  4307. if (!sharedCtx)
  4308. wolfSSL_CTX_free(ctx);
  4309. CloseSocket(sockfd);
  4310. #ifdef WOLFSSL_TIRTOS
  4311. fdCloseSession(Task_self());
  4312. #endif
  4313. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4314. && defined(HAVE_THREAD_LS)
  4315. wc_ecc_fp_free(); /* free per thread cache */
  4316. #endif
  4317. return;
  4318. }
  4319. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4320. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  4321. {
  4322. SOCKET_T sockfd = 0;
  4323. callback_functions* cbf;
  4324. WOLFSSL_CTX* ctx = 0;
  4325. WOLFSSL* ssl = 0;
  4326. WOLFSSL_SESSION* session = NULL;
  4327. char msg[64] = "hello wolfssl!";
  4328. char reply[1024];
  4329. int input;
  4330. int msgSz = (int)XSTRLEN(msg);
  4331. int ret, err = 0;
  4332. int sharedCtx = 0;
  4333. #ifdef WOLFSSL_TIRTOS
  4334. fdOpenSession(Task_self());
  4335. #endif
  4336. ((func_args*)args)->return_code = TEST_FAIL;
  4337. cbf = ((func_args*)args)->callbacks;
  4338. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4339. if (cbf != NULL && cbf->ctx) {
  4340. ctx = cbf->ctx;
  4341. sharedCtx = 1;
  4342. }
  4343. else
  4344. #endif
  4345. {
  4346. WOLFSSL_METHOD* method = NULL;
  4347. if (cbf != NULL && cbf->method != NULL) {
  4348. method = cbf->method();
  4349. }
  4350. else {
  4351. method = wolfSSLv23_client_method();
  4352. }
  4353. ctx = wolfSSL_CTX_new(method);
  4354. }
  4355. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4356. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4357. #endif
  4358. /* Do connect here so server detects failures */
  4359. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4360. 0, 0, NULL);
  4361. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4362. {
  4363. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4364. goto done;
  4365. }
  4366. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4367. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4368. /*err_sys("can't load client cert file, "
  4369. "Please run from wolfSSL home dir");*/
  4370. goto done;
  4371. }
  4372. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4373. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4374. /*err_sys("can't load client key file, "
  4375. "Please run from wolfSSL home dir");*/
  4376. goto done;
  4377. }
  4378. /* call ctx setup callback */
  4379. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4380. cbf->ctx_ready(ctx);
  4381. }
  4382. ssl = wolfSSL_new(ctx);
  4383. if (ssl == NULL) {
  4384. goto done;
  4385. }
  4386. /* keep handshakre resources for re-using WOLFSSL obj */
  4387. wolfSSL_KeepArrays(ssl);
  4388. if(wolfSSL_KeepHandshakeResources(ssl)) {
  4389. /* err_sys("SSL_KeepHandshakeResources failed"); */
  4390. goto done;
  4391. }
  4392. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4393. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4394. /*err_sys("can't load client cert file, "
  4395. "Please run from wolfSSL home dir");*/
  4396. goto done;
  4397. }
  4398. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4399. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4400. /*err_sys("can't load client key file, "
  4401. "Please run from wolfSSL home dir");*/
  4402. goto done;
  4403. }
  4404. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4405. /*err_sys("SSL_set_fd failed");*/
  4406. goto done;
  4407. }
  4408. /* call ssl setup callback */
  4409. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4410. cbf->ssl_ready(ssl);
  4411. }
  4412. #ifdef WOLFSSL_ASYNC_CRYPT
  4413. err = 0; /* Reset error */
  4414. #endif
  4415. do {
  4416. #ifdef WOLFSSL_ASYNC_CRYPT
  4417. if (err == WC_PENDING_E) {
  4418. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4419. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4420. }
  4421. #endif
  4422. ret = wolfSSL_connect(ssl);
  4423. err = wolfSSL_get_error(ssl, 0);
  4424. } while (err == WC_PENDING_E);
  4425. if (ret != WOLFSSL_SUCCESS) {
  4426. char buff[WOLFSSL_MAX_ERROR_SZ];
  4427. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4428. /*err_sys("SSL_connect failed");*/
  4429. goto done;
  4430. }
  4431. /* Build first session */
  4432. if (cb != NULL)
  4433. ((cbType)cb)(ctx, ssl);
  4434. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4435. /*err_sys("SSL_write failed");*/
  4436. goto done;
  4437. }
  4438. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4439. if (input > 0) {
  4440. reply[input] = '\0';
  4441. printf("Server response: %s\n", reply);
  4442. }
  4443. /* Session Resumption by re-using WOLFSSL object */
  4444. wolfSSL_set_quiet_shutdown(ssl, 1);
  4445. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  4446. /* err_sys ("SSL shutdown failed"); */
  4447. goto done;
  4448. }
  4449. session = wolfSSL_get_session(ssl);
  4450. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  4451. /* err_sys ("SSL_clear failed"); */
  4452. goto done;
  4453. }
  4454. wolfSSL_set_session(ssl, session);
  4455. /* close socket once */
  4456. CloseSocket(sockfd);
  4457. sockfd = 0;
  4458. /* wait until server ready */
  4459. wait_tcp_ready((func_args*)server_args);
  4460. printf("session resumption\n");
  4461. /* Do re-connect */
  4462. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4463. 0, 0, NULL);
  4464. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4465. /*err_sys("SSL_set_fd failed");*/
  4466. goto done;
  4467. }
  4468. #ifdef WOLFSSL_ASYNC_CRYPT
  4469. err = 0; /* Reset error */
  4470. #endif
  4471. do {
  4472. #ifdef WOLFSSL_ASYNC_CRYPT
  4473. if (err == WC_PENDING_E) {
  4474. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4475. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4476. }
  4477. #endif
  4478. ret = wolfSSL_connect(ssl);
  4479. err = wolfSSL_get_error(ssl, 0);
  4480. } while (err == WC_PENDING_E);
  4481. if (ret != WOLFSSL_SUCCESS) {
  4482. char buff[WOLFSSL_MAX_ERROR_SZ];
  4483. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4484. /*err_sys("SSL_connect failed");*/
  4485. goto done;
  4486. }
  4487. /* Build first session */
  4488. if (cb != NULL)
  4489. ((cbType)cb)(ctx, ssl);
  4490. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4491. /*err_sys("SSL_write failed");*/
  4492. goto done;
  4493. }
  4494. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4495. if (input > 0) {
  4496. reply[input] = '\0';
  4497. printf("Server response: %s\n", reply);
  4498. }
  4499. ((func_args*)args)->return_code = TEST_SUCCESS;
  4500. done:
  4501. wolfSSL_free(ssl);
  4502. if (!sharedCtx)
  4503. wolfSSL_CTX_free(ctx);
  4504. CloseSocket(sockfd);
  4505. #ifdef WOLFSSL_TIRTOS
  4506. fdCloseSession(Task_self());
  4507. #endif
  4508. return;
  4509. }
  4510. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4511. static void test_client_verifyDepth(void* args)
  4512. {
  4513. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  4514. SOCKET_T sockfd = 0;
  4515. callback_functions* cbf;
  4516. WOLFSSL_CTX* ctx = 0;
  4517. WOLFSSL* ssl = 0;
  4518. char msg[64] = "hello wolfssl!";
  4519. char reply[1024];
  4520. int input;
  4521. int msgSz = (int)XSTRLEN(msg);
  4522. int ret, err = 0;
  4523. int verify_depth = ((func_args*)args)->argc;
  4524. ((func_args*)args)->return_code = TEST_FAIL;
  4525. cbf = ((func_args*)args)->callbacks;
  4526. {
  4527. WOLFSSL_METHOD* method = NULL;
  4528. if (cbf != NULL && cbf->method != NULL) {
  4529. method = cbf->method();
  4530. }
  4531. else {
  4532. method = wolfSSLv23_client_method();
  4533. }
  4534. ctx = wolfSSL_CTX_new(method);
  4535. }
  4536. /* Do connect here so server detects failures */
  4537. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4538. 0, 0, NULL);
  4539. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  4540. != WOLFSSL_SUCCESS)
  4541. {
  4542. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4543. goto done;
  4544. }
  4545. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4546. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4547. /*err_sys("can't load client cert file, "
  4548. "Please run from wolfSSL home dir");*/
  4549. goto done;
  4550. }
  4551. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4552. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4553. /*err_sys("can't load client key file, "
  4554. "Please run from wolfSSL home dir");*/
  4555. goto done;
  4556. }
  4557. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  4558. /* set verify depth */
  4559. if (verify_depth == 0) {
  4560. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  4561. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4562. } else if (verify_depth == -1) {
  4563. myVerifyAction = VERIFY_USE_PREVERFIY;
  4564. SSL_CTX_set_verify_depth(ctx, 0);
  4565. } else if (verify_depth > 0) {
  4566. myVerifyAction = VERIFY_USE_PREVERFIY;
  4567. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4568. }
  4569. ssl = wolfSSL_new(ctx);
  4570. if (ssl == NULL) {
  4571. goto done;
  4572. }
  4573. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4574. /*err_sys("SSL_set_fd failed");*/
  4575. goto done;
  4576. }
  4577. #ifdef WOLFSSL_ASYNC_CRYPT
  4578. err = 0; /* Reset error */
  4579. #endif
  4580. do {
  4581. #ifdef WOLFSSL_ASYNC_CRYPT
  4582. if (err == WC_PENDING_E) {
  4583. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4584. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4585. }
  4586. #endif
  4587. ret = wolfSSL_connect(ssl);
  4588. err = wolfSSL_get_error(ssl, 0);
  4589. } while (err == WC_PENDING_E);
  4590. if (ret != WOLFSSL_SUCCESS) {
  4591. char buff[WOLFSSL_MAX_ERROR_SZ];
  4592. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4593. goto done;
  4594. }
  4595. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4596. goto done;
  4597. }
  4598. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4599. if (input > 0) {
  4600. reply[input] = '\0';
  4601. printf("Server response: %s\n", reply);
  4602. }
  4603. ((func_args*)args)->return_code = TEST_SUCCESS;
  4604. done:
  4605. wolfSSL_free(ssl);
  4606. wolfSSL_CTX_free(ctx);
  4607. CloseSocket(sockfd);
  4608. #else
  4609. (void)args;
  4610. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  4611. }
  4612. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  4613. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  4614. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  4615. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  4616. #define HAVE_ALPN_PROTOS_SUPPORT
  4617. #endif
  4618. /* Generic TLS client / server with callbacks for API unit tests
  4619. * Used by SNI / ALPN / crypto callback helper functions */
  4620. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  4621. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  4622. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  4623. #define ENABLE_TLS_CALLBACK_TEST
  4624. #endif
  4625. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  4626. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  4627. /* TLS server for API unit testing - generic */
  4628. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  4629. {
  4630. callback_functions* callbacks = ((func_args*)args)->callbacks;
  4631. WOLFSSL_CTX* ctx = NULL;
  4632. WOLFSSL* ssl = NULL;
  4633. SOCKET_T sfd = 0;
  4634. SOCKET_T cfd = 0;
  4635. word16 port;
  4636. char msg[] = "I hear you fa shizzle!";
  4637. int len = (int) XSTRLEN(msg);
  4638. char input[1024];
  4639. int idx;
  4640. int ret, err = 0;
  4641. ((func_args*)args)->return_code = TEST_FAIL;
  4642. #ifdef WOLFSSL_STATIC_MEMORY
  4643. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  4644. callbacks->memSz > 0) {
  4645. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  4646. callbacks->mem, callbacks->memSz, 0, 1);
  4647. if (ret != WOLFSSL_SUCCESS) {
  4648. printf("CTX static new failed %d\n", ret);
  4649. return 0;
  4650. }
  4651. }
  4652. #endif
  4653. if (ctx == NULL) {
  4654. ctx = wolfSSL_CTX_new(callbacks->method());
  4655. }
  4656. if (ctx == NULL) {
  4657. printf("CTX new failed\n");
  4658. return 0;
  4659. }
  4660. /* set defaults */
  4661. if (callbacks->caPemFile == NULL)
  4662. callbacks->caPemFile = cliCertFile;
  4663. if (callbacks->certPemFile == NULL)
  4664. callbacks->certPemFile = svrCertFile;
  4665. if (callbacks->keyPemFile == NULL)
  4666. callbacks->keyPemFile = svrKeyFile;
  4667. #ifdef WOLFSSL_TIRTOS
  4668. fdOpenSession(Task_self());
  4669. #endif
  4670. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  4671. #if defined(USE_WINDOWS_API)
  4672. port = ((func_args*)args)->signal->port;
  4673. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4674. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4675. /* Let tcp_listen assign port */
  4676. port = 0;
  4677. #else
  4678. /* Use default port */
  4679. port = wolfSSLPort;
  4680. #endif
  4681. wolfSSL_CTX_set_verify(ctx,
  4682. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4683. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4684. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4685. #endif
  4686. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  4687. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  4688. #endif
  4689. AssertIntEQ(WOLFSSL_SUCCESS,
  4690. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  4691. AssertIntEQ(WOLFSSL_SUCCESS,
  4692. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  4693. WOLFSSL_FILETYPE_PEM));
  4694. AssertIntEQ(WOLFSSL_SUCCESS,
  4695. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  4696. WOLFSSL_FILETYPE_PEM));
  4697. if (callbacks->ctx_ready)
  4698. callbacks->ctx_ready(ctx);
  4699. ssl = wolfSSL_new(ctx);
  4700. if (ssl == NULL) {
  4701. printf("SSL new failed\n");
  4702. wolfSSL_CTX_free(ctx);
  4703. return 0;
  4704. }
  4705. if (wolfSSL_dtls(ssl)) {
  4706. SOCKADDR_IN_T cliAddr;
  4707. socklen_t cliLen;
  4708. cliLen = sizeof(cliAddr);
  4709. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  4710. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  4711. (struct sockaddr*)&cliAddr, &cliLen);
  4712. AssertIntGT(idx, 0);
  4713. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4714. }
  4715. else {
  4716. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4717. CloseSocket(sfd);
  4718. }
  4719. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  4720. if (callbacks->loadToSSL) {
  4721. wolfSSL_SetDevId(ssl, callbacks->devId);
  4722. AssertIntEQ(WOLFSSL_SUCCESS,
  4723. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  4724. WOLFSSL_FILETYPE_PEM));
  4725. AssertIntEQ(WOLFSSL_SUCCESS,
  4726. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  4727. WOLFSSL_FILETYPE_PEM));
  4728. }
  4729. #ifdef NO_PSK
  4730. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4731. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4732. #elif !defined(NO_DH)
  4733. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4734. #endif
  4735. #endif
  4736. if (callbacks->ssl_ready)
  4737. callbacks->ssl_ready(ssl);
  4738. #ifdef WOLFSSL_ASYNC_CRYPT
  4739. err = 0; /* Reset error */
  4740. #endif
  4741. do {
  4742. #ifdef WOLFSSL_ASYNC_CRYPT
  4743. if (err == WC_PENDING_E) {
  4744. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4745. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4746. }
  4747. #endif
  4748. ret = wolfSSL_accept(ssl);
  4749. err = wolfSSL_get_error(ssl, 0);
  4750. } while (err == WC_PENDING_E);
  4751. if (ret != WOLFSSL_SUCCESS) {
  4752. char buff[WOLFSSL_MAX_ERROR_SZ];
  4753. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4754. /*err_sys("SSL_accept failed");*/
  4755. }
  4756. else {
  4757. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  4758. input[idx] = 0;
  4759. printf("Client message: %s\n", input);
  4760. }
  4761. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  4762. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  4763. defined(WOLFSSL_DTLS)
  4764. if (wolfSSL_dtls(ssl)) {
  4765. byte* import;
  4766. word32 sz;
  4767. wolfSSL_dtls_export(ssl, NULL, &sz);
  4768. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4769. AssertNotNull(import);
  4770. idx = wolfSSL_dtls_export(ssl, import, &sz);
  4771. AssertIntGE(idx, 0);
  4772. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  4773. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4774. }
  4775. #endif
  4776. #ifdef WOLFSSL_TIRTOS
  4777. Task_yield();
  4778. #endif
  4779. ((func_args*)args)->return_code = TEST_SUCCESS;
  4780. }
  4781. if (callbacks->on_result)
  4782. callbacks->on_result(ssl);
  4783. wolfSSL_shutdown(ssl);
  4784. wolfSSL_free(ssl);
  4785. wolfSSL_CTX_free(ctx);
  4786. CloseSocket(cfd);
  4787. #ifdef WOLFSSL_TIRTOS
  4788. fdCloseSession(Task_self());
  4789. #endif
  4790. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4791. && defined(HAVE_THREAD_LS)
  4792. wc_ecc_fp_free(); /* free per thread cache */
  4793. #endif
  4794. #ifndef WOLFSSL_TIRTOS
  4795. return 0;
  4796. #endif
  4797. }
  4798. /* TLS Client for API unit testing - generic */
  4799. static void run_wolfssl_client(void* args)
  4800. {
  4801. callback_functions* callbacks = ((func_args*)args)->callbacks;
  4802. WOLFSSL_CTX* ctx = NULL;
  4803. WOLFSSL* ssl = NULL;
  4804. SOCKET_T sfd = 0;
  4805. char msg[] = "hello wolfssl server!";
  4806. int len = (int) XSTRLEN(msg);
  4807. char input[1024];
  4808. int ret, err = 0;
  4809. ((func_args*)args)->return_code = TEST_FAIL;
  4810. /* set defaults */
  4811. if (callbacks->caPemFile == NULL)
  4812. callbacks->caPemFile = caCertFile;
  4813. if (callbacks->certPemFile == NULL)
  4814. callbacks->certPemFile = cliCertFile;
  4815. if (callbacks->keyPemFile == NULL)
  4816. callbacks->keyPemFile = cliKeyFile;
  4817. #ifdef WOLFSSL_STATIC_MEMORY
  4818. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  4819. callbacks->memSz > 0) {
  4820. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  4821. callbacks->mem, callbacks->memSz, 0, 1);
  4822. if (ret != WOLFSSL_SUCCESS) {
  4823. printf("CTX static new failed %d\n", ret);
  4824. return;
  4825. }
  4826. }
  4827. #endif
  4828. if (ctx == NULL) {
  4829. ctx = wolfSSL_CTX_new(callbacks->method());
  4830. }
  4831. if (ctx == NULL) {
  4832. printf("CTX new failed\n");
  4833. return;
  4834. }
  4835. #ifdef WOLFSSL_TIRTOS
  4836. fdOpenSession(Task_self());
  4837. #endif
  4838. if (!callbacks->loadToSSL) {
  4839. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  4840. }
  4841. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4842. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4843. #endif
  4844. AssertIntEQ(WOLFSSL_SUCCESS,
  4845. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  4846. if (!callbacks->loadToSSL) {
  4847. AssertIntEQ(WOLFSSL_SUCCESS,
  4848. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  4849. WOLFSSL_FILETYPE_PEM));
  4850. AssertIntEQ(WOLFSSL_SUCCESS,
  4851. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  4852. WOLFSSL_FILETYPE_PEM));
  4853. }
  4854. if (callbacks->ctx_ready)
  4855. callbacks->ctx_ready(ctx);
  4856. ssl = wolfSSL_new(ctx);
  4857. if (wolfSSL_dtls(ssl)) {
  4858. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4859. 1, 0, ssl);
  4860. }
  4861. else {
  4862. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4863. 0, 0, ssl);
  4864. }
  4865. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  4866. if (callbacks->loadToSSL) {
  4867. wolfSSL_SetDevId(ssl, callbacks->devId);
  4868. AssertIntEQ(WOLFSSL_SUCCESS,
  4869. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  4870. WOLFSSL_FILETYPE_PEM));
  4871. AssertIntEQ(WOLFSSL_SUCCESS,
  4872. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  4873. WOLFSSL_FILETYPE_PEM));
  4874. }
  4875. if (callbacks->ssl_ready)
  4876. callbacks->ssl_ready(ssl);
  4877. #ifdef WOLFSSL_ASYNC_CRYPT
  4878. err = 0; /* Reset error */
  4879. #endif
  4880. do {
  4881. #ifdef WOLFSSL_ASYNC_CRYPT
  4882. if (err == WC_PENDING_E) {
  4883. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4884. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4885. }
  4886. #endif
  4887. ret = wolfSSL_connect(ssl);
  4888. err = wolfSSL_get_error(ssl, 0);
  4889. } while (err == WC_PENDING_E);
  4890. if (ret != WOLFSSL_SUCCESS) {
  4891. char buff[WOLFSSL_MAX_ERROR_SZ];
  4892. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4893. /*err_sys("SSL_connect failed");*/
  4894. }
  4895. else {
  4896. #ifdef WOLFSSL_ASYNC_CRYPT
  4897. err = 0; /* Reset error */
  4898. #endif
  4899. do {
  4900. #ifdef WOLFSSL_ASYNC_CRYPT
  4901. if (err == WC_PENDING_E) {
  4902. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4903. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4904. }
  4905. #endif
  4906. ret = wolfSSL_write(ssl, msg, len);
  4907. err = wolfSSL_get_error(ssl, 0);
  4908. } while (err == WC_PENDING_E);
  4909. AssertIntEQ(len, ret);
  4910. #ifdef WOLFSSL_ASYNC_CRYPT
  4911. err = 0; /* Reset error */
  4912. #endif
  4913. do {
  4914. #ifdef WOLFSSL_ASYNC_CRYPT
  4915. if (err == WC_PENDING_E) {
  4916. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4917. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4918. }
  4919. #endif
  4920. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  4921. err = wolfSSL_get_error(ssl, 0);
  4922. } while (err == WC_PENDING_E);
  4923. if (ret > 0) {
  4924. input[ret] = '\0'; /* null term */
  4925. printf("Server response: %s\n", input);
  4926. }
  4927. ((func_args*)args)->return_code = TEST_SUCCESS;
  4928. }
  4929. if (callbacks->on_result)
  4930. callbacks->on_result(ssl);
  4931. wolfSSL_free(ssl);
  4932. wolfSSL_CTX_free(ctx);
  4933. CloseSocket(sfd);
  4934. #ifdef WOLFSSL_TIRTOS
  4935. fdCloseSession(Task_self());
  4936. #endif
  4937. }
  4938. #endif /* ENABLE_TLS_CALLBACK_TEST */
  4939. static void test_wolfSSL_read_write(void)
  4940. {
  4941. /* The unit testing for read and write shall happen simultaneously, since
  4942. * one can't do anything with one without the other. (Except for a failure
  4943. * test case.) This function will call all the others that will set up,
  4944. * execute, and report their test findings.
  4945. *
  4946. * Set up the success case first. This function will become the template
  4947. * for the other tests. This should eventually be renamed
  4948. *
  4949. * The success case isn't interesting, how can this fail?
  4950. * - Do not give the client context a CA certificate. The connect should
  4951. * fail. Do not need server for this?
  4952. * - Using NULL for the ssl object on server. Do not need client for this.
  4953. * - Using NULL for the ssl object on client. Do not need server for this.
  4954. * - Good ssl objects for client and server. Client write() without server
  4955. * read().
  4956. * - Good ssl objects for client and server. Server write() without client
  4957. * read().
  4958. * - Forgetting the password callback?
  4959. */
  4960. tcp_ready ready;
  4961. func_args client_args;
  4962. func_args server_args;
  4963. THREAD_TYPE serverThread;
  4964. XMEMSET(&client_args, 0, sizeof(func_args));
  4965. XMEMSET(&server_args, 0, sizeof(func_args));
  4966. #ifdef WOLFSSL_TIRTOS
  4967. fdOpenSession(Task_self());
  4968. #endif
  4969. StartTCP();
  4970. InitTcpReady(&ready);
  4971. #if defined(USE_WINDOWS_API)
  4972. /* use RNG to get random port if using windows */
  4973. ready.port = GetRandomPort();
  4974. #endif
  4975. server_args.signal = &ready;
  4976. client_args.signal = &ready;
  4977. start_thread(test_server_nofail, &server_args, &serverThread);
  4978. wait_tcp_ready(&server_args);
  4979. test_client_nofail(&client_args, NULL);
  4980. join_thread(serverThread);
  4981. AssertTrue(client_args.return_code);
  4982. AssertTrue(server_args.return_code);
  4983. FreeTcpReady(&ready);
  4984. #ifdef WOLFSSL_TIRTOS
  4985. fdOpenSession(Task_self());
  4986. #endif
  4987. }
  4988. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4989. static void test_wolfSSL_reuse_WOLFSSLobj(void)
  4990. {
  4991. /* The unit test for session resumption by re-using WOLFSSL object.
  4992. * WOLFSSL object is not cleared after first session. It re-use the obeject
  4993. * for second connection.
  4994. */
  4995. tcp_ready ready;
  4996. func_args client_args;
  4997. func_args server_args;
  4998. THREAD_TYPE serverThread;
  4999. XMEMSET(&client_args, 0, sizeof(func_args));
  5000. XMEMSET(&server_args, 0, sizeof(func_args));
  5001. #ifdef WOLFSSL_TIRTOS
  5002. fdOpenSession(Task_self());
  5003. #endif
  5004. StartTCP();
  5005. InitTcpReady(&ready);
  5006. #if defined(USE_WINDOWS_API)
  5007. /* use RNG to get random port if using windows */
  5008. ready.port = GetRandomPort();
  5009. #endif
  5010. server_args.signal = &ready;
  5011. client_args.signal = &ready;
  5012. /* the var is used for loop number */
  5013. server_args.argc = 2;
  5014. start_thread(test_server_loop, &server_args, &serverThread);
  5015. wait_tcp_ready(&server_args);
  5016. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5017. join_thread(serverThread);
  5018. AssertTrue(client_args.return_code);
  5019. AssertTrue(server_args.return_code);
  5020. FreeTcpReady(&ready);
  5021. #ifdef WOLFSSL_TIRTOS
  5022. fdOpenSession(Task_self());
  5023. #endif
  5024. }
  5025. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5026. static void test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5027. {
  5028. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5029. /* This unit test is to check set verify Depth */
  5030. tcp_ready ready;
  5031. func_args client_args;
  5032. func_args server_args;
  5033. THREAD_TYPE serverThread;
  5034. callback_functions client_cbf;
  5035. XMEMSET(&client_args, 0, sizeof(func_args));
  5036. XMEMSET(&server_args, 0, sizeof(func_args));
  5037. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5038. printf(testingFmt, "test_wolfSSL_CTX_verifyDepth_ServerClient()\n");
  5039. #ifdef WOLFSSL_TLS13
  5040. client_cbf.method = wolfTLSv1_3_client_method;
  5041. #endif /* WOLFSSL_TLS13 */
  5042. client_args.callbacks = &client_cbf;
  5043. StartTCP();
  5044. InitTcpReady(&ready);
  5045. #if defined(USE_WINDOWS_API)
  5046. /* use RNG to get random port if using windows */
  5047. ready.port = GetRandomPort();
  5048. #endif
  5049. server_args.signal = &ready;
  5050. client_args.signal = &ready;
  5051. /* the var is used for loop number */
  5052. server_args.argc = 1;
  5053. /* test case 1 verify depth is equal to peer chain */
  5054. {
  5055. start_thread(test_server_nofail, &server_args, &serverThread);
  5056. wait_tcp_ready(&server_args);
  5057. /* the var is used for verify depth */
  5058. client_args.argc = 2;
  5059. test_client_verifyDepth(&client_args);
  5060. join_thread(serverThread);
  5061. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5062. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5063. }
  5064. /* test case 2
  5065. * verify depth is zero, number of peer's chain is 2.
  5066. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5067. * callback.
  5068. */
  5069. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5070. {
  5071. start_thread(test_server_nofail, &server_args, &serverThread);
  5072. wait_tcp_ready(&server_args);
  5073. client_args.argc = 0;
  5074. test_client_verifyDepth(&client_args);
  5075. join_thread(serverThread);
  5076. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5077. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5078. }
  5079. /* test case 3
  5080. * verify depth is zero, number of peer's chain is 2
  5081. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5082. * therefore, handshake becomes failure.
  5083. */
  5084. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5085. {
  5086. start_thread(test_server_nofail, &server_args, &serverThread);
  5087. wait_tcp_ready(&server_args);
  5088. client_args.argc = -1;
  5089. test_client_verifyDepth(&client_args);
  5090. join_thread(serverThread);
  5091. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5092. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5093. }
  5094. FreeTcpReady(&ready);
  5095. printf(resultFmt, passed);
  5096. #else
  5097. (void)test_client_verifyDepth;
  5098. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5099. }
  5100. static void test_client_get_finished(void* args, cbType cb)
  5101. {
  5102. (void) args;
  5103. (void) cb;
  5104. #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  5105. SOCKET_T sockfd = 0;
  5106. callback_functions* cbf;
  5107. WOLFSSL_CTX* ctx = 0;
  5108. WOLFSSL* ssl = 0;
  5109. char msg[64] = "hello wolfssl!";
  5110. char reply[1024];
  5111. int msgSz = (int)XSTRLEN(msg);
  5112. int ret, err = 0;
  5113. WOLFSSL_METHOD* method = NULL;
  5114. size_t msg_len = 0;
  5115. ((func_args*)args)->return_code = TEST_FAIL;
  5116. cbf = ((func_args*)args)->callbacks;
  5117. if (cbf != NULL && cbf->method != NULL) {
  5118. method = cbf->method();
  5119. }
  5120. else {
  5121. method = wolfSSLv23_client_method();
  5122. }
  5123. ctx = wolfSSL_CTX_new(method);
  5124. /* Do connect here so server detects failures */
  5125. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5126. 0, 0, NULL);
  5127. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5128. {
  5129. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5130. goto done;
  5131. }
  5132. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5133. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5134. goto done;
  5135. }
  5136. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5137. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5138. goto done;
  5139. }
  5140. /* call ctx setup callback */
  5141. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5142. cbf->ctx_ready(ctx);
  5143. }
  5144. ssl = wolfSSL_new(ctx);
  5145. if (ssl == NULL) {
  5146. goto done;
  5147. }
  5148. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5149. goto done;
  5150. }
  5151. /* call ssl setup callback */
  5152. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5153. cbf->ssl_ready(ssl);
  5154. }
  5155. #ifdef WOLFSSL_ASYNC_CRYPT
  5156. err = 0; /* Reset error */
  5157. #endif
  5158. do {
  5159. #ifdef WOLFSSL_ASYNC_CRYPT
  5160. if (err == WC_PENDING_E) {
  5161. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5162. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5163. }
  5164. #endif
  5165. ret = wolfSSL_connect(ssl);
  5166. err = wolfSSL_get_error(ssl, 0);
  5167. } while (err == WC_PENDING_E);
  5168. if (ret != WOLFSSL_SUCCESS) {
  5169. char buff[WOLFSSL_MAX_ERROR_SZ];
  5170. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5171. goto done;
  5172. }
  5173. /* get_finished test */
  5174. /* 1. get own sent message */
  5175. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  5176. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  5177. AssertIntGE(msg_len, 0);
  5178. /* 2. get peer message */
  5179. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  5180. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  5181. AssertIntGE(msg_len, 0);
  5182. if (cb != NULL)
  5183. (cb)(ctx, ssl);
  5184. #ifdef WOLFSSL_ASYNC_CRYPT
  5185. err = 0; /* Reset error */
  5186. #endif
  5187. do {
  5188. #ifdef WOLFSSL_ASYNC_CRYPT
  5189. if (err == WC_PENDING_E) {
  5190. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5191. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5192. }
  5193. #endif
  5194. ret = wolfSSL_write(ssl, msg, msgSz);
  5195. err = wolfSSL_get_error(ssl, 0);
  5196. } while (err == WC_PENDING_E);
  5197. if (ret != msgSz) {
  5198. /*err_sys("SSL_write failed");*/
  5199. goto done;
  5200. }
  5201. #ifdef WOLFSSL_ASYNC_CRYPT
  5202. err = 0; /* Reset error */
  5203. #endif
  5204. do {
  5205. #ifdef WOLFSSL_ASYNC_CRYPT
  5206. if (err == WC_PENDING_E) {
  5207. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5208. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5209. }
  5210. #endif
  5211. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5212. err = wolfSSL_get_error(ssl, 0);
  5213. } while (err == WC_PENDING_E);
  5214. if (ret > 0) {
  5215. reply[ret] = '\0';
  5216. printf("Server response: %s\n", reply);
  5217. }
  5218. ((func_args*)args)->return_code = TEST_SUCCESS;
  5219. done:
  5220. wolfSSL_free(ssl);
  5221. wolfSSL_CTX_free(ctx);
  5222. CloseSocket(sockfd);
  5223. return;
  5224. #endif
  5225. }
  5226. static void test_wolfSSL_get_finished(void)
  5227. {
  5228. #if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  5229. tcp_ready ready;
  5230. func_args client_args;
  5231. func_args server_args;
  5232. THREAD_TYPE serverThread;
  5233. XMEMSET(&client_args, 0, sizeof(func_args));
  5234. XMEMSET(&server_args, 0, sizeof(func_args));
  5235. StartTCP();
  5236. InitTcpReady(&ready);
  5237. #if defined(USE_WINDOWS_API)
  5238. /* use RNG to get random port if using windows */
  5239. ready.port = GetRandomPort();
  5240. #endif
  5241. server_args.signal = &ready;
  5242. client_args.signal = &ready;
  5243. start_thread(test_server_nofail, &server_args, &serverThread);
  5244. wait_tcp_ready(&server_args);
  5245. test_client_get_finished(&client_args, NULL);
  5246. join_thread(serverThread);
  5247. AssertTrue(client_args.return_code);
  5248. AssertTrue(server_args.return_code);
  5249. /* test received msg vs sent msg */
  5250. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  5251. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  5252. FreeTcpReady(&ready);
  5253. #else
  5254. (void)test_client_get_finished;
  5255. #endif
  5256. }
  5257. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  5258. /* canned export of a session using older version 3 */
  5259. static unsigned char version_3[] = {
  5260. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  5261. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  5262. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  5263. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  5264. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  5265. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5266. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  5267. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  5268. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  5269. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5270. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  5271. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  5272. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5273. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  5274. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  5275. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  5276. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  5277. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  5278. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  5279. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  5280. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5281. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5282. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5283. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5284. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5285. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5286. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5287. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5289. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5290. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5291. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  5292. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  5293. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  5294. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  5295. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  5296. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  5297. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  5298. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  5299. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  5300. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  5301. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5302. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  5303. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  5304. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5305. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  5306. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  5307. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  5308. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  5309. 0x2E, 0x31, 0xED, 0x4F
  5310. };
  5311. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  5312. static void test_wolfSSL_dtls_export(void)
  5313. {
  5314. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  5315. tcp_ready ready;
  5316. func_args client_args;
  5317. func_args server_args;
  5318. THREAD_TYPE serverThread;
  5319. callback_functions server_cbf;
  5320. callback_functions client_cbf;
  5321. #ifdef WOLFSSL_TIRTOS
  5322. fdOpenSession(Task_self());
  5323. #endif
  5324. InitTcpReady(&ready);
  5325. #if defined(USE_WINDOWS_API)
  5326. /* use RNG to get random port if using windows */
  5327. ready.port = GetRandomPort();
  5328. #endif
  5329. /* set using dtls */
  5330. XMEMSET(&client_args, 0, sizeof(func_args));
  5331. XMEMSET(&server_args, 0, sizeof(func_args));
  5332. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  5333. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5334. server_cbf.method = wolfDTLSv1_2_server_method;
  5335. client_cbf.method = wolfDTLSv1_2_client_method;
  5336. server_args.callbacks = &server_cbf;
  5337. client_args.callbacks = &client_cbf;
  5338. server_args.signal = &ready;
  5339. client_args.signal = &ready;
  5340. start_thread(run_wolfssl_server, &server_args, &serverThread);
  5341. wait_tcp_ready(&server_args);
  5342. run_wolfssl_client(&client_args);
  5343. join_thread(serverThread);
  5344. AssertTrue(client_args.return_code);
  5345. AssertTrue(server_args.return_code);
  5346. FreeTcpReady(&ready);
  5347. #ifdef WOLFSSL_TIRTOS
  5348. fdOpenSession(Task_self());
  5349. #endif
  5350. {
  5351. SOCKET_T sockfd = 0;
  5352. WOLFSSL_CTX* ctx;
  5353. WOLFSSL* ssl;
  5354. char msg[64] = "hello wolfssl!";
  5355. char reply[1024];
  5356. int msgSz = (int)XSTRLEN(msg);
  5357. byte *session, *window;
  5358. unsigned int sessionSz, windowSz;
  5359. struct sockaddr_in peerAddr;
  5360. int i;
  5361. /* Set ctx to DTLS 1.2 */
  5362. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  5363. AssertNotNull(ssl = wolfSSL_new(ctx));
  5364. /* test importing version 3 */
  5365. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5366. /* test importing bad length and bad version */
  5367. version_3[2] += 1;
  5368. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5369. version_3[2] -= 1; version_3[1] = 0XA0;
  5370. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5371. wolfSSL_free(ssl);
  5372. wolfSSL_CTX_free(ctx);
  5373. /* check storing client state after connection and storing window only */
  5374. #ifdef WOLFSSL_TIRTOS
  5375. fdOpenSession(Task_self());
  5376. #endif
  5377. InitTcpReady(&ready);
  5378. #if defined(USE_WINDOWS_API)
  5379. /* use RNG to get random port if using windows */
  5380. ready.port = GetRandomPort();
  5381. #endif
  5382. /* set using dtls */
  5383. XMEMSET(&server_args, 0, sizeof(func_args));
  5384. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  5385. server_cbf.method = wolfDTLSv1_2_server_method;
  5386. server_args.callbacks = &server_cbf;
  5387. server_args.argc = 3; /* set loop_count to 3 */
  5388. server_args.signal = &ready;
  5389. start_thread(test_server_nofail, &server_args, &serverThread);
  5390. wait_tcp_ready(&server_args);
  5391. /* create and connect with client */
  5392. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  5393. AssertIntEQ(WOLFSSL_SUCCESS,
  5394. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  5395. AssertIntEQ(WOLFSSL_SUCCESS,
  5396. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  5397. AssertIntEQ(WOLFSSL_SUCCESS,
  5398. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  5399. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  5400. AssertNotNull(ssl = wolfSSL_new(ctx));
  5401. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  5402. /* store server information connected too */
  5403. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  5404. peerAddr.sin_family = AF_INET;
  5405. peerAddr.sin_port = XHTONS(server_args.signal->port);
  5406. wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr));
  5407. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  5408. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  5409. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5410. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  5411. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  5412. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  5413. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  5414. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5415. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  5416. wolfSSL_free(ssl);
  5417. for (i = 1; i < server_args.argc; i++) {
  5418. /* restore state */
  5419. AssertNotNull(ssl = wolfSSL_new(ctx));
  5420. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  5421. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  5422. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  5423. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  5424. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  5425. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  5426. wolfSSL_free(ssl);
  5427. }
  5428. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5429. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5430. wolfSSL_CTX_free(ctx);
  5431. printf("done and waiting for server\n");
  5432. join_thread(serverThread);
  5433. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5434. FreeTcpReady(&ready);
  5435. #ifdef WOLFSSL_TIRTOS
  5436. fdOpenSession(Task_self());
  5437. #endif
  5438. }
  5439. printf(testingFmt, "wolfSSL_dtls_export()");
  5440. printf(resultFmt, passed);
  5441. #endif
  5442. }
  5443. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  5444. #ifdef WOLFSSL_TLS13
  5445. static const byte canned_client_tls13_session[] = {
  5446. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  5447. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  5448. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  5449. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5450. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  5451. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5452. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  5453. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  5454. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  5455. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5456. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5457. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  5458. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  5459. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  5460. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  5461. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  5462. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  5463. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  5464. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  5465. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  5466. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  5467. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  5468. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  5469. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  5470. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  5471. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  5472. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  5473. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  5474. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5475. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  5476. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  5477. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  5478. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  5479. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  5480. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5481. 0x00, 0x03
  5482. };
  5483. static const byte canned_server_tls13_session[] = {
  5484. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  5485. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  5486. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  5487. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5488. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5489. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5490. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  5491. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  5492. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  5493. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5494. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5495. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  5496. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  5497. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  5498. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  5499. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  5500. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  5501. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  5502. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  5503. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  5504. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  5505. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  5506. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  5507. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  5508. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  5509. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  5510. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  5511. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  5512. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5513. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  5514. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  5515. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  5516. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  5517. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  5518. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5519. 0x00, 0x04
  5520. };
  5521. #endif /* WOLFSSL_TLS13 */
  5522. static const byte canned_client_session[] = {
  5523. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  5524. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  5525. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  5526. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5527. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  5528. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5529. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  5530. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  5531. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  5532. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5533. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5534. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  5535. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  5536. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  5537. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  5538. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  5539. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  5540. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  5541. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  5542. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  5543. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  5544. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  5545. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  5546. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  5547. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  5548. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  5549. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  5550. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  5551. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  5552. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5553. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5554. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5555. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5556. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  5557. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  5558. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  5559. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  5560. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  5561. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  5562. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5563. 0x00, 0x03
  5564. };
  5565. static const byte canned_server_session[] = {
  5566. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  5567. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  5568. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  5569. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5570. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  5571. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5572. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  5573. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  5574. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  5575. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5576. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5577. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  5578. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  5579. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  5580. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  5581. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  5582. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  5583. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  5584. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  5585. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  5586. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  5587. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  5588. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  5589. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  5590. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  5591. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  5592. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  5593. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  5594. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  5595. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5596. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5597. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5598. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5599. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  5600. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  5601. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  5602. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  5603. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  5604. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  5605. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5606. 0x00, 0x04
  5607. };
  5608. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  5609. {
  5610. SOCKET_T sockfd = 0;
  5611. SOCKET_T clientfd = 0;
  5612. word16 port;
  5613. callback_functions* cbf;
  5614. WOLFSSL_CTX* ctx = 0;
  5615. WOLFSSL* ssl = 0;
  5616. char msg[] = "I hear you fa shizzle!";
  5617. char input[1024];
  5618. int idx;
  5619. #ifdef WOLFSSL_TIRTOS
  5620. fdOpenSession(Task_self());
  5621. #endif
  5622. ((func_args*)args)->return_code = TEST_FAIL;
  5623. cbf = ((func_args*)args)->callbacks;
  5624. {
  5625. WOLFSSL_METHOD* method = NULL;
  5626. if (cbf != NULL && cbf->method != NULL) {
  5627. method = cbf->method();
  5628. }
  5629. else {
  5630. method = wolfTLSv1_2_server_method();
  5631. }
  5632. ctx = wolfSSL_CTX_new(method);
  5633. }
  5634. if (ctx == NULL) {
  5635. goto done;
  5636. }
  5637. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  5638. #if defined(USE_WINDOWS_API)
  5639. port = ((func_args*)args)->signal->port;
  5640. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5641. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5642. /* Let tcp_listen assign port */
  5643. port = 0;
  5644. #else
  5645. /* Use default port */
  5646. port = wolfSSLPort;
  5647. #endif
  5648. /* do it here to detect failure */
  5649. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5650. CloseSocket(sockfd);
  5651. /* call ctx setup callback */
  5652. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5653. cbf->ctx_ready(ctx);
  5654. }
  5655. ssl = wolfSSL_new(ctx);
  5656. if (ssl == NULL) {
  5657. goto done;
  5658. }
  5659. wolfSSL_set_fd(ssl, clientfd);
  5660. /* call ssl setup callback */
  5661. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5662. cbf->ssl_ready(ssl);
  5663. }
  5664. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5665. if (idx > 0) {
  5666. input[idx] = '\0';
  5667. printf("Client message export/import: %s\n", input);
  5668. }
  5669. else {
  5670. printf("ret = %d error = %d\n", idx, wolfSSL_get_error(ssl, idx));
  5671. goto done;
  5672. }
  5673. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5674. /*err_sys("SSL_write failed");*/
  5675. #ifdef WOLFSSL_TIRTOS
  5676. return;
  5677. #else
  5678. return 0;
  5679. #endif
  5680. }
  5681. #ifdef WOLFSSL_TIRTOS
  5682. Task_yield();
  5683. #endif
  5684. ((func_args*)args)->return_code = TEST_SUCCESS;
  5685. done:
  5686. wolfSSL_shutdown(ssl);
  5687. wolfSSL_free(ssl);
  5688. wolfSSL_CTX_free(ctx);
  5689. CloseSocket(clientfd);
  5690. #ifdef WOLFSSL_TIRTOS
  5691. fdCloseSession(Task_self());
  5692. #endif
  5693. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5694. && defined(HAVE_THREAD_LS)
  5695. wc_ecc_fp_free(); /* free per thread cache */
  5696. #endif
  5697. #if defined(HAVE_SESSION_TICKET) && \
  5698. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5699. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  5700. OpenSSLTicketCleanup();
  5701. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5702. TicketCleanup();
  5703. #endif
  5704. #endif
  5705. #ifndef WOLFSSL_TIRTOS
  5706. return 0;
  5707. #endif
  5708. }
  5709. static void load_tls12_canned_server(WOLFSSL* ssl)
  5710. {
  5711. int clientfd = wolfSSL_get_fd(ssl);
  5712. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  5713. sizeof(canned_server_session)), sizeof(canned_server_session));
  5714. wolfSSL_set_fd(ssl, clientfd);
  5715. }
  5716. #ifdef WOLFSSL_TLS13
  5717. static void load_tls13_canned_server(WOLFSSL* ssl)
  5718. {
  5719. int clientfd = wolfSSL_get_fd(ssl);
  5720. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  5721. sizeof(canned_server_tls13_session)),
  5722. sizeof(canned_server_tls13_session));
  5723. wolfSSL_set_fd(ssl, clientfd);
  5724. }
  5725. #endif
  5726. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  5727. static void test_wolfSSL_tls_export_run(int v)
  5728. {
  5729. SOCKET_T sockfd = 0;
  5730. WOLFSSL_CTX* ctx = 0;
  5731. WOLFSSL* ssl = 0;
  5732. char msg[64] = "hello wolfssl!";
  5733. char reply[1024];
  5734. word32 replySz;
  5735. int msgSz = (int)XSTRLEN(msg);
  5736. const byte* clientSession = NULL;
  5737. int clientSessionSz = 0;
  5738. tcp_ready ready;
  5739. func_args server_args;
  5740. THREAD_TYPE serverThread;
  5741. callback_functions server_cbf;
  5742. #ifdef WOLFSSL_TIRTOS
  5743. fdOpenSession(Task_self());
  5744. #endif
  5745. InitTcpReady(&ready);
  5746. #if defined(USE_WINDOWS_API)
  5747. /* use RNG to get random port if using windows */
  5748. ready.port = GetRandomPort();
  5749. #endif
  5750. XMEMSET(&server_args, 0, sizeof(func_args));
  5751. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  5752. switch (v) {
  5753. case WOLFSSL_TLSV1_2:
  5754. server_cbf.method = wolfTLSv1_2_server_method;
  5755. server_cbf.ssl_ready = load_tls12_canned_server;
  5756. /* setup the client side */
  5757. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  5758. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  5759. clientSession = canned_client_session;
  5760. clientSessionSz = sizeof(canned_client_session);
  5761. break;
  5762. #ifdef WOLFSSL_TLS13
  5763. case WOLFSSL_TLSV1_3:
  5764. server_cbf.method = wolfTLSv1_3_server_method;
  5765. server_cbf.ssl_ready = load_tls13_canned_server;
  5766. /* setup the client side */
  5767. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  5768. clientSession = canned_client_tls13_session;
  5769. clientSessionSz = sizeof(canned_client_tls13_session);
  5770. break;
  5771. #endif
  5772. }
  5773. server_args.callbacks = &server_cbf;
  5774. server_args.signal = &ready;
  5775. start_thread(tls_export_server, &server_args, &serverThread);
  5776. wait_tcp_ready(&server_args);
  5777. #ifdef WOLFSSL_TIRTOS
  5778. fdOpenSession(Task_self());
  5779. #endif
  5780. AssertNotNull(ssl = wolfSSL_new(ctx));
  5781. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  5782. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  5783. clientSessionSz);
  5784. replySz = sizeof(reply);
  5785. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  5786. #if !defined(NO_PSK) && defined(HAVE_ANON)
  5787. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  5788. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  5789. #endif
  5790. wolfSSL_set_fd(ssl, sockfd);
  5791. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  5792. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  5793. wolfSSL_free(ssl);
  5794. wolfSSL_CTX_free(ctx);
  5795. CloseSocket(sockfd);
  5796. #ifdef WOLFSSL_TIRTOS
  5797. fdCloseSession(Task_self());
  5798. #endif
  5799. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5800. && defined(HAVE_THREAD_LS)
  5801. wc_ecc_fp_free(); /* free per thread cache */
  5802. #endif
  5803. join_thread(serverThread);
  5804. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5805. FreeTcpReady(&ready);
  5806. #ifdef WOLFSSL_TIRTOS
  5807. fdOpenSession(Task_self());
  5808. #endif
  5809. }
  5810. #endif
  5811. static void test_wolfSSL_tls_export(void)
  5812. {
  5813. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  5814. printf(testingFmt, "wolfSSL_tls_export()");
  5815. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  5816. #ifdef WOLFSSL_TLS13
  5817. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  5818. #endif
  5819. printf(resultFmt, passed);
  5820. #endif
  5821. }
  5822. /*----------------------------------------------------------------------------*
  5823. | TLS extensions tests
  5824. *----------------------------------------------------------------------------*/
  5825. #ifdef ENABLE_TLS_CALLBACK_TEST
  5826. /* Connection test runner - generic */
  5827. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  5828. callback_functions* server_callbacks)
  5829. {
  5830. tcp_ready ready;
  5831. func_args client_args;
  5832. func_args server_args;
  5833. THREAD_TYPE serverThread;
  5834. XMEMSET(&client_args, 0, sizeof(func_args));
  5835. XMEMSET(&server_args, 0, sizeof(func_args));
  5836. StartTCP();
  5837. client_args.callbacks = client_callbacks;
  5838. server_args.callbacks = server_callbacks;
  5839. #ifdef WOLFSSL_TIRTOS
  5840. fdOpenSession(Task_self());
  5841. #endif
  5842. /* RUN Server side */
  5843. InitTcpReady(&ready);
  5844. #if defined(USE_WINDOWS_API)
  5845. /* use RNG to get random port if using windows */
  5846. ready.port = GetRandomPort();
  5847. #endif
  5848. server_args.signal = &ready;
  5849. client_args.signal = &ready;
  5850. start_thread(run_wolfssl_server, &server_args, &serverThread);
  5851. wait_tcp_ready(&server_args);
  5852. /* RUN Client side */
  5853. run_wolfssl_client(&client_args);
  5854. join_thread(serverThread);
  5855. FreeTcpReady(&ready);
  5856. #ifdef WOLFSSL_TIRTOS
  5857. fdCloseSession(Task_self());
  5858. #endif
  5859. client_callbacks->return_code = client_args.return_code;
  5860. server_callbacks->return_code = server_args.return_code;
  5861. }
  5862. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5863. #ifdef HAVE_SNI
  5864. static void test_wolfSSL_UseSNI_params(void)
  5865. {
  5866. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  5867. WOLFSSL *ssl = wolfSSL_new(ctx);
  5868. AssertNotNull(ctx);
  5869. AssertNotNull(ssl);
  5870. /* invalid [ctx|ssl] */
  5871. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  5872. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  5873. /* invalid type */
  5874. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  5875. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  5876. /* invalid data */
  5877. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  5878. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  5879. /* success case */
  5880. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  5881. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  5882. wolfSSL_free(ssl);
  5883. wolfSSL_CTX_free(ctx);
  5884. }
  5885. /* BEGIN of connection tests callbacks */
  5886. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  5887. {
  5888. AssertIntEQ(WOLFSSL_SUCCESS,
  5889. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  5890. }
  5891. static void use_SNI_at_ssl(WOLFSSL* ssl)
  5892. {
  5893. AssertIntEQ(WOLFSSL_SUCCESS,
  5894. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  5895. }
  5896. static void different_SNI_at_ssl(WOLFSSL* ssl)
  5897. {
  5898. AssertIntEQ(WOLFSSL_SUCCESS,
  5899. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  5900. }
  5901. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  5902. {
  5903. use_SNI_at_ssl(ssl);
  5904. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  5905. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  5906. }
  5907. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  5908. {
  5909. use_SNI_at_ssl(ssl);
  5910. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  5911. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  5912. }
  5913. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  5914. {
  5915. use_SNI_at_ctx(ctx);
  5916. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  5917. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  5918. }
  5919. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  5920. {
  5921. use_SNI_at_ssl(ssl);
  5922. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  5923. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  5924. }
  5925. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  5926. {
  5927. use_SNI_at_ctx(ctx);
  5928. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  5929. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  5930. }
  5931. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  5932. {
  5933. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  5934. }
  5935. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  5936. {
  5937. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  5938. }
  5939. static void verify_SNI_no_matching(WOLFSSL* ssl)
  5940. {
  5941. byte type = WOLFSSL_SNI_HOST_NAME;
  5942. char* request = (char*) &type; /* to be overwritten */
  5943. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  5944. AssertNotNull(request);
  5945. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  5946. AssertNull(request);
  5947. }
  5948. static void verify_SNI_real_matching(WOLFSSL* ssl)
  5949. {
  5950. byte type = WOLFSSL_SNI_HOST_NAME;
  5951. char* request = NULL;
  5952. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  5953. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  5954. AssertNotNull(request);
  5955. AssertStrEQ("www.wolfssl.com", request);
  5956. }
  5957. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  5958. {
  5959. byte type = WOLFSSL_SNI_HOST_NAME;
  5960. char* request = NULL;
  5961. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  5962. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  5963. AssertNotNull(request);
  5964. AssertStrEQ("ww2.wolfssl.com", request);
  5965. }
  5966. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  5967. {
  5968. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  5969. }
  5970. /* END of connection tests callbacks */
  5971. static void test_wolfSSL_UseSNI_connection(void)
  5972. {
  5973. callback_functions client_cb;
  5974. callback_functions server_cb;
  5975. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  5976. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  5977. client_cb.method = wolfSSLv23_client_method;
  5978. server_cb.method = wolfSSLv23_server_method;
  5979. server_cb.devId = devId;
  5980. server_cb.devId = devId;
  5981. /* success case at ctx */
  5982. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  5983. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  5984. test_wolfSSL_client_server(&client_cb, &server_cb);
  5985. /* success case at ssl */
  5986. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  5987. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  5988. test_wolfSSL_client_server(&client_cb, &server_cb);
  5989. /* default mismatch behavior */
  5990. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  5991. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  5992. test_wolfSSL_client_server(&client_cb, &server_cb);
  5993. /* continue on mismatch */
  5994. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  5995. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  5996. test_wolfSSL_client_server(&client_cb, &server_cb);
  5997. /* fake answer on mismatch */
  5998. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  5999. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  6000. test_wolfSSL_client_server(&client_cb, &server_cb);
  6001. /* sni abort - success */
  6002. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6003. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  6004. test_wolfSSL_client_server(&client_cb, &server_cb);
  6005. /* sni abort - abort when absent (ctx) */
  6006. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6007. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6008. /* sni abort - abort when absent (ssl) */
  6009. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6010. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6011. test_wolfSSL_client_server(&client_cb, &server_cb);
  6012. /* sni abort - success when overwritten */
  6013. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6014. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  6015. test_wolfSSL_client_server(&client_cb, &server_cb);
  6016. /* sni abort - success when allowing mismatches */
  6017. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6018. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  6019. test_wolfSSL_client_server(&client_cb, &server_cb);
  6020. }
  6021. static void test_wolfSSL_SNI_GetFromBuffer(void)
  6022. {
  6023. byte buff[] = { /* www.paypal.com */
  6024. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  6025. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  6026. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  6027. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  6028. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  6029. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  6030. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  6031. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  6032. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  6033. };
  6034. byte buff2[] = { /* api.textmate.org */
  6035. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  6036. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  6037. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  6038. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  6039. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  6040. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  6041. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  6042. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  6043. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  6044. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  6045. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  6046. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  6047. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  6048. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  6049. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  6050. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  6051. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  6052. };
  6053. byte buff3[] = { /* no sni extension */
  6054. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  6055. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  6056. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  6057. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  6058. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  6059. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  6060. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  6061. };
  6062. byte buff4[] = { /* last extension has zero size */
  6063. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  6064. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  6065. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  6066. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  6067. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  6068. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  6069. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  6070. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  6071. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  6072. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  6073. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  6074. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  6075. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  6076. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  6077. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  6078. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  6079. 0x12, 0x00, 0x00
  6080. };
  6081. byte buff5[] = { /* SSL v2.0 client hello */
  6082. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  6083. /* dummy bytes bellow, just to pass size check */
  6084. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  6085. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  6086. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  6087. };
  6088. byte result[32] = {0};
  6089. word32 length = 32;
  6090. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  6091. 0, result, &length));
  6092. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  6093. 0, result, &length));
  6094. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  6095. 1, result, &length));
  6096. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6097. 0, result, &length));
  6098. buff[0] = 0x16;
  6099. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6100. 0, result, &length));
  6101. buff[1] = 0x03;
  6102. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  6103. sizeof(buff), 0, result, &length));
  6104. buff[2] = 0x03;
  6105. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  6106. sizeof(buff), 0, result, &length));
  6107. buff[4] = 0x64;
  6108. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6109. 0, result, &length));
  6110. result[length] = 0;
  6111. AssertStrEQ("www.paypal.com", (const char*) result);
  6112. length = 32;
  6113. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  6114. 0, result, &length));
  6115. result[length] = 0;
  6116. AssertStrEQ("api.textmate.org", (const char*) result);
  6117. /* SSL v2.0 tests */
  6118. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  6119. sizeof(buff5), 0, result, &length));
  6120. buff5[2] = 0x02;
  6121. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6122. sizeof(buff5), 0, result, &length));
  6123. buff5[2] = 0x01; buff5[6] = 0x08;
  6124. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6125. sizeof(buff5), 0, result, &length));
  6126. buff5[6] = 0x09; buff5[8] = 0x01;
  6127. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6128. sizeof(buff5), 0, result, &length));
  6129. }
  6130. #endif /* HAVE_SNI */
  6131. static void test_wolfSSL_UseSNI(void)
  6132. {
  6133. #ifdef HAVE_SNI
  6134. test_wolfSSL_UseSNI_params();
  6135. test_wolfSSL_UseSNI_connection();
  6136. test_wolfSSL_SNI_GetFromBuffer();
  6137. #endif
  6138. }
  6139. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  6140. static void test_wolfSSL_UseTrustedCA(void)
  6141. {
  6142. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  6143. && !defined(NO_RSA)
  6144. WOLFSSL_CTX *ctx;
  6145. WOLFSSL *ssl;
  6146. byte id[20];
  6147. #ifndef NO_WOLFSSL_SERVER
  6148. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  6149. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  6150. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  6151. #else
  6152. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  6153. #endif
  6154. AssertNotNull((ssl = wolfSSL_new(ctx)));
  6155. XMEMSET(id, 0, sizeof(id));
  6156. /* error cases */
  6157. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  6158. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6159. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  6160. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6161. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  6162. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6163. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  6164. #ifdef NO_SHA
  6165. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6166. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  6167. #endif
  6168. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6169. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  6170. /* success cases */
  6171. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6172. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  6173. #ifndef NO_SHA
  6174. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6175. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  6176. #endif
  6177. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6178. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  6179. wolfSSL_free(ssl);
  6180. wolfSSL_CTX_free(ctx);
  6181. #endif /* HAVE_TRUSTED_CA */
  6182. }
  6183. static void test_wolfSSL_UseMaxFragment(void)
  6184. {
  6185. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  6186. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  6187. #ifndef NO_WOLFSSL_SERVER
  6188. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  6189. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6190. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6191. #else
  6192. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6193. #endif
  6194. WOLFSSL *ssl = wolfSSL_new(ctx);
  6195. #ifdef OPENSSL_EXTRA
  6196. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  6197. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  6198. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  6199. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  6200. #else
  6201. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  6202. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  6203. UseMaxFragment = wolfSSL_UseMaxFragment;
  6204. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  6205. #endif
  6206. AssertNotNull(ctx);
  6207. AssertNotNull(ssl);
  6208. /* error cases */
  6209. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  6210. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  6211. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  6212. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  6213. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  6214. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  6215. /* success case */
  6216. #ifdef OPENSSL_EXTRA
  6217. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  6218. #else
  6219. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  6220. #endif
  6221. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  6222. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  6223. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  6224. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  6225. #ifdef OPENSSL_EXTRA
  6226. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  6227. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  6228. #else
  6229. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  6230. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  6231. #endif
  6232. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  6233. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  6234. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  6235. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  6236. #ifdef OPENSSL_EXTRA
  6237. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  6238. #else
  6239. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  6240. #endif
  6241. wolfSSL_free(ssl);
  6242. wolfSSL_CTX_free(ctx);
  6243. #endif
  6244. }
  6245. static void test_wolfSSL_UseTruncatedHMAC(void)
  6246. {
  6247. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  6248. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  6249. #ifndef NO_WOLFSSL_SERVER
  6250. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  6251. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6252. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6253. #else
  6254. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6255. #endif
  6256. WOLFSSL *ssl = wolfSSL_new(ctx);
  6257. AssertNotNull(ctx);
  6258. AssertNotNull(ssl);
  6259. /* error cases */
  6260. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  6261. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  6262. /* success case */
  6263. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  6264. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  6265. wolfSSL_free(ssl);
  6266. wolfSSL_CTX_free(ctx);
  6267. #endif
  6268. }
  6269. static void test_wolfSSL_UseSupportedCurve(void)
  6270. {
  6271. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  6272. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6273. WOLFSSL *ssl = wolfSSL_new(ctx);
  6274. AssertNotNull(ctx);
  6275. AssertNotNull(ssl);
  6276. /* error cases */
  6277. AssertIntNE(WOLFSSL_SUCCESS,
  6278. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  6279. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  6280. AssertIntNE(WOLFSSL_SUCCESS,
  6281. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  6282. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  6283. /* success case */
  6284. AssertIntEQ(WOLFSSL_SUCCESS,
  6285. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  6286. AssertIntEQ(WOLFSSL_SUCCESS,
  6287. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  6288. wolfSSL_free(ssl);
  6289. wolfSSL_CTX_free(ctx);
  6290. #endif
  6291. }
  6292. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  6293. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  6294. {
  6295. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  6296. }
  6297. static void use_ALPN_all(WOLFSSL* ssl)
  6298. {
  6299. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6300. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6301. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6302. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6303. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6304. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  6305. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6306. }
  6307. static void use_ALPN_all_continue(WOLFSSL* ssl)
  6308. {
  6309. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6310. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6311. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6312. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6313. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6314. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  6315. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6316. }
  6317. static void use_ALPN_one(WOLFSSL* ssl)
  6318. {
  6319. /* spdy/2 */
  6320. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6321. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6322. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6323. }
  6324. static void use_ALPN_unknown(WOLFSSL* ssl)
  6325. {
  6326. /* http/2.0 */
  6327. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  6328. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6329. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6330. }
  6331. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  6332. {
  6333. /* http/2.0 */
  6334. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  6335. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6336. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6337. }
  6338. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  6339. {
  6340. /* spdy/3 */
  6341. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6342. char *proto = NULL;
  6343. word16 protoSz = 0;
  6344. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6345. /* check value */
  6346. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  6347. if (proto) {
  6348. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  6349. }
  6350. }
  6351. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  6352. {
  6353. char *proto = NULL;
  6354. word16 protoSz = 0;
  6355. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  6356. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6357. /* check value */
  6358. AssertIntEQ(1, (0 == protoSz));
  6359. AssertIntEQ(1, (NULL == proto));
  6360. }
  6361. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  6362. {
  6363. /* http/1.1 */
  6364. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6365. char *proto;
  6366. word16 protoSz = 0;
  6367. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6368. /* check value */
  6369. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6370. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6371. }
  6372. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  6373. {
  6374. /* spdy/2 */
  6375. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6376. char *proto;
  6377. word16 protoSz = 0;
  6378. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6379. /* check value */
  6380. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6381. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6382. }
  6383. static void verify_ALPN_client_list(WOLFSSL* ssl)
  6384. {
  6385. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6386. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6387. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6388. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6389. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6390. char *clist = NULL;
  6391. word16 clistSz = 0;
  6392. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  6393. &clistSz));
  6394. /* check value */
  6395. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  6396. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  6397. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  6398. }
  6399. static void test_wolfSSL_UseALPN_connection(void)
  6400. {
  6401. callback_functions client_cb;
  6402. callback_functions server_cb;
  6403. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6404. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6405. client_cb.method = wolfSSLv23_client_method;
  6406. server_cb.method = wolfSSLv23_server_method;
  6407. server_cb.devId = devId;
  6408. server_cb.devId = devId;
  6409. /* success case same list */
  6410. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6411. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  6412. test_wolfSSL_client_server(&client_cb, &server_cb);
  6413. /* success case only one for server */
  6414. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6415. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  6416. test_wolfSSL_client_server(&client_cb, &server_cb);
  6417. /* success case only one for client */
  6418. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  6419. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  6420. test_wolfSSL_client_server(&client_cb, &server_cb);
  6421. /* success case none for client */
  6422. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6423. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  6424. test_wolfSSL_client_server(&client_cb, &server_cb);
  6425. /* success case mismatch behavior but option 'continue' set */
  6426. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  6427. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  6428. test_wolfSSL_client_server(&client_cb, &server_cb);
  6429. /* success case read protocol send by client */
  6430. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6431. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  6432. test_wolfSSL_client_server(&client_cb, &server_cb);
  6433. /* mismatch behavior with same list
  6434. * the first and only this one must be taken */
  6435. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6436. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  6437. test_wolfSSL_client_server(&client_cb, &server_cb);
  6438. /* default mismatch behavior */
  6439. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6440. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  6441. test_wolfSSL_client_server(&client_cb, &server_cb);
  6442. }
  6443. static void test_wolfSSL_UseALPN_params(void)
  6444. {
  6445. #ifndef NO_WOLFSSL_CLIENT
  6446. /* "http/1.1" */
  6447. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6448. /* "spdy/1" */
  6449. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  6450. /* "spdy/2" */
  6451. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6452. /* "spdy/3" */
  6453. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6454. char buff[256];
  6455. word32 idx;
  6456. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6457. WOLFSSL *ssl = wolfSSL_new(ctx);
  6458. AssertNotNull(ctx);
  6459. AssertNotNull(ssl);
  6460. /* error cases */
  6461. AssertIntNE(WOLFSSL_SUCCESS,
  6462. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  6463. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6464. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  6465. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6466. /* success case */
  6467. /* http1 only */
  6468. AssertIntEQ(WOLFSSL_SUCCESS,
  6469. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  6470. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6471. /* http1, spdy1 */
  6472. XMEMCPY(buff, http1, sizeof(http1));
  6473. idx = sizeof(http1);
  6474. buff[idx++] = ',';
  6475. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  6476. idx += sizeof(spdy1);
  6477. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  6478. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6479. /* http1, spdy2, spdy1 */
  6480. XMEMCPY(buff, http1, sizeof(http1));
  6481. idx = sizeof(http1);
  6482. buff[idx++] = ',';
  6483. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  6484. idx += sizeof(spdy2);
  6485. buff[idx++] = ',';
  6486. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  6487. idx += sizeof(spdy1);
  6488. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  6489. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6490. /* spdy3, http1, spdy2, spdy1 */
  6491. XMEMCPY(buff, spdy3, sizeof(spdy3));
  6492. idx = sizeof(spdy3);
  6493. buff[idx++] = ',';
  6494. XMEMCPY(buff+idx, http1, sizeof(http1));
  6495. idx += sizeof(http1);
  6496. buff[idx++] = ',';
  6497. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  6498. idx += sizeof(spdy2);
  6499. buff[idx++] = ',';
  6500. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  6501. idx += sizeof(spdy1);
  6502. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  6503. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6504. wolfSSL_free(ssl);
  6505. wolfSSL_CTX_free(ctx);
  6506. #endif
  6507. }
  6508. #endif /* HAVE_ALPN */
  6509. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  6510. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  6511. {
  6512. unsigned char p[] = {
  6513. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  6514. 6, 's', 'p', 'd', 'y', '/', '2',
  6515. 6, 's', 'p', 'd', 'y', '/', '1',
  6516. };
  6517. unsigned char p_len = sizeof(p);
  6518. int ret;
  6519. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  6520. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  6521. AssertIntEQ(ret, 0);
  6522. #else
  6523. AssertIntEQ(ret, SSL_SUCCESS);
  6524. #endif
  6525. }
  6526. static void set_alpn_protos(SSL* ssl)
  6527. {
  6528. unsigned char p[] = {
  6529. 6, 's', 'p', 'd', 'y', '/', '3',
  6530. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  6531. 6, 's', 'p', 'd', 'y', '/', '2',
  6532. 6, 's', 'p', 'd', 'y', '/', '1',
  6533. };
  6534. unsigned char p_len = sizeof(p);
  6535. int ret;
  6536. ret = SSL_set_alpn_protos(ssl, p, p_len);
  6537. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  6538. AssertIntEQ(ret, 0);
  6539. #else
  6540. AssertIntEQ(ret, SSL_SUCCESS);
  6541. #endif
  6542. }
  6543. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  6544. {
  6545. /* "spdy/3" */
  6546. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6547. const unsigned char *proto;
  6548. unsigned int protoSz = 0;
  6549. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  6550. /* check value */
  6551. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6552. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6553. }
  6554. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  6555. {
  6556. /* "http/1.1" */
  6557. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6558. const unsigned char *proto;
  6559. unsigned int protoSz = 0;
  6560. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  6561. /* check value */
  6562. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6563. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6564. }
  6565. static void test_wolfSSL_set_alpn_protos(void)
  6566. {
  6567. callback_functions client_cb;
  6568. callback_functions server_cb;
  6569. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6570. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6571. client_cb.method = wolfSSLv23_client_method;
  6572. server_cb.method = wolfSSLv23_server_method;
  6573. server_cb.devId = devId;
  6574. server_cb.devId = devId;
  6575. /* use CTX_alpn_protos */
  6576. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6577. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  6578. test_wolfSSL_client_server(&client_cb, &server_cb);
  6579. /* use set_alpn_protos */
  6580. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  6581. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  6582. test_wolfSSL_client_server(&client_cb, &server_cb);
  6583. }
  6584. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  6585. static void test_wolfSSL_UseALPN(void)
  6586. {
  6587. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
  6588. defined(HAVE_IO_TESTS_DEPENDENCIES)
  6589. test_wolfSSL_UseALPN_connection();
  6590. test_wolfSSL_UseALPN_params();
  6591. #endif
  6592. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  6593. test_wolfSSL_set_alpn_protos();
  6594. #endif
  6595. }
  6596. static void test_wolfSSL_DisableExtendedMasterSecret(void)
  6597. {
  6598. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  6599. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6600. WOLFSSL *ssl = wolfSSL_new(ctx);
  6601. AssertNotNull(ctx);
  6602. AssertNotNull(ssl);
  6603. /* error cases */
  6604. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  6605. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  6606. /* success cases */
  6607. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  6608. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  6609. wolfSSL_free(ssl);
  6610. wolfSSL_CTX_free(ctx);
  6611. #endif
  6612. }
  6613. static void test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  6614. {
  6615. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  6616. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6617. WOLFSSL *ssl = wolfSSL_new(ctx);
  6618. AssertNotNull(ctx);
  6619. AssertNotNull(ssl);
  6620. /* error cases */
  6621. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  6622. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  6623. /* success cases */
  6624. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  6625. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  6626. wolfSSL_free(ssl);
  6627. wolfSSL_CTX_free(ctx);
  6628. #endif
  6629. }
  6630. /*----------------------------------------------------------------------------*
  6631. | X509 Tests
  6632. *----------------------------------------------------------------------------*/
  6633. static void test_wolfSSL_X509_NAME_get_entry(void)
  6634. {
  6635. #if !defined(NO_CERTS) && !defined(NO_RSA)
  6636. #if defined(OPENSSL_ALL) || \
  6637. (defined(OPENSSL_EXTRA) && \
  6638. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  6639. printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
  6640. {
  6641. /* use openssl like name to test mapping */
  6642. X509_NAME_ENTRY* ne;
  6643. X509_NAME* name;
  6644. X509* x509;
  6645. #ifndef NO_FILESYSTEM
  6646. ASN1_STRING* asn;
  6647. char* subCN = NULL;
  6648. #endif
  6649. int idx;
  6650. ASN1_OBJECT *object = NULL;
  6651. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  6652. #ifndef NO_BIO
  6653. BIO* bio;
  6654. #endif
  6655. #endif
  6656. #ifndef NO_FILESYSTEM
  6657. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  6658. AssertNotNull(x509);
  6659. name = X509_get_subject_name(x509);
  6660. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  6661. AssertIntGE(idx, 0);
  6662. ne = X509_NAME_get_entry(name, idx);
  6663. AssertNotNull(ne);
  6664. asn = X509_NAME_ENTRY_get_data(ne);
  6665. AssertNotNull(asn);
  6666. subCN = (char*)ASN1_STRING_data(asn);
  6667. AssertNotNull(subCN);
  6668. wolfSSL_FreeX509(x509);
  6669. #endif
  6670. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  6671. AssertNotNull(x509);
  6672. name = X509_get_subject_name(x509);
  6673. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  6674. AssertIntGE(idx, 0);
  6675. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  6676. #ifndef NO_BIO
  6677. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  6678. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  6679. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  6680. AssertIntEQ(X509_NAME_print_ex_fp(stdout, name, 4,
  6681. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  6682. BIO_free(bio);
  6683. #endif
  6684. #endif
  6685. ne = X509_NAME_get_entry(name, idx);
  6686. AssertNotNull(ne);
  6687. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  6688. wolfSSL_FreeX509(x509);
  6689. }
  6690. printf(resultFmt, passed);
  6691. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  6692. #endif /* !NO_CERTS && !NO_RSA */
  6693. }
  6694. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  6695. static void test_wolfSSL_PKCS12(void)
  6696. {
  6697. /* .p12 file is encrypted with DES3 */
  6698. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  6699. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  6700. * Password Key
  6701. */
  6702. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  6703. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  6704. !defined(NO_SHA) && defined(HAVE_PKCS12)
  6705. byte buf[6000];
  6706. char file[] = "./certs/test-servercert.p12";
  6707. char order[] = "./certs/ecc-rsa-server.p12";
  6708. #ifdef WC_RC2
  6709. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  6710. #endif
  6711. char pass[] = "a password";
  6712. const char goodPsw[] = "wolfSSL test";
  6713. const char badPsw[] = "bad";
  6714. #ifdef HAVE_ECC
  6715. WOLFSSL_X509_NAME* subject;
  6716. WOLFSSL_X509 *x509;
  6717. #endif
  6718. XFILE f;
  6719. int bytes, ret, goodPswLen, badPswLen;
  6720. WOLFSSL_BIO *bio;
  6721. WOLFSSL_EVP_PKEY *pkey;
  6722. WC_PKCS12 *pkcs12;
  6723. WC_PKCS12 *pkcs12_2;
  6724. WOLFSSL_X509 *cert;
  6725. WOLFSSL_X509 *tmp;
  6726. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  6727. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  6728. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  6729. WOLFSSL_CTX *ctx;
  6730. WOLFSSL *ssl;
  6731. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  6732. #endif
  6733. printf(testingFmt, "wolfSSL_PKCS12()");
  6734. f = XFOPEN(file, "rb");
  6735. AssertTrue((f != XBADFILE));
  6736. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  6737. XFCLOSE(f);
  6738. goodPswLen = (int)XSTRLEN(goodPsw);
  6739. badPswLen = (int)XSTRLEN(badPsw);
  6740. bio = BIO_new_mem_buf((void*)buf, bytes);
  6741. AssertNotNull(bio);
  6742. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  6743. AssertNotNull(pkcs12);
  6744. PKCS12_free(pkcs12);
  6745. d2i_PKCS12_bio(bio, &pkcs12);
  6746. AssertNotNull(pkcs12);
  6747. BIO_free(bio);
  6748. /* check verify MAC directly */
  6749. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  6750. AssertIntEQ(ret, 1);
  6751. /* check verify MAC fail case directly */
  6752. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  6753. AssertIntEQ(ret, 0);
  6754. /* check verify MAC fail case */
  6755. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  6756. AssertIntEQ(ret, 0);
  6757. AssertNull(pkey);
  6758. AssertNull(cert);
  6759. /* check parse with no extra certs kept */
  6760. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  6761. AssertIntEQ(ret, 1);
  6762. AssertNotNull(pkey);
  6763. AssertNotNull(cert);
  6764. wolfSSL_EVP_PKEY_free(pkey);
  6765. wolfSSL_X509_free(cert);
  6766. /* check parse with extra certs kept */
  6767. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  6768. AssertIntEQ(ret, 1);
  6769. AssertNotNull(pkey);
  6770. AssertNotNull(cert);
  6771. AssertNotNull(ca);
  6772. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  6773. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  6774. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  6775. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  6776. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  6777. #else
  6778. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  6779. #endif
  6780. /* Copy stack structure */
  6781. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  6782. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  6783. /* CTX now owns the tmp_ca stack structure */
  6784. tmp_ca = NULL;
  6785. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  6786. AssertNotNull(tmp_ca);
  6787. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  6788. /* Check that the main cert is also set */
  6789. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  6790. AssertNotNull(ssl = SSL_new(ctx));
  6791. AssertNotNull(SSL_get_certificate(ssl));
  6792. SSL_free(ssl);
  6793. SSL_CTX_free(ctx);
  6794. #endif
  6795. /* should be 2 other certs on stack */
  6796. tmp = sk_X509_pop(ca);
  6797. AssertNotNull(tmp);
  6798. X509_free(tmp);
  6799. tmp = sk_X509_pop(ca);
  6800. AssertNotNull(tmp);
  6801. X509_free(tmp);
  6802. AssertNull(sk_X509_pop(ca));
  6803. EVP_PKEY_free(pkey);
  6804. X509_free(cert);
  6805. sk_X509_pop_free(ca, X509_free);
  6806. /* check PKCS12_create */
  6807. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  6808. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  6809. SSL_SUCCESS);
  6810. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  6811. -1, -1, 100, -1, 0)));
  6812. EVP_PKEY_free(pkey);
  6813. X509_free(cert);
  6814. sk_X509_pop_free(ca, NULL);
  6815. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  6816. SSL_SUCCESS);
  6817. PKCS12_free(pkcs12_2);
  6818. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  6819. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  6820. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  6821. 2000, 1, 0)));
  6822. EVP_PKEY_free(pkey);
  6823. X509_free(cert);
  6824. sk_X509_pop_free(ca, NULL);
  6825. /* convert to DER then back and parse */
  6826. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  6827. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  6828. PKCS12_free(pkcs12_2);
  6829. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  6830. BIO_free(bio);
  6831. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  6832. SSL_SUCCESS);
  6833. /* should be 2 other certs on stack */
  6834. tmp = sk_X509_pop(ca);
  6835. AssertNotNull(tmp);
  6836. X509_free(tmp);
  6837. tmp = sk_X509_pop(ca);
  6838. AssertNotNull(tmp);
  6839. X509_free(tmp);
  6840. AssertNull(sk_X509_pop(ca));
  6841. #ifndef NO_RC4
  6842. PKCS12_free(pkcs12_2);
  6843. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  6844. NID_pbe_WithSHA1And128BitRC4,
  6845. NID_pbe_WithSHA1And128BitRC4,
  6846. 2000, 1, 0)));
  6847. EVP_PKEY_free(pkey);
  6848. X509_free(cert);
  6849. sk_X509_pop_free(ca, NULL);
  6850. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  6851. SSL_SUCCESS);
  6852. #endif /* NO_RC4 */
  6853. EVP_PKEY_free(pkey);
  6854. X509_free(cert);
  6855. PKCS12_free(pkcs12);
  6856. PKCS12_free(pkcs12_2);
  6857. sk_X509_pop_free(ca, NULL);
  6858. #ifdef HAVE_ECC
  6859. /* test order of parsing */
  6860. f = XFOPEN(order, "rb");
  6861. AssertTrue(f != XBADFILE);
  6862. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  6863. XFCLOSE(f);
  6864. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  6865. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  6866. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  6867. WOLFSSL_SUCCESS);
  6868. AssertNotNull(pkey);
  6869. AssertNotNull(cert);
  6870. AssertNotNull(ca);
  6871. /* compare subject lines of certificates */
  6872. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  6873. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  6874. SSL_FILETYPE_PEM));
  6875. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  6876. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  6877. X509_free(x509);
  6878. /* test expected fail case */
  6879. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  6880. SSL_FILETYPE_PEM));
  6881. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  6882. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  6883. X509_free(x509);
  6884. X509_free(cert);
  6885. /* get subject line from ca stack */
  6886. AssertNotNull(cert = sk_X509_pop(ca));
  6887. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  6888. /* compare subject from certificate in ca to expected */
  6889. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  6890. SSL_FILETYPE_PEM));
  6891. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  6892. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  6893. EVP_PKEY_free(pkey);
  6894. X509_free(x509);
  6895. X509_free(cert);
  6896. BIO_free(bio);
  6897. PKCS12_free(pkcs12);
  6898. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  6899. /* test order of parsing */
  6900. f = XFOPEN(file, "rb");
  6901. AssertTrue(f != XBADFILE);
  6902. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  6903. XFCLOSE(f);
  6904. /* check verify MAC fail case */
  6905. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  6906. AssertIntEQ(ret, 0);
  6907. AssertNull(pkey);
  6908. AssertNull(cert);
  6909. /* check parse with no extra certs kept */
  6910. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  6911. AssertIntEQ(ret, 1);
  6912. AssertNotNull(pkey);
  6913. AssertNotNull(cert);
  6914. wolfSSL_EVP_PKEY_free(pkey);
  6915. wolfSSL_X509_free(cert);
  6916. /* check parse with extra certs kept */
  6917. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  6918. AssertIntEQ(ret, 1);
  6919. AssertNotNull(pkey);
  6920. AssertNotNull(cert);
  6921. AssertNotNull(ca);
  6922. wolfSSL_EVP_PKEY_free(pkey);
  6923. wolfSSL_X509_free(cert);
  6924. sk_X509_pop_free(ca, NULL);
  6925. PKCS12_free(pkcs12);
  6926. #endif /* HAVE_ECC */
  6927. #ifdef WC_RC2
  6928. /* test PKCS#12 with RC2 encryption */
  6929. f = XFOPEN(rc2p12, "rb");
  6930. AssertTrue(f != XBADFILE);
  6931. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  6932. XFCLOSE(f);
  6933. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  6934. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  6935. /* check verify MAC fail case */
  6936. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  6937. AssertIntEQ(ret, 0);
  6938. AssertNull(pkey);
  6939. AssertNull(cert);
  6940. /* check parse iwth not extra certs kept */
  6941. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  6942. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  6943. AssertNotNull(pkey);
  6944. AssertNotNull(cert);
  6945. /* check parse with extra certs kept */
  6946. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  6947. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  6948. AssertNotNull(pkey);
  6949. AssertNotNull(cert);
  6950. AssertNotNull(ca);
  6951. wolfSSL_EVP_PKEY_free(pkey);
  6952. wolfSSL_X509_free(cert);
  6953. sk_X509_pop_free(ca, NULL);
  6954. BIO_free(bio);
  6955. PKCS12_free(pkcs12);
  6956. #endif /* WC_RC2 */
  6957. /* Test i2d_PKCS12_bio */
  6958. f = XFOPEN(file, "rb");
  6959. AssertTrue((f != XBADFILE));
  6960. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  6961. XFCLOSE(f);
  6962. bio = BIO_new(BIO_s_mem());
  6963. AssertNotNull(bio);
  6964. ret = i2d_PKCS12_bio(bio, pkcs12);
  6965. AssertIntEQ(ret, 1);
  6966. ret = i2d_PKCS12_bio(NULL, pkcs12);
  6967. AssertIntEQ(ret, 0);
  6968. ret = i2d_PKCS12_bio(bio, NULL);
  6969. AssertIntEQ(ret, 0);
  6970. PKCS12_free(pkcs12);
  6971. BIO_free(bio);
  6972. (void)order;
  6973. printf(resultFmt, passed);
  6974. #endif /* OPENSSL_EXTRA */
  6975. #endif /* HAVE_FIPS */
  6976. }
  6977. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  6978. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  6979. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  6980. #define TEST_PKCS8_ENC
  6981. #endif
  6982. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  6983. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  6984. /* used to keep track if FailTestCallback was called */
  6985. static int failTestCallbackCalled = 0;
  6986. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  6987. {
  6988. (void)passwd;
  6989. (void)sz;
  6990. (void)rw;
  6991. (void)userdata;
  6992. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  6993. failTestCallbackCalled = 1;
  6994. return -1;
  6995. }
  6996. #endif
  6997. static void test_wolfSSL_no_password_cb(void)
  6998. {
  6999. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  7000. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  7001. WOLFSSL_CTX* ctx;
  7002. byte buff[FOURK_BUF];
  7003. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  7004. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  7005. XFILE f;
  7006. int bytes;
  7007. printf(testingFmt, "test_wolfSSL_no_password_cb()");
  7008. #ifndef NO_WOLFSSL_CLIENT
  7009. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  7010. #else
  7011. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  7012. #endif
  7013. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  7014. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  7015. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7016. XFCLOSE(f);
  7017. AssertIntLE(bytes, sizeof(buff));
  7018. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7019. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7020. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  7021. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7022. XFCLOSE(f);
  7023. AssertIntLE(bytes, sizeof(buff));
  7024. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7025. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7026. wolfSSL_CTX_free(ctx);
  7027. if (failTestCallbackCalled != 0) {
  7028. Fail(("Password callback should not be called by default"),
  7029. ("Password callback was called without attempting "
  7030. "to first decipher private key without password."));
  7031. }
  7032. printf(resultFmt, passed);
  7033. #endif
  7034. }
  7035. #ifdef TEST_PKCS8_ENC
  7036. /* for PKCS8 test case */
  7037. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  7038. {
  7039. int flag = 0;
  7040. (void)rw;
  7041. if (userdata != NULL) {
  7042. flag = *((int*)userdata); /* user set data */
  7043. }
  7044. switch (flag) {
  7045. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  7046. * can be anything the user wishes to be passed to the callback
  7047. * associated with the WOLFSSL_CTX */
  7048. XSTRNCPY(passwd, "yassl123", sz);
  7049. return 8;
  7050. default:
  7051. return BAD_FUNC_ARG;
  7052. }
  7053. }
  7054. #endif /* TEST_PKCS8_ENC */
  7055. /* Testing functions dealing with PKCS8 */
  7056. static void test_wolfSSL_PKCS8(void)
  7057. {
  7058. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  7059. byte buff[FOURK_BUF];
  7060. byte der[FOURK_BUF];
  7061. #ifndef NO_RSA
  7062. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  7063. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  7064. #endif
  7065. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  7066. #ifdef HAVE_ECC
  7067. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  7068. #endif
  7069. XFILE f;
  7070. int bytes;
  7071. WOLFSSL_CTX* ctx;
  7072. #if defined(HAVE_ECC) && !defined(NO_CODING)
  7073. int ret;
  7074. ecc_key key;
  7075. word32 x = 0;
  7076. #endif
  7077. #ifdef TEST_PKCS8_ENC
  7078. #if !defined(NO_RSA) && !defined(NO_SHA)
  7079. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  7080. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  7081. #endif
  7082. #if defined(HAVE_ECC) && !defined(NO_SHA)
  7083. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  7084. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  7085. #endif
  7086. int flag;
  7087. #endif
  7088. (void)der;
  7089. printf(testingFmt, "wolfSSL_PKCS8()");
  7090. #ifndef NO_WOLFSSL_CLIENT
  7091. #ifndef WOLFSSL_NO_TLS12
  7092. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  7093. #else
  7094. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  7095. #endif
  7096. #else
  7097. #ifndef WOLFSSL_NO_TLS12
  7098. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  7099. #else
  7100. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  7101. #endif
  7102. #endif
  7103. #ifdef TEST_PKCS8_ENC
  7104. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  7105. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  7106. flag = 1; /* used by password callback as return code */
  7107. #if !defined(NO_RSA) && !defined(NO_SHA)
  7108. /* test loading PEM PKCS8 encrypted file */
  7109. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  7110. AssertTrue((f != XBADFILE));
  7111. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7112. XFCLOSE(f);
  7113. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7114. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7115. /* this next case should fail because of password callback return code */
  7116. flag = 0; /* used by password callback as return code */
  7117. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7118. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7119. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  7120. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7121. "yassl123"), 0);
  7122. /* test that error value is returned with a bad password */
  7123. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7124. "bad"), 0);
  7125. /* test loading PEM PKCS8 encrypted file */
  7126. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  7127. AssertTrue((f != XBADFILE));
  7128. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7129. XFCLOSE(f);
  7130. flag = 1; /* used by password callback as return code */
  7131. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7132. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7133. /* this next case should fail because of password callback return code */
  7134. flag = 0; /* used by password callback as return code */
  7135. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7136. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7137. #endif /* !NO_RSA && !NO_SHA */
  7138. #if defined(HAVE_ECC) && !defined(NO_SHA)
  7139. /* test loading PEM PKCS8 encrypted ECC Key file */
  7140. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  7141. AssertTrue((f != XBADFILE));
  7142. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7143. XFCLOSE(f);
  7144. flag = 1; /* used by password callback as return code */
  7145. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7146. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7147. /* this next case should fail because of password callback return code */
  7148. flag = 0; /* used by password callback as return code */
  7149. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7150. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7151. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  7152. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7153. "yassl123"), 0);
  7154. /* test that error value is returned with a bad password */
  7155. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7156. "bad"), 0);
  7157. /* test loading DER PKCS8 encrypted ECC Key file */
  7158. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  7159. AssertTrue((f != XBADFILE));
  7160. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7161. XFCLOSE(f);
  7162. flag = 1; /* used by password callback as return code */
  7163. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7164. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7165. /* this next case should fail because of password callback return code */
  7166. flag = 0; /* used by password callback as return code */
  7167. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7168. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7169. /* leave flag as "okay" */
  7170. flag = 1;
  7171. #endif /* HAVE_ECC && !NO_SHA */
  7172. #endif /* TEST_PKCS8_ENC */
  7173. #ifndef NO_RSA
  7174. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  7175. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  7176. AssertTrue((f != XBADFILE));
  7177. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7178. XFCLOSE(f);
  7179. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7180. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7181. /* test loading PEM PKCS8 private key file (not encrypted) */
  7182. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  7183. AssertTrue((f != XBADFILE));
  7184. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7185. XFCLOSE(f);
  7186. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7187. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7188. #endif /* !NO_RSA */
  7189. /* Test PKCS8 PEM ECC key no crypt */
  7190. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  7191. AssertTrue((f != XBADFILE));
  7192. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7193. XFCLOSE(f);
  7194. #ifdef HAVE_ECC
  7195. /* Test PKCS8 PEM ECC key no crypt */
  7196. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7197. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7198. #ifndef NO_CODING
  7199. /* decrypt PKCS8 PEM to key in DER format */
  7200. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  7201. (word32)sizeof(der), NULL)), 0);
  7202. ret = wc_ecc_init(&key);
  7203. if (ret == 0) {
  7204. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  7205. wc_ecc_free(&key);
  7206. }
  7207. AssertIntEQ(ret, 0);
  7208. #endif
  7209. /* Test PKCS8 DER ECC key no crypt */
  7210. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  7211. AssertTrue((f != XBADFILE));
  7212. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7213. XFCLOSE(f);
  7214. /* Test using a PKCS8 ECC PEM */
  7215. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7216. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7217. #else
  7218. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  7219. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  7220. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  7221. #endif /* HAVE_ECC */
  7222. wolfSSL_CTX_free(ctx);
  7223. printf(resultFmt, passed);
  7224. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  7225. }
  7226. static void test_wolfSSL_PKCS8_ED25519(void)
  7227. {
  7228. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  7229. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  7230. defined(HAVE_ED25519_KEY_IMPORT)
  7231. const byte encPrivKey[] = \
  7232. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  7233. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  7234. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  7235. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  7236. "2L9W4v7swXkV+X+a1ww=\n"
  7237. "-----END ENCRYPTED PRIVATE KEY-----\n";
  7238. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  7239. byte der[FOURK_BUF];
  7240. WOLFSSL_CTX* ctx;
  7241. int bytes;
  7242. XMEMSET(der, 0, sizeof(der));
  7243. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  7244. (word32)sizeof(der), password)), 0);
  7245. #ifndef NO_WOLFSSL_SERVER
  7246. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7247. #else
  7248. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7249. #endif
  7250. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  7251. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7252. wolfSSL_CTX_free(ctx);
  7253. #endif
  7254. }
  7255. static void test_wolfSSL_PKCS8_ED448(void)
  7256. {
  7257. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  7258. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  7259. defined(HAVE_ED448_KEY_IMPORT)
  7260. const byte encPrivKey[] = \
  7261. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  7262. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  7263. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  7264. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  7265. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  7266. "-----END ENCRYPTED PRIVATE KEY-----\n";
  7267. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  7268. byte der[FOURK_BUF];
  7269. WOLFSSL_CTX* ctx;
  7270. int bytes;
  7271. XMEMSET(der, 0, sizeof(der));
  7272. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  7273. (word32)sizeof(der), password)), 0);
  7274. #ifndef NO_WOLFSSL_SERVER
  7275. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7276. #else
  7277. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7278. #endif
  7279. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  7280. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7281. wolfSSL_CTX_free(ctx);
  7282. #endif
  7283. }
  7284. /* Testing functions dealing with PKCS5 */
  7285. static void test_wolfSSL_PKCS5(void)
  7286. {
  7287. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  7288. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  7289. const char* passwd = "myfipsPa$$W0rd";
  7290. #else
  7291. const char *passwd = "pass1234";
  7292. #endif
  7293. const unsigned char *salt = (unsigned char *)"salt1234";
  7294. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  7295. DYNAMIC_TYPE_TMP_BUFFER);
  7296. int ret = 0;
  7297. AssertNotNull(out);
  7298. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  7299. (int)XSTRLEN((const char *) salt), 10,
  7300. WC_SHA_DIGEST_SIZE,out);
  7301. AssertIntEQ(ret, SSL_SUCCESS);
  7302. #ifdef WOLFSSL_SHA512
  7303. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  7304. (int)XSTRLEN((const char *) salt), 10,
  7305. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  7306. AssertIntEQ(ret, SSL_SUCCESS);
  7307. #endif
  7308. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7309. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  7310. }
  7311. /* test parsing URI from certificate */
  7312. static void test_wolfSSL_URI(void)
  7313. {
  7314. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7315. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  7316. defined(OPENSSL_EXTRA))
  7317. WOLFSSL_X509* x509;
  7318. const char uri[] = "./certs/client-uri-cert.pem";
  7319. const char badUri[] = "./certs/client-relative-uri.pem";
  7320. printf(testingFmt, "wolfSSL URI parse");
  7321. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  7322. AssertNotNull(x509);
  7323. wolfSSL_FreeX509(x509);
  7324. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  7325. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT)
  7326. AssertNull(x509);
  7327. #else
  7328. AssertNotNull(x509);
  7329. #endif
  7330. printf(resultFmt, passed);
  7331. #endif
  7332. }
  7333. static void test_wolfSSL_TBS(void)
  7334. {
  7335. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7336. && defined(OPENSSL_EXTRA)
  7337. WOLFSSL_X509* x509;
  7338. const unsigned char* tbs;
  7339. int tbsSz;
  7340. printf(testingFmt, "wolfSSL TBS");
  7341. AssertNotNull(x509 =
  7342. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  7343. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  7344. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  7345. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  7346. AssertIntEQ(tbsSz, 981);
  7347. wolfSSL_FreeX509(x509);
  7348. printf(resultFmt, passed);
  7349. #endif
  7350. }
  7351. static void test_wolfSSL_X509_verify(void)
  7352. {
  7353. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7354. && defined(OPENSSL_EXTRA)
  7355. WOLFSSL_X509* ca;
  7356. WOLFSSL_X509* serv;
  7357. WOLFSSL_EVP_PKEY* pkey;
  7358. unsigned char buf[2048];
  7359. const unsigned char* pt = NULL;
  7360. int bufSz;
  7361. printf(testingFmt, "wolfSSL X509 verify");
  7362. AssertNotNull(ca =
  7363. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  7364. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  7365. WOLFSSL_SUCCESS);
  7366. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  7367. WOLFSSL_SUCCESS);
  7368. AssertIntEQ(bufSz, 294);
  7369. bufSz = 2048;
  7370. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  7371. WOLFSSL_SUCCESS);
  7372. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  7373. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  7374. AssertNotNull(serv =
  7375. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  7376. /* success case */
  7377. pt = buf;
  7378. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  7379. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  7380. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  7381. wolfSSL_EVP_PKEY_free(pkey);
  7382. /* fail case */
  7383. bufSz = 2048;
  7384. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  7385. WOLFSSL_SUCCESS);
  7386. pt = buf;
  7387. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  7388. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  7389. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  7390. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  7391. wolfSSL_EVP_PKEY_free(pkey);
  7392. wolfSSL_FreeX509(ca);
  7393. wolfSSL_FreeX509(serv);
  7394. printf(resultFmt, passed);
  7395. #endif
  7396. }
  7397. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  7398. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  7399. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  7400. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  7401. /* create certificate with version 2 */
  7402. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  7403. {
  7404. WOLFSSL_X509 *x509, *x509v2;
  7405. WOLFSSL_EVP_PKEY *priv, *pub;
  7406. unsigned char *der = NULL, *key = NULL, *pt;
  7407. char *header, *name;
  7408. int derSz;
  7409. long keySz;
  7410. XFILE fp;
  7411. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  7412. time_t t;
  7413. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  7414. WOLFSSL_FILETYPE_PEM));
  7415. fp = XFOPEN(cliKeyFile, "rb");
  7416. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  7417. WOLFSSL_SUCCESS);
  7418. XFCLOSE(fp);
  7419. pt = key;
  7420. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  7421. (const unsigned char**)&pt, keySz));
  7422. /* create the version 2 certificate */
  7423. AssertNotNull(x509v2 = X509_new());
  7424. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  7425. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  7426. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  7427. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  7428. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  7429. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  7430. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  7431. t = time(NULL);
  7432. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  7433. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  7434. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  7435. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  7436. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  7437. derSz = wolfSSL_i2d_X509(x509v2, &der);
  7438. AssertIntGT(derSz, 0);
  7439. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  7440. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7441. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  7442. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7443. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7444. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7445. wolfSSL_X509_free(x509);
  7446. wolfSSL_X509_free(x509v2);
  7447. wolfSSL_EVP_PKEY_free(priv);
  7448. wolfSSL_EVP_PKEY_free(pub);
  7449. wolfSSL_ASN1_TIME_free(notBefore);
  7450. wolfSSL_ASN1_TIME_free(notAfter);
  7451. }
  7452. /* override certificate version error */
  7453. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  7454. {
  7455. AssertIntEQ(store->error, ASN_VERSION_E);
  7456. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  7457. (void)preverify;
  7458. return 1;
  7459. }
  7460. /* set verify callback that will override bad certificate version */
  7461. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  7462. {
  7463. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  7464. }
  7465. #endif
  7466. static void test_wolfSSL_X509_TLS_version(void)
  7467. {
  7468. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  7469. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  7470. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  7471. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  7472. tcp_ready ready;
  7473. func_args server_args;
  7474. func_args client_args;
  7475. THREAD_TYPE serverThread;
  7476. callback_functions func_cb_client;
  7477. callback_functions func_cb_server;
  7478. printf(testingFmt, "test_wolfSSL_X509_TLS_version");
  7479. /* test server rejects a client certificate that is not version 3 */
  7480. #ifdef WOLFSSL_TIRTOS
  7481. fdOpenSession(Task_self());
  7482. #endif
  7483. XMEMSET(&server_args, 0, sizeof(func_args));
  7484. XMEMSET(&client_args, 0, sizeof(func_args));
  7485. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  7486. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  7487. StartTCP();
  7488. InitTcpReady(&ready);
  7489. #if defined(USE_WINDOWS_API)
  7490. /* use RNG to get random port if using windows */
  7491. ready.port = GetRandomPort();
  7492. #endif
  7493. server_args.signal = &ready;
  7494. client_args.signal = &ready;
  7495. server_args.return_code = TEST_FAIL;
  7496. client_args.return_code = TEST_FAIL;
  7497. func_cb_client.ctx_ready = &test_set_x509_badversion;
  7498. #ifndef WOLFSSL_NO_TLS12
  7499. func_cb_client.method = wolfTLSv1_2_client_method;
  7500. #else
  7501. func_cb_client.method = wolfTLSv1_3_client_method;
  7502. #endif
  7503. client_args.callbacks = &func_cb_client;
  7504. #ifndef WOLFSSL_NO_TLS12
  7505. func_cb_server.method = wolfTLSv1_2_server_method;
  7506. #else
  7507. func_cb_server.method = wolfTLSv1_3_server_method;
  7508. #endif
  7509. server_args.callbacks = &func_cb_server;
  7510. start_thread(test_server_nofail, &server_args, &serverThread);
  7511. wait_tcp_ready(&server_args);
  7512. test_client_nofail(&client_args, NULL);
  7513. join_thread(serverThread);
  7514. AssertIntEQ(client_args.return_code, TEST_FAIL);
  7515. AssertIntEQ(server_args.return_code, TEST_FAIL);
  7516. FreeTcpReady(&ready);
  7517. #ifdef WOLFSSL_TIRTOS
  7518. fdCloseSession(Task_self());
  7519. #endif
  7520. /* Now re run but override the bad X509 version */
  7521. #ifdef WOLFSSL_TIRTOS
  7522. fdOpenSession(Task_self());
  7523. #endif
  7524. XMEMSET(&server_args, 0, sizeof(func_args));
  7525. XMEMSET(&client_args, 0, sizeof(func_args));
  7526. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  7527. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  7528. StartTCP();
  7529. InitTcpReady(&ready);
  7530. #if defined(USE_WINDOWS_API)
  7531. /* use RNG to get random port if using windows */
  7532. ready.port = GetRandomPort();
  7533. #endif
  7534. server_args.signal = &ready;
  7535. client_args.signal = &ready;
  7536. server_args.return_code = TEST_FAIL;
  7537. client_args.return_code = TEST_FAIL;
  7538. func_cb_client.ctx_ready = &test_set_x509_badversion;
  7539. func_cb_server.ctx_ready = &test_set_override_x509;
  7540. #ifndef WOLFSSL_NO_TLS12
  7541. func_cb_client.method = wolfTLSv1_2_client_method;
  7542. #else
  7543. func_cb_client.method = wolfTLSv1_3_client_method;
  7544. #endif
  7545. client_args.callbacks = &func_cb_client;
  7546. #ifndef WOLFSSL_NO_TLS12
  7547. func_cb_server.method = wolfTLSv1_2_server_method;
  7548. #else
  7549. func_cb_server.method = wolfTLSv1_3_server_method;
  7550. #endif
  7551. server_args.callbacks = &func_cb_server;
  7552. start_thread(test_server_nofail, &server_args, &serverThread);
  7553. wait_tcp_ready(&server_args);
  7554. test_client_nofail(&client_args, NULL);
  7555. join_thread(serverThread);
  7556. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  7557. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  7558. FreeTcpReady(&ready);
  7559. #ifdef WOLFSSL_TIRTOS
  7560. fdCloseSession(Task_self());
  7561. #endif
  7562. printf(resultFmt, passed);
  7563. #endif
  7564. }
  7565. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  7566. * version allowed.
  7567. * POST: 1 on success.
  7568. */
  7569. static int test_wolfSSL_CTX_SetMinVersion(void)
  7570. {
  7571. int failFlag = WOLFSSL_SUCCESS;
  7572. #ifndef NO_WOLFSSL_CLIENT
  7573. WOLFSSL_CTX* ctx;
  7574. int itr;
  7575. #ifndef NO_OLD_TLS
  7576. const int versions[] = {
  7577. #ifdef WOLFSSL_ALLOW_TLSV10
  7578. WOLFSSL_TLSV1,
  7579. #endif
  7580. WOLFSSL_TLSV1_1,
  7581. WOLFSSL_TLSV1_2 };
  7582. #elif !defined(WOLFSSL_NO_TLS12)
  7583. const int versions[] = { WOLFSSL_TLSV1_2 };
  7584. #elif defined(WOLFSSL_TLS13)
  7585. const int versions[] = { WOLFSSL_TLSV1_3 };
  7586. #else
  7587. const int versions[0];
  7588. #endif
  7589. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7590. printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
  7591. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  7592. if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
  7593. failFlag = WOLFSSL_FAILURE;
  7594. }
  7595. }
  7596. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  7597. wolfSSL_CTX_free(ctx);
  7598. #endif
  7599. return failFlag;
  7600. } /* END test_wolfSSL_CTX_SetMinVersion */
  7601. /*----------------------------------------------------------------------------*
  7602. | OCSP Stapling
  7603. *----------------------------------------------------------------------------*/
  7604. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  7605. * need to contact the CA, lowering the cost of cert revocation checking.
  7606. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  7607. * POST: 1 returned for success.
  7608. */
  7609. static int test_wolfSSL_UseOCSPStapling(void)
  7610. {
  7611. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  7612. !defined(NO_WOLFSSL_CLIENT)
  7613. int ret;
  7614. WOLFSSL_CTX* ctx;
  7615. WOLFSSL* ssl;
  7616. #ifndef NO_WOLFSSL_CLIENT
  7617. #ifndef WOLFSSL_NO_TLS12
  7618. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  7619. #else
  7620. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  7621. #endif
  7622. #else
  7623. #ifndef WOLFSSL_NO_TLS12
  7624. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  7625. #else
  7626. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  7627. #endif
  7628. #endif
  7629. ssl = wolfSSL_new(ctx);
  7630. printf(testingFmt, "wolfSSL_UseOCSPStapling()");
  7631. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  7632. WOLFSSL_CSR2_OCSP_USE_NONCE);
  7633. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  7634. wolfSSL_free(ssl);
  7635. wolfSSL_CTX_free(ctx);
  7636. return ret;
  7637. #else
  7638. return WOLFSSL_SUCCESS;
  7639. #endif
  7640. } /*END test_wolfSSL_UseOCSPStapling */
  7641. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  7642. * stapling eliminates the need to contact the CA and lowers cert revocation
  7643. * check.
  7644. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  7645. */
  7646. static int test_wolfSSL_UseOCSPStaplingV2 (void)
  7647. {
  7648. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  7649. !defined(NO_WOLFSSL_CLIENT)
  7650. int ret;
  7651. WOLFSSL_CTX* ctx;
  7652. WOLFSSL* ssl;
  7653. #ifndef NO_WOLFSSL_CLIENT
  7654. #ifndef WOLFSSL_NO_TLS12
  7655. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  7656. #else
  7657. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  7658. #endif
  7659. #else
  7660. #ifndef WOLFSSL_NO_TLS12
  7661. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  7662. #else
  7663. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  7664. #endif
  7665. #endif
  7666. ssl = wolfSSL_new(ctx);
  7667. printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
  7668. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  7669. WOLFSSL_CSR2_OCSP_USE_NONCE );
  7670. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  7671. wolfSSL_free(ssl);
  7672. wolfSSL_CTX_free(ctx);
  7673. return ret;
  7674. #else
  7675. return WOLFSSL_SUCCESS;
  7676. #endif
  7677. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  7678. /*----------------------------------------------------------------------------*
  7679. | Multicast Tests
  7680. *----------------------------------------------------------------------------*/
  7681. static void test_wolfSSL_mcast(void)
  7682. {
  7683. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  7684. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  7685. WOLFSSL_CTX* ctx;
  7686. WOLFSSL* ssl;
  7687. int result;
  7688. byte preMasterSecret[512];
  7689. byte clientRandom[32];
  7690. byte serverRandom[32];
  7691. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  7692. byte buf[256];
  7693. word16 newId;
  7694. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  7695. AssertNotNull(ctx);
  7696. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  7697. AssertIntEQ(result, WOLFSSL_SUCCESS);
  7698. ssl = wolfSSL_new(ctx);
  7699. AssertNotNull(ssl);
  7700. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  7701. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  7702. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  7703. result = wolfSSL_set_secret(ssl, 23,
  7704. preMasterSecret, sizeof(preMasterSecret),
  7705. clientRandom, serverRandom, suite);
  7706. AssertIntEQ(result, WOLFSSL_SUCCESS);
  7707. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  7708. AssertIntLE(result, 0);
  7709. AssertIntLE(newId, 100);
  7710. wolfSSL_free(ssl);
  7711. wolfSSL_CTX_free(ctx);
  7712. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 || WOLFSSL_SNIFFER) */
  7713. }
  7714. /*----------------------------------------------------------------------------*
  7715. | Wolfcrypt
  7716. *----------------------------------------------------------------------------*/
  7717. /*
  7718. * Unit test for the wc_InitBlake2b()
  7719. */
  7720. static int test_wc_InitBlake2b (void)
  7721. {
  7722. int ret = 0;
  7723. #ifdef HAVE_BLAKE2
  7724. Blake2b blake2b;
  7725. printf(testingFmt, "wc_InitBlake2B()");
  7726. /* Test good arg. */
  7727. ret = wc_InitBlake2b(&blake2b, 64);
  7728. if (ret != 0) {
  7729. ret = WOLFSSL_FATAL_ERROR;
  7730. }
  7731. /* Test bad arg. */
  7732. if (!ret) {
  7733. ret = wc_InitBlake2b(NULL, 64);
  7734. if (ret == 0) {
  7735. ret = WOLFSSL_FATAL_ERROR;
  7736. } else {
  7737. ret = 0;
  7738. }
  7739. }
  7740. if (!ret) {
  7741. ret = wc_InitBlake2b(NULL, 128);
  7742. if (ret == 0) {
  7743. ret = WOLFSSL_FATAL_ERROR;
  7744. } else {
  7745. ret = 0;
  7746. }
  7747. }
  7748. if (!ret) {
  7749. ret = wc_InitBlake2b(&blake2b, 128);
  7750. if (ret == 0) {
  7751. ret = WOLFSSL_FATAL_ERROR;
  7752. } else {
  7753. ret = 0;
  7754. }
  7755. }
  7756. if (!ret) {
  7757. ret = wc_InitBlake2b(NULL, 0);
  7758. if (ret == 0) {
  7759. ret = WOLFSSL_FATAL_ERROR;
  7760. } else {
  7761. ret = 0;
  7762. }
  7763. }
  7764. if (!ret) {
  7765. ret = wc_InitBlake2b(&blake2b, 0);
  7766. if (ret == 0) {
  7767. ret = WOLFSSL_FATAL_ERROR;
  7768. } else {
  7769. ret = 0;
  7770. }
  7771. }
  7772. printf(resultFmt, ret == 0 ? passed : failed);
  7773. #endif
  7774. return ret;
  7775. } /*END test_wc_InitBlake2b*/
  7776. /*
  7777. * Unit test for the wc_InitBlake2b_WithKey()
  7778. */
  7779. static int test_wc_InitBlake2b_WithKey (void)
  7780. {
  7781. int ret = 0;
  7782. #ifdef HAVE_BLAKE2
  7783. Blake2b blake2b;
  7784. word32 digestSz = BLAKE2B_KEYBYTES;
  7785. byte key[BLAKE2B_KEYBYTES];
  7786. word32 keylen = BLAKE2B_KEYBYTES;
  7787. printf(testingFmt, "wc_InitBlake2b_WithKey()");
  7788. /* Test good arg. */
  7789. ret = wc_InitBlake2b_WithKey(&blake2b, digestSz, key, keylen);
  7790. if (ret != 0) {
  7791. ret = WOLFSSL_FATAL_ERROR;
  7792. }
  7793. /* Test bad args. */
  7794. if (ret == 0) {
  7795. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  7796. if (ret == BAD_FUNC_ARG) {
  7797. ret = 0;
  7798. }
  7799. }
  7800. if (ret == 0) {
  7801. ret = wc_InitBlake2b_WithKey(&blake2b, digestSz, key, 256);
  7802. if (ret == BAD_FUNC_ARG) {
  7803. ret = 0;
  7804. }
  7805. }
  7806. if (ret == 0) {
  7807. ret = wc_InitBlake2b_WithKey(&blake2b, digestSz, NULL, keylen);
  7808. }
  7809. printf(resultFmt, ret == 0 ? passed : failed);
  7810. #endif
  7811. return ret;
  7812. } /*END wc_InitBlake2b_WithKey*/
  7813. /*
  7814. * Unit test for the wc_InitBlake2s_WithKey()
  7815. */
  7816. static int test_wc_InitBlake2s_WithKey (void)
  7817. {
  7818. int ret = 0;
  7819. #ifdef HAVE_BLAKE2S
  7820. Blake2s blake2s;
  7821. word32 digestSz = BLAKE2S_KEYBYTES;
  7822. byte *key = (byte*)"01234567890123456789012345678901";
  7823. word32 keylen = BLAKE2S_KEYBYTES;
  7824. printf(testingFmt, "wc_InitBlake2s_WithKey()");
  7825. /* Test good arg. */
  7826. ret = wc_InitBlake2s_WithKey(&blake2s, digestSz, key, keylen);
  7827. if (ret != 0) {
  7828. ret = WOLFSSL_FATAL_ERROR;
  7829. }
  7830. /* Test bad args. */
  7831. if (ret == 0) {
  7832. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  7833. if (ret == BAD_FUNC_ARG) {
  7834. ret = 0;
  7835. }
  7836. }
  7837. if (ret == 0) {
  7838. ret = wc_InitBlake2s_WithKey(&blake2s, digestSz, key, 256);
  7839. if (ret == BAD_FUNC_ARG) {
  7840. ret = 0;
  7841. }
  7842. }
  7843. if (ret == 0) {
  7844. ret = wc_InitBlake2s_WithKey(&blake2s, digestSz, NULL, keylen);
  7845. }
  7846. printf(resultFmt, ret == 0 ? passed : failed);
  7847. #endif
  7848. return ret;
  7849. } /*END wc_InitBlake2s_WithKey*/
  7850. /*
  7851. * Unit test for the wc_InitMd5()
  7852. */
  7853. static int test_wc_InitMd5 (void)
  7854. {
  7855. int flag = 0;
  7856. #ifndef NO_MD5
  7857. wc_Md5 md5;
  7858. int ret;
  7859. printf(testingFmt, "wc_InitMd5()");
  7860. /* Test good arg. */
  7861. ret = wc_InitMd5(&md5);
  7862. if (ret != 0) {
  7863. flag = WOLFSSL_FATAL_ERROR;
  7864. }
  7865. /* Test bad arg. */
  7866. if (!flag) {
  7867. ret = wc_InitMd5(NULL);
  7868. if (ret != BAD_FUNC_ARG) {
  7869. flag = WOLFSSL_FATAL_ERROR;
  7870. }
  7871. }
  7872. wc_Md5Free(&md5);
  7873. printf(resultFmt, flag == 0 ? passed : failed);
  7874. #endif
  7875. return flag;
  7876. } /* END test_wc_InitMd5 */
  7877. /*
  7878. * Testing wc_UpdateMd5()
  7879. */
  7880. static int test_wc_Md5Update (void)
  7881. {
  7882. int flag = 0;
  7883. #ifndef NO_MD5
  7884. wc_Md5 md5;
  7885. byte hash[WC_MD5_DIGEST_SIZE];
  7886. testVector a, b, c;
  7887. int ret;
  7888. ret = wc_InitMd5(&md5);
  7889. if (ret != 0) {
  7890. flag = ret;
  7891. }
  7892. printf(testingFmt, "wc_Md5Update()");
  7893. /* Input */
  7894. if (!flag) {
  7895. a.input = "a";
  7896. a.inLen = XSTRLEN(a.input);
  7897. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  7898. if (ret != 0) {
  7899. flag = ret;
  7900. }
  7901. }
  7902. if (!flag) {
  7903. ret = wc_Md5Final(&md5, hash);
  7904. if (ret != 0) {
  7905. flag = ret;
  7906. }
  7907. }
  7908. /* Update input. */
  7909. if (!flag) {
  7910. a.input = "abc";
  7911. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  7912. "\x72";
  7913. a.inLen = XSTRLEN(a.input);
  7914. a.outLen = XSTRLEN(a.output);
  7915. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  7916. if (ret != 0) {
  7917. flag = ret;
  7918. }
  7919. }
  7920. if (!flag) {
  7921. ret = wc_Md5Final(&md5, hash);
  7922. if (ret != 0) {
  7923. flag = ret;
  7924. }
  7925. }
  7926. if (!flag) {
  7927. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  7928. flag = WOLFSSL_FATAL_ERROR;
  7929. }
  7930. }
  7931. /*Pass in bad values. */
  7932. if (!flag) {
  7933. b.input = NULL;
  7934. b.inLen = 0;
  7935. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  7936. if (ret != 0) {
  7937. flag = ret;
  7938. }
  7939. }
  7940. if (!flag) {
  7941. c.input = NULL;
  7942. c.inLen = WC_MD5_DIGEST_SIZE;
  7943. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  7944. if (ret != BAD_FUNC_ARG) {
  7945. flag = WOLFSSL_FATAL_ERROR;
  7946. }
  7947. }
  7948. if (!flag) {
  7949. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  7950. if (ret != BAD_FUNC_ARG) {
  7951. flag = WOLFSSL_FATAL_ERROR;
  7952. }
  7953. }
  7954. wc_Md5Free(&md5);
  7955. printf(resultFmt, flag == 0 ? passed : failed);
  7956. #endif
  7957. return flag;
  7958. } /* END test_wc_Md5Update() */
  7959. /*
  7960. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  7961. */
  7962. static int test_wc_Md5Final (void)
  7963. {
  7964. int flag = 0;
  7965. #ifndef NO_MD5
  7966. /* Instantiate */
  7967. wc_Md5 md5;
  7968. byte* hash_test[3];
  7969. byte hash1[WC_MD5_DIGEST_SIZE];
  7970. byte hash2[2*WC_MD5_DIGEST_SIZE];
  7971. byte hash3[5*WC_MD5_DIGEST_SIZE];
  7972. int times, i, ret;
  7973. /* Initialize */
  7974. ret = wc_InitMd5(&md5);
  7975. if (ret != 0) {
  7976. flag = ret;
  7977. }
  7978. if (!flag) {
  7979. hash_test[0] = hash1;
  7980. hash_test[1] = hash2;
  7981. hash_test[2] = hash3;
  7982. }
  7983. times = sizeof(hash_test)/sizeof(byte*);
  7984. /* Test good args. */
  7985. printf(testingFmt, "wc_Md5Final()");
  7986. for (i = 0; i < times; i++) {
  7987. if (!flag) {
  7988. ret = wc_Md5Final(&md5, hash_test[i]);
  7989. if (ret != 0) {
  7990. flag = WOLFSSL_FATAL_ERROR;
  7991. }
  7992. }
  7993. }
  7994. /* Test bad args. */
  7995. if (!flag) {
  7996. ret = wc_Md5Final(NULL, NULL);
  7997. if (ret != BAD_FUNC_ARG) {
  7998. flag = WOLFSSL_FATAL_ERROR;
  7999. }
  8000. }
  8001. if (!flag) {
  8002. ret = wc_Md5Final(NULL, hash1);
  8003. if (ret != BAD_FUNC_ARG) {
  8004. flag = WOLFSSL_FATAL_ERROR;
  8005. }
  8006. }
  8007. if (!flag) {
  8008. ret = wc_Md5Final(&md5, NULL);
  8009. if (ret != BAD_FUNC_ARG) {
  8010. flag = WOLFSSL_FATAL_ERROR;
  8011. }
  8012. }
  8013. wc_Md5Free(&md5);
  8014. printf(resultFmt, flag == 0 ? passed : failed);
  8015. #endif
  8016. return flag;
  8017. }
  8018. /*
  8019. * Unit test for the wc_InitSha()
  8020. */
  8021. static int test_wc_InitSha(void)
  8022. {
  8023. int flag = 0;
  8024. #ifndef NO_SHA
  8025. wc_Sha sha;
  8026. int ret;
  8027. printf(testingFmt, "wc_InitSha()");
  8028. /* Test good arg. */
  8029. ret = wc_InitSha(&sha);
  8030. if (ret != 0) {
  8031. flag = WOLFSSL_FATAL_ERROR;
  8032. }
  8033. /* Test bad arg. */
  8034. if (!flag) {
  8035. ret = wc_InitSha(NULL);
  8036. if (ret != BAD_FUNC_ARG) {
  8037. flag = WOLFSSL_FATAL_ERROR;
  8038. }
  8039. }
  8040. wc_ShaFree(&sha);
  8041. printf(resultFmt, flag == 0 ? passed : failed);
  8042. #endif
  8043. return flag;
  8044. } /* END test_wc_InitSha */
  8045. /*
  8046. * Tesing wc_ShaUpdate()
  8047. */
  8048. static int test_wc_ShaUpdate (void)
  8049. {
  8050. int flag = 0;
  8051. #ifndef NO_SHA
  8052. wc_Sha sha;
  8053. byte hash[WC_SHA_DIGEST_SIZE];
  8054. testVector a, b, c;
  8055. int ret;
  8056. ret = wc_InitSha(&sha);
  8057. if (ret != 0) {
  8058. flag = ret;
  8059. }
  8060. printf(testingFmt, "wc_ShaUpdate()");
  8061. /* Input. */
  8062. if (!flag) {
  8063. a.input = "a";
  8064. a.inLen = XSTRLEN(a.input);
  8065. ret = wc_ShaUpdate(&sha, NULL, 0);
  8066. if (ret != 0) {
  8067. flag = ret;
  8068. }
  8069. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  8070. if (ret != 0) {
  8071. flag = ret;
  8072. }
  8073. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  8074. if (ret != 0) {
  8075. flag = ret;
  8076. }
  8077. }
  8078. if (!flag) {
  8079. ret = wc_ShaFinal(&sha, hash);
  8080. if (ret != 0) {
  8081. flag = ret;
  8082. }
  8083. }
  8084. /* Update input. */
  8085. if (!flag) {
  8086. a.input = "abc";
  8087. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  8088. "\x6C\x9C\xD0\xD8\x9D";
  8089. a.inLen = XSTRLEN(a.input);
  8090. a.outLen = XSTRLEN(a.output);
  8091. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  8092. if (ret != 0) {
  8093. flag = ret;
  8094. }
  8095. }
  8096. if (!flag) {
  8097. ret = wc_ShaFinal(&sha, hash);
  8098. if (ret !=0) {
  8099. flag = ret;
  8100. }
  8101. }
  8102. if (!flag) {
  8103. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  8104. flag = WOLFSSL_FATAL_ERROR;
  8105. }
  8106. }
  8107. /* Try passing in bad values. */
  8108. if (!flag) {
  8109. b.input = NULL;
  8110. b.inLen = 0;
  8111. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  8112. if (ret != 0) {
  8113. flag = ret;
  8114. }
  8115. }
  8116. if (!flag) {
  8117. c.input = NULL;
  8118. c.inLen = WC_SHA_DIGEST_SIZE;
  8119. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  8120. if (ret != BAD_FUNC_ARG) {
  8121. flag = WOLFSSL_FATAL_ERROR;
  8122. }
  8123. }
  8124. if (!flag) {
  8125. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8126. if (ret != BAD_FUNC_ARG) {
  8127. flag = WOLFSSL_FATAL_ERROR;
  8128. }
  8129. }
  8130. wc_ShaFree(&sha);
  8131. /* If not returned then the unit test passed test vectors. */
  8132. printf(resultFmt, flag == 0 ? passed : failed);
  8133. #endif
  8134. return flag;
  8135. } /* END test_wc_ShaUpdate() */
  8136. /*
  8137. * Unit test on wc_ShaFinal
  8138. */
  8139. static int test_wc_ShaFinal (void)
  8140. {
  8141. int flag = 0;
  8142. #ifndef NO_SHA
  8143. wc_Sha sha;
  8144. byte* hash_test[3];
  8145. byte hash1[WC_SHA_DIGEST_SIZE];
  8146. byte hash2[2*WC_SHA_DIGEST_SIZE];
  8147. byte hash3[5*WC_SHA_DIGEST_SIZE];
  8148. int times, i, ret;
  8149. /*Initialize*/
  8150. ret = wc_InitSha(&sha);
  8151. if (ret) {
  8152. flag = ret;
  8153. }
  8154. if (!flag) {
  8155. hash_test[0] = hash1;
  8156. hash_test[1] = hash2;
  8157. hash_test[2] = hash3;
  8158. }
  8159. times = sizeof(hash_test)/sizeof(byte*);
  8160. /* Good test args. */
  8161. printf(testingFmt, "wc_ShaFinal()");
  8162. for (i = 0; i < times; i++) {
  8163. if (!flag) {
  8164. ret = wc_ShaFinal(&sha, hash_test[i]);
  8165. if (ret != 0) {
  8166. flag = WOLFSSL_FATAL_ERROR;
  8167. }
  8168. }
  8169. }
  8170. /* Test bad args. */
  8171. if (!flag) {
  8172. ret = wc_ShaFinal(NULL, NULL);
  8173. if (ret != BAD_FUNC_ARG) {
  8174. flag = WOLFSSL_FATAL_ERROR;
  8175. }
  8176. }
  8177. if (!flag) {
  8178. ret = wc_ShaFinal(NULL, hash1);
  8179. if (ret != BAD_FUNC_ARG) {
  8180. flag = WOLFSSL_FATAL_ERROR;
  8181. }
  8182. }
  8183. if (!flag) {
  8184. ret = wc_ShaFinal(&sha, NULL);
  8185. if (ret != BAD_FUNC_ARG) {
  8186. flag = WOLFSSL_FATAL_ERROR;
  8187. }
  8188. }
  8189. wc_ShaFree(&sha);
  8190. printf(resultFmt, flag == 0 ? passed : failed);
  8191. #endif
  8192. return flag;
  8193. } /* END test_wc_ShaFinal */
  8194. /*
  8195. * Unit test for wc_InitSha256()
  8196. */
  8197. static int test_wc_InitSha256 (void)
  8198. {
  8199. int flag = 0;
  8200. #ifndef NO_SHA256
  8201. wc_Sha256 sha256;
  8202. int ret;
  8203. printf(testingFmt, "wc_InitSha256()");
  8204. /* Test good arg. */
  8205. ret = wc_InitSha256(&sha256);
  8206. if (ret != 0) {
  8207. flag = WOLFSSL_FATAL_ERROR;
  8208. }
  8209. /* Test bad arg. */
  8210. if (!flag) {
  8211. ret = wc_InitSha256(NULL);
  8212. if (ret != BAD_FUNC_ARG) {
  8213. flag = WOLFSSL_FATAL_ERROR;
  8214. }
  8215. }
  8216. wc_Sha256Free(&sha256);
  8217. printf(resultFmt, flag == 0 ? passed : failed);
  8218. #endif
  8219. return flag;
  8220. } /* END test_wc_InitSha256 */
  8221. /*
  8222. * Unit test for wc_Sha256Update()
  8223. */
  8224. static int test_wc_Sha256Update (void)
  8225. {
  8226. int flag = 0;
  8227. #ifndef NO_SHA256
  8228. wc_Sha256 sha256;
  8229. byte hash[WC_SHA256_DIGEST_SIZE];
  8230. testVector a, b, c;
  8231. int ret;
  8232. ret = wc_InitSha256(&sha256);
  8233. if (ret != 0) {
  8234. flag = ret;
  8235. }
  8236. printf(testingFmt, "wc_Sha256Update()");
  8237. /* Input. */
  8238. if (!flag) {
  8239. a.input = "a";
  8240. a.inLen = XSTRLEN(a.input);
  8241. ret = wc_Sha256Update(&sha256, NULL, 0);
  8242. if (ret != 0) {
  8243. flag = ret;
  8244. }
  8245. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  8246. if (ret != 0) {
  8247. flag = ret;
  8248. }
  8249. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  8250. if (ret != 0) {
  8251. flag = ret;
  8252. }
  8253. }
  8254. if (!flag) {
  8255. ret = wc_Sha256Final(&sha256, hash);
  8256. if (ret != 0) {
  8257. flag = ret;
  8258. }
  8259. }
  8260. /* Update input. */
  8261. if (!flag) {
  8262. a.input = "abc";
  8263. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  8264. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  8265. "\x15\xAD";
  8266. a.inLen = XSTRLEN(a.input);
  8267. a.outLen = XSTRLEN(a.output);
  8268. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  8269. if (ret != 0) {
  8270. flag = ret;
  8271. }
  8272. }
  8273. if (!flag) {
  8274. ret = wc_Sha256Final(&sha256, hash);
  8275. if (ret != 0) {
  8276. flag = ret;
  8277. }
  8278. }
  8279. if (!flag) {
  8280. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  8281. flag = WOLFSSL_FATAL_ERROR;
  8282. }
  8283. }
  8284. /* Try passing in bad values */
  8285. if (!flag) {
  8286. b.input = NULL;
  8287. b.inLen = 0;
  8288. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  8289. if (ret != 0) {
  8290. flag = ret;
  8291. }
  8292. }
  8293. if (!flag) {
  8294. c.input = NULL;
  8295. c.inLen = WC_SHA256_DIGEST_SIZE;
  8296. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  8297. if (ret != BAD_FUNC_ARG) {
  8298. flag = WOLFSSL_FATAL_ERROR;
  8299. }
  8300. }
  8301. if (!flag) {
  8302. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  8303. if (ret != BAD_FUNC_ARG) {
  8304. flag = WOLFSSL_FATAL_ERROR;
  8305. }
  8306. }
  8307. wc_Sha256Free(&sha256);
  8308. /* If not returned then the unit test passed. */
  8309. printf(resultFmt, flag == 0 ? passed : failed);
  8310. #endif
  8311. return flag;
  8312. } /* END test_wc_Sha256Update */
  8313. /*
  8314. * Unit test function for wc_Sha256Final()
  8315. */
  8316. static int test_wc_Sha256Final (void)
  8317. {
  8318. int flag = 0;
  8319. #ifndef NO_SHA256
  8320. wc_Sha256 sha256;
  8321. byte* hash_test[3];
  8322. byte hash1[WC_SHA256_DIGEST_SIZE];
  8323. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  8324. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  8325. int times, i, ret;
  8326. /* Initialize */
  8327. ret = wc_InitSha256(&sha256);
  8328. if (ret != 0) {
  8329. flag = ret;
  8330. }
  8331. if (!flag) {
  8332. hash_test[0] = hash1;
  8333. hash_test[1] = hash2;
  8334. hash_test[2] = hash3;
  8335. }
  8336. times = sizeof(hash_test) / sizeof(byte*);
  8337. /* Good test args. */
  8338. printf(testingFmt, "wc_Sha256Final()");
  8339. for (i = 0; i < times; i++) {
  8340. if (!flag) {
  8341. ret = wc_Sha256Final(&sha256, hash_test[i]);
  8342. if (ret != 0) {
  8343. flag = WOLFSSL_FATAL_ERROR;
  8344. }
  8345. }
  8346. }
  8347. /* Test bad args. */
  8348. if (!flag ) {
  8349. ret = wc_Sha256Final(NULL, NULL);
  8350. if (ret != BAD_FUNC_ARG) {
  8351. flag = WOLFSSL_FATAL_ERROR;
  8352. }
  8353. }
  8354. if (!flag) {
  8355. ret = wc_Sha256Final(NULL, hash1);
  8356. if (ret != BAD_FUNC_ARG) {
  8357. flag = WOLFSSL_FATAL_ERROR;
  8358. }
  8359. }
  8360. if (!flag) {
  8361. ret = wc_Sha256Final(&sha256, NULL);
  8362. if (ret != BAD_FUNC_ARG) {
  8363. flag = WOLFSSL_FATAL_ERROR;
  8364. }
  8365. }
  8366. wc_Sha256Free(&sha256);
  8367. printf(resultFmt, flag == 0 ? passed : failed);
  8368. #endif
  8369. return flag;
  8370. } /* END test_wc_Sha256Final */
  8371. /*
  8372. * Unit test function for wc_Sha256FinalRaw()
  8373. */
  8374. static int test_wc_Sha256FinalRaw (void)
  8375. {
  8376. int flag = 0;
  8377. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  8378. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  8379. !defined(WOLFSSL_NO_HASH_RAW)
  8380. wc_Sha256 sha256;
  8381. byte* hash_test[3];
  8382. byte hash1[WC_SHA256_DIGEST_SIZE];
  8383. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  8384. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  8385. int times, i, ret;
  8386. /* Initialize */
  8387. ret = wc_InitSha256(&sha256);
  8388. if (ret != 0) {
  8389. flag = ret;
  8390. }
  8391. if (!flag) {
  8392. hash_test[0] = hash1;
  8393. hash_test[1] = hash2;
  8394. hash_test[2] = hash3;
  8395. }
  8396. times = sizeof(hash_test) / sizeof(byte*);
  8397. /* Good test args. */
  8398. printf(testingFmt, "wc_Sha256FinalRaw()");
  8399. for (i = 0; i < times; i++) {
  8400. if (!flag) {
  8401. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  8402. if (ret != 0) {
  8403. flag = WOLFSSL_FATAL_ERROR;
  8404. }
  8405. }
  8406. }
  8407. /* Test bad args. */
  8408. if (!flag ) {
  8409. ret = wc_Sha256FinalRaw(NULL, NULL);
  8410. if (ret != BAD_FUNC_ARG) {
  8411. flag = WOLFSSL_FATAL_ERROR;
  8412. }
  8413. }
  8414. if (!flag) {
  8415. ret = wc_Sha256FinalRaw(NULL, hash1);
  8416. if (ret != BAD_FUNC_ARG) {
  8417. flag = WOLFSSL_FATAL_ERROR;
  8418. }
  8419. }
  8420. if (!flag) {
  8421. ret = wc_Sha256FinalRaw(&sha256, NULL);
  8422. if (ret != BAD_FUNC_ARG) {
  8423. flag = WOLFSSL_FATAL_ERROR;
  8424. }
  8425. }
  8426. wc_Sha256Free(&sha256);
  8427. printf(resultFmt, flag == 0 ? passed : failed);
  8428. #endif
  8429. return flag;
  8430. } /* END test_wc_Sha256FinalRaw */
  8431. /*
  8432. * Unit test function for wc_Sha256GetFlags()
  8433. */
  8434. static int test_wc_Sha256GetFlags (void)
  8435. {
  8436. int flag = 0;
  8437. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  8438. wc_Sha256 sha256;
  8439. word32 flags = 0;
  8440. printf(testingFmt, "wc_Sha256GetFlags()");
  8441. /* Initialize */
  8442. flag = wc_InitSha256(&sha256);
  8443. if (flag == 0) {
  8444. flag = wc_Sha256GetFlags(&sha256, &flags);
  8445. }
  8446. if (flag == 0) {
  8447. if (flags & WC_HASH_FLAG_ISCOPY) {
  8448. flag = 0;
  8449. }
  8450. }
  8451. wc_Sha256Free(&sha256);
  8452. printf(resultFmt, flag == 0 ? passed : failed);
  8453. #endif
  8454. return flag;
  8455. } /* END test_wc_Sha256GetFlags */
  8456. /*
  8457. * Unit test function for wc_Sha256Free()
  8458. */
  8459. static int test_wc_Sha256Free (void)
  8460. {
  8461. int flag = 0;
  8462. #ifndef NO_SHA256
  8463. printf(testingFmt, "wc_Sha256Free()");
  8464. wc_Sha256Free(NULL);
  8465. printf(resultFmt, flag == 0 ? passed : failed);
  8466. #endif
  8467. return flag;
  8468. } /* END test_wc_Sha256Free */
  8469. /*
  8470. * Unit test function for wc_Sha256GetHash()
  8471. */
  8472. static int test_wc_Sha256GetHash (void)
  8473. {
  8474. int flag = 0;
  8475. #ifndef NO_SHA256
  8476. wc_Sha256 sha256;
  8477. byte hash1[WC_SHA256_DIGEST_SIZE];
  8478. printf(testingFmt, "wc_Sha256GetHash()");
  8479. /* Initialize */
  8480. flag = wc_InitSha256(&sha256);
  8481. if (flag == 0) {
  8482. flag = wc_Sha256GetHash(&sha256, hash1);
  8483. }
  8484. /*test bad arguements*/
  8485. if (flag == 0) {
  8486. flag = wc_Sha256GetHash(NULL, NULL);
  8487. if (flag == BAD_FUNC_ARG) {
  8488. flag = 0;
  8489. }
  8490. }
  8491. if (flag == 0) {
  8492. flag = wc_Sha256GetHash(NULL, hash1);
  8493. if (flag == BAD_FUNC_ARG) {
  8494. flag = 0;
  8495. }
  8496. }
  8497. if (flag == 0) {
  8498. flag = wc_Sha256GetHash(&sha256, NULL);
  8499. if (flag == BAD_FUNC_ARG) {
  8500. flag = 0;
  8501. }
  8502. }
  8503. wc_Sha256Free(&sha256);
  8504. printf(resultFmt, flag == 0 ? passed : failed);
  8505. #endif
  8506. return flag;
  8507. } /* END test_wc_Sha256GetHash */
  8508. /*
  8509. * Unit test function for wc_Sha256Copy()
  8510. */
  8511. static int test_wc_Sha256Copy (void)
  8512. {
  8513. int flag = 0;
  8514. #ifndef NO_SHA256
  8515. wc_Sha256 sha256;
  8516. wc_Sha256 temp;
  8517. printf(testingFmt, "wc_Sha256Copy()");
  8518. /* Initialize */
  8519. flag = wc_InitSha256(&sha256);
  8520. if (flag == 0) {
  8521. flag = wc_InitSha256(&temp);
  8522. }
  8523. if (flag == 0) {
  8524. flag = wc_Sha256Copy(&sha256, &temp);
  8525. }
  8526. /*test bad arguements*/
  8527. if (flag == 0) {
  8528. flag = wc_Sha256Copy(NULL, NULL);
  8529. if (flag == BAD_FUNC_ARG) {
  8530. flag = 0;
  8531. }
  8532. }
  8533. if (flag == 0) {
  8534. flag = wc_Sha256Copy(NULL, &temp);
  8535. if (flag == BAD_FUNC_ARG) {
  8536. flag = 0;
  8537. }
  8538. }
  8539. if (flag == 0) {
  8540. flag = wc_Sha256Copy(&sha256, NULL);
  8541. if (flag == BAD_FUNC_ARG) {
  8542. flag = 0;
  8543. }
  8544. }
  8545. wc_Sha256Free(&sha256);
  8546. wc_Sha256Free(&temp);
  8547. printf(resultFmt, flag == 0 ? passed : failed);
  8548. #endif
  8549. return flag;
  8550. } /* END test_wc_Sha256Copy */
  8551. /*
  8552. * Testing wc_InitSha512()
  8553. */
  8554. static int test_wc_InitSha512 (void)
  8555. {
  8556. int flag = 0;
  8557. #ifdef WOLFSSL_SHA512
  8558. wc_Sha512 sha512;
  8559. int ret;
  8560. printf(testingFmt, "wc_InitSha512()");
  8561. /* Test good arg. */
  8562. ret = wc_InitSha512(&sha512);
  8563. if (ret != 0) {
  8564. flag = WOLFSSL_FATAL_ERROR;
  8565. }
  8566. /* Test bad arg. */
  8567. if (!flag) {
  8568. ret = wc_InitSha512(NULL);
  8569. if (ret != BAD_FUNC_ARG) {
  8570. flag = WOLFSSL_FATAL_ERROR;
  8571. }
  8572. }
  8573. wc_Sha512Free(&sha512);
  8574. printf(resultFmt, flag == 0 ? passed : failed);
  8575. #endif
  8576. return flag;
  8577. } /* END test_wc_InitSha512 */
  8578. /*
  8579. * wc_Sha512Update() test.
  8580. */
  8581. static int test_wc_Sha512Update (void)
  8582. {
  8583. int flag = 0;
  8584. #ifdef WOLFSSL_SHA512
  8585. wc_Sha512 sha512;
  8586. byte hash[WC_SHA512_DIGEST_SIZE];
  8587. testVector a, b, c;
  8588. int ret;
  8589. ret = wc_InitSha512(&sha512);
  8590. if (ret != 0) {
  8591. flag = ret;
  8592. }
  8593. printf(testingFmt, "wc_Sha512Update()");
  8594. /* Input. */
  8595. if (!flag) {
  8596. a.input = "a";
  8597. a.inLen = XSTRLEN(a.input);
  8598. ret = wc_Sha512Update(&sha512, NULL, 0);
  8599. if (ret != 0) {
  8600. flag = ret;
  8601. }
  8602. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  8603. if (ret != 0) {
  8604. flag = ret;
  8605. }
  8606. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  8607. if (ret != 0) {
  8608. flag = ret;
  8609. }
  8610. ret = wc_Sha512Final(&sha512, hash);
  8611. if (ret != 0) {
  8612. flag = ret;
  8613. }
  8614. }
  8615. /* Update input. */
  8616. if (!flag) {
  8617. a.input = "abc";
  8618. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  8619. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  8620. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  8621. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  8622. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  8623. a.inLen = XSTRLEN(a.input);
  8624. a.outLen = XSTRLEN(a.output);
  8625. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  8626. if (ret != 0) {
  8627. flag = ret;
  8628. }
  8629. }
  8630. if (!flag) {
  8631. ret = wc_Sha512Final(&sha512, hash);
  8632. if (ret != 0) {
  8633. flag = ret;
  8634. }
  8635. }
  8636. if (!flag) {
  8637. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  8638. flag = WOLFSSL_FATAL_ERROR;
  8639. }
  8640. }
  8641. /* Try passing in bad values */
  8642. if (!flag) {
  8643. b.input = NULL;
  8644. b.inLen = 0;
  8645. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  8646. if (ret != 0) {
  8647. flag = ret;
  8648. }
  8649. }
  8650. if (!flag) {
  8651. c.input = NULL;
  8652. c.inLen = WC_SHA512_DIGEST_SIZE;
  8653. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  8654. if (ret != BAD_FUNC_ARG) {
  8655. flag = WOLFSSL_FATAL_ERROR;
  8656. }
  8657. }
  8658. if (!flag) {
  8659. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  8660. if (ret != BAD_FUNC_ARG) {
  8661. flag = WOLFSSL_FATAL_ERROR;
  8662. }
  8663. }
  8664. wc_Sha512Free(&sha512);
  8665. /* If not returned then the unit test passed test vectors. */
  8666. printf(resultFmt, flag == 0 ? passed : failed);
  8667. #endif
  8668. return flag;
  8669. } /* END test_wc_Sha512Update */
  8670. #ifdef WOLFSSL_SHA512
  8671. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  8672. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  8673. /* Perfoms test for
  8674. * - wc_Sha512Final/wc_Sha512FinalRaw
  8675. * - wc_Sha512_224Final/wc_Sha512_224Final
  8676. * - wc_Sha512_256Final/wc_Sha512_256Final
  8677. * parameter:
  8678. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  8679. * WC_HASH_TYPE_SHA512_256
  8680. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  8681. *return 0 on success
  8682. */
  8683. static int test_Sha512_Family_Final(int type, int isRaw)
  8684. {
  8685. wc_Sha512 sha512;
  8686. byte* hash_test[3];
  8687. byte hash1[WC_SHA512_DIGEST_SIZE];
  8688. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  8689. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  8690. int times, i, ret;
  8691. int(*initFp)(wc_Sha512*);
  8692. int(*finalFp)(wc_Sha512*, byte*);
  8693. void(*freeFp)(wc_Sha512*);
  8694. if (type == WC_HASH_TYPE_SHA512) {
  8695. initFp = wc_InitSha512;
  8696. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  8697. !defined(WOLFSSL_NO_HASH_RAW)
  8698. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  8699. #else
  8700. finalFp = (isRaw)? NULL : wc_Sha512Final;
  8701. #endif
  8702. freeFp = wc_Sha512Free;
  8703. }
  8704. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8705. #if !defined(WOLFSSL_NOSHA512_224)
  8706. else if (type == WC_HASH_TYPE_SHA512_224) {
  8707. initFp = wc_InitSha512_224;
  8708. #if !defined(WOLFSSL_NO_HASH_RAW)
  8709. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  8710. #else
  8711. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  8712. #endif
  8713. freeFp = wc_Sha512_224Free;
  8714. }
  8715. #endif
  8716. #if !defined(WOLFSSL_NOSHA512_256)
  8717. else if (type == WC_HASH_TYPE_SHA512_256) {
  8718. initFp = wc_InitSha512_256;
  8719. #if !defined(WOLFSSL_NO_HASH_RAW)
  8720. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  8721. #else
  8722. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  8723. #endif
  8724. freeFp = wc_Sha512_256Free;
  8725. }
  8726. #endif
  8727. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  8728. else
  8729. return BAD_FUNC_ARG;
  8730. /* Initialize */
  8731. ret = initFp(&sha512);
  8732. if (!ret) {
  8733. hash_test[0] = hash1;
  8734. hash_test[1] = hash2;
  8735. hash_test[2] = hash3;
  8736. }
  8737. times = sizeof(hash_test) / sizeof(byte *);
  8738. /* Good test args. */
  8739. for (i = 0; i < times && ret == 0; i++) {
  8740. ret = finalFp(&sha512, hash_test[i]);
  8741. }
  8742. /* Test bad args. */
  8743. if (!ret) {
  8744. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  8745. ret = WOLFSSL_FATAL_ERROR;
  8746. }
  8747. }
  8748. if (!ret) {
  8749. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  8750. ret = WOLFSSL_FATAL_ERROR;
  8751. }
  8752. }
  8753. if (!ret) {
  8754. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  8755. ret = WOLFSSL_FATAL_ERROR;
  8756. }
  8757. }
  8758. freeFp(&sha512);
  8759. return ret;
  8760. }
  8761. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  8762. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  8763. #endif /* WOLFSSL_SHA512 */
  8764. /*
  8765. * Unit test function for wc_Sha512Final()
  8766. */
  8767. static int test_wc_Sha512Final (void)
  8768. {
  8769. int flag = 0;
  8770. #ifdef WOLFSSL_SHA512
  8771. wc_Sha512 sha512;
  8772. byte* hash_test[3];
  8773. byte hash1[WC_SHA512_DIGEST_SIZE];
  8774. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  8775. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  8776. int times, i, ret;
  8777. /* Initialize */
  8778. ret = wc_InitSha512(&sha512);
  8779. if (ret != 0) {
  8780. flag = ret;
  8781. }
  8782. if (!flag) {
  8783. hash_test[0] = hash1;
  8784. hash_test[1] = hash2;
  8785. hash_test[2] = hash3;
  8786. }
  8787. times = sizeof(hash_test) / sizeof(byte *);
  8788. /* Good test args. */
  8789. printf(testingFmt, "wc_Sha512Final()");
  8790. for (i = 0; i < times; i++) {
  8791. if (!flag) {
  8792. ret = wc_Sha512Final(&sha512, hash_test[i]);
  8793. if (ret != 0) {
  8794. flag = WOLFSSL_FATAL_ERROR;
  8795. }
  8796. }
  8797. }
  8798. /* Test bad args. */
  8799. if (!flag) {
  8800. ret = wc_Sha512Final(NULL, NULL);
  8801. if (ret != BAD_FUNC_ARG) {
  8802. flag = WOLFSSL_FATAL_ERROR;
  8803. }
  8804. if (!flag) {}
  8805. ret = wc_Sha512Final(NULL, hash1);
  8806. if (ret != BAD_FUNC_ARG) {
  8807. flag = WOLFSSL_FATAL_ERROR;
  8808. }
  8809. }
  8810. if (!flag) {
  8811. ret = wc_Sha512Final(&sha512, NULL);
  8812. if (ret != BAD_FUNC_ARG) {
  8813. flag = WOLFSSL_FATAL_ERROR;
  8814. }
  8815. }
  8816. wc_Sha512Free(&sha512);
  8817. printf(resultFmt, flag == 0 ? passed : failed);
  8818. #endif
  8819. return flag;
  8820. } /* END test_wc_Sha512Final */
  8821. /*
  8822. * Unit test function for wc_Sha512GetFlags()
  8823. */
  8824. static int test_wc_Sha512GetFlags (void)
  8825. {
  8826. int flag = 0;
  8827. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  8828. wc_Sha512 sha512;
  8829. word32 flags = 0;
  8830. printf(testingFmt, "wc_Sha512GetFlags()");
  8831. /* Initialize */
  8832. flag = wc_InitSha512(&sha512);
  8833. if (flag == 0) {
  8834. flag = wc_Sha512GetFlags(&sha512, &flags);
  8835. }
  8836. if (flag == 0) {
  8837. if (flags & WC_HASH_FLAG_ISCOPY) {
  8838. flag = 0;
  8839. }
  8840. }
  8841. wc_Sha512Free(&sha512);
  8842. printf(resultFmt, flag == 0 ? passed : failed);
  8843. #endif
  8844. return flag;
  8845. } /* END test_wc_Sha512GetFlags */
  8846. /*
  8847. * Unit test function for wc_Sha512FinalRaw()
  8848. */
  8849. static int test_wc_Sha512FinalRaw (void)
  8850. {
  8851. int flag = 0;
  8852. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  8853. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  8854. !defined(WOLFSSL_NO_HASH_RAW)
  8855. wc_Sha512 sha512;
  8856. byte* hash_test[3];
  8857. byte hash1[WC_SHA512_DIGEST_SIZE];
  8858. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  8859. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  8860. int times, i, ret;
  8861. /* Initialize */
  8862. ret = wc_InitSha512(&sha512);
  8863. if (ret != 0) {
  8864. flag = ret;
  8865. }
  8866. if (!flag) {
  8867. hash_test[0] = hash1;
  8868. hash_test[1] = hash2;
  8869. hash_test[2] = hash3;
  8870. }
  8871. times = sizeof(hash_test) / sizeof(byte*);
  8872. /* Good test args. */
  8873. printf(testingFmt, "wc_Sha512FinalRaw()");
  8874. for (i = 0; i < times; i++) {
  8875. if (!flag) {
  8876. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  8877. if (ret != 0) {
  8878. flag = WOLFSSL_FATAL_ERROR;
  8879. }
  8880. }
  8881. }
  8882. /* Test bad args. */
  8883. if (!flag ) {
  8884. ret = wc_Sha512FinalRaw(NULL, NULL);
  8885. if (ret != BAD_FUNC_ARG) {
  8886. flag = WOLFSSL_FATAL_ERROR;
  8887. }
  8888. }
  8889. if (!flag) {
  8890. ret = wc_Sha512FinalRaw(NULL, hash1);
  8891. if (ret != BAD_FUNC_ARG) {
  8892. flag = WOLFSSL_FATAL_ERROR;
  8893. }
  8894. }
  8895. if (!flag) {
  8896. ret = wc_Sha512FinalRaw(&sha512, NULL);
  8897. if (ret != BAD_FUNC_ARG) {
  8898. flag = WOLFSSL_FATAL_ERROR;
  8899. }
  8900. }
  8901. wc_Sha512Free(&sha512);
  8902. printf(resultFmt, flag == 0 ? passed : failed);
  8903. #endif
  8904. return flag;
  8905. } /* END test_wc_Sha512FinalRaw */
  8906. /*
  8907. * Unit test function for wc_Sha512Free()
  8908. */
  8909. static int test_wc_Sha512Free (void)
  8910. {
  8911. int flag = 0;
  8912. #ifdef WOLFSSL_SHA512
  8913. printf(testingFmt, "wc_Sha512Free()");
  8914. wc_Sha512Free(NULL);
  8915. printf(resultFmt, flag == 0 ? passed : failed);
  8916. #endif
  8917. return flag;
  8918. } /* END test_wc_Sha512Free */
  8919. #ifdef WOLFSSL_SHA512
  8920. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  8921. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  8922. static int test_Sha512_Family_GetHash(int type )
  8923. {
  8924. int flag = 0;
  8925. int(*initFp)(wc_Sha512*);
  8926. int(*ghashFp)(wc_Sha512*, byte*);
  8927. wc_Sha512 sha512;
  8928. byte hash1[WC_SHA512_DIGEST_SIZE];
  8929. if (type == WC_HASH_TYPE_SHA512) {
  8930. initFp = wc_InitSha512;
  8931. ghashFp = wc_Sha512GetHash;
  8932. }
  8933. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8934. #if !defined(WOLFSSL_NOSHA512_224)
  8935. else if (type == WC_HASH_TYPE_SHA512_224) {
  8936. initFp = wc_InitSha512_224;
  8937. ghashFp = wc_Sha512_224GetHash;
  8938. }
  8939. #endif
  8940. #if !defined(WOLFSSL_NOSHA512_256)
  8941. else if (type == WC_HASH_TYPE_SHA512_256) {
  8942. initFp = wc_InitSha512_256;
  8943. ghashFp = wc_Sha512_256GetHash;
  8944. }
  8945. #endif
  8946. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  8947. else {
  8948. initFp = NULL;
  8949. ghashFp = NULL;
  8950. }
  8951. if (initFp == NULL || ghashFp == NULL)
  8952. return WOLFSSL_FATAL_ERROR;
  8953. if (!flag) {
  8954. flag = initFp(&sha512);
  8955. }
  8956. if (!flag) {
  8957. flag = ghashFp(&sha512, hash1);
  8958. }
  8959. /*test bad arguements*/
  8960. if (!flag) {
  8961. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  8962. flag = WOLFSSL_FATAL_ERROR;
  8963. }
  8964. if (!flag) {
  8965. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  8966. flag = WOLFSSL_FATAL_ERROR;
  8967. }
  8968. if (!flag) {
  8969. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  8970. flag = WOLFSSL_FATAL_ERROR;
  8971. }
  8972. wc_Sha512Free(&sha512);
  8973. return flag;
  8974. }
  8975. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  8976. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  8977. #endif /* WOLFSSL_SHA512 */
  8978. /*
  8979. * Unit test function for wc_Sha512GetHash()
  8980. */
  8981. static int test_wc_Sha512GetHash (void)
  8982. {
  8983. int flag = 0;
  8984. #ifdef WOLFSSL_SHA512
  8985. wc_Sha512 sha512;
  8986. byte hash1[WC_SHA512_DIGEST_SIZE];
  8987. printf(testingFmt, "wc_Sha512GetHash()");
  8988. /* Initialize */
  8989. flag = wc_InitSha512(&sha512);
  8990. if (flag == 0) {
  8991. flag = wc_Sha512GetHash(&sha512, hash1);
  8992. }
  8993. /*test bad arguements*/
  8994. if (flag == 0) {
  8995. flag = wc_Sha512GetHash(NULL, NULL);
  8996. if (flag == BAD_FUNC_ARG) {
  8997. flag = 0;
  8998. }
  8999. }
  9000. if (flag == 0) {
  9001. flag = wc_Sha512GetHash(NULL, hash1);
  9002. if (flag == BAD_FUNC_ARG) {
  9003. flag = 0;
  9004. }
  9005. }
  9006. if (flag == 0) {
  9007. flag = wc_Sha512GetHash(&sha512, NULL);
  9008. if (flag == BAD_FUNC_ARG) {
  9009. flag = 0;
  9010. }
  9011. }
  9012. wc_Sha512Free(&sha512);
  9013. printf(resultFmt, flag == 0 ? passed : failed);
  9014. #endif
  9015. return flag;
  9016. } /* END test_wc_Sha512GetHash */
  9017. /*
  9018. * Unit test function for wc_Sha512Copy()
  9019. */
  9020. static int test_wc_Sha512Copy (void)
  9021. {
  9022. int flag = 0;
  9023. #ifdef WOLFSSL_SHA512
  9024. wc_Sha512 sha512;
  9025. wc_Sha512 temp;
  9026. printf(testingFmt, "wc_Sha512Copy()");
  9027. /* Initialize */
  9028. flag = wc_InitSha512(&sha512);
  9029. if (flag == 0) {
  9030. flag = wc_InitSha512(&temp);
  9031. }
  9032. if (flag == 0) {
  9033. flag = wc_Sha512Copy(&sha512, &temp);
  9034. }
  9035. /*test bad arguements*/
  9036. if (flag == 0) {
  9037. flag = wc_Sha512Copy(NULL, NULL);
  9038. if (flag == BAD_FUNC_ARG) {
  9039. flag = 0;
  9040. }
  9041. }
  9042. if (flag == 0) {
  9043. flag = wc_Sha512Copy(NULL, &temp);
  9044. if (flag == BAD_FUNC_ARG) {
  9045. flag = 0;
  9046. }
  9047. }
  9048. if (flag == 0) {
  9049. flag = wc_Sha512Copy(&sha512, NULL);
  9050. if (flag == BAD_FUNC_ARG) {
  9051. flag = 0;
  9052. }
  9053. }
  9054. wc_Sha512Free(&sha512);
  9055. wc_Sha512Free(&temp);
  9056. printf(resultFmt, flag == 0 ? passed : failed);
  9057. #endif
  9058. return flag;
  9059. } /* END test_wc_Sha512Copy */
  9060. static int test_wc_InitSha512_224 (void)
  9061. {
  9062. int flag = 0;
  9063. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9064. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9065. wc_Sha512 sha512;
  9066. int ret;
  9067. printf(testingFmt, "wc_InitSha512_224()");
  9068. /* Test good arg. */
  9069. ret = wc_InitSha512_224(&sha512);
  9070. if (ret != 0) {
  9071. flag = WOLFSSL_FATAL_ERROR;
  9072. }
  9073. /* Test bad arg. */
  9074. if (!flag) {
  9075. ret = wc_InitSha512_224(NULL);
  9076. if (ret != BAD_FUNC_ARG) {
  9077. flag = WOLFSSL_FATAL_ERROR;
  9078. }
  9079. }
  9080. wc_Sha512_224Free(&sha512);
  9081. printf(resultFmt, flag == 0 ? passed : failed);
  9082. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9083. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9084. return flag;
  9085. }
  9086. static int test_wc_Sha512_224Update (void)
  9087. {
  9088. int flag = 0;
  9089. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9090. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9091. wc_Sha512 sha512;
  9092. byte hash[WC_SHA512_DIGEST_SIZE];
  9093. testVector a, c;
  9094. int ret;
  9095. ret = wc_InitSha512_224(&sha512);
  9096. if (ret != 0) {
  9097. flag = ret;
  9098. }
  9099. printf(testingFmt, "wc_Sha512_224Update()");
  9100. /* Input. */
  9101. if (!flag) {
  9102. a.input = "a";
  9103. a.inLen = XSTRLEN(a.input);
  9104. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  9105. if (ret != 0) {
  9106. flag = ret;
  9107. }
  9108. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  9109. if (ret != 0) {
  9110. flag = ret;
  9111. }
  9112. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9113. if (ret != 0) {
  9114. flag = ret;
  9115. }
  9116. ret = wc_Sha512_224Final(&sha512, hash);
  9117. if (ret != 0) {
  9118. flag = ret;
  9119. }
  9120. }
  9121. /* Update input. */
  9122. if (!flag) {
  9123. a.input = "abc";
  9124. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  9125. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  9126. a.inLen = XSTRLEN(a.input);
  9127. a.outLen = XSTRLEN(a.output);
  9128. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9129. if (ret != 0) {
  9130. flag = ret;
  9131. }
  9132. }
  9133. if (!flag) {
  9134. ret = wc_Sha512_224Final(&sha512, hash);
  9135. if (ret != 0) {
  9136. flag = ret;
  9137. }
  9138. }
  9139. if (!flag) {
  9140. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  9141. flag = WOLFSSL_FATAL_ERROR;
  9142. }
  9143. }
  9144. if (!flag) {
  9145. c.input = NULL;
  9146. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  9147. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9148. if (ret != BAD_FUNC_ARG) {
  9149. flag = WOLFSSL_FATAL_ERROR;
  9150. }
  9151. }
  9152. if (!flag) {
  9153. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  9154. if (ret != BAD_FUNC_ARG) {
  9155. flag = WOLFSSL_FATAL_ERROR;
  9156. }
  9157. }
  9158. wc_Sha512_224Free(&sha512);
  9159. /* If not returned then the unit test passed test vectors. */
  9160. printf(resultFmt, flag == 0 ? passed : failed);
  9161. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9162. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9163. return flag;
  9164. }
  9165. static int test_wc_Sha512_224Final (void)
  9166. {
  9167. int flag = 0;
  9168. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9169. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9170. printf(testingFmt, "wc_Sha512_224Final()");
  9171. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  9172. printf(resultFmt, flag == 0 ? passed : failed);
  9173. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9174. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9175. return flag;
  9176. }
  9177. static int test_wc_Sha512_224GetFlags (void)
  9178. {
  9179. int flag = 0;
  9180. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9181. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  9182. wc_Sha512 sha512, copy;
  9183. word32 flags = 0;
  9184. printf(testingFmt, "wc_Sha512_224GetFlags()");
  9185. /* Initialize */
  9186. flag = wc_InitSha512_224(&sha512);
  9187. if (!flag) {
  9188. flag = wc_InitSha512_224(&copy);
  9189. }
  9190. if (!flag) {
  9191. flag = wc_Sha512_224Copy(&sha512, &copy);
  9192. }
  9193. if (!flag) {
  9194. flag = wc_Sha512_224GetFlags(&copy, &flags);
  9195. }
  9196. if (!flag) {
  9197. if (flags & WC_HASH_FLAG_ISCOPY)
  9198. flag = 0;
  9199. else
  9200. flag = WOLFSSL_FATAL_ERROR;
  9201. }
  9202. wc_Sha512_224Free(&copy);
  9203. wc_Sha512_224Free(&sha512);
  9204. printf(resultFmt, flag == 0 ? passed : failed);
  9205. #endif
  9206. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9207. return flag;
  9208. }
  9209. static int test_wc_Sha512_224FinalRaw (void)
  9210. {
  9211. int flag = 0;
  9212. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9213. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  9214. !defined(WOLFSSL_NO_HASH_RAW)
  9215. printf(testingFmt, "wc_Sha512_224FinalRaw()");
  9216. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  9217. printf(resultFmt, flag == 0 ? passed : failed);
  9218. #endif
  9219. return flag;
  9220. }
  9221. static int test_wc_Sha512_224Free (void)
  9222. {
  9223. int flag = 0;
  9224. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9225. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9226. printf(testingFmt, "wc_Sha512_224Free()");
  9227. wc_Sha512_224Free(NULL);
  9228. printf(resultFmt, passed);
  9229. #endif
  9230. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9231. return flag;
  9232. }
  9233. static int test_wc_Sha512_224GetHash (void)
  9234. {
  9235. int flag = 0;
  9236. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9237. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9238. printf(testingFmt, "wc_Sha512_224GetHash()");
  9239. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  9240. printf(resultFmt, flag == 0 ? passed : failed);
  9241. #endif
  9242. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9243. return flag;
  9244. }
  9245. static int test_wc_Sha512_224Copy (void)
  9246. {
  9247. int flag = 0;
  9248. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9249. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9250. wc_Sha512 sha512;
  9251. wc_Sha512 temp;
  9252. printf(testingFmt, "wc_Sha512_224Copy()");
  9253. /* Initialize */
  9254. flag = wc_InitSha512_224(&sha512);
  9255. if (flag == 0) {
  9256. flag = wc_InitSha512_224(&temp);
  9257. }
  9258. if (flag == 0) {
  9259. flag = wc_Sha512_224Copy(&sha512, &temp);
  9260. }
  9261. /*test bad arguements*/
  9262. if (flag == 0) {
  9263. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  9264. flag = WOLFSSL_FATAL_ERROR;
  9265. }
  9266. if (flag == 0) {
  9267. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  9268. flag = WOLFSSL_FATAL_ERROR;
  9269. }
  9270. if (flag == 0) {
  9271. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  9272. flag = WOLFSSL_FATAL_ERROR;
  9273. }
  9274. wc_Sha512_224Free(&sha512);
  9275. wc_Sha512_224Free(&temp);
  9276. printf(resultFmt, flag == 0 ? passed : failed);
  9277. #endif
  9278. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9279. return flag;
  9280. }
  9281. static int test_wc_InitSha512_256 (void)
  9282. {
  9283. int flag = 0;
  9284. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9285. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9286. wc_Sha512 sha512;
  9287. int ret;
  9288. printf(testingFmt, "wc_InitSha512_256()");
  9289. /* Test good arg. */
  9290. ret = wc_InitSha512_256(&sha512);
  9291. if (ret != 0) {
  9292. flag = WOLFSSL_FATAL_ERROR;
  9293. }
  9294. /* Test bad arg. */
  9295. if (!flag) {
  9296. ret = wc_InitSha512_256(NULL);
  9297. if (ret != BAD_FUNC_ARG) {
  9298. flag = WOLFSSL_FATAL_ERROR;
  9299. }
  9300. }
  9301. wc_Sha512_256Free(&sha512);
  9302. printf(resultFmt, flag == 0 ? passed : failed);
  9303. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9304. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9305. return flag;
  9306. }
  9307. static int test_wc_Sha512_256Update (void)
  9308. {
  9309. int flag = 0;
  9310. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9311. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9312. wc_Sha512 sha512;
  9313. byte hash[WC_SHA512_DIGEST_SIZE];
  9314. testVector a, c;
  9315. int ret;
  9316. ret = wc_InitSha512_256(&sha512);
  9317. if (ret != 0) {
  9318. flag = ret;
  9319. }
  9320. printf(testingFmt, "wc_Sha512_256Update()");
  9321. /* Input. */
  9322. if (!flag) {
  9323. a.input = "a";
  9324. a.inLen = XSTRLEN(a.input);
  9325. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  9326. if (ret != 0) {
  9327. flag = ret;
  9328. }
  9329. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  9330. if (ret != 0) {
  9331. flag = ret;
  9332. }
  9333. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9334. if (ret != 0) {
  9335. flag = ret;
  9336. }
  9337. ret = wc_Sha512_256Final(&sha512, hash);
  9338. if (ret != 0) {
  9339. flag = ret;
  9340. }
  9341. }
  9342. /* Update input. */
  9343. if (!flag) {
  9344. a.input = "abc";
  9345. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  9346. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  9347. "\x07\xe7\xaf\x23";
  9348. a.inLen = XSTRLEN(a.input);
  9349. a.outLen = XSTRLEN(a.output);
  9350. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9351. if (ret != 0) {
  9352. flag = ret;
  9353. }
  9354. }
  9355. if (!flag) {
  9356. ret = wc_Sha512_256Final(&sha512, hash);
  9357. if (ret != 0) {
  9358. flag = ret;
  9359. }
  9360. }
  9361. if (!flag) {
  9362. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  9363. flag = WOLFSSL_FATAL_ERROR;
  9364. }
  9365. }
  9366. if (!flag) {
  9367. c.input = NULL;
  9368. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  9369. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9370. if (ret != BAD_FUNC_ARG) {
  9371. flag = WOLFSSL_FATAL_ERROR;
  9372. }
  9373. }
  9374. if (!flag) {
  9375. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  9376. if (ret != BAD_FUNC_ARG) {
  9377. flag = WOLFSSL_FATAL_ERROR;
  9378. }
  9379. }
  9380. wc_Sha512_256Free(&sha512);
  9381. /* If not returned then the unit test passed test vectors. */
  9382. printf(resultFmt, flag == 0 ? passed : failed);
  9383. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9384. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9385. return flag;
  9386. }
  9387. static int test_wc_Sha512_256Final (void)
  9388. {
  9389. int flag = 0;
  9390. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9391. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9392. printf(testingFmt, "wc_Sha512_256Final()");
  9393. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  9394. printf(resultFmt, flag == 0 ? passed : failed);
  9395. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9396. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9397. return flag;
  9398. }
  9399. static int test_wc_Sha512_256GetFlags (void)
  9400. {
  9401. int flag = 0;
  9402. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9403. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  9404. wc_Sha512 sha512, copy;
  9405. word32 flags = 0;
  9406. printf(testingFmt, "wc_Sha512_256GetFlags()");
  9407. /* Initialize */
  9408. flag = wc_InitSha512_256(&sha512);
  9409. if (!flag ) {
  9410. flag = wc_InitSha512_256(&copy);
  9411. }
  9412. if (!flag ) {
  9413. flag = wc_Sha512_256Copy(&sha512, &copy);
  9414. }
  9415. if (!flag ) {
  9416. flag = wc_Sha512_256GetFlags(&copy, &flags);
  9417. }
  9418. if (!flag) {
  9419. if (flags & WC_HASH_FLAG_ISCOPY)
  9420. flag = 0;
  9421. else
  9422. flag = WOLFSSL_FATAL_ERROR;
  9423. }
  9424. wc_Sha512_256Free(&sha512);
  9425. printf(resultFmt, flag == 0 ? passed : failed);
  9426. #endif
  9427. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9428. return flag;
  9429. }
  9430. static int test_wc_Sha512_256FinalRaw (void)
  9431. {
  9432. int flag = 0;
  9433. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9434. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  9435. !defined(WOLFSSL_NO_HASH_RAW)
  9436. printf(testingFmt, "wc_Sha512_256FinalRaw()");
  9437. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  9438. printf(resultFmt, flag == 0 ? passed : failed);
  9439. #endif
  9440. return flag;
  9441. }
  9442. static int test_wc_Sha512_256Free (void)
  9443. {
  9444. int flag = 0;
  9445. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9446. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9447. printf(testingFmt, "wc_Sha512_256Free()");
  9448. wc_Sha512_256Free(NULL);
  9449. printf(resultFmt, passed);
  9450. #endif
  9451. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9452. return flag;
  9453. }
  9454. static int test_wc_Sha512_256GetHash (void)
  9455. {
  9456. int flag = 0;
  9457. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9458. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9459. printf(testingFmt, "wc_Sha512_256GetHash()");
  9460. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  9461. printf(resultFmt, flag == 0 ? passed : failed);
  9462. #endif
  9463. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9464. return flag;
  9465. }
  9466. static int test_wc_Sha512_256Copy (void)
  9467. {
  9468. int flag = 0;
  9469. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9470. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9471. wc_Sha512 sha512;
  9472. wc_Sha512 temp;
  9473. printf(testingFmt, "wc_Sha512_256Copy()");
  9474. /* Initialize */
  9475. flag = wc_InitSha512_256(&sha512);
  9476. if (flag == 0) {
  9477. flag = wc_InitSha512_256(&temp);
  9478. }
  9479. if (flag == 0) {
  9480. flag = wc_Sha512_256Copy(&sha512, &temp);
  9481. }
  9482. /*test bad arguements*/
  9483. if (flag == 0) {
  9484. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  9485. flag = WOLFSSL_FATAL_ERROR;
  9486. }
  9487. if (flag == 0) {
  9488. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  9489. flag = WOLFSSL_FATAL_ERROR;
  9490. }
  9491. if (flag == 0) {
  9492. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  9493. flag = WOLFSSL_FATAL_ERROR;
  9494. }
  9495. wc_Sha512_256Free(&sha512);
  9496. wc_Sha512_256Free(&temp);
  9497. printf(resultFmt, flag == 0 ? passed : failed);
  9498. #endif
  9499. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9500. return flag;
  9501. }
  9502. /*
  9503. * Testing wc_InitSha384()
  9504. */
  9505. static int test_wc_InitSha384 (void)
  9506. {
  9507. int flag = 0;
  9508. #ifdef WOLFSSL_SHA384
  9509. wc_Sha384 sha384;
  9510. int ret;
  9511. printf(testingFmt, "wc_InitSha384()");
  9512. /* Test good arg. */
  9513. ret = wc_InitSha384(&sha384);
  9514. if (ret != 0) {
  9515. flag = WOLFSSL_FATAL_ERROR;
  9516. }
  9517. /* Test bad arg. */
  9518. if (!flag) {
  9519. ret = wc_InitSha384(NULL);
  9520. if (ret != BAD_FUNC_ARG) {
  9521. flag = WOLFSSL_FATAL_ERROR;
  9522. }
  9523. }
  9524. wc_Sha384Free(&sha384);
  9525. printf(resultFmt, flag == 0 ? passed : failed);
  9526. #endif
  9527. return flag;
  9528. } /* END test_wc_InitSha384 */
  9529. /*
  9530. * test wc_Sha384Update()
  9531. */
  9532. static int test_wc_Sha384Update (void)
  9533. {
  9534. int flag = 0;
  9535. #ifdef WOLFSSL_SHA384
  9536. wc_Sha384 sha384;
  9537. byte hash[WC_SHA384_DIGEST_SIZE];
  9538. testVector a, b, c;
  9539. int ret;
  9540. ret = wc_InitSha384(&sha384);
  9541. if (ret != 0) {
  9542. flag = ret;
  9543. }
  9544. printf(testingFmt, "wc_Sha384Update()");
  9545. /* Input */
  9546. if (!flag) {
  9547. a.input = "a";
  9548. a.inLen = XSTRLEN(a.input);
  9549. ret = wc_Sha384Update(&sha384, NULL, 0);
  9550. if (ret != 0) {
  9551. flag = ret;
  9552. }
  9553. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  9554. if (ret != 0) {
  9555. flag = ret;
  9556. }
  9557. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  9558. if (ret != 0) {
  9559. flag = ret;
  9560. }
  9561. }
  9562. if (!flag) {
  9563. ret = wc_Sha384Final(&sha384, hash);
  9564. if (ret != 0) {
  9565. flag = ret;
  9566. }
  9567. }
  9568. /* Update input. */
  9569. if (!flag) {
  9570. a.input = "abc";
  9571. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  9572. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  9573. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  9574. "\xc8\x25\xa7";
  9575. a.inLen = XSTRLEN(a.input);
  9576. a.outLen = XSTRLEN(a.output);
  9577. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  9578. if (ret != 0) {
  9579. flag = ret;
  9580. }
  9581. }
  9582. if (!flag) {
  9583. ret = wc_Sha384Final(&sha384, hash);
  9584. if (ret != 0) {
  9585. flag = ret;
  9586. }
  9587. }
  9588. if (!flag) {
  9589. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  9590. flag = WOLFSSL_FATAL_ERROR;
  9591. }
  9592. }
  9593. /* Pass in bad values. */
  9594. if (!flag) {
  9595. b.input = NULL;
  9596. b.inLen = 0;
  9597. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  9598. if (ret != 0) {
  9599. flag = ret;
  9600. }
  9601. }
  9602. if (!flag) {
  9603. c.input = NULL;
  9604. c.inLen = WC_SHA384_DIGEST_SIZE;
  9605. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  9606. if (ret != BAD_FUNC_ARG) {
  9607. flag = WOLFSSL_FATAL_ERROR;
  9608. }
  9609. }
  9610. if (!flag) {
  9611. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  9612. if (ret != BAD_FUNC_ARG) {
  9613. flag = WOLFSSL_FATAL_ERROR;
  9614. }
  9615. }
  9616. wc_Sha384Free(&sha384);
  9617. /* If not returned then the unit test passed test vectors. */
  9618. printf(resultFmt, flag == 0 ? passed : failed);
  9619. #endif
  9620. return flag;
  9621. } /* END test_wc_Sha384Update */
  9622. /*
  9623. * Unit test function for wc_Sha384Final();
  9624. */
  9625. static int test_wc_Sha384Final (void)
  9626. {
  9627. int flag = 0;
  9628. #ifdef WOLFSSL_SHA384
  9629. wc_Sha384 sha384;
  9630. byte* hash_test[3];
  9631. byte hash1[WC_SHA384_DIGEST_SIZE];
  9632. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  9633. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  9634. int times, i, ret;
  9635. /* Initialize */
  9636. ret = wc_InitSha384(&sha384);
  9637. if (ret) {
  9638. flag = ret;
  9639. }
  9640. if (!flag) {
  9641. hash_test[0] = hash1;
  9642. hash_test[1] = hash2;
  9643. hash_test[2] = hash3;
  9644. }
  9645. times = sizeof(hash_test) / sizeof(byte*);
  9646. /* Good test args. */
  9647. printf(testingFmt, "wc_Sha384Final()");
  9648. for (i = 0; i < times; i++) {
  9649. if (!flag) {
  9650. ret = wc_Sha384Final(&sha384, hash_test[i]);
  9651. if (ret != 0) {
  9652. flag = WOLFSSL_FATAL_ERROR;
  9653. }
  9654. }
  9655. }
  9656. /* Test bad args. */
  9657. if (!flag) {
  9658. ret = wc_Sha384Final(NULL, NULL);
  9659. if (ret != BAD_FUNC_ARG) {
  9660. flag = WOLFSSL_FATAL_ERROR;
  9661. }
  9662. }
  9663. if (!flag) {
  9664. ret = wc_Sha384Final(NULL, hash1);
  9665. if (ret != BAD_FUNC_ARG) {
  9666. flag = WOLFSSL_FATAL_ERROR;
  9667. }
  9668. }
  9669. if (!flag) {
  9670. ret = wc_Sha384Final(&sha384, NULL);
  9671. if (ret != BAD_FUNC_ARG) {
  9672. flag = WOLFSSL_FATAL_ERROR;
  9673. }
  9674. }
  9675. wc_Sha384Free(&sha384);
  9676. printf(resultFmt, flag == 0 ? passed : failed);
  9677. #endif
  9678. return flag;
  9679. } /* END test_wc_Sha384Final */
  9680. /*
  9681. * Unit test function for wc_Sha384GetFlags()
  9682. */
  9683. static int test_wc_Sha384GetFlags (void)
  9684. {
  9685. int flag = 0;
  9686. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  9687. wc_Sha384 sha384;
  9688. word32 flags = 0;
  9689. printf(testingFmt, "wc_Sha384GetFlags()");
  9690. /* Initialize */
  9691. flag = wc_InitSha384(&sha384);
  9692. if (flag == 0) {
  9693. flag = wc_Sha384GetFlags(&sha384, &flags);
  9694. }
  9695. if (flag == 0) {
  9696. if (flags & WC_HASH_FLAG_ISCOPY) {
  9697. flag = 0;
  9698. }
  9699. }
  9700. wc_Sha384Free(&sha384);
  9701. printf(resultFmt, flag == 0 ? passed : failed);
  9702. #endif
  9703. return flag;
  9704. } /* END test_wc_Sha384GetFlags */
  9705. /*
  9706. * Unit test function for wc_Sha384FinalRaw()
  9707. */
  9708. static int test_wc_Sha384FinalRaw (void)
  9709. {
  9710. int flag = 0;
  9711. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  9712. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  9713. !defined(WOLFSSL_NO_HASH_RAW)
  9714. wc_Sha384 sha384;
  9715. byte* hash_test[3];
  9716. byte hash1[WC_SHA384_DIGEST_SIZE];
  9717. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  9718. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  9719. int times, i, ret;
  9720. /* Initialize */
  9721. ret = wc_InitSha384(&sha384);
  9722. if (ret != 0) {
  9723. flag = ret;
  9724. }
  9725. if (!flag) {
  9726. hash_test[0] = hash1;
  9727. hash_test[1] = hash2;
  9728. hash_test[2] = hash3;
  9729. }
  9730. times = sizeof(hash_test) / sizeof(byte*);
  9731. /* Good test args. */
  9732. printf(testingFmt, "wc_Sha384FinalRaw()");
  9733. for (i = 0; i < times; i++) {
  9734. if (!flag) {
  9735. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  9736. if (ret != 0) {
  9737. flag = WOLFSSL_FATAL_ERROR;
  9738. }
  9739. }
  9740. }
  9741. /* Test bad args. */
  9742. if (!flag ) {
  9743. ret = wc_Sha384FinalRaw(NULL, NULL);
  9744. if (ret != BAD_FUNC_ARG) {
  9745. flag = WOLFSSL_FATAL_ERROR;
  9746. }
  9747. }
  9748. if (!flag) {
  9749. ret = wc_Sha384FinalRaw(NULL, hash1);
  9750. if (ret != BAD_FUNC_ARG) {
  9751. flag = WOLFSSL_FATAL_ERROR;
  9752. }
  9753. }
  9754. if (!flag) {
  9755. ret = wc_Sha384FinalRaw(&sha384, NULL);
  9756. if (ret != BAD_FUNC_ARG) {
  9757. flag = WOLFSSL_FATAL_ERROR;
  9758. }
  9759. }
  9760. wc_Sha384Free(&sha384);
  9761. printf(resultFmt, flag == 0 ? passed : failed);
  9762. #endif
  9763. return flag;
  9764. } /* END test_wc_Sha384FinalRaw */
  9765. /*
  9766. * Unit test function for wc_Sha384Free()
  9767. */
  9768. static int test_wc_Sha384Free (void)
  9769. {
  9770. int flag = 0;
  9771. #ifdef WOLFSSL_SHA384
  9772. printf(testingFmt, "wc_Sha384Free()");
  9773. wc_Sha384Free(NULL);
  9774. printf(resultFmt, flag == 0 ? passed : failed);
  9775. #endif
  9776. return flag;
  9777. } /* END test_wc_Sha384Free */
  9778. /*
  9779. * Unit test function for wc_Sha384GetHash()
  9780. */
  9781. static int test_wc_Sha384GetHash (void)
  9782. {
  9783. int flag = 0;
  9784. #ifdef WOLFSSL_SHA384
  9785. wc_Sha384 sha384;
  9786. byte hash1[WC_SHA384_DIGEST_SIZE];
  9787. printf(testingFmt, "wc_Sha384GetHash()");
  9788. /* Initialize */
  9789. flag = wc_InitSha384(&sha384);
  9790. if (flag == 0) {
  9791. flag = wc_Sha384GetHash(&sha384, hash1);
  9792. }
  9793. /*test bad arguements*/
  9794. if (flag == 0) {
  9795. flag = wc_Sha384GetHash(NULL, NULL);
  9796. if (flag == BAD_FUNC_ARG) {
  9797. flag = 0;
  9798. }
  9799. }
  9800. if (flag == 0) {
  9801. flag = wc_Sha384GetHash(NULL, hash1);
  9802. if (flag == BAD_FUNC_ARG) {
  9803. flag = 0;
  9804. }
  9805. }
  9806. if (flag == 0) {
  9807. flag = wc_Sha384GetHash(&sha384, NULL);
  9808. if (flag == BAD_FUNC_ARG) {
  9809. flag = 0;
  9810. }
  9811. }
  9812. wc_Sha384Free(&sha384);
  9813. printf(resultFmt, flag == 0 ? passed : failed);
  9814. #endif
  9815. return flag;
  9816. } /* END test_wc_Sha384GetHash */
  9817. /*
  9818. * Unit test function for wc_Sha384Copy()
  9819. */
  9820. static int test_wc_Sha384Copy (void)
  9821. {
  9822. int flag = 0;
  9823. #ifdef WOLFSSL_SHA384
  9824. wc_Sha384 sha384;
  9825. wc_Sha384 temp;
  9826. printf(testingFmt, "wc_Sha384Copy()");
  9827. /* Initialize */
  9828. flag = wc_InitSha384(&sha384);
  9829. if (flag == 0) {
  9830. flag = wc_InitSha384(&temp);
  9831. }
  9832. if (flag == 0) {
  9833. flag = wc_Sha384Copy(&sha384, &temp);
  9834. }
  9835. /*test bad arguements*/
  9836. if (flag == 0) {
  9837. flag = wc_Sha384Copy(NULL, NULL);
  9838. if (flag == BAD_FUNC_ARG) {
  9839. flag = 0;
  9840. }
  9841. }
  9842. if (flag == 0) {
  9843. flag = wc_Sha384Copy(NULL, &temp);
  9844. if (flag == BAD_FUNC_ARG) {
  9845. flag = 0;
  9846. }
  9847. }
  9848. if (flag == 0) {
  9849. flag = wc_Sha384Copy(&sha384, NULL);
  9850. if (flag == BAD_FUNC_ARG) {
  9851. flag = 0;
  9852. }
  9853. }
  9854. wc_Sha384Free(&sha384);
  9855. wc_Sha384Free(&temp);
  9856. printf(resultFmt, flag == 0 ? passed : failed);
  9857. #endif
  9858. return flag;
  9859. } /* END test_wc_Sha384Copy */
  9860. /*
  9861. * Testing wc_InitSha224();
  9862. */
  9863. static int test_wc_InitSha224 (void)
  9864. {
  9865. int flag = 0;
  9866. #ifdef WOLFSSL_SHA224
  9867. wc_Sha224 sha224;
  9868. int ret;
  9869. printf(testingFmt, "wc_InitSha224()");
  9870. /* Test good arg. */
  9871. ret = wc_InitSha224(&sha224);
  9872. if (ret != 0) {
  9873. flag = WOLFSSL_FATAL_ERROR;
  9874. }
  9875. /* Test bad arg. */
  9876. if (!flag) {
  9877. ret = wc_InitSha224(NULL);
  9878. if (ret != BAD_FUNC_ARG) {
  9879. flag = WOLFSSL_FATAL_ERROR;
  9880. }
  9881. }
  9882. wc_Sha224Free(&sha224);
  9883. printf(resultFmt, flag == 0 ? passed : failed);
  9884. #endif
  9885. return flag;
  9886. } /* END test_wc_InitSha224 */
  9887. /*
  9888. * Unit test on wc_Sha224Update
  9889. */
  9890. static int test_wc_Sha224Update (void)
  9891. {
  9892. int flag = 0;
  9893. #ifdef WOLFSSL_SHA224
  9894. wc_Sha224 sha224;
  9895. byte hash[WC_SHA224_DIGEST_SIZE];
  9896. testVector a, b, c;
  9897. int ret;
  9898. ret = wc_InitSha224(&sha224);
  9899. if (ret != 0) {
  9900. flag = ret;
  9901. }
  9902. printf(testingFmt, "wc_Sha224Update()");
  9903. /* Input. */
  9904. if (!flag) {
  9905. a.input = "a";
  9906. a.inLen = XSTRLEN(a.input);
  9907. ret = wc_Sha224Update(&sha224, NULL, 0);
  9908. if (ret != 0) {
  9909. flag = ret;
  9910. }
  9911. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  9912. if (ret != 0) {
  9913. flag = ret;
  9914. }
  9915. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  9916. if (ret != 0) {
  9917. flag = ret;
  9918. }
  9919. }
  9920. if (!flag) {
  9921. ret = wc_Sha224Final(&sha224, hash);
  9922. if (ret != 0) {
  9923. flag = ret;
  9924. }
  9925. }
  9926. /* Update input. */
  9927. if (!flag) {
  9928. a.input = "abc";
  9929. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  9930. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  9931. a.inLen = XSTRLEN(a.input);
  9932. a.outLen = XSTRLEN(a.output);
  9933. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  9934. if (ret != 0) {
  9935. flag = ret;
  9936. }
  9937. }
  9938. if (!flag) {
  9939. ret = wc_Sha224Final(&sha224, hash);
  9940. if (ret != 0) {
  9941. flag = ret;
  9942. }
  9943. }
  9944. if (!flag) {
  9945. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  9946. flag = WOLFSSL_FATAL_ERROR;
  9947. }
  9948. }
  9949. /* Pass in bad values. */
  9950. if (!flag) {
  9951. b.input = NULL;
  9952. b.inLen = 0;
  9953. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  9954. if (ret != 0) {
  9955. flag = ret;
  9956. }
  9957. }
  9958. if (!flag) {
  9959. c.input = NULL;
  9960. c.inLen = WC_SHA224_DIGEST_SIZE;
  9961. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  9962. if (ret != BAD_FUNC_ARG) {
  9963. flag = WOLFSSL_FATAL_ERROR;
  9964. }
  9965. }
  9966. if (!flag) {
  9967. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  9968. if (ret != BAD_FUNC_ARG) {
  9969. flag = WOLFSSL_FATAL_ERROR;
  9970. }
  9971. }
  9972. wc_Sha224Free(&sha224);
  9973. /* If not returned then the unit test passed test vectors. */
  9974. printf(resultFmt, flag == 0 ? passed : failed);
  9975. #endif
  9976. return flag;
  9977. } /* END test_wc_Sha224Update */
  9978. /*
  9979. * Unit test for wc_Sha224Final();
  9980. */
  9981. static int test_wc_Sha224Final (void)
  9982. {
  9983. int flag = 0;
  9984. #ifdef WOLFSSL_SHA224
  9985. wc_Sha224 sha224;
  9986. byte* hash_test[3];
  9987. byte hash1[WC_SHA224_DIGEST_SIZE];
  9988. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  9989. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  9990. int times, i, ret;
  9991. /* Initialize */
  9992. ret = wc_InitSha224(&sha224);
  9993. if (ret) {
  9994. flag = ret;
  9995. }
  9996. if (!flag) {
  9997. hash_test[0] = hash1;
  9998. hash_test[1] = hash2;
  9999. hash_test[2] = hash3;
  10000. }
  10001. times = sizeof(hash_test) / sizeof(byte*);
  10002. /* Good test args. */
  10003. printf(testingFmt, "wc_sha224Final()");
  10004. /* Testing oversized buffers. */
  10005. for (i = 0; i < times; i++) {
  10006. if (!flag) {
  10007. ret = wc_Sha224Final(&sha224, hash_test[i]);
  10008. if (ret != 0) {
  10009. flag = WOLFSSL_FATAL_ERROR;
  10010. }
  10011. }
  10012. }
  10013. /* Test bad args. */
  10014. if (!flag) {
  10015. ret = wc_Sha224Final(NULL, NULL);
  10016. if (ret != BAD_FUNC_ARG) {
  10017. flag = WOLFSSL_FATAL_ERROR;
  10018. }
  10019. }
  10020. if (!flag) {
  10021. ret = wc_Sha224Final(NULL, hash1);
  10022. if (ret != BAD_FUNC_ARG) {
  10023. flag = WOLFSSL_FATAL_ERROR;
  10024. }
  10025. }
  10026. if (!flag) {
  10027. ret = wc_Sha224Final(&sha224, NULL);
  10028. if (ret != BAD_FUNC_ARG) {
  10029. flag = WOLFSSL_FATAL_ERROR;
  10030. }
  10031. }
  10032. wc_Sha224Free(&sha224);
  10033. printf(resultFmt, flag == 0 ? passed : failed);
  10034. #endif
  10035. return flag;
  10036. } /* END test_wc_Sha224Final */
  10037. /*
  10038. * Unit test function for wc_Sha224SetFlags()
  10039. */
  10040. static int test_wc_Sha224SetFlags (void)
  10041. {
  10042. int flag = 0;
  10043. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  10044. wc_Sha224 sha224;
  10045. word32 flags = 0;
  10046. printf(testingFmt, "wc_Sha224SetFlags()");
  10047. /* Initialize */
  10048. flag = wc_InitSha224(&sha224);
  10049. if (flag == 0) {
  10050. flag = wc_Sha224SetFlags(&sha224, flags);
  10051. }
  10052. if (flag == 0) {
  10053. if (flags & WC_HASH_FLAG_ISCOPY) {
  10054. flag = 0;
  10055. }
  10056. }
  10057. wc_Sha224Free(&sha224);
  10058. printf(resultFmt, flag == 0 ? passed : failed);
  10059. #endif
  10060. return flag;
  10061. } /* END test_wc_Sha224SetFlags */
  10062. /*
  10063. * Unit test function for wc_Sha224GetFlags()
  10064. */
  10065. static int test_wc_Sha224GetFlags (void)
  10066. {
  10067. int flag = 0;
  10068. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  10069. wc_Sha224 sha224;
  10070. word32 flags = 0;
  10071. printf(testingFmt, "wc_Sha224GetFlags()");
  10072. /* Initialize */
  10073. flag = wc_InitSha224(&sha224);
  10074. if (flag == 0) {
  10075. flag = wc_Sha224GetFlags(&sha224, &flags);
  10076. }
  10077. if (flag == 0) {
  10078. if (flags & WC_HASH_FLAG_ISCOPY) {
  10079. flag = 0;
  10080. }
  10081. }
  10082. wc_Sha224Free(&sha224);
  10083. printf(resultFmt, flag == 0 ? passed : failed);
  10084. #endif
  10085. return flag;
  10086. } /* END test_wc_Sha224GetFlags */
  10087. /*
  10088. * Unit test function for wc_Sha224Free()
  10089. */
  10090. static int test_wc_Sha224Free (void)
  10091. {
  10092. int flag = 0;
  10093. #ifdef WOLFSSL_SHA224
  10094. printf(testingFmt, "wc_Sha224Free()");
  10095. wc_Sha224Free(NULL);
  10096. printf(resultFmt, flag == 0 ? passed : failed);
  10097. #endif
  10098. return flag;
  10099. } /* END test_wc_Sha224Free */
  10100. /*
  10101. * Unit test function for wc_Sha224GetHash()
  10102. */
  10103. static int test_wc_Sha224GetHash (void)
  10104. {
  10105. int flag = 0;
  10106. #ifdef WOLFSSL_SHA224
  10107. wc_Sha224 sha224;
  10108. byte hash1[WC_SHA224_DIGEST_SIZE];
  10109. printf(testingFmt, "wc_Sha224GetHash()");
  10110. /* Initialize */
  10111. flag = wc_InitSha224(&sha224);
  10112. if (flag == 0) {
  10113. flag = wc_Sha224GetHash(&sha224, hash1);
  10114. }
  10115. /*test bad arguements*/
  10116. if (flag == 0) {
  10117. flag = wc_Sha224GetHash(NULL, NULL);
  10118. if (flag == BAD_FUNC_ARG) {
  10119. flag = 0;
  10120. }
  10121. }
  10122. if (flag == 0) {
  10123. flag = wc_Sha224GetHash(NULL, hash1);
  10124. if (flag == BAD_FUNC_ARG) {
  10125. flag = 0;
  10126. }
  10127. }
  10128. if (flag == 0) {
  10129. flag = wc_Sha224GetHash(&sha224, NULL);
  10130. if (flag == BAD_FUNC_ARG) {
  10131. flag = 0;
  10132. }
  10133. }
  10134. wc_Sha224Free(&sha224);
  10135. printf(resultFmt, flag == 0 ? passed : failed);
  10136. #endif
  10137. return flag;
  10138. } /* END test_wc_Sha224GetHash */
  10139. /*
  10140. * Unit test function for wc_Sha224Copy()
  10141. */
  10142. static int test_wc_Sha224Copy (void)
  10143. {
  10144. int flag = 0;
  10145. #ifdef WOLFSSL_SHA224
  10146. wc_Sha224 sha224;
  10147. wc_Sha224 temp;
  10148. printf(testingFmt, "wc_Sha224Copy()");
  10149. /* Initialize */
  10150. flag = wc_InitSha224(&sha224);
  10151. if (flag == 0) {
  10152. flag = wc_InitSha224(&temp);
  10153. }
  10154. if (flag == 0) {
  10155. flag = wc_Sha224Copy(&sha224, &temp);
  10156. }
  10157. /*test bad arguements*/
  10158. if (flag == 0) {
  10159. flag = wc_Sha224Copy(NULL, NULL);
  10160. if (flag == BAD_FUNC_ARG) {
  10161. flag = 0;
  10162. }
  10163. }
  10164. if (flag == 0) {
  10165. flag = wc_Sha224Copy(NULL, &temp);
  10166. if (flag == BAD_FUNC_ARG) {
  10167. flag = 0;
  10168. }
  10169. }
  10170. if (flag == 0) {
  10171. flag = wc_Sha224Copy(&sha224, NULL);
  10172. if (flag == BAD_FUNC_ARG) {
  10173. flag = 0;
  10174. }
  10175. }
  10176. wc_Sha224Free(&sha224);
  10177. wc_Sha224Free(&temp);
  10178. printf(resultFmt, flag == 0 ? passed : failed);
  10179. #endif
  10180. return flag;
  10181. } /* END test_wc_Sha224Copy */
  10182. /*
  10183. * Testing wc_InitRipeMd()
  10184. */
  10185. static int test_wc_InitRipeMd (void)
  10186. {
  10187. int flag = 0;
  10188. #ifdef WOLFSSL_RIPEMD
  10189. RipeMd ripemd;
  10190. int ret;
  10191. printf(testingFmt, "wc_InitRipeMd()");
  10192. /* Test good arg. */
  10193. ret = wc_InitRipeMd(&ripemd);
  10194. if (ret != 0) {
  10195. flag = WOLFSSL_FATAL_ERROR;
  10196. }
  10197. /* Test bad arg. */
  10198. if (!flag) {
  10199. ret = wc_InitRipeMd(NULL);
  10200. if (ret != BAD_FUNC_ARG) {
  10201. flag = WOLFSSL_FATAL_ERROR;
  10202. }
  10203. }
  10204. printf(resultFmt, flag == 0 ? passed : failed);
  10205. #endif
  10206. return flag;
  10207. } /* END test_wc_InitRipeMd */
  10208. /*
  10209. * Testing wc_RipeMdUpdate()
  10210. */
  10211. static int test_wc_RipeMdUpdate (void)
  10212. {
  10213. int flag = 0;
  10214. #ifdef WOLFSSL_RIPEMD
  10215. RipeMd ripemd;
  10216. byte hash[RIPEMD_DIGEST_SIZE];
  10217. testVector a, b, c;
  10218. int ret;
  10219. ret = wc_InitRipeMd(&ripemd);
  10220. if (ret != 0) {
  10221. flag = ret;
  10222. }
  10223. printf(testingFmt, "wc_RipeMdUpdate()");
  10224. /* Input */
  10225. if (!flag) {
  10226. a.input = "a";
  10227. a.inLen = XSTRLEN(a.input);
  10228. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  10229. if (ret != 0) {
  10230. flag = ret;
  10231. }
  10232. }
  10233. if (!flag) {
  10234. ret = wc_RipeMdFinal(&ripemd, hash);
  10235. if (ret != 0) {
  10236. flag = ret;
  10237. }
  10238. }
  10239. /* Update input. */
  10240. if (!flag) {
  10241. a.input = "abc";
  10242. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  10243. "\xb0\x87\xf1\x5a\x0b\xfc";
  10244. a.inLen = XSTRLEN(a.input);
  10245. a.outLen = XSTRLEN(a.output);
  10246. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  10247. if (ret != 0) {
  10248. flag = ret;
  10249. }
  10250. }
  10251. if (!flag) {
  10252. ret = wc_RipeMdFinal(&ripemd, hash);
  10253. if (ret != 0) {
  10254. flag = ret;
  10255. }
  10256. }
  10257. if (!flag) {
  10258. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  10259. flag = WOLFSSL_FATAL_ERROR;
  10260. }
  10261. }
  10262. /* Pass in bad values. */
  10263. if (!flag) {
  10264. b.input = NULL;
  10265. b.inLen = 0;
  10266. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  10267. if (ret != 0) {
  10268. flag = ret;
  10269. }
  10270. }
  10271. if (!flag) {
  10272. c.input = NULL;
  10273. c.inLen = RIPEMD_DIGEST_SIZE;
  10274. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  10275. if (ret != BAD_FUNC_ARG) {
  10276. flag = WOLFSSL_FATAL_ERROR;
  10277. }
  10278. }
  10279. if (!flag) {
  10280. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10281. if (ret != BAD_FUNC_ARG) {
  10282. flag = WOLFSSL_FATAL_ERROR;
  10283. }
  10284. }
  10285. printf(resultFmt, flag == 0 ? passed : failed);
  10286. #endif
  10287. return flag;
  10288. } /* END test_wc_RipeMdUdpate */
  10289. /*
  10290. * Unit test function for wc_RipeMdFinal()
  10291. */
  10292. static int test_wc_RipeMdFinal (void)
  10293. {
  10294. int flag = 0;
  10295. #ifdef WOLFSSL_RIPEMD
  10296. RipeMd ripemd;
  10297. byte* hash_test[3];
  10298. byte hash1[RIPEMD_DIGEST_SIZE];
  10299. byte hash2[2*RIPEMD_DIGEST_SIZE];
  10300. byte hash3[5*RIPEMD_DIGEST_SIZE];
  10301. int times, i, ret;
  10302. /* Initialize */
  10303. ret = wc_InitRipeMd(&ripemd);
  10304. if (ret != 0) {
  10305. flag = ret;
  10306. }
  10307. if (!flag) {
  10308. hash_test[0] = hash1;
  10309. hash_test[1] = hash2;
  10310. hash_test[2] = hash3;
  10311. }
  10312. times = sizeof(hash_test) / sizeof(byte*);
  10313. /* Good test args. */
  10314. printf(testingFmt, "wc_RipeMdFinal()");
  10315. /* Testing oversized buffers. */
  10316. for (i = 0; i < times; i++) {
  10317. if (!flag) {
  10318. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  10319. if (ret != 0) {
  10320. flag = WOLFSSL_FATAL_ERROR;
  10321. }
  10322. }
  10323. }
  10324. /* Test bad args. */
  10325. if (!flag) {
  10326. ret = wc_RipeMdFinal(NULL, NULL);
  10327. if (ret != BAD_FUNC_ARG) {
  10328. flag = WOLFSSL_FATAL_ERROR;
  10329. }
  10330. }
  10331. if (!flag) {
  10332. ret = wc_RipeMdFinal(NULL, hash1);
  10333. if (ret != BAD_FUNC_ARG) {
  10334. flag = WOLFSSL_FATAL_ERROR;
  10335. }
  10336. }
  10337. if (!flag) {
  10338. ret = wc_RipeMdFinal(&ripemd, NULL);
  10339. if (ret != BAD_FUNC_ARG) {
  10340. flag = WOLFSSL_FATAL_ERROR;
  10341. }
  10342. }
  10343. printf(resultFmt, flag == 0 ? passed : failed);
  10344. #endif
  10345. return flag;
  10346. } /* END test_wc_RipeMdFinal */
  10347. /*
  10348. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  10349. * wc_InitSha3_512
  10350. */
  10351. static int test_wc_InitSha3 (void)
  10352. {
  10353. int ret = 0;
  10354. #if defined(WOLFSSL_SHA3)
  10355. wc_Sha3 sha3;
  10356. (void)sha3;
  10357. #if !defined(WOLFSSL_NOSHA3_224)
  10358. printf(testingFmt, "wc_InitSha3_224()");
  10359. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10360. /* Test bad args. */
  10361. if (ret == 0) {
  10362. ret = wc_InitSha3_224(NULL, HEAP_HINT, devId);
  10363. if (ret == BAD_FUNC_ARG) {
  10364. ret = 0;
  10365. } else if (ret == 0) {
  10366. ret = WOLFSSL_FATAL_ERROR;
  10367. }
  10368. }
  10369. wc_Sha3_224_Free(&sha3);
  10370. printf(resultFmt, ret == 0 ? passed : failed);
  10371. #endif /* NOSHA3_224 */
  10372. #if !defined(WOLFSSL_NOSHA3_256)
  10373. if (ret == 0) {
  10374. printf(testingFmt, "wc_InitSha3_256()");
  10375. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  10376. /* Test bad args. */
  10377. if (ret == 0) {
  10378. ret = wc_InitSha3_256(NULL, HEAP_HINT, devId);
  10379. if (ret == BAD_FUNC_ARG) {
  10380. ret = 0;
  10381. } else if (ret == 0) {
  10382. ret = WOLFSSL_FATAL_ERROR;
  10383. }
  10384. }
  10385. wc_Sha3_256_Free(&sha3);
  10386. printf(resultFmt, ret == 0 ? passed : failed);
  10387. } /* END sha3_256 */
  10388. #endif /* NOSHA3_256 */
  10389. #if !defined(WOLFSSL_NOSHA3_384)
  10390. if (ret == 0) {
  10391. printf(testingFmt, "wc_InitSha3_384()");
  10392. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  10393. /* Test bad args. */
  10394. if (ret == 0) {
  10395. ret = wc_InitSha3_384(NULL, HEAP_HINT, devId);
  10396. if (ret == BAD_FUNC_ARG) {
  10397. ret = 0;
  10398. } else if (ret == 0) {
  10399. ret = WOLFSSL_FATAL_ERROR;
  10400. }
  10401. }
  10402. wc_Sha3_384_Free(&sha3);
  10403. printf(resultFmt, ret == 0 ? passed : failed);
  10404. } /* END sha3_384 */
  10405. #endif /* NOSHA3_384 */
  10406. #if !defined(WOLFSSL_NOSHA3_512)
  10407. if (ret == 0) {
  10408. printf(testingFmt, "wc_InitSha3_512()");
  10409. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  10410. /* Test bad args. */
  10411. if (ret == 0) {
  10412. ret = wc_InitSha3_512(NULL, HEAP_HINT, devId);
  10413. if (ret == BAD_FUNC_ARG) {
  10414. ret = 0;
  10415. } else if (ret == 0) {
  10416. ret = WOLFSSL_FATAL_ERROR;
  10417. }
  10418. }
  10419. wc_Sha3_512_Free(&sha3);
  10420. printf(resultFmt, ret == 0 ? passed : failed);
  10421. } /* END sha3_512 */
  10422. #endif /* NOSHA3_512 */
  10423. #endif
  10424. return ret;
  10425. } /* END test_wc_InitSha3 */
  10426. /*
  10427. * Testing wc_Sha3_Update()
  10428. */
  10429. static int testing_wc_Sha3_Update (void)
  10430. {
  10431. int ret = 0;
  10432. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  10433. !defined(WOLFSSL_AFALG_XILINX)
  10434. wc_Sha3 sha3;
  10435. byte msg[] = "Everybody's working for the weekend.";
  10436. byte msg2[] = "Everybody gets Friday off.";
  10437. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  10438. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  10439. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  10440. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  10441. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  10442. word32 msglen = sizeof(msg) - 1;
  10443. word32 msg2len = sizeof(msg2);
  10444. word32 msgCmplen = sizeof(msgCmp);
  10445. #if !defined(WOLFSSL_NOSHA3_224)
  10446. printf(testingFmt, "wc_Sha3_224_Update()");
  10447. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10448. if (ret != 0) {
  10449. return ret;
  10450. }
  10451. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  10452. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  10453. ret = WOLFSSL_FATAL_ERROR;
  10454. }
  10455. if (ret == 0) {
  10456. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  10457. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  10458. ret = WOLFSSL_FATAL_ERROR;
  10459. }
  10460. }
  10461. /* Pass bad args. */
  10462. if (ret == 0) {
  10463. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  10464. if (ret == BAD_FUNC_ARG) {
  10465. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  10466. }
  10467. if (ret == BAD_FUNC_ARG) {
  10468. wc_Sha3_224_Free(&sha3);
  10469. if (wc_InitSha3_224(&sha3, HEAP_HINT, devId)) {
  10470. return ret;
  10471. }
  10472. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  10473. if (ret == 0) {
  10474. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  10475. }
  10476. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  10477. ret = WOLFSSL_FATAL_ERROR;
  10478. }
  10479. }
  10480. }
  10481. wc_Sha3_224_Free(&sha3);
  10482. printf(resultFmt, ret == 0 ? passed : failed);
  10483. #endif /* SHA3_224 */
  10484. #if !defined(WOLFSSL_NOSHA3_256)
  10485. if (ret == 0) {
  10486. printf(testingFmt, "wc_Sha3_256_Update()");
  10487. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  10488. if (ret != 0) {
  10489. return ret;
  10490. }
  10491. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  10492. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  10493. ret = WOLFSSL_FATAL_ERROR;
  10494. }
  10495. if (ret == 0) {
  10496. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  10497. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  10498. ret = WOLFSSL_FATAL_ERROR;
  10499. }
  10500. }
  10501. /* Pass bad args. */
  10502. if (ret == 0) {
  10503. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  10504. if (ret == BAD_FUNC_ARG) {
  10505. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  10506. }
  10507. if (ret == BAD_FUNC_ARG) {
  10508. wc_Sha3_256_Free(&sha3);
  10509. if (wc_InitSha3_256(&sha3, HEAP_HINT, devId)) {
  10510. return ret;
  10511. }
  10512. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  10513. if (ret == 0) {
  10514. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  10515. }
  10516. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  10517. ret = WOLFSSL_FATAL_ERROR;
  10518. }
  10519. }
  10520. }
  10521. wc_Sha3_256_Free(&sha3);
  10522. printf(resultFmt, ret == 0 ? passed : failed);
  10523. }
  10524. #endif /* SHA3_256 */
  10525. #if !defined(WOLFSSL_NOSHA3_384)
  10526. if (ret == 0) {
  10527. printf(testingFmt, "wc_Sha3_384_Update()");
  10528. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  10529. if (ret != 0) {
  10530. return ret;
  10531. }
  10532. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  10533. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  10534. ret = WOLFSSL_FATAL_ERROR;
  10535. }
  10536. if (ret == 0) {
  10537. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  10538. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  10539. ret = WOLFSSL_FATAL_ERROR;
  10540. }
  10541. }
  10542. /* Pass bad args. */
  10543. if (ret == 0) {
  10544. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  10545. if (ret == BAD_FUNC_ARG) {
  10546. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  10547. }
  10548. if (ret == BAD_FUNC_ARG) {
  10549. wc_Sha3_384_Free(&sha3);
  10550. if (wc_InitSha3_384(&sha3, HEAP_HINT, devId)) {
  10551. return ret;
  10552. }
  10553. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  10554. if (ret == 0) {
  10555. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  10556. }
  10557. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  10558. ret = WOLFSSL_FATAL_ERROR;
  10559. }
  10560. }
  10561. }
  10562. wc_Sha3_384_Free(&sha3);
  10563. printf(resultFmt, ret == 0 ? passed : failed);
  10564. }
  10565. #endif /* SHA3_384 */
  10566. #if !defined(WOLFSSL_NOSHA3_512)
  10567. if (ret == 0) {
  10568. printf(testingFmt, "wc_Sha3_512_Update()");
  10569. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  10570. if (ret != 0) {
  10571. return ret;
  10572. }
  10573. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  10574. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  10575. ret = WOLFSSL_FATAL_ERROR;
  10576. }
  10577. if (ret == 0) {
  10578. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  10579. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  10580. ret = WOLFSSL_FATAL_ERROR;
  10581. }
  10582. }
  10583. /* Pass bad args. */
  10584. if (ret == 0) {
  10585. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  10586. if (ret == BAD_FUNC_ARG) {
  10587. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  10588. }
  10589. if (ret == BAD_FUNC_ARG) {
  10590. wc_Sha3_512_Free(&sha3);
  10591. if (wc_InitSha3_512(&sha3, HEAP_HINT, devId)) {
  10592. return ret;
  10593. }
  10594. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  10595. if (ret == 0) {
  10596. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  10597. }
  10598. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  10599. ret = WOLFSSL_FATAL_ERROR;
  10600. }
  10601. }
  10602. }
  10603. wc_Sha3_512_Free(&sha3);
  10604. printf(resultFmt, ret == 0 ? passed : failed);
  10605. }
  10606. #endif /* SHA3_512 */
  10607. #endif /* WOLFSSL_SHA3 */
  10608. return ret;
  10609. } /* END testing_wc_Sha3_Update */
  10610. /*
  10611. * Testing wc_Sha3_224_Final()
  10612. */
  10613. static int test_wc_Sha3_224_Final (void)
  10614. {
  10615. int ret = 0;
  10616. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  10617. wc_Sha3 sha3;
  10618. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  10619. "nopnopq";
  10620. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  10621. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  10622. "\x64\xea\xd0\xfc\xce\x33";
  10623. byte hash[WC_SHA3_224_DIGEST_SIZE];
  10624. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  10625. /* Init stack variables. */
  10626. XMEMSET(hash, 0, sizeof(hash));
  10627. printf(testingFmt, "wc_Sha3_224_Final()");
  10628. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10629. if (ret != 0) {
  10630. return ret;
  10631. }
  10632. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10633. if (ret == 0) {
  10634. ret = wc_Sha3_224_Final(&sha3, hash);
  10635. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  10636. ret = WOLFSSL_FATAL_ERROR;
  10637. }
  10638. }
  10639. /* Test bad args. */
  10640. if (ret == 0) {
  10641. ret = wc_Sha3_224_Final(NULL, hash);
  10642. if (ret == 0) {
  10643. ret = wc_Sha3_224_Final(&sha3, NULL);
  10644. }
  10645. if (ret == BAD_FUNC_ARG) {
  10646. ret = 0;
  10647. } else if (ret == 0) {
  10648. ret = WOLFSSL_FATAL_ERROR;
  10649. }
  10650. }
  10651. wc_Sha3_224_Free(&sha3);
  10652. printf(resultFmt, ret == 0 ? passed : failed);
  10653. if (ret == 0) {
  10654. printf(testingFmt, "wc_Sha3_224_GetHash()");
  10655. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10656. if (ret != 0) {
  10657. return ret;
  10658. }
  10659. /* Init stack variables. */
  10660. XMEMSET(hash, 0, sizeof(hash));
  10661. XMEMSET(hashRet, 0, sizeof(hashRet));
  10662. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10663. if (ret == 0) {
  10664. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  10665. }
  10666. if (ret == 0) {
  10667. ret = wc_Sha3_224_Final(&sha3, hash);
  10668. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  10669. ret = WOLFSSL_FATAL_ERROR;
  10670. }
  10671. }
  10672. if (ret == 0) {
  10673. /* Test bad args. */
  10674. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  10675. if (ret == BAD_FUNC_ARG) {
  10676. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  10677. }
  10678. if (ret == BAD_FUNC_ARG) {
  10679. ret = 0;
  10680. } else if (ret == 0) {
  10681. ret = WOLFSSL_FATAL_ERROR;
  10682. }
  10683. }
  10684. printf(resultFmt, ret == 0 ? passed : failed);
  10685. }
  10686. wc_Sha3_224_Free(&sha3);
  10687. #endif
  10688. return ret;
  10689. } /* END test_wc_Sha3_224_Final */
  10690. /*
  10691. * Testing wc_Sha3_256_Final()
  10692. */
  10693. static int test_wc_Sha3_256_Final (void)
  10694. {
  10695. int ret = 0;
  10696. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  10697. wc_Sha3 sha3;
  10698. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  10699. "nopnopq";
  10700. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  10701. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  10702. "\xdd\x97\x49\x6d\x33\x76";
  10703. byte hash[WC_SHA3_256_DIGEST_SIZE];
  10704. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  10705. /* Init stack variables. */
  10706. XMEMSET(hash, 0, sizeof(hash));
  10707. printf(testingFmt, "wc_Sha3_256_Final()");
  10708. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  10709. if (ret != 0) {
  10710. return ret;
  10711. }
  10712. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10713. if (ret == 0) {
  10714. ret = wc_Sha3_256_Final(&sha3, hash);
  10715. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  10716. ret = WOLFSSL_FATAL_ERROR;
  10717. }
  10718. }
  10719. /* Test bad args. */
  10720. if (ret == 0) {
  10721. ret = wc_Sha3_256_Final(NULL, hash);
  10722. if (ret == 0) {
  10723. ret = wc_Sha3_256_Final(&sha3, NULL);
  10724. }
  10725. if (ret == BAD_FUNC_ARG) {
  10726. ret = 0;
  10727. } else if (ret == 0) {
  10728. ret = WOLFSSL_FATAL_ERROR;
  10729. }
  10730. }
  10731. wc_Sha3_256_Free(&sha3);
  10732. printf(resultFmt, ret == 0 ? passed : failed);
  10733. if (ret == 0) {
  10734. printf(testingFmt, "wc_Sha3_256_GetHash()");
  10735. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  10736. if (ret != 0) {
  10737. return ret;
  10738. }
  10739. /* Init stack variables. */
  10740. XMEMSET(hash, 0, sizeof(hash));
  10741. XMEMSET(hashRet, 0, sizeof(hashRet));
  10742. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10743. if (ret == 0) {
  10744. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  10745. }
  10746. if (ret == 0) {
  10747. ret = wc_Sha3_256_Final(&sha3, hash);
  10748. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  10749. ret = WOLFSSL_FATAL_ERROR;
  10750. }
  10751. }
  10752. if (ret == 0) {
  10753. /* Test bad args. */
  10754. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  10755. if (ret == BAD_FUNC_ARG) {
  10756. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  10757. }
  10758. if (ret == BAD_FUNC_ARG) {
  10759. ret = 0;
  10760. } else if (ret == 0) {
  10761. ret = WOLFSSL_FATAL_ERROR;
  10762. }
  10763. }
  10764. printf(resultFmt, ret == 0 ? passed : failed);
  10765. }
  10766. wc_Sha3_256_Free(&sha3);
  10767. #endif
  10768. return ret;
  10769. } /* END test_wc_Sha3_256_Final */
  10770. /*
  10771. * Testing wc_Sha3_384_Final()
  10772. */
  10773. static int test_wc_Sha3_384_Final (void)
  10774. {
  10775. int ret = 0;
  10776. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  10777. wc_Sha3 sha3;
  10778. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  10779. "nopnopq";
  10780. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  10781. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  10782. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  10783. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  10784. byte hash[WC_SHA3_384_DIGEST_SIZE];
  10785. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  10786. /* Init stack variables. */
  10787. XMEMSET(hash, 0, sizeof(hash));
  10788. printf(testingFmt, "wc_Sha3_384_Final()");
  10789. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  10790. if (ret != 0) {
  10791. return ret;
  10792. }
  10793. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10794. if (ret == 0) {
  10795. ret = wc_Sha3_384_Final(&sha3, hash);
  10796. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  10797. ret = WOLFSSL_FATAL_ERROR;
  10798. }
  10799. }
  10800. /* Test bad args. */
  10801. if (ret == 0) {
  10802. ret = wc_Sha3_384_Final(NULL, hash);
  10803. if (ret == 0) {
  10804. ret = wc_Sha3_384_Final(&sha3, NULL);
  10805. }
  10806. if (ret == BAD_FUNC_ARG) {
  10807. ret = 0;
  10808. } else if (ret == 0) {
  10809. ret = WOLFSSL_FATAL_ERROR;
  10810. }
  10811. }
  10812. wc_Sha3_384_Free(&sha3);
  10813. printf(resultFmt, ret == 0 ? passed : failed);
  10814. if (ret == 0) {
  10815. printf(testingFmt, "wc_Sha3_384_GetHash()");
  10816. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  10817. if (ret != 0) {
  10818. return ret;
  10819. }
  10820. /* Init stack variables. */
  10821. XMEMSET(hash, 0, sizeof(hash));
  10822. XMEMSET(hashRet, 0, sizeof(hashRet));
  10823. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10824. if (ret == 0) {
  10825. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  10826. }
  10827. if (ret == 0) {
  10828. ret = wc_Sha3_384_Final(&sha3, hash);
  10829. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  10830. ret = WOLFSSL_FATAL_ERROR;
  10831. }
  10832. }
  10833. if (ret == 0) {
  10834. /* Test bad args. */
  10835. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  10836. if (ret == BAD_FUNC_ARG) {
  10837. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  10838. }
  10839. if (ret == BAD_FUNC_ARG) {
  10840. ret = 0;
  10841. } else if (ret == 0) {
  10842. ret = WOLFSSL_FATAL_ERROR;
  10843. }
  10844. }
  10845. printf(resultFmt, ret == 0 ? passed : failed);
  10846. }
  10847. wc_Sha3_384_Free(&sha3);
  10848. #endif
  10849. return ret;
  10850. } /* END test_wc_Sha3_384_Final */
  10851. /*
  10852. * Testing wc_Sha3_512_Final()
  10853. */
  10854. static int test_wc_Sha3_512_Final (void)
  10855. {
  10856. int ret = 0;
  10857. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  10858. !defined(WOLFSSL_NOSHA3_384)
  10859. wc_Sha3 sha3;
  10860. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  10861. "nopnopq";
  10862. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  10863. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  10864. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  10865. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  10866. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  10867. byte hash[WC_SHA3_512_DIGEST_SIZE];
  10868. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  10869. /* Init stack variables. */
  10870. XMEMSET(hash, 0, sizeof(hash));
  10871. printf(testingFmt, "wc_Sha3_512_Final()");
  10872. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  10873. if (ret != 0) {
  10874. return ret;
  10875. }
  10876. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10877. if (ret == 0) {
  10878. ret = wc_Sha3_512_Final(&sha3, hash);
  10879. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  10880. ret = WOLFSSL_FATAL_ERROR;
  10881. }
  10882. }
  10883. /* Test bad args. */
  10884. if (ret == 0) {
  10885. ret = wc_Sha3_512_Final(NULL, hash);
  10886. if (ret == 0) {
  10887. ret = wc_Sha3_384_Final(&sha3, NULL);
  10888. }
  10889. if (ret == BAD_FUNC_ARG) {
  10890. ret = 0;
  10891. } else if (ret == 0) {
  10892. ret = WOLFSSL_FATAL_ERROR;
  10893. }
  10894. }
  10895. wc_Sha3_512_Free(&sha3);
  10896. printf(resultFmt, ret == 0 ? passed : failed);
  10897. if (ret == 0) {
  10898. printf(testingFmt, "wc_Sha3_512_GetHash()");
  10899. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  10900. if (ret != 0) {
  10901. return ret;
  10902. }
  10903. /* Init stack variables. */
  10904. XMEMSET(hash, 0, sizeof(hash));
  10905. XMEMSET(hashRet, 0, sizeof(hashRet));
  10906. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  10907. if (ret == 0) {
  10908. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  10909. }
  10910. if (ret == 0) {
  10911. ret = wc_Sha3_512_Final(&sha3, hash);
  10912. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  10913. ret = WOLFSSL_FATAL_ERROR;
  10914. }
  10915. }
  10916. if (ret == 0) {
  10917. /* Test bad args. */
  10918. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  10919. if (ret == BAD_FUNC_ARG) {
  10920. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  10921. }
  10922. if (ret == BAD_FUNC_ARG) {
  10923. ret = 0;
  10924. } else if (ret == 0) {
  10925. ret = WOLFSSL_FATAL_ERROR;
  10926. }
  10927. }
  10928. printf(resultFmt, ret == 0 ? passed : failed);
  10929. }
  10930. wc_Sha3_512_Free(&sha3);
  10931. #endif
  10932. return ret;
  10933. } /* END test_wc_Sha3_512_Final */
  10934. /*
  10935. * Testing wc_Sha3_224_Copy()
  10936. */
  10937. static int test_wc_Sha3_224_Copy (void)
  10938. {
  10939. int ret = 0;
  10940. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  10941. wc_Sha3 sha3, sha3Cpy;
  10942. const char* msg = TEST_STRING;
  10943. word32 msglen = (word32)TEST_STRING_SZ;
  10944. byte hash[WC_SHA3_224_DIGEST_SIZE];
  10945. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  10946. XMEMSET(hash, 0, sizeof(hash));
  10947. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  10948. printf(testingFmt, "wc_Sha3_224_Copy()");
  10949. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10950. if (ret != 0) {
  10951. return ret;
  10952. }
  10953. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, devId);
  10954. if (ret != 0) {
  10955. wc_Sha3_224_Free(&sha3);
  10956. return ret;
  10957. }
  10958. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  10959. if (ret == 0) {
  10960. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  10961. if (ret == 0) {
  10962. ret = wc_Sha3_224_Final(&sha3, hash);
  10963. if (ret == 0) {
  10964. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  10965. }
  10966. }
  10967. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  10968. ret = WOLFSSL_FATAL_ERROR;
  10969. }
  10970. }
  10971. /* Test bad args. */
  10972. if (ret == 0) {
  10973. ret = wc_Sha3_224_Copy(NULL, &sha3);
  10974. if (ret == BAD_FUNC_ARG) {
  10975. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  10976. }
  10977. if (ret == BAD_FUNC_ARG) {
  10978. ret = 0;
  10979. } else if (ret == 0) {
  10980. ret = WOLFSSL_FATAL_ERROR;
  10981. }
  10982. }
  10983. printf(resultFmt, ret == 0 ? passed : failed);
  10984. #endif
  10985. return ret;
  10986. } /* END test_wc_Sha3_224_Copy */
  10987. /*
  10988. * Testing wc_Sha3_256_Copy()
  10989. */
  10990. static int test_wc_Sha3_256_Copy (void)
  10991. {
  10992. int ret = 0;
  10993. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  10994. wc_Sha3 sha3, sha3Cpy;
  10995. const char* msg = TEST_STRING;
  10996. word32 msglen = (word32)TEST_STRING_SZ;
  10997. byte hash[WC_SHA3_256_DIGEST_SIZE];
  10998. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  10999. XMEMSET(hash, 0, sizeof(hash));
  11000. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11001. printf(testingFmt, "wc_Sha3_256_Copy()");
  11002. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  11003. if (ret != 0) {
  11004. return ret;
  11005. }
  11006. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, devId);
  11007. if (ret != 0) {
  11008. wc_Sha3_256_Free(&sha3);
  11009. return ret;
  11010. }
  11011. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  11012. if (ret == 0) {
  11013. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  11014. if (ret == 0) {
  11015. ret = wc_Sha3_256_Final(&sha3, hash);
  11016. if (ret == 0) {
  11017. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  11018. }
  11019. }
  11020. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11021. ret = WOLFSSL_FATAL_ERROR;
  11022. }
  11023. }
  11024. /* Test bad args. */
  11025. if (ret == 0) {
  11026. ret = wc_Sha3_256_Copy(NULL, &sha3);
  11027. if (ret == BAD_FUNC_ARG) {
  11028. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  11029. }
  11030. if (ret == BAD_FUNC_ARG) {
  11031. ret = 0;
  11032. } else if (ret == 0) {
  11033. ret = WOLFSSL_FATAL_ERROR;
  11034. }
  11035. }
  11036. printf(resultFmt, ret == 0 ? passed : failed);
  11037. #endif
  11038. return ret;
  11039. } /* END test_wc_Sha3_256_Copy */
  11040. /*
  11041. * Testing wc_Sha3_384_Copy()
  11042. */
  11043. static int test_wc_Sha3_384_Copy (void)
  11044. {
  11045. int ret = 0;
  11046. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  11047. wc_Sha3 sha3, sha3Cpy;
  11048. const char* msg = TEST_STRING;
  11049. word32 msglen = (word32)TEST_STRING_SZ;
  11050. byte hash[WC_SHA3_384_DIGEST_SIZE];
  11051. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  11052. XMEMSET(hash, 0, sizeof(hash));
  11053. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11054. printf(testingFmt, "wc_Sha3_384_Copy()");
  11055. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  11056. if (ret != 0) {
  11057. return ret;
  11058. }
  11059. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, devId);
  11060. if (ret != 0) {
  11061. wc_Sha3_384_Free(&sha3);
  11062. return ret;
  11063. }
  11064. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  11065. if (ret == 0) {
  11066. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  11067. if (ret == 0) {
  11068. ret = wc_Sha3_384_Final(&sha3, hash);
  11069. if (ret == 0) {
  11070. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  11071. }
  11072. }
  11073. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11074. ret = WOLFSSL_FATAL_ERROR;
  11075. }
  11076. }
  11077. /* Test bad args. */
  11078. if (ret == 0) {
  11079. ret = wc_Sha3_384_Copy(NULL, &sha3);
  11080. if (ret == BAD_FUNC_ARG) {
  11081. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  11082. }
  11083. if (ret == BAD_FUNC_ARG) {
  11084. ret = 0;
  11085. } else if (ret == 0) {
  11086. ret = WOLFSSL_FATAL_ERROR;
  11087. }
  11088. }
  11089. printf(resultFmt, ret == 0 ? passed : failed);
  11090. #endif
  11091. return ret;
  11092. } /* END test_wc_Sha3_384_Copy */
  11093. /*
  11094. * Testing wc_Sha3_512_Copy()
  11095. */
  11096. static int test_wc_Sha3_512_Copy (void)
  11097. {
  11098. int ret = 0;
  11099. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  11100. wc_Sha3 sha3, sha3Cpy;
  11101. const char* msg = TEST_STRING;
  11102. word32 msglen = (word32)TEST_STRING_SZ;
  11103. byte hash[WC_SHA3_512_DIGEST_SIZE];
  11104. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  11105. XMEMSET(hash, 0, sizeof(hash));
  11106. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11107. printf(testingFmt, "wc_Sha3_512_Copy()");
  11108. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  11109. if (ret != 0) {
  11110. return ret;
  11111. }
  11112. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, devId);
  11113. if (ret != 0) {
  11114. wc_Sha3_512_Free(&sha3);
  11115. return ret;
  11116. }
  11117. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  11118. if (ret == 0) {
  11119. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  11120. if (ret == 0) {
  11121. ret = wc_Sha3_512_Final(&sha3, hash);
  11122. if (ret == 0) {
  11123. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  11124. }
  11125. }
  11126. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11127. ret = WOLFSSL_FATAL_ERROR;
  11128. }
  11129. }
  11130. /* Test bad args. */
  11131. if (ret == 0) {
  11132. ret = wc_Sha3_512_Copy(NULL, &sha3);
  11133. if (ret == BAD_FUNC_ARG) {
  11134. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  11135. }
  11136. if (ret == BAD_FUNC_ARG) {
  11137. ret = 0;
  11138. } else if (ret == 0) {
  11139. ret = WOLFSSL_FATAL_ERROR;
  11140. }
  11141. }
  11142. printf(resultFmt, ret == 0 ? passed : failed);
  11143. #endif
  11144. return ret;
  11145. } /* END test_wc_Sha3_512_Copy */
  11146. /*
  11147. * Unit test function for wc_Sha3_GetFlags()
  11148. */
  11149. static int test_wc_Sha3_GetFlags (void)
  11150. {
  11151. int ret = 0;
  11152. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  11153. wc_Sha3 sha3;
  11154. word32 flags = 0;
  11155. printf(testingFmt, "wc_Sha3_GetFlags()");
  11156. /* Initialize */
  11157. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  11158. if (ret != 0) {
  11159. return ret;
  11160. }
  11161. if (ret == 0) {
  11162. ret = wc_Sha3_GetFlags(&sha3, &flags);
  11163. }
  11164. if (ret == 0) {
  11165. if (flags & WC_HASH_FLAG_ISCOPY) {
  11166. ret = 0;
  11167. }
  11168. }
  11169. wc_Sha3_224_Free(&sha3);
  11170. printf(resultFmt, ret == 0 ? passed : failed);
  11171. #endif
  11172. return ret;
  11173. } /* END test_wc_Sha3_GetFlags */
  11174. static int test_wc_InitShake256 (void)
  11175. {
  11176. int ret = 0;
  11177. #ifdef WOLFSSL_SHAKE256
  11178. wc_Shake shake;
  11179. printf(testingFmt, "wc_InitShake256()");
  11180. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11181. /* Test bad args. */
  11182. if (ret == 0) {
  11183. ret = wc_InitShake256(NULL, HEAP_HINT, devId);
  11184. if (ret == BAD_FUNC_ARG) {
  11185. ret = 0;
  11186. } else if (ret == 0) {
  11187. ret = WOLFSSL_FATAL_ERROR;
  11188. }
  11189. }
  11190. wc_Shake256_Free(&shake);
  11191. printf(resultFmt, ret == 0 ? passed : failed);
  11192. #endif
  11193. return ret;
  11194. } /* END test_wc_InitSha3 */
  11195. static int testing_wc_Shake256_Update (void)
  11196. {
  11197. int ret = 0;
  11198. #ifdef WOLFSSL_SHAKE256
  11199. wc_Shake shake;
  11200. byte msg[] = "Everybody's working for the weekend.";
  11201. byte msg2[] = "Everybody gets Friday off.";
  11202. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  11203. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  11204. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  11205. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11206. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11207. word32 msglen = sizeof(msg) - 1;
  11208. word32 msg2len = sizeof(msg2);
  11209. word32 msgCmplen = sizeof(msgCmp);
  11210. printf(testingFmt, "wc_Shake256_Update()");
  11211. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11212. if (ret != 0) {
  11213. return ret;
  11214. }
  11215. ret = wc_Shake256_Update(&shake, msg, msglen);
  11216. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  11217. ret = WOLFSSL_FATAL_ERROR;
  11218. }
  11219. if (ret == 0) {
  11220. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  11221. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  11222. ret = WOLFSSL_FATAL_ERROR;
  11223. }
  11224. }
  11225. /* Pass bad args. */
  11226. if (ret == 0) {
  11227. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  11228. if (ret == BAD_FUNC_ARG) {
  11229. ret = wc_Shake256_Update(&shake, NULL, 5);
  11230. }
  11231. if (ret == BAD_FUNC_ARG) {
  11232. wc_Shake256_Free(&shake);
  11233. if (wc_InitShake256(&shake, HEAP_HINT, devId)) {
  11234. return ret;
  11235. }
  11236. ret = wc_Shake256_Update(&shake, NULL, 0);
  11237. if (ret == 0) {
  11238. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  11239. }
  11240. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  11241. ret = WOLFSSL_FATAL_ERROR;
  11242. }
  11243. }
  11244. }
  11245. wc_Shake256_Free(&shake);
  11246. printf(resultFmt, ret == 0 ? passed : failed);
  11247. #endif /* WOLFSSL_SHAKE256 */
  11248. return ret;
  11249. }
  11250. static int test_wc_Shake256_Final (void)
  11251. {
  11252. int ret = 0;
  11253. #ifdef WOLFSSL_SHAKE256
  11254. wc_Shake shake;
  11255. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11256. "nopnopq";
  11257. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  11258. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  11259. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  11260. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  11261. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  11262. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  11263. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  11264. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  11265. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  11266. byte hash[114];
  11267. /* Init stack variables. */
  11268. XMEMSET(hash, 0, sizeof(hash));
  11269. printf(testingFmt, "wc_Shake256_Final()");
  11270. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11271. if (ret != 0) {
  11272. return ret;
  11273. }
  11274. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  11275. if (ret == 0) {
  11276. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  11277. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  11278. ret = WOLFSSL_FATAL_ERROR;
  11279. }
  11280. }
  11281. /* Test bad args. */
  11282. if (ret == 0) {
  11283. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  11284. if (ret == 0) {
  11285. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  11286. }
  11287. if (ret == BAD_FUNC_ARG) {
  11288. ret = 0;
  11289. } else if (ret == 0) {
  11290. ret = WOLFSSL_FATAL_ERROR;
  11291. }
  11292. }
  11293. wc_Shake256_Free(&shake);
  11294. printf(resultFmt, ret == 0 ? passed : failed);
  11295. #endif
  11296. return ret;
  11297. }
  11298. /*
  11299. * Testing wc_Shake256_Copy()
  11300. */
  11301. static int test_wc_Shake256_Copy (void)
  11302. {
  11303. int ret = 0;
  11304. #ifdef WOLFSSL_SHAKE256
  11305. wc_Shake shake, shakeCpy;
  11306. const char* msg = TEST_STRING;
  11307. word32 msglen = (word32)TEST_STRING_SZ;
  11308. byte hash[144];
  11309. byte hashCpy[144];
  11310. word32 hashLen = sizeof(hash);
  11311. word32 hashLenCpy = sizeof(hashCpy);
  11312. XMEMSET(hash, 0, sizeof(hash));
  11313. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11314. printf(testingFmt, "wc_Shake256_Copy()");
  11315. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11316. if (ret != 0) {
  11317. return ret;
  11318. }
  11319. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, devId);
  11320. if (ret != 0) {
  11321. wc_Shake256_Free(&shake);
  11322. return ret;
  11323. }
  11324. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  11325. if (ret == 0) {
  11326. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  11327. if (ret == 0) {
  11328. ret = wc_Shake256_Final(&shake, hash, hashLen);
  11329. if (ret == 0) {
  11330. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  11331. }
  11332. }
  11333. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11334. ret = WOLFSSL_FATAL_ERROR;
  11335. }
  11336. }
  11337. /* Test bad args. */
  11338. if (ret == 0) {
  11339. ret = wc_Shake256_Copy(NULL, &shake);
  11340. if (ret == BAD_FUNC_ARG) {
  11341. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  11342. }
  11343. if (ret == BAD_FUNC_ARG) {
  11344. ret = 0;
  11345. } else if (ret == 0) {
  11346. ret = WOLFSSL_FATAL_ERROR;
  11347. }
  11348. }
  11349. wc_Shake256_Free(&shake);
  11350. printf(resultFmt, ret == 0 ? passed : failed);
  11351. #endif
  11352. return ret;
  11353. } /* END test_wc_Shake256_Copy */
  11354. /*
  11355. * Unit test function for wc_Shake256Hash()
  11356. */
  11357. static int test_wc_Shake256Hash(void)
  11358. {
  11359. int ret = 0;
  11360. #ifdef WOLFSSL_SHAKE256
  11361. const byte data[] = { /* Hello World */
  11362. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  11363. 0x72,0x6c,0x64
  11364. };
  11365. word32 len = sizeof(data);
  11366. byte hash[144];
  11367. word32 hashLen = sizeof(hash);
  11368. printf(testingFmt, "wc_Shake256Hash()");
  11369. ret = wc_Shake256Hash(data, len, hash, hashLen);
  11370. printf(resultFmt, ret == 0 ? passed : failed);
  11371. #endif
  11372. return ret;
  11373. } /* END test_wc_Shake256Hash */
  11374. /*
  11375. * unit test for wc_IdeaSetKey()
  11376. */
  11377. static int test_wc_IdeaSetKey (void)
  11378. {
  11379. int ret = 0;
  11380. #ifdef HAVE_IDEA
  11381. Idea idea;
  11382. const byte key[] =
  11383. {
  11384. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  11385. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  11386. };
  11387. int flag = 0;
  11388. printf(testingFmt, "wc_IdeaSetKey()");
  11389. /*IV can be NULL, default value is 0*/
  11390. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  11391. if (ret == 0) {
  11392. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  11393. }
  11394. /* Bad args. */
  11395. if (ret == 0) {
  11396. ret = wc_IdeaSetKey(NULL, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  11397. if (ret != BAD_FUNC_ARG) {
  11398. flag = 1;
  11399. }
  11400. ret = wc_IdeaSetKey(&idea, NULL, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  11401. if (ret != BAD_FUNC_ARG) {
  11402. flag = 1;
  11403. }
  11404. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE - 1,
  11405. NULL, IDEA_ENCRYPTION);
  11406. if (ret != BAD_FUNC_ARG) {
  11407. flag = 1;
  11408. }
  11409. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, -1);
  11410. if (ret != BAD_FUNC_ARG) {
  11411. flag = 1;
  11412. }
  11413. if (flag == 1) {
  11414. ret = WOLFSSL_FATAL_ERROR;
  11415. } else {
  11416. ret = 0;
  11417. }
  11418. } /* END Test Bad Args. */
  11419. printf(resultFmt, ret == 0 ? passed : failed);
  11420. #endif
  11421. return ret;
  11422. } /* END test_wc_IdeaSetKey */
  11423. /*
  11424. * Unit test for wc_IdeaSetIV()
  11425. */
  11426. static int test_wc_IdeaSetIV (void)
  11427. {
  11428. int ret = 0;
  11429. #ifdef HAVE_IDEA
  11430. Idea idea;
  11431. printf(testingFmt, "wc_IdeaSetIV()");
  11432. ret = wc_IdeaSetIV(&idea, NULL);
  11433. /* Test bad args. */
  11434. if (ret == 0) {
  11435. ret = wc_IdeaSetIV(NULL, NULL);
  11436. if (ret == BAD_FUNC_ARG) {
  11437. ret = 0;
  11438. } else {
  11439. ret = WOLFSSL_FATAL_ERROR;
  11440. }
  11441. }
  11442. printf(resultFmt, ret == 0 ? passed : failed);
  11443. #endif
  11444. return ret;
  11445. } /* END test_wc_IdeaSetIV */
  11446. /*
  11447. * Unit test for wc_IdeaCipher()
  11448. */
  11449. static int test_wc_IdeaCipher (void)
  11450. {
  11451. int ret = 0;
  11452. #ifdef HAVE_IDEA
  11453. Idea idea;
  11454. const byte key[] =
  11455. {
  11456. 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  11457. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48
  11458. };
  11459. const byte plain[] =
  11460. {
  11461. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  11462. };
  11463. byte enc[sizeof(plain)];
  11464. byte dec[sizeof(enc)];
  11465. printf(testingFmt, "wc_IdeaCipher()");
  11466. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
  11467. if (ret == 0) {
  11468. ret = wc_IdeaCipher(&idea, enc, plain);
  11469. if (ret != 0) {
  11470. ret = WOLFSSL_FATAL_ERROR;
  11471. }
  11472. }
  11473. if (ret == 0) {
  11474. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
  11475. if (ret == 0) {
  11476. ret = wc_IdeaCipher(&idea, dec, enc);
  11477. }
  11478. if (ret == 0) {
  11479. ret = XMEMCMP(plain, dec, IDEA_BLOCK_SIZE);
  11480. }
  11481. if (ret != 0) {
  11482. ret = WOLFSSL_FATAL_ERROR;
  11483. }
  11484. }
  11485. /* Pass Bad Args. */
  11486. if (ret == 0) {
  11487. ret = wc_IdeaCipher(NULL, enc, dec);
  11488. if (ret == BAD_FUNC_ARG) {
  11489. ret = wc_IdeaCipher(&idea, NULL, dec);
  11490. }
  11491. if (ret == BAD_FUNC_ARG) {
  11492. ret = wc_IdeaCipher(&idea, enc, NULL);
  11493. }
  11494. if (ret == BAD_FUNC_ARG) {
  11495. ret = 0;
  11496. } else {
  11497. ret = WOLFSSL_FATAL_ERROR;
  11498. }
  11499. }
  11500. printf(resultFmt, ret == 0 ? passed : failed);
  11501. #endif
  11502. return ret;
  11503. } /* END test_wc_IdeaCipher */
  11504. /*
  11505. * Unit test for functions wc_IdeaCbcEncrypt and wc_IdeaCbcDecrypt
  11506. */
  11507. static int test_wc_IdeaCbcEncyptDecrypt (void)
  11508. {
  11509. int ret = 0;
  11510. #ifdef HAVE_IDEA
  11511. Idea idea;
  11512. const byte key[] =
  11513. {
  11514. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  11515. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
  11516. };
  11517. const char* message = "International Data Encryption Algorithm";
  11518. byte msg_enc[40];
  11519. byte msg_dec[40];
  11520. printf(testingFmt, "wc_IdeaCbcEncrypt()");
  11521. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_ENCRYPTION);
  11522. if (ret == 0) {
  11523. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  11524. (word32)XSTRLEN(message) + 1);
  11525. }
  11526. if (ret == 0) {
  11527. ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_DECRYPTION);
  11528. }
  11529. if (ret == 0) {
  11530. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  11531. (word32)XSTRLEN(message) + 1);
  11532. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  11533. ret = WOLFSSL_FATAL_ERROR;
  11534. }
  11535. }
  11536. /* Test bad args. Enc */
  11537. if (ret == 0) {
  11538. ret = wc_IdeaCbcEncrypt(NULL, msg_enc, (byte*)message,
  11539. (word32)XSTRLEN(message) + 1);
  11540. if (ret == BAD_FUNC_ARG) {
  11541. ret = wc_IdeaCbcEncrypt(&idea, NULL, (byte*)message,
  11542. (word32)XSTRLEN(message) + 1);
  11543. }
  11544. if (ret == BAD_FUNC_ARG) {
  11545. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, NULL,
  11546. (word32)XSTRLEN(message) + 1);
  11547. }
  11548. if (ret != BAD_FUNC_ARG) {
  11549. ret = WOLFSSL_FATAL_ERROR;
  11550. } else {
  11551. ret = 0;
  11552. }
  11553. } /* END test bad args ENC */
  11554. /* Test bad args DEC */
  11555. if (ret == 0) {
  11556. ret = wc_IdeaCbcDecrypt(NULL, msg_dec, msg_enc,
  11557. (word32)XSTRLEN(message) + 1);
  11558. if (ret == BAD_FUNC_ARG) {
  11559. ret = wc_IdeaCbcDecrypt(&idea, NULL, msg_enc,
  11560. (word32)XSTRLEN(message) + 1);
  11561. }
  11562. if (ret == BAD_FUNC_ARG) {
  11563. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, NULL,
  11564. (word32)XSTRLEN(message) + 1);
  11565. }
  11566. if (ret != BAD_FUNC_ARG) {
  11567. ret = WOLFSSL_FATAL_ERROR;
  11568. } else {
  11569. ret = 0;
  11570. }
  11571. }
  11572. printf(resultFmt, ret == 0 ? passed : failed);
  11573. #endif
  11574. return ret;
  11575. } /* END test_wc_IdeaCbcEncryptDecrypt */
  11576. /*
  11577. * Test function for wc_HmacSetKey
  11578. */
  11579. static int test_wc_Md5HmacSetKey (void)
  11580. {
  11581. int flag = 0;
  11582. #if !defined(NO_HMAC) && !defined(NO_MD5)
  11583. Hmac hmac;
  11584. int ret, times, itr;
  11585. const char* keys[]=
  11586. {
  11587. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  11588. #ifndef HAVE_FIPS
  11589. "Jefe", /* smaller than minimum FIPS key size */
  11590. #endif
  11591. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11592. };
  11593. times = sizeof(keys) / sizeof(char*);
  11594. flag = 0;
  11595. printf(testingFmt, "wc_HmacSetKey() with MD5");
  11596. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11597. if (ret != 0)
  11598. return ret;
  11599. for (itr = 0; itr < times; itr++) {
  11600. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  11601. (word32)XSTRLEN(keys[itr]));
  11602. if (ret != 0) {
  11603. flag = ret;
  11604. }
  11605. }
  11606. /* Bad args. */
  11607. if (!flag) {
  11608. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  11609. (word32)XSTRLEN(keys[0]));
  11610. if (ret != BAD_FUNC_ARG) {
  11611. flag = WOLFSSL_FATAL_ERROR;
  11612. }
  11613. }
  11614. if (!flag) {
  11615. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  11616. if (ret != BAD_FUNC_ARG) {
  11617. flag = WOLFSSL_FATAL_ERROR;
  11618. }
  11619. }
  11620. if (!flag) {
  11621. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11622. (word32)XSTRLEN(keys[0]));
  11623. if (ret != BAD_FUNC_ARG) {
  11624. flag = WOLFSSL_FATAL_ERROR;
  11625. }
  11626. }
  11627. if (!flag) {
  11628. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  11629. #ifdef HAVE_FIPS
  11630. if (ret != HMAC_MIN_KEYLEN_E) {
  11631. flag = WOLFSSL_FATAL_ERROR;
  11632. }
  11633. #else
  11634. if (ret != 0) {
  11635. flag = WOLFSSL_FATAL_ERROR;
  11636. }
  11637. #endif
  11638. }
  11639. wc_HmacFree(&hmac);
  11640. printf(resultFmt, flag == 0 ? passed : failed);
  11641. #endif
  11642. return flag;
  11643. } /* END test_wc_Md5HmacSetKey */
  11644. /*
  11645. * testing wc_HmacSetKey() on wc_Sha hash.
  11646. */
  11647. static int test_wc_ShaHmacSetKey (void)
  11648. {
  11649. int flag = 0;
  11650. #if !defined(NO_HMAC) && !defined(NO_SHA)
  11651. Hmac hmac;
  11652. int ret, times, itr;
  11653. const char* keys[]=
  11654. {
  11655. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11656. "\x0b\x0b\x0b",
  11657. #ifndef HAVE_FIPS
  11658. "Jefe", /* smaller than minimum FIPS key size */
  11659. #endif
  11660. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11661. "\xAA\xAA\xAA"
  11662. };
  11663. times = sizeof(keys) / sizeof(char*);
  11664. flag = 0;
  11665. printf(testingFmt, "wc_HmacSetKey() with SHA");
  11666. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11667. if (ret != 0)
  11668. return ret;
  11669. for (itr = 0; itr < times; itr++) {
  11670. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  11671. (word32)XSTRLEN(keys[itr]));
  11672. if (ret != 0) {
  11673. flag = ret;
  11674. }
  11675. }
  11676. /* Bad args. */
  11677. if (!flag) {
  11678. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  11679. (word32)XSTRLEN(keys[0]));
  11680. if (ret != BAD_FUNC_ARG) {
  11681. flag = WOLFSSL_FATAL_ERROR;
  11682. }
  11683. }
  11684. if (!flag) {
  11685. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  11686. if (ret != BAD_FUNC_ARG) {
  11687. flag = WOLFSSL_FATAL_ERROR;
  11688. }
  11689. }
  11690. if (!flag) {
  11691. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11692. (word32)XSTRLEN(keys[0]));
  11693. if (ret != BAD_FUNC_ARG) {
  11694. flag = WOLFSSL_FATAL_ERROR;
  11695. }
  11696. }
  11697. if (!flag) {
  11698. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  11699. #ifdef HAVE_FIPS
  11700. if (ret != HMAC_MIN_KEYLEN_E) {
  11701. flag = WOLFSSL_FATAL_ERROR;
  11702. }
  11703. #else
  11704. if (ret != 0) {
  11705. flag = WOLFSSL_FATAL_ERROR;
  11706. }
  11707. #endif
  11708. }
  11709. wc_HmacFree(&hmac);
  11710. printf(resultFmt, flag == 0 ? passed : failed);
  11711. #endif
  11712. return flag;
  11713. } /* END test_wc_ShaHmacSetKey() */
  11714. /*
  11715. * testing wc_HmacSetKey() on Sha224 hash.
  11716. */
  11717. static int test_wc_Sha224HmacSetKey (void)
  11718. {
  11719. int flag = 0;
  11720. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  11721. Hmac hmac;
  11722. int ret, times, itr;
  11723. const char* keys[]=
  11724. {
  11725. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11726. "\x0b\x0b\x0b",
  11727. #ifndef HAVE_FIPS
  11728. "Jefe", /* smaller than minimum FIPS key size */
  11729. #endif
  11730. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11731. "\xAA\xAA\xAA"
  11732. };
  11733. times = sizeof(keys) / sizeof(char*);
  11734. flag = 0;
  11735. printf(testingFmt, "wc_HmacSetKey() with SHA 224");
  11736. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11737. if (ret != 0)
  11738. return ret;
  11739. for (itr = 0; itr < times; itr++) {
  11740. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  11741. (word32)XSTRLEN(keys[itr]));
  11742. if (ret != 0) {
  11743. flag = ret;
  11744. }
  11745. }
  11746. /* Bad args. */
  11747. if (!flag) {
  11748. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  11749. (word32)XSTRLEN(keys[0]));
  11750. if (ret != BAD_FUNC_ARG) {
  11751. flag = WOLFSSL_FATAL_ERROR;
  11752. }
  11753. }
  11754. if (!flag) {
  11755. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  11756. if (ret != BAD_FUNC_ARG) {
  11757. flag = WOLFSSL_FATAL_ERROR;
  11758. }
  11759. }
  11760. if (!flag) {
  11761. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11762. (word32)XSTRLEN(keys[0]));
  11763. if (ret != BAD_FUNC_ARG) {
  11764. flag = WOLFSSL_FATAL_ERROR;
  11765. }
  11766. }
  11767. if (!flag) {
  11768. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  11769. #ifdef HAVE_FIPS
  11770. if (ret != HMAC_MIN_KEYLEN_E) {
  11771. flag = WOLFSSL_FATAL_ERROR;
  11772. }
  11773. #else
  11774. if (ret != 0) {
  11775. flag = WOLFSSL_FATAL_ERROR;
  11776. }
  11777. #endif
  11778. }
  11779. wc_HmacFree(&hmac);
  11780. printf(resultFmt, flag == 0 ? passed : failed);
  11781. #endif
  11782. return flag;
  11783. } /* END test_wc_Sha224HmacSetKey() */
  11784. /*
  11785. * testing wc_HmacSetKey() on Sha256 hash
  11786. */
  11787. static int test_wc_Sha256HmacSetKey (void)
  11788. {
  11789. int flag = 0;
  11790. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  11791. Hmac hmac;
  11792. int ret, times, itr;
  11793. const char* keys[]=
  11794. {
  11795. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11796. "\x0b\x0b\x0b",
  11797. #ifndef HAVE_FIPS
  11798. "Jefe", /* smaller than minimum FIPS key size */
  11799. #endif
  11800. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11801. "\xAA\xAA\xAA"
  11802. };
  11803. times = sizeof(keys) / sizeof(char*);
  11804. flag = 0;
  11805. printf(testingFmt, "wc_HmacSetKey() with SHA256");
  11806. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11807. if (ret != 0)
  11808. return ret;
  11809. for (itr = 0; itr < times; itr++) {
  11810. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  11811. (word32)XSTRLEN(keys[itr]));
  11812. if (ret != 0) {
  11813. flag = ret;
  11814. }
  11815. }
  11816. /* Bad args. */
  11817. if (!flag) {
  11818. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  11819. (word32)XSTRLEN(keys[0]));
  11820. if (ret != BAD_FUNC_ARG) {
  11821. flag = WOLFSSL_FATAL_ERROR;
  11822. }
  11823. }
  11824. if (!flag) {
  11825. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  11826. if (ret != BAD_FUNC_ARG) {
  11827. flag = WOLFSSL_FATAL_ERROR;
  11828. }
  11829. }
  11830. if (!flag) {
  11831. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11832. (word32)XSTRLEN(keys[0]));
  11833. if (ret != BAD_FUNC_ARG) {
  11834. flag = WOLFSSL_FATAL_ERROR;
  11835. }
  11836. }
  11837. if (!flag) {
  11838. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  11839. #ifdef HAVE_FIPS
  11840. if (ret != HMAC_MIN_KEYLEN_E) {
  11841. flag = WOLFSSL_FATAL_ERROR;
  11842. }
  11843. #else
  11844. if (ret != 0) {
  11845. flag = WOLFSSL_FATAL_ERROR;
  11846. }
  11847. #endif
  11848. }
  11849. wc_HmacFree(&hmac);
  11850. printf(resultFmt, flag == 0 ? passed : failed);
  11851. #endif
  11852. return flag;
  11853. } /* END test_wc_Sha256HmacSetKey() */
  11854. /*
  11855. * testing wc_HmacSetKey on Sha384 hash.
  11856. */
  11857. static int test_wc_Sha384HmacSetKey (void)
  11858. {
  11859. int flag = 0;
  11860. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  11861. Hmac hmac;
  11862. int ret, times, itr;
  11863. const char* keys[]=
  11864. {
  11865. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  11866. "\x0b\x0b\x0b",
  11867. #ifndef HAVE_FIPS
  11868. "Jefe", /* smaller than minimum FIPS key size */
  11869. #endif
  11870. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11871. "\xAA\xAA\xAA"
  11872. };
  11873. times = sizeof(keys) / sizeof(char*);
  11874. flag = 0;
  11875. printf(testingFmt, "wc_HmacSetKey() with SHA384");
  11876. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11877. if (ret != 0)
  11878. return ret;
  11879. for (itr = 0; itr < times; itr++) {
  11880. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  11881. (word32)XSTRLEN(keys[itr]));
  11882. if (ret != 0) {
  11883. flag = ret;
  11884. }
  11885. }
  11886. /* Bad args. */
  11887. if (!flag) {
  11888. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  11889. (word32)XSTRLEN(keys[0]));
  11890. if (ret != BAD_FUNC_ARG) {
  11891. flag = WOLFSSL_FATAL_ERROR;
  11892. }
  11893. }
  11894. if (!flag) {
  11895. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  11896. if (ret != BAD_FUNC_ARG) {
  11897. flag = WOLFSSL_FATAL_ERROR;
  11898. }
  11899. }
  11900. if (!flag) {
  11901. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11902. (word32)XSTRLEN(keys[0]));
  11903. if (ret != BAD_FUNC_ARG) {
  11904. flag = WOLFSSL_FATAL_ERROR;
  11905. }
  11906. }
  11907. if (!flag) {
  11908. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  11909. #ifdef HAVE_FIPS
  11910. if (ret != HMAC_MIN_KEYLEN_E) {
  11911. flag = WOLFSSL_FATAL_ERROR;
  11912. }
  11913. #else
  11914. if (ret != 0) {
  11915. flag = WOLFSSL_FATAL_ERROR;
  11916. }
  11917. #endif
  11918. }
  11919. wc_HmacFree(&hmac);
  11920. printf(resultFmt, flag == 0 ? passed : failed);
  11921. #endif
  11922. return flag;
  11923. } /* END test_wc_Sha384HmacSetKey() */
  11924. /*
  11925. * testing wc_HmacUpdate on wc_Md5 hash.
  11926. */
  11927. static int test_wc_Md5HmacUpdate (void)
  11928. {
  11929. int flag = 0;
  11930. #if !defined(NO_HMAC) && !defined(NO_MD5)
  11931. Hmac hmac;
  11932. testVector a, b;
  11933. int ret;
  11934. #ifdef HAVE_FIPS
  11935. const char* keys =
  11936. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  11937. #else
  11938. const char* keys = "Jefe";
  11939. #endif
  11940. a.input = "what do ya want for nothing?";
  11941. a.inLen = XSTRLEN(a.input);
  11942. b.input = "Hi There";
  11943. b.inLen = XSTRLEN(b.input);
  11944. flag = 0;
  11945. printf(testingFmt, "wc_HmacUpdate() with MD5");
  11946. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11947. if (ret != 0)
  11948. return ret;
  11949. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  11950. if (ret != 0) {
  11951. flag = ret;
  11952. }
  11953. if (!flag) {
  11954. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  11955. if (ret != 0) {
  11956. flag = ret;
  11957. }
  11958. }
  11959. /* Update Hmac. */
  11960. if (!flag) {
  11961. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  11962. if (ret != 0) {
  11963. flag = ret;
  11964. }
  11965. }
  11966. /* Test bad args. */
  11967. if (!flag) {
  11968. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  11969. if (ret != BAD_FUNC_ARG) {
  11970. flag = WOLFSSL_FATAL_ERROR;
  11971. }
  11972. }
  11973. if (!flag) {
  11974. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  11975. if (ret != BAD_FUNC_ARG) {
  11976. flag = WOLFSSL_FATAL_ERROR;
  11977. }
  11978. }
  11979. if (!flag) {
  11980. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  11981. if (ret != 0) {
  11982. flag = ret;
  11983. }
  11984. }
  11985. wc_HmacFree(&hmac);
  11986. printf(resultFmt, flag == 0 ? passed : failed);
  11987. #endif
  11988. return flag;
  11989. } /* END test_wc_Md5HmacUpdate */
  11990. /*
  11991. * testing wc_HmacUpdate on SHA hash.
  11992. */
  11993. static int test_wc_ShaHmacUpdate (void)
  11994. {
  11995. int flag = 0;
  11996. #if !defined(NO_HMAC) && !defined(NO_SHA)
  11997. Hmac hmac;
  11998. testVector a, b;
  11999. int ret;
  12000. #ifdef HAVE_FIPS
  12001. const char* keys =
  12002. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12003. #else
  12004. const char* keys = "Jefe";
  12005. #endif
  12006. a.input = "what do ya want for nothing?";
  12007. a.inLen = XSTRLEN(a.input);
  12008. b.input = "Hi There";
  12009. b.inLen = XSTRLEN(b.input);
  12010. flag = 0;
  12011. printf(testingFmt, "wc_HmacUpdate() with SHA");
  12012. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12013. if (ret != 0)
  12014. return ret;
  12015. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  12016. if (ret != 0) {
  12017. flag = ret;
  12018. }
  12019. if (!flag) {
  12020. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12021. if (ret != 0) {
  12022. flag = ret;
  12023. }
  12024. }
  12025. /* Update Hmac. */
  12026. if (!flag) {
  12027. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12028. if (ret != 0) {
  12029. flag = ret;
  12030. }
  12031. }
  12032. /* Test bad args. */
  12033. if (!flag) {
  12034. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12035. if (ret != BAD_FUNC_ARG) {
  12036. flag = WOLFSSL_FATAL_ERROR;
  12037. }
  12038. }
  12039. if (!flag) {
  12040. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12041. if (ret != BAD_FUNC_ARG) {
  12042. flag = WOLFSSL_FATAL_ERROR;
  12043. }
  12044. }
  12045. if (!flag) {
  12046. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12047. if (ret != 0) {
  12048. flag = ret;
  12049. }
  12050. }
  12051. wc_HmacFree(&hmac);
  12052. printf(resultFmt, flag == 0 ? passed : failed);
  12053. #endif
  12054. return flag;
  12055. } /* END test_wc_ShaHmacUpdate */
  12056. /*
  12057. * testing wc_HmacUpdate on SHA224 hash.
  12058. */
  12059. static int test_wc_Sha224HmacUpdate (void)
  12060. {
  12061. int flag = 0;
  12062. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12063. Hmac hmac;
  12064. testVector a, b;
  12065. int ret;
  12066. #ifdef HAVE_FIPS
  12067. const char* keys =
  12068. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12069. #else
  12070. const char* keys = "Jefe";
  12071. #endif
  12072. a.input = "what do ya want for nothing?";
  12073. a.inLen = XSTRLEN(a.input);
  12074. b.input = "Hi There";
  12075. b.inLen = XSTRLEN(b.input);
  12076. flag = 0;
  12077. printf(testingFmt, "wc_HmacUpdate() with SHA224");
  12078. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12079. if (ret != 0)
  12080. return ret;
  12081. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  12082. if (ret != 0) {
  12083. flag = ret;
  12084. }
  12085. if (!flag) {
  12086. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12087. if (ret != 0) {
  12088. flag = ret;
  12089. }
  12090. }
  12091. /* Update Hmac. */
  12092. if (!flag) {
  12093. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12094. if (ret != 0) {
  12095. flag = ret;
  12096. }
  12097. }
  12098. /* Test bad args. */
  12099. if (!flag) {
  12100. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12101. if (ret != BAD_FUNC_ARG) {
  12102. flag = WOLFSSL_FATAL_ERROR;
  12103. }
  12104. }
  12105. if (!flag) {
  12106. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12107. if (ret != BAD_FUNC_ARG) {
  12108. flag = WOLFSSL_FATAL_ERROR;
  12109. }
  12110. }
  12111. if (!flag) {
  12112. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12113. if (ret != 0) {
  12114. flag = ret;
  12115. }
  12116. }
  12117. wc_HmacFree(&hmac);
  12118. printf(resultFmt, flag == 0 ? passed : failed);
  12119. #endif
  12120. return flag;
  12121. } /* END test_wc_Sha224HmacUpdate */
  12122. /*
  12123. * testing wc_HmacUpdate on SHA256 hash.
  12124. */
  12125. static int test_wc_Sha256HmacUpdate (void)
  12126. {
  12127. int flag = 0;
  12128. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12129. Hmac hmac;
  12130. testVector a, b;
  12131. int ret;
  12132. #ifdef HAVE_FIPS
  12133. const char* keys =
  12134. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12135. #else
  12136. const char* keys = "Jefe";
  12137. #endif
  12138. a.input = "what do ya want for nothing?";
  12139. a.inLen = XSTRLEN(a.input);
  12140. b.input = "Hi There";
  12141. b.inLen = XSTRLEN(b.input);
  12142. flag = 0;
  12143. printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
  12144. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12145. if (ret != 0)
  12146. return ret;
  12147. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  12148. if (ret != 0) {
  12149. flag = ret;
  12150. }
  12151. if (!flag) {
  12152. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12153. if (ret != 0) {
  12154. flag = ret;
  12155. }
  12156. }
  12157. /* Update Hmac. */
  12158. if (!flag) {
  12159. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12160. if (ret != 0) {
  12161. flag = ret;
  12162. }
  12163. }
  12164. /* Test bad args. */
  12165. if (!flag) {
  12166. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12167. if (ret != BAD_FUNC_ARG) {
  12168. flag = WOLFSSL_FATAL_ERROR;
  12169. }
  12170. }
  12171. if (!flag) {
  12172. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12173. if (ret != BAD_FUNC_ARG) {
  12174. flag = WOLFSSL_FATAL_ERROR;
  12175. }
  12176. }
  12177. if (!flag) {
  12178. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12179. if (ret != 0) {
  12180. flag = ret;
  12181. }
  12182. }
  12183. wc_HmacFree(&hmac);
  12184. printf(resultFmt, flag == 0 ? passed : failed);
  12185. #endif
  12186. return flag;
  12187. } /* END test_wc_Sha256HmacUpdate */
  12188. /*
  12189. * testing wc_HmacUpdate on SHA384 hash.
  12190. */
  12191. static int test_wc_Sha384HmacUpdate (void)
  12192. {
  12193. int flag = 0;
  12194. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12195. Hmac hmac;
  12196. testVector a, b;
  12197. int ret;
  12198. #ifdef HAVE_FIPS
  12199. const char* keys =
  12200. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12201. #else
  12202. const char* keys = "Jefe";
  12203. #endif
  12204. a.input = "what do ya want for nothing?";
  12205. a.inLen = XSTRLEN(a.input);
  12206. b.input = "Hi There";
  12207. b.inLen = XSTRLEN(b.input);
  12208. flag = 0;
  12209. printf(testingFmt, "wc_HmacUpdate() with SHA384");
  12210. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12211. if (ret != 0)
  12212. return ret;
  12213. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  12214. if (ret != 0) {
  12215. flag = ret;
  12216. }
  12217. if (!flag) {
  12218. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12219. if (ret != 0) {
  12220. flag = ret;
  12221. }
  12222. }
  12223. /* Update Hmac. */
  12224. if (!flag) {
  12225. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12226. if (ret != 0) {
  12227. flag = ret;
  12228. }
  12229. }
  12230. /* Test bad args. */
  12231. if (!flag) {
  12232. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12233. if (ret != BAD_FUNC_ARG) {
  12234. flag = WOLFSSL_FATAL_ERROR;
  12235. }
  12236. }
  12237. if (!flag) {
  12238. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12239. if (ret != BAD_FUNC_ARG) {
  12240. flag = WOLFSSL_FATAL_ERROR;
  12241. }
  12242. }
  12243. if (!flag) {
  12244. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12245. if (ret != 0) {
  12246. flag = ret;
  12247. }
  12248. }
  12249. wc_HmacFree(&hmac);
  12250. printf(resultFmt, flag == 0 ? passed : failed);
  12251. #endif
  12252. return flag;
  12253. } /* END test_wc_Sha384HmacUpdate */
  12254. /*
  12255. * Testing wc_HmacFinal() with MD5
  12256. */
  12257. static int test_wc_Md5HmacFinal (void)
  12258. {
  12259. int flag = 0;
  12260. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12261. Hmac hmac;
  12262. byte hash[WC_MD5_DIGEST_SIZE];
  12263. testVector a;
  12264. int ret;
  12265. const char* key;
  12266. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12267. a.input = "Hi There";
  12268. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  12269. "\x9d";
  12270. a.inLen = XSTRLEN(a.input);
  12271. a.outLen = XSTRLEN(a.output);
  12272. flag = 0;
  12273. printf(testingFmt, "wc_HmacFinal() with MD5");
  12274. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12275. if (ret != 0)
  12276. return ret;
  12277. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  12278. if (ret != 0) {
  12279. flag = ret;
  12280. }
  12281. if (!flag) {
  12282. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12283. if (ret != 0) {
  12284. flag = ret;
  12285. }
  12286. }
  12287. if (!flag) {
  12288. ret = wc_HmacFinal(&hmac, hash);
  12289. if (ret != 0) {
  12290. flag = ret;
  12291. }
  12292. }
  12293. if (!flag) {
  12294. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  12295. flag = WOLFSSL_FATAL_ERROR;
  12296. }
  12297. }
  12298. /* Try bad parameters. */
  12299. if (!flag) {
  12300. ret = wc_HmacFinal(NULL, hash);
  12301. if (ret != BAD_FUNC_ARG) {
  12302. flag = WOLFSSL_FATAL_ERROR;
  12303. }
  12304. }
  12305. #ifndef HAVE_FIPS
  12306. if (!flag) {
  12307. ret = wc_HmacFinal(&hmac, NULL);
  12308. if (ret != BAD_FUNC_ARG) {
  12309. flag = WOLFSSL_FATAL_ERROR;
  12310. }
  12311. }
  12312. #endif
  12313. wc_HmacFree(&hmac);
  12314. printf(resultFmt, flag == 0 ? passed : failed);
  12315. #endif
  12316. return flag;
  12317. } /* END test_wc_Md5HmacFinal */
  12318. /*
  12319. * Testing wc_HmacFinal() with SHA
  12320. */
  12321. static int test_wc_ShaHmacFinal (void)
  12322. {
  12323. int flag = 0;
  12324. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12325. Hmac hmac;
  12326. byte hash[WC_SHA_DIGEST_SIZE];
  12327. testVector a;
  12328. int ret;
  12329. const char* key;
  12330. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12331. "\x0b\x0b\x0b";
  12332. a.input = "Hi There";
  12333. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  12334. "\x8e\xf1\x46\xbe\x00";
  12335. a.inLen = XSTRLEN(a.input);
  12336. a.outLen = XSTRLEN(a.output);
  12337. flag = 0;
  12338. printf(testingFmt, "wc_HmacFinal() with SHA");
  12339. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12340. if (ret != 0)
  12341. return ret;
  12342. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  12343. if (ret != 0) {
  12344. flag = ret;
  12345. }
  12346. if (!flag) {
  12347. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12348. if (ret != 0) {
  12349. flag = ret;
  12350. }
  12351. }
  12352. if (!flag) {
  12353. ret = wc_HmacFinal(&hmac, hash);
  12354. if (ret != 0) {
  12355. flag = ret;
  12356. }
  12357. }
  12358. if (!flag) {
  12359. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  12360. flag = WOLFSSL_FATAL_ERROR;
  12361. }
  12362. }
  12363. /* Try bad parameters. */
  12364. if (!flag) {
  12365. ret = wc_HmacFinal(NULL, hash);
  12366. if (ret != BAD_FUNC_ARG) {
  12367. flag = WOLFSSL_FATAL_ERROR;
  12368. }
  12369. }
  12370. #ifndef HAVE_FIPS
  12371. if (!flag) {
  12372. ret = wc_HmacFinal(&hmac, NULL);
  12373. if (ret != BAD_FUNC_ARG) {
  12374. flag = WOLFSSL_FATAL_ERROR;
  12375. }
  12376. }
  12377. #endif
  12378. wc_HmacFree(&hmac);
  12379. printf(resultFmt, flag == 0 ? passed : failed);
  12380. #endif
  12381. return flag;
  12382. } /* END test_wc_ShaHmacFinal */
  12383. /*
  12384. * Testing wc_HmacFinal() with SHA224
  12385. */
  12386. static int test_wc_Sha224HmacFinal (void)
  12387. {
  12388. int flag = 0;
  12389. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12390. Hmac hmac;
  12391. byte hash[WC_SHA224_DIGEST_SIZE];
  12392. testVector a;
  12393. int ret;
  12394. const char* key;
  12395. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12396. "\x0b\x0b\x0b";
  12397. a.input = "Hi There";
  12398. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  12399. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  12400. a.inLen = XSTRLEN(a.input);
  12401. a.outLen = XSTRLEN(a.output);
  12402. flag = 0;
  12403. printf(testingFmt, "wc_HmacFinal() with SHA224");
  12404. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12405. if (ret != 0)
  12406. return ret;
  12407. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  12408. if (ret != 0) {
  12409. flag = ret;
  12410. }
  12411. if (!flag) {
  12412. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12413. if (ret != 0) {
  12414. flag = ret;
  12415. }
  12416. }
  12417. if (!flag) {
  12418. ret = wc_HmacFinal(&hmac, hash);
  12419. if (ret != 0) {
  12420. flag = ret;
  12421. }
  12422. }
  12423. if (!flag) {
  12424. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  12425. flag = WOLFSSL_FATAL_ERROR;
  12426. }
  12427. }
  12428. /* Try bad parameters. */
  12429. if (!flag) {
  12430. ret = wc_HmacFinal(NULL, hash);
  12431. if (ret != BAD_FUNC_ARG) {
  12432. flag = WOLFSSL_FATAL_ERROR;
  12433. }
  12434. }
  12435. #ifndef HAVE_FIPS
  12436. if (!flag) {
  12437. ret = wc_HmacFinal(&hmac, NULL);
  12438. if (ret != BAD_FUNC_ARG) {
  12439. flag = WOLFSSL_FATAL_ERROR;
  12440. }
  12441. }
  12442. #endif
  12443. wc_HmacFree(&hmac);
  12444. printf(resultFmt, flag == 0 ? passed : failed);
  12445. #endif
  12446. return flag;
  12447. } /* END test_wc_Sha224HmacFinal */
  12448. /*
  12449. * Testing wc_HmacFinal() with SHA256
  12450. */
  12451. static int test_wc_Sha256HmacFinal (void)
  12452. {
  12453. int flag = 0;
  12454. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12455. Hmac hmac;
  12456. byte hash[WC_SHA256_DIGEST_SIZE];
  12457. testVector a;
  12458. int ret;
  12459. const char* key;
  12460. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12461. "\x0b\x0b\x0b";
  12462. a.input = "Hi There";
  12463. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  12464. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  12465. "\xcf\xf7";
  12466. a.inLen = XSTRLEN(a.input);
  12467. a.outLen = XSTRLEN(a.output);
  12468. flag = 0;
  12469. printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
  12470. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12471. if (ret != 0)
  12472. return ret;
  12473. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  12474. if (ret != 0) {
  12475. flag = ret;
  12476. }
  12477. if (!flag) {
  12478. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12479. if (ret != 0) {
  12480. flag = ret;
  12481. }
  12482. }
  12483. if (!flag) {
  12484. ret = wc_HmacFinal(&hmac, hash);
  12485. if (ret != 0) {
  12486. flag = ret;
  12487. }
  12488. }
  12489. if (!flag) {
  12490. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  12491. flag = WOLFSSL_FATAL_ERROR;
  12492. }
  12493. }
  12494. /* Try bad parameters. */
  12495. if (!flag) {
  12496. ret = wc_HmacFinal(NULL, hash);
  12497. if (ret != BAD_FUNC_ARG) {
  12498. flag = WOLFSSL_FATAL_ERROR;
  12499. }
  12500. }
  12501. #ifndef HAVE_FIPS
  12502. if (!flag) {
  12503. ret = wc_HmacFinal(&hmac, NULL);
  12504. if (ret != BAD_FUNC_ARG) {
  12505. flag = WOLFSSL_FATAL_ERROR;
  12506. }
  12507. }
  12508. #endif
  12509. wc_HmacFree(&hmac);
  12510. printf(resultFmt, flag == 0 ? passed : failed);
  12511. #endif
  12512. return flag;
  12513. } /* END test_wc_Sha256HmacFinal */
  12514. /*
  12515. * Testing wc_HmacFinal() with SHA384
  12516. */
  12517. static int test_wc_Sha384HmacFinal (void)
  12518. {
  12519. int flag = 0;
  12520. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12521. Hmac hmac;
  12522. byte hash[WC_SHA384_DIGEST_SIZE];
  12523. testVector a;
  12524. int ret;
  12525. const char* key;
  12526. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12527. "\x0b\x0b\x0b";
  12528. a.input = "Hi There";
  12529. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  12530. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  12531. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  12532. "\xfa\x9c\xb6";
  12533. a.inLen = XSTRLEN(a.input);
  12534. a.outLen = XSTRLEN(a.output);
  12535. flag = 0;
  12536. printf(testingFmt, "wc_HmacFinal() with SHA384");
  12537. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12538. if (ret != 0)
  12539. return ret;
  12540. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  12541. if (ret != 0) {
  12542. flag = ret;
  12543. }
  12544. if (!flag) {
  12545. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12546. if (ret != 0) {
  12547. flag = ret;
  12548. }
  12549. }
  12550. if (!flag) {
  12551. ret = wc_HmacFinal(&hmac, hash);
  12552. if (ret != 0) {
  12553. flag = ret;
  12554. }
  12555. }
  12556. if (!flag) {
  12557. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  12558. flag = WOLFSSL_FATAL_ERROR;
  12559. }
  12560. }
  12561. /* Try bad parameters. */
  12562. if (!flag) {
  12563. ret = wc_HmacFinal(NULL, hash);
  12564. if (ret != BAD_FUNC_ARG) {
  12565. flag = WOLFSSL_FATAL_ERROR;
  12566. }
  12567. }
  12568. #ifndef HAVE_FIPS
  12569. if (!flag) {
  12570. ret = wc_HmacFinal(&hmac, NULL);
  12571. if (ret != BAD_FUNC_ARG) {
  12572. flag = WOLFSSL_FATAL_ERROR;
  12573. }
  12574. }
  12575. #endif
  12576. wc_HmacFree(&hmac);
  12577. printf(resultFmt, flag == 0 ? passed : failed);
  12578. #endif
  12579. return flag;
  12580. } /* END test_wc_Sha384HmacFinal */
  12581. /*
  12582. * Testing wc_InitCmac()
  12583. */
  12584. static int test_wc_InitCmac (void)
  12585. {
  12586. int ret = 0;
  12587. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  12588. Cmac cmac1, cmac2, cmac3;
  12589. /* AES 128 key. */
  12590. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  12591. "\x09\x10\x11\x12\x13\x14\x15\x16";
  12592. /* AES 192 key. */
  12593. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  12594. "\x09\x01\x11\x12\x13\x14\x15\x16"
  12595. "\x01\x02\x03\x04\x05\x06\x07\x08";
  12596. /* AES 256 key. */
  12597. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  12598. "\x09\x01\x11\x12\x13\x14\x15\x16"
  12599. "\x01\x02\x03\x04\x05\x06\x07\x08"
  12600. "\x09\x01\x11\x12\x13\x14\x15\x16";
  12601. word32 key1Sz = (word32)sizeof(key1) - 1;
  12602. word32 key2Sz = (word32)sizeof(key2) - 1;
  12603. word32 key3Sz = (word32)sizeof(key3) - 1;
  12604. int type = WC_CMAC_AES;
  12605. printf(testingFmt, "wc_InitCmac()");
  12606. #ifdef WOLFSSL_AES_128
  12607. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  12608. #endif
  12609. #ifdef WOLFSSL_AES_192
  12610. if (ret == 0)
  12611. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  12612. #endif
  12613. #ifdef WOLFSSL_AES_256
  12614. if (ret == 0)
  12615. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  12616. #endif
  12617. /* Test bad args. */
  12618. if (ret == 0) {
  12619. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  12620. if (ret == BAD_FUNC_ARG) {
  12621. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  12622. }
  12623. if (ret == BAD_FUNC_ARG) {
  12624. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  12625. }
  12626. if (ret == BAD_FUNC_ARG) {
  12627. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  12628. }
  12629. if (ret == BAD_FUNC_ARG) {
  12630. ret = 0;
  12631. } else {
  12632. ret = WOLFSSL_FATAL_ERROR;
  12633. }
  12634. }
  12635. (void)key1;
  12636. (void)key1Sz;
  12637. (void)key2;
  12638. (void)key2Sz;
  12639. (void)cmac1;
  12640. (void)cmac2;
  12641. printf(resultFmt, ret == 0 ? passed : failed);
  12642. #endif
  12643. return ret;
  12644. } /* END test_wc_InitCmac */
  12645. /*
  12646. * Testing wc_CmacUpdate()
  12647. */
  12648. static int test_wc_CmacUpdate (void)
  12649. {
  12650. int ret = 0;
  12651. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  12652. Cmac cmac;
  12653. byte key[] =
  12654. {
  12655. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  12656. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  12657. };
  12658. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  12659. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  12660. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  12661. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  12662. "\xf4";
  12663. word32 inSz = (word32)sizeof(in) - 1;
  12664. word32 keySz = (word32)sizeof(key);
  12665. int type = WC_CMAC_AES;
  12666. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  12667. if (ret != 0) {
  12668. return ret;
  12669. }
  12670. printf(testingFmt, "wc_CmacUpdate()");
  12671. ret = wc_CmacUpdate(&cmac, in, inSz);
  12672. /* Test bad args. */
  12673. if (ret == 0) {
  12674. ret = wc_CmacUpdate(NULL, in, inSz);
  12675. if (ret == BAD_FUNC_ARG) {
  12676. ret = wc_CmacUpdate(&cmac, NULL, 30);
  12677. }
  12678. if (ret == BAD_FUNC_ARG) {
  12679. ret = 0;
  12680. } else if (ret == 0) {
  12681. ret = WOLFSSL_FATAL_ERROR;
  12682. }
  12683. }
  12684. printf(resultFmt, ret == 0 ? passed : failed);
  12685. #endif
  12686. return ret;
  12687. } /* END test_wc_CmacUpdate */
  12688. /*
  12689. * Testing wc_CmacFinal()
  12690. */
  12691. static int test_wc_CmacFinal (void)
  12692. {
  12693. int ret = 0;
  12694. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  12695. Cmac cmac;
  12696. byte key[] =
  12697. {
  12698. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  12699. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  12700. };
  12701. byte msg[] =
  12702. {
  12703. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  12704. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  12705. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  12706. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  12707. 0xf4
  12708. };
  12709. /* Test vectors from CMACGenAES128.rsp from
  12710. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  12711. * Per RFC4493 truncation of lsb is possible.
  12712. */
  12713. byte expMac[] =
  12714. {
  12715. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  12716. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  12717. };
  12718. byte mac[AES_BLOCK_SIZE];
  12719. word32 msgSz = (word32)sizeof(msg);
  12720. word32 keySz = (word32)sizeof(key);
  12721. word32 macSz = sizeof(mac);
  12722. word32 badMacSz = 17;
  12723. int expMacSz = sizeof(expMac);
  12724. int type = WC_CMAC_AES;
  12725. XMEMSET(mac, 0, macSz);
  12726. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  12727. if (ret != 0) {
  12728. return ret;
  12729. }
  12730. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  12731. printf(testingFmt, "wc_CmacFinal()");
  12732. if (ret == 0) {
  12733. ret = wc_CmacFinal(&cmac, mac, &macSz);
  12734. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  12735. ret = WOLFSSL_FATAL_ERROR;
  12736. }
  12737. /* Pass in bad args. */
  12738. if (ret == 0) {
  12739. ret = wc_CmacFinal(NULL, mac, &macSz);
  12740. if (ret == BAD_FUNC_ARG) {
  12741. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  12742. }
  12743. if (ret == BAD_FUNC_ARG) {
  12744. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  12745. if (ret == BUFFER_E) {
  12746. ret = 0;
  12747. }
  12748. } else if (ret == 0) {
  12749. ret = WOLFSSL_FATAL_ERROR;
  12750. }
  12751. }
  12752. }
  12753. printf(resultFmt, ret == 0 ? passed : failed);
  12754. #endif
  12755. return ret;
  12756. } /* END test_wc_CmacFinal */
  12757. /*
  12758. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  12759. */
  12760. static int test_wc_AesCmacGenerate (void)
  12761. {
  12762. int ret = 0;
  12763. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  12764. Cmac cmac;
  12765. byte key[] =
  12766. {
  12767. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  12768. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  12769. };
  12770. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  12771. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  12772. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  12773. "\x3d\x32\x65\x4c\x66\x23\xc5";
  12774. byte mac[AES_BLOCK_SIZE];
  12775. word32 keySz = sizeof(key);
  12776. word32 macSz = sizeof(mac);
  12777. word32 msgSz = sizeof(msg) - 1;
  12778. word32 expMacSz = sizeof(expMac) - 1;
  12779. int type = WC_CMAC_AES;
  12780. XMEMSET(mac, 0, macSz);
  12781. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  12782. if (ret != 0) {
  12783. return ret;
  12784. }
  12785. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  12786. if (ret != 0) {
  12787. return ret;
  12788. }
  12789. printf(testingFmt, "wc_AesCmacGenerate()");
  12790. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  12791. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  12792. ret = WOLFSSL_FATAL_ERROR;
  12793. }
  12794. /* Pass in bad args. */
  12795. if (ret == 0) {
  12796. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  12797. if (ret == BAD_FUNC_ARG) {
  12798. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  12799. }
  12800. if (ret == BAD_FUNC_ARG) {
  12801. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  12802. }
  12803. if (ret == BAD_FUNC_ARG) {
  12804. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  12805. }
  12806. if (ret == BAD_FUNC_ARG) {
  12807. ret = 0;
  12808. } else if (ret == 0) {
  12809. ret = WOLFSSL_FATAL_ERROR;
  12810. }
  12811. }
  12812. printf(resultFmt, ret == 0 ? passed : failed);
  12813. if (ret == 0) {
  12814. printf(testingFmt, "wc_AesCmacVerify()");
  12815. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  12816. /* Test bad args. */
  12817. if (ret == 0) {
  12818. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  12819. if (ret == BAD_FUNC_ARG) {
  12820. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  12821. }
  12822. if (ret == BAD_FUNC_ARG) {
  12823. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  12824. }
  12825. if (ret == BAD_FUNC_ARG) {
  12826. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  12827. }
  12828. if (ret == BAD_FUNC_ARG) {
  12829. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  12830. }
  12831. if (ret == BAD_FUNC_ARG) {
  12832. ret = 0;
  12833. } else if (ret == 0) {
  12834. ret = WOLFSSL_FATAL_ERROR;
  12835. }
  12836. }
  12837. printf(resultFmt, ret == 0 ? passed : failed);
  12838. }
  12839. #endif
  12840. return ret;
  12841. } /* END test_wc_AesCmacGenerate */
  12842. /*
  12843. * Testing streaming AES-GCM API.
  12844. */
  12845. static int test_wc_AesGcmStream (void)
  12846. {
  12847. int ret = 0;
  12848. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  12849. defined(WOLFSSL_AESGCM_STREAM)
  12850. int i;
  12851. WC_RNG rng[1];
  12852. Aes aesEnc[1];
  12853. Aes aesDec[1];
  12854. byte tag[AES_BLOCK_SIZE];
  12855. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  12856. byte out[AES_BLOCK_SIZE * 3 + 2];
  12857. byte plain[AES_BLOCK_SIZE * 3 + 2];
  12858. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  12859. byte key[AES_128_KEY_SIZE] = { 0, };
  12860. byte iv[AES_IV_SIZE] = { 1, };
  12861. byte ivOut[AES_IV_SIZE];
  12862. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  12863. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  12864. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  12865. };
  12866. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  12867. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  12868. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  12869. };
  12870. static const byte expTag[AES_BLOCK_SIZE] = {
  12871. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  12872. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  12873. };
  12874. /* Create a random for generating IV/nonce. */
  12875. AssertIntEQ(wc_InitRng(rng), 0);
  12876. /* Initialize data structures. */
  12877. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  12878. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  12879. /* BadParameters to streaming init. */
  12880. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  12881. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  12882. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  12883. BAD_FUNC_ARG);
  12884. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  12885. BAD_FUNC_ARG);
  12886. /* Bad parameters to encrypt update. */
  12887. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  12888. BAD_FUNC_ARG);
  12889. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  12890. BAD_FUNC_ARG);
  12891. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  12892. BAD_FUNC_ARG);
  12893. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  12894. BAD_FUNC_ARG);
  12895. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  12896. BAD_FUNC_ARG);
  12897. /* Bad parameters to decrypt update. */
  12898. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  12899. BAD_FUNC_ARG);
  12900. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  12901. BAD_FUNC_ARG);
  12902. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  12903. BAD_FUNC_ARG);
  12904. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  12905. BAD_FUNC_ARG);
  12906. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  12907. BAD_FUNC_ARG);
  12908. /* Bad parameters to encrypt final. */
  12909. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  12910. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  12911. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  12912. BAD_FUNC_ARG);
  12913. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  12914. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  12915. BAD_FUNC_ARG);
  12916. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  12917. BAD_FUNC_ARG);
  12918. /* Bad parameters to decrypt final. */
  12919. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  12920. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  12921. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  12922. BAD_FUNC_ARG);
  12923. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  12924. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  12925. BAD_FUNC_ARG);
  12926. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  12927. BAD_FUNC_ARG);
  12928. /* Check calling final before setting key fails. */
  12929. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  12930. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  12931. /* Check calling update before setting key else fails. */
  12932. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  12933. MISSING_KEY);
  12934. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  12935. MISSING_KEY);
  12936. /* Set key but not IV. */
  12937. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  12938. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  12939. /* Check calling final before setting IV fails. */
  12940. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  12941. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  12942. /* Check calling update before setting IV else fails. */
  12943. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  12944. MISSING_IV);
  12945. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  12946. MISSING_IV);
  12947. /* Set IV using fixed part IV and external IV APIs. */
  12948. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  12949. rng), 0);
  12950. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  12951. GCM_NONCE_MID_SZ), 0);
  12952. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  12953. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  12954. /* Encrypt and decrypt data. */
  12955. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  12956. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  12957. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  12958. /* Finalize and check tag matches. */
  12959. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  12960. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  12961. /* Set key and IV through streaming init API. */
  12962. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  12963. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  12964. /* Encrypt/decrypt one block and AAD of one block. */
  12965. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  12966. AES_BLOCK_SIZE), 0);
  12967. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  12968. AES_BLOCK_SIZE), 0);
  12969. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  12970. /* Finalize and check tag matches. */
  12971. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  12972. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  12973. /* Set key and IV through streaming init API. */
  12974. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  12975. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  12976. /* No data to encrypt/decrypt one byte of AAD. */
  12977. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  12978. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  12979. /* Finalize and check tag matches. */
  12980. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  12981. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  12982. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  12983. /* Set key and IV through streaming init API. */
  12984. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  12985. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  12986. /* Encrypt/decrypt one byte and no AAD. */
  12987. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  12988. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  12989. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  12990. /* Finalize and check tag matches. */
  12991. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  12992. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  12993. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  12994. /* Set key and IV through streaming init API. */
  12995. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  12996. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  12997. /* Encryption AES is one byte at a time */
  12998. for (i = 0; i < (int)sizeof(aad); i++) {
  12999. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13000. 0);
  13001. }
  13002. for (i = 0; i < (int)sizeof(in); i++) {
  13003. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13004. 0);
  13005. }
  13006. /* Decryption AES is two bytes at a time */
  13007. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13008. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13009. 0);
  13010. }
  13011. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13012. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13013. 0), 0);
  13014. }
  13015. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13016. /* Finalize and check tag matches. */
  13017. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13018. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13019. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13020. /* Check streaming encryption can be decrypted with one shot. */
  13021. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13022. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13023. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13024. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13025. wc_AesFree(aesEnc);
  13026. wc_AesFree(aesDec);
  13027. wc_FreeRng(rng);
  13028. #endif
  13029. return ret;
  13030. } /* END test_wc_AesGcmStream */
  13031. /*
  13032. * unit test for wc_Des3_SetIV()
  13033. */
  13034. static int test_wc_Des3_SetIV (void)
  13035. {
  13036. int ret = 0;
  13037. #ifndef NO_DES3
  13038. Des3 des;
  13039. const byte key[] =
  13040. {
  13041. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13042. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13043. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13044. };
  13045. const byte iv[] =
  13046. {
  13047. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13048. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13049. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13050. };
  13051. printf(testingFmt, "wc_Des3_SetIV()");
  13052. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13053. if (ret != 0)
  13054. return ret;
  13055. /* DES_ENCRYPTION or DES_DECRYPTION */
  13056. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13057. if (ret == 0) {
  13058. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13059. ret = WOLFSSL_FATAL_ERROR;
  13060. }
  13061. }
  13062. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  13063. /* Test explicitly wc_Des3_SetIV() */
  13064. if (ret == 0) {
  13065. ret = wc_Des3_SetIV(NULL, iv);
  13066. if (ret == BAD_FUNC_ARG) {
  13067. ret = wc_Des3_SetIV(&des, NULL);
  13068. } else if (ret == 0) {
  13069. ret = WOLFSSL_FATAL_ERROR;
  13070. }
  13071. }
  13072. #endif
  13073. wc_Des3Free(&des);
  13074. printf(resultFmt, ret == 0 ? passed : failed);
  13075. #endif
  13076. return ret;
  13077. } /* END test_wc_Des3_SetIV */
  13078. /*
  13079. * unit test for wc_Des3_SetKey()
  13080. */
  13081. static int test_wc_Des3_SetKey (void)
  13082. {
  13083. int ret = 0;
  13084. #ifndef NO_DES3
  13085. Des3 des;
  13086. const byte key[] =
  13087. {
  13088. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13089. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13090. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13091. };
  13092. const byte iv[] =
  13093. {
  13094. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13095. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13096. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13097. };
  13098. printf(testingFmt, "wc_Des3_SetKey()");
  13099. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13100. if (ret != 0)
  13101. return ret;
  13102. /* DES_ENCRYPTION or DES_DECRYPTION */
  13103. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13104. if (ret == 0) {
  13105. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13106. ret = WOLFSSL_FATAL_ERROR;
  13107. }
  13108. }
  13109. /* Test bad args. */
  13110. if (ret == 0) {
  13111. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  13112. if (ret == BAD_FUNC_ARG) {
  13113. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  13114. }
  13115. if (ret == BAD_FUNC_ARG) {
  13116. ret = wc_Des3_SetKey(&des, key, iv, -1);
  13117. }
  13118. if (ret == BAD_FUNC_ARG) {
  13119. /* Default case. Should return 0. */
  13120. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  13121. }
  13122. } /* END if ret != 0 */
  13123. wc_Des3Free(&des);
  13124. printf(resultFmt, ret == 0 ? passed : failed);
  13125. #endif
  13126. return ret;
  13127. } /* END test_wc_Des3_SetKey */
  13128. /*
  13129. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  13130. */
  13131. static int test_wc_Des3_CbcEncryptDecrypt (void)
  13132. {
  13133. int ret = 0;
  13134. #ifndef NO_DES3
  13135. Des3 des;
  13136. byte cipher[24];
  13137. byte plain[24];
  13138. const byte key[] =
  13139. {
  13140. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13141. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13142. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13143. };
  13144. const byte iv[] =
  13145. {
  13146. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13147. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13148. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13149. };
  13150. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  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. printf(testingFmt, "wc_Des3_CbcEncrypt()");
  13156. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13157. if (ret != 0)
  13158. return ret;
  13159. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13160. if (ret == 0) {
  13161. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  13162. if (ret == 0) {
  13163. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  13164. }
  13165. if (ret == 0) {
  13166. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  13167. }
  13168. }
  13169. if (ret == 0) {
  13170. if (XMEMCMP(plain, vector, 24) != 0) {
  13171. ret = WOLFSSL_FATAL_ERROR;
  13172. }
  13173. }
  13174. /* Pass in bad args. */
  13175. if (ret == 0) {
  13176. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  13177. if (ret == BAD_FUNC_ARG) {
  13178. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  13179. }
  13180. if (ret == BAD_FUNC_ARG) {
  13181. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  13182. }
  13183. if (ret != BAD_FUNC_ARG) {
  13184. ret = WOLFSSL_FATAL_ERROR;
  13185. } else {
  13186. ret = 0;
  13187. }
  13188. }
  13189. if (ret == 0) {
  13190. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  13191. if (ret == BAD_FUNC_ARG) {
  13192. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  13193. }
  13194. if (ret == BAD_FUNC_ARG) {
  13195. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  13196. }
  13197. if (ret != BAD_FUNC_ARG) {
  13198. ret = WOLFSSL_FATAL_ERROR;
  13199. } else {
  13200. ret = 0;
  13201. }
  13202. }
  13203. wc_Des3Free(&des);
  13204. printf(resultFmt, ret == 0 ? passed : failed);
  13205. #endif
  13206. return ret;
  13207. } /* END wc_Des3_CbcEncrypt */
  13208. /*
  13209. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  13210. */
  13211. static int test_wc_Des3_CbcEncryptDecryptWithKey (void)
  13212. {
  13213. int ret = 0;
  13214. #ifndef NO_DES3
  13215. word32 vectorSz, cipherSz;
  13216. byte cipher[24];
  13217. byte plain[24];
  13218. byte vector[] = /* Now is the time for all w/o trailing 0 */
  13219. {
  13220. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13221. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13222. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13223. };
  13224. byte key[] =
  13225. {
  13226. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13227. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13228. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13229. };
  13230. byte iv[] =
  13231. {
  13232. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13233. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13234. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13235. };
  13236. vectorSz = sizeof(byte) * 24;
  13237. cipherSz = sizeof(byte) * 24;
  13238. printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
  13239. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  13240. if (ret == 0) {
  13241. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  13242. if (ret == 0) {
  13243. if (XMEMCMP(plain, vector, 24) != 0) {
  13244. ret = WOLFSSL_FATAL_ERROR;
  13245. }
  13246. }
  13247. }
  13248. /* pass in bad args. */
  13249. if (ret == 0) {
  13250. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  13251. if (ret == BAD_FUNC_ARG) {
  13252. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  13253. }
  13254. if (ret == BAD_FUNC_ARG) {
  13255. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  13256. }
  13257. if (ret == BAD_FUNC_ARG) {
  13258. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  13259. key, NULL);
  13260. } else {
  13261. /* Return code catch. */
  13262. ret = WOLFSSL_FAILURE;
  13263. }
  13264. }
  13265. if (ret == 0) {
  13266. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  13267. if (ret == BAD_FUNC_ARG) {
  13268. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  13269. }
  13270. if (ret == BAD_FUNC_ARG) {
  13271. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  13272. }
  13273. if (ret == BAD_FUNC_ARG) {
  13274. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  13275. } else {
  13276. ret = WOLFSSL_FAILURE;
  13277. }
  13278. }
  13279. printf(resultFmt, ret == 0 ? passed : failed);
  13280. #endif
  13281. return ret;
  13282. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  13283. /*
  13284. * Unit test for wc_Des3_EcbEncrypt
  13285. */
  13286. static int test_wc_Des3_EcbEncrypt (void)
  13287. {
  13288. int ret = 0;
  13289. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  13290. Des3 des;
  13291. byte cipher[24];
  13292. word32 cipherSz = sizeof(cipher);
  13293. const byte key[] =
  13294. {
  13295. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13296. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13297. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13298. };
  13299. const byte iv[] =
  13300. {
  13301. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13302. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13303. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13304. };
  13305. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  13306. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13307. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13308. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13309. };
  13310. printf(testingFmt, "wc_Des3_EcbEncrypt()");
  13311. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13312. if (ret != 0) {
  13313. return ret;
  13314. }
  13315. if (ret == 0 ) {
  13316. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13317. }
  13318. /* Bad Cases */
  13319. if (ret == 0) {
  13320. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  13321. if (ret == BAD_FUNC_ARG) {
  13322. ret = 0;
  13323. }
  13324. }
  13325. if (ret == 0) {
  13326. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  13327. if (ret == BAD_FUNC_ARG) {
  13328. ret = 0;
  13329. }
  13330. }
  13331. if (ret == 0) {
  13332. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  13333. if (ret == BAD_FUNC_ARG) {
  13334. ret = 0;
  13335. }
  13336. }
  13337. if (ret == 0) {
  13338. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  13339. if (ret == BAD_FUNC_ARG) {
  13340. ret = 0;
  13341. }
  13342. }
  13343. if (ret == 0) {
  13344. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  13345. if (ret == BAD_FUNC_ARG) {
  13346. ret = 0;
  13347. }
  13348. }
  13349. /* Good Cases */
  13350. if (ret == 0) {
  13351. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  13352. }
  13353. wc_Des3Free(&des);
  13354. printf(resultFmt, ret == 0 ? passed : failed);
  13355. #endif
  13356. return ret;
  13357. } /* END test_wc_Des3_EcbEncrypt */
  13358. /*
  13359. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  13360. */
  13361. static int test_wc_Chacha_SetKey (void)
  13362. {
  13363. int ret = 0;
  13364. #ifdef HAVE_CHACHA
  13365. ChaCha ctx;
  13366. const byte key[] =
  13367. {
  13368. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13369. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13370. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13371. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13372. };
  13373. byte cipher[128];
  13374. printf(testingFmt, "wc_Chacha_SetKey()");
  13375. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  13376. /* Test bad args. */
  13377. if (ret == 0) {
  13378. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  13379. if (ret == BAD_FUNC_ARG) {
  13380. ret = wc_Chacha_SetKey(&ctx, key, 18);
  13381. }
  13382. if (ret == BAD_FUNC_ARG) {
  13383. ret = 0;
  13384. } else {
  13385. ret = WOLFSSL_FATAL_ERROR;
  13386. }
  13387. }
  13388. printf(resultFmt, ret == 0 ? passed : failed);
  13389. if (ret != 0) {
  13390. return ret;
  13391. }
  13392. printf(testingFmt, "wc_Chacha_SetIV");
  13393. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  13394. if (ret == 0) {
  13395. /* Test bad args. */
  13396. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  13397. if (ret == BAD_FUNC_ARG) {
  13398. ret = 0;
  13399. } else {
  13400. ret = WOLFSSL_FAILURE;
  13401. }
  13402. }
  13403. printf(resultFmt, ret == 0 ? passed : failed);
  13404. #endif
  13405. return ret;
  13406. } /* END test_wc_Chacha_SetKey */
  13407. /*
  13408. * unit test for wc_Poly1305SetKey()
  13409. */
  13410. static int test_wc_Poly1305SetKey(void)
  13411. {
  13412. int ret = 0;
  13413. #ifdef HAVE_POLY1305
  13414. Poly1305 ctx;
  13415. const byte key[] =
  13416. {
  13417. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13418. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13419. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13420. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13421. };
  13422. printf(testingFmt, "wc_Poly1305_SetKey()");
  13423. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  13424. /* Test bad args. */
  13425. if (ret == 0) {
  13426. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  13427. if(ret == BAD_FUNC_ARG) {
  13428. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  13429. }
  13430. if (ret == BAD_FUNC_ARG) {
  13431. ret = wc_Poly1305SetKey(&ctx, key, 18);
  13432. }
  13433. if (ret == BAD_FUNC_ARG) {
  13434. ret = 0;
  13435. } else {
  13436. ret = WOLFSSL_FATAL_ERROR;
  13437. }
  13438. }
  13439. printf(resultFmt, ret == 0 ? passed : failed);
  13440. #endif
  13441. return ret;
  13442. } /* END test_wc_Poly1305_SetKey() */
  13443. /*
  13444. * Testing wc_Chacha_Process()
  13445. */
  13446. static int test_wc_Chacha_Process (void)
  13447. {
  13448. int ret = 0;
  13449. #ifdef HAVE_CHACHA
  13450. ChaCha enc, dec;
  13451. byte cipher[128];
  13452. byte plain[128];
  13453. const byte key[] =
  13454. {
  13455. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13456. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13457. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13458. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13459. };
  13460. const char* input = "Everybody gets Friday off.";
  13461. word32 keySz = sizeof(key)/sizeof(byte);
  13462. unsigned long int inlen = XSTRLEN(input);
  13463. /*Initialize stack varialbes.*/
  13464. XMEMSET(cipher, 0, 128);
  13465. XMEMSET(plain, 0, 128);
  13466. printf(testingFmt, "wc_Chacha_Process()");
  13467. ret = wc_Chacha_SetKey(&enc, key, keySz);
  13468. AssertIntEQ(ret, 0);
  13469. ret = wc_Chacha_SetKey(&dec, key, keySz);
  13470. AssertIntEQ(ret, 0);
  13471. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  13472. AssertIntEQ(ret, 0);
  13473. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  13474. AssertIntEQ(ret, 0);
  13475. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  13476. AssertIntEQ(ret, 0);
  13477. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  13478. AssertIntEQ(ret, 0);
  13479. ret = XMEMCMP(input, plain, (int)inlen);
  13480. AssertIntEQ(ret, 0);
  13481. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  13482. /* test checking and using leftovers, currently just in C code */
  13483. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  13484. AssertIntEQ(ret, 0);
  13485. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  13486. AssertIntEQ(ret, 0);
  13487. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  13488. AssertIntEQ(ret, 0);
  13489. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  13490. (byte*)input + (inlen - 2), 2);
  13491. AssertIntEQ(ret, 0);
  13492. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  13493. AssertIntEQ(ret, 0);
  13494. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  13495. (byte*)input + (inlen - 2), 2);
  13496. AssertIntEQ(ret, 0);
  13497. ret = XMEMCMP(input, plain, (int)inlen);
  13498. AssertIntEQ(ret, 0);
  13499. /* check edge cases with counter increment */
  13500. {
  13501. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  13502. const byte expected[] = {
  13503. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  13504. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  13505. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  13506. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  13507. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  13508. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  13509. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  13510. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  13511. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  13512. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  13513. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  13514. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  13515. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  13516. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  13517. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  13518. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  13519. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  13520. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  13521. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  13522. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  13523. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  13524. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  13525. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  13526. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  13527. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  13528. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  13529. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  13530. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  13531. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  13532. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  13533. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  13534. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  13535. };
  13536. const byte iv2[] = {
  13537. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  13538. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  13539. };
  13540. byte input2[256];
  13541. int i;
  13542. for (i = 0; i < 256; i++)
  13543. input2[i] = i;
  13544. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  13545. AssertIntEQ(ret, 0);
  13546. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  13547. AssertIntEQ(ret, 0);
  13548. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  13549. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  13550. AssertIntEQ(ret, 0);
  13551. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  13552. /* partial */
  13553. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  13554. AssertIntEQ(ret, 0);
  13555. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  13556. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  13557. AssertIntEQ(ret, 0);
  13558. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  13559. }
  13560. #endif
  13561. /* Test bad args. */
  13562. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  13563. AssertIntEQ(ret, BAD_FUNC_ARG);
  13564. if (ret == BAD_FUNC_ARG) {
  13565. ret = 0;
  13566. }
  13567. printf(resultFmt, ret == 0 ? passed : failed);
  13568. #endif
  13569. return ret;
  13570. } /* END test_wc_Chacha_Process */
  13571. /*
  13572. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  13573. */
  13574. static int test_wc_ChaCha20Poly1305_aead (void)
  13575. {
  13576. int ret = 0;
  13577. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  13578. const byte key[] = {
  13579. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  13580. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  13581. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  13582. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  13583. };
  13584. const byte plaintext[] = {
  13585. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  13586. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  13587. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  13588. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  13589. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  13590. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  13591. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  13592. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  13593. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  13594. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  13595. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  13596. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  13597. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  13598. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  13599. 0x74, 0x2e
  13600. };
  13601. const byte iv[] = {
  13602. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  13603. 0x44, 0x45, 0x46, 0x47
  13604. };
  13605. const byte aad[] = { /* additional data */
  13606. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  13607. 0xc4, 0xc5, 0xc6, 0xc7
  13608. };
  13609. const byte cipher[] = { /* expected output from operation */
  13610. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  13611. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  13612. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  13613. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  13614. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  13615. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  13616. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  13617. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  13618. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  13619. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  13620. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  13621. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  13622. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  13623. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  13624. 0x61, 0x16
  13625. };
  13626. const byte authTag[] = { /* expected output from operation */
  13627. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  13628. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  13629. };
  13630. byte generatedCiphertext[272];
  13631. byte generatedPlaintext[272];
  13632. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  13633. /* Initialize stack variables. */
  13634. XMEMSET(generatedCiphertext, 0, 272);
  13635. XMEMSET(generatedPlaintext, 0, 272);
  13636. /* Test Encrypt */
  13637. printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
  13638. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  13639. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  13640. AssertIntEQ(ret, 0);
  13641. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  13642. AssertIntEQ(ret, 0);
  13643. /* Test bad args. */
  13644. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  13645. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  13646. AssertIntEQ(ret, BAD_FUNC_ARG);
  13647. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  13648. plaintext, sizeof(plaintext),
  13649. generatedCiphertext, generatedAuthTag);
  13650. AssertIntEQ(ret, BAD_FUNC_ARG);
  13651. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  13652. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  13653. AssertIntEQ(ret, BAD_FUNC_ARG);
  13654. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  13655. plaintext, 0, generatedCiphertext, generatedAuthTag);
  13656. AssertIntEQ(ret, BAD_FUNC_ARG);
  13657. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  13658. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  13659. AssertIntEQ(ret, BAD_FUNC_ARG);
  13660. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  13661. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  13662. if (ret == BAD_FUNC_ARG) {
  13663. ret = 0;
  13664. }
  13665. printf(resultFmt, ret == 0 ? passed : failed);
  13666. if (ret != 0) {
  13667. return ret;
  13668. }
  13669. printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
  13670. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  13671. sizeof(cipher), authTag, generatedPlaintext);
  13672. AssertIntEQ(ret, 0);
  13673. ret = XMEMCMP(generatedPlaintext, plaintext,
  13674. sizeof(plaintext)/sizeof(byte));
  13675. AssertIntEQ(ret, 0);
  13676. /* Test bad args. */
  13677. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  13678. sizeof(cipher), authTag, generatedPlaintext);
  13679. AssertIntEQ(ret, BAD_FUNC_ARG);
  13680. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  13681. cipher, sizeof(cipher), authTag, generatedPlaintext);
  13682. AssertIntEQ(ret, BAD_FUNC_ARG);
  13683. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  13684. sizeof(cipher), authTag, generatedPlaintext);
  13685. AssertIntEQ(ret, BAD_FUNC_ARG);
  13686. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  13687. sizeof(cipher), NULL, generatedPlaintext);
  13688. AssertIntEQ(ret, BAD_FUNC_ARG);
  13689. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  13690. sizeof(cipher), authTag, NULL);
  13691. AssertIntEQ(ret, BAD_FUNC_ARG);
  13692. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  13693. 0, authTag, generatedPlaintext);
  13694. AssertIntEQ(ret, BAD_FUNC_ARG);
  13695. if (ret == BAD_FUNC_ARG) {
  13696. ret = 0;
  13697. }
  13698. printf(resultFmt, ret == 0 ? passed : failed);
  13699. #endif
  13700. return ret;
  13701. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  13702. /*
  13703. * Testing function for wc_Rc2SetKey().
  13704. */
  13705. static int test_wc_Rc2SetKey(void)
  13706. {
  13707. int ret = 0;
  13708. #ifdef WC_RC2
  13709. Rc2 rc2;
  13710. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  13711. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  13712. printf(testingFmt, "wc_Rc2SetKey()");
  13713. /* valid key and IV */
  13714. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  13715. iv, 40);
  13716. if (ret == 0) {
  13717. /* valid key, no IV */
  13718. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  13719. NULL, 40);
  13720. }
  13721. /* bad arguments */
  13722. if (ret == 0) {
  13723. /* null Rc2 struct */
  13724. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  13725. iv, 40);
  13726. if (ret == BAD_FUNC_ARG) {
  13727. ret = 0;
  13728. }
  13729. }
  13730. if (ret == 0) {
  13731. /* null key */
  13732. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  13733. iv, 40);
  13734. if (ret == BAD_FUNC_ARG) {
  13735. ret = 0;
  13736. }
  13737. }
  13738. if (ret == 0) {
  13739. /* key size == 0 */
  13740. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  13741. if (ret == WC_KEY_SIZE_E) {
  13742. ret = 0;
  13743. }
  13744. }
  13745. if (ret == 0) {
  13746. /* key size > 128 */
  13747. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  13748. if (ret == WC_KEY_SIZE_E) {
  13749. ret = 0;
  13750. }
  13751. }
  13752. if (ret == 0) {
  13753. /* effective bits == 0 */
  13754. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  13755. iv, 0);
  13756. if (ret == WC_KEY_SIZE_E) {
  13757. ret = 0;
  13758. }
  13759. }
  13760. if (ret == 0) {
  13761. /* effective bits > 1024 */
  13762. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  13763. iv, 1025);
  13764. if (ret == WC_KEY_SIZE_E) {
  13765. ret = 0;
  13766. }
  13767. }
  13768. printf(resultFmt, ret == 0 ? passed : failed);
  13769. #endif
  13770. return ret;
  13771. } /* END test_wc_Rc2SetKey */
  13772. /*
  13773. * Testing function for wc_Rc2SetIV().
  13774. */
  13775. static int test_wc_Rc2SetIV(void)
  13776. {
  13777. int ret = 0;
  13778. #ifdef WC_RC2
  13779. Rc2 rc2;
  13780. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  13781. printf(testingFmt, "wc_Rc2SetIV()");
  13782. /* valid IV */
  13783. ret = wc_Rc2SetIV(&rc2, iv);
  13784. if (ret == 0) {
  13785. /* valid NULL IV */
  13786. ret = wc_Rc2SetIV(&rc2, NULL);
  13787. }
  13788. /* bad arguments */
  13789. if (ret == 0) {
  13790. ret = wc_Rc2SetIV(NULL, iv);
  13791. if (ret == BAD_FUNC_ARG) {
  13792. ret = 0;
  13793. }
  13794. }
  13795. printf(resultFmt, ret == 0 ? passed : failed);
  13796. #endif
  13797. return ret;
  13798. } /* END test_wc_Rc2SetKey */
  13799. /*
  13800. * Testing function for wc_Rc2EcbEncrypt().
  13801. */
  13802. static int test_wc_Rc2EcbEncryptDecrypt(void)
  13803. {
  13804. int ret = 0;
  13805. #ifdef WC_RC2
  13806. Rc2 rc2;
  13807. int effectiveKeyBits = 63;
  13808. byte cipher[RC2_BLOCK_SIZE];
  13809. byte plain[RC2_BLOCK_SIZE];
  13810. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  13811. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  13812. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  13813. printf(testingFmt, "wc_Rc2EcbEncryptDecrypt()");
  13814. XMEMSET(cipher, 0, sizeof(cipher));
  13815. XMEMSET(plain, 0, sizeof(plain));
  13816. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  13817. NULL, effectiveKeyBits);
  13818. if (ret == 0) {
  13819. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  13820. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  13821. ret = WOLFSSL_FATAL_ERROR;
  13822. }
  13823. if (ret == 0) {
  13824. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  13825. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  13826. ret = WOLFSSL_FATAL_ERROR;
  13827. }
  13828. }
  13829. }
  13830. /* Rc2EcbEncrypt bad arguments */
  13831. if (ret == 0) {
  13832. /* null Rc2 struct */
  13833. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  13834. if (ret == BAD_FUNC_ARG) {
  13835. ret = 0;
  13836. }
  13837. }
  13838. if (ret == 0) {
  13839. /* null out buffer */
  13840. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  13841. if (ret == BAD_FUNC_ARG) {
  13842. ret = 0;
  13843. }
  13844. }
  13845. if (ret == 0) {
  13846. /* null input buffer */
  13847. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  13848. if (ret == BAD_FUNC_ARG) {
  13849. ret = 0;
  13850. }
  13851. }
  13852. if (ret == 0) {
  13853. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  13854. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  13855. if (ret == BUFFER_E) {
  13856. ret = 0;
  13857. }
  13858. }
  13859. /* Rc2EcbDecrypt bad arguments */
  13860. if (ret == 0) {
  13861. /* null Rc2 struct */
  13862. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  13863. if (ret == BAD_FUNC_ARG) {
  13864. ret = 0;
  13865. }
  13866. }
  13867. if (ret == 0) {
  13868. /* null out buffer */
  13869. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  13870. if (ret == BAD_FUNC_ARG) {
  13871. ret = 0;
  13872. }
  13873. }
  13874. if (ret == 0) {
  13875. /* null input buffer */
  13876. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  13877. if (ret == BAD_FUNC_ARG) {
  13878. ret = 0;
  13879. }
  13880. }
  13881. if (ret == 0) {
  13882. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  13883. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  13884. if (ret == BUFFER_E) {
  13885. ret = 0;
  13886. }
  13887. }
  13888. printf(resultFmt, ret == 0 ? passed : failed);
  13889. #endif
  13890. return ret;
  13891. } /* END test_wc_Rc2SetKey */
  13892. /*
  13893. * Testing function for wc_Rc2CbcEncrypt().
  13894. */
  13895. static int test_wc_Rc2CbcEncryptDecrypt(void)
  13896. {
  13897. int ret = 0;
  13898. #ifdef WC_RC2
  13899. Rc2 rc2;
  13900. int effectiveKeyBits = 63;
  13901. byte cipher[RC2_BLOCK_SIZE*2];
  13902. byte plain[RC2_BLOCK_SIZE*2];
  13903. /* vector taken from test.c */
  13904. byte key[] = {
  13905. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  13906. };
  13907. byte iv[] = {
  13908. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  13909. };
  13910. byte input[] = {
  13911. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  13912. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  13913. };
  13914. byte output[] = {
  13915. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  13916. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  13917. };
  13918. printf(testingFmt, "wc_Rc2CbcEncryptDecrypt()");
  13919. XMEMSET(cipher, 0, sizeof(cipher));
  13920. XMEMSET(plain, 0, sizeof(plain));
  13921. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  13922. iv, effectiveKeyBits);
  13923. if (ret == 0) {
  13924. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  13925. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  13926. ret = WOLFSSL_FATAL_ERROR;
  13927. } else {
  13928. /* reset IV for decrypt */
  13929. ret = wc_Rc2SetIV(&rc2, iv);
  13930. }
  13931. if (ret == 0) {
  13932. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  13933. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  13934. ret = WOLFSSL_FATAL_ERROR;
  13935. }
  13936. }
  13937. }
  13938. /* Rc2CbcEncrypt bad arguments */
  13939. if (ret == 0) {
  13940. /* null Rc2 struct */
  13941. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  13942. if (ret == BAD_FUNC_ARG) {
  13943. ret = 0;
  13944. }
  13945. }
  13946. if (ret == 0) {
  13947. /* null out buffer */
  13948. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  13949. if (ret == BAD_FUNC_ARG) {
  13950. ret = 0;
  13951. }
  13952. }
  13953. if (ret == 0) {
  13954. /* null input buffer */
  13955. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  13956. if (ret == BAD_FUNC_ARG) {
  13957. ret = 0;
  13958. }
  13959. }
  13960. /* Rc2CbcDecrypt bad arguments */
  13961. if (ret == 0) {
  13962. /* in size is 0 */
  13963. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  13964. if (ret != 0) {
  13965. ret = WOLFSSL_FATAL_ERROR;
  13966. }
  13967. }
  13968. if (ret == 0) {
  13969. /* null Rc2 struct */
  13970. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  13971. if (ret == BAD_FUNC_ARG) {
  13972. ret = 0;
  13973. }
  13974. }
  13975. if (ret == 0) {
  13976. /* null out buffer */
  13977. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  13978. if (ret == BAD_FUNC_ARG) {
  13979. ret = 0;
  13980. }
  13981. }
  13982. if (ret == 0) {
  13983. /* null input buffer */
  13984. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  13985. if (ret == BAD_FUNC_ARG) {
  13986. ret = 0;
  13987. }
  13988. }
  13989. printf(resultFmt, ret == 0 ? passed : failed);
  13990. #endif
  13991. return ret;
  13992. } /* END test_wc_Rc2SetKey */
  13993. /*
  13994. * Testing function for wc_AesSetIV
  13995. */
  13996. static int test_wc_AesSetIV (void)
  13997. {
  13998. int ret = 0;
  13999. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14000. Aes aes;
  14001. byte key16[] =
  14002. {
  14003. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14004. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14005. };
  14006. byte iv1[] = "1234567890abcdef";
  14007. byte iv2[] = "0987654321fedcba";
  14008. printf(testingFmt, "wc_AesSetIV()");
  14009. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14010. if (ret != 0)
  14011. return ret;
  14012. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14013. iv1, AES_ENCRYPTION);
  14014. if(ret == 0) {
  14015. ret = wc_AesSetIV(&aes, iv2);
  14016. }
  14017. /* Test bad args. */
  14018. if(ret == 0) {
  14019. ret = wc_AesSetIV(NULL, iv1);
  14020. if(ret == BAD_FUNC_ARG) {
  14021. /* NULL iv should return 0. */
  14022. ret = wc_AesSetIV(&aes, NULL);
  14023. } else {
  14024. ret = WOLFSSL_FATAL_ERROR;
  14025. }
  14026. }
  14027. wc_AesFree(&aes);
  14028. printf(resultFmt, ret == 0 ? passed : failed);
  14029. #endif
  14030. return ret;
  14031. } /* test_wc_AesSetIV */
  14032. /*
  14033. * Testing function for wc_AesSetKey().
  14034. */
  14035. static int test_wc_AesSetKey (void)
  14036. {
  14037. int ret = 0;
  14038. #ifndef NO_AES
  14039. Aes aes;
  14040. byte key16[] =
  14041. {
  14042. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14043. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14044. };
  14045. #ifdef WOLFSSL_AES_192
  14046. byte key24[] =
  14047. {
  14048. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14049. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14050. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14051. };
  14052. #endif
  14053. #ifdef WOLFSSL_AES_256
  14054. byte key32[] =
  14055. {
  14056. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14057. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14058. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14059. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14060. };
  14061. #endif
  14062. byte badKey16[] =
  14063. {
  14064. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14065. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14066. };
  14067. byte iv[] = "1234567890abcdef";
  14068. printf(testingFmt, "wc_AesSetKey()");
  14069. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14070. if (ret != 0)
  14071. return ret;
  14072. #ifdef WOLFSSL_AES_128
  14073. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14074. iv, AES_ENCRYPTION);
  14075. #endif
  14076. #ifdef WOLFSSL_AES_192
  14077. if (ret == 0) {
  14078. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  14079. iv, AES_ENCRYPTION);
  14080. }
  14081. #endif
  14082. #ifdef WOLFSSL_AES_256
  14083. if (ret == 0) {
  14084. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  14085. iv, AES_ENCRYPTION);
  14086. }
  14087. #endif
  14088. /* Pass in bad args. */
  14089. if (ret == 0) {
  14090. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  14091. iv, AES_ENCRYPTION);
  14092. if (ret == BAD_FUNC_ARG) {
  14093. ret = wc_AesSetKey(&aes, badKey16,
  14094. (word32) sizeof(badKey16) / sizeof(byte),
  14095. iv, AES_ENCRYPTION);
  14096. }
  14097. if (ret == BAD_FUNC_ARG) {
  14098. ret = 0;
  14099. } else {
  14100. ret = WOLFSSL_FATAL_ERROR;
  14101. }
  14102. }
  14103. wc_AesFree(&aes);
  14104. printf(resultFmt, ret == 0 ? passed : failed);
  14105. #endif
  14106. return ret;
  14107. } /* END test_wc_AesSetKey */
  14108. /*
  14109. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  14110. * and wc_AesCbcDecryptWithKey()
  14111. */
  14112. static int test_wc_AesCbcEncryptDecrypt (void)
  14113. {
  14114. int ret = 0;
  14115. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  14116. defined(WOLFSSL_AES_256)
  14117. Aes aes;
  14118. byte key32[] =
  14119. {
  14120. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14121. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14122. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14123. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14124. };
  14125. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  14126. {
  14127. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14128. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14129. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  14130. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  14131. };
  14132. byte iv[] = "1234567890abcdef";
  14133. byte enc[sizeof(vector)];
  14134. byte dec[sizeof(vector)];
  14135. int cbcE = WOLFSSL_FATAL_ERROR;
  14136. int cbcD = WOLFSSL_FATAL_ERROR;
  14137. int cbcDWK = WOLFSSL_FATAL_ERROR;
  14138. byte dec2[sizeof(vector)];
  14139. /* Init stack variables. */
  14140. XMEMSET(enc, 0, sizeof(enc));
  14141. XMEMSET(dec, 0, sizeof(vector));
  14142. XMEMSET(dec2, 0, sizeof(vector));
  14143. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14144. if (ret != 0)
  14145. return ret;
  14146. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  14147. if (ret == 0) {
  14148. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  14149. if (ret == 0) {
  14150. /* Re init for decrypt and set flag. */
  14151. cbcE = 0;
  14152. wc_AesFree(&aes);
  14153. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  14154. iv, AES_DECRYPTION);
  14155. }
  14156. if (ret == 0) {
  14157. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  14158. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  14159. ret = WOLFSSL_FATAL_ERROR;
  14160. } else {
  14161. /* Set flag. */
  14162. cbcD = 0;
  14163. }
  14164. }
  14165. }
  14166. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  14167. if (ret == 0 || cbcE == 0) {
  14168. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14169. key32, sizeof(key32)/sizeof(byte), iv);
  14170. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  14171. cbcDWK = 0;
  14172. }
  14173. }
  14174. printf(testingFmt, "wc_AesCbcEncrypt()");
  14175. /* Pass in bad args */
  14176. if (cbcE == 0) {
  14177. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  14178. if (cbcE == BAD_FUNC_ARG) {
  14179. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  14180. }
  14181. if (cbcE == BAD_FUNC_ARG) {
  14182. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  14183. }
  14184. if (cbcE == BAD_FUNC_ARG) {
  14185. cbcE = 0;
  14186. } else {
  14187. cbcE = WOLFSSL_FATAL_ERROR;
  14188. }
  14189. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  14190. if (cbcE == 0) {
  14191. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  14192. }
  14193. if (cbcE == BAD_LENGTH_E) {
  14194. cbcE = 0;
  14195. } else {
  14196. cbcE = WOLFSSL_FATAL_ERROR;
  14197. }
  14198. #endif
  14199. }
  14200. if (cbcE == 0) {
  14201. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14202. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  14203. printf("Zero length inputs not supported with AESNI in FIPS mode (v2),"
  14204. " skip test");
  14205. #else
  14206. /* Test passing in size of 0 */
  14207. XMEMSET(enc, 0, sizeof(enc));
  14208. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  14209. if (cbcE == 0) {
  14210. /* Check enc was not modified */
  14211. int i;
  14212. for (i = 0; i < (int)sizeof(enc); i++)
  14213. cbcE |= enc[i];
  14214. }
  14215. #endif
  14216. }
  14217. printf(resultFmt, cbcE == 0 ? passed : failed);
  14218. if (cbcE != 0) {
  14219. wc_AesFree(&aes);
  14220. return cbcE;
  14221. }
  14222. printf(testingFmt, "wc_AesCbcDecrypt()");
  14223. if (cbcD == 0) {
  14224. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  14225. if (cbcD == BAD_FUNC_ARG) {
  14226. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  14227. }
  14228. if (cbcD == BAD_FUNC_ARG) {
  14229. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  14230. }
  14231. if (cbcD == BAD_FUNC_ARG) {
  14232. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  14233. }
  14234. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  14235. if (cbcD == BAD_LENGTH_E) {
  14236. cbcD = 0;
  14237. } else {
  14238. cbcD = WOLFSSL_FATAL_ERROR;
  14239. }
  14240. #else
  14241. if (cbcD == BAD_FUNC_ARG) {
  14242. cbcD = 0;
  14243. } else {
  14244. cbcD = WOLFSSL_FATAL_ERROR;
  14245. }
  14246. #endif
  14247. }
  14248. if (cbcD == 0) {
  14249. /* Test passing in size of 0 */
  14250. XMEMSET(dec, 0, sizeof(dec));
  14251. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  14252. if (cbcD == 0) {
  14253. /* Check dec was not modified */
  14254. int i;
  14255. for (i = 0; i < (int)sizeof(dec); i++)
  14256. cbcD |= dec[i];
  14257. }
  14258. }
  14259. printf(resultFmt, cbcD == 0 ? passed : failed);
  14260. if (cbcD != 0) {
  14261. wc_AesFree(&aes);
  14262. return cbcD;
  14263. }
  14264. printf(testingFmt, "wc_AesCbcDecryptWithKey()");
  14265. if (cbcDWK == 0) {
  14266. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  14267. key32, sizeof(key32)/sizeof(byte), iv);
  14268. if (cbcDWK == BAD_FUNC_ARG) {
  14269. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  14270. key32, sizeof(key32)/sizeof(byte), iv);
  14271. }
  14272. if (cbcDWK == BAD_FUNC_ARG) {
  14273. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14274. NULL, sizeof(key32)/sizeof(byte), iv);
  14275. }
  14276. if (cbcDWK == BAD_FUNC_ARG) {
  14277. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14278. key32, sizeof(key32)/sizeof(byte), NULL);
  14279. }
  14280. if (cbcDWK == BAD_FUNC_ARG) {
  14281. cbcDWK = 0;
  14282. } else {
  14283. cbcDWK = WOLFSSL_FATAL_ERROR;
  14284. }
  14285. }
  14286. wc_AesFree(&aes);
  14287. printf(resultFmt, cbcDWK == 0 ? passed : failed);
  14288. if (cbcDWK != 0) {
  14289. return cbcDWK;
  14290. }
  14291. #endif
  14292. return ret;
  14293. } /* END test_wc_AesCbcEncryptDecrypt */
  14294. /*
  14295. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  14296. */
  14297. static int test_wc_AesCtrEncryptDecrypt (void)
  14298. {
  14299. int ret = 0;
  14300. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  14301. Aes aesEnc, aesDec;
  14302. byte key32[] =
  14303. {
  14304. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14305. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14306. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14307. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14308. };
  14309. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14310. {
  14311. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14312. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14313. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14314. };
  14315. byte iv[] = "1234567890abcdef";
  14316. byte enc[AES_BLOCK_SIZE * 2];
  14317. byte dec[AES_BLOCK_SIZE * 2];
  14318. /* Init stack variables. */
  14319. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  14320. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  14321. printf(testingFmt, "wc_AesCtrEncrypt()");
  14322. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  14323. if (ret != 0)
  14324. return ret;
  14325. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  14326. if (ret != 0) {
  14327. wc_AesFree(&aesEnc);
  14328. return ret;
  14329. }
  14330. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  14331. iv, AES_ENCRYPTION);
  14332. if (ret == 0) {
  14333. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  14334. sizeof(vector)/sizeof(byte));
  14335. if (ret == 0) {
  14336. /* Decrypt with wc_AesCtrEncrypt() */
  14337. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  14338. iv, AES_ENCRYPTION);
  14339. }
  14340. if (ret == 0) {
  14341. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  14342. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  14343. ret = WOLFSSL_FATAL_ERROR;
  14344. }
  14345. }
  14346. }
  14347. /* Test bad args. */
  14348. if (ret == 0) {
  14349. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  14350. if (ret == BAD_FUNC_ARG) {
  14351. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  14352. }
  14353. if (ret == BAD_FUNC_ARG) {
  14354. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  14355. }
  14356. if (ret == BAD_FUNC_ARG) {
  14357. ret = 0;
  14358. } else {
  14359. ret = WOLFSSL_FATAL_ERROR;
  14360. }
  14361. }
  14362. wc_AesFree(&aesEnc);
  14363. wc_AesFree(&aesDec);
  14364. printf(resultFmt, ret == 0 ? passed : failed);
  14365. #endif
  14366. return ret;
  14367. } /* END test_wc_AesCtrEncryptDecrypt */
  14368. /*
  14369. * test function for wc_AesGcmSetKey()
  14370. */
  14371. static int test_wc_AesGcmSetKey (void)
  14372. {
  14373. int ret = 0;
  14374. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  14375. Aes aes;
  14376. #ifdef WOLFSSL_AES_128
  14377. byte key16[] =
  14378. {
  14379. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14380. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14381. };
  14382. #endif
  14383. #ifdef WOLFSSL_AES_192
  14384. byte key24[] =
  14385. {
  14386. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14387. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14388. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14389. };
  14390. #endif
  14391. #ifdef WOLFSSL_AES_256
  14392. byte key32[] =
  14393. {
  14394. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14395. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14396. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14397. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14398. };
  14399. #endif
  14400. byte badKey16[] =
  14401. {
  14402. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14403. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14404. };
  14405. byte badKey24[] =
  14406. {
  14407. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14408. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14409. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  14410. };
  14411. byte badKey32[] =
  14412. {
  14413. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  14414. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14415. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14416. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14417. };
  14418. printf(testingFmt, "wc_AesGcmSetKey()");
  14419. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14420. if (ret != 0)
  14421. return ret;
  14422. #ifdef WOLFSSL_AES_128
  14423. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  14424. #endif
  14425. #ifdef WOLFSSL_AES_192
  14426. if (ret == 0) {
  14427. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  14428. }
  14429. #endif
  14430. #ifdef WOLFSSL_AES_256
  14431. if (ret == 0) {
  14432. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  14433. }
  14434. #endif
  14435. /* Pass in bad args. */
  14436. if (ret == 0) {
  14437. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  14438. if (ret == BAD_FUNC_ARG) {
  14439. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  14440. }
  14441. if (ret == BAD_FUNC_ARG) {
  14442. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  14443. }
  14444. if (ret == BAD_FUNC_ARG) {
  14445. ret = 0;
  14446. } else {
  14447. ret = WOLFSSL_FATAL_ERROR;
  14448. }
  14449. }
  14450. wc_AesFree(&aes);
  14451. printf(resultFmt, ret == 0 ? passed : failed);
  14452. #endif
  14453. return ret;
  14454. } /* END test_wc_AesGcmSetKey */
  14455. /*
  14456. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  14457. */
  14458. static int test_wc_AesGcmEncryptDecrypt (void)
  14459. {
  14460. int ret = 0;
  14461. /* WOLFSSL_AFALG requires 12 byte IV */
  14462. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  14463. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  14464. Aes aes;
  14465. byte key32[] =
  14466. {
  14467. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14468. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14469. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14470. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14471. };
  14472. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14473. {
  14474. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14475. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14476. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14477. };
  14478. const byte a[] =
  14479. {
  14480. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  14481. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  14482. 0xab, 0xad, 0xda, 0xd2
  14483. };
  14484. byte iv[] = "1234567890a";
  14485. byte longIV[] = "1234567890abcdefghij";
  14486. byte enc[sizeof(vector)];
  14487. byte resultT[AES_BLOCK_SIZE];
  14488. byte dec[sizeof(vector)];
  14489. int gcmD = WOLFSSL_FATAL_ERROR;
  14490. int gcmE = WOLFSSL_FATAL_ERROR;
  14491. /* Init stack variables. */
  14492. XMEMSET(enc, 0, sizeof(vector));
  14493. XMEMSET(dec, 0, sizeof(vector));
  14494. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  14495. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14496. if (ret != 0)
  14497. return ret;
  14498. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  14499. if (ret == 0) {
  14500. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  14501. iv, sizeof(iv)/sizeof(byte), resultT,
  14502. sizeof(resultT), a, sizeof(a));
  14503. }
  14504. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  14505. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  14506. iv, sizeof(iv)/sizeof(byte), resultT,
  14507. sizeof(resultT), a, sizeof(a));
  14508. if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  14509. gcmD = WOLFSSL_FATAL_ERROR;
  14510. }
  14511. }
  14512. printf(testingFmt, "wc_AesGcmEncrypt()");
  14513. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  14514. if (gcmE == 0) {
  14515. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  14516. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  14517. a, sizeof(a));
  14518. if (gcmE == BAD_FUNC_ARG) {
  14519. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14520. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  14521. resultT, sizeof(resultT) + 1, a, sizeof(a));
  14522. }
  14523. if (gcmE == BAD_FUNC_ARG) {
  14524. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14525. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  14526. resultT, sizeof(resultT) - 5, a, sizeof(a));
  14527. }
  14528. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14529. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  14530. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14531. /* FIPS does not check the lower bound of ivSz */
  14532. #else
  14533. if (gcmE == BAD_FUNC_ARG) {
  14534. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14535. sizeof(vector), iv, 0,
  14536. resultT, sizeof(resultT), a, sizeof(a));
  14537. }
  14538. #endif
  14539. if (gcmE == BAD_FUNC_ARG) {
  14540. gcmE = 0;
  14541. } else {
  14542. gcmE = WOLFSSL_FATAL_ERROR;
  14543. }
  14544. }
  14545. /* This case is now considered good. Long IVs are now allowed.
  14546. * Except for the original FIPS release, it still has an upper
  14547. * bound on the IV length. */
  14548. #if (!defined(HAVE_FIPS) || \
  14549. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  14550. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14551. if (gcmE == 0) {
  14552. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  14553. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  14554. a, sizeof(a));
  14555. }
  14556. #else
  14557. (void)longIV;
  14558. #endif /* Old FIPS */
  14559. /* END wc_AesGcmEncrypt */
  14560. printf(resultFmt, gcmE == 0 ? passed : failed);
  14561. if (gcmE != 0) {
  14562. wc_AesFree(&aes);
  14563. return gcmE;
  14564. }
  14565. #ifdef HAVE_AES_DECRYPT
  14566. printf(testingFmt, "wc_AesGcmDecrypt()");
  14567. if (gcmD == 0) {
  14568. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  14569. iv, sizeof(iv)/sizeof(byte), resultT,
  14570. sizeof(resultT), a, sizeof(a));
  14571. if (gcmD == BAD_FUNC_ARG) {
  14572. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  14573. iv, sizeof(iv)/sizeof(byte), resultT,
  14574. sizeof(resultT), a, sizeof(a));
  14575. }
  14576. if (gcmD == BAD_FUNC_ARG) {
  14577. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  14578. iv, sizeof(iv)/sizeof(byte), resultT,
  14579. sizeof(resultT), a, sizeof(a));
  14580. }
  14581. if (gcmD == BAD_FUNC_ARG) {
  14582. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14583. NULL, sizeof(iv)/sizeof(byte), resultT,
  14584. sizeof(resultT), a, sizeof(a));
  14585. }
  14586. if (gcmD == BAD_FUNC_ARG) {
  14587. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14588. iv, sizeof(iv)/sizeof(byte), NULL,
  14589. sizeof(resultT), a, sizeof(a));
  14590. }
  14591. if (gcmD == BAD_FUNC_ARG) {
  14592. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14593. iv, sizeof(iv)/sizeof(byte), resultT,
  14594. sizeof(resultT) + 1, a, sizeof(a));
  14595. }
  14596. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14597. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  14598. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14599. /* FIPS does not check the lower bound of ivSz */
  14600. #else
  14601. if (gcmD == BAD_FUNC_ARG) {
  14602. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14603. iv, 0, resultT,
  14604. sizeof(resultT), a, sizeof(a));
  14605. }
  14606. #endif
  14607. if (gcmD == BAD_FUNC_ARG) {
  14608. gcmD = 0;
  14609. } else {
  14610. gcmD = WOLFSSL_FATAL_ERROR;
  14611. }
  14612. } /* END wc_AesGcmDecrypt */
  14613. printf(resultFmt, gcmD == 0 ? passed : failed);
  14614. #endif /* HAVE_AES_DECRYPT */
  14615. wc_AesFree(&aes);
  14616. #endif
  14617. return ret;
  14618. } /* END test_wc_AesGcmEncryptDecrypt */
  14619. /*
  14620. * unit test for wc_GmacSetKey()
  14621. */
  14622. static int test_wc_GmacSetKey (void)
  14623. {
  14624. int ret = 0;
  14625. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  14626. Gmac gmac;
  14627. byte key16[] =
  14628. {
  14629. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14630. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14631. };
  14632. #ifdef WOLFSSL_AES_192
  14633. byte key24[] =
  14634. {
  14635. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14636. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14637. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14638. };
  14639. #endif
  14640. #ifdef WOLFSSL_AES_256
  14641. byte key32[] =
  14642. {
  14643. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14644. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14645. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14646. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14647. };
  14648. #endif
  14649. byte badKey16[] =
  14650. {
  14651. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14652. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  14653. };
  14654. byte badKey24[] =
  14655. {
  14656. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  14657. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14658. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14659. };
  14660. byte badKey32[] =
  14661. {
  14662. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14663. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  14664. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14665. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14666. };
  14667. printf(testingFmt, "wc_GmacSetKey()");
  14668. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  14669. if (ret != 0)
  14670. return ret;
  14671. #ifdef WOLFSSL_AES_128
  14672. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  14673. #endif
  14674. #ifdef WOLFSSL_AES_192
  14675. if (ret == 0) {
  14676. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  14677. }
  14678. #endif
  14679. #ifdef WOLFSSL_AES_256
  14680. if (ret == 0) {
  14681. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  14682. }
  14683. #endif
  14684. /* Pass in bad args. */
  14685. if (ret == 0) {
  14686. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  14687. if (ret == BAD_FUNC_ARG) {
  14688. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  14689. }
  14690. if (ret == BAD_FUNC_ARG) {
  14691. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  14692. }
  14693. if (ret == BAD_FUNC_ARG) {
  14694. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  14695. }
  14696. if (ret == BAD_FUNC_ARG) {
  14697. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  14698. }
  14699. if (ret == BAD_FUNC_ARG) {
  14700. ret = 0;
  14701. } else {
  14702. ret = WOLFSSL_FATAL_ERROR;
  14703. }
  14704. }
  14705. wc_AesFree(&gmac.aes);
  14706. printf(resultFmt, ret == 0 ? passed : failed);
  14707. #endif
  14708. return ret;
  14709. } /* END test_wc_GmacSetKey */
  14710. /*
  14711. * unit test for wc_GmacUpdate
  14712. */
  14713. static int test_wc_GmacUpdate (void)
  14714. {
  14715. int ret = 0;
  14716. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  14717. Gmac gmac;
  14718. #ifdef WOLFSSL_AES_128
  14719. const byte key16[] =
  14720. {
  14721. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  14722. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  14723. };
  14724. #endif
  14725. #ifdef WOLFSSL_AES_192
  14726. byte key24[] =
  14727. {
  14728. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  14729. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  14730. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  14731. };
  14732. #endif
  14733. #ifdef WOLFSSL_AES_256
  14734. byte key32[] =
  14735. {
  14736. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  14737. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  14738. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  14739. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  14740. };
  14741. #endif
  14742. #ifdef WOLFSSL_AES_128
  14743. const byte authIn[] =
  14744. {
  14745. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  14746. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  14747. };
  14748. #endif
  14749. #ifdef WOLFSSL_AES_192
  14750. const byte authIn2[] =
  14751. {
  14752. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  14753. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  14754. };
  14755. #endif
  14756. const byte authIn3[] =
  14757. {
  14758. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  14759. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  14760. };
  14761. #ifdef WOLFSSL_AES_128
  14762. const byte tag1[] = /* Known. */
  14763. {
  14764. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  14765. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  14766. };
  14767. #endif
  14768. #ifdef WOLFSSL_AES_192
  14769. const byte tag2[] = /* Known */
  14770. {
  14771. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  14772. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  14773. };
  14774. #endif
  14775. const byte tag3[] = /* Known */
  14776. {
  14777. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  14778. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  14779. };
  14780. #ifdef WOLFSSL_AES_128
  14781. const byte iv[] =
  14782. {
  14783. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  14784. 0xe2, 0x8c, 0x8f, 0x16
  14785. };
  14786. #endif
  14787. #ifdef WOLFSSL_AES_192
  14788. const byte iv2[] =
  14789. {
  14790. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  14791. 0x7e, 0x1a, 0x6f, 0xbc
  14792. };
  14793. #endif
  14794. const byte iv3[] =
  14795. {
  14796. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  14797. 0xc3, 0xfb, 0x6c, 0x8a
  14798. };
  14799. byte tagOut[16];
  14800. byte tagOut2[24];
  14801. byte tagOut3[32];
  14802. /* Init stack variables. */
  14803. XMEMSET(tagOut, 0, sizeof(tagOut));
  14804. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  14805. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  14806. printf(testingFmt, "wc_GmacUpdate()");
  14807. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  14808. if (ret != 0)
  14809. return ret;
  14810. #ifdef WOLFSSL_AES_128
  14811. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  14812. if (ret == 0) {
  14813. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  14814. tagOut, sizeof(tag1));
  14815. if (ret == 0) {
  14816. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  14817. }
  14818. }
  14819. #endif
  14820. #ifdef WOLFSSL_AES_192
  14821. if (ret == 0) {
  14822. XMEMSET(&gmac, 0, sizeof(Gmac));
  14823. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  14824. }
  14825. if (ret == 0) {
  14826. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  14827. sizeof(authIn2), tagOut2, sizeof(tag2));
  14828. }
  14829. if (ret == 0) {
  14830. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  14831. }
  14832. #endif
  14833. #ifdef WOLFSSL_AES_256
  14834. if (ret == 0) {
  14835. XMEMSET(&gmac, 0, sizeof(Gmac));
  14836. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  14837. }
  14838. if (ret == 0) {
  14839. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  14840. sizeof(authIn3), tagOut3, sizeof(tag3));
  14841. }
  14842. if (ret == 0) {
  14843. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  14844. }
  14845. #endif
  14846. /*Pass bad args. */
  14847. if (ret == 0) {
  14848. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  14849. sizeof(authIn3), tagOut3, sizeof(tag3));
  14850. if (ret == BAD_FUNC_ARG) {
  14851. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  14852. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  14853. }
  14854. if (ret == BAD_FUNC_ARG) {
  14855. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  14856. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  14857. }
  14858. if (ret == BAD_FUNC_ARG) {
  14859. ret = 0;
  14860. } else {
  14861. ret = WOLFSSL_FATAL_ERROR;
  14862. }
  14863. }
  14864. wc_AesFree(&gmac.aes);
  14865. printf(resultFmt, ret == 0 ? passed : failed);
  14866. #endif
  14867. return ret;
  14868. } /* END test_wc_GmacUpdate */
  14869. /*
  14870. * testing wc_CamelliaSetKey
  14871. */
  14872. static int test_wc_CamelliaSetKey (void)
  14873. {
  14874. int ret = 0;
  14875. #ifdef HAVE_CAMELLIA
  14876. Camellia camellia;
  14877. /*128-bit key*/
  14878. static const byte key16[] =
  14879. {
  14880. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14881. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  14882. };
  14883. /* 192-bit key */
  14884. static const byte key24[] =
  14885. {
  14886. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14887. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14888. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  14889. };
  14890. /* 256-bit key */
  14891. static const byte key32[] =
  14892. {
  14893. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14894. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14895. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  14896. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  14897. };
  14898. static const byte iv[] =
  14899. {
  14900. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14901. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14902. };
  14903. printf(testingFmt, "wc_CamelliaSetKey()");
  14904. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  14905. if (ret == 0) {
  14906. ret = wc_CamelliaSetKey(&camellia, key16,
  14907. (word32)sizeof(key16), NULL);
  14908. if (ret == 0) {
  14909. ret = wc_CamelliaSetKey(&camellia, key24,
  14910. (word32)sizeof(key24), iv);
  14911. }
  14912. if (ret == 0) {
  14913. ret = wc_CamelliaSetKey(&camellia, key24,
  14914. (word32)sizeof(key24), NULL);
  14915. }
  14916. if (ret == 0) {
  14917. ret = wc_CamelliaSetKey(&camellia, key32,
  14918. (word32)sizeof(key32), iv);
  14919. }
  14920. if (ret == 0) {
  14921. ret = wc_CamelliaSetKey(&camellia, key32,
  14922. (word32)sizeof(key32), NULL);
  14923. }
  14924. }
  14925. /* Bad args. */
  14926. if (ret == 0) {
  14927. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  14928. if (ret != BAD_FUNC_ARG) {
  14929. ret = WOLFSSL_FATAL_ERROR;
  14930. } else {
  14931. ret = 0;
  14932. }
  14933. } /* END bad args. */
  14934. #endif
  14935. return ret;
  14936. } /* END test_wc_CammeliaSetKey */
  14937. /*
  14938. * Testing wc_CamelliaSetIV()
  14939. */
  14940. static int test_wc_CamelliaSetIV (void)
  14941. {
  14942. int ret = 0;
  14943. #ifdef HAVE_CAMELLIA
  14944. Camellia camellia;
  14945. static const byte iv[] =
  14946. {
  14947. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14948. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14949. };
  14950. printf(testingFmt, "wc_CamelliaSetIV()");
  14951. ret = wc_CamelliaSetIV(&camellia, iv);
  14952. if (ret == 0) {
  14953. ret = wc_CamelliaSetIV(&camellia, NULL);
  14954. }
  14955. /* Bad args. */
  14956. if (ret == 0) {
  14957. ret = wc_CamelliaSetIV(NULL, NULL);
  14958. if (ret != BAD_FUNC_ARG) {
  14959. ret = WOLFSSL_FATAL_ERROR;
  14960. } else {
  14961. ret = 0;
  14962. }
  14963. }
  14964. printf(resultFmt, ret == 0 ? passed : failed);
  14965. #endif
  14966. return ret;
  14967. } /*END test_wc_CamelliaSetIV*/
  14968. /*
  14969. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  14970. */
  14971. static int test_wc_CamelliaEncryptDecryptDirect (void)
  14972. {
  14973. int ret = 0;
  14974. #ifdef HAVE_CAMELLIA
  14975. Camellia camellia;
  14976. static const byte key24[] =
  14977. {
  14978. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  14979. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  14980. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  14981. };
  14982. static const byte iv[] =
  14983. {
  14984. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  14985. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  14986. };
  14987. static const byte plainT[] =
  14988. {
  14989. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  14990. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  14991. };
  14992. byte enc[sizeof(plainT)];
  14993. byte dec[sizeof(enc)];
  14994. int camE = WOLFSSL_FATAL_ERROR;
  14995. int camD = WOLFSSL_FATAL_ERROR;
  14996. /*Init stack variables.*/
  14997. XMEMSET(enc, 0, 16);
  14998. XMEMSET(enc, 0, 16);
  14999. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  15000. if (ret == 0) {
  15001. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  15002. if (ret == 0) {
  15003. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  15004. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15005. ret = WOLFSSL_FATAL_ERROR;
  15006. }
  15007. }
  15008. }
  15009. printf(testingFmt, "wc_CamelliaEncryptDirect()");
  15010. /* Pass bad args. */
  15011. if (ret == 0) {
  15012. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  15013. if (camE == BAD_FUNC_ARG) {
  15014. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  15015. }
  15016. if (camE == BAD_FUNC_ARG) {
  15017. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  15018. }
  15019. if (camE == BAD_FUNC_ARG) {
  15020. camE = 0;
  15021. } else {
  15022. camE = WOLFSSL_FATAL_ERROR;
  15023. }
  15024. }
  15025. printf(resultFmt, camE == 0 ? passed : failed);
  15026. if (camE != 0) {
  15027. return camE;
  15028. }
  15029. printf(testingFmt, "wc_CamelliaDecryptDirect()");
  15030. if (ret == 0) {
  15031. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  15032. if (camD == BAD_FUNC_ARG) {
  15033. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  15034. }
  15035. if (camD == BAD_FUNC_ARG) {
  15036. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  15037. }
  15038. if (camD == BAD_FUNC_ARG) {
  15039. camD = 0;
  15040. } else {
  15041. camD = WOLFSSL_FATAL_ERROR;
  15042. }
  15043. }
  15044. printf(resultFmt, camD == 0 ? passed : failed);
  15045. if (camD != 0) {
  15046. return camD;
  15047. }
  15048. #endif
  15049. return ret;
  15050. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15051. /*
  15052. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15053. */
  15054. static int test_wc_CamelliaCbcEncryptDecrypt (void)
  15055. {
  15056. int ret = 0;
  15057. #ifdef HAVE_CAMELLIA
  15058. Camellia camellia;
  15059. static const byte key24[] =
  15060. {
  15061. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15062. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15063. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15064. };
  15065. static const byte plainT[] =
  15066. {
  15067. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15068. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15069. };
  15070. byte enc[CAMELLIA_BLOCK_SIZE];
  15071. byte dec[CAMELLIA_BLOCK_SIZE];
  15072. int camCbcE = WOLFSSL_FATAL_ERROR;
  15073. int camCbcD = WOLFSSL_FATAL_ERROR;
  15074. /* Init stack variables. */
  15075. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15076. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15077. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15078. if (ret == 0) {
  15079. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15080. if (ret != 0) {
  15081. ret = WOLFSSL_FATAL_ERROR;
  15082. }
  15083. }
  15084. if (ret == 0) {
  15085. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15086. if (ret == 0) {
  15087. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  15088. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15089. ret = WOLFSSL_FATAL_ERROR;
  15090. }
  15091. }
  15092. }
  15093. printf(testingFmt, "wc_CamelliaCbcEncrypt");
  15094. /* Pass in bad args. */
  15095. if (ret == 0) {
  15096. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15097. if (camCbcE == BAD_FUNC_ARG) {
  15098. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15099. CAMELLIA_BLOCK_SIZE);
  15100. }
  15101. if (camCbcE == BAD_FUNC_ARG) {
  15102. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15103. CAMELLIA_BLOCK_SIZE);
  15104. }
  15105. if (camCbcE == BAD_FUNC_ARG) {
  15106. camCbcE = 0;
  15107. } else {
  15108. camCbcE = WOLFSSL_FATAL_ERROR;
  15109. }
  15110. }
  15111. printf(resultFmt, camCbcE == 0 ? passed : failed);
  15112. if (camCbcE != 0) {
  15113. return camCbcE;
  15114. }
  15115. printf(testingFmt, "wc_CamelliaCbcDecrypt()");
  15116. if (ret == 0) {
  15117. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  15118. if (camCbcD == BAD_FUNC_ARG) {
  15119. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15120. CAMELLIA_BLOCK_SIZE);
  15121. }
  15122. if (camCbcD == BAD_FUNC_ARG) {
  15123. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15124. CAMELLIA_BLOCK_SIZE);
  15125. }
  15126. if (camCbcD == BAD_FUNC_ARG) {
  15127. camCbcD = 0;
  15128. } else {
  15129. camCbcD = WOLFSSL_FATAL_ERROR;
  15130. }
  15131. } /* END bad args. */
  15132. printf(resultFmt, camCbcD == 0 ? passed : failed);
  15133. if (camCbcD != 0) {
  15134. return camCbcD;
  15135. }
  15136. #endif
  15137. return ret;
  15138. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  15139. /*
  15140. * Testing wc_RabbitSetKey()
  15141. */
  15142. static int test_wc_RabbitSetKey (void)
  15143. {
  15144. int ret = 0;
  15145. #ifndef NO_RABBIT
  15146. Rabbit rabbit;
  15147. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  15148. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  15149. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  15150. printf(testingFmt, "wc_RabbitSetKey()");
  15151. ret = wc_RabbitSetKey(&rabbit, (byte*)key, (byte*)iv);
  15152. /* Test bad args. */
  15153. if (ret == 0) {
  15154. ret = wc_RabbitSetKey(NULL, (byte*)key, (byte*)iv);
  15155. if (ret == BAD_FUNC_ARG) {
  15156. ret = wc_RabbitSetKey(&rabbit, NULL, (byte*)iv);
  15157. }
  15158. if (ret == BAD_FUNC_ARG) {
  15159. ret = wc_RabbitSetKey(&rabbit, (byte*)key, NULL);
  15160. }
  15161. }
  15162. printf(resultFmt, ret == 0 ? passed : failed);
  15163. #endif
  15164. return ret;
  15165. } /* END test_wc_RabbitSetKey */
  15166. /*
  15167. * Test wc_RabbitProcess()
  15168. */
  15169. static int test_wc_RabbitProcess (void)
  15170. {
  15171. int ret = 0;
  15172. #if !defined(NO_RABBIT) && !defined(BIG_ENDIAN_ORDER)
  15173. Rabbit enc, dec;
  15174. byte cipher[25];
  15175. byte plain[25];
  15176. const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
  15177. "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
  15178. const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
  15179. const char* input = TEST_STRING;
  15180. unsigned long int inlen = (unsigned long int)TEST_STRING_SZ;
  15181. /* Initialize stack variables. */
  15182. XMEMSET(cipher, 0, sizeof(cipher));
  15183. XMEMSET(plain, 0, sizeof(plain));
  15184. printf(testingFmt, "wc_RabbitProcess()");
  15185. ret = wc_RabbitSetKey(&enc, (byte*)key, (byte*)iv);
  15186. if (ret == 0) {
  15187. ret = wc_RabbitSetKey(&dec, (byte*)key, (byte*)iv);
  15188. }
  15189. if (ret == 0) {
  15190. ret = wc_RabbitProcess(&enc, cipher, (byte*)input, (word32)inlen);
  15191. }
  15192. if (ret == 0) {
  15193. ret = wc_RabbitProcess(&dec, plain, cipher, (word32)inlen);
  15194. if (ret != 0 || XMEMCMP(input, plain, inlen)) {
  15195. ret = WOLFSSL_FATAL_ERROR;
  15196. } else {
  15197. ret = 0;
  15198. }
  15199. }
  15200. /* Test bad args. */
  15201. if (ret == 0) {
  15202. ret = wc_RabbitProcess(NULL, plain, cipher, (word32)inlen);
  15203. if (ret == BAD_FUNC_ARG) {
  15204. ret = wc_RabbitProcess(&dec, NULL, cipher, (word32)inlen);
  15205. }
  15206. if (ret == BAD_FUNC_ARG) {
  15207. ret = wc_RabbitProcess(&dec, plain, NULL, (word32)inlen);
  15208. }
  15209. if (ret == BAD_FUNC_ARG) {
  15210. ret = 0;
  15211. } else {
  15212. ret = WOLFSSL_FATAL_ERROR;
  15213. }
  15214. }
  15215. printf(resultFmt, ret == 0 ? passed : failed);
  15216. #endif
  15217. return ret;
  15218. } /* END test_wc_RabbitProcess */
  15219. /*
  15220. * Testing wc_Arc4SetKey()
  15221. */
  15222. static int test_wc_Arc4SetKey (void)
  15223. {
  15224. int ret = 0;
  15225. #ifndef NO_RC4
  15226. Arc4 arc;
  15227. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15228. int keyLen = 8;
  15229. printf(testingFmt, "wc_Arch4SetKey()");
  15230. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  15231. /* Test bad args. */
  15232. if (ret == 0) {
  15233. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  15234. if (ret == BAD_FUNC_ARG)
  15235. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  15236. if (ret == BAD_FUNC_ARG)
  15237. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  15238. if (ret == BAD_FUNC_ARG)
  15239. ret = WOLFSSL_ERROR_NONE;
  15240. else
  15241. ret = WOLFSSL_FATAL_ERROR;
  15242. } /* END test bad args. */
  15243. printf(resultFmt, ret == 0 ? passed : failed);
  15244. #endif
  15245. return ret;
  15246. } /* END test_wc_Arc4SetKey */
  15247. /*
  15248. * Testing wc_Arc4Process for ENC/DEC.
  15249. */
  15250. static int test_wc_Arc4Process (void)
  15251. {
  15252. int ret = 0;
  15253. #ifndef NO_RC4
  15254. Arc4 enc, dec;
  15255. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15256. int keyLen = 8;
  15257. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15258. byte cipher[8];
  15259. byte plain[8];
  15260. /* Init stack variables */
  15261. XMEMSET(cipher, 0, sizeof(cipher));
  15262. XMEMSET(plain, 0, sizeof(plain));
  15263. /* Use for async. */
  15264. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  15265. if (ret == 0) {
  15266. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  15267. }
  15268. printf(testingFmt, "wc_Arc4Process()");
  15269. if (ret == 0) {
  15270. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  15271. }
  15272. if (ret == 0) {
  15273. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  15274. }
  15275. if (ret == 0) {
  15276. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  15277. }
  15278. if (ret == 0) {
  15279. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  15280. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  15281. ret = WOLFSSL_FATAL_ERROR;
  15282. } else {
  15283. ret = 0;
  15284. }
  15285. }
  15286. /* Bad args. */
  15287. if (ret == 0) {
  15288. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  15289. if (ret == BAD_FUNC_ARG) {
  15290. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  15291. }
  15292. if (ret == BAD_FUNC_ARG) {
  15293. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  15294. }
  15295. if (ret == BAD_FUNC_ARG) {
  15296. ret = 0;
  15297. } else {
  15298. ret = WOLFSSL_FATAL_ERROR;
  15299. }
  15300. }
  15301. printf(resultFmt, ret == 0 ? passed : failed);
  15302. wc_Arc4Free(&enc);
  15303. wc_Arc4Free(&dec);
  15304. #endif
  15305. return ret;
  15306. }/* END test_wc_Arc4Process */
  15307. /*
  15308. * Testing wc_Init RsaKey()
  15309. */
  15310. static int test_wc_InitRsaKey (void)
  15311. {
  15312. int ret = 0;
  15313. #ifndef NO_RSA
  15314. RsaKey key;
  15315. printf(testingFmt, "wc_InitRsaKey()");
  15316. ret = wc_InitRsaKey(&key, NULL);
  15317. /* Test bad args. */
  15318. if (ret == 0) {
  15319. ret = wc_InitRsaKey(NULL, NULL);
  15320. #ifndef HAVE_USER_RSA
  15321. if (ret == BAD_FUNC_ARG) {
  15322. ret = 0;
  15323. } else {
  15324. #else
  15325. if (ret == USER_CRYPTO_ERROR) {
  15326. ret = 0;
  15327. } else {
  15328. #endif
  15329. ret = WOLFSSL_FATAL_ERROR;
  15330. }
  15331. } /* end if */
  15332. if (wc_FreeRsaKey(&key) || ret != 0) {
  15333. ret = WOLFSSL_FATAL_ERROR;
  15334. }
  15335. printf(resultFmt, ret == 0 ? passed : failed);
  15336. #endif
  15337. return ret;
  15338. } /* END test_wc_InitRsaKey */
  15339. /*
  15340. * Testing wc_RsaPrivateKeyDecode()
  15341. */
  15342. static int test_wc_RsaPrivateKeyDecode (void)
  15343. {
  15344. int ret = 0;
  15345. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15346. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15347. RsaKey key;
  15348. byte* tmp;
  15349. word32 idx = 0;
  15350. int bytes = 0;
  15351. printf(testingFmt, "wc_RsaPrivateKeyDecode()");
  15352. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15353. if (tmp == NULL) {
  15354. ret = WOLFSSL_FATAL_ERROR;
  15355. }
  15356. if (ret == 0) {
  15357. ret = wc_InitRsaKey(&key, NULL);
  15358. }
  15359. if (ret == 0) {
  15360. #ifdef USE_CERT_BUFFERS_1024
  15361. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  15362. bytes = sizeof_client_key_der_1024;
  15363. #else
  15364. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  15365. bytes = sizeof_client_key_der_2048;
  15366. #endif /* Use cert buffers. */
  15367. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  15368. }
  15369. #ifndef HAVE_USER_RSA
  15370. /* Test bad args. */
  15371. if (ret == 0) {
  15372. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  15373. if (ret == BAD_FUNC_ARG) {
  15374. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  15375. }
  15376. if (ret == BAD_FUNC_ARG) {
  15377. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15378. }
  15379. if (ret == BAD_FUNC_ARG) {
  15380. ret = 0;
  15381. } else {
  15382. ret = WOLFSSL_FATAL_ERROR;
  15383. }
  15384. }
  15385. #else
  15386. /* Test bad args. User RSA. */
  15387. if (ret == 0) {
  15388. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  15389. if (ret == USER_CRYPTO_ERROR) {
  15390. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  15391. }
  15392. if (ret == USER_CRYPTO_ERROR) {
  15393. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15394. }
  15395. if (ret == USER_CRYPTO_ERROR) {
  15396. ret = 0;
  15397. } else {
  15398. ret = WOLFSSL_FATAL_ERROR;
  15399. }
  15400. }
  15401. #endif
  15402. if (tmp != NULL) {
  15403. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15404. }
  15405. if (wc_FreeRsaKey(&key) || ret != 0) {
  15406. ret = WOLFSSL_FATAL_ERROR;
  15407. }
  15408. printf(resultFmt, ret == 0 ? passed : failed);
  15409. #endif
  15410. return ret;
  15411. } /* END test_wc_RsaPrivateKeyDecode */
  15412. /*
  15413. * Testing wc_RsaPublicKeyDecode()
  15414. */
  15415. static int test_wc_RsaPublicKeyDecode (void)
  15416. {
  15417. int ret = 0;
  15418. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15419. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15420. RsaKey keyPub;
  15421. byte* tmp;
  15422. word32 idx = 0;
  15423. int bytes = 0;
  15424. word32 keySz = 0;
  15425. word32 tstKeySz = 0;
  15426. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15427. if (tmp == NULL) {
  15428. ret = WOLFSSL_FATAL_ERROR;
  15429. }
  15430. if (ret == 0) {
  15431. ret = wc_InitRsaKey(&keyPub, NULL);
  15432. }
  15433. if (ret == 0) {
  15434. #ifdef USE_CERT_BUFFERS_1024
  15435. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  15436. bytes = sizeof_client_keypub_der_1024;
  15437. keySz = 1024;
  15438. #else
  15439. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  15440. bytes = sizeof_client_keypub_der_2048;
  15441. keySz = 2048;
  15442. #endif
  15443. printf(testingFmt, "wc_RsaPublicKeyDecode()");
  15444. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  15445. }
  15446. #ifndef HAVE_USER_RSA
  15447. /* Pass in bad args. */
  15448. if (ret == 0) {
  15449. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  15450. if (ret == BAD_FUNC_ARG) {
  15451. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  15452. }
  15453. if (ret == BAD_FUNC_ARG) {
  15454. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15455. }
  15456. if (ret == BAD_FUNC_ARG) {
  15457. ret = 0;
  15458. } else {
  15459. ret = WOLFSSL_FATAL_ERROR;
  15460. }
  15461. }
  15462. #else
  15463. /* Pass in bad args. */
  15464. if (ret == 0) {
  15465. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  15466. if (ret == USER_CRYPTO_ERROR) {
  15467. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  15468. }
  15469. if (ret == USER_CRYPTO_ERROR) {
  15470. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15471. }
  15472. if (ret == USER_CRYPTO_ERROR) {
  15473. ret = 0;
  15474. } else {
  15475. ret = WOLFSSL_FATAL_ERROR;
  15476. }
  15477. }
  15478. #endif
  15479. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  15480. ret = WOLFSSL_FATAL_ERROR;
  15481. }
  15482. if (ret == 0) {
  15483. /* Test for getting modulus key size */
  15484. idx = 0;
  15485. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  15486. &tstKeySz, NULL, NULL);
  15487. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  15488. }
  15489. if (tmp != NULL) {
  15490. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15491. }
  15492. printf(resultFmt, ret == 0 ? passed : failed);
  15493. #endif
  15494. return ret;
  15495. } /* END test_wc_RsaPublicKeyDecode */
  15496. /*
  15497. * Testing wc_RsaPublicKeyDecodeRaw()
  15498. */
  15499. static int test_wc_RsaPublicKeyDecodeRaw (void)
  15500. {
  15501. int ret = 0;
  15502. #if !defined(NO_RSA)
  15503. RsaKey key;
  15504. const byte n = 0x23;
  15505. const byte e = 0x03;
  15506. int nSz = sizeof(n);
  15507. int eSz = sizeof(e);
  15508. printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
  15509. ret = wc_InitRsaKey(&key, NULL);
  15510. if (ret == 0) {
  15511. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  15512. }
  15513. #ifndef HAVE_USER_RSA
  15514. /* Pass in bad args. */
  15515. if (ret == 0) {
  15516. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  15517. if (ret == BAD_FUNC_ARG) {
  15518. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  15519. }
  15520. if (ret == BAD_FUNC_ARG) {
  15521. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  15522. }
  15523. if (ret == BAD_FUNC_ARG) {
  15524. ret = 0;
  15525. } else {
  15526. ret = WOLFSSL_FATAL_ERROR;
  15527. }
  15528. }
  15529. #else
  15530. /* Pass in bad args. User RSA. */
  15531. if (ret == 0) {
  15532. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  15533. if (ret == USER_CRYPTO_ERROR) {
  15534. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  15535. }
  15536. if (ret == USER_CRYPTO_ERROR) {
  15537. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  15538. }
  15539. if (ret == USER_CRYPTO_ERROR) {
  15540. ret = 0;
  15541. } else {
  15542. ret = WOLFSSL_FATAL_ERROR;
  15543. }
  15544. }
  15545. #endif
  15546. if (wc_FreeRsaKey(&key) || ret != 0) {
  15547. ret = WOLFSSL_FATAL_ERROR;
  15548. }
  15549. printf(resultFmt, ret == 0 ? passed : failed);
  15550. #endif
  15551. return ret;
  15552. } /* END test_wc_RsaPublicKeyDecodeRaw */
  15553. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  15554. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  15555. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  15556. * trying until it gets a probable prime. */
  15557. #ifdef HAVE_FIPS
  15558. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  15559. {
  15560. int ret;
  15561. for (;;) {
  15562. ret = wc_MakeRsaKey(key, size, e, rng);
  15563. if (ret != PRIME_GEN_E) break;
  15564. printf("MakeRsaKey couldn't find prime; trying again.\n");
  15565. }
  15566. return ret;
  15567. }
  15568. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  15569. #else
  15570. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  15571. #endif
  15572. #endif
  15573. /*
  15574. * Testing wc_MakeRsaKey()
  15575. */
  15576. static int test_wc_MakeRsaKey (void)
  15577. {
  15578. int ret = 0;
  15579. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15580. RsaKey genKey;
  15581. WC_RNG rng;
  15582. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  15583. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  15584. int bits = 1024;
  15585. #else
  15586. int bits = 2048;
  15587. #endif
  15588. printf(testingFmt, "wc_MakeRsaKey()");
  15589. ret = wc_InitRsaKey(&genKey, NULL);
  15590. if (ret == 0) {
  15591. ret = wc_InitRng(&rng);
  15592. if (ret == 0) {
  15593. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  15594. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  15595. ret = WOLFSSL_FATAL_ERROR;
  15596. }
  15597. }
  15598. }
  15599. #ifndef HAVE_USER_RSA
  15600. /* Test bad args. */
  15601. if (ret == 0) {
  15602. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  15603. if (ret == BAD_FUNC_ARG) {
  15604. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  15605. }
  15606. if (ret == BAD_FUNC_ARG) {
  15607. /* e < 3 */
  15608. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  15609. }
  15610. if (ret == BAD_FUNC_ARG) {
  15611. /* e & 1 == 0 */
  15612. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  15613. }
  15614. if (ret == BAD_FUNC_ARG) {
  15615. ret = 0;
  15616. } else {
  15617. ret = WOLFSSL_FATAL_ERROR;
  15618. }
  15619. }
  15620. #else
  15621. /* Test bad args. */
  15622. if (ret == 0) {
  15623. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  15624. if (ret == USER_CRYPTO_ERROR) {
  15625. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  15626. }
  15627. if (ret == USER_CRYPTO_ERROR) {
  15628. /* e < 3 */
  15629. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  15630. }
  15631. if (ret == USER_CRYPTO_ERROR) {
  15632. /* e & 1 == 0 */
  15633. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  15634. }
  15635. if (ret == USER_CRYPTO_ERROR) {
  15636. ret = 0;
  15637. } else {
  15638. ret = WOLFSSL_FATAL_ERROR;
  15639. }
  15640. }
  15641. #endif
  15642. if (wc_FreeRng(&rng) || ret != 0) {
  15643. ret = WOLFSSL_FATAL_ERROR;
  15644. }
  15645. printf(resultFmt, ret == 0 ? passed : failed);
  15646. #endif
  15647. return ret;
  15648. } /* END test_wc_MakeRsaKey */
  15649. /*
  15650. * Test the bounds checking on the cipher text versus the key modulus.
  15651. * 1. Make a new RSA key.
  15652. * 2. Set c to 1.
  15653. * 3. Decrypt c into k. (error)
  15654. * 4. Copy the key modulus to c and sub 1 from the copy.
  15655. * 5. Decrypt c into k. (error)
  15656. * Valid bounds test cases are covered by all the other RSA tests.
  15657. */
  15658. static int test_RsaDecryptBoundsCheck(void)
  15659. {
  15660. int ret = 0;
  15661. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  15662. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  15663. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  15664. RsaKey key;
  15665. byte flatC[256];
  15666. word32 flatCSz;
  15667. byte out[256];
  15668. word32 outSz = sizeof(out);
  15669. WC_RNG rng;
  15670. printf(testingFmt, "RSA decrypt bounds check");
  15671. XMEMSET(&rng, 0, sizeof(rng));
  15672. ret = wc_InitRng(&rng);
  15673. if (ret == 0)
  15674. ret = wc_InitRsaKey(&key, NULL);
  15675. if (ret == 0) {
  15676. const byte* derKey;
  15677. word32 derKeySz;
  15678. word32 idx = 0;
  15679. #ifdef USE_CERT_BUFFERS_1024
  15680. derKey = server_key_der_1024;
  15681. derKeySz = (word32)sizeof_server_key_der_1024;
  15682. flatCSz = 128;
  15683. #else
  15684. derKey = server_key_der_2048;
  15685. derKeySz = (word32)sizeof_server_key_der_2048;
  15686. flatCSz = 256;
  15687. #endif
  15688. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  15689. }
  15690. if (ret == 0) {
  15691. XMEMSET(flatC, 0, flatCSz);
  15692. flatC[flatCSz-1] = 1;
  15693. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  15694. RSA_PRIVATE_DECRYPT, &rng);
  15695. }
  15696. if (ret == RSA_OUT_OF_RANGE_E) {
  15697. mp_int c;
  15698. mp_init_copy(&c, &key.n);
  15699. mp_sub_d(&c, 1, &c);
  15700. mp_to_unsigned_bin(&c, flatC);
  15701. ret = wc_RsaDirect(flatC, sizeof(flatC), out, &outSz, &key,
  15702. RSA_PRIVATE_DECRYPT, NULL);
  15703. mp_clear(&c);
  15704. }
  15705. if (ret == RSA_OUT_OF_RANGE_E)
  15706. ret = 0;
  15707. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  15708. ret = WOLFSSL_FATAL_ERROR;
  15709. printf(resultFmt, ret == 0 ? passed : failed);
  15710. #endif
  15711. return ret;
  15712. } /* END test_wc_RsaDecryptBoundsCheck */
  15713. /*
  15714. * Testing wc_SetKeyUsage()
  15715. */
  15716. static int test_wc_SetKeyUsage (void)
  15717. {
  15718. int ret = 0;
  15719. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  15720. Cert myCert;
  15721. ret = wc_InitCert(&myCert);
  15722. printf(testingFmt, "wc_SetKeyUsage()");
  15723. if (ret == 0) {
  15724. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  15725. if (ret == 0) {
  15726. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  15727. }
  15728. if (ret == 0) {
  15729. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  15730. }
  15731. if (ret == 0) {
  15732. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  15733. }
  15734. if (ret == 0) {
  15735. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  15736. }
  15737. }
  15738. /* Test bad args. */
  15739. if (ret == 0) {
  15740. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  15741. if (ret == BAD_FUNC_ARG) {
  15742. ret = wc_SetKeyUsage(&myCert, NULL);
  15743. }
  15744. if (ret == BAD_FUNC_ARG) {
  15745. ret = wc_SetKeyUsage(&myCert, "");
  15746. }
  15747. if (ret == KEYUSAGE_E) {
  15748. ret = wc_SetKeyUsage(&myCert, ",");
  15749. }
  15750. if (ret == KEYUSAGE_E) {
  15751. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  15752. }
  15753. if (ret == KEYUSAGE_E) {
  15754. ret = 0;
  15755. } else {
  15756. ret = WOLFSSL_FATAL_ERROR;
  15757. }
  15758. }
  15759. printf(resultFmt, ret == 0 ? passed : failed);
  15760. #endif
  15761. return ret;
  15762. } /* END test_wc_SetKeyUsage */
  15763. /*
  15764. * Testing wc_CheckProbablePrime()
  15765. */
  15766. static int test_wc_CheckProbablePrime (void)
  15767. {
  15768. int ret = 0;
  15769. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  15770. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  15771. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  15772. RsaKey key;
  15773. WC_RNG rng;
  15774. byte e[3];
  15775. word32 eSz = (word32)sizeof(e);
  15776. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  15777. word32 nSz = (word32)sizeof(n);
  15778. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  15779. word32 dSz = (word32)sizeof(d);
  15780. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  15781. word32 pSz = (word32)sizeof(p);
  15782. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  15783. word32 qSz = (word32)sizeof(q);
  15784. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  15785. int* isPrime;
  15786. int test[5];
  15787. isPrime = test;
  15788. printf(testingFmt, "wc_CheckProbablePrime()");
  15789. ret = wc_InitRsaKey(&key, NULL);
  15790. if (ret == 0) {
  15791. ret = wc_InitRng(&rng);
  15792. }
  15793. if (ret == 0) {
  15794. ret = wc_RsaSetRNG(&key, &rng);
  15795. }
  15796. if (ret == 0) {
  15797. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  15798. }
  15799. if (ret == 0) {
  15800. PRIVATE_KEY_UNLOCK();
  15801. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  15802. p, &pSz, q, &qSz);
  15803. PRIVATE_KEY_LOCK();
  15804. }
  15805. /* Bad cases */
  15806. if (ret == 0) {
  15807. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  15808. nlen, isPrime);
  15809. if (ret == BAD_FUNC_ARG) {
  15810. ret = 0;
  15811. }
  15812. }
  15813. if (ret == 0) {
  15814. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  15815. nlen, isPrime);
  15816. if (ret == BAD_FUNC_ARG) {
  15817. ret = 0;
  15818. }
  15819. }
  15820. if (ret == 0) {
  15821. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  15822. nlen, isPrime);
  15823. if (ret == BAD_FUNC_ARG) {
  15824. ret = 0;
  15825. }
  15826. }
  15827. if (ret == 0) {
  15828. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  15829. nlen, isPrime);
  15830. if (ret == BAD_FUNC_ARG) {
  15831. ret = 0;
  15832. }
  15833. }
  15834. if (ret == 0) {
  15835. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  15836. nlen, isPrime);
  15837. if (ret == BAD_FUNC_ARG) {
  15838. ret = 0;
  15839. }
  15840. }
  15841. if (ret == 0) {
  15842. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  15843. nlen, isPrime);
  15844. if (ret == BAD_FUNC_ARG) {
  15845. ret = 0;
  15846. }
  15847. }
  15848. if (ret == 0) {
  15849. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  15850. nlen, isPrime);
  15851. if (ret == BAD_FUNC_ARG) {
  15852. ret = 0;
  15853. }
  15854. }
  15855. /* Good case */
  15856. if (ret == 0) {
  15857. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  15858. nlen, isPrime);
  15859. }
  15860. wc_FreeRsaKey(&key);
  15861. wc_FreeRng(&rng);
  15862. printf(resultFmt, ret == 0 ? passed : failed);
  15863. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  15864. #endif
  15865. return ret;
  15866. } /* END test_wc_CheckProbablePrime */
  15867. /*
  15868. * Testing wc_RsaPSS_Verify()
  15869. */
  15870. static int test_wc_RsaPSS_Verify (void)
  15871. {
  15872. int ret = 0;
  15873. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  15874. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  15875. RsaKey key;
  15876. WC_RNG rng;
  15877. int sz = 256;
  15878. byte* pt;
  15879. const char* szMessage = "This is the string to be signed";
  15880. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  15881. unsigned char pDecrypted[2048/8];
  15882. word32 outLen = sizeof(pDecrypted);
  15883. pt = pDecrypted;
  15884. printf(testingFmt, "wc_RsaPSS_Verify()");
  15885. ret = wc_InitRsaKey(&key, NULL);
  15886. if (ret == 0) {
  15887. ret = wc_InitRng(&rng);
  15888. }
  15889. if (ret == 0) {
  15890. ret = wc_RsaSetRNG(&key, &rng);
  15891. }
  15892. if (ret == 0) {
  15893. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  15894. }
  15895. if (ret == 0) {
  15896. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  15897. pSignature, sizeof(pSignature),
  15898. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  15899. if (ret > 0 ){
  15900. sz = ret;
  15901. ret = 0;
  15902. }
  15903. }
  15904. /* Bad cases */
  15905. if (ret == 0) {
  15906. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  15907. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15908. if (ret == BAD_FUNC_ARG) {
  15909. ret = 0;
  15910. }
  15911. }
  15912. if (ret == 0) {
  15913. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  15914. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15915. if (ret == BAD_FUNC_ARG) {
  15916. ret = 0;
  15917. }
  15918. }
  15919. if (ret == 0) {
  15920. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  15921. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15922. if (ret == BAD_FUNC_ARG) {
  15923. ret = 0;
  15924. }
  15925. }
  15926. if (ret == 0) {
  15927. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  15928. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15929. if (ret == BAD_FUNC_ARG) {
  15930. ret = 0;
  15931. }
  15932. }
  15933. /* Good case */
  15934. if (ret == 0) {
  15935. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  15936. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15937. if (ret > 0) {
  15938. ret = 0;
  15939. }
  15940. }
  15941. wc_FreeRsaKey(&key);
  15942. wc_FreeRng(&rng);
  15943. printf(resultFmt, ret == 0 ? passed : failed);
  15944. #endif
  15945. return ret;
  15946. } /* END test_wc_RsaPSS_Verify */
  15947. /*
  15948. * Testing wc_RsaPSS_VerifyCheck()
  15949. */
  15950. static int test_wc_RsaPSS_VerifyCheck (void)
  15951. {
  15952. int ret = 0;
  15953. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  15954. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  15955. RsaKey key;
  15956. WC_RNG rng;
  15957. int sz = 256; /* 2048/8 */
  15958. byte* pt;
  15959. byte digest[32];
  15960. word32 digestSz;
  15961. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  15962. word32 pSignatureSz = sizeof(pSignature);
  15963. unsigned char pDecrypted[2048/8];
  15964. word32 outLen = sizeof(pDecrypted);
  15965. pt = pDecrypted;
  15966. printf(testingFmt, "wc_RsaPSS_VerifyCheck()");
  15967. XMEMSET(digest, 0, sizeof(digest));
  15968. XMEMSET(pSignature, 0, sizeof(pSignature));
  15969. ret = wc_InitRsaKey(&key, NULL);
  15970. if (ret == 0) {
  15971. ret = wc_InitRng(&rng);
  15972. }
  15973. if (ret == 0) {
  15974. ret = wc_RsaSetRNG(&key, &rng);
  15975. }
  15976. if (ret == 0) {
  15977. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  15978. }
  15979. if (ret == 0) {
  15980. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  15981. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  15982. }
  15983. if (ret == 0) {
  15984. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  15985. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  15986. if (ret > 0 ){
  15987. sz = ret;
  15988. ret = 0;
  15989. }
  15990. }
  15991. /* Bad cases */
  15992. if (ret == 0) {
  15993. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  15994. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  15995. if (ret == BAD_FUNC_ARG) {
  15996. ret = 0;
  15997. }
  15998. }
  15999. if (ret == 0) {
  16000. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  16001. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16002. if (ret == BAD_FUNC_ARG) {
  16003. ret = 0;
  16004. }
  16005. }
  16006. if (ret == 0) {
  16007. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  16008. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16009. if (ret == BAD_FUNC_ARG) {
  16010. ret = 0;
  16011. }
  16012. }
  16013. if (ret == 0) {
  16014. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  16015. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16016. if (ret == BAD_FUNC_ARG) {
  16017. ret = 0;
  16018. }
  16019. }
  16020. /* Good case */
  16021. if (ret == 0) {
  16022. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  16023. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16024. if (ret > 0) {
  16025. ret = 0;
  16026. }
  16027. }
  16028. wc_FreeRsaKey(&key);
  16029. wc_FreeRng(&rng);
  16030. printf(resultFmt, ret == 0 ? passed : failed);
  16031. #endif
  16032. return ret;
  16033. } /* END test_wc_RsaPSS_VerifyCheck */
  16034. /*
  16035. * Testing wc_RsaPSS_VerifyCheckInline()
  16036. */
  16037. static int test_wc_RsaPSS_VerifyCheckInline (void)
  16038. {
  16039. int ret = 0;
  16040. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16041. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16042. RsaKey key;
  16043. WC_RNG rng;
  16044. int sz = 256;
  16045. byte* pt;
  16046. byte digest[32];
  16047. word32 digestSz;
  16048. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16049. unsigned char pDecrypted[2048/8];
  16050. pt = pDecrypted;
  16051. printf(testingFmt, "wc_RsaPSS_VerifyCheckInline()");
  16052. ret = wc_InitRsaKey(&key, NULL);
  16053. XMEMSET(digest, 0, sizeof(digest));
  16054. XMEMSET(pSignature, 0, sizeof(pSignature));
  16055. if (ret == 0) {
  16056. ret = wc_InitRng(&rng);
  16057. }
  16058. if (ret == 0) {
  16059. ret = wc_RsaSetRNG(&key, &rng);
  16060. }
  16061. if (ret == 0) {
  16062. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16063. }
  16064. if (ret == 0) {
  16065. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  16066. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  16067. }
  16068. if (ret == 0) {
  16069. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  16070. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16071. if (ret > 0 ){
  16072. sz = ret;
  16073. ret = 0;
  16074. }
  16075. }
  16076. /* Bad Cases */
  16077. if (ret == 0) {
  16078. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  16079. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16080. if (ret == BAD_FUNC_ARG) {
  16081. ret = 0;
  16082. }
  16083. }
  16084. if (ret == 0) {
  16085. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  16086. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16087. if (ret == BAD_FUNC_ARG) {
  16088. ret = 0;
  16089. }
  16090. }
  16091. if (ret == 0) {
  16092. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  16093. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16094. if (ret == BAD_FUNC_ARG) {
  16095. ret = 0;
  16096. }
  16097. }
  16098. if (ret == 0) {
  16099. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16100. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  16101. if (ret == BAD_FUNC_ARG) {
  16102. ret = 0;
  16103. }
  16104. }
  16105. /* Good case */
  16106. if (ret == 0) {
  16107. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16108. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16109. if (ret > 0) {
  16110. ret = 0;
  16111. }
  16112. }
  16113. wc_FreeRsaKey(&key);
  16114. wc_FreeRng(&rng);
  16115. printf(resultFmt, ret == 0 ? passed : failed);
  16116. #endif
  16117. return ret;
  16118. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16119. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16120. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16121. {
  16122. (void)flag;
  16123. (void)type;
  16124. (void)file;
  16125. (void)line;
  16126. }
  16127. #endif
  16128. /*
  16129. * Testing wc_LockMutex_ex
  16130. */
  16131. static int test_wc_LockMutex_ex (void)
  16132. {
  16133. int ret = 0;
  16134. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16135. int flag = CRYPTO_LOCK;
  16136. int type = 0;
  16137. const char* file = "./test-LockMutex_ex.txt";
  16138. int line = 0;
  16139. printf(testingFmt, "wc_LockMutex_ex()");
  16140. /*without SetMutexCb*/
  16141. ret = wc_LockMutex_ex(flag, type, file, line);
  16142. if (ret == BAD_STATE_E) {
  16143. ret = 0;
  16144. }
  16145. /*with SetMutexCb*/
  16146. if (ret == 0) {
  16147. ret = wc_SetMutexCb(sample_mutex_cb);
  16148. if (ret == 0) {
  16149. ret = wc_LockMutex_ex(flag, type, file, line);
  16150. }
  16151. }
  16152. printf(resultFmt, ret == 0 ? passed : failed);
  16153. #endif
  16154. return ret;
  16155. }/*End test_wc_LockMutex_ex*/
  16156. /*
  16157. * Testing wc_SetMutexCb
  16158. */
  16159. static int test_wc_SetMutexCb (void)
  16160. {
  16161. int ret = 0;
  16162. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16163. printf(testingFmt, "wc_SetMutexCb()");
  16164. ret = wc_SetMutexCb(sample_mutex_cb);
  16165. printf(resultFmt, ret == 0 ? passed : failed);
  16166. #endif
  16167. return ret;
  16168. }/*End test_wc_SetMutexCb*/
  16169. /*
  16170. * Testing wc_RsaKeyToDer()
  16171. */
  16172. static int test_wc_RsaKeyToDer (void)
  16173. {
  16174. int ret = 0;
  16175. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16176. RsaKey genKey;
  16177. WC_RNG rng;
  16178. byte* der;
  16179. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16180. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16181. int bits = 1024;
  16182. word32 derSz = 611;
  16183. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16184. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16185. #else
  16186. int bits = 2048;
  16187. word32 derSz = 1196;
  16188. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16189. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16190. #endif
  16191. XMEMSET(&rng, 0, sizeof(rng));
  16192. XMEMSET(&genKey, 0, sizeof(genKey));
  16193. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16194. if (der == NULL) {
  16195. ret = WOLFSSL_FATAL_ERROR;
  16196. }
  16197. /* Init structures. */
  16198. if (ret == 0) {
  16199. ret = wc_InitRsaKey(&genKey, NULL);
  16200. }
  16201. if (ret == 0) {
  16202. ret = wc_InitRng(&rng);
  16203. }
  16204. /* Make key. */
  16205. if (ret == 0) {
  16206. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  16207. if (ret != 0) {
  16208. ret = WOLFSSL_FATAL_ERROR;
  16209. }
  16210. }
  16211. printf(testingFmt, "wc_RsaKeyToDer()");
  16212. if (ret == 0) {
  16213. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  16214. if (ret > 0) {
  16215. ret = 0;
  16216. } else {
  16217. ret = WOLFSSL_FATAL_ERROR;
  16218. }
  16219. }
  16220. #ifndef HAVE_USER_RSA
  16221. /* Pass good/bad args. */
  16222. if (ret == 0) {
  16223. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  16224. if (ret == BAD_FUNC_ARG) {
  16225. /* Get just the output length */
  16226. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  16227. }
  16228. if (ret > 0) {
  16229. /* Try Public Key. */
  16230. genKey.type = 0;
  16231. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  16232. }
  16233. if (ret == BAD_FUNC_ARG) {
  16234. ret = 0;
  16235. } else {
  16236. ret = WOLFSSL_FATAL_ERROR;
  16237. }
  16238. }
  16239. #else
  16240. /* Pass good/bad args. */
  16241. if (ret == 0) {
  16242. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  16243. if (ret == USER_CRYPTO_ERROR) {
  16244. /* Get just the output length */
  16245. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  16246. }
  16247. if (ret > 0) {
  16248. /* Try Public Key. */
  16249. genKey.type = 0;
  16250. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  16251. }
  16252. if (ret == USER_CRYPTO_ERROR) {
  16253. ret = 0;
  16254. } else {
  16255. ret = WOLFSSL_FATAL_ERROR;
  16256. }
  16257. }
  16258. #endif
  16259. if (der != NULL) {
  16260. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16261. }
  16262. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  16263. ret = WOLFSSL_FATAL_ERROR;
  16264. }
  16265. if (wc_FreeRng(&rng) || ret != 0) {
  16266. ret = WOLFSSL_FATAL_ERROR;
  16267. }
  16268. printf(resultFmt, ret == 0 ? passed : failed);
  16269. #endif
  16270. return ret;
  16271. } /* END test_wc_RsaKeyToDer */
  16272. /*
  16273. * Testing wc_RsaKeyToPublicDer()
  16274. */
  16275. static int test_wc_RsaKeyToPublicDer (void)
  16276. {
  16277. int ret = 0;
  16278. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16279. RsaKey key;
  16280. WC_RNG rng;
  16281. byte* der;
  16282. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16283. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16284. int bits = 1024;
  16285. word32 derLen = 162;
  16286. #else
  16287. int bits = 2048;
  16288. word32 derLen = 294;
  16289. #endif
  16290. XMEMSET(&rng, 0, sizeof(rng));
  16291. XMEMSET(&key, 0, sizeof(key));
  16292. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16293. if (der == NULL) {
  16294. ret = WOLFSSL_FATAL_ERROR;
  16295. }
  16296. if (ret == 0) {
  16297. ret = wc_InitRsaKey(&key, NULL);
  16298. }
  16299. if (ret == 0) {
  16300. ret = wc_InitRng(&rng);
  16301. }
  16302. if (ret == 0) {
  16303. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16304. }
  16305. printf(testingFmt, "wc_RsaKeyToPublicDer()");
  16306. if (ret == 0) {
  16307. /* test getting size only */
  16308. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  16309. if (ret >= 0)
  16310. ret = 0;
  16311. }
  16312. if (ret == 0) {
  16313. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  16314. if (ret >= 0) {
  16315. ret = 0;
  16316. } else {
  16317. ret = WOLFSSL_FATAL_ERROR;
  16318. }
  16319. }
  16320. if (ret == 0) {
  16321. /* test getting size only */
  16322. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  16323. if (ret >= 0)
  16324. ret = 0;
  16325. }
  16326. if (ret == 0) {
  16327. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  16328. if (ret >= 0) {
  16329. ret = 0;
  16330. } else {
  16331. ret = WOLFSSL_FATAL_ERROR;
  16332. }
  16333. }
  16334. #ifndef HAVE_USER_RSA
  16335. /* Pass in bad args. */
  16336. if (ret == 0) {
  16337. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  16338. if (ret == BAD_FUNC_ARG) {
  16339. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  16340. }
  16341. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  16342. ret = 0;
  16343. } else {
  16344. ret = WOLFSSL_FATAL_ERROR;
  16345. }
  16346. }
  16347. #else
  16348. /* Pass in bad args. */
  16349. if (ret == 0) {
  16350. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  16351. if (ret == USER_CRYPTO_ERROR) {
  16352. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  16353. }
  16354. if (ret == USER_CRYPTO_ERROR) {
  16355. ret = 0;
  16356. } else {
  16357. ret = WOLFSSL_FATAL_ERROR;
  16358. }
  16359. }
  16360. #endif
  16361. if (der != NULL) {
  16362. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16363. }
  16364. if (wc_FreeRsaKey(&key) || ret != 0) {
  16365. ret = WOLFSSL_FATAL_ERROR;
  16366. }
  16367. if (wc_FreeRng(&rng) || ret != 0) {
  16368. ret = WOLFSSL_FATAL_ERROR;
  16369. }
  16370. printf(resultFmt, ret == 0 ? passed : failed);
  16371. #endif
  16372. return ret;
  16373. } /* END test_wc_RsaKeyToPublicDer */
  16374. /*
  16375. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16376. */
  16377. static int test_wc_RsaPublicEncryptDecrypt (void)
  16378. {
  16379. int ret = 0;
  16380. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16381. RsaKey key;
  16382. WC_RNG rng;
  16383. const char inStr[] = TEST_STRING;
  16384. const word32 plainLen = (word32)TEST_STRING_SZ;
  16385. const word32 inLen = (word32)TEST_STRING_SZ;
  16386. int bits = TEST_RSA_BITS;
  16387. const word32 cipherLen = TEST_RSA_BYTES;
  16388. word32 cipherLenResult = cipherLen;
  16389. DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16390. DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16391. DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16392. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  16393. if (in == NULL || plain == NULL || cipher == NULL) {
  16394. printf("test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  16395. return MEMORY_E;
  16396. }
  16397. #endif
  16398. XMEMCPY(in, inStr, inLen);
  16399. ret = wc_InitRsaKey(&key, NULL);
  16400. if (ret == 0) {
  16401. ret = wc_InitRng(&rng);
  16402. }
  16403. if (ret == 0) {
  16404. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16405. }
  16406. /* Encrypt. */
  16407. printf(testingFmt, "wc_RsaPublicEncrypt()");
  16408. if (ret == 0) {
  16409. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  16410. if (ret >= 0) {
  16411. cipherLenResult = ret;
  16412. ret = 0;
  16413. } else {
  16414. ret = WOLFSSL_FATAL_ERROR;
  16415. }
  16416. }
  16417. /* Pass bad args. */
  16418. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  16419. printf(resultFmt, ret == 0 ? passed : failed);
  16420. if (ret != 0) {
  16421. return ret;
  16422. }
  16423. /* Decrypt */
  16424. printf(testingFmt, "wc_RsaPrivateDecrypt()");
  16425. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16426. /* Bind rng */
  16427. if (ret == 0) {
  16428. ret = wc_RsaSetRNG(&key, &rng);
  16429. }
  16430. #endif
  16431. if (ret == 0) {
  16432. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  16433. }
  16434. if (ret >= 0) {
  16435. ret = XMEMCMP(plain, inStr, plainLen);
  16436. }
  16437. /* Pass in bad args. */
  16438. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  16439. FREE_VAR(in, NULL);
  16440. FREE_VAR(plain, NULL);
  16441. FREE_VAR(cipher, NULL);
  16442. if (wc_FreeRsaKey(&key) || ret != 0) {
  16443. ret = WOLFSSL_FATAL_ERROR;
  16444. }
  16445. if (wc_FreeRng(&rng) || ret != 0) {
  16446. ret = WOLFSSL_FATAL_ERROR;
  16447. }
  16448. printf(resultFmt, ret == 0 ? passed : failed);
  16449. #endif
  16450. return ret;
  16451. } /* END test_wc_RsaPublicEncryptDecrypt */
  16452. /*
  16453. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16454. */
  16455. static int test_wc_RsaPublicEncryptDecrypt_ex (void)
  16456. {
  16457. int ret = 0;
  16458. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16459. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16460. && !defined(NO_SHA)
  16461. RsaKey key;
  16462. WC_RNG rng;
  16463. const char inStr[] = TEST_STRING;
  16464. const word32 inLen = (word32)TEST_STRING_SZ;
  16465. const word32 plainSz = (word32)TEST_STRING_SZ;
  16466. byte* res = NULL;
  16467. int idx = 0;
  16468. int bits = TEST_RSA_BITS;
  16469. const word32 cipherSz = TEST_RSA_BYTES;
  16470. DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16471. DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16472. DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16473. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  16474. if (in == NULL || plain == NULL || cipher == NULL) {
  16475. printf("test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  16476. return MEMORY_E;
  16477. }
  16478. #endif
  16479. XMEMCPY(in, inStr, inLen);
  16480. /* Initialize stack structures. */
  16481. XMEMSET(&rng, 0, sizeof(rng));
  16482. XMEMSET(&key, 0, sizeof(key));
  16483. ret = wc_InitRsaKey_ex(&key, NULL, INVALID_DEVID);
  16484. if (ret == 0) {
  16485. ret = wc_InitRng(&rng);
  16486. }
  16487. if (ret == 0) {
  16488. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16489. }
  16490. /* Encrypt */
  16491. printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
  16492. if (ret == 0) {
  16493. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  16494. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  16495. if (ret >= 0) {
  16496. idx = ret;
  16497. ret = 0;
  16498. } else {
  16499. ret = WOLFSSL_FATAL_ERROR;
  16500. }
  16501. }
  16502. /*Pass bad args.*/
  16503. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  16504. printf(resultFmt, ret == 0 ? passed : failed);
  16505. if (ret != 0) {
  16506. return ret;
  16507. }
  16508. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16509. /* Decrypt */
  16510. printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
  16511. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16512. if (ret == 0) {
  16513. ret = wc_RsaSetRNG(&key, &rng);
  16514. }
  16515. #endif
  16516. if (ret == 0) {
  16517. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  16518. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  16519. WC_MGF1SHA1, NULL, 0);
  16520. }
  16521. if (ret >= 0) {
  16522. if (!XMEMCMP(plain, inStr, plainSz)) {
  16523. ret = 0;
  16524. } else {
  16525. ret = WOLFSSL_FATAL_ERROR;
  16526. }
  16527. }
  16528. /*Pass bad args.*/
  16529. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  16530. printf(resultFmt, ret == 0 ? passed : failed);
  16531. if (ret != 0) {
  16532. return ret;
  16533. }
  16534. printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
  16535. if (ret == 0) {
  16536. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  16537. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  16538. WC_MGF1SHA1, NULL, 0);
  16539. if (ret >= 0) {
  16540. if (!XMEMCMP(inStr, res, plainSz)) {
  16541. ret = 0;
  16542. } else {
  16543. ret = WOLFSSL_FATAL_ERROR;
  16544. }
  16545. }
  16546. }
  16547. #endif
  16548. FREE_VAR(in, NULL);
  16549. FREE_VAR(plain, NULL);
  16550. FREE_VAR(cipher, NULL);
  16551. if (wc_FreeRsaKey(&key) || ret != 0) {
  16552. ret = WOLFSSL_FATAL_ERROR;
  16553. }
  16554. if (wc_FreeRng(&rng) || ret != 0) {
  16555. ret = WOLFSSL_FATAL_ERROR;
  16556. }
  16557. printf(resultFmt, ret == 0 ? passed : failed);
  16558. #endif
  16559. return ret;
  16560. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16561. /*
  16562. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16563. */
  16564. static int test_wc_RsaSSL_SignVerify (void)
  16565. {
  16566. int ret = 0;
  16567. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16568. RsaKey key;
  16569. WC_RNG rng;
  16570. const char inStr[] = TEST_STRING;
  16571. const word32 plainSz = (word32)TEST_STRING_SZ;
  16572. const word32 inLen = (word32)TEST_STRING_SZ;
  16573. word32 idx = 0;
  16574. int bits = TEST_RSA_BITS;
  16575. const word32 outSz = TEST_RSA_BYTES;
  16576. DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16577. DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16578. DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16579. #ifdef DECLARE_VAR_IS_HEAP_ALLOC
  16580. if (in == NULL || out == NULL || plain == NULL) {
  16581. printf("test_wc_RsaSSL_SignVerify failed\n");
  16582. return MEMORY_E;
  16583. }
  16584. #endif
  16585. XMEMCPY(in, inStr, inLen);
  16586. ret = wc_InitRsaKey(&key, NULL);
  16587. if (ret == 0) {
  16588. ret = wc_InitRng(&rng);
  16589. }
  16590. if (ret == 0) {
  16591. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16592. }
  16593. /* Sign. */
  16594. printf(testingFmt, "wc_RsaSSL_Sign()");
  16595. if (ret == 0) {
  16596. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  16597. if (ret == (int)outSz) {
  16598. idx = ret;
  16599. ret = 0;
  16600. } else {
  16601. ret = WOLFSSL_FATAL_ERROR;
  16602. }
  16603. }
  16604. #ifndef HAVE_USER_RSA
  16605. /* Test bad args. */
  16606. if (ret == 0) {
  16607. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  16608. if (ret == BAD_FUNC_ARG) {
  16609. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  16610. }
  16611. if (ret == BAD_FUNC_ARG) {
  16612. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  16613. }
  16614. if (ret == BAD_FUNC_ARG) {
  16615. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  16616. }
  16617. if (ret == BAD_FUNC_ARG) {
  16618. ret = 0;
  16619. } else {
  16620. ret = WOLFSSL_FATAL_ERROR;
  16621. }
  16622. }
  16623. #else
  16624. /* Test bad args. */
  16625. if (ret == 0) {
  16626. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  16627. if (ret == USER_CRYPTO_ERROR) {
  16628. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  16629. }
  16630. if (ret == USER_CRYPTO_ERROR) {
  16631. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  16632. }
  16633. if (ret == USER_CRYPTO_ERROR) {
  16634. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  16635. }
  16636. if (ret == USER_CRYPTO_ERROR) {
  16637. ret = 0;
  16638. } else {
  16639. ret = WOLFSSL_FATAL_ERROR;
  16640. }
  16641. }
  16642. #endif
  16643. printf(resultFmt, ret == 0 ? passed : failed);
  16644. if (ret != 0) {
  16645. return ret;
  16646. }
  16647. /* Verify. */
  16648. printf(testingFmt, "wc_RsaSSL_Verify()");
  16649. if (ret == 0) {
  16650. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  16651. if (ret == (int)inLen) {
  16652. ret = 0;
  16653. } else {
  16654. ret = WOLFSSL_FATAL_ERROR;
  16655. }
  16656. }
  16657. #ifndef HAVE_USER_RSA
  16658. /* Pass bad args. */
  16659. if (ret == 0) {
  16660. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  16661. if (ret == BAD_FUNC_ARG) {
  16662. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  16663. }
  16664. if (ret == BAD_FUNC_ARG) {
  16665. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  16666. }
  16667. if (ret == BAD_FUNC_ARG) {
  16668. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  16669. }
  16670. if (ret == BAD_FUNC_ARG) {
  16671. ret = 0;
  16672. } else {
  16673. ret = WOLFSSL_FATAL_ERROR;
  16674. }
  16675. }
  16676. #else
  16677. /* Pass bad args. */
  16678. if (ret == 0) {
  16679. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  16680. if (ret == USER_CRYPTO_ERROR) {
  16681. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  16682. }
  16683. if (ret == USER_CRYPTO_ERROR) {
  16684. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  16685. }
  16686. if (ret == USER_CRYPTO_ERROR) {
  16687. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  16688. }
  16689. if (ret == USER_CRYPTO_ERROR) {
  16690. ret = 0;
  16691. } else {
  16692. ret = WOLFSSL_FATAL_ERROR;
  16693. }
  16694. }
  16695. #endif
  16696. FREE_VAR(in, NULL);
  16697. FREE_VAR(out, NULL);
  16698. FREE_VAR(plain, NULL);
  16699. if (wc_FreeRsaKey(&key) || ret != 0) {
  16700. ret = WOLFSSL_FATAL_ERROR;
  16701. }
  16702. if (wc_FreeRng(&rng) || ret != 0) {
  16703. ret = WOLFSSL_FATAL_ERROR;
  16704. }
  16705. printf(resultFmt, ret == 0 ? passed : failed);
  16706. #endif
  16707. return ret;
  16708. } /* END test_wc_RsaSSL_SignVerify */
  16709. /*
  16710. * Testing wc_RsaEncryptSize()
  16711. */
  16712. static int test_wc_RsaEncryptSize (void)
  16713. {
  16714. int ret = 0;
  16715. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16716. RsaKey key;
  16717. WC_RNG rng;
  16718. ret = wc_InitRsaKey(&key, NULL);
  16719. if (ret == 0) {
  16720. ret = wc_InitRng(&rng);
  16721. }
  16722. printf(testingFmt, "wc_RsaEncryptSize()");
  16723. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16724. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16725. if (ret == 0) {
  16726. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  16727. if (ret == 0) {
  16728. ret = wc_RsaEncryptSize(&key);
  16729. }
  16730. if (ret == 128) {
  16731. ret = 0;
  16732. } else {
  16733. ret = WOLFSSL_FATAL_ERROR;
  16734. }
  16735. }
  16736. if (wc_FreeRsaKey(&key) || ret != 0) {
  16737. ret = WOLFSSL_FATAL_ERROR;
  16738. } else {
  16739. ret = 0;
  16740. }
  16741. #endif
  16742. if (ret == 0) {
  16743. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  16744. if (ret == 0) {
  16745. ret = wc_RsaEncryptSize(&key);
  16746. }
  16747. if (ret == 256) {
  16748. ret = 0;
  16749. } else {
  16750. ret = WOLFSSL_FATAL_ERROR;
  16751. }
  16752. }
  16753. /* Pass in bad arg. */
  16754. if (ret == 0) {
  16755. ret = wc_RsaEncryptSize(NULL);
  16756. #ifndef HAVE_USER_RSA
  16757. if (ret == BAD_FUNC_ARG) {
  16758. ret = 0;
  16759. } else {
  16760. ret = WOLFSSL_FATAL_ERROR;
  16761. }
  16762. #endif
  16763. }
  16764. if (wc_FreeRsaKey(&key) || ret != 0) {
  16765. ret = WOLFSSL_FATAL_ERROR;
  16766. }
  16767. if (wc_FreeRng(&rng) || ret != 0) {
  16768. ret = WOLFSSL_FATAL_ERROR;
  16769. }
  16770. printf(resultFmt, ret == 0 ? passed : failed);
  16771. #endif
  16772. return ret;
  16773. } /* END test_wc_RsaEncryptSize*/
  16774. /*
  16775. * Testing wc_RsaFlattenPublicKey()
  16776. */
  16777. static int test_wc_RsaFlattenPublicKey (void)
  16778. {
  16779. int ret = 0;
  16780. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16781. RsaKey key;
  16782. WC_RNG rng;
  16783. byte e[256];
  16784. byte n[256];
  16785. word32 eSz = sizeof(e);
  16786. word32 nSz = sizeof(n);
  16787. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16788. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16789. int bits = 1024;
  16790. #else
  16791. int bits = 2048;
  16792. #endif
  16793. ret = wc_InitRsaKey(&key, NULL);
  16794. if (ret == 0) {
  16795. ret = wc_InitRng(&rng);
  16796. }
  16797. if (ret == 0) {
  16798. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16799. if (ret >= 0) {
  16800. ret = 0;
  16801. } else {
  16802. ret = WOLFSSL_FATAL_ERROR;
  16803. }
  16804. }
  16805. printf(testingFmt, "wc_RsaFlattenPublicKey()");
  16806. if (ret == 0) {
  16807. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  16808. }
  16809. #ifndef HAVE_USER_RSA
  16810. /* Pass bad args. */
  16811. if (ret == 0) {
  16812. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  16813. if (ret == BAD_FUNC_ARG) {
  16814. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  16815. }
  16816. if (ret == BAD_FUNC_ARG) {
  16817. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  16818. }
  16819. if (ret == BAD_FUNC_ARG) {
  16820. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  16821. }
  16822. if (ret == BAD_FUNC_ARG) {
  16823. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  16824. }
  16825. if (ret == BAD_FUNC_ARG) {
  16826. ret = 0;
  16827. } else {
  16828. ret = WOLFSSL_FATAL_ERROR;
  16829. }
  16830. }
  16831. #else
  16832. /* Pass bad args. */
  16833. if (ret == 0) {
  16834. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  16835. if (ret == USER_CRYPTO_ERROR) {
  16836. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  16837. }
  16838. if (ret == USER_CRYPTO_ERROR) {
  16839. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  16840. }
  16841. if (ret == USER_CRYPTO_ERROR) {
  16842. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  16843. }
  16844. if (ret == USER_CRYPTO_ERROR) {
  16845. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  16846. }
  16847. if (ret == USER_CRYPTO_ERROR) {
  16848. ret = 0;
  16849. } else {
  16850. ret = WOLFSSL_FATAL_ERROR;
  16851. }
  16852. }
  16853. #endif
  16854. if (wc_FreeRsaKey(&key) || ret != 0) {
  16855. ret = WOLFSSL_FATAL_ERROR;
  16856. }
  16857. if (wc_FreeRng(&rng) || ret != 0) {
  16858. ret = WOLFSSL_FATAL_ERROR;
  16859. }
  16860. printf(resultFmt, ret == 0 ? passed : failed);
  16861. #endif
  16862. return ret;
  16863. } /* END test_wc_RsaFlattenPublicKey */
  16864. /*
  16865. * unit test for wc_AesCcmSetKey
  16866. */
  16867. static int test_wc_AesCcmSetKey (void)
  16868. {
  16869. int ret = 0;
  16870. #ifdef HAVE_AESCCM
  16871. Aes aes;
  16872. const byte key16[] =
  16873. {
  16874. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16875. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16876. };
  16877. const byte key24[] =
  16878. {
  16879. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16880. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16881. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16882. };
  16883. const byte key32[] =
  16884. {
  16885. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16886. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16887. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16888. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16889. };
  16890. printf(testingFmt, "wc_AesCcmSetKey()");
  16891. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16892. if (ret != 0)
  16893. return ret;
  16894. #ifdef WOLFSSL_AES_128
  16895. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  16896. #endif
  16897. #ifdef WOLFSSL_AES_192
  16898. if (ret == 0) {
  16899. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  16900. }
  16901. #endif
  16902. #ifdef WOLFSSL_AES_256
  16903. if (ret == 0) {
  16904. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  16905. }
  16906. #endif
  16907. /* Test bad args. */
  16908. if (ret == 0) {
  16909. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  16910. if (ret == BAD_FUNC_ARG) {
  16911. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  16912. }
  16913. if (ret == BAD_FUNC_ARG) {
  16914. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  16915. }
  16916. if (ret != BAD_FUNC_ARG) {
  16917. ret = WOLFSSL_FATAL_ERROR;
  16918. } else {
  16919. ret = 0;
  16920. }
  16921. }
  16922. wc_AesFree(&aes);
  16923. printf(resultFmt, ret == 0 ? passed : failed);
  16924. #endif
  16925. return ret;
  16926. } /* END test_wc_AesCcmSetKey */
  16927. /*
  16928. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  16929. */
  16930. static int test_wc_AesCcmEncryptDecrypt (void)
  16931. {
  16932. int ret = 0;
  16933. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  16934. Aes aes;
  16935. const byte key16[] =
  16936. {
  16937. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16938. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16939. };
  16940. /* plaintext */
  16941. const byte plainT[] =
  16942. {
  16943. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  16944. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  16945. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  16946. };
  16947. /* nonce */
  16948. const byte iv[] =
  16949. {
  16950. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  16951. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  16952. };
  16953. const byte c[] = /* cipher text. */
  16954. {
  16955. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  16956. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  16957. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  16958. };
  16959. const byte t[] = /* Auth tag */
  16960. {
  16961. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  16962. };
  16963. const byte authIn[] =
  16964. {
  16965. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  16966. };
  16967. byte cipherOut[sizeof(plainT)];
  16968. byte authTag[sizeof(t)];
  16969. int ccmE = WOLFSSL_FATAL_ERROR;
  16970. #ifdef HAVE_AES_DECRYPT
  16971. int ccmD = WOLFSSL_FATAL_ERROR;
  16972. byte plainOut[sizeof(cipherOut)];
  16973. #endif
  16974. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16975. if (ret != 0)
  16976. return ret;
  16977. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  16978. if (ret == 0) {
  16979. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16980. iv, sizeof(iv), authTag, sizeof(authTag),
  16981. authIn , sizeof(authIn));
  16982. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  16983. XMEMCMP(t, authTag, sizeof(t))) {
  16984. ccmE = WOLFSSL_FATAL_ERROR;
  16985. ret = WOLFSSL_FATAL_ERROR;
  16986. }
  16987. #ifdef HAVE_AES_DECRYPT
  16988. if (ret == 0) {
  16989. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  16990. sizeof(plainOut), iv, sizeof(iv),
  16991. authTag, sizeof(authTag),
  16992. authIn, sizeof(authIn));
  16993. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  16994. ccmD = WOLFSSL_FATAL_ERROR;
  16995. }
  16996. }
  16997. #endif
  16998. }
  16999. printf(testingFmt, "wc_AesCcmEncrypt()");
  17000. /* Pass in bad args. Encrypt*/
  17001. if (ret == 0 && ccmE == 0) {
  17002. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17003. iv, sizeof(iv), authTag, sizeof(authTag),
  17004. authIn , sizeof(authIn));
  17005. if (ccmE == BAD_FUNC_ARG) {
  17006. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17007. iv, sizeof(iv), authTag, sizeof(authTag),
  17008. authIn , sizeof(authIn));
  17009. }
  17010. if (ccmE == BAD_FUNC_ARG) {
  17011. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17012. iv, sizeof(iv), authTag, sizeof(authTag),
  17013. authIn , sizeof(authIn));
  17014. }
  17015. if (ccmE == BAD_FUNC_ARG) {
  17016. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17017. NULL, sizeof(iv), authTag, sizeof(authTag),
  17018. authIn , sizeof(authIn));
  17019. }
  17020. if (ccmE == BAD_FUNC_ARG) {
  17021. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17022. iv, sizeof(iv), NULL, sizeof(authTag),
  17023. authIn , sizeof(authIn));
  17024. }
  17025. if (ccmE == BAD_FUNC_ARG) {
  17026. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17027. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  17028. authIn , sizeof(authIn));
  17029. }
  17030. if (ccmE == BAD_FUNC_ARG) {
  17031. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17032. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  17033. authIn , sizeof(authIn));
  17034. }
  17035. if (ccmE != BAD_FUNC_ARG) {
  17036. ccmE = WOLFSSL_FATAL_ERROR;
  17037. } else {
  17038. ccmE = 0;
  17039. }
  17040. } /* End Encrypt */
  17041. printf(resultFmt, ccmE == 0 ? passed : failed);
  17042. if (ccmE != 0) {
  17043. wc_AesFree(&aes);
  17044. return ccmE;
  17045. }
  17046. #ifdef HAVE_AES_DECRYPT
  17047. printf(testingFmt, "wc_AesCcmDecrypt()");
  17048. /* Pass in bad args. Decrypt*/
  17049. if (ret == 0 && ccmD == 0) {
  17050. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17051. iv, sizeof(iv), authTag, sizeof(authTag),
  17052. authIn, sizeof(authIn));
  17053. if (ccmD == BAD_FUNC_ARG) {
  17054. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17055. iv, sizeof(iv), authTag, sizeof(authTag),
  17056. authIn, sizeof(authIn));
  17057. }
  17058. if (ccmD == BAD_FUNC_ARG) {
  17059. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17060. iv, sizeof(iv), authTag, sizeof(authTag),
  17061. authIn, sizeof(authIn));
  17062. }
  17063. if (ccmD == BAD_FUNC_ARG) {
  17064. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17065. sizeof(plainOut), NULL, sizeof(iv),
  17066. authTag, sizeof(authTag),
  17067. authIn, sizeof(authIn));
  17068. }
  17069. if (ccmD == BAD_FUNC_ARG) {
  17070. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17071. sizeof(plainOut), iv, sizeof(iv), NULL,
  17072. sizeof(authTag), authIn, sizeof(authIn));
  17073. }
  17074. if (ccmD == BAD_FUNC_ARG) {
  17075. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17076. sizeof(plainOut), iv, sizeof(iv) + 1,
  17077. authTag, sizeof(authTag),
  17078. authIn, sizeof(authIn));
  17079. }
  17080. if (ccmD == BAD_FUNC_ARG) {
  17081. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17082. sizeof(plainOut), iv, sizeof(iv) - 7,
  17083. authTag, sizeof(authTag),
  17084. authIn, sizeof(authIn));
  17085. }
  17086. if (ccmD != BAD_FUNC_ARG) {
  17087. ccmD = WOLFSSL_FATAL_ERROR;
  17088. } else {
  17089. ccmD = 0;
  17090. }
  17091. } /* END Decrypt */
  17092. printf(resultFmt, ccmD == 0 ? passed : failed);
  17093. if (ccmD != 0) {
  17094. return ccmD;
  17095. }
  17096. #endif
  17097. wc_AesFree(&aes);
  17098. #endif /* HAVE_AESCCM */
  17099. return ret;
  17100. } /* END test_wc_AesCcmEncryptDecrypt */
  17101. /*
  17102. * Test wc_Hc128_SetKey()
  17103. */
  17104. static int test_wc_Hc128_SetKey (void)
  17105. {
  17106. int ret = 0;
  17107. #ifdef HAVE_HC128
  17108. HC128 ctx;
  17109. const char* key = "\x80\x00\x00\x00\x00\x00\x00\x00"
  17110. "\x00\x00\x00\x00\x00\x00\x00\x00";
  17111. const char* iv = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  17112. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  17113. printf(testingFmt, "wc_Hc128_SetKey()");
  17114. ret = wc_Hc128_SetKey(&ctx, (byte*)key, (byte*)iv);
  17115. /* Test bad args. */
  17116. if (ret == 0) {
  17117. ret = wc_Hc128_SetKey(NULL, (byte*)key, (byte*)iv);
  17118. if (ret == BAD_FUNC_ARG) {
  17119. ret = wc_Hc128_SetKey(&ctx, NULL, (byte*)iv);
  17120. }
  17121. if (ret == BAD_FUNC_ARG) {
  17122. ret = wc_Hc128_SetKey(&ctx, (byte*)key, NULL);
  17123. }
  17124. }
  17125. printf(resultFmt, ret == 0 ? passed : failed);
  17126. #endif
  17127. return ret;
  17128. } /* END test_wc_Hc128_SetKey */
  17129. /*
  17130. * Testing wc_Hc128_Process()
  17131. */
  17132. static int test_wc_Hc128_Process (void)
  17133. {
  17134. int ret = 0;
  17135. #ifdef HAVE_HC128
  17136. HC128 enc;
  17137. HC128 dec;
  17138. const char* key = "\x0F\x62\xB5\x08\x5B\xAE\x01\x54"
  17139. "\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC";
  17140. const char* input = "Encrypt Hc128, and then Decrypt.";
  17141. size_t inlen = XSTRLEN(input) + 1; /* Add null terminator */
  17142. byte cipher[inlen];
  17143. byte plain[inlen];
  17144. printf(testingFmt, "wc_Hc128_Process()");
  17145. ret = wc_Hc128_SetKey(&enc, (byte*)key, NULL);
  17146. if (ret == 0) {
  17147. ret = wc_Hc128_SetKey(&dec, (byte*)key, NULL);
  17148. }
  17149. if (ret == 0) {
  17150. ret = wc_Hc128_Process(&enc, cipher, (byte*)input, (word32)inlen);
  17151. if (ret == 0) {
  17152. ret = wc_Hc128_Process(&dec, plain, cipher, (word32)inlen);
  17153. }
  17154. }
  17155. /* Bad args. */
  17156. if (ret == 0) {
  17157. ret = wc_Hc128_Process(NULL, plain, cipher, (word32)inlen);
  17158. if (ret == BAD_FUNC_ARG) {
  17159. ret = wc_Hc128_Process(&dec, NULL, cipher, (word32)inlen);
  17160. }
  17161. if (ret == BAD_FUNC_ARG) {
  17162. ret = wc_Hc128_Process(&dec, plain, NULL, (word32)inlen);
  17163. }
  17164. if (ret == BAD_FUNC_ARG) {
  17165. ret = 0;
  17166. } else {
  17167. ret = WOLFSSL_FATAL_ERROR;
  17168. }
  17169. }
  17170. printf(resultFmt, ret == 0 ? passed : failed);
  17171. #endif
  17172. return ret;
  17173. } /* END test_wc_Hc128_Process */
  17174. /*
  17175. * Testing wc_InitDsaKey()
  17176. */
  17177. static int test_wc_InitDsaKey (void)
  17178. {
  17179. int ret = 0;
  17180. #ifndef NO_DSA
  17181. DsaKey key;
  17182. printf(testingFmt, "wc_InitDsaKey()");
  17183. ret = wc_InitDsaKey(&key);
  17184. /* Pass in bad args. */
  17185. if (ret == 0) {
  17186. ret = wc_InitDsaKey(NULL);
  17187. if (ret == BAD_FUNC_ARG) {
  17188. ret = 0;
  17189. } else {
  17190. ret = WOLFSSL_FATAL_ERROR;
  17191. }
  17192. }
  17193. printf(resultFmt, ret == 0 ? passed : failed);
  17194. wc_FreeDsaKey(&key);
  17195. #endif
  17196. return ret;
  17197. } /* END test_wc_InitDsaKey */
  17198. /*
  17199. * Testing wc_DsaSign() and wc_DsaVerify()
  17200. */
  17201. static int test_wc_DsaSignVerify (void)
  17202. {
  17203. int ret = 0;
  17204. #if !defined(NO_DSA)
  17205. DsaKey key;
  17206. WC_RNG rng;
  17207. wc_Sha sha;
  17208. byte signature[DSA_SIG_SIZE];
  17209. byte hash[WC_SHA_DIGEST_SIZE];
  17210. word32 idx = 0;
  17211. word32 bytes;
  17212. int answer;
  17213. #ifdef USE_CERT_BUFFERS_1024
  17214. byte tmp[ONEK_BUF];
  17215. XMEMSET(tmp, 0, sizeof(tmp));
  17216. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17217. bytes = sizeof_dsa_key_der_1024;
  17218. #elif defined(USE_CERT_BUFFERS_2048)
  17219. byte tmp[TWOK_BUF];
  17220. XMEMSET(tmp, 0, sizeof(tmp));
  17221. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17222. bytes = sizeof_dsa_key_der_2048;
  17223. #else
  17224. byte tmp[TWOK_BUF];
  17225. XMEMSET(tmp, 0, sizeof(tmp));
  17226. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17227. if (fp == XBADFILE) {
  17228. return WOLFSSL_BAD_FILE;
  17229. }
  17230. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17231. XFCLOSE(fp);
  17232. #endif /* END USE_CERT_BUFFERS_1024 */
  17233. ret = wc_InitSha(&sha);
  17234. if (ret == 0) {
  17235. ret = wc_ShaUpdate(&sha, tmp, bytes);
  17236. if (ret == 0) {
  17237. ret = wc_ShaFinal(&sha, hash);
  17238. }
  17239. if (ret == 0) {
  17240. ret = wc_InitDsaKey(&key);
  17241. }
  17242. if (ret == 0) {
  17243. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17244. }
  17245. if (ret == 0) {
  17246. ret = wc_InitRng(&rng);
  17247. }
  17248. }
  17249. printf(testingFmt, "wc_DsaSign()");
  17250. /* Sign. */
  17251. if (ret == 0) {
  17252. ret = wc_DsaSign(hash, signature, &key, &rng);
  17253. }
  17254. /* Test bad args. */
  17255. if (ret == 0) {
  17256. ret = wc_DsaSign(NULL, signature, &key, &rng);
  17257. if (ret == BAD_FUNC_ARG) {
  17258. ret = wc_DsaSign(hash, NULL, &key, &rng);
  17259. }
  17260. if (ret == BAD_FUNC_ARG) {
  17261. ret = wc_DsaSign(hash, signature, NULL, &rng);
  17262. }
  17263. if (ret == BAD_FUNC_ARG) {
  17264. ret = wc_DsaSign(hash, signature, &key, NULL);
  17265. }
  17266. if (ret == BAD_FUNC_ARG) {
  17267. ret = 0;
  17268. } else {
  17269. ret = WOLFSSL_FATAL_ERROR;
  17270. }
  17271. }
  17272. printf(resultFmt, ret == 0 ? passed : failed);
  17273. if (ret != 0) {
  17274. return ret;
  17275. }
  17276. /* Verify. */
  17277. printf(testingFmt, "wc_DsaVerify()");
  17278. ret = wc_DsaVerify(hash, signature, &key, &answer);
  17279. if (ret != 0 || answer != 1) {
  17280. ret = WOLFSSL_FATAL_ERROR;
  17281. } else {
  17282. ret = 0;
  17283. }
  17284. /* Pass in bad args. */
  17285. if (ret == 0) {
  17286. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  17287. if (ret == BAD_FUNC_ARG) {
  17288. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  17289. }
  17290. if (ret == BAD_FUNC_ARG) {
  17291. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  17292. }
  17293. if (ret == BAD_FUNC_ARG) {
  17294. ret = wc_DsaVerify(hash, signature, &key, NULL);
  17295. }
  17296. if (ret == BAD_FUNC_ARG) {
  17297. ret = 0;
  17298. } else {
  17299. ret = WOLFSSL_FATAL_ERROR;
  17300. }
  17301. }
  17302. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  17303. /* hard set q to 0 and test fail case */
  17304. mp_free(&key.q);
  17305. mp_init(&key.q);
  17306. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17307. mp_set_int(&key.q, 1);
  17308. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17309. #endif
  17310. if (wc_FreeRng(&rng) && ret == 0) {
  17311. ret = WOLFSSL_FATAL_ERROR;
  17312. }
  17313. printf(resultFmt, ret == 0 ? passed : failed);
  17314. wc_FreeDsaKey(&key);
  17315. wc_ShaFree(&sha);
  17316. #endif
  17317. return ret;
  17318. } /* END test_wc_DsaSign */
  17319. /*
  17320. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  17321. */
  17322. static int test_wc_DsaPublicPrivateKeyDecode (void)
  17323. {
  17324. int ret = 0;
  17325. #if !defined(NO_DSA)
  17326. DsaKey key;
  17327. word32 bytes;
  17328. word32 idx = 0;
  17329. int priv = WOLFSSL_FATAL_ERROR;
  17330. int pub = WOLFSSL_FATAL_ERROR;
  17331. #ifdef USE_CERT_BUFFERS_1024
  17332. byte tmp[ONEK_BUF];
  17333. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17334. bytes = sizeof_dsa_key_der_1024;
  17335. #elif defined(USE_CERT_BUFFERS_2048)
  17336. byte tmp[TWOK_BUF];
  17337. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17338. bytes = sizeof_dsa_key_der_2048;
  17339. #else
  17340. byte tmp[TWOK_BUF];
  17341. XMEMSET(tmp, 0, sizeof(tmp));
  17342. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17343. if (fp == XBADFILE)
  17344. {
  17345. return WOLFSSL_BAD_FILE;
  17346. }
  17347. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17348. XFCLOSE(fp);
  17349. #endif /* END USE_CERT_BUFFERS_1024 */
  17350. ret = wc_InitDsaKey(&key);
  17351. printf(testingFmt, "wc_DsaPrivateKeyDecode()");
  17352. if (ret == 0) {
  17353. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17354. /* Test bad args. */
  17355. if (priv == 0) {
  17356. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  17357. if (priv == BAD_FUNC_ARG) {
  17358. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  17359. }
  17360. if (priv == BAD_FUNC_ARG) {
  17361. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  17362. }
  17363. if (priv == BAD_FUNC_ARG) {
  17364. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17365. }
  17366. if (priv == ASN_PARSE_E) {
  17367. priv = 0;
  17368. } else {
  17369. priv = WOLFSSL_FATAL_ERROR;
  17370. }
  17371. }
  17372. wc_FreeDsaKey(&key);
  17373. ret = wc_InitDsaKey(&key);
  17374. }
  17375. printf(resultFmt, priv == 0 ? passed : failed);
  17376. printf(testingFmt, "wc_DsaPublicKeyDecode()");
  17377. if (ret == 0) {
  17378. idx = 0; /* Reset */
  17379. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  17380. /* Test bad args. */
  17381. if (pub == 0) {
  17382. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  17383. if (pub == BAD_FUNC_ARG) {
  17384. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  17385. }
  17386. if (pub == BAD_FUNC_ARG) {
  17387. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  17388. }
  17389. if (pub == BAD_FUNC_ARG) {
  17390. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  17391. }
  17392. if (pub == ASN_PARSE_E) {
  17393. pub = 0;
  17394. } else {
  17395. pub = WOLFSSL_FATAL_ERROR;
  17396. }
  17397. }
  17398. } /* END Public Key */
  17399. printf(resultFmt, pub == 0 ? passed : failed);
  17400. wc_FreeDsaKey(&key);
  17401. #endif
  17402. return ret;
  17403. } /* END test_wc_DsaPublicPrivateKeyDecode */
  17404. /*
  17405. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  17406. */
  17407. static int test_wc_MakeDsaKey (void)
  17408. {
  17409. int ret = 0;
  17410. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17411. DsaKey genKey;
  17412. WC_RNG rng;
  17413. XMEMSET(&rng, 0, sizeof(rng));
  17414. XMEMSET(&genKey, 0, sizeof(genKey));
  17415. ret = wc_InitRng(&rng);
  17416. if (ret == 0) {
  17417. ret = wc_InitDsaKey(&genKey);
  17418. }
  17419. printf(testingFmt, "wc_MakeDsaParameters()");
  17420. if (ret == 0) {
  17421. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  17422. }
  17423. /* Test bad args. */
  17424. if (ret == 0) {
  17425. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  17426. if (ret == BAD_FUNC_ARG) {
  17427. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  17428. }
  17429. if (ret == BAD_FUNC_ARG) {
  17430. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  17431. }
  17432. if (ret == BAD_FUNC_ARG) {
  17433. ret = 0;
  17434. } else {
  17435. ret = WOLFSSL_FATAL_ERROR;
  17436. }
  17437. }
  17438. printf(resultFmt, ret == 0 ? passed : failed);
  17439. printf(testingFmt, "wc_MakeDsaKey()");
  17440. if (ret == 0) {
  17441. ret = wc_MakeDsaKey(&rng, &genKey);
  17442. }
  17443. /* Test bad args. */
  17444. if (ret == 0) {
  17445. ret = wc_MakeDsaKey(NULL, &genKey);
  17446. if (ret == BAD_FUNC_ARG) {
  17447. ret = wc_MakeDsaKey(&rng, NULL);
  17448. }
  17449. if (ret == BAD_FUNC_ARG) {
  17450. ret = 0;
  17451. } else {
  17452. ret = WOLFSSL_FATAL_ERROR;
  17453. }
  17454. }
  17455. if (wc_FreeRng(&rng) && ret == 0) {
  17456. ret = WOLFSSL_FAILURE;
  17457. }
  17458. printf(resultFmt, ret == 0 ? passed : failed);
  17459. wc_FreeDsaKey(&genKey);
  17460. #endif
  17461. return ret;
  17462. } /* END test_wc_MakeDsaKey */
  17463. /*
  17464. * Testing wc_DsaKeyToDer()
  17465. */
  17466. static int test_wc_DsaKeyToDer (void)
  17467. {
  17468. int ret = 0;
  17469. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17470. DsaKey genKey;
  17471. WC_RNG rng;
  17472. word32 bytes;
  17473. word32 idx = 0;
  17474. #ifdef USE_CERT_BUFFERS_1024
  17475. byte tmp[ONEK_BUF];
  17476. byte der[ONEK_BUF];
  17477. XMEMSET(tmp, 0, sizeof(tmp));
  17478. XMEMSET(der, 0, sizeof(der));
  17479. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17480. bytes = sizeof_dsa_key_der_1024;
  17481. #elif defined(USE_CERT_BUFFERS_2048)
  17482. byte tmp[TWOK_BUF];
  17483. byte der[TWOK_BUF];
  17484. XMEMSET(tmp, 0, sizeof(tmp));
  17485. XMEMSET(der, 0, sizeof(der));
  17486. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17487. bytes = sizeof_dsa_key_der_2048;
  17488. #else
  17489. byte tmp[TWOK_BUF];
  17490. byte der[TWOK_BUF];
  17491. XMEMSET(tmp, 0, sizeof(tmp));
  17492. XMEMSET(der, 0, sizeof(der));
  17493. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17494. if (fp == XBADFILE) {
  17495. return WOLFSSL_BAD_FILE;
  17496. }
  17497. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17498. XFCLOSE(fp);
  17499. #endif /* END USE_CERT_BUFFERS_1024 */
  17500. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17501. XMEMSET(&rng, 0, sizeof(rng));
  17502. XMEMSET(&genKey, 0, sizeof(genKey));
  17503. #endif
  17504. ret = wc_InitRng(&rng);
  17505. if (ret == 0) {
  17506. ret = wc_InitDsaKey(&genKey);
  17507. }
  17508. if (ret == 0) {
  17509. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  17510. if (ret == 0) {
  17511. wc_FreeDsaKey(&genKey);
  17512. ret = wc_InitDsaKey(&genKey);
  17513. }
  17514. }
  17515. if (ret == 0) {
  17516. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  17517. }
  17518. printf(testingFmt, "wc_DsaKeyToDer()");
  17519. if (ret == 0) {
  17520. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  17521. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  17522. ret = 0;
  17523. }
  17524. }
  17525. /* Test bad args. */
  17526. if (ret == 0) {
  17527. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  17528. if (ret == BAD_FUNC_ARG) {
  17529. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  17530. }
  17531. if (ret == BAD_FUNC_ARG) {
  17532. ret = 0;
  17533. } else {
  17534. ret = WOLFSSL_FATAL_ERROR;
  17535. }
  17536. }
  17537. if (wc_FreeRng(&rng) && ret == 0) {
  17538. ret = WOLFSSL_FATAL_ERROR;
  17539. }
  17540. printf(resultFmt, ret == 0 ? passed : failed);
  17541. wc_FreeDsaKey(&genKey);
  17542. #endif
  17543. return ret;
  17544. } /* END test_wc_DsaKeyToDer */
  17545. /*
  17546. * Testing wc_DsaKeyToPublicDer()
  17547. * (indirectly testing setDsaPublicKey())
  17548. */
  17549. static int test_wc_DsaKeyToPublicDer(void)
  17550. {
  17551. int ret = 0;
  17552. #ifndef HAVE_SELFTEST
  17553. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17554. DsaKey genKey;
  17555. WC_RNG rng;
  17556. byte* der;
  17557. word32 sz;
  17558. printf(testingFmt, "wc_DsaKeyToPublicDer()");
  17559. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17560. if (der == NULL) {
  17561. ret = WOLFSSL_FATAL_ERROR;
  17562. }
  17563. if (ret == 0) {
  17564. ret = wc_InitDsaKey(&genKey);
  17565. }
  17566. if (ret == 0) {
  17567. ret = wc_InitRng(&rng);
  17568. }
  17569. if (ret == 0) {
  17570. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  17571. }
  17572. if (ret == 0) {
  17573. ret = wc_MakeDsaKey(&rng, &genKey);
  17574. }
  17575. if (ret == 0) {
  17576. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  17577. if (ret >= 0) {
  17578. sz = ret;
  17579. ret = 0;
  17580. } else {
  17581. ret = WOLFSSL_FATAL_ERROR;
  17582. }
  17583. }
  17584. if (ret == 0) {
  17585. word32 idx = 0;
  17586. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  17587. }
  17588. /* Test bad args. */
  17589. if (ret == 0) {
  17590. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  17591. if (ret == BAD_FUNC_ARG) {
  17592. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  17593. }
  17594. if (ret == BAD_FUNC_ARG) {
  17595. ret = 0;
  17596. } else {
  17597. ret = WOLFSSL_FATAL_ERROR;
  17598. }
  17599. }
  17600. if (wc_FreeRng(&rng) && ret == 0) {
  17601. ret = WOLFSSL_FATAL_ERROR;
  17602. }
  17603. printf(resultFmt, ret == 0 ? passed : failed);
  17604. XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
  17605. wc_FreeDsaKey(&genKey);
  17606. #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
  17607. #endif /* HAVE_SELFTEST */
  17608. return ret;
  17609. } /* END test_wc_DsaKeyToPublicDer */
  17610. /*
  17611. * Testing wc_DsaImportParamsRaw()
  17612. */
  17613. static int test_wc_DsaImportParamsRaw (void)
  17614. {
  17615. int ret = 0;
  17616. #if !defined(NO_DSA)
  17617. DsaKey key;
  17618. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17619. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17620. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17621. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17622. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17623. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17624. "80a0093113a8bd73";
  17625. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17626. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17627. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17628. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17629. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17630. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17631. "76341a7e7d9";
  17632. /* invalid p and q parameters */
  17633. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17634. const char* invalidQ = "96c5390a";
  17635. printf(testingFmt, "wc_DsaImportParamsRaw()");
  17636. ret = wc_InitDsaKey(&key);
  17637. if (ret == 0) {
  17638. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  17639. }
  17640. /* test bad args */
  17641. if (ret == 0) {
  17642. /* null key struct */
  17643. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  17644. if (ret == BAD_FUNC_ARG) {
  17645. /* null param pointers */
  17646. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  17647. }
  17648. if (ret == BAD_FUNC_ARG) {
  17649. /* illegal p length */
  17650. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  17651. }
  17652. if (ret == BAD_FUNC_ARG) {
  17653. /* illegal q length */
  17654. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  17655. if (ret == BAD_FUNC_ARG)
  17656. ret = 0;
  17657. }
  17658. }
  17659. printf(resultFmt, ret == 0 ? passed : failed);
  17660. wc_FreeDsaKey(&key);
  17661. #endif
  17662. return ret;
  17663. } /* END test_wc_DsaImportParamsRaw */
  17664. /*
  17665. * Testing wc_DsaImportParamsRawCheck()
  17666. */
  17667. static int test_wc_DsaImportParamsRawCheck (void)
  17668. {
  17669. int ret = 0;
  17670. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17671. DsaKey key;
  17672. int trusted = 0;
  17673. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17674. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17675. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17676. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17677. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17678. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17679. "80a0093113a8bd73";
  17680. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17681. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17682. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17683. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17684. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17685. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17686. "76341a7e7d9";
  17687. /* invalid p and q parameters */
  17688. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17689. const char* invalidQ = "96c5390a";
  17690. printf(testingFmt, "wc_DsaImportParamsRawCheck()");
  17691. ret = wc_InitDsaKey(&key);
  17692. if (ret == 0) {
  17693. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  17694. }
  17695. /* test bad args */
  17696. if (ret == 0) {
  17697. /* null key struct */
  17698. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  17699. if (ret == BAD_FUNC_ARG) {
  17700. /* null param pointers */
  17701. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  17702. }
  17703. if (ret == BAD_FUNC_ARG) {
  17704. /* illegal p length */
  17705. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  17706. }
  17707. if (ret == BAD_FUNC_ARG) {
  17708. /* illegal q length */
  17709. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  17710. if (ret == BAD_FUNC_ARG)
  17711. ret = 0;
  17712. }
  17713. }
  17714. printf(resultFmt, ret == 0 ? passed : failed);
  17715. wc_FreeDsaKey(&key);
  17716. #endif
  17717. return ret;
  17718. } /* END test_wc_DsaImportParamsRawCheck */
  17719. /*
  17720. * Testing wc_DsaExportParamsRaw()
  17721. */
  17722. static int test_wc_DsaExportParamsRaw (void)
  17723. {
  17724. int ret = 0;
  17725. #if !defined(NO_DSA)
  17726. DsaKey key;
  17727. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17728. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17729. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17730. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17731. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17732. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17733. "80a0093113a8bd73";
  17734. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17735. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17736. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17737. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17738. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17739. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17740. "76341a7e7d9";
  17741. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  17742. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  17743. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  17744. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  17745. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  17746. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  17747. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  17748. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  17749. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  17750. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  17751. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  17752. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  17753. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  17754. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  17755. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  17756. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  17757. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  17758. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  17759. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  17760. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  17761. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  17762. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  17763. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  17764. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  17765. byte pOut[MAX_DSA_PARAM_SIZE];
  17766. byte qOut[MAX_DSA_PARAM_SIZE];
  17767. byte gOut[MAX_DSA_PARAM_SIZE];
  17768. word32 pOutSz, qOutSz, gOutSz;
  17769. printf(testingFmt, "wc_DsaExportParamsRaw()");
  17770. ret = wc_InitDsaKey(&key);
  17771. if (ret == 0) {
  17772. /* first test using imported raw parameters, for expected */
  17773. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  17774. }
  17775. if (ret == 0) {
  17776. pOutSz = sizeof(pOut);
  17777. qOutSz = sizeof(qOut);
  17778. gOutSz = sizeof(gOut);
  17779. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  17780. gOut, &gOutSz);
  17781. }
  17782. if (ret == 0) {
  17783. /* validate exported parameters are correct */
  17784. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  17785. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  17786. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  17787. ret = -1;
  17788. }
  17789. }
  17790. /* test bad args */
  17791. if (ret == 0) {
  17792. /* null key struct */
  17793. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  17794. gOut, &gOutSz);
  17795. if (ret == BAD_FUNC_ARG) {
  17796. /* null output pointers */
  17797. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  17798. NULL, &gOutSz);
  17799. }
  17800. if (ret == LENGTH_ONLY_E) {
  17801. /* null output size pointers */
  17802. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  17803. gOut, NULL);
  17804. }
  17805. if (ret == BAD_FUNC_ARG) {
  17806. /* p output buffer size too small */
  17807. pOutSz = 1;
  17808. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  17809. gOut, &gOutSz);
  17810. pOutSz = sizeof(pOut);
  17811. }
  17812. if (ret == BUFFER_E) {
  17813. /* q output buffer size too small */
  17814. qOutSz = 1;
  17815. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  17816. gOut, &gOutSz);
  17817. qOutSz = sizeof(qOut);
  17818. }
  17819. if (ret == BUFFER_E) {
  17820. /* g output buffer size too small */
  17821. gOutSz = 1;
  17822. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  17823. gOut, &gOutSz);
  17824. if (ret == BUFFER_E)
  17825. ret = 0;
  17826. }
  17827. }
  17828. printf(resultFmt, ret == 0 ? passed : failed);
  17829. wc_FreeDsaKey(&key);
  17830. #endif
  17831. return ret;
  17832. } /* END test_wc_DsaExportParamsRaw */
  17833. /*
  17834. * Testing wc_DsaExportKeyRaw()
  17835. */
  17836. static int test_wc_DsaExportKeyRaw (void)
  17837. {
  17838. int ret = 0;
  17839. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17840. DsaKey key;
  17841. WC_RNG rng;
  17842. byte xOut[MAX_DSA_PARAM_SIZE];
  17843. byte yOut[MAX_DSA_PARAM_SIZE];
  17844. word32 xOutSz, yOutSz;
  17845. printf(testingFmt, "wc_DsaExportKeyRaw()");
  17846. XMEMSET(&rng, 0, sizeof(rng));
  17847. XMEMSET(&key, 0, sizeof(key));
  17848. ret = wc_InitRng(&rng);
  17849. if (ret == 0) {
  17850. ret = wc_InitDsaKey(&key);
  17851. }
  17852. if (ret == 0) {
  17853. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  17854. if (ret == 0) {
  17855. ret = wc_MakeDsaKey(&rng, &key);
  17856. }
  17857. }
  17858. /* try successful export */
  17859. if (ret == 0) {
  17860. xOutSz = sizeof(xOut);
  17861. yOutSz = sizeof(yOut);
  17862. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  17863. }
  17864. /* test bad args */
  17865. if (ret == 0) {
  17866. /* null key struct */
  17867. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  17868. if (ret == BAD_FUNC_ARG) {
  17869. /* null output pointers */
  17870. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  17871. }
  17872. if (ret == LENGTH_ONLY_E) {
  17873. /* null output size pointers */
  17874. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  17875. }
  17876. if (ret == BAD_FUNC_ARG) {
  17877. /* x output buffer size too small */
  17878. xOutSz = 1;
  17879. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  17880. xOutSz = sizeof(xOut);
  17881. }
  17882. if (ret == BUFFER_E) {
  17883. /* y output buffer size too small */
  17884. yOutSz = 1;
  17885. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  17886. if (ret == BUFFER_E)
  17887. ret = 0;
  17888. }
  17889. }
  17890. printf(resultFmt, ret == 0 ? passed : failed);
  17891. wc_FreeDsaKey(&key);
  17892. wc_FreeRng(&rng);
  17893. #endif
  17894. return ret;
  17895. } /* END test_wc_DsaExportParamsRaw */
  17896. /*
  17897. * Testing wc_ed25519_make_key().
  17898. */
  17899. static int test_wc_ed25519_make_key (void)
  17900. {
  17901. int ret = 0;
  17902. #if defined(HAVE_ED25519)
  17903. ed25519_key key;
  17904. WC_RNG rng;
  17905. ret = wc_InitRng(&rng);
  17906. if (ret == 0) {
  17907. ret = wc_ed25519_init(&key);
  17908. }
  17909. printf(testingFmt, "wc_ed25519_make_key()");
  17910. if (ret == 0) {
  17911. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  17912. }
  17913. /* Test bad args. */
  17914. if (ret == 0) {
  17915. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  17916. if (ret == BAD_FUNC_ARG) {
  17917. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  17918. }
  17919. if (ret == BAD_FUNC_ARG) {
  17920. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  17921. }
  17922. if (ret == BAD_FUNC_ARG) {
  17923. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  17924. }
  17925. if (ret == BAD_FUNC_ARG) {
  17926. ret = 0;
  17927. } else if (ret == 0) {
  17928. ret = WOLFSSL_FATAL_ERROR;
  17929. }
  17930. }
  17931. printf(resultFmt, ret == 0 ? passed : failed);
  17932. if (wc_FreeRng(&rng) && ret == 0) {
  17933. ret = WOLFSSL_FATAL_ERROR;
  17934. }
  17935. wc_ed25519_free(&key);
  17936. #endif
  17937. return ret;
  17938. } /* END test_wc_ed25519_make_key */
  17939. /*
  17940. * Testing wc_ed25519_init()
  17941. */
  17942. static int test_wc_ed25519_init (void)
  17943. {
  17944. int ret = 0;
  17945. #if defined(HAVE_ED25519)
  17946. ed25519_key key;
  17947. printf(testingFmt, "wc_ed25519_init()");
  17948. ret = wc_ed25519_init(&key);
  17949. /* Test bad args. */
  17950. if (ret == 0) {
  17951. ret = wc_ed25519_init(NULL);
  17952. if (ret == BAD_FUNC_ARG) {
  17953. ret = 0;
  17954. } else if (ret == 0) {
  17955. ret = WOLFSSL_FATAL_ERROR;
  17956. }
  17957. }
  17958. printf(resultFmt, ret == 0 ? passed : failed);
  17959. wc_ed25519_free(&key);
  17960. #endif
  17961. return ret;
  17962. } /* END test_wc_ed25519_init */
  17963. /*
  17964. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  17965. */
  17966. static int test_wc_ed25519_sign_msg (void)
  17967. {
  17968. int ret = 0;
  17969. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  17970. WC_RNG rng;
  17971. ed25519_key key;
  17972. byte msg[] = "Everybody gets Friday off.\n";
  17973. byte sig[ED25519_SIG_SIZE];
  17974. word32 msglen = sizeof(msg);
  17975. word32 siglen = sizeof(sig);
  17976. word32 badSigLen = sizeof(sig) - 1;
  17977. #ifdef HAVE_ED25519_VERIFY
  17978. int verify_ok = 0; /*1 = Verify success.*/
  17979. #endif
  17980. /* Initialize stack variables. */
  17981. XMEMSET(sig, 0, siglen);
  17982. /* Initialize key. */
  17983. ret = wc_InitRng(&rng);
  17984. if (ret == 0) {
  17985. ret = wc_ed25519_init(&key);
  17986. if (ret == 0) {
  17987. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  17988. }
  17989. }
  17990. printf(testingFmt, "wc_ed25519_sign_msg()");
  17991. if (ret == 0) {
  17992. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  17993. }
  17994. /* Test bad args. */
  17995. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  17996. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  17997. if (ret == BAD_FUNC_ARG) {
  17998. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  17999. }
  18000. if (ret == BAD_FUNC_ARG) {
  18001. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  18002. }
  18003. if (ret == BAD_FUNC_ARG) {
  18004. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  18005. }
  18006. if (ret == BAD_FUNC_ARG) {
  18007. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  18008. }
  18009. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  18010. badSigLen -= 1;
  18011. ret = 0;
  18012. } else if (ret == 0) {
  18013. ret = WOLFSSL_FATAL_ERROR;
  18014. }
  18015. } /* END sign */
  18016. printf(resultFmt, ret == 0 ? passed : failed);
  18017. #ifdef HAVE_ED25519_VERIFY
  18018. printf(testingFmt, "wc_ed25519_verify_msg()");
  18019. if (ret == 0) {
  18020. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  18021. if (ret == 0 && verify_ok == 1) {
  18022. ret = 0;
  18023. } else if (ret == 0) {
  18024. ret = WOLFSSL_FATAL_ERROR;
  18025. }
  18026. /* Test bad args. */
  18027. if (ret == 0) {
  18028. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  18029. msglen, &verify_ok, &key),
  18030. BAD_FUNC_ARG);
  18031. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  18032. msglen, &verify_ok, &key),
  18033. BAD_FUNC_ARG);
  18034. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  18035. &key);
  18036. if (ret == BAD_FUNC_ARG) {
  18037. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  18038. &verify_ok, &key);
  18039. }
  18040. if (ret == BAD_FUNC_ARG) {
  18041. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18042. NULL, &key);
  18043. }
  18044. if (ret == BAD_FUNC_ARG) {
  18045. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18046. &verify_ok, NULL);
  18047. }
  18048. if (ret == BAD_FUNC_ARG) {
  18049. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  18050. &verify_ok, &key);
  18051. }
  18052. if (ret == BAD_FUNC_ARG) {
  18053. ret = 0;
  18054. } else if (ret == 0) {
  18055. ret = WOLFSSL_FATAL_ERROR;
  18056. }
  18057. }
  18058. } /* END verify. */
  18059. printf(resultFmt, ret == 0 ? passed : failed);
  18060. #endif /* Verify. */
  18061. if (wc_FreeRng(&rng) && ret == 0) {
  18062. ret = WOLFSSL_FATAL_ERROR;
  18063. }
  18064. wc_ed25519_free(&key);
  18065. #endif
  18066. return ret;
  18067. } /* END test_wc_ed25519_sign_msg */
  18068. /*
  18069. * Testing wc_ed25519_import_public()
  18070. */
  18071. static int test_wc_ed25519_import_public (void)
  18072. {
  18073. int ret = 0;
  18074. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18075. WC_RNG rng;
  18076. ed25519_key pubKey;
  18077. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  18078. word32 inlen = sizeof(in);
  18079. ret = wc_InitRng(&rng);
  18080. if (ret == 0) {
  18081. ret = wc_ed25519_init(&pubKey);
  18082. if (ret == 0) {
  18083. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  18084. }
  18085. }
  18086. printf(testingFmt, "wc_ed25519_import_public()");
  18087. if (ret == 0) {
  18088. ret = wc_ed25519_import_public(in, inlen, &pubKey);
  18089. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  18090. ret = 0;
  18091. } else {
  18092. ret = WOLFSSL_FATAL_ERROR;
  18093. }
  18094. /* Test bad args. */
  18095. if (ret == 0) {
  18096. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  18097. if (ret == BAD_FUNC_ARG) {
  18098. ret = wc_ed25519_import_public(in, inlen, NULL);
  18099. }
  18100. if (ret == BAD_FUNC_ARG) {
  18101. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  18102. }
  18103. if (ret == BAD_FUNC_ARG) {
  18104. ret = 0;
  18105. } else if (ret == 0) {
  18106. ret = WOLFSSL_FATAL_ERROR;
  18107. }
  18108. }
  18109. }
  18110. printf(resultFmt, ret == 0 ? passed : failed);
  18111. if (wc_FreeRng(&rng) && ret == 0) {
  18112. ret = WOLFSSL_FATAL_ERROR;
  18113. }
  18114. wc_ed25519_free(&pubKey);
  18115. #endif
  18116. return ret;
  18117. } /* END wc_ed25519_import_public */
  18118. /*
  18119. * Testing wc_ed25519_import_private_key()
  18120. */
  18121. static int test_wc_ed25519_import_private_key (void)
  18122. {
  18123. int ret = 0;
  18124. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18125. WC_RNG rng;
  18126. ed25519_key key;
  18127. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  18128. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  18129. word32 privKeySz = sizeof(privKey);
  18130. word32 pubKeySz = sizeof(pubKey);
  18131. #ifdef HAVE_ED25519_KEY_EXPORT
  18132. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  18133. word32 bothKeysSz = sizeof(bothKeys);
  18134. #endif
  18135. ret = wc_InitRng(&rng);
  18136. if (ret != 0) {
  18137. return ret;
  18138. }
  18139. ret = wc_ed25519_init(&key);
  18140. if (ret != 0) {
  18141. wc_FreeRng(&rng);
  18142. return ret;
  18143. }
  18144. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18145. printf(testingFmt, "wc_ed25519_import_private_key()");
  18146. if (ret == 0) {
  18147. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18148. pubKeySz, &key);
  18149. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18150. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18151. ret = WOLFSSL_FATAL_ERROR;
  18152. }
  18153. }
  18154. #ifdef HAVE_ED25519_KEY_EXPORT
  18155. if (ret == 0)
  18156. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  18157. if (ret == 0) {
  18158. ret = wc_ed25519_import_private_key(bothKeys, bothKeysSz, NULL, 0, &key);
  18159. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18160. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18161. ret = WOLFSSL_FATAL_ERROR;
  18162. }
  18163. }
  18164. #endif
  18165. /* Test bad args. */
  18166. if (ret == 0) {
  18167. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  18168. &key);
  18169. if (ret == BAD_FUNC_ARG) {
  18170. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18171. pubKeySz, &key);
  18172. }
  18173. if (ret == BAD_FUNC_ARG) {
  18174. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18175. pubKeySz, NULL);
  18176. }
  18177. if (ret == BAD_FUNC_ARG) {
  18178. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  18179. pubKeySz, &key);
  18180. }
  18181. if (ret == BAD_FUNC_ARG) {
  18182. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18183. pubKeySz - 1, &key);
  18184. }
  18185. if (ret == BAD_FUNC_ARG) {
  18186. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18187. 0, &key);
  18188. }
  18189. if (ret == BAD_FUNC_ARG) {
  18190. ret = 0;
  18191. } else if (ret == 0) {
  18192. ret = WOLFSSL_FATAL_ERROR;
  18193. }
  18194. }
  18195. printf(resultFmt, ret == 0 ? passed : failed);
  18196. if (wc_FreeRng(&rng) && ret == 0) {
  18197. ret = WOLFSSL_FATAL_ERROR;
  18198. }
  18199. wc_ed25519_free(&key);
  18200. #endif
  18201. return ret;
  18202. } /* END test_wc_ed25519_import_private_key */
  18203. /*
  18204. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  18205. */
  18206. static int test_wc_ed25519_export (void)
  18207. {
  18208. int ret = 0;
  18209. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  18210. WC_RNG rng;
  18211. ed25519_key key;
  18212. byte priv[ED25519_PRV_KEY_SIZE];
  18213. byte pub[ED25519_PUB_KEY_SIZE];
  18214. word32 privSz = sizeof(priv);
  18215. word32 pubSz = sizeof(pub);
  18216. ret = wc_InitRng(&rng);
  18217. if (ret != 0) {
  18218. return ret;
  18219. }
  18220. ret = wc_ed25519_init(&key);
  18221. if (ret != 0) {
  18222. wc_FreeRng(&rng);
  18223. return ret;
  18224. }
  18225. if (ret == 0) {
  18226. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18227. }
  18228. printf(testingFmt, "wc_ed25519_export_public()");
  18229. if (ret == 0) {
  18230. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  18231. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  18232. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  18233. ret = WOLFSSL_FATAL_ERROR;
  18234. }
  18235. if (ret == 0) {
  18236. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  18237. if (ret == BAD_FUNC_ARG) {
  18238. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  18239. }
  18240. if (ret == BAD_FUNC_ARG) {
  18241. ret = wc_ed25519_export_public(&key, pub, NULL);
  18242. }
  18243. if (ret == BAD_FUNC_ARG) {
  18244. ret = 0;
  18245. } else if (ret == 0) {
  18246. ret = WOLFSSL_FATAL_ERROR;
  18247. }
  18248. }
  18249. }
  18250. printf(resultFmt, ret == 0 ? passed : failed);
  18251. printf(testingFmt, "wc_ed25519_export_private_only()");
  18252. if (ret == 0) {
  18253. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  18254. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  18255. || XMEMCMP(key.k, priv, privSz) != 0)) {
  18256. ret = WOLFSSL_FATAL_ERROR;
  18257. }
  18258. if (ret == 0) {
  18259. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  18260. if (ret == BAD_FUNC_ARG) {
  18261. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  18262. }
  18263. if (ret == BAD_FUNC_ARG) {
  18264. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  18265. }
  18266. if (ret == BAD_FUNC_ARG) {
  18267. ret = 0;
  18268. } else if (ret == 0) {
  18269. ret = WOLFSSL_FATAL_ERROR;
  18270. }
  18271. }
  18272. }
  18273. printf(resultFmt, ret == 0 ? passed : failed);
  18274. if (wc_FreeRng(&rng) && ret == 0) {
  18275. ret = WOLFSSL_FATAL_ERROR;
  18276. }
  18277. wc_ed25519_free(&key);
  18278. #endif
  18279. return ret;
  18280. } /* END test_wc_ed25519_export */
  18281. /*
  18282. * Testing wc_ed25519_size()
  18283. */
  18284. static int test_wc_ed25519_size (void)
  18285. {
  18286. int ret = 0;
  18287. #if defined(HAVE_ED25519)
  18288. WC_RNG rng;
  18289. ed25519_key key;
  18290. ret = wc_InitRng(&rng);
  18291. if (ret != 0) {
  18292. return ret;
  18293. }
  18294. ret = wc_ed25519_init(&key);
  18295. if (ret != 0) {
  18296. wc_FreeRng(&rng);
  18297. return ret;
  18298. }
  18299. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18300. if (ret != 0) {
  18301. wc_FreeRng(&rng);
  18302. wc_ed25519_free(&key);
  18303. return ret;
  18304. }
  18305. printf(testingFmt, "wc_ed25519_size()");
  18306. ret = wc_ed25519_size(&key);
  18307. /* Test bad args. */
  18308. if (ret == ED25519_KEY_SIZE) {
  18309. ret = wc_ed25519_size(NULL);
  18310. if (ret == BAD_FUNC_ARG) {
  18311. ret = 0;
  18312. }
  18313. }
  18314. printf(resultFmt, ret == 0 ? passed : failed);
  18315. if (ret == 0) {
  18316. printf(testingFmt, "wc_ed25519_sig_size()");
  18317. ret = wc_ed25519_sig_size(&key);
  18318. if (ret == ED25519_SIG_SIZE) {
  18319. ret = 0;
  18320. }
  18321. /* Test bad args. */
  18322. if (ret == 0) {
  18323. ret = wc_ed25519_sig_size(NULL);
  18324. if (ret == BAD_FUNC_ARG) {
  18325. ret = 0;
  18326. }
  18327. }
  18328. printf(resultFmt, ret == 0 ? passed : failed);
  18329. } /* END wc_ed25519_sig_size() */
  18330. if (ret == 0) {
  18331. printf(testingFmt, "wc_ed25519_pub_size");
  18332. ret = wc_ed25519_pub_size(&key);
  18333. if (ret == ED25519_PUB_KEY_SIZE) {
  18334. ret = 0;
  18335. }
  18336. if (ret == 0) {
  18337. ret = wc_ed25519_pub_size(NULL);
  18338. if (ret == BAD_FUNC_ARG) {
  18339. ret = 0;
  18340. }
  18341. }
  18342. printf(resultFmt, ret == 0 ? passed : failed);
  18343. } /* END wc_ed25519_pub_size */
  18344. if (ret == 0) {
  18345. printf(testingFmt, "wc_ed25519_priv_size");
  18346. ret = wc_ed25519_priv_size(&key);
  18347. if (ret == ED25519_PRV_KEY_SIZE) {
  18348. ret = 0;
  18349. }
  18350. if (ret == 0) {
  18351. ret = wc_ed25519_priv_size(NULL);
  18352. if (ret == BAD_FUNC_ARG) {
  18353. ret = 0;
  18354. }
  18355. }
  18356. printf(resultFmt, ret == 0 ? passed : failed);
  18357. } /* END wc_ed25519_pub_size */
  18358. if (wc_FreeRng(&rng) && ret == 0) {
  18359. ret = WOLFSSL_FATAL_ERROR;
  18360. }
  18361. wc_ed25519_free(&key);
  18362. #endif
  18363. return ret;
  18364. } /* END test_wc_ed25519_size */
  18365. /*
  18366. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  18367. */
  18368. static int test_wc_ed25519_exportKey (void)
  18369. {
  18370. int ret = 0;
  18371. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  18372. WC_RNG rng;
  18373. ed25519_key key;
  18374. byte priv[ED25519_PRV_KEY_SIZE];
  18375. byte pub[ED25519_PUB_KEY_SIZE];
  18376. byte privOnly[ED25519_PRV_KEY_SIZE];
  18377. word32 privSz = sizeof(priv);
  18378. word32 pubSz = sizeof(pub);
  18379. word32 privOnlySz = sizeof(privOnly);
  18380. ret = wc_InitRng(&rng);
  18381. if (ret != 0) {
  18382. return ret;
  18383. }
  18384. ret = wc_ed25519_init(&key);
  18385. if (ret != 0) {
  18386. wc_FreeRng(&rng);
  18387. return ret;
  18388. }
  18389. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18390. if (ret != 0) {
  18391. wc_FreeRng(&rng);
  18392. wc_ed25519_free(&key);
  18393. return ret;
  18394. }
  18395. printf(testingFmt, "wc_ed25519_export_private()");
  18396. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  18397. if (ret == 0) {
  18398. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  18399. if (ret == BAD_FUNC_ARG) {
  18400. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  18401. }
  18402. if (ret == BAD_FUNC_ARG) {
  18403. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  18404. }
  18405. if (ret == BAD_FUNC_ARG) {
  18406. ret = 0;
  18407. } else if (ret == 0) {
  18408. ret = WOLFSSL_FATAL_ERROR;
  18409. }
  18410. }
  18411. printf(resultFmt, ret == 0 ? passed : failed);
  18412. if (ret == 0) {
  18413. printf(testingFmt, "wc_ed25519_export_key()");
  18414. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  18415. if (ret == 0) {
  18416. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  18417. if (ret == BAD_FUNC_ARG) {
  18418. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  18419. }
  18420. if (ret == BAD_FUNC_ARG) {
  18421. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  18422. }
  18423. if (ret == BAD_FUNC_ARG) {
  18424. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  18425. }
  18426. if (ret == BAD_FUNC_ARG) {
  18427. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  18428. }
  18429. if (ret == BAD_FUNC_ARG) {
  18430. ret = 0;
  18431. } else if (ret == 0) {
  18432. ret = WOLFSSL_FATAL_ERROR;
  18433. }
  18434. }
  18435. printf(resultFmt, ret == 0 ? passed : failed);
  18436. } /* END wc_ed25519_export_key() */
  18437. /* Cross check output. */
  18438. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  18439. ret = WOLFSSL_FATAL_ERROR;
  18440. }
  18441. if (wc_FreeRng(&rng) && ret == 0) {
  18442. ret = WOLFSSL_FATAL_ERROR;
  18443. }
  18444. wc_ed25519_free(&key);
  18445. #endif
  18446. return ret;
  18447. } /* END test_wc_ed25519_exportKey */
  18448. /*
  18449. * Testing wc_Ed25519PublicKeyToDer
  18450. */
  18451. static int test_wc_Ed25519PublicKeyToDer (void)
  18452. {
  18453. int ret = 0;
  18454. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  18455. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  18456. int tmp;
  18457. ed25519_key key;
  18458. byte derBuf[1024];
  18459. printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
  18460. /* Test bad args */
  18461. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  18462. if (tmp != BAD_FUNC_ARG) {
  18463. ret = WOLFSSL_FATAL_ERROR;
  18464. }
  18465. if (ret == 0) {
  18466. wc_ed25519_init(&key);
  18467. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  18468. if (tmp != BUFFER_E) {
  18469. ret = WOLFSSL_FATAL_ERROR;
  18470. }
  18471. wc_ed25519_free(&key);
  18472. }
  18473. /* Test good args */
  18474. if (ret == 0) {
  18475. WC_RNG rng;
  18476. ret = wc_InitRng(&rng);
  18477. if (ret != 0) {
  18478. return ret;
  18479. }
  18480. ret = wc_ed25519_init(&key);
  18481. if (ret != 0) {
  18482. wc_FreeRng(&rng);
  18483. return ret;
  18484. }
  18485. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18486. if (ret != 0) {
  18487. wc_FreeRng(&rng);
  18488. wc_ed25519_free(&key);
  18489. return ret;
  18490. }
  18491. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  18492. if (tmp <= 0) {
  18493. ret = WOLFSSL_FATAL_ERROR;
  18494. }
  18495. wc_FreeRng(&rng);
  18496. wc_ed25519_free(&key);
  18497. }
  18498. printf(resultFmt, ret == 0 ? passed : failed);
  18499. #endif
  18500. return ret;
  18501. } /* END testing wc_Ed25519PublicKeyToDer */
  18502. /*
  18503. * Testing wc_curve25519_init and wc_curve25519_free.
  18504. */
  18505. static int test_wc_curve25519_init (void)
  18506. {
  18507. int ret = 0;
  18508. #if defined(HAVE_CURVE25519)
  18509. curve25519_key key;
  18510. printf(testingFmt, "wc_curve25519_init()");
  18511. ret = wc_curve25519_init(&key);
  18512. /* Test bad args for wc_curve25519_init */
  18513. if (ret == 0) {
  18514. ret = wc_curve25519_init(NULL);
  18515. if (ret == BAD_FUNC_ARG) {
  18516. ret = 0;
  18517. } else if (ret == 0) {
  18518. ret = WOLFSSL_FATAL_ERROR;
  18519. }
  18520. }
  18521. printf(resultFmt, ret == 0 ? passed : failed);
  18522. /* Test good args for wc_curve_25519_free */
  18523. wc_curve25519_free(&key);
  18524. wc_curve25519_free(NULL);
  18525. #endif
  18526. return ret;
  18527. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  18528. /*
  18529. * Testing test_wc_curve25519_size.
  18530. */
  18531. static int test_wc_curve25519_size (void)
  18532. {
  18533. int ret = 0;
  18534. #if defined(HAVE_CURVE25519)
  18535. curve25519_key key;
  18536. printf(testingFmt, "wc_curve25519_size()");
  18537. ret = wc_curve25519_init(&key);
  18538. /* Test good args for wc_curve25519_size */
  18539. if (ret == 0) {
  18540. ret = wc_curve25519_size(&key);
  18541. }
  18542. /* Test bad args for wc_curve25519_size */
  18543. if (ret != 0) {
  18544. ret = wc_curve25519_size(NULL);
  18545. }
  18546. printf(resultFmt, ret == 0 ? passed : failed);
  18547. wc_curve25519_free(&key);
  18548. #endif
  18549. return ret;
  18550. } /* END test_wc_curve25519_size*/
  18551. /*
  18552. * Testing test_wc_curve25519_export_key_raw().
  18553. */
  18554. static int test_wc_curve25519_export_key_raw (void)
  18555. {
  18556. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  18557. curve25519_key key;
  18558. WC_RNG rng;
  18559. byte privateKey[CURVE25519_KEYSIZE];
  18560. byte publicKey[CURVE25519_KEYSIZE];
  18561. word32 prvkSz;
  18562. word32 pubkSz;
  18563. byte prik[CURVE25519_KEYSIZE];
  18564. byte pubk[CURVE25519_KEYSIZE];
  18565. word32 prksz;
  18566. word32 pbksz;
  18567. printf(testingFmt, "wc_curve25519_export_key_raw()");
  18568. if(0 != wc_InitRng(&rng)){
  18569. printf(testingFmt, "failed due to wc_InitRng");
  18570. fflush( stdout );
  18571. return 1;
  18572. }
  18573. if(0 != wc_curve25519_init(&key)){
  18574. printf(testingFmt, "failed due to wc_curve25519_init");
  18575. fflush( stdout );
  18576. wc_FreeRng(&rng);
  18577. return 1;
  18578. }
  18579. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  18580. printf(testingFmt, "failed due to wc_curve25519_make_key");
  18581. fflush( stdout );
  18582. wc_curve25519_free(&key);
  18583. wc_FreeRng(&rng);
  18584. return 1;
  18585. }
  18586. /*
  18587. bad-argument-test cases
  18588. target function sould return BAD_FUNC_ARG
  18589. */
  18590. prvkSz = CURVE25519_KEYSIZE;
  18591. pubkSz = CURVE25519_KEYSIZE;
  18592. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18593. NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
  18594. printf(testingFmt,"failed at bad-arg-case-1.");
  18595. fflush( stdout );
  18596. wc_curve25519_free(&key);
  18597. wc_FreeRng(&rng);
  18598. return 1;
  18599. }
  18600. prvkSz = CURVE25519_KEYSIZE;
  18601. pubkSz = CURVE25519_KEYSIZE;
  18602. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18603. &key , NULL, &prvkSz, publicKey, &pubkSz)){
  18604. printf(testingFmt,"failed at bad-arg-case-2.");
  18605. fflush( stdout );
  18606. wc_curve25519_free(&key);
  18607. wc_FreeRng(&rng);
  18608. return 1;
  18609. }
  18610. prvkSz = CURVE25519_KEYSIZE;
  18611. pubkSz = CURVE25519_KEYSIZE;
  18612. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18613. &key , privateKey, NULL, publicKey, &pubkSz)){
  18614. printf(testingFmt,"failed at bad-arg-case-3.");
  18615. fflush( stdout );
  18616. wc_curve25519_free(&key);
  18617. wc_FreeRng(&rng);
  18618. return 1;
  18619. }
  18620. /* prvkSz = CURVE25519_KEYSIZE; */
  18621. pubkSz = CURVE25519_KEYSIZE;
  18622. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18623. &key , privateKey, &prvkSz, NULL, &pubkSz)){
  18624. printf(testingFmt,"failed at bad-arg-case-4.");
  18625. fflush( stdout );
  18626. wc_curve25519_free(&key);
  18627. wc_FreeRng(&rng);
  18628. return 1;
  18629. }
  18630. prvkSz = CURVE25519_KEYSIZE;
  18631. pubkSz = CURVE25519_KEYSIZE;
  18632. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18633. &key , privateKey, &prvkSz, publicKey, NULL )){
  18634. printf(testingFmt,"failed at bad-arg-case-5.");
  18635. fflush( stdout );
  18636. wc_curve25519_free(&key);
  18637. wc_FreeRng(&rng);
  18638. return 1;
  18639. }
  18640. /*
  18641. cross-testing
  18642. */
  18643. prksz = CURVE25519_KEYSIZE;
  18644. if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
  18645. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  18646. fflush( stdout );
  18647. wc_curve25519_free(&key);
  18648. wc_FreeRng(&rng);
  18649. return 1;
  18650. }
  18651. pbksz = CURVE25519_KEYSIZE;
  18652. if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
  18653. printf(testingFmt,"failed due to wc_curve25519_export_public");
  18654. fflush( stdout );
  18655. wc_curve25519_free(&key);
  18656. wc_FreeRng(&rng);
  18657. return 1;
  18658. }
  18659. prvkSz = CURVE25519_KEYSIZE;
  18660. /* pubkSz = CURVE25519_KEYSIZE; */
  18661. if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  18662. publicKey, &pubkSz)){
  18663. printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
  18664. fflush( stdout );
  18665. wc_curve25519_free(&key);
  18666. wc_FreeRng(&rng);
  18667. return 1;
  18668. }
  18669. if((prksz == CURVE25519_KEYSIZE) &&
  18670. (pbksz == CURVE25519_KEYSIZE) &&
  18671. (prvkSz == CURVE25519_KEYSIZE) &&
  18672. (pubkSz == CURVE25519_KEYSIZE)){
  18673. if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) &&
  18674. 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){
  18675. printf(resultFmt,passed);
  18676. fflush( stdout );
  18677. wc_curve25519_free(&key);
  18678. wc_FreeRng(&rng);
  18679. return 0;
  18680. }
  18681. else{
  18682. printf(testingFmt,"failed due to key-contents-inconsistency.");
  18683. fflush( stdout );
  18684. wc_curve25519_free(&key);
  18685. wc_FreeRng(&rng);
  18686. return 1;
  18687. }
  18688. }
  18689. else{
  18690. printf(testingFmt,"failed due to bad-key-size.");
  18691. fflush( stdout );
  18692. wc_curve25519_free(&key);
  18693. wc_FreeRng(&rng);
  18694. return 1;
  18695. }
  18696. #endif
  18697. fflush( stdout );
  18698. return 0;
  18699. } /* end of test_wc_curve25519_export_key_raw */
  18700. /*
  18701. * Testing test_wc_curve25519_export_key_raw_ex().
  18702. */
  18703. static int test_wc_curve25519_export_key_raw_ex (void)
  18704. {
  18705. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  18706. curve25519_key key;
  18707. WC_RNG rng;
  18708. byte privateKey[CURVE25519_KEYSIZE];
  18709. byte publicKey[CURVE25519_KEYSIZE];
  18710. word32 prvkSz;
  18711. word32 pubkSz;
  18712. byte prik[CURVE25519_KEYSIZE];
  18713. byte pubk[CURVE25519_KEYSIZE];
  18714. word32 prksz;
  18715. word32 pbksz;
  18716. printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
  18717. if(0 != wc_InitRng(&rng)){
  18718. printf(testingFmt, "failed due to wc_InitRng");
  18719. fflush( stdout );
  18720. return 1;
  18721. }
  18722. if(0 != wc_curve25519_init(&key)){
  18723. printf(testingFmt, "failed due to wc_curve25519_init");
  18724. fflush( stdout );
  18725. wc_FreeRng(&rng);
  18726. return 1;
  18727. }
  18728. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  18729. printf(testingFmt, "failed due to wc_curve25519_make_key");
  18730. fflush( stdout );
  18731. wc_curve25519_free(&key);
  18732. wc_FreeRng(&rng);
  18733. return 1;
  18734. }
  18735. /*
  18736. bad-argument-test cases
  18737. target function sould return BAD_FUNC_ARG
  18738. */
  18739. prvkSz = CURVE25519_KEYSIZE;
  18740. pubkSz = CURVE25519_KEYSIZE;
  18741. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  18742. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  18743. printf(testingFmt,"failed at bad-arg-case-1.");
  18744. fflush( stdout );
  18745. wc_curve25519_free(&key);
  18746. wc_FreeRng(&rng);
  18747. return 1;
  18748. }
  18749. prvkSz = CURVE25519_KEYSIZE;
  18750. pubkSz = CURVE25519_KEYSIZE;
  18751. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  18752. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  18753. printf(testingFmt,"failed at bad-arg-case-2.");
  18754. fflush( stdout );
  18755. wc_curve25519_free(&key);
  18756. wc_FreeRng(&rng);
  18757. return 1;
  18758. }
  18759. prvkSz = CURVE25519_KEYSIZE;
  18760. pubkSz = CURVE25519_KEYSIZE;
  18761. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  18762. NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
  18763. printf(testingFmt,"failed at bad-arg-case-3.");
  18764. fflush( stdout );
  18765. wc_curve25519_free(&key);
  18766. wc_FreeRng(&rng);
  18767. return 1;
  18768. }
  18769. /* prvkSz = CURVE25519_KEYSIZE; */
  18770. pubkSz = CURVE25519_KEYSIZE;
  18771. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18772. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
  18773. printf(testingFmt,"failed at bad-arg-case-4.");
  18774. fflush( stdout );
  18775. wc_curve25519_free(&key);
  18776. wc_FreeRng(&rng);
  18777. return 1;
  18778. }
  18779. prvkSz = CURVE25519_KEYSIZE;
  18780. pubkSz = CURVE25519_KEYSIZE;
  18781. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18782. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
  18783. printf(testingFmt,"failed at bad-arg-case-5.");
  18784. fflush( stdout );
  18785. wc_curve25519_free(&key);
  18786. wc_FreeRng(&rng);
  18787. return 1;
  18788. }
  18789. prvkSz = CURVE25519_KEYSIZE;
  18790. /* pubkSz = CURVE25519_KEYSIZE; */
  18791. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  18792. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  18793. printf(testingFmt,"failed at bad-arg-case-6.");
  18794. fflush( stdout );
  18795. wc_curve25519_free(&key);
  18796. wc_FreeRng(&rng);
  18797. return 1;
  18798. }
  18799. prvkSz = CURVE25519_KEYSIZE;
  18800. pubkSz = CURVE25519_KEYSIZE;
  18801. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
  18802. publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  18803. printf(testingFmt,"failed at bad-arg-case-7.");
  18804. fflush( stdout );
  18805. wc_curve25519_free(&key);
  18806. wc_FreeRng(&rng);
  18807. return 1;
  18808. }
  18809. prvkSz = CURVE25519_KEYSIZE;
  18810. pubkSz = CURVE25519_KEYSIZE;
  18811. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18812. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  18813. printf(testingFmt,"failed at bad-arg-case-8.");
  18814. fflush( stdout );
  18815. wc_curve25519_free(&key);
  18816. wc_FreeRng(&rng);
  18817. return 1;
  18818. }
  18819. /* prvkSz = CURVE25519_KEYSIZE; */
  18820. pubkSz = CURVE25519_KEYSIZE;
  18821. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18822. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
  18823. printf(testingFmt,"failed at bad-arg-case-9.");
  18824. fflush( stdout );
  18825. wc_curve25519_free(&key);
  18826. wc_FreeRng(&rng);
  18827. return 1;
  18828. }
  18829. prvkSz = CURVE25519_KEYSIZE;
  18830. pubkSz = CURVE25519_KEYSIZE;
  18831. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18832. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
  18833. printf(testingFmt,"failed at bad-arg-case-10.");
  18834. fflush( stdout );
  18835. wc_curve25519_free(&key);
  18836. wc_FreeRng(&rng);
  18837. return 1;
  18838. }
  18839. /* illegal value for endien */
  18840. prvkSz = CURVE25519_KEYSIZE;
  18841. /* pubkSz = CURVE25519_KEYSIZE; */
  18842. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  18843. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){
  18844. printf(testingFmt,"failed at bad-arg-case-11.");
  18845. fflush( stdout );
  18846. wc_curve25519_free(&key);
  18847. wc_FreeRng(&rng);
  18848. return 1;
  18849. }
  18850. /*
  18851. cross-testing
  18852. */
  18853. prksz = CURVE25519_KEYSIZE;
  18854. if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
  18855. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  18856. fflush( stdout );
  18857. wc_curve25519_free(&key);
  18858. wc_FreeRng(&rng);
  18859. return 1;
  18860. }
  18861. pbksz = CURVE25519_KEYSIZE;
  18862. if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
  18863. printf(testingFmt,"failed due to wc_curve25519_export_public");
  18864. fflush( stdout );
  18865. wc_curve25519_free(&key);
  18866. wc_FreeRng(&rng);
  18867. return 1;
  18868. }
  18869. prvkSz = CURVE25519_KEYSIZE;
  18870. /* pubkSz = CURVE25519_KEYSIZE; */
  18871. if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  18872. publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  18873. printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
  18874. fflush( stdout );
  18875. wc_curve25519_free(&key);
  18876. wc_FreeRng(&rng);
  18877. return 1;
  18878. }
  18879. if( prksz == CURVE25519_KEYSIZE &&
  18880. pbksz == CURVE25519_KEYSIZE &&
  18881. prvkSz == CURVE25519_KEYSIZE &&
  18882. pubkSz == CURVE25519_KEYSIZE ){
  18883. if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) &&
  18884. 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){
  18885. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  18886. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  18887. if( prvkSz == CURVE25519_KEYSIZE &&
  18888. pubkSz == CURVE25519_KEYSIZE ){
  18889. ; /* proceed to the next test */
  18890. }
  18891. else{
  18892. printf(testingFmt,"failed due to key-size-inconsistency");
  18893. fflush( stdout );
  18894. wc_curve25519_free(&key);
  18895. wc_FreeRng(&rng);
  18896. return 1;
  18897. }
  18898. }
  18899. else{
  18900. printf(testingFmt,
  18901. "failed due to wc_curve25519_export_key_raw_ex");
  18902. fflush( stdout );
  18903. wc_curve25519_free(&key);
  18904. wc_FreeRng(&rng);
  18905. return 1;
  18906. }
  18907. }
  18908. else{
  18909. printf(testingFmt,"failed due to key-contents-inconsistency");
  18910. fflush( stdout );
  18911. wc_curve25519_free(&key);
  18912. wc_FreeRng(&rng);
  18913. return 1;
  18914. }
  18915. }
  18916. else{
  18917. printf(testingFmt,"failed due to bad-key-size");
  18918. fflush( stdout );
  18919. wc_curve25519_free(&key);
  18920. wc_FreeRng(&rng);
  18921. return 1;
  18922. }
  18923. /*
  18924. try once with another endian
  18925. */
  18926. prvkSz = CURVE25519_KEYSIZE;
  18927. pubkSz = CURVE25519_KEYSIZE;
  18928. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  18929. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  18930. if( prvkSz == CURVE25519_KEYSIZE &&
  18931. pubkSz == CURVE25519_KEYSIZE ){
  18932. /* no more test*/
  18933. printf(resultFmt, passed );
  18934. fflush( stdout );
  18935. wc_curve25519_free(&key);
  18936. wc_FreeRng(&rng);
  18937. return 0;
  18938. }
  18939. else{
  18940. printf(testingFmt,"failed due to key-size-inconsistency");
  18941. fflush( stdout );
  18942. wc_curve25519_free(&key);
  18943. wc_FreeRng(&rng);
  18944. return 1;
  18945. }
  18946. }
  18947. else{
  18948. printf(testingFmt,
  18949. "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
  18950. fflush( stdout );
  18951. wc_curve25519_free(&key);
  18952. wc_FreeRng(&rng);
  18953. return 1;
  18954. }
  18955. #else
  18956. return 0;
  18957. #endif
  18958. } /* end of test_wc_curve25519_export_key_raw_ex */
  18959. /*
  18960. * Testing wc_curve25519_make_key
  18961. */
  18962. static int test_wc_curve25519_make_key (void)
  18963. {
  18964. int ret = 0;
  18965. #if defined(HAVE_CURVE25519)
  18966. WC_RNG rng;
  18967. curve25519_key key;
  18968. int keysize;
  18969. printf(testingFmt, "wc_curve25519_make_key()");
  18970. ret = wc_curve25519_init(&key);
  18971. if (ret == 0) {
  18972. ret = wc_InitRng(&rng);
  18973. }
  18974. if (ret == 0) {
  18975. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  18976. if (ret == 0) {
  18977. keysize = wc_curve25519_size(&key);
  18978. if (keysize != CURVE25519_KEYSIZE) {
  18979. ret = WOLFSSL_FATAL_ERROR;
  18980. }
  18981. }
  18982. if (ret == 0) {
  18983. ret = wc_curve25519_make_key(&rng, keysize, &key);
  18984. }
  18985. }
  18986. /*test bad cases*/
  18987. if (ret == 0) {
  18988. ret = wc_curve25519_make_key(NULL, 0, NULL);
  18989. if (ret == BAD_FUNC_ARG) {
  18990. ret = 0;
  18991. }
  18992. }
  18993. if (ret == 0) {
  18994. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  18995. if (ret == BAD_FUNC_ARG) {
  18996. ret = 0;
  18997. }
  18998. }
  18999. if (ret == 0) {
  19000. ret = wc_curve25519_make_key(NULL, keysize, &key);
  19001. if (ret == BAD_FUNC_ARG) {
  19002. ret = 0;
  19003. }
  19004. }
  19005. if (ret == 0) {
  19006. ret = wc_curve25519_make_key(&rng, 0, &key);
  19007. if (ret == ECC_BAD_ARG_E) {
  19008. ret = 0;
  19009. }
  19010. }
  19011. printf(resultFmt, ret == 0 ? passed : failed);
  19012. wc_curve25519_free(&key);
  19013. wc_FreeRng(&rng);
  19014. #endif
  19015. return ret;
  19016. } /*END test_wc_curve25519_make_key*/
  19017. /*
  19018. * Testing wc_curve25519_shared_secret_ex
  19019. */
  19020. static int test_wc_curve25519_shared_secret_ex(void)
  19021. {
  19022. int ret = 0;
  19023. #if defined(HAVE_CURVE25519)
  19024. WC_RNG rng;
  19025. curve25519_key private_key, public_key;
  19026. byte out[CURVE25519_KEYSIZE];
  19027. word32 outLen = sizeof(out);
  19028. int endian = EC25519_BIG_ENDIAN;
  19029. printf(testingFmt, "wc_curve25519_shared_secret_ex()");
  19030. ret = wc_curve25519_init(&private_key);
  19031. if (ret == 0) {
  19032. ret = wc_curve25519_init(&public_key);
  19033. }
  19034. if (ret == 0) {
  19035. ret = wc_InitRng(&rng);
  19036. }
  19037. if (ret == 0) {
  19038. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  19039. }
  19040. if (ret == 0) {
  19041. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  19042. }
  19043. if (ret == 0) {
  19044. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19045. &outLen, endian);
  19046. }
  19047. /*test bad cases*/
  19048. if (ret == 0) {
  19049. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  19050. 0, endian);
  19051. if (ret == 0) {
  19052. ret = -1;
  19053. }
  19054. if (ret == BAD_FUNC_ARG) {
  19055. ret = 0;
  19056. }
  19057. }
  19058. if (ret == 0) {
  19059. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  19060. &outLen, endian);
  19061. if (ret == 0) {
  19062. ret = -1;
  19063. }
  19064. else if (ret == BAD_FUNC_ARG) {
  19065. ret = 0;
  19066. }
  19067. }
  19068. if (ret == 0) {
  19069. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  19070. &outLen, endian);
  19071. if (ret == 0) {
  19072. ret = -1;
  19073. }
  19074. else if (ret == BAD_FUNC_ARG) {
  19075. ret = 0;
  19076. }
  19077. }
  19078. if (ret == 0) {
  19079. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19080. &outLen, endian);
  19081. if (ret == 0) {
  19082. ret = -1;
  19083. }
  19084. else if (ret == BAD_FUNC_ARG) {
  19085. ret = 0;
  19086. }
  19087. }
  19088. if (ret == 0) {
  19089. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19090. NULL, endian);
  19091. if (ret == 0) {
  19092. ret = -1;
  19093. }
  19094. else if (ret == BAD_FUNC_ARG) {
  19095. ret = 0;
  19096. }
  19097. }
  19098. if (ret == 0) {
  19099. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  19100. *increasing to 0x8f checks for error being returned*/
  19101. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  19102. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19103. &outLen, endian);
  19104. if (ret == 0) {
  19105. ret = -1;
  19106. }
  19107. else if (ret == ECC_BAD_ARG_E) {
  19108. ret = 0;
  19109. }
  19110. }
  19111. outLen = outLen - 2;
  19112. if (ret == 0) {
  19113. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19114. &outLen, endian);
  19115. if (ret == 0) {
  19116. ret = -1;
  19117. }
  19118. else if (ret == BAD_FUNC_ARG) {
  19119. ret = 0;
  19120. }
  19121. }
  19122. printf(resultFmt, ret == 0 ? passed : failed);
  19123. wc_curve25519_free(&private_key);
  19124. wc_curve25519_free(&public_key);
  19125. wc_FreeRng(&rng);
  19126. #endif
  19127. return ret;
  19128. } /*END test_wc_curve25519_shared_secret_ex*/
  19129. /*
  19130. * Testing wc_curve25519_make_pub
  19131. */
  19132. static int test_wc_curve25519_make_pub(void)
  19133. {
  19134. int ret = 0;
  19135. #ifdef HAVE_CURVE25519
  19136. WC_RNG rng;
  19137. curve25519_key key;
  19138. byte out[CURVE25519_KEYSIZE];
  19139. printf(testingFmt, "wc_curve25519_make_pub()");
  19140. ret = wc_curve25519_init(&key);
  19141. if (ret == 0) {
  19142. ret = wc_InitRng(&rng);
  19143. if (ret == 0) {
  19144. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19145. }
  19146. }
  19147. if (ret == 0) {
  19148. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  19149. }
  19150. /*test bad cases*/
  19151. if (ret == 0) {
  19152. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  19153. if (ret == ECC_BAD_ARG_E) {
  19154. ret = 0;
  19155. }
  19156. }
  19157. if (ret == 0) {
  19158. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  19159. if (ret == ECC_BAD_ARG_E) {
  19160. ret = 0;
  19161. }
  19162. }
  19163. if (ret == 0) {
  19164. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  19165. if (ret == ECC_BAD_ARG_E) {
  19166. ret = 0;
  19167. }
  19168. }
  19169. if (ret == 0) {
  19170. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  19171. if (ret == ECC_BAD_ARG_E) {
  19172. ret = 0;
  19173. }
  19174. }
  19175. if (ret == 0) {
  19176. /* verify clamping test */
  19177. key.k[0] |= ~248;
  19178. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19179. if (ret == ECC_BAD_ARG_E) {
  19180. ret = 0;
  19181. }
  19182. key.k[0] &= 248;
  19183. }
  19184. /* repeat the expected-to-succeed test. */
  19185. if (ret == 0) {
  19186. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19187. }
  19188. printf(resultFmt, ret == 0 ? passed : failed);
  19189. wc_curve25519_free(&key);
  19190. wc_FreeRng(&rng);
  19191. #endif
  19192. return ret;
  19193. } /*END test_wc_curve25519_make_pub */
  19194. /*
  19195. * Testing test_wc_curve25519_export_public_ex
  19196. */
  19197. static int test_wc_curve25519_export_public_ex (void)
  19198. {
  19199. int ret = 0;
  19200. #if defined(HAVE_CURVE25519)
  19201. WC_RNG rng;
  19202. curve25519_key key;
  19203. byte out[CURVE25519_KEYSIZE];
  19204. word32 outLen = sizeof(out);
  19205. int endian = EC25519_BIG_ENDIAN;
  19206. printf(testingFmt, "wc_curve25519_export_public_ex()");
  19207. ret = wc_curve25519_init(&key);
  19208. if (ret == 0) {
  19209. ret = wc_InitRng(&rng);
  19210. }
  19211. if (ret == 0) {
  19212. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19213. if (ret == 0) {
  19214. ret = wc_curve25519_export_public(&key, out, &outLen);
  19215. }
  19216. if (ret == 0) {
  19217. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  19218. }
  19219. }
  19220. /*test bad cases*/
  19221. if (ret == 0) {
  19222. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  19223. if (ret == BAD_FUNC_ARG) {
  19224. ret = 0;
  19225. }
  19226. }
  19227. if (ret == 0) {
  19228. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  19229. if (ret == BAD_FUNC_ARG) {
  19230. ret = 0;
  19231. }
  19232. }
  19233. if (ret == 0) {
  19234. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  19235. if (ret == BAD_FUNC_ARG) {
  19236. ret = 0;
  19237. }
  19238. }
  19239. if (ret == 0) {
  19240. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  19241. if (ret == BAD_FUNC_ARG) {
  19242. ret = 0;
  19243. }
  19244. }
  19245. outLen = outLen - 2;
  19246. if (ret == 0) {
  19247. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  19248. if (ret == ECC_BAD_ARG_E) {
  19249. ret = 0;
  19250. }
  19251. }
  19252. printf(resultFmt, ret == 0 ? passed : failed);
  19253. wc_curve25519_free(&key);
  19254. wc_FreeRng(&rng);
  19255. #endif
  19256. return ret;
  19257. } /*END test_wc_curve25519_export_public_ex*/
  19258. /*
  19259. * Testing test_wc_curve25519_import_private_raw_ex
  19260. */
  19261. static int test_wc_curve25519_import_private_raw_ex (void)
  19262. {
  19263. int ret = 0;
  19264. #if defined(HAVE_CURVE25519)
  19265. WC_RNG rng;
  19266. curve25519_key key;
  19267. byte priv[CURVE25519_KEYSIZE];
  19268. byte pub[CURVE25519_KEYSIZE];
  19269. word32 privSz = sizeof(priv);
  19270. word32 pubSz = sizeof(pub);
  19271. int endian = EC25519_BIG_ENDIAN;
  19272. printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
  19273. ret = wc_curve25519_init(&key);
  19274. if (ret == 0) {
  19275. ret = wc_InitRng(&rng);
  19276. }
  19277. if (ret == 0) {
  19278. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19279. if (ret == 0) {
  19280. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  19281. }
  19282. if (ret == 0) {
  19283. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  19284. }
  19285. if (ret == 0) {
  19286. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19287. &key, endian);
  19288. }
  19289. }
  19290. /*test bad cases*/
  19291. if (ret == 0) {
  19292. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  19293. endian);
  19294. if (ret == BAD_FUNC_ARG) {
  19295. ret = 0;
  19296. }
  19297. }
  19298. if (ret == 0) {
  19299. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  19300. &key, endian);
  19301. if (ret == BAD_FUNC_ARG) {
  19302. ret = 0;
  19303. }
  19304. }
  19305. if (ret == 0) {
  19306. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  19307. &key, endian);
  19308. if (ret == BAD_FUNC_ARG) {
  19309. ret = 0;
  19310. }
  19311. }
  19312. if (ret == 0) {
  19313. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19314. NULL, endian);
  19315. if (ret == BAD_FUNC_ARG) {
  19316. ret = 0;
  19317. }
  19318. }
  19319. if (ret == 0) {
  19320. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  19321. &key, endian);
  19322. if (ret == ECC_BAD_ARG_E) {
  19323. ret = 0;
  19324. }
  19325. }
  19326. if (ret == 0) {
  19327. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  19328. &key, endian);
  19329. if (ret == ECC_BAD_ARG_E) {
  19330. ret = 0;
  19331. }
  19332. }
  19333. if (ret == 0) {
  19334. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19335. &key, EC25519_LITTLE_ENDIAN);
  19336. }
  19337. printf(resultFmt, ret == 0 ? passed : failed);
  19338. wc_curve25519_free(&key);
  19339. wc_FreeRng(&rng);
  19340. #endif
  19341. return ret;
  19342. } /*END test_wc_curve25519_import_private_raw_ex*/
  19343. /*
  19344. * Testing test_wc_curve25519_import_private
  19345. */
  19346. static int test_wc_curve25519_import_private (void)
  19347. {
  19348. int ret = 0;
  19349. #if defined(HAVE_CURVE25519)
  19350. curve25519_key key;
  19351. WC_RNG rng;
  19352. byte priv[CURVE25519_KEYSIZE];
  19353. word32 privSz = sizeof(priv);
  19354. printf(testingFmt, "wc_curve25519_import_private()");
  19355. ret = wc_curve25519_init(&key);
  19356. if (ret == 0) {
  19357. ret = wc_InitRng(&rng);
  19358. }
  19359. if (ret == 0) {
  19360. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19361. if (ret == 0) {
  19362. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  19363. }
  19364. }
  19365. if (ret == 0) {
  19366. ret = wc_curve25519_import_private(priv, privSz, &key);
  19367. }
  19368. printf(resultFmt, ret == 0 ? passed : failed);
  19369. wc_curve25519_free(&key);
  19370. wc_FreeRng(&rng);
  19371. #endif
  19372. return ret;
  19373. } /*END test_wc_curve25519_import*/
  19374. /*
  19375. * Testing test_wc_curve25519_export_private_raw_ex
  19376. */
  19377. static int test_wc_curve25519_export_private_raw_ex (void)
  19378. {
  19379. int ret = 0;
  19380. #if defined(HAVE_CURVE25519)
  19381. curve25519_key key;
  19382. byte out[CURVE25519_KEYSIZE];
  19383. word32 outLen = sizeof(out);
  19384. int endian = EC25519_BIG_ENDIAN;
  19385. printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
  19386. ret = wc_curve25519_init(&key);
  19387. if (ret == 0) {
  19388. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  19389. }
  19390. /*test bad cases*/
  19391. if (ret == 0) {
  19392. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  19393. if (ret == BAD_FUNC_ARG) {
  19394. ret = 0;
  19395. }
  19396. }
  19397. if (ret == 0) {
  19398. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  19399. if (ret == BAD_FUNC_ARG) {
  19400. ret = 0;
  19401. }
  19402. }
  19403. if (ret == 0) {
  19404. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  19405. if (ret == BAD_FUNC_ARG) {
  19406. ret = 0;
  19407. }
  19408. }
  19409. if (ret == 0) {
  19410. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  19411. if (ret == BAD_FUNC_ARG) {
  19412. ret = 0;
  19413. }
  19414. }
  19415. if (ret == 0) {
  19416. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  19417. EC25519_LITTLE_ENDIAN);
  19418. }
  19419. outLen = outLen - 2;
  19420. if (ret == 0) {
  19421. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  19422. if (ret == ECC_BAD_ARG_E) {
  19423. ret = 0;
  19424. }
  19425. }
  19426. printf(resultFmt, ret == 0 ? passed : failed);
  19427. wc_curve25519_free(&key);
  19428. #endif
  19429. return ret;
  19430. }/*END test_wc_curve25519_export_private_raw_ex*/
  19431. /*
  19432. * Testing wc_ed448_make_key().
  19433. */
  19434. static int test_wc_ed448_make_key (void)
  19435. {
  19436. int ret = 0;
  19437. #if defined(HAVE_ED448)
  19438. ed448_key key;
  19439. WC_RNG rng;
  19440. ret = wc_InitRng(&rng);
  19441. if (ret == 0) {
  19442. ret = wc_ed448_init(&key);
  19443. }
  19444. printf(testingFmt, "wc_ed448_make_key()");
  19445. if (ret == 0) {
  19446. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19447. }
  19448. /* Test bad args. */
  19449. if (ret == 0) {
  19450. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  19451. if (ret == BAD_FUNC_ARG) {
  19452. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  19453. }
  19454. if (ret == BAD_FUNC_ARG) {
  19455. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  19456. }
  19457. if (ret == BAD_FUNC_ARG) {
  19458. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  19459. }
  19460. if (ret == BAD_FUNC_ARG) {
  19461. ret = 0;
  19462. } else if (ret == 0) {
  19463. ret = WOLFSSL_FATAL_ERROR;
  19464. }
  19465. }
  19466. printf(resultFmt, ret == 0 ? passed : failed);
  19467. if (wc_FreeRng(&rng) && ret == 0) {
  19468. ret = WOLFSSL_FATAL_ERROR;
  19469. }
  19470. wc_ed448_free(&key);
  19471. #endif
  19472. return ret;
  19473. } /* END test_wc_ed448_make_key */
  19474. /*
  19475. * Testing wc_ed448_init()
  19476. */
  19477. static int test_wc_ed448_init (void)
  19478. {
  19479. int ret = 0;
  19480. #if defined(HAVE_ED448)
  19481. ed448_key key;
  19482. printf(testingFmt, "wc_ed448_init()");
  19483. ret = wc_ed448_init(&key);
  19484. /* Test bad args. */
  19485. if (ret == 0) {
  19486. ret = wc_ed448_init(NULL);
  19487. if (ret == BAD_FUNC_ARG) {
  19488. ret = 0;
  19489. } else if (ret == 0) {
  19490. ret = WOLFSSL_FATAL_ERROR;
  19491. }
  19492. }
  19493. printf(resultFmt, ret == 0 ? passed : failed);
  19494. wc_ed448_free(&key);
  19495. #endif
  19496. return ret;
  19497. } /* END test_wc_ed448_init */
  19498. /*
  19499. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  19500. */
  19501. static int test_wc_ed448_sign_msg (void)
  19502. {
  19503. int ret = 0;
  19504. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  19505. WC_RNG rng;
  19506. ed448_key key;
  19507. byte msg[] = "Everybody gets Friday off.\n";
  19508. byte sig[ED448_SIG_SIZE];
  19509. word32 msglen = sizeof(msg);
  19510. word32 siglen = sizeof(sig);
  19511. word32 badSigLen = sizeof(sig) - 1;
  19512. #ifdef HAVE_ED448_VERIFY
  19513. int verify_ok = 0; /*1 = Verify success.*/
  19514. #endif
  19515. /* Initialize stack variables. */
  19516. XMEMSET(sig, 0, siglen);
  19517. /* Initialize key. */
  19518. ret = wc_InitRng(&rng);
  19519. if (ret == 0) {
  19520. ret = wc_ed448_init(&key);
  19521. if (ret == 0) {
  19522. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19523. }
  19524. }
  19525. printf(testingFmt, "wc_ed448_sign_msg()");
  19526. if (ret == 0) {
  19527. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  19528. }
  19529. /* Test bad args. */
  19530. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  19531. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  19532. if (ret == BAD_FUNC_ARG) {
  19533. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  19534. }
  19535. if (ret == BAD_FUNC_ARG) {
  19536. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  19537. }
  19538. if (ret == BAD_FUNC_ARG) {
  19539. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  19540. }
  19541. if (ret == BAD_FUNC_ARG) {
  19542. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  19543. NULL, 0);
  19544. }
  19545. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  19546. badSigLen -= 1;
  19547. ret = 0;
  19548. } else if (ret == 0) {
  19549. ret = WOLFSSL_FATAL_ERROR;
  19550. }
  19551. } /* END sign */
  19552. printf(resultFmt, ret == 0 ? passed : failed);
  19553. #ifdef HAVE_ED448_VERIFY
  19554. printf(testingFmt, "wc_ed448_verify_msg()");
  19555. if (ret == 0) {
  19556. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  19557. &key, NULL, 0);
  19558. if (ret == 0 && verify_ok == 1) {
  19559. ret = 0;
  19560. } else if (ret == 0) {
  19561. ret = WOLFSSL_FATAL_ERROR;
  19562. }
  19563. /* Test bad args. */
  19564. if (ret == 0) {
  19565. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  19566. msglen, &verify_ok, &key,
  19567. NULL, 0),
  19568. BAD_FUNC_ARG);
  19569. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  19570. msglen, &verify_ok, &key,
  19571. NULL, 0),
  19572. BAD_FUNC_ARG);
  19573. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19574. &key, NULL, 0);
  19575. if (ret == BAD_FUNC_ARG) {
  19576. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  19577. &verify_ok, &key, NULL, 0);
  19578. }
  19579. if (ret == BAD_FUNC_ARG) {
  19580. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  19581. NULL, &key, NULL, 0);
  19582. }
  19583. if (ret == BAD_FUNC_ARG) {
  19584. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  19585. &verify_ok, NULL, NULL, 0);
  19586. }
  19587. if (ret == BAD_FUNC_ARG) {
  19588. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  19589. &verify_ok, &key, NULL, 0);
  19590. }
  19591. if (ret == BAD_FUNC_ARG) {
  19592. ret = 0;
  19593. } else if (ret == 0) {
  19594. ret = WOLFSSL_FATAL_ERROR;
  19595. }
  19596. }
  19597. } /* END verify. */
  19598. printf(resultFmt, ret == 0 ? passed : failed);
  19599. #endif /* Verify. */
  19600. if (wc_FreeRng(&rng) && ret == 0) {
  19601. ret = WOLFSSL_FATAL_ERROR;
  19602. }
  19603. wc_ed448_free(&key);
  19604. #endif
  19605. return ret;
  19606. } /* END test_wc_ed448_sign_msg */
  19607. /*
  19608. * Testing wc_ed448_import_public()
  19609. */
  19610. static int test_wc_ed448_import_public (void)
  19611. {
  19612. int ret = 0;
  19613. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19614. WC_RNG rng;
  19615. ed448_key pubKey;
  19616. const byte in[] =
  19617. "Ed448PublicKeyUnitTest.................................\n";
  19618. word32 inlen = sizeof(in);
  19619. ret = wc_InitRng(&rng);
  19620. if (ret == 0) {
  19621. ret = wc_ed448_init(&pubKey);
  19622. if (ret == 0) {
  19623. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  19624. }
  19625. }
  19626. printf(testingFmt, "wc_ed448_import_public()");
  19627. if (ret == 0) {
  19628. ret = wc_ed448_import_public(in, inlen, &pubKey);
  19629. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  19630. ret = 0;
  19631. } else {
  19632. ret = WOLFSSL_FATAL_ERROR;
  19633. }
  19634. /* Test bad args. */
  19635. if (ret == 0) {
  19636. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  19637. if (ret == BAD_FUNC_ARG) {
  19638. ret = wc_ed448_import_public(in, inlen, NULL);
  19639. }
  19640. if (ret == BAD_FUNC_ARG) {
  19641. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  19642. }
  19643. if (ret == BAD_FUNC_ARG) {
  19644. ret = 0;
  19645. } else if (ret == 0) {
  19646. ret = WOLFSSL_FATAL_ERROR;
  19647. }
  19648. }
  19649. }
  19650. printf(resultFmt, ret == 0 ? passed : failed);
  19651. if (wc_FreeRng(&rng) && ret == 0) {
  19652. ret = WOLFSSL_FATAL_ERROR;
  19653. }
  19654. wc_ed448_free(&pubKey);
  19655. #endif
  19656. return ret;
  19657. } /* END wc_ed448_import_public */
  19658. /*
  19659. * Testing wc_ed448_import_private_key()
  19660. */
  19661. static int test_wc_ed448_import_private_key (void)
  19662. {
  19663. int ret = 0;
  19664. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19665. WC_RNG rng;
  19666. ed448_key key;
  19667. const byte privKey[] =
  19668. "Ed448PrivateKeyUnitTest................................\n";
  19669. const byte pubKey[] =
  19670. "Ed448PublicKeyUnitTest.................................\n";
  19671. word32 privKeySz = sizeof(privKey);
  19672. word32 pubKeySz = sizeof(pubKey);
  19673. #ifdef HAVE_ED448_KEY_EXPORT
  19674. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19675. word32 bothKeysSz = sizeof(bothKeys);
  19676. #endif
  19677. ret = wc_InitRng(&rng);
  19678. if (ret != 0) {
  19679. return ret;
  19680. }
  19681. ret = wc_ed448_init(&key);
  19682. if (ret != 0) {
  19683. wc_FreeRng(&rng);
  19684. return ret;
  19685. }
  19686. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19687. printf(testingFmt, "wc_ed448_import_private_key()");
  19688. if (ret == 0) {
  19689. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey, pubKeySz,
  19690. &key);
  19691. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  19692. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19693. ret = WOLFSSL_FATAL_ERROR;
  19694. }
  19695. }
  19696. #ifdef HAVE_ED448_KEY_EXPORT
  19697. if (ret == 0)
  19698. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  19699. if (ret == 0) {
  19700. ret = wc_ed448_import_private_key(bothKeys, bothKeysSz, NULL, 0, &key);
  19701. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  19702. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19703. ret = WOLFSSL_FATAL_ERROR;
  19704. }
  19705. }
  19706. #endif
  19707. /* Test bad args. */
  19708. if (ret == 0) {
  19709. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19710. &key);
  19711. if (ret == BAD_FUNC_ARG) {
  19712. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  19713. pubKeySz, &key);
  19714. }
  19715. if (ret == BAD_FUNC_ARG) {
  19716. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  19717. pubKeySz, NULL);
  19718. }
  19719. if (ret == BAD_FUNC_ARG) {
  19720. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  19721. pubKeySz, &key);
  19722. }
  19723. if (ret == BAD_FUNC_ARG) {
  19724. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  19725. pubKeySz - 1, &key);
  19726. }
  19727. if (ret == BAD_FUNC_ARG) {
  19728. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  19729. 0, &key);
  19730. }
  19731. if (ret == BAD_FUNC_ARG) {
  19732. ret = 0;
  19733. } else if (ret == 0) {
  19734. ret = WOLFSSL_FATAL_ERROR;
  19735. }
  19736. }
  19737. printf(resultFmt, ret == 0 ? passed : failed);
  19738. if (wc_FreeRng(&rng) && ret == 0) {
  19739. ret = WOLFSSL_FATAL_ERROR;
  19740. }
  19741. wc_ed448_free(&key);
  19742. #endif
  19743. return ret;
  19744. } /* END test_wc_ed448_import_private_key */
  19745. /*
  19746. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  19747. */
  19748. static int test_wc_ed448_export (void)
  19749. {
  19750. int ret = 0;
  19751. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  19752. WC_RNG rng;
  19753. ed448_key key;
  19754. byte priv[ED448_PRV_KEY_SIZE];
  19755. byte pub[ED448_PUB_KEY_SIZE];
  19756. word32 privSz = sizeof(priv);
  19757. word32 pubSz = sizeof(pub);
  19758. ret = wc_InitRng(&rng);
  19759. if (ret != 0) {
  19760. return ret;
  19761. }
  19762. ret = wc_ed448_init(&key);
  19763. if (ret != 0) {
  19764. wc_FreeRng(&rng);
  19765. return ret;
  19766. }
  19767. if (ret == 0) {
  19768. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19769. }
  19770. printf(testingFmt, "wc_ed448_export_public()");
  19771. if (ret == 0) {
  19772. ret = wc_ed448_export_public(&key, pub, &pubSz);
  19773. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  19774. XMEMCMP(key.p, pub, pubSz) != 0)) {
  19775. ret = WOLFSSL_FATAL_ERROR;
  19776. }
  19777. if (ret == 0) {
  19778. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  19779. if (ret == BAD_FUNC_ARG) {
  19780. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  19781. }
  19782. if (ret == BAD_FUNC_ARG) {
  19783. ret = wc_ed448_export_public(&key, pub, NULL);
  19784. }
  19785. if (ret == BAD_FUNC_ARG) {
  19786. ret = 0;
  19787. } else if (ret == 0) {
  19788. ret = WOLFSSL_FATAL_ERROR;
  19789. }
  19790. }
  19791. }
  19792. printf(resultFmt, ret == 0 ? passed : failed);
  19793. printf(testingFmt, "wc_ed448_export_private_only()");
  19794. if (ret == 0) {
  19795. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  19796. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  19797. XMEMCMP(key.k, priv, privSz) != 0)) {
  19798. ret = WOLFSSL_FATAL_ERROR;
  19799. }
  19800. if (ret == 0) {
  19801. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  19802. if (ret == BAD_FUNC_ARG) {
  19803. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  19804. }
  19805. if (ret == BAD_FUNC_ARG) {
  19806. ret = wc_ed448_export_private_only(&key, priv, NULL);
  19807. }
  19808. if (ret == BAD_FUNC_ARG) {
  19809. ret = 0;
  19810. } else if (ret == 0) {
  19811. ret = WOLFSSL_FATAL_ERROR;
  19812. }
  19813. }
  19814. }
  19815. printf(resultFmt, ret == 0 ? passed : failed);
  19816. if (wc_FreeRng(&rng) && ret == 0) {
  19817. ret = WOLFSSL_FATAL_ERROR;
  19818. }
  19819. wc_ed448_free(&key);
  19820. #endif
  19821. return ret;
  19822. } /* END test_wc_ed448_export */
  19823. /*
  19824. * Testing wc_ed448_size()
  19825. */
  19826. static int test_wc_ed448_size (void)
  19827. {
  19828. int ret = 0;
  19829. #if defined(HAVE_ED448)
  19830. WC_RNG rng;
  19831. ed448_key key;
  19832. ret = wc_InitRng(&rng);
  19833. if (ret != 0) {
  19834. return ret;
  19835. }
  19836. ret = wc_ed448_init(&key);
  19837. if (ret != 0) {
  19838. wc_FreeRng(&rng);
  19839. return ret;
  19840. }
  19841. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19842. if (ret != 0) {
  19843. wc_FreeRng(&rng);
  19844. wc_ed448_free(&key);
  19845. return ret;
  19846. }
  19847. printf(testingFmt, "wc_ed448_size()");
  19848. ret = wc_ed448_size(&key);
  19849. /* Test bad args. */
  19850. if (ret == ED448_KEY_SIZE) {
  19851. ret = wc_ed448_size(NULL);
  19852. if (ret == BAD_FUNC_ARG) {
  19853. ret = 0;
  19854. }
  19855. }
  19856. printf(resultFmt, ret == 0 ? passed : failed);
  19857. if (ret == 0) {
  19858. printf(testingFmt, "wc_ed448_sig_size()");
  19859. ret = wc_ed448_sig_size(&key);
  19860. if (ret == ED448_SIG_SIZE) {
  19861. ret = 0;
  19862. }
  19863. /* Test bad args. */
  19864. if (ret == 0) {
  19865. ret = wc_ed448_sig_size(NULL);
  19866. if (ret == BAD_FUNC_ARG) {
  19867. ret = 0;
  19868. }
  19869. }
  19870. printf(resultFmt, ret == 0 ? passed : failed);
  19871. } /* END wc_ed448_sig_size() */
  19872. if (ret == 0) {
  19873. printf(testingFmt, "wc_ed448_pub_size");
  19874. ret = wc_ed448_pub_size(&key);
  19875. if (ret == ED448_PUB_KEY_SIZE) {
  19876. ret = 0;
  19877. }
  19878. if (ret == 0) {
  19879. ret = wc_ed448_pub_size(NULL);
  19880. if (ret == BAD_FUNC_ARG) {
  19881. ret = 0;
  19882. }
  19883. }
  19884. printf(resultFmt, ret == 0 ? passed : failed);
  19885. } /* END wc_ed448_pub_size */
  19886. if (ret == 0) {
  19887. printf(testingFmt, "wc_ed448_priv_size");
  19888. ret = wc_ed448_priv_size(&key);
  19889. if (ret == ED448_PRV_KEY_SIZE) {
  19890. ret = 0;
  19891. }
  19892. if (ret == 0) {
  19893. ret = wc_ed448_priv_size(NULL);
  19894. if (ret == BAD_FUNC_ARG) {
  19895. ret = 0;
  19896. }
  19897. }
  19898. printf(resultFmt, ret == 0 ? passed : failed);
  19899. } /* END wc_ed448_pub_size */
  19900. if (wc_FreeRng(&rng) && ret == 0) {
  19901. ret = WOLFSSL_FATAL_ERROR;
  19902. }
  19903. wc_ed448_free(&key);
  19904. #endif
  19905. return ret;
  19906. } /* END test_wc_ed448_size */
  19907. /*
  19908. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  19909. */
  19910. static int test_wc_ed448_exportKey (void)
  19911. {
  19912. int ret = 0;
  19913. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  19914. WC_RNG rng;
  19915. ed448_key key;
  19916. byte priv[ED448_PRV_KEY_SIZE];
  19917. byte pub[ED448_PUB_KEY_SIZE];
  19918. byte privOnly[ED448_PRV_KEY_SIZE];
  19919. word32 privSz = sizeof(priv);
  19920. word32 pubSz = sizeof(pub);
  19921. word32 privOnlySz = sizeof(privOnly);
  19922. ret = wc_InitRng(&rng);
  19923. if (ret != 0) {
  19924. return ret;
  19925. }
  19926. ret = wc_ed448_init(&key);
  19927. if (ret != 0) {
  19928. wc_FreeRng(&rng);
  19929. return ret;
  19930. }
  19931. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19932. if (ret != 0) {
  19933. wc_FreeRng(&rng);
  19934. wc_ed448_free(&key);
  19935. return ret;
  19936. }
  19937. printf(testingFmt, "wc_ed448_export_private()");
  19938. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  19939. if (ret == 0) {
  19940. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  19941. if (ret == BAD_FUNC_ARG) {
  19942. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  19943. }
  19944. if (ret == BAD_FUNC_ARG) {
  19945. ret = wc_ed448_export_private(&key, privOnly, NULL);
  19946. }
  19947. if (ret == BAD_FUNC_ARG) {
  19948. ret = 0;
  19949. } else if (ret == 0) {
  19950. ret = WOLFSSL_FATAL_ERROR;
  19951. }
  19952. }
  19953. printf(resultFmt, ret == 0 ? passed : failed);
  19954. if (ret == 0) {
  19955. printf(testingFmt, "wc_ed448_export_key()");
  19956. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  19957. if (ret == 0) {
  19958. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  19959. if (ret == BAD_FUNC_ARG) {
  19960. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  19961. }
  19962. if (ret == BAD_FUNC_ARG) {
  19963. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  19964. }
  19965. if (ret == BAD_FUNC_ARG) {
  19966. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  19967. }
  19968. if (ret == BAD_FUNC_ARG) {
  19969. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  19970. }
  19971. if (ret == BAD_FUNC_ARG) {
  19972. ret = 0;
  19973. } else if (ret == 0) {
  19974. ret = WOLFSSL_FATAL_ERROR;
  19975. }
  19976. }
  19977. printf(resultFmt, ret == 0 ? passed : failed);
  19978. } /* END wc_ed448_export_key() */
  19979. /* Cross check output. */
  19980. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  19981. ret = WOLFSSL_FATAL_ERROR;
  19982. }
  19983. if (wc_FreeRng(&rng) && ret == 0) {
  19984. ret = WOLFSSL_FATAL_ERROR;
  19985. }
  19986. wc_ed448_free(&key);
  19987. #endif
  19988. return ret;
  19989. } /* END test_wc_ed448_exportKey */
  19990. /*
  19991. * Testing wc_Ed448PublicKeyToDer
  19992. */
  19993. static int test_wc_Ed448PublicKeyToDer (void)
  19994. {
  19995. int ret = 0;
  19996. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  19997. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19998. int tmp;
  19999. ed448_key key;
  20000. byte derBuf[1024];
  20001. printf(testingFmt, "wc_Ed448PublicKeyToDer()");
  20002. /* Test bad args */
  20003. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  20004. if (tmp != BAD_FUNC_ARG) {
  20005. ret = WOLFSSL_FATAL_ERROR;
  20006. }
  20007. if (ret == 0) {
  20008. wc_ed448_init(&key);
  20009. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  20010. if (tmp != BUFFER_E) {
  20011. ret = WOLFSSL_FATAL_ERROR;
  20012. }
  20013. wc_ed448_free(&key);
  20014. }
  20015. /* Test good args */
  20016. if (ret == 0) {
  20017. WC_RNG rng;
  20018. ret = wc_InitRng(&rng);
  20019. if (ret != 0) {
  20020. return ret;
  20021. }
  20022. ret = wc_ed448_init(&key);
  20023. if (ret != 0) {
  20024. wc_FreeRng(&rng);
  20025. return ret;
  20026. }
  20027. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20028. if (ret != 0) {
  20029. wc_FreeRng(&rng);
  20030. wc_ed448_free(&key);
  20031. return ret;
  20032. }
  20033. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  20034. if (tmp <= 0) {
  20035. ret = WOLFSSL_FATAL_ERROR;
  20036. }
  20037. wc_FreeRng(&rng);
  20038. wc_ed448_free(&key);
  20039. }
  20040. printf(resultFmt, ret == 0 ? passed : failed);
  20041. #endif
  20042. return ret;
  20043. } /* END testing wc_Ed448PublicKeyToDer */
  20044. /*
  20045. * Testing wc_curve448_init and wc_curve448_free.
  20046. */
  20047. static int test_wc_curve448_init (void)
  20048. {
  20049. int ret = 0;
  20050. #if defined(HAVE_CURVE448)
  20051. curve448_key key;
  20052. printf(testingFmt, "wc_curve448_init()");
  20053. ret = wc_curve448_init(&key);
  20054. /* Test bad args for wc_curve448_init */
  20055. if (ret == 0) {
  20056. ret = wc_curve448_init(NULL);
  20057. if (ret == BAD_FUNC_ARG) {
  20058. ret = 0;
  20059. } else if (ret == 0) {
  20060. ret = WOLFSSL_FATAL_ERROR;
  20061. }
  20062. }
  20063. printf(resultFmt, ret == 0 ? passed : failed);
  20064. /* Test good args for wc_curve_448_free */
  20065. wc_curve448_free(&key);
  20066. wc_curve448_free(NULL);
  20067. #endif
  20068. return ret;
  20069. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20070. /*
  20071. * Testing wc_curve448_make_key
  20072. */
  20073. static int test_wc_curve448_make_key (void)
  20074. {
  20075. int ret = 0;
  20076. #if defined(HAVE_CURVE448)
  20077. WC_RNG rng;
  20078. curve448_key key;
  20079. int keysize;
  20080. printf(testingFmt, "wc_curve448_make_key()");
  20081. ret = wc_curve448_init(&key);
  20082. if (ret == 0) {
  20083. ret = wc_InitRng(&rng);
  20084. }
  20085. if (ret == 0) {
  20086. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20087. if (ret == 0) {
  20088. keysize = wc_curve448_size(&key);
  20089. if (keysize != CURVE448_KEY_SIZE) {
  20090. ret = WOLFSSL_FATAL_ERROR;
  20091. }
  20092. }
  20093. if (ret == 0) {
  20094. ret = wc_curve448_make_key(&rng, keysize, &key);
  20095. }
  20096. }
  20097. /*test bad cases*/
  20098. if (ret == 0) {
  20099. ret = wc_curve448_make_key(NULL, 0, NULL);
  20100. if (ret == BAD_FUNC_ARG) {
  20101. ret = 0;
  20102. }
  20103. }
  20104. if (ret == 0) {
  20105. ret = wc_curve448_make_key(&rng, keysize, NULL);
  20106. if (ret == BAD_FUNC_ARG) {
  20107. ret = 0;
  20108. }
  20109. }
  20110. if (ret == 0) {
  20111. ret = wc_curve448_make_key(NULL, keysize, &key);
  20112. if (ret == BAD_FUNC_ARG) {
  20113. ret = 0;
  20114. }
  20115. }
  20116. if (ret == 0) {
  20117. ret = wc_curve448_make_key(&rng, 0, &key);
  20118. if (ret == ECC_BAD_ARG_E) {
  20119. ret = 0;
  20120. }
  20121. }
  20122. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20123. ret = WOLFSSL_FATAL_ERROR;
  20124. }
  20125. printf(resultFmt, ret == 0 ? passed : failed);
  20126. wc_curve448_free(&key);
  20127. #endif
  20128. return ret;
  20129. } /*END test_wc_curve448_make_key*/
  20130. /*
  20131. * Testing test_wc_curve448_shared_secret_ex
  20132. */
  20133. static int test_wc_curve448_shared_secret_ex (void)
  20134. {
  20135. int ret = 0;
  20136. #if defined(HAVE_CURVE448)
  20137. WC_RNG rng;
  20138. curve448_key private_key, public_key;
  20139. byte out[CURVE448_KEY_SIZE];
  20140. word32 outLen = sizeof(out);
  20141. int endian = EC448_BIG_ENDIAN;
  20142. printf(testingFmt, "wc_curve448_shared_secret_ex()");
  20143. ret = wc_curve448_init(&private_key);
  20144. if (ret == 0) {
  20145. ret = wc_InitRng(&rng);
  20146. if (ret == 0) {
  20147. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  20148. }
  20149. }
  20150. if (ret == 0) {
  20151. ret = wc_curve448_init(&public_key);
  20152. }
  20153. if (ret == 0) {
  20154. if (ret == 0) {
  20155. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  20156. }
  20157. }
  20158. if (ret == 0) {
  20159. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20160. &outLen, endian);
  20161. }
  20162. /*test bad cases*/
  20163. if (ret == 0) {
  20164. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL,
  20165. 0, endian);
  20166. if (ret == BAD_FUNC_ARG) {
  20167. ret = 0;
  20168. }
  20169. }
  20170. if (ret == 0) {
  20171. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  20172. &outLen, endian);
  20173. if (ret == BAD_FUNC_ARG) {
  20174. ret = 0;
  20175. }
  20176. }
  20177. if (ret == 0) {
  20178. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  20179. &outLen, endian);
  20180. if (ret == BAD_FUNC_ARG) {
  20181. ret = 0;
  20182. }
  20183. }
  20184. if (ret == 0) {
  20185. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20186. &outLen, endian);
  20187. if (ret == BAD_FUNC_ARG) {
  20188. ret = 0;
  20189. }
  20190. }
  20191. if (ret == 0) {
  20192. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20193. NULL, endian);
  20194. if (ret == BAD_FUNC_ARG) {
  20195. ret = 0;
  20196. }
  20197. }
  20198. outLen = outLen - 2;
  20199. if (ret == 0) {
  20200. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20201. &outLen, endian);
  20202. if (ret == BAD_FUNC_ARG) {
  20203. ret = 0;
  20204. }
  20205. }
  20206. printf(resultFmt, ret == 0 ? passed : failed);
  20207. wc_curve448_free(&private_key);
  20208. wc_curve448_free(&public_key);
  20209. wc_FreeRng(&rng);
  20210. #endif
  20211. return ret;
  20212. } /*END test_wc_curve448_shared_secret_ex*/
  20213. /*
  20214. * Testing test_wc_curve448_export_public_ex
  20215. */
  20216. static int test_wc_curve448_export_public_ex (void)
  20217. {
  20218. int ret = 0;
  20219. #if defined(HAVE_CURVE448)
  20220. WC_RNG rng;
  20221. curve448_key key;
  20222. byte out[CURVE448_KEY_SIZE];
  20223. word32 outLen = sizeof(out);
  20224. int endian = EC448_BIG_ENDIAN;
  20225. printf(testingFmt, "wc_curve448_export_public_ex()");
  20226. ret = wc_curve448_init(&key);
  20227. if (ret == 0) {
  20228. ret = wc_InitRng(&rng);
  20229. }
  20230. if (ret == 0) {
  20231. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20232. if (ret == 0){
  20233. ret = wc_curve448_export_public(&key, out, &outLen);
  20234. }
  20235. if (ret == 0) {
  20236. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  20237. }
  20238. }
  20239. /*test bad cases*/
  20240. if (ret == 0) {
  20241. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  20242. if (ret == BAD_FUNC_ARG) {
  20243. ret = 0;
  20244. }
  20245. }
  20246. if (ret == 0) {
  20247. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  20248. if (ret == BAD_FUNC_ARG) {
  20249. ret = 0;
  20250. }
  20251. }
  20252. if (ret == 0) {
  20253. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  20254. if (ret == BAD_FUNC_ARG) {
  20255. ret = 0;
  20256. }
  20257. }
  20258. if (ret == 0) {
  20259. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  20260. if (ret == BAD_FUNC_ARG) {
  20261. ret = 0;
  20262. }
  20263. }
  20264. outLen = outLen - 2;
  20265. if (ret == 0) {
  20266. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  20267. if (ret == ECC_BAD_ARG_E) {
  20268. ret = 0;
  20269. }
  20270. }
  20271. printf(resultFmt, ret == 0 ? passed : failed);
  20272. wc_curve448_free(&key);
  20273. wc_FreeRng(&rng);
  20274. #endif
  20275. return ret;
  20276. } /*END test_wc_curve448_export_public_ex*/
  20277. /*
  20278. * Testing test_wc_curve448_export_private_raw_ex
  20279. */
  20280. static int test_wc_curve448_export_private_raw_ex (void)
  20281. {
  20282. int ret = 0;
  20283. #if defined(HAVE_CURVE448)
  20284. curve448_key key;
  20285. byte out[CURVE448_KEY_SIZE];
  20286. word32 outLen = sizeof(out);
  20287. int endian = EC448_BIG_ENDIAN;
  20288. printf(testingFmt, "wc_curve448_export_private_raw_ex()");
  20289. ret = wc_curve448_init(&key);
  20290. if (ret == 0) {
  20291. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  20292. }
  20293. /*test bad cases*/
  20294. if (ret == 0) {
  20295. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  20296. if (ret == BAD_FUNC_ARG) {
  20297. ret = 0;
  20298. }
  20299. }
  20300. if (ret == 0) {
  20301. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  20302. if (ret == BAD_FUNC_ARG) {
  20303. ret = 0;
  20304. }
  20305. }
  20306. if (ret == 0) {
  20307. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  20308. if (ret == BAD_FUNC_ARG) {
  20309. ret = 0;
  20310. }
  20311. }
  20312. if (ret == 0) {
  20313. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  20314. if (ret == BAD_FUNC_ARG) {
  20315. ret = 0;
  20316. }
  20317. }
  20318. if (ret == 0) {
  20319. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20320. EC448_LITTLE_ENDIAN);
  20321. }
  20322. outLen = outLen - 2;
  20323. if (ret == 0) {
  20324. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  20325. if (ret == ECC_BAD_ARG_E) {
  20326. ret = 0;
  20327. }
  20328. }
  20329. printf(resultFmt, ret == 0 ? passed : failed);
  20330. wc_curve448_free(&key);
  20331. #endif
  20332. return ret;
  20333. }/*END test_wc_curve448_export_private_raw_ex*/
  20334. /*
  20335. * Testing test_wc_curve448_import_private_raw_ex
  20336. */
  20337. static int test_wc_curve448_import_private_raw_ex (void)
  20338. {
  20339. int ret = 0;
  20340. #if defined(HAVE_CURVE448)
  20341. WC_RNG rng;
  20342. curve448_key key;
  20343. byte priv[CURVE448_KEY_SIZE];
  20344. byte pub[CURVE448_KEY_SIZE];
  20345. word32 privSz = sizeof(priv);
  20346. word32 pubSz = sizeof(pub);
  20347. int endian = EC448_BIG_ENDIAN;
  20348. printf(testingFmt, "wc_curve448_import_private_raw_ex()");
  20349. ret = wc_curve448_init(&key);
  20350. if (ret == 0) {
  20351. ret = wc_InitRng(&rng);
  20352. }
  20353. if (ret == 0) {
  20354. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20355. if (ret == 0){
  20356. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20357. }
  20358. if (ret == 0){
  20359. ret = wc_curve448_export_public(&key, pub, &pubSz);
  20360. }
  20361. if (ret == 0) {
  20362. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20363. &key, endian);
  20364. }
  20365. }
  20366. /*test bad cases*/
  20367. if (ret == 0) {
  20368. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  20369. if (ret == BAD_FUNC_ARG) {
  20370. ret = 0;
  20371. }
  20372. }
  20373. if (ret == 0) {
  20374. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20375. &key, endian);
  20376. if (ret == BAD_FUNC_ARG) {
  20377. ret = 0;
  20378. }
  20379. }
  20380. if (ret == 0) {
  20381. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20382. &key, endian);
  20383. if (ret == BAD_FUNC_ARG) {
  20384. ret = 0;
  20385. }
  20386. }
  20387. if (ret == 0) {
  20388. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20389. NULL, endian);
  20390. if (ret == BAD_FUNC_ARG) {
  20391. ret = 0;
  20392. }
  20393. }
  20394. if (ret == 0) {
  20395. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20396. &key, endian);
  20397. if (ret == ECC_BAD_ARG_E) {
  20398. ret = 0;
  20399. }
  20400. }
  20401. if (ret == 0) {
  20402. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20403. &key, endian);
  20404. if (ret == ECC_BAD_ARG_E) {
  20405. ret = 0;
  20406. }
  20407. }
  20408. if (ret == 0) {
  20409. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20410. &key, EC448_LITTLE_ENDIAN);
  20411. }
  20412. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20413. ret = WOLFSSL_FATAL_ERROR;
  20414. }
  20415. printf(resultFmt, ret == 0 ? passed : failed);
  20416. wc_curve448_free(&key);
  20417. #endif
  20418. return ret;
  20419. } /*END test_wc_curve448_import_private_raw_ex*/
  20420. /*
  20421. * Testing test_curve448_export_key_raw
  20422. */
  20423. static int test_wc_curve448_export_key_raw (void)
  20424. {
  20425. int ret = 0;
  20426. #if defined(HAVE_CURVE448)
  20427. WC_RNG rng;
  20428. curve448_key key;
  20429. byte priv[CURVE448_KEY_SIZE];
  20430. byte pub[CURVE448_KEY_SIZE];
  20431. word32 privSz = sizeof(priv);
  20432. word32 pubSz = sizeof(pub);
  20433. printf(testingFmt, "wc_curve448_export_key_raw()");
  20434. ret = wc_curve448_init(&key);
  20435. if (ret == 0) {
  20436. ret = wc_InitRng(&rng);
  20437. }
  20438. if (ret == 0) {
  20439. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20440. if (ret == 0) {
  20441. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20442. }
  20443. if (ret == 0) {
  20444. ret = wc_curve448_export_public(&key, pub, &pubSz);
  20445. }
  20446. if (ret == 0) {
  20447. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  20448. }
  20449. }
  20450. printf(resultFmt, ret == 0 ? passed : failed);
  20451. wc_curve448_free(&key);
  20452. wc_FreeRng(&rng);
  20453. #endif
  20454. return ret;
  20455. }/*END test_wc_curve448_import_private_raw_ex*/
  20456. /*
  20457. * Testing test_wc_curve448_import_private
  20458. */
  20459. static int test_wc_curve448_import_private (void)
  20460. {
  20461. int ret = 0;
  20462. #if defined(HAVE_CURVE448)
  20463. curve448_key key;
  20464. WC_RNG rng;
  20465. byte priv[CURVE448_KEY_SIZE];
  20466. word32 privSz = sizeof(priv);
  20467. printf(testingFmt, "wc_curve448_import_private()");
  20468. ret = wc_curve448_init(&key);
  20469. if (ret == 0) {
  20470. ret = wc_InitRng(&rng);
  20471. }
  20472. if (ret == 0) {
  20473. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20474. if (ret == 0) {
  20475. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20476. }
  20477. }
  20478. if (ret == 0) {
  20479. ret = wc_curve448_import_private(priv, privSz, &key);
  20480. }
  20481. printf(resultFmt, ret == 0 ? passed : failed);
  20482. wc_curve448_free(&key);
  20483. wc_FreeRng(&rng);
  20484. #endif
  20485. return ret;
  20486. } /*END test_wc_curve448_import*/
  20487. /*
  20488. * Testing test_wc_curve448_size.
  20489. */
  20490. static int test_wc_curve448_size (void)
  20491. {
  20492. int ret = 0;
  20493. #if defined(HAVE_CURVE448)
  20494. curve448_key key;
  20495. printf(testingFmt, "wc_curve448_size()");
  20496. ret = wc_curve448_init(&key);
  20497. /* Test good args for wc_curve448_size */
  20498. if (ret == 0) {
  20499. ret = wc_curve448_size(&key);
  20500. }
  20501. /* Test bad args for wc_curve448_size */
  20502. if (ret != 0) {
  20503. ret = wc_curve448_size(NULL);
  20504. }
  20505. printf(resultFmt, ret == 0 ? passed : failed);
  20506. wc_curve448_free(&key);
  20507. #endif
  20508. return ret;
  20509. } /* END test_wc_curve448_size*/
  20510. /*
  20511. * Testing wc_ecc_make_key.
  20512. */
  20513. static int test_wc_ecc_make_key (void)
  20514. {
  20515. int ret = 0;
  20516. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20517. WC_RNG rng;
  20518. ecc_key key;
  20519. printf(testingFmt, "wc_ecc_make_key()");
  20520. ret = wc_InitRng(&rng);
  20521. if (ret != 0)
  20522. return ret;
  20523. ret = wc_ecc_init(&key);
  20524. if (ret == 0) {
  20525. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20526. /* Pass in bad args. */
  20527. if (ret == 0) {
  20528. ret = wc_ecc_make_key(NULL, KEY14, &key);
  20529. if (ret == BAD_FUNC_ARG) {
  20530. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  20531. }
  20532. if (ret == BAD_FUNC_ARG) {
  20533. ret = 0;
  20534. } else if (ret == 0) {
  20535. ret = WOLFSSL_FATAL_ERROR;
  20536. }
  20537. }
  20538. wc_ecc_free(&key);
  20539. }
  20540. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20541. ret = WOLFSSL_FATAL_ERROR;
  20542. }
  20543. #ifdef FP_ECC
  20544. wc_ecc_fp_free();
  20545. #endif
  20546. printf(resultFmt, ret == 0 ? passed : failed);
  20547. #endif
  20548. return ret;
  20549. } /* END test_wc_ecc_make_key */
  20550. /*
  20551. * Testing wc_ecc_init()
  20552. */
  20553. static int test_wc_ecc_init (void)
  20554. {
  20555. int ret = 0;
  20556. #ifdef HAVE_ECC
  20557. ecc_key key;
  20558. printf(testingFmt, "wc_ecc_init()");
  20559. ret = wc_ecc_init(&key);
  20560. /* Pass in bad args. */
  20561. if (ret == 0) {
  20562. ret = wc_ecc_init(NULL);
  20563. if (ret == BAD_FUNC_ARG) {
  20564. ret = 0;
  20565. } else if (ret == 0) {
  20566. ret = WOLFSSL_FATAL_ERROR;
  20567. }
  20568. }
  20569. printf(resultFmt, ret == 0 ? passed : failed);
  20570. wc_ecc_free(&key);
  20571. #endif
  20572. return ret;
  20573. } /* END test_wc_ecc_init */
  20574. /*
  20575. * Testing wc_ecc_check_key()
  20576. */
  20577. static int test_wc_ecc_check_key (void)
  20578. {
  20579. int ret = 0;
  20580. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20581. WC_RNG rng;
  20582. ecc_key key;
  20583. XMEMSET(&rng, 0, sizeof(rng));
  20584. XMEMSET(&key, 0, sizeof(key));
  20585. ret = wc_InitRng(&rng);
  20586. if (ret == 0) {
  20587. ret = wc_ecc_init(&key);
  20588. if (ret == 0) {
  20589. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20590. }
  20591. }
  20592. printf(testingFmt, "wc_ecc_check_key()");
  20593. if (ret == 0) {
  20594. ret = wc_ecc_check_key(&key);
  20595. }
  20596. /* Pass in bad args. */
  20597. if (ret == 0) {
  20598. ret = wc_ecc_check_key(NULL);
  20599. if (ret == BAD_FUNC_ARG) {
  20600. ret = 0;
  20601. } else if (ret == 0) {
  20602. ret = WOLFSSL_FATAL_ERROR;
  20603. }
  20604. }
  20605. printf(resultFmt, ret == 0 ? passed : failed);
  20606. if (wc_FreeRng(&rng) && ret == 0) {
  20607. ret = WOLFSSL_FATAL_ERROR;
  20608. }
  20609. wc_ecc_free(&key);
  20610. #ifdef FP_ECC
  20611. wc_ecc_fp_free();
  20612. #endif
  20613. #endif
  20614. return ret;
  20615. } /* END test_wc_ecc_check_key */
  20616. /*
  20617. * Testing wc_ecc_get_generator()
  20618. */
  20619. static int test_wc_ecc_get_generator(void)
  20620. {
  20621. int ret = 0;
  20622. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  20623. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  20624. ecc_point* pt;
  20625. printf(testingFmt, "wc_ecc_new_point()");
  20626. pt = wc_ecc_new_point();
  20627. if (!pt) {
  20628. ret = WOLFSSL_FATAL_ERROR;
  20629. }
  20630. printf(testingFmt, "wc_ecc_get_generator()");
  20631. if (ret == 0) {
  20632. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  20633. }
  20634. /* Test bad args. */
  20635. if (ret == MP_OKAY) {
  20636. /* Returns Zero for bad arg. */
  20637. ret = wc_ecc_get_generator(pt, -1);
  20638. if (ret != MP_OKAY)
  20639. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  20640. if (ret != MP_OKAY)
  20641. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  20642. * increase this number */
  20643. if (ret != MP_OKAY)
  20644. wc_ecc_get_generator(NULL, -1);
  20645. ret = ret == MP_OKAY ? WOLFSSL_FATAL_ERROR : 0;
  20646. }
  20647. printf(resultFmt, ret == 0 ? passed : failed);
  20648. wc_ecc_del_point(pt);
  20649. #endif
  20650. return ret;
  20651. } /* END test_wc_ecc_get_generator */
  20652. /*
  20653. * Testing wc_ecc_size()
  20654. */
  20655. static int test_wc_ecc_size (void)
  20656. {
  20657. int ret = 0;
  20658. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20659. WC_RNG rng;
  20660. ecc_key key;
  20661. XMEMSET(&rng, 0, sizeof(rng));
  20662. XMEMSET(&key, 0, sizeof(key));
  20663. ret = wc_InitRng(&rng);
  20664. if (ret == 0) {
  20665. ret = wc_ecc_init(&key);
  20666. if (ret == 0) {
  20667. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20668. }
  20669. }
  20670. printf(testingFmt, "wc_ecc_size()");
  20671. if (ret == 0) {
  20672. ret = wc_ecc_size(&key);
  20673. if (ret == KEY14) {
  20674. ret = 0;
  20675. } else if (ret == 0){
  20676. ret = WOLFSSL_FATAL_ERROR;
  20677. }
  20678. }
  20679. /* Test bad args. */
  20680. if (ret == 0) {
  20681. /* Returns Zero for bad arg. */
  20682. ret = wc_ecc_size(NULL);
  20683. }
  20684. printf(resultFmt, ret == 0 ? passed : failed);
  20685. if (wc_FreeRng(&rng) && ret == 0) {
  20686. ret = WOLFSSL_FATAL_ERROR;
  20687. }
  20688. wc_ecc_free(&key);
  20689. #endif
  20690. return ret;
  20691. } /* END test_wc_ecc_size */
  20692. static void test_wc_ecc_params(void)
  20693. {
  20694. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  20695. It was added after certifications */
  20696. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20697. const ecc_set_type* ecc_set;
  20698. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  20699. /* Test for SECP256R1 curve */
  20700. int curve_id = ECC_SECP256R1;
  20701. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  20702. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  20703. ecc_set = wc_ecc_get_curve_params(curve_idx);
  20704. AssertNotNull(ecc_set);
  20705. AssertIntEQ(ecc_set->id, curve_id);
  20706. #endif
  20707. /* Test case when SECP256R1 is not enabled */
  20708. /* Test that we get curve params for index 0 */
  20709. ecc_set = wc_ecc_get_curve_params(0);
  20710. AssertNotNull(ecc_set);
  20711. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  20712. }
  20713. /*
  20714. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  20715. */
  20716. static int test_wc_ecc_signVerify_hash (void)
  20717. {
  20718. int ret = 0;
  20719. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  20720. WC_RNG rng;
  20721. ecc_key key;
  20722. int signH = WOLFSSL_FATAL_ERROR;
  20723. #ifdef HAVE_ECC_VERIFY
  20724. int verifyH = WOLFSSL_FATAL_ERROR;
  20725. int verify = 0;
  20726. #endif
  20727. word32 siglen = ECC_BUFSIZE;
  20728. byte sig[ECC_BUFSIZE];
  20729. byte adjustedSig[ECC_BUFSIZE+1];
  20730. byte digest[] = TEST_STRING;
  20731. word32 digestlen = (word32)TEST_STRING_SZ;
  20732. /* Init stack var */
  20733. XMEMSET(sig, 0, siglen);
  20734. XMEMSET(&key, 0, sizeof(key));
  20735. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  20736. /* Init structs. */
  20737. ret = wc_InitRng(&rng);
  20738. if (ret == 0) {
  20739. ret = wc_ecc_init(&key);
  20740. if (ret == 0) {
  20741. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20742. }
  20743. }
  20744. printf(testingFmt, "wc_ecc_sign_hash()");
  20745. if (ret == 0) {
  20746. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  20747. }
  20748. /* Check bad args. */
  20749. if (ret == 0) {
  20750. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  20751. if (signH == ECC_BAD_ARG_E) {
  20752. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  20753. &rng, &key);
  20754. }
  20755. if (signH == ECC_BAD_ARG_E) {
  20756. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  20757. &rng, &key);
  20758. }
  20759. if (signH == ECC_BAD_ARG_E) {
  20760. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  20761. NULL, &key);
  20762. }
  20763. if (signH == ECC_BAD_ARG_E) {
  20764. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  20765. &rng, NULL);
  20766. }
  20767. if (signH == ECC_BAD_ARG_E) {
  20768. signH = 0;
  20769. } else if (ret == 0) {
  20770. signH = WOLFSSL_FATAL_ERROR;
  20771. }
  20772. }
  20773. printf(resultFmt, signH == 0 ? passed : failed);
  20774. #ifdef HAVE_ECC_VERIFY
  20775. printf(testingFmt, "wc_ecc_verify_hash()");
  20776. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  20777. if (verify != 1 && ret == 0) {
  20778. ret = WOLFSSL_FATAL_ERROR;
  20779. }
  20780. /* test check on length of signature passed in */
  20781. XMEMCPY(adjustedSig, sig, siglen);
  20782. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  20783. #ifndef NO_STRICT_ECDSA_LEN
  20784. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20785. &verify, &key), 0);
  20786. #else
  20787. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  20788. * is allowed */
  20789. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20790. &verify, &key), 0);
  20791. #endif
  20792. /* Test bad args. */
  20793. if (ret == 0) {
  20794. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  20795. &verify, &key);
  20796. if (verifyH == ECC_BAD_ARG_E) {
  20797. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  20798. &verify, &key);
  20799. }
  20800. if (verifyH == ECC_BAD_ARG_E) {
  20801. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  20802. NULL, &key);
  20803. }
  20804. if (verifyH == ECC_BAD_ARG_E) {
  20805. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  20806. &verify, NULL);
  20807. }
  20808. if (verifyH == ECC_BAD_ARG_E) {
  20809. verifyH = 0;
  20810. } else if (ret == 0) {
  20811. verifyH = WOLFSSL_FATAL_ERROR;
  20812. }
  20813. }
  20814. printf(resultFmt, verifyH == 0 ? passed : failed);
  20815. #endif /* HAVE_ECC_VERIFY */
  20816. if (wc_FreeRng(&rng) && ret == 0) {
  20817. ret = WOLFSSL_FATAL_ERROR;
  20818. }
  20819. wc_ecc_free(&key);
  20820. #ifdef FP_ECC
  20821. wc_ecc_fp_free();
  20822. #endif
  20823. #endif
  20824. return ret;
  20825. } /* END test_wc_ecc_sign_hash */
  20826. /*
  20827. * Testing wc_ecc_shared_secret()
  20828. */
  20829. static int test_wc_ecc_shared_secret (void)
  20830. {
  20831. int ret = 0;
  20832. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  20833. ecc_key key, pubKey;
  20834. WC_RNG rng;
  20835. byte out[KEY32];
  20836. int keySz = sizeof(out);
  20837. word32 outlen = (word32)sizeof(out);
  20838. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20839. const char* qx =
  20840. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  20841. const char* qy =
  20842. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  20843. const char* d =
  20844. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  20845. const char* curveName = "SECP256R1";
  20846. const byte expected_shared_secret[] =
  20847. {
  20848. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  20849. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  20850. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  20851. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  20852. };
  20853. #endif
  20854. /* Initialize variables. */
  20855. XMEMSET(out, 0, keySz);
  20856. XMEMSET(&rng, 0, sizeof(rng));
  20857. XMEMSET(&key, 0, sizeof(key));
  20858. XMEMSET(&pubKey, 0, sizeof(pubKey));
  20859. ret = wc_InitRng(&rng);
  20860. if (ret == 0) {
  20861. ret = wc_ecc_init(&key);
  20862. if (ret == 0) {
  20863. ret = wc_ecc_init(&pubKey);
  20864. }
  20865. }
  20866. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20867. if (ret == 0) {
  20868. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  20869. }
  20870. if (ret == 0) {
  20871. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  20872. }
  20873. #else
  20874. if (ret == 0) {
  20875. ret = wc_ecc_make_key(&rng, keySz, &key);
  20876. }
  20877. if (ret == 0) {
  20878. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  20879. }
  20880. #endif
  20881. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20882. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20883. !defined(HAVE_SELFTEST)
  20884. if (ret == 0) {
  20885. ret = wc_ecc_set_rng(&key, &rng);
  20886. }
  20887. #endif
  20888. printf(testingFmt, "wc_ecc_shared_secret()");
  20889. if (ret == 0) {
  20890. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  20891. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20892. if (ret == 0) {
  20893. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  20894. ret = WOLFSSL_FATAL_ERROR;
  20895. }
  20896. }
  20897. #endif
  20898. /* Test bad args. */
  20899. if (ret == 0) {
  20900. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  20901. if (ret == BAD_FUNC_ARG) {
  20902. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  20903. }
  20904. if (ret == BAD_FUNC_ARG) {
  20905. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  20906. }
  20907. if (ret == BAD_FUNC_ARG) {
  20908. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  20909. }
  20910. if (ret == BAD_FUNC_ARG) {
  20911. /* Invalid length */
  20912. outlen = 1;
  20913. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  20914. }
  20915. if (ret == BUFFER_E) {
  20916. ret = 0;
  20917. } else if (ret == 0) {
  20918. ret = WOLFSSL_FATAL_ERROR;
  20919. }
  20920. }
  20921. }
  20922. printf(resultFmt, ret == 0 ? passed : failed);
  20923. if (wc_FreeRng(&rng) && ret == 0) {
  20924. ret = WOLFSSL_FATAL_ERROR;
  20925. }
  20926. wc_ecc_free(&key);
  20927. wc_ecc_free(&pubKey);
  20928. #ifdef FP_ECC
  20929. wc_ecc_fp_free();
  20930. #endif
  20931. #endif
  20932. return ret;
  20933. } /* END tests_wc_ecc_shared_secret */
  20934. /*
  20935. * testint wc_ecc_export_x963()
  20936. */
  20937. static int test_wc_ecc_export_x963 (void)
  20938. {
  20939. int ret = 0;
  20940. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20941. ecc_key key;
  20942. WC_RNG rng;
  20943. byte out[ECC_ASN963_MAX_BUF_SZ];
  20944. word32 outlen = sizeof(out);
  20945. /* Initialize variables. */
  20946. XMEMSET(out, 0, outlen);
  20947. XMEMSET(&rng, 0, sizeof(rng));
  20948. XMEMSET(&key, 0, sizeof(key));
  20949. ret = wc_InitRng(&rng);
  20950. if (ret == 0) {
  20951. ret = wc_ecc_init(&key);
  20952. if (ret == 0) {
  20953. ret = wc_ecc_make_key(&rng, KEY20, &key);
  20954. }
  20955. }
  20956. printf(testingFmt, "wc_ecc_export_x963()");
  20957. if (ret == 0) {
  20958. ret = wc_ecc_export_x963(&key, out, &outlen);
  20959. }
  20960. /* Test bad args. */
  20961. if (ret == 0) {
  20962. ret = wc_ecc_export_x963(NULL, out, &outlen);
  20963. if (ret == ECC_BAD_ARG_E) {
  20964. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  20965. }
  20966. if (ret == LENGTH_ONLY_E) {
  20967. ret = wc_ecc_export_x963(&key, out, NULL);
  20968. }
  20969. if (ret == ECC_BAD_ARG_E) {
  20970. key.idx = -4;
  20971. ret = wc_ecc_export_x963(&key, out, &outlen);
  20972. }
  20973. if (ret == ECC_BAD_ARG_E) {
  20974. ret = 0;
  20975. } else {
  20976. ret = WOLFSSL_FATAL_ERROR;
  20977. }
  20978. }
  20979. printf(resultFmt, ret == 0 ? passed : failed);
  20980. if (wc_FreeRng(&rng) && ret == 0) {
  20981. ret = WOLFSSL_FATAL_ERROR;
  20982. }
  20983. wc_ecc_free(&key);
  20984. #ifdef FP_ECC
  20985. wc_ecc_fp_free();
  20986. #endif
  20987. #endif
  20988. return ret;
  20989. } /* END test_wc_ecc_export_x963 */
  20990. /*
  20991. * Testing wc_ecc_export_x963_ex()
  20992. * compile with --enable-compkey will use compression.
  20993. */
  20994. static int test_wc_ecc_export_x963_ex (void)
  20995. {
  20996. int ret = 0;
  20997. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20998. ecc_key key;
  20999. WC_RNG rng;
  21000. byte out[ECC_ASN963_MAX_BUF_SZ];
  21001. word32 outlen = sizeof(out);
  21002. #ifdef HAVE_COMP_KEY
  21003. word32 badOutLen = 5;
  21004. #endif
  21005. /* Init stack variables. */
  21006. XMEMSET(out, 0, outlen);
  21007. XMEMSET(&rng, 0, sizeof(rng));
  21008. XMEMSET(&key, 0, sizeof(key));
  21009. ret = wc_InitRng(&rng);
  21010. if (ret == 0) {
  21011. ret = wc_ecc_init(&key);
  21012. if (ret == 0) {
  21013. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21014. }
  21015. }
  21016. printf(testingFmt, "wc_ecc_export_x963_ex()");
  21017. #ifdef HAVE_COMP_KEY
  21018. if (ret == 0) {
  21019. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21020. }
  21021. #else
  21022. if (ret == 0) {
  21023. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21024. }
  21025. #endif
  21026. /* Test bad args. */
  21027. #ifdef HAVE_COMP_KEY
  21028. if (ret == 0) {
  21029. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  21030. if (ret == BAD_FUNC_ARG) {
  21031. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  21032. }
  21033. if (ret == BAD_FUNC_ARG) {
  21034. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  21035. }
  21036. if (ret == BAD_FUNC_ARG) {
  21037. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  21038. }
  21039. if (ret == BUFFER_E) {
  21040. key.idx = -4;
  21041. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21042. }
  21043. if (ret == ECC_BAD_ARG_E) {
  21044. ret = 0;
  21045. } else {
  21046. ret = WOLFSSL_FATAL_ERROR;
  21047. }
  21048. }
  21049. #else
  21050. if (ret == 0) {
  21051. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  21052. if (ret == BAD_FUNC_ARG) {
  21053. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  21054. }
  21055. if (ret == BAD_FUNC_ARG) {
  21056. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  21057. }
  21058. if (ret == NOT_COMPILED_IN) {
  21059. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  21060. }
  21061. if (ret == BAD_FUNC_ARG) {
  21062. key.idx = -4;
  21063. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21064. }
  21065. if (ret == ECC_BAD_ARG_E) {
  21066. ret = 0;
  21067. } else if (ret == 0) {
  21068. ret = WOLFSSL_FATAL_ERROR;
  21069. }
  21070. }
  21071. #endif
  21072. printf(resultFmt, ret == 0 ? passed : failed);
  21073. if (wc_FreeRng(&rng) && ret == 0) {
  21074. ret = WOLFSSL_FATAL_ERROR;
  21075. }
  21076. wc_ecc_free(&key);
  21077. #ifdef FP_ECC
  21078. wc_ecc_fp_free();
  21079. #endif
  21080. #endif
  21081. return ret;
  21082. } /* END test_wc_ecc_export_x963_ex */
  21083. /*
  21084. * testing wc_ecc_import_x963()
  21085. */
  21086. static int test_wc_ecc_import_x963 (void)
  21087. {
  21088. int ret = 0;
  21089. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21090. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21091. ecc_key pubKey, key;
  21092. WC_RNG rng;
  21093. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21094. word32 x963Len = (word32)sizeof(x963);
  21095. /* Init stack variables. */
  21096. XMEMSET(x963, 0, x963Len);
  21097. XMEMSET(&rng, 0, sizeof(rng));
  21098. XMEMSET(&key, 0, sizeof(key));
  21099. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21100. ret = wc_InitRng(&rng);
  21101. if (ret == 0) {
  21102. ret = wc_ecc_init(&pubKey);
  21103. if (ret == 0) {
  21104. ret = wc_ecc_init(&key);
  21105. }
  21106. if (ret == 0) {
  21107. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21108. }
  21109. if (ret == 0) {
  21110. PRIVATE_KEY_UNLOCK();
  21111. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  21112. PRIVATE_KEY_LOCK();
  21113. }
  21114. }
  21115. printf(testingFmt, "wc_ecc_import_x963()");
  21116. if (ret == 0) {
  21117. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  21118. }
  21119. /* Test bad args. */
  21120. if (ret == 0) {
  21121. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  21122. if (ret == BAD_FUNC_ARG) {
  21123. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  21124. }
  21125. if (ret == BAD_FUNC_ARG) {
  21126. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  21127. }
  21128. if (ret == ECC_BAD_ARG_E) {
  21129. ret = 0;
  21130. } else if (ret == 0) {
  21131. ret = WOLFSSL_FATAL_ERROR;
  21132. }
  21133. }
  21134. printf(resultFmt, ret == 0 ? passed : failed);
  21135. if (wc_FreeRng(&rng) && ret == 0) {
  21136. ret = WOLFSSL_FATAL_ERROR;
  21137. }
  21138. wc_ecc_free(&key);
  21139. wc_ecc_free(&pubKey);
  21140. #ifdef FP_ECC
  21141. wc_ecc_fp_free();
  21142. #endif
  21143. #endif
  21144. return ret;
  21145. } /* END wc_ecc_import_x963 */
  21146. /*
  21147. * testing wc_ecc_import_private_key()
  21148. */
  21149. static int ecc_import_private_key (void)
  21150. {
  21151. int ret = 0;
  21152. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21153. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21154. ecc_key key, keyImp;
  21155. WC_RNG rng;
  21156. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21157. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21158. word32 privKeySz = (word32)sizeof(privKey);
  21159. word32 x963KeySz = (word32)sizeof(x963Key);
  21160. /* Init stack variables. */
  21161. XMEMSET(privKey, 0, privKeySz);
  21162. XMEMSET(x963Key, 0, x963KeySz);
  21163. XMEMSET(&rng, 0, sizeof(rng));
  21164. XMEMSET(&key, 0, sizeof(key));
  21165. XMEMSET(&keyImp, 0, sizeof(keyImp));
  21166. ret = wc_InitRng(&rng);
  21167. if (ret == 0) {
  21168. ret = wc_ecc_init(&key);
  21169. if (ret == 0) {
  21170. ret = wc_ecc_init(&keyImp);
  21171. }
  21172. if (ret == 0) {
  21173. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21174. }
  21175. if (ret == 0) {
  21176. PRIVATE_KEY_UNLOCK();
  21177. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  21178. PRIVATE_KEY_LOCK();
  21179. }
  21180. if (ret == 0) {
  21181. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  21182. }
  21183. }
  21184. printf(testingFmt, "wc_ecc_import_private_key()");
  21185. if (ret == 0) {
  21186. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21187. x963KeySz, &keyImp);
  21188. }
  21189. /* Pass in bad args. */
  21190. if (ret == 0) {
  21191. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21192. x963KeySz, NULL);
  21193. if (ret == BAD_FUNC_ARG) {
  21194. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  21195. x963KeySz, &keyImp);
  21196. }
  21197. if (ret == BAD_FUNC_ARG) {
  21198. ret = 0;
  21199. } else if (ret == 0) {
  21200. ret = WOLFSSL_FATAL_ERROR;
  21201. }
  21202. }
  21203. printf(resultFmt, ret == 0 ? passed : failed);
  21204. if (wc_FreeRng(&rng) && ret == 0) {
  21205. ret = WOLFSSL_FATAL_ERROR;
  21206. }
  21207. wc_ecc_free(&key);
  21208. wc_ecc_free(&keyImp);
  21209. #ifdef FP_ECC
  21210. wc_ecc_fp_free();
  21211. #endif
  21212. #endif
  21213. return ret;
  21214. } /* END wc_ecc_import_private_key */
  21215. /*
  21216. * Testing wc_ecc_export_private_only()
  21217. */
  21218. static int test_wc_ecc_export_private_only (void)
  21219. {
  21220. int ret = 0;
  21221. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21222. ecc_key key;
  21223. WC_RNG rng;
  21224. byte out[ECC_PRIV_KEY_BUF];
  21225. word32 outlen = sizeof(out);
  21226. /* Init stack variables. */
  21227. XMEMSET(out, 0, outlen);
  21228. XMEMSET(&rng, 0, sizeof(rng));
  21229. XMEMSET(&key, 0, sizeof(key));
  21230. ret = wc_InitRng(&rng);
  21231. if (ret == 0) {
  21232. ret = wc_ecc_init(&key);
  21233. if (ret == 0) {
  21234. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21235. }
  21236. }
  21237. printf(testingFmt, "wc_ecc_export_private_only()");
  21238. if (ret == 0) {
  21239. ret = wc_ecc_export_private_only(&key, out, &outlen);
  21240. }
  21241. /* Pass in bad args. */
  21242. if (ret == 0) {
  21243. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  21244. if (ret == BAD_FUNC_ARG) {
  21245. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  21246. }
  21247. if (ret == BAD_FUNC_ARG) {
  21248. ret = wc_ecc_export_private_only(&key, out, NULL);
  21249. }
  21250. if (ret == BAD_FUNC_ARG) {
  21251. ret = 0;
  21252. } else if (ret == 0) {
  21253. ret = WOLFSSL_FATAL_ERROR;
  21254. }
  21255. }
  21256. printf(resultFmt, ret == 0 ? passed : failed);
  21257. if (wc_FreeRng(&rng) && ret == 0) {
  21258. ret = WOLFSSL_FATAL_ERROR;
  21259. }
  21260. wc_ecc_free(&key);
  21261. #ifdef FP_ECC
  21262. wc_ecc_fp_free();
  21263. #endif
  21264. #endif
  21265. return ret;
  21266. } /* END test_wc_ecc_export_private_only */
  21267. /*
  21268. * Testing wc_ecc_rs_to_sig()
  21269. */
  21270. static int test_wc_ecc_rs_to_sig (void)
  21271. {
  21272. int ret = 0;
  21273. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21274. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21275. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21276. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21277. const char* zeroStr = "0";
  21278. byte sig[ECC_MAX_SIG_SIZE];
  21279. word32 siglen = (word32)sizeof(sig);
  21280. /*R and S max size is the order of curve. 2^192.*/
  21281. int keySz = KEY24;
  21282. byte r[KEY24];
  21283. byte s[KEY24];
  21284. word32 rlen = (word32)sizeof(r);
  21285. word32 slen = (word32)sizeof(s);
  21286. /* Init stack variables. */
  21287. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21288. XMEMSET(r, 0, keySz);
  21289. XMEMSET(s, 0, keySz);
  21290. printf(testingFmt, "wc_ecc_rs_to_sig()");
  21291. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  21292. /* Test bad args. */
  21293. if (ret == 0) {
  21294. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  21295. if (ret == ECC_BAD_ARG_E) {
  21296. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  21297. }
  21298. if (ret == ECC_BAD_ARG_E) {
  21299. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  21300. }
  21301. if (ret == ECC_BAD_ARG_E) {
  21302. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  21303. }
  21304. if (ret == ECC_BAD_ARG_E) {
  21305. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  21306. }
  21307. if (ret == MP_ZERO_E) {
  21308. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  21309. }
  21310. if (ret == MP_ZERO_E) {
  21311. ret = 0;
  21312. } else {
  21313. ret = WOLFSSL_FATAL_ERROR;
  21314. }
  21315. }
  21316. printf(resultFmt, ret == 0 ? passed : failed);
  21317. printf(testingFmt, "wc_ecc_sig_to_rs()");
  21318. if (ret == 0) {
  21319. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  21320. }
  21321. /* Test bad args. */
  21322. if (ret == 0) {
  21323. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  21324. if (ret == ECC_BAD_ARG_E) {
  21325. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  21326. }
  21327. if (ret == ECC_BAD_ARG_E) {
  21328. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  21329. }
  21330. if (ret == ECC_BAD_ARG_E) {
  21331. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  21332. }
  21333. if (ret == ECC_BAD_ARG_E) {
  21334. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  21335. }
  21336. if (ret == ECC_BAD_ARG_E) {
  21337. ret = 0;
  21338. } else if (ret == 0) {
  21339. ret = WOLFSSL_FATAL_ERROR;
  21340. }
  21341. }
  21342. printf(resultFmt, ret == 0 ? passed : failed);
  21343. #endif
  21344. return ret;
  21345. } /* END test_wc_ecc_rs_to_sig */
  21346. static int test_wc_ecc_import_raw(void)
  21347. {
  21348. int ret = 0;
  21349. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21350. ecc_key key;
  21351. const char* qx =
  21352. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21353. const char* qy =
  21354. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21355. const char* d =
  21356. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21357. const char* curveName = "SECP256R1";
  21358. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21359. const char* kNullStr = "";
  21360. #endif
  21361. ret = wc_ecc_init(&key);
  21362. printf(testingFmt, "wc_ecc_import_raw()");
  21363. if (ret == 0) {
  21364. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21365. }
  21366. /* Test bad args. */
  21367. if (ret == 0) {
  21368. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  21369. if (ret == BAD_FUNC_ARG) {
  21370. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  21371. }
  21372. if (ret == BAD_FUNC_ARG) {
  21373. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  21374. }
  21375. if (ret == BAD_FUNC_ARG) {
  21376. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  21377. }
  21378. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21379. if (ret == BAD_FUNC_ARG) {
  21380. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21381. wc_ecc_free(&key);
  21382. #endif
  21383. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  21384. if (ret == ECC_INF_E)
  21385. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  21386. }
  21387. #endif
  21388. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21389. if (ret == BAD_FUNC_ARG) {
  21390. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21391. wc_ecc_free(&key);
  21392. #endif
  21393. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  21394. }
  21395. if (ret == BAD_FUNC_ARG) {
  21396. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21397. wc_ecc_free(&key);
  21398. #endif
  21399. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  21400. }
  21401. #endif
  21402. if (ret == BAD_FUNC_ARG) {
  21403. ret = 0;
  21404. }
  21405. }
  21406. printf(resultFmt, ret == 0 ? passed : failed);
  21407. wc_ecc_free(&key);
  21408. #endif
  21409. return ret;
  21410. } /* END test_wc_ecc_import_raw */
  21411. static int test_wc_ecc_import_unsigned(void)
  21412. {
  21413. int ret = 0;
  21414. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21415. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  21416. ecc_key key;
  21417. const byte qx[] = {
  21418. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21419. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21420. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21421. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21422. };
  21423. const byte qy[] = {
  21424. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21425. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21426. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21427. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21428. };
  21429. const byte d[] = {
  21430. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21431. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21432. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21433. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21434. };
  21435. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21436. const byte nullBytes[32] = {0};
  21437. #endif
  21438. int curveId = ECC_SECP256R1;
  21439. ret = wc_ecc_init(&key);
  21440. printf(testingFmt, "wc_ecc_import_unsigned()");
  21441. if (ret == 0) {
  21442. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21443. curveId);
  21444. }
  21445. /* Test bad args. */
  21446. if (ret == 0) {
  21447. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21448. curveId);
  21449. if (ret == BAD_FUNC_ARG) {
  21450. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21451. curveId);
  21452. }
  21453. if (ret == BAD_FUNC_ARG) {
  21454. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21455. curveId);
  21456. }
  21457. if (ret == BAD_FUNC_ARG) {
  21458. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21459. ECC_CURVE_INVALID);
  21460. }
  21461. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21462. if (ret == BAD_FUNC_ARG) {
  21463. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21464. (byte*)nullBytes, (byte*)nullBytes, curveId);
  21465. }
  21466. #endif
  21467. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  21468. ret = 0;
  21469. }
  21470. }
  21471. printf(resultFmt, ret == 0 ? passed : failed);
  21472. wc_ecc_free(&key);
  21473. #endif
  21474. return ret;
  21475. } /* END test_wc_ecc_import_unsigned */
  21476. /*
  21477. * Testing wc_ecc_sig_size()
  21478. */
  21479. static int test_wc_ecc_sig_size (void)
  21480. {
  21481. int ret = 0;
  21482. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21483. ecc_key key;
  21484. WC_RNG rng;
  21485. int keySz = KEY16;
  21486. XMEMSET(&rng, 0, sizeof(rng));
  21487. XMEMSET(&key, 0, sizeof(key));
  21488. ret = wc_InitRng(&rng);
  21489. if (ret == 0) {
  21490. ret = wc_ecc_init(&key);
  21491. if (ret == 0) {
  21492. ret = wc_ecc_make_key(&rng, keySz, &key);
  21493. }
  21494. }
  21495. printf(testingFmt, "wc_ecc_sig_size()");
  21496. if (ret == 0) {
  21497. ret = wc_ecc_sig_size(&key);
  21498. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  21499. ret = 0;
  21500. }
  21501. }
  21502. printf(resultFmt, ret == 0 ? passed : failed);
  21503. if (wc_FreeRng(&rng) && ret == 0) {
  21504. ret = WOLFSSL_FATAL_ERROR;
  21505. }
  21506. wc_ecc_free(&key);
  21507. #endif
  21508. return ret;
  21509. } /* END test_wc_ecc_sig_size */
  21510. /*
  21511. * Testing wc_ecc_ctx_new()
  21512. */
  21513. static int test_wc_ecc_ctx_new (void)
  21514. {
  21515. int ret = 0;
  21516. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21517. WC_RNG rng;
  21518. ecEncCtx* cli = NULL;
  21519. ecEncCtx* srv = NULL;
  21520. ret = wc_InitRng(&rng);
  21521. printf(testingFmt, "wc_ecc_ctx_new()");
  21522. if (ret == 0) {
  21523. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  21524. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  21525. }
  21526. if (ret == 0 && (cli == NULL || srv == NULL)) {
  21527. ret = WOLFSSL_FATAL_ERROR;
  21528. }
  21529. wc_ecc_ctx_free(cli);
  21530. wc_ecc_ctx_free(srv);
  21531. /* Test bad args. */
  21532. if (ret == 0) {
  21533. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21534. cli = wc_ecc_ctx_new(0, &rng);
  21535. if (cli != NULL) {
  21536. ret = WOLFSSL_FATAL_ERROR;
  21537. }
  21538. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  21539. if (cli != NULL) {
  21540. ret = WOLFSSL_FATAL_ERROR;
  21541. }
  21542. }
  21543. printf(resultFmt, ret == 0 ? passed : failed);
  21544. if (wc_FreeRng(&rng) && ret == 0) {
  21545. ret = WOLFSSL_FATAL_ERROR;
  21546. }
  21547. wc_ecc_ctx_free(cli);
  21548. #endif
  21549. return ret;
  21550. } /* END test_wc_ecc_ctx_new */
  21551. /*
  21552. * Tesing wc_ecc_reset()
  21553. */
  21554. static int test_wc_ecc_ctx_reset (void)
  21555. {
  21556. int ret = 0;
  21557. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21558. ecEncCtx* ctx = NULL;
  21559. WC_RNG rng;
  21560. ret = wc_InitRng(&rng);
  21561. if (ret == 0) {
  21562. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  21563. ret = WOLFSSL_FATAL_ERROR;
  21564. }
  21565. }
  21566. printf(testingFmt, "wc_ecc_ctx_reset()");
  21567. if (ret == 0) {
  21568. ret = wc_ecc_ctx_reset(ctx, &rng);
  21569. }
  21570. /* Pass in bad args. */
  21571. if (ret == 0) {
  21572. ret = wc_ecc_ctx_reset(NULL, &rng);
  21573. if (ret == BAD_FUNC_ARG) {
  21574. ret = wc_ecc_ctx_reset(ctx, NULL);
  21575. }
  21576. if (ret == BAD_FUNC_ARG) {
  21577. ret = 0;
  21578. } else if (ret == 0) {
  21579. ret = WOLFSSL_FATAL_ERROR;
  21580. }
  21581. }
  21582. printf(resultFmt, ret == 0 ? passed : failed);
  21583. if (wc_FreeRng(&rng) && ret == 0) {
  21584. ret = WOLFSSL_FATAL_ERROR;
  21585. }
  21586. wc_ecc_ctx_free(ctx);
  21587. #endif
  21588. return ret;
  21589. } /* END test_wc_ecc_ctx_reset */
  21590. /*
  21591. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  21592. */
  21593. static int test_wc_ecc_ctx_set_peer_salt (void)
  21594. {
  21595. int ret = 0;
  21596. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21597. WC_RNG rng;
  21598. ecEncCtx* cliCtx = NULL;
  21599. ecEncCtx* servCtx = NULL;
  21600. const byte* cliSalt = NULL;
  21601. const byte* servSalt = NULL;
  21602. ret = wc_InitRng(&rng);
  21603. if (ret == 0) {
  21604. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  21605. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  21606. ret = WOLFSSL_FATAL_ERROR;
  21607. }
  21608. }
  21609. printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
  21610. /* Test bad args. */
  21611. if (ret == 0) {
  21612. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  21613. if (cliSalt != NULL) {
  21614. ret = WOLFSSL_FATAL_ERROR;
  21615. }
  21616. }
  21617. if (ret == 0) {
  21618. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  21619. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  21620. if (cliSalt == NULL || servSalt == NULL) {
  21621. ret = WOLFSSL_FATAL_ERROR;
  21622. }
  21623. }
  21624. printf(resultFmt, ret == 0 ? passed : failed);
  21625. printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
  21626. if (ret == 0) {
  21627. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  21628. }
  21629. /* Test bad args. */
  21630. if (ret == 0) {
  21631. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  21632. if (ret == BAD_FUNC_ARG) {
  21633. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  21634. }
  21635. if (ret == BAD_FUNC_ARG) {
  21636. ret = 0;
  21637. } else if (ret == 0) {
  21638. ret = WOLFSSL_FATAL_ERROR;
  21639. }
  21640. }
  21641. printf(resultFmt, ret == 0 ? passed : failed);
  21642. if (wc_FreeRng(&rng) && ret == 0) {
  21643. ret = WOLFSSL_FATAL_ERROR;
  21644. }
  21645. wc_ecc_ctx_free(cliCtx);
  21646. wc_ecc_ctx_free(servCtx);
  21647. #endif
  21648. return ret;
  21649. } /* END test_wc_ecc_ctx_set_peer_salt */
  21650. /*
  21651. * Testing wc_ecc_ctx_set_info()
  21652. */
  21653. static int test_wc_ecc_ctx_set_info (void)
  21654. {
  21655. int ret = 0;
  21656. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21657. ecEncCtx* ctx = NULL;
  21658. WC_RNG rng;
  21659. const char* optInfo = "Optional Test Info.";
  21660. int optInfoSz = (int)XSTRLEN(optInfo);
  21661. const char* badOptInfo = NULL;
  21662. ret = wc_InitRng(&rng);
  21663. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  21664. ret = WOLFSSL_FATAL_ERROR;
  21665. }
  21666. printf(testingFmt, "wc_ecc_ctx_set_info()");
  21667. if (ret == 0) {
  21668. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  21669. }
  21670. /* Test bad args. */
  21671. if (ret == 0) {
  21672. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  21673. if (ret == BAD_FUNC_ARG) {
  21674. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  21675. }
  21676. if (ret == BAD_FUNC_ARG) {
  21677. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  21678. }
  21679. if (ret == BAD_FUNC_ARG) {
  21680. ret = 0;
  21681. } else if (ret == 0) {
  21682. ret = WOLFSSL_FATAL_ERROR;
  21683. }
  21684. }
  21685. printf(resultFmt, ret == 0 ? passed : failed);
  21686. if (wc_FreeRng(&rng) && ret == 0) {
  21687. ret = WOLFSSL_FATAL_ERROR;
  21688. }
  21689. wc_ecc_ctx_free(ctx);
  21690. #endif
  21691. return ret;
  21692. } /* END test_wc_ecc_ctx_set_info */
  21693. /*
  21694. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  21695. */
  21696. static int test_wc_ecc_encryptDecrypt (void)
  21697. {
  21698. int ret = 0;
  21699. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  21700. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  21701. ecc_key srvKey, cliKey, tmpKey;
  21702. WC_RNG rng;
  21703. const char* msg = "EccBlock Size 16";
  21704. word32 msgSz = (word32)XSTRLEN(msg);
  21705. #ifdef WOLFSSL_ECIES_OLD
  21706. byte out[XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
  21707. #else
  21708. byte out[KEY20 * 2 + 1 + XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
  21709. #endif
  21710. word32 outSz = (word32)sizeof(out);
  21711. byte plain[XSTRLEN(msg) + 1];
  21712. word32 plainSz = (word32)sizeof(plain);
  21713. int keySz = KEY20;
  21714. /* Init stack variables. */
  21715. XMEMSET(out, 0, outSz);
  21716. XMEMSET(plain, 0, plainSz);
  21717. XMEMSET(&rng, 0, sizeof(rng));
  21718. XMEMSET(&srvKey, 0, sizeof(srvKey));
  21719. XMEMSET(&cliKey, 0, sizeof(cliKey));
  21720. ret = wc_InitRng(&rng);
  21721. if (ret == 0) {
  21722. ret = wc_ecc_init(&cliKey);
  21723. if (ret == 0) {
  21724. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  21725. }
  21726. if (ret == 0) {
  21727. ret = wc_ecc_init(&srvKey);
  21728. }
  21729. if (ret == 0) {
  21730. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  21731. }
  21732. if (ret == 0) {
  21733. ret = wc_ecc_init(&tmpKey);
  21734. }
  21735. }
  21736. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21737. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21738. !defined(HAVE_SELFTEST)
  21739. if (ret == 0) {
  21740. ret = wc_ecc_set_rng(&srvKey, &rng);
  21741. }
  21742. if (ret == 0) {
  21743. ret = wc_ecc_set_rng(&cliKey, &rng);
  21744. }
  21745. #endif
  21746. printf(testingFmt, "wc_ecc_encrypt()");
  21747. if (ret == 0) {
  21748. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21749. &outSz, NULL);
  21750. }
  21751. if (ret == 0) {
  21752. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  21753. &outSz, NULL);
  21754. if (ret == BAD_FUNC_ARG) {
  21755. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  21756. &outSz, NULL);
  21757. }
  21758. if (ret == BAD_FUNC_ARG) {
  21759. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  21760. &outSz, NULL);
  21761. }
  21762. if (ret == BAD_FUNC_ARG) {
  21763. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  21764. &outSz, NULL);
  21765. }
  21766. if (ret == BAD_FUNC_ARG) {
  21767. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21768. NULL, NULL);
  21769. }
  21770. if (ret == BAD_FUNC_ARG) {
  21771. ret = 0;
  21772. } else if (ret == 0) {
  21773. ret = WOLFSSL_FATAL_ERROR;
  21774. }
  21775. }
  21776. printf(resultFmt, ret == 0 ? passed : failed);
  21777. printf(testingFmt, "wc_ecc_decrypt()");
  21778. #ifdef WOLFSSL_ECIES_OLD
  21779. if (ret == 0) {
  21780. tmpKey.dp = cliKey.dp;
  21781. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  21782. }
  21783. #endif
  21784. if (ret == 0) {
  21785. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  21786. &plainSz, NULL);
  21787. }
  21788. if (ret == 0) {
  21789. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  21790. &plainSz, NULL);
  21791. #ifdef WOLFSSL_ECIES_OLD
  21792. /* NULL parameter allowed in new implementations - public key comes from
  21793. * the message. */
  21794. if (ret == BAD_FUNC_ARG) {
  21795. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  21796. &plainSz, NULL);
  21797. }
  21798. #endif
  21799. if (ret == BAD_FUNC_ARG) {
  21800. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  21801. &plainSz, NULL);
  21802. }
  21803. if (ret == BAD_FUNC_ARG) {
  21804. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  21805. &plainSz, NULL);
  21806. }
  21807. if (ret == BAD_FUNC_ARG) {
  21808. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  21809. plain, NULL, NULL);
  21810. }
  21811. if (ret == BAD_FUNC_ARG) {
  21812. ret = 0;
  21813. } else if (ret == 0) {
  21814. ret = WOLFSSL_FATAL_ERROR;
  21815. }
  21816. }
  21817. if (XMEMCMP(msg, plain, msgSz) != 0) {
  21818. ret = WOLFSSL_FATAL_ERROR;
  21819. }
  21820. printf(resultFmt, ret == 0 ? passed : failed);
  21821. if (wc_FreeRng(&rng) && ret == 0) {
  21822. ret = WOLFSSL_FATAL_ERROR;
  21823. }
  21824. wc_ecc_free(&tmpKey);
  21825. wc_ecc_free(&cliKey);
  21826. wc_ecc_free(&srvKey);
  21827. #endif
  21828. return ret;
  21829. } /* END test_wc_ecc_encryptDecrypt */
  21830. /*
  21831. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  21832. */
  21833. static int test_wc_ecc_del_point (void)
  21834. {
  21835. int ret = 0;
  21836. #if defined(HAVE_ECC)
  21837. ecc_point* pt;
  21838. printf(testingFmt, "wc_ecc_new_point()");
  21839. pt = wc_ecc_new_point();
  21840. if (!pt) {
  21841. ret = WOLFSSL_FATAL_ERROR;
  21842. }
  21843. printf(resultFmt, ret == 0 ? passed : failed);
  21844. wc_ecc_del_point(pt);
  21845. #endif
  21846. return ret;
  21847. } /* END test_wc_ecc_del_point */
  21848. /*
  21849. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  21850. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  21851. * and wc_ecc_cmp_point()
  21852. */
  21853. static int test_wc_ecc_pointFns (void)
  21854. {
  21855. int ret = 0;
  21856. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  21857. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21858. !defined(WOLFSSL_ATECC608A)
  21859. ecc_key key;
  21860. WC_RNG rng;
  21861. ecc_point* point = NULL;
  21862. ecc_point* cpypt = NULL;
  21863. int idx = 0;
  21864. int keySz = KEY32;
  21865. byte der[DER_SZ(KEY32)];
  21866. word32 derlenChk = 0;
  21867. word32 derSz = DER_SZ(KEY32);
  21868. /* Init stack variables. */
  21869. XMEMSET(der, 0, derSz);
  21870. XMEMSET(&rng, 0, sizeof(rng));
  21871. XMEMSET(&key, 0, sizeof(key));
  21872. ret = wc_InitRng(&rng);
  21873. if (ret == 0) {
  21874. ret = wc_ecc_init(&key);
  21875. if (ret == 0) {
  21876. ret = wc_ecc_make_key(&rng, keySz, &key);
  21877. }
  21878. }
  21879. if (ret == 0) {
  21880. point = wc_ecc_new_point();
  21881. if (!point) {
  21882. ret = WOLFSSL_FATAL_ERROR;
  21883. }
  21884. }
  21885. if (ret == 0) {
  21886. cpypt = wc_ecc_new_point();
  21887. if (!cpypt) {
  21888. ret = WOLFSSL_FATAL_ERROR;
  21889. }
  21890. }
  21891. /* Export */
  21892. printf(testingFmt, "wc_ecc_export_point_der()");
  21893. if (ret == 0) {
  21894. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  21895. NULL, &derlenChk);
  21896. /* Check length value. */
  21897. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  21898. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  21899. der, &derSz);
  21900. }
  21901. }
  21902. /* Test bad args. */
  21903. if (ret == 0) {
  21904. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  21905. if (ret == ECC_BAD_ARG_E) {
  21906. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  21907. }
  21908. if (ret == ECC_BAD_ARG_E) {
  21909. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  21910. der, NULL);
  21911. }
  21912. if (ret == ECC_BAD_ARG_E) {
  21913. ret = 0;
  21914. } else if (ret == 0) {
  21915. ret = WOLFSSL_FATAL_ERROR;
  21916. }
  21917. }
  21918. printf(resultFmt, ret == 0 ? passed : failed);
  21919. /* Import */
  21920. printf(testingFmt, "wc_ecc_import_point_der()");
  21921. if (ret == 0) {
  21922. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  21923. /* Condition double checks wc_ecc_cmp_point(). */
  21924. if (ret == 0 && XMEMCMP(&key.pubkey, point, sizeof(key.pubkey))) {
  21925. ret = wc_ecc_cmp_point(&key.pubkey, point);
  21926. }
  21927. }
  21928. /* Test bad args. */
  21929. if (ret == 0) {
  21930. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  21931. if (ret == ECC_BAD_ARG_E) {
  21932. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  21933. }
  21934. if (ret == ECC_BAD_ARG_E) {
  21935. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  21936. }
  21937. if (ret == ECC_BAD_ARG_E) {
  21938. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  21939. }
  21940. if (ret == ECC_BAD_ARG_E) {
  21941. ret = 0;
  21942. } else if (ret == 0) {
  21943. ret = WOLFSSL_FATAL_ERROR;
  21944. }
  21945. }
  21946. printf(resultFmt, ret == 0 ? passed : failed);
  21947. /* Copy */
  21948. printf(testingFmt, "wc_ecc_copy_point()");
  21949. if (ret == 0) {
  21950. ret = wc_ecc_copy_point(point, cpypt);
  21951. }
  21952. /* Test bad args. */
  21953. if (ret == 0) {
  21954. ret = wc_ecc_copy_point(NULL, cpypt);
  21955. if (ret == ECC_BAD_ARG_E) {
  21956. ret = wc_ecc_copy_point(point, NULL);
  21957. }
  21958. if (ret == ECC_BAD_ARG_E) {
  21959. ret = 0;
  21960. } else if (ret == 0) {
  21961. ret = WOLFSSL_FATAL_ERROR;
  21962. }
  21963. }
  21964. printf(resultFmt, ret == 0 ? passed : failed);
  21965. printf(testingFmt, "wc_ecc_cmp_point()");
  21966. /* Compare point */
  21967. if (ret == 0) {
  21968. ret = wc_ecc_cmp_point(point, cpypt);
  21969. }
  21970. /* Test bad args. */
  21971. if (ret == 0) {
  21972. ret = wc_ecc_cmp_point(NULL, cpypt);
  21973. if (ret == BAD_FUNC_ARG) {
  21974. ret = wc_ecc_cmp_point(point, NULL);
  21975. }
  21976. if (ret == BAD_FUNC_ARG) {
  21977. ret = 0;
  21978. } else if (ret == 0) {
  21979. ret = WOLFSSL_FATAL_ERROR;
  21980. }
  21981. }
  21982. printf(resultFmt, ret == 0 ? passed : failed);
  21983. printf(testingFmt, "wc_ecc_point_is_at_infinity()");
  21984. /* At infinity if return == 1, otherwise return == 0. */
  21985. if (ret == 0) {
  21986. ret = wc_ecc_point_is_at_infinity(point);
  21987. }
  21988. /* Test bad args. */
  21989. if (ret == 0) {
  21990. ret = wc_ecc_point_is_at_infinity(NULL);
  21991. if (ret == BAD_FUNC_ARG) {
  21992. ret = 0;
  21993. } else if (ret == 0) {
  21994. ret = WOLFSSL_FATAL_ERROR;
  21995. }
  21996. }
  21997. printf(resultFmt, ret == 0 ? passed : failed);
  21998. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  21999. #ifdef USE_ECC_B_PARAM
  22000. printf(testingFmt, "wc_ecc_point_is_on_curve()");
  22001. /* On curve if ret == 0 */
  22002. if (ret == 0) {
  22003. ret = wc_ecc_point_is_on_curve(point, idx);
  22004. }
  22005. /* Test bad args. */
  22006. if (ret == 0) {
  22007. ret = wc_ecc_point_is_on_curve(NULL, idx);
  22008. if (ret == BAD_FUNC_ARG) {
  22009. ret = wc_ecc_point_is_on_curve(point, 1000);
  22010. }
  22011. if (ret == ECC_BAD_ARG_E) {
  22012. ret = 0;
  22013. } else if (ret == 0) {
  22014. ret = WOLFSSL_FATAL_ERROR;
  22015. }
  22016. }
  22017. printf(resultFmt, ret == 0 ? passed : failed);
  22018. #endif /* USE_ECC_B_PARAM */
  22019. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  22020. /* Free */
  22021. wc_ecc_del_point(point);
  22022. wc_ecc_del_point(cpypt);
  22023. wc_ecc_free(&key);
  22024. if (wc_FreeRng(&rng) && ret == 0) {
  22025. ret = WOLFSSL_FATAL_ERROR;
  22026. }
  22027. #endif
  22028. return ret;
  22029. } /* END test_wc_ecc_pointFns */
  22030. /*
  22031. * Testing wc_ecc_sahred_secret_ssh()
  22032. */
  22033. static int test_wc_ecc_shared_secret_ssh (void)
  22034. {
  22035. int ret = 0;
  22036. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22037. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22038. !defined(WOLFSSL_ATECC608A)
  22039. ecc_key key, key2;
  22040. WC_RNG rng;
  22041. int keySz = KEY32;
  22042. int key2Sz = KEY24;
  22043. byte secret[KEY32];
  22044. word32 secretLen = keySz;
  22045. /* Init stack variables. */
  22046. XMEMSET(secret, 0, secretLen);
  22047. XMEMSET(&rng, 0, sizeof(rng));
  22048. XMEMSET(&key, 0, sizeof(key));
  22049. XMEMSET(&key2, 0, sizeof(key2));
  22050. /* Make keys */
  22051. ret = wc_InitRng(&rng);
  22052. if (ret == 0) {
  22053. ret = wc_ecc_init(&key);
  22054. if (ret == 0) {
  22055. ret = wc_ecc_make_key(&rng, keySz, &key);
  22056. }
  22057. if (wc_FreeRng(&rng) && ret == 0) {
  22058. ret = WOLFSSL_FATAL_ERROR;
  22059. }
  22060. }
  22061. if (ret == 0) {
  22062. ret = wc_InitRng(&rng);
  22063. if (ret == 0) {
  22064. ret = wc_ecc_init(&key2);
  22065. }
  22066. if (ret == 0) {
  22067. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22068. }
  22069. }
  22070. printf(testingFmt, "ecc_shared_secret_ssh()");
  22071. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22072. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22073. !defined(HAVE_SELFTEST)
  22074. if (ret == 0) {
  22075. ret = wc_ecc_set_rng(&key, &rng);
  22076. }
  22077. #endif
  22078. if (ret == 0) {
  22079. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  22080. }
  22081. /* Pass in bad args. */
  22082. if (ret == 0) {
  22083. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  22084. if (ret == BAD_FUNC_ARG) {
  22085. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  22086. }
  22087. if (ret == BAD_FUNC_ARG) {
  22088. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  22089. }
  22090. if (ret == BAD_FUNC_ARG) {
  22091. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  22092. }
  22093. if (ret == BAD_FUNC_ARG) {
  22094. key.type = ECC_PUBLICKEY;
  22095. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  22096. if (ret == ECC_BAD_ARG_E) {
  22097. ret = 0;
  22098. } else if (ret == 0) {
  22099. ret = WOLFSSL_FATAL_ERROR;
  22100. }
  22101. } else if (ret == 0) {
  22102. ret = WOLFSSL_FATAL_ERROR;
  22103. }
  22104. }
  22105. printf(resultFmt, ret == 0 ? passed : failed);
  22106. if (wc_FreeRng(&rng) && ret == 0) {
  22107. ret = WOLFSSL_FATAL_ERROR;
  22108. }
  22109. wc_ecc_free(&key);
  22110. wc_ecc_free(&key2);
  22111. #ifdef FP_ECC
  22112. wc_ecc_fp_free();
  22113. #endif
  22114. #endif
  22115. return ret;
  22116. } /* END test_wc_ecc_shared_secret_ssh */
  22117. /*
  22118. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22119. */
  22120. static int test_wc_ecc_verify_hash_ex (void)
  22121. {
  22122. int ret = 0;
  22123. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22124. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22125. !defined(WOLFSSL_ATECC608A)
  22126. ecc_key key;
  22127. WC_RNG rng;
  22128. mp_int r;
  22129. mp_int s;
  22130. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22131. unsigned char iHash[] = "Everyone gets Friday off.......";
  22132. unsigned char shortHash[] = TEST_STRING;
  22133. word32 hashlen = sizeof(hash);
  22134. word32 iHashLen = sizeof(iHash);
  22135. word32 shortHashLen = sizeof(shortHash);
  22136. int keySz = KEY32;
  22137. int sig = WOLFSSL_FATAL_ERROR;
  22138. int ver = WOLFSSL_FATAL_ERROR;
  22139. int verify_ok = 0;
  22140. /* Initialize r and s. */
  22141. ret = mp_init_multi(&r, &s, NULL, NULL, NULL, NULL);
  22142. if (ret != MP_OKAY) {
  22143. return MP_INIT_E;
  22144. }
  22145. ret = wc_InitRng(&rng);
  22146. if (ret == 0) {
  22147. ret = wc_ecc_init(&key);
  22148. if (ret == 0) {
  22149. ret = wc_ecc_make_key(&rng, keySz, &key);
  22150. }
  22151. }
  22152. if (ret == 0) {
  22153. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  22154. if (ret == 0) {
  22155. /* verify_ok should be 1. */
  22156. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  22157. if (verify_ok != 1 && ret == 0) {
  22158. ret = WOLFSSL_FATAL_ERROR;
  22159. }
  22160. }
  22161. if (ret == 0) {
  22162. /* verify_ok should be 0 */
  22163. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  22164. &verify_ok, &key);
  22165. if (verify_ok != 0 && ret == 0) {
  22166. ret = WOLFSSL_FATAL_ERROR;
  22167. }
  22168. }
  22169. if (ret == 0) {
  22170. /* verify_ok should be 0. */
  22171. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22172. &verify_ok, &key);
  22173. if (verify_ok != 0 && ret == 0) {
  22174. ret = WOLFSSL_FATAL_ERROR;
  22175. }
  22176. }
  22177. }
  22178. printf(testingFmt, "wc_ecc_sign_hash_ex()");
  22179. /* Test bad args. */
  22180. if (ret == 0) {
  22181. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  22182. == ECC_BAD_ARG_E) {
  22183. sig = 0;
  22184. }
  22185. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  22186. != ECC_BAD_ARG_E) {
  22187. sig = WOLFSSL_FATAL_ERROR;
  22188. }
  22189. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  22190. != ECC_BAD_ARG_E) {
  22191. sig = WOLFSSL_FATAL_ERROR;
  22192. }
  22193. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  22194. != ECC_BAD_ARG_E) {
  22195. sig = WOLFSSL_FATAL_ERROR;
  22196. }
  22197. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  22198. != ECC_BAD_ARG_E) {
  22199. sig = WOLFSSL_FATAL_ERROR;
  22200. }
  22201. }
  22202. printf(resultFmt, sig == 0 ? passed : failed);
  22203. printf(testingFmt, "wc_ecc_verify_hash_ex()");
  22204. /* Test bad args. */
  22205. if (ret == 0) {
  22206. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  22207. == ECC_BAD_ARG_E) {
  22208. ver = 0;
  22209. }
  22210. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22211. &verify_ok, &key) != ECC_BAD_ARG_E) {
  22212. ver = WOLFSSL_FATAL_ERROR;
  22213. }
  22214. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22215. &key) != ECC_BAD_ARG_E) {
  22216. ver = WOLFSSL_FATAL_ERROR;
  22217. }
  22218. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22219. NULL, &key) != ECC_BAD_ARG_E) {
  22220. ver = WOLFSSL_FATAL_ERROR;
  22221. }
  22222. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22223. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  22224. ver = WOLFSSL_FATAL_ERROR;
  22225. }
  22226. }
  22227. printf(resultFmt, ver == 0 ? passed : failed);
  22228. wc_ecc_free(&key);
  22229. mp_free(&r);
  22230. mp_free(&s);
  22231. if (wc_FreeRng(&rng)) {
  22232. return WOLFSSL_FATAL_ERROR;
  22233. }
  22234. if (ret == 0 && (sig != 0 || ver != 0)) {
  22235. ret = WOLFSSL_FATAL_ERROR;
  22236. }
  22237. #endif
  22238. return ret;
  22239. } /* END test_wc_ecc_verify_hash_ex */
  22240. /*
  22241. * Testing wc_ecc_mulmod()
  22242. */
  22243. static int test_wc_ecc_mulmod (void)
  22244. {
  22245. int ret = 0;
  22246. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22247. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22248. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22249. ecc_key key1, key2, key3;
  22250. WC_RNG rng;
  22251. ret = wc_InitRng(&rng);
  22252. if (ret == 0) {
  22253. ret = wc_ecc_init(&key1);
  22254. if (ret == 0) {
  22255. ret = wc_ecc_init(&key2);
  22256. }
  22257. if (ret == 0) {
  22258. ret = wc_ecc_init(&key3);
  22259. }
  22260. if (ret == 0) {
  22261. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22262. }
  22263. wc_FreeRng(&rng);
  22264. }
  22265. if (ret == 0) {
  22266. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  22267. ECC_SECP256R1);
  22268. if (ret == 0) {
  22269. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22270. key1.dp->prime, ECC_SECP256R1);
  22271. }
  22272. }
  22273. printf(testingFmt, "wc_ecc_mulmod()");
  22274. if (ret == 0) {
  22275. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  22276. &key3.k, 1);
  22277. }
  22278. /* Test bad args. */
  22279. if (ret == 0) {
  22280. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  22281. &key3.k, 1);
  22282. if (ret == ECC_BAD_ARG_E) {
  22283. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  22284. &key3.k, 1);
  22285. }
  22286. if (ret == ECC_BAD_ARG_E) {
  22287. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  22288. &key3.k, 1);
  22289. }
  22290. if (ret == ECC_BAD_ARG_E) {
  22291. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  22292. &key2.k, NULL, 1);
  22293. }
  22294. if (ret == ECC_BAD_ARG_E) {
  22295. ret = 0;
  22296. } else if (ret == 0) {
  22297. ret = WOLFSSL_FATAL_ERROR;
  22298. }
  22299. }
  22300. printf(resultFmt, ret == 0 ? passed : failed);
  22301. wc_ecc_free(&key1);
  22302. wc_ecc_free(&key2);
  22303. wc_ecc_free(&key3);
  22304. #ifdef FP_ECC
  22305. wc_ecc_fp_free();
  22306. #endif
  22307. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22308. return ret;
  22309. } /* END test_wc_ecc_mulmod */
  22310. /*
  22311. * Testing wc_ecc_is_valid_idx()
  22312. */
  22313. static int test_wc_ecc_is_valid_idx (void)
  22314. {
  22315. int ret = 0;
  22316. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22317. ecc_key key;
  22318. WC_RNG rng;
  22319. int iVal = -2;
  22320. int iVal2 = 3000;
  22321. XMEMSET(&rng, 0, sizeof(rng));
  22322. XMEMSET(&key, 0, sizeof(key));
  22323. ret = wc_InitRng(&rng);
  22324. if (ret == 0) {
  22325. ret = wc_ecc_init(&key);
  22326. if (ret == 0) {
  22327. ret = wc_ecc_make_key(&rng, 32, &key);
  22328. }
  22329. }
  22330. printf(testingFmt, "wc_ecc_is_valid_idx()");
  22331. if (ret == 0) {
  22332. ret = wc_ecc_is_valid_idx(key.idx);
  22333. if (ret == 1) {
  22334. ret = 0;
  22335. } else {
  22336. ret = WOLFSSL_FATAL_ERROR;
  22337. }
  22338. }
  22339. /* Test bad args. */
  22340. if (ret == 0) {
  22341. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  22342. if (ret == 0) {
  22343. ret = wc_ecc_is_valid_idx(iVal2);
  22344. }
  22345. if (ret != 0) {
  22346. ret = WOLFSSL_FATAL_ERROR;
  22347. }
  22348. }
  22349. printf(resultFmt, ret == 0 ? passed : failed);
  22350. if (wc_FreeRng(&rng) && ret == 0) {
  22351. ret = WOLFSSL_FATAL_ERROR;
  22352. }
  22353. wc_ecc_free(&key);
  22354. #ifdef FP_ECC
  22355. wc_ecc_fp_free();
  22356. #endif
  22357. #endif
  22358. return ret;
  22359. } /* END test_wc_ecc_is_valid_idx */
  22360. /*
  22361. * Testing wc_ecc_get_curve_id_from_oid()
  22362. */
  22363. static int test_wc_ecc_get_curve_id_from_oid (void)
  22364. {
  22365. int ret = 0;
  22366. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22367. !defined(HAVE_FIPS)
  22368. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22369. word32 len = sizeof(oid);
  22370. printf(testingFmt, "wc_ecc_get_curve_id_from_oid()");
  22371. /* Bad Cases */
  22372. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  22373. if (ret == BAD_FUNC_ARG) {
  22374. ret = 0;
  22375. }
  22376. if (ret == 0) {
  22377. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  22378. if (ret == ECC_CURVE_INVALID) {
  22379. ret = 0;
  22380. }
  22381. }
  22382. /* Good Case */
  22383. if (ret == 0) {
  22384. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  22385. if (ret == 7) {
  22386. ret = 0;
  22387. }
  22388. }
  22389. printf(resultFmt, ret == 0 ? passed : failed);
  22390. #endif
  22391. return ret;
  22392. }/* END test_wc_ecc_get_curve_id_from_oid */
  22393. /*
  22394. * Testing wc_ecc_sig_size_calc()
  22395. */
  22396. static int test_wc_ecc_sig_size_calc (void)
  22397. {
  22398. int ret = 0;
  22399. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22400. ecc_key key;
  22401. WC_RNG rng;
  22402. int sz = 0;
  22403. printf(testingFmt, "wc_ecc_sig_size_calc()");
  22404. ret = wc_InitRng(&rng);
  22405. if (ret == 0) {
  22406. ret = wc_ecc_init(&key);
  22407. if (ret == 0) {
  22408. ret = wc_ecc_make_key(&rng, 16, &key);
  22409. }
  22410. sz = key.dp->size;
  22411. }
  22412. if (ret == 0) {
  22413. ret = wc_ecc_sig_size_calc(sz);
  22414. if (ret > 0) {
  22415. ret = 0;
  22416. }
  22417. }
  22418. printf(resultFmt, ret == 0 ? passed : failed);
  22419. wc_ecc_free(&key);
  22420. wc_FreeRng(&rng);
  22421. #endif
  22422. return ret;
  22423. } /* END test_wc_ecc_sig_size_calc */
  22424. /*
  22425. * Testing ToTraditional
  22426. */
  22427. static int test_ToTraditional (void)
  22428. {
  22429. int ret = 0;
  22430. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22431. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22432. defined(OPENSSL_EXTRA_X509_SMALL))
  22433. XFILE f;
  22434. byte input[TWOK_BUF];
  22435. word32 sz;
  22436. printf(testingFmt, "ToTraditional()");
  22437. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  22438. AssertTrue((f != XBADFILE));
  22439. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  22440. XFCLOSE(f);
  22441. /* Good case */
  22442. ret = ToTraditional(input, sz);
  22443. if (ret > 0) {
  22444. ret = 0;
  22445. }
  22446. /* Bad cases */
  22447. if (ret == 0) {
  22448. ret = ToTraditional(NULL, 0);
  22449. if (ret == BAD_FUNC_ARG) {
  22450. ret = 0;
  22451. }
  22452. }
  22453. if (ret == 0) {
  22454. ret = ToTraditional(NULL, sz);
  22455. if (ret == BAD_FUNC_ARG) {
  22456. ret = 0;
  22457. }
  22458. }
  22459. if (ret == 0) {
  22460. ret = ToTraditional(input, 0);
  22461. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  22462. ret = 0;
  22463. }
  22464. }
  22465. printf(resultFmt, ret == 0 ? passed : failed);
  22466. #endif
  22467. return ret;
  22468. }/* End test_ToTraditional*/
  22469. /*
  22470. * Testing wc_EccPrivateKeyToDer
  22471. */
  22472. static int test_wc_EccPrivateKeyToDer (void)
  22473. {
  22474. int ret = 0;
  22475. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22476. byte output[ONEK_BUF];
  22477. ecc_key eccKey;
  22478. WC_RNG rng;
  22479. word32 inLen;
  22480. printf(testingFmt, "wc_EccPrivateKeyToDer()");
  22481. ret = wc_InitRng(&rng);
  22482. if (ret == 0) {
  22483. ret = wc_ecc_init(&eccKey);
  22484. if (ret == 0) {
  22485. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22486. }
  22487. inLen = (word32)sizeof(output);
  22488. /* Bad Cases */
  22489. if (ret == 0) {
  22490. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  22491. if (ret == BAD_FUNC_ARG) {
  22492. ret = 0;
  22493. }
  22494. }
  22495. if (ret == 0) {
  22496. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  22497. if (ret == BAD_FUNC_ARG) {
  22498. ret = 0;
  22499. }
  22500. }
  22501. if (ret == 0) {
  22502. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  22503. if (ret == LENGTH_ONLY_E) {
  22504. ret = 0;
  22505. }
  22506. }
  22507. if (ret == 0) {
  22508. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  22509. if (ret == BAD_FUNC_ARG) {
  22510. ret = 0;
  22511. }
  22512. }
  22513. /*Good Case */
  22514. if (ret == 0) {
  22515. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  22516. if (ret > 0) {
  22517. ret = 0;
  22518. }
  22519. }
  22520. wc_ecc_free(&eccKey);
  22521. }
  22522. wc_FreeRng(&rng);
  22523. printf(resultFmt, ret == 0 ? passed : failed);
  22524. #endif
  22525. return ret;
  22526. }/* End test_wc_EccPrivateKeyToDer*/
  22527. /*
  22528. * Testing wc_DhPublicKeyDecode
  22529. */
  22530. static int test_wc_DhPublicKeyDecode(void)
  22531. {
  22532. int ret = 0;
  22533. word32 inOutIdx;
  22534. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  22535. DhKey key;
  22536. AssertIntEQ(wc_InitDhKey(&key), 0);
  22537. printf(testingFmt, "wc_DhPublicKeyDecode()");
  22538. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  22539. BAD_FUNC_ARG);
  22540. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22541. BAD_FUNC_ARG);
  22542. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22543. BAD_FUNC_ARG);
  22544. inOutIdx = 0;
  22545. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  22546. BAD_FUNC_ARG);
  22547. inOutIdx = 0;
  22548. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  22549. BAD_FUNC_ARG);
  22550. inOutIdx = 0;
  22551. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  22552. sizeof_dh_pub_key_der_2048), 0);
  22553. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  22554. key.pub.used != 0 && key.priv.used == 0);
  22555. wc_FreeDhKey(&key);
  22556. printf(resultFmt, passed);
  22557. #endif
  22558. (void)inOutIdx;
  22559. return ret;
  22560. }
  22561. /*
  22562. * Testing wc_Ed25519KeyToDer
  22563. */
  22564. static int test_wc_Ed25519KeyToDer (void)
  22565. {
  22566. int ret = 0;
  22567. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22568. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22569. byte output[ONEK_BUF];
  22570. ed25519_key ed25519Key;
  22571. WC_RNG rng;
  22572. word32 inLen;
  22573. printf(testingFmt, "wc_Ed25519KeyToDer()");
  22574. ret = wc_InitRng(&rng);
  22575. if (ret == 0) {
  22576. ret = wc_ed25519_init(&ed25519Key);
  22577. if (ret == 0) {
  22578. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  22579. }
  22580. inLen = (word32)sizeof(output);
  22581. /* Bad Cases */
  22582. if (ret == 0) {
  22583. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  22584. if (ret == BAD_FUNC_ARG) {
  22585. ret = 0;
  22586. }
  22587. }
  22588. if (ret == 0) {
  22589. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  22590. if (ret == BAD_FUNC_ARG) {
  22591. ret = 0;
  22592. }
  22593. }
  22594. if (ret == 0) {
  22595. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  22596. if (ret == BAD_FUNC_ARG) {
  22597. ret = 0;
  22598. }
  22599. }
  22600. if (ret == 0) {
  22601. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  22602. if (ret == BAD_FUNC_ARG) {
  22603. ret = 0;
  22604. }
  22605. }
  22606. /* Good Case */
  22607. if (ret == 0) {
  22608. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  22609. if (ret > 0) {
  22610. ret = 0;
  22611. }
  22612. }
  22613. wc_ed25519_free(&ed25519Key);
  22614. }
  22615. wc_FreeRng(&rng);
  22616. printf(resultFmt, ret == 0 ? passed : failed);
  22617. #endif
  22618. return ret;
  22619. }/* End test_wc_Ed25519KeyToDer*/
  22620. /*
  22621. * Testing wc_Ed25519PrivateKeyToDer
  22622. */
  22623. static int test_wc_Ed25519PrivateKeyToDer (void)
  22624. {
  22625. int ret = 0;
  22626. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22627. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22628. byte output[ONEK_BUF];
  22629. ed25519_key ed25519PrivKey;
  22630. WC_RNG rng;
  22631. word32 inLen;
  22632. printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
  22633. ret = wc_InitRng(&rng);
  22634. if (ret == 0) {
  22635. ret = wc_ed25519_init(&ed25519PrivKey);
  22636. if (ret == 0) {
  22637. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  22638. }
  22639. inLen = (word32)sizeof(output);
  22640. /* Bad Cases */
  22641. if (ret == 0) {
  22642. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  22643. if (ret == BAD_FUNC_ARG) {
  22644. ret = 0;
  22645. }
  22646. }
  22647. if (ret == 0) {
  22648. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  22649. if (ret == BAD_FUNC_ARG) {
  22650. ret = 0;
  22651. }
  22652. }
  22653. if (ret == 0) {
  22654. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  22655. if (ret == BAD_FUNC_ARG) {
  22656. ret = 0;
  22657. }
  22658. }
  22659. if (ret == 0) {
  22660. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  22661. if (ret == BAD_FUNC_ARG) {
  22662. ret = 0;
  22663. }
  22664. }
  22665. /* Good Case */
  22666. if (ret == 0) {
  22667. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  22668. if (ret > 0) {
  22669. ret = 0;
  22670. }
  22671. }
  22672. wc_ed25519_free(&ed25519PrivKey);
  22673. }
  22674. wc_FreeRng(&rng);
  22675. printf(resultFmt, ret == 0 ? passed : failed);
  22676. #endif
  22677. return ret;
  22678. }/* End test_wc_Ed25519PrivateKeyToDer*/
  22679. /*
  22680. * Testing wc_Ed448KeyToDer
  22681. */
  22682. static int test_wc_Ed448KeyToDer (void)
  22683. {
  22684. int ret = 0;
  22685. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22686. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22687. byte output[ONEK_BUF];
  22688. ed448_key ed448Key;
  22689. WC_RNG rng;
  22690. word32 inLen;
  22691. printf(testingFmt, "wc_Ed448KeyToDer()");
  22692. ret = wc_InitRng(&rng);
  22693. if (ret == 0) {
  22694. ret = wc_ed448_init(&ed448Key);
  22695. if (ret == 0) {
  22696. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  22697. }
  22698. inLen = sizeof(output);
  22699. /* Bad Cases */
  22700. if (ret == 0) {
  22701. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  22702. if (ret == BAD_FUNC_ARG) {
  22703. ret = 0;
  22704. }
  22705. }
  22706. if (ret == 0) {
  22707. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  22708. if (ret == BAD_FUNC_ARG) {
  22709. ret = 0;
  22710. }
  22711. }
  22712. if (ret == 0) {
  22713. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  22714. if (ret == BAD_FUNC_ARG) {
  22715. ret = 0;
  22716. }
  22717. }
  22718. if (ret == 0) {
  22719. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  22720. if (ret == BAD_FUNC_ARG) {
  22721. ret = 0;
  22722. }
  22723. }
  22724. /* Good Case */
  22725. if (ret == 0) {
  22726. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  22727. if (ret > 0) {
  22728. ret = 0;
  22729. }
  22730. }
  22731. wc_ed448_free(&ed448Key);
  22732. }
  22733. wc_FreeRng(&rng);
  22734. printf(resultFmt, ret == 0 ? passed : failed);
  22735. #endif
  22736. return ret;
  22737. }/* End test_wc_Ed448KeyToDer*/
  22738. /*
  22739. * Testing wc_Ed448PrivateKeyToDer
  22740. */
  22741. static int test_wc_Ed448PrivateKeyToDer (void)
  22742. {
  22743. int ret = 0;
  22744. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22745. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22746. byte output[ONEK_BUF];
  22747. ed448_key ed448PrivKey;
  22748. WC_RNG rng;
  22749. word32 inLen;
  22750. printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
  22751. ret = wc_InitRng(&rng);
  22752. if (ret == 0) {
  22753. ret = wc_ed448_init(&ed448PrivKey);
  22754. if (ret == 0) {
  22755. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  22756. }
  22757. inLen = sizeof(output);
  22758. /* Bad Cases */
  22759. if (ret == 0) {
  22760. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  22761. if (ret == BAD_FUNC_ARG) {
  22762. ret = 0;
  22763. }
  22764. }
  22765. if (ret == 0) {
  22766. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  22767. if (ret == BAD_FUNC_ARG) {
  22768. ret = 0;
  22769. }
  22770. }
  22771. if (ret == 0) {
  22772. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  22773. if (ret == BAD_FUNC_ARG) {
  22774. ret = 0;
  22775. }
  22776. }
  22777. if (ret == 0) {
  22778. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  22779. if (ret == BAD_FUNC_ARG) {
  22780. ret = 0;
  22781. }
  22782. }
  22783. /* Good case */
  22784. if (ret == 0) {
  22785. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  22786. if (ret > 0) {
  22787. ret = 0;
  22788. }
  22789. }
  22790. wc_ed448_free(&ed448PrivKey);
  22791. }
  22792. wc_FreeRng(&rng);
  22793. printf(resultFmt, ret == 0 ? passed : failed);
  22794. #endif
  22795. return ret;
  22796. }/* End test_wc_Ed448PrivateKeyToDer*/
  22797. /*
  22798. * Testing wc_SetSubjectBuffer
  22799. */
  22800. static int test_wc_SetSubjectBuffer (void)
  22801. {
  22802. int ret = 0;
  22803. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  22804. Cert cert;
  22805. FILE* file;
  22806. byte* der;
  22807. word32 derSz;
  22808. printf(testingFmt, "wc_SetSubjectBuffer()");
  22809. derSz = FOURK_BUF;
  22810. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22811. if (der == NULL) {
  22812. ret = -1;
  22813. }
  22814. if (ret == 0) {
  22815. file = XFOPEN("./certs/ca-cert.der", "rb");
  22816. if (file != NULL) {
  22817. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  22818. XFCLOSE(file);
  22819. }
  22820. else {
  22821. ret = -1;
  22822. }
  22823. }
  22824. if (ret == 0) {
  22825. ret = wc_InitCert(&cert);
  22826. }
  22827. if (ret == 0) {
  22828. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  22829. }
  22830. if (ret == 0) {
  22831. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  22832. if (ret == BAD_FUNC_ARG) {
  22833. ret = 0;
  22834. }
  22835. }
  22836. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22837. printf(resultFmt, ret == 0 ? passed : failed);
  22838. #endif
  22839. return ret;
  22840. }/* End test_wc_SetSubjectBuffer*/
  22841. /*
  22842. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  22843. */
  22844. static int test_wc_SetSubjectKeyIdFromPublicKey_ex (void)
  22845. {
  22846. int ret = 0;
  22847. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22848. WC_RNG rng;
  22849. Cert cert;
  22850. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22851. ed25519_key ed25519Key;
  22852. #endif
  22853. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22854. RsaKey rsaKey;
  22855. int bits = 2048;
  22856. #endif
  22857. #if defined(HAVE_ECC)
  22858. ecc_key eccKey;
  22859. #endif
  22860. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22861. ed448_key ed448Key;
  22862. #endif
  22863. printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
  22864. #ifndef HAVE_FIPS
  22865. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22866. #else
  22867. ret = wc_InitRng(&rng);
  22868. #endif
  22869. wc_InitCert(&cert);
  22870. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22871. if (ret == 0) { /*ED25519*/
  22872. ret = wc_ed25519_init(&ed25519Key);
  22873. if (ret == 0) {
  22874. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  22875. }
  22876. if (ret == 0) {
  22877. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22878. &ed25519Key);
  22879. }
  22880. wc_ed25519_free(&ed25519Key);
  22881. }
  22882. #endif
  22883. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22884. if (ret == 0) { /*RSA*/
  22885. ret = wc_InitRsaKey(&rsaKey, NULL);
  22886. if (ret == 0) {
  22887. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  22888. }
  22889. if (ret == 0) {
  22890. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  22891. }
  22892. wc_FreeRsaKey(&rsaKey);
  22893. }
  22894. #endif
  22895. #if defined(HAVE_ECC)
  22896. if (ret == 0) { /*ECC*/
  22897. ret = wc_ecc_init(&eccKey);
  22898. if (ret == 0) {
  22899. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22900. }
  22901. if (ret == 0) {
  22902. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  22903. }
  22904. wc_ecc_free(&eccKey);
  22905. }
  22906. #endif
  22907. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22908. if (ret == 0) { /*ED448*/
  22909. ret = wc_ed448_init(&ed448Key);
  22910. if (ret == 0) {
  22911. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  22912. }
  22913. if (ret == 0) {
  22914. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  22915. &ed448Key);
  22916. }
  22917. wc_ed448_free(&ed448Key);
  22918. }
  22919. #endif
  22920. printf(resultFmt, ret == 0 ? passed : failed);
  22921. wc_FreeRng(&rng);
  22922. #endif
  22923. return ret;
  22924. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  22925. /*
  22926. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  22927. */
  22928. static int test_wc_SetAuthKeyIdFromPublicKey_ex (void)
  22929. {
  22930. int ret = 0;
  22931. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22932. WC_RNG rng;
  22933. Cert cert;
  22934. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22935. ed25519_key ed25519Key;
  22936. #endif
  22937. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22938. RsaKey rsaKey;
  22939. int bits = 2048;
  22940. #endif
  22941. #if defined(HAVE_ECC)
  22942. ecc_key eccKey;
  22943. #endif
  22944. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22945. ed448_key ed448Key;
  22946. #endif
  22947. printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
  22948. #ifndef HAVE_FIPS
  22949. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22950. #else
  22951. ret = wc_InitRng(&rng);
  22952. #endif
  22953. wc_InitCert(&cert);
  22954. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22955. if (ret == 0) { /*ED25519*/
  22956. ret = wc_ed25519_init(&ed25519Key);
  22957. if (ret == 0) {
  22958. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  22959. }
  22960. if (ret == 0) {
  22961. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22962. &ed25519Key);
  22963. }
  22964. wc_ed25519_free(&ed25519Key);
  22965. }
  22966. #endif
  22967. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22968. if (ret == 0) { /*RSA*/
  22969. ret = wc_InitRsaKey(&rsaKey, NULL);
  22970. if (ret == 0) {
  22971. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  22972. }
  22973. if (ret == 0) {
  22974. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  22975. }
  22976. wc_FreeRsaKey(&rsaKey);
  22977. }
  22978. #endif
  22979. #if defined(HAVE_ECC)
  22980. if (ret == 0) { /*ECC*/
  22981. ret = wc_ecc_init(&eccKey);
  22982. if (ret == 0) {
  22983. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22984. }
  22985. if (ret == 0) {
  22986. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  22987. }
  22988. wc_ecc_free(&eccKey);
  22989. }
  22990. #endif
  22991. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22992. if (ret == 0) { /*ED448*/
  22993. ret = wc_ed448_init(&ed448Key);
  22994. if (ret == 0) {
  22995. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  22996. }
  22997. if (ret == 0) {
  22998. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  22999. &ed448Key);
  23000. }
  23001. wc_ed448_free(&ed448Key);
  23002. }
  23003. #endif
  23004. printf(resultFmt, ret == 0 ? passed : failed);
  23005. wc_FreeRng(&rng);
  23006. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23007. return ret;
  23008. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23009. /*
  23010. * Testing wc_PKCS7_New()
  23011. */
  23012. static void test_wc_PKCS7_New (void)
  23013. {
  23014. #if defined(HAVE_PKCS7)
  23015. PKCS7* pkcs7;
  23016. void* heap = NULL;
  23017. printf(testingFmt, "wc_PKCS7_New()");
  23018. pkcs7 = wc_PKCS7_New(heap, devId);
  23019. AssertNotNull(pkcs7);
  23020. printf(resultFmt, passed);
  23021. wc_PKCS7_Free(pkcs7);
  23022. #endif
  23023. } /* END test-wc_PKCS7_New */
  23024. /*
  23025. * Testing wc_PKCS7_Init()
  23026. */
  23027. static void test_wc_PKCS7_Init (void)
  23028. {
  23029. #if defined(HAVE_PKCS7)
  23030. PKCS7* pkcs7;
  23031. void* heap = NULL;
  23032. printf(testingFmt, "wc_PKCS7_Init()");
  23033. pkcs7 = wc_PKCS7_New(heap, devId);
  23034. AssertNotNull(pkcs7);
  23035. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  23036. /* Pass in bad args. */
  23037. AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
  23038. printf(resultFmt, passed);
  23039. wc_PKCS7_Free(pkcs7);
  23040. #endif
  23041. } /* END test-wc_PKCS7_Init */
  23042. /*
  23043. * Testing wc_PKCS7_InitWithCert()
  23044. */
  23045. static void test_wc_PKCS7_InitWithCert (void)
  23046. {
  23047. #if defined(HAVE_PKCS7)
  23048. PKCS7* pkcs7;
  23049. #ifndef NO_RSA
  23050. #if defined(USE_CERT_BUFFERS_2048)
  23051. unsigned char cert[sizeof(client_cert_der_2048)];
  23052. int certSz = (int)sizeof(cert);
  23053. XMEMSET(cert, 0, certSz);
  23054. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23055. #elif defined(USE_CERT_BUFFERS_1024)
  23056. unsigned char cert[sizeof(client_cert_der_1024)];
  23057. int certSz = (int)sizeof(cert);
  23058. XMEMSET(cert, 0, certSz);
  23059. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23060. #else
  23061. unsigned char cert[ONEK_BUF];
  23062. XFILE fp;
  23063. int certSz;
  23064. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23065. AssertTrue(fp != XBADFILE);
  23066. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23067. XFCLOSE(fp);
  23068. #endif
  23069. #elif defined(HAVE_ECC)
  23070. #if defined(USE_CERT_BUFFERS_256)
  23071. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23072. int certSz = (int)sizeof(cert);
  23073. XMEMSET(cert, 0, certSz);
  23074. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23075. #else
  23076. unsigned char cert[ONEK_BUF];
  23077. XFILE fp;
  23078. int certSz;
  23079. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23080. AssertTrue(fp != XBADFILE);
  23081. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  23082. XFCLOSE(fp);
  23083. #endif
  23084. #else
  23085. #error PKCS7 requires ECC or RSA
  23086. #endif
  23087. #ifdef HAVE_ECC
  23088. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23089. unsigned char certWithInvalidEccKey[] = {
  23090. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23091. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23092. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23093. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23094. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23095. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23096. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23097. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23098. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23099. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23100. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23101. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23102. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23103. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23104. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23105. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23106. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23107. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23108. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23109. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23110. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23111. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23112. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23113. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23114. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23115. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23116. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23117. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23118. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23119. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23120. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23121. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23122. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23123. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23124. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23125. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23126. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23127. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23128. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23129. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23130. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23131. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23132. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23133. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23134. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23135. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23136. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23137. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23138. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23139. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23140. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23141. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23142. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23143. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23144. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23145. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23146. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23147. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23148. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23149. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23150. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23151. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23152. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23153. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23154. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23155. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23156. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23157. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23158. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23159. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23160. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23161. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23162. 0x08, 0xA8, 0xB4};
  23163. #endif
  23164. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  23165. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23166. /* If initialization is not successful, it's free'd in init func. */
  23167. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  23168. wc_PKCS7_Free(pkcs7);
  23169. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23170. /* Valid initialization usage. */
  23171. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23172. /* Pass in bad args. No need free for null checks, free at end.*/
  23173. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23174. BAD_FUNC_ARG);
  23175. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23176. BAD_FUNC_ARG);
  23177. #ifdef HAVE_ECC
  23178. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23179. sizeof(certWithInvalidEccKey)), 0);
  23180. #endif
  23181. printf(resultFmt, passed);
  23182. wc_PKCS7_Free(pkcs7);
  23183. #endif
  23184. } /* END test_wc_PKCS7_InitWithCert */
  23185. /*
  23186. * Testing wc_PKCS7_EncodeData()
  23187. */
  23188. static void test_wc_PKCS7_EncodeData (void)
  23189. {
  23190. #if defined(HAVE_PKCS7)
  23191. PKCS7* pkcs7;
  23192. byte output[FOURK_BUF];
  23193. byte data[] = "My encoded DER cert.";
  23194. #ifndef NO_RSA
  23195. #if defined(USE_CERT_BUFFERS_2048)
  23196. unsigned char cert[sizeof(client_cert_der_2048)];
  23197. unsigned char key[sizeof(client_key_der_2048)];
  23198. int certSz = (int)sizeof(cert);
  23199. int keySz = (int)sizeof(key);
  23200. XMEMSET(cert, 0, certSz);
  23201. XMEMSET(key, 0, keySz);
  23202. XMEMCPY(cert, client_cert_der_2048, certSz);
  23203. XMEMCPY(key, client_key_der_2048, keySz);
  23204. #elif defined(USE_CERT_BUFFERS_1024)
  23205. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23206. unsigned char key[sizeof_client_key_der_1024];
  23207. int certSz = (int)sizeof(cert);
  23208. int keySz = (int)sizeof(key);
  23209. XMEMSET(cert, 0, certSz);
  23210. XMEMSET(key, 0, keySz);
  23211. XMEMCPY(cert, client_cert_der_1024, certSz);
  23212. XMEMCPY(key, client_key_der_1024, keySz);
  23213. #else
  23214. unsigned char cert[ONEK_BUF];
  23215. unsigned char key[ONEK_BUF];
  23216. XFILE fp;
  23217. int certSz;
  23218. int keySz;
  23219. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23220. AssertTrue(fp != XBADFILE);
  23221. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23222. XFCLOSE(fp);
  23223. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23224. AssertTrue(fp != XBADFILE);
  23225. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23226. XFCLOSE(fp);
  23227. #endif
  23228. #elif defined(HAVE_ECC)
  23229. #if defined(USE_CERT_BUFFERS_256)
  23230. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23231. unsigned char key[sizeof(ecc_clikey_der_256)];
  23232. int certSz = (int)sizeof(cert);
  23233. int keySz = (int)sizeof(key);
  23234. XMEMSET(cert, 0, certSz);
  23235. XMEMSET(key, 0, keySz);
  23236. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23237. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23238. #else
  23239. unsigned char cert[ONEK_BUF];
  23240. unsigned char key[ONEK_BUF];
  23241. XFILE fp;
  23242. int certSz, keySz;
  23243. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23244. AssertTrue(fp != XBADFILE);
  23245. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  23246. XFCLOSE(fp);
  23247. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23248. AssertTrue(fp != XBADFILE);
  23249. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  23250. XFCLOSE(fp);
  23251. #endif
  23252. #endif
  23253. XMEMSET(output, 0, sizeof(output));
  23254. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23255. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23256. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23257. printf(testingFmt, "wc_PKCS7_EncodeData()");
  23258. pkcs7->content = data;
  23259. pkcs7->contentSz = sizeof(data);
  23260. pkcs7->privateKey = key;
  23261. pkcs7->privateKeySz = keySz;
  23262. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23263. /* Test bad args. */
  23264. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23265. BAD_FUNC_ARG);
  23266. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23267. BAD_FUNC_ARG);
  23268. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23269. printf(resultFmt, passed);
  23270. wc_PKCS7_Free(pkcs7);
  23271. #endif
  23272. } /* END test_wc_PKCS7_EncodeData */
  23273. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23274. !defined(NO_RSA) && !defined(NO_SHA256)
  23275. /* RSA sign raw digest callback */
  23276. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23277. byte* out, word32 outSz, byte* privateKey,
  23278. word32 privateKeySz, int devid, int hashOID)
  23279. {
  23280. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23281. byte digInfoEncoding[] = {
  23282. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23283. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23284. 0x00, 0x04, 0x20
  23285. };
  23286. int ret;
  23287. byte digestInfo[ONEK_BUF];
  23288. byte sig[FOURK_BUF];
  23289. word32 digestInfoSz = 0;
  23290. word32 idx = 0;
  23291. RsaKey rsa;
  23292. /* SHA-256 required only for this example callback due to above
  23293. * digInfoEncoding[] */
  23294. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23295. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23296. (hashOID != SHA256h)) {
  23297. return -1;
  23298. }
  23299. /* build DigestInfo */
  23300. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23301. digestInfoSz += sizeof(digInfoEncoding);
  23302. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23303. digestInfoSz += digestSz;
  23304. /* set up RSA key */
  23305. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23306. if (ret != 0) {
  23307. return ret;
  23308. }
  23309. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23310. /* sign DigestInfo */
  23311. if (ret == 0) {
  23312. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23313. &rsa, pkcs7->rng);
  23314. if (ret > 0) {
  23315. if (ret > (int)outSz) {
  23316. /* output buffer too small */
  23317. ret = -1;
  23318. } else {
  23319. /* success, ret holds sig size */
  23320. XMEMCPY(out, sig, ret);
  23321. }
  23322. }
  23323. }
  23324. wc_FreeRsaKey(&rsa);
  23325. return ret;
  23326. }
  23327. #endif
  23328. /*
  23329. * Testing wc_PKCS7_EncodeSignedData()
  23330. */
  23331. static void test_wc_PKCS7_EncodeSignedData(void)
  23332. {
  23333. #if defined(HAVE_PKCS7)
  23334. PKCS7* pkcs7;
  23335. WC_RNG rng;
  23336. byte output[FOURK_BUF];
  23337. byte badOut[1];
  23338. word32 outputSz = (word32)sizeof(output);
  23339. word32 badOutSz = 0;
  23340. byte data[] = "Test data to encode.";
  23341. #ifndef NO_RSA
  23342. #if defined(USE_CERT_BUFFERS_2048)
  23343. byte key[sizeof(client_key_der_2048)];
  23344. byte cert[sizeof(client_cert_der_2048)];
  23345. word32 keySz = (word32)sizeof(key);
  23346. word32 certSz = (word32)sizeof(cert);
  23347. XMEMSET(key, 0, keySz);
  23348. XMEMSET(cert, 0, certSz);
  23349. XMEMCPY(key, client_key_der_2048, keySz);
  23350. XMEMCPY(cert, client_cert_der_2048, certSz);
  23351. #elif defined(USE_CERT_BUFFERS_1024)
  23352. byte key[sizeof_client_key_der_1024];
  23353. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23354. word32 keySz = (word32)sizeof(key);
  23355. word32 certSz = (word32)sizeof(cert);
  23356. XMEMSET(key, 0, keySz);
  23357. XMEMSET(cert, 0, certSz);
  23358. XMEMCPY(key, client_key_der_1024, keySz);
  23359. XMEMCPY(cert, client_cert_der_1024, certSz);
  23360. #else
  23361. unsigned char cert[ONEK_BUF];
  23362. unsigned char key[ONEK_BUF];
  23363. XFILE fp;
  23364. int certSz;
  23365. int keySz;
  23366. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23367. AssertTrue(fp != XBADFILE);
  23368. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23369. XFCLOSE(fp);
  23370. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23371. AssertTrue(fp != XBADFILE);
  23372. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23373. XFCLOSE(fp);
  23374. #endif
  23375. #elif defined(HAVE_ECC)
  23376. #if defined(USE_CERT_BUFFERS_256)
  23377. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23378. unsigned char key[sizeof(ecc_clikey_der_256)];
  23379. int certSz = (int)sizeof(cert);
  23380. int keySz = (int)sizeof(key);
  23381. XMEMSET(cert, 0, certSz);
  23382. XMEMSET(key, 0, keySz);
  23383. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23384. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23385. #else
  23386. unsigned char cert[ONEK_BUF];
  23387. unsigned char key[ONEK_BUF];
  23388. XFILE fp;
  23389. int certSz, keySz;
  23390. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  23391. AssertTrue(fp != XBADFILE);
  23392. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  23393. XFCLOSE(fp);
  23394. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23395. AssertTrue(fp != XBADFILE);
  23396. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  23397. XFCLOSE(fp);
  23398. #endif
  23399. #endif
  23400. XMEMSET(output, 0, outputSz);
  23401. AssertIntEQ(wc_InitRng(&rng), 0);
  23402. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23403. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23404. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23405. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  23406. pkcs7->content = data;
  23407. pkcs7->contentSz = (word32)sizeof(data);
  23408. pkcs7->privateKey = key;
  23409. pkcs7->privateKeySz = (word32)sizeof(key);
  23410. pkcs7->encryptOID = RSAk;
  23411. pkcs7->hashOID = SHAh;
  23412. pkcs7->rng = &rng;
  23413. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23414. wc_PKCS7_Free(pkcs7);
  23415. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23416. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23417. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23418. /* Pass in bad args. */
  23419. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  23420. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23421. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23422. badOutSz), BAD_FUNC_ARG);
  23423. pkcs7->hashOID = 0; /* bad hashOID */
  23424. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  23425. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23426. !defined(NO_RSA) && !defined(NO_SHA256)
  23427. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23428. * Example callback assumes SHA-256, so only run test if compiled in. */
  23429. wc_PKCS7_Free(pkcs7);
  23430. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23431. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23432. pkcs7->content = data;
  23433. pkcs7->contentSz = (word32)sizeof(data);
  23434. pkcs7->privateKey = key;
  23435. pkcs7->privateKeySz = (word32)sizeof(key);
  23436. pkcs7->encryptOID = RSAk;
  23437. pkcs7->hashOID = SHA256h;
  23438. pkcs7->rng = &rng;
  23439. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23440. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23441. #endif
  23442. printf(resultFmt, passed);
  23443. wc_PKCS7_Free(pkcs7);
  23444. wc_FreeRng(&rng);
  23445. #endif
  23446. } /* END test_wc_PKCS7_EncodeSignedData */
  23447. /*
  23448. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23449. */
  23450. static void test_wc_PKCS7_EncodeSignedData_ex(void)
  23451. {
  23452. #if defined(HAVE_PKCS7)
  23453. int ret, i;
  23454. PKCS7* pkcs7;
  23455. WC_RNG rng;
  23456. byte outputHead[FOURK_BUF/2];
  23457. byte outputFoot[FOURK_BUF/2];
  23458. word32 outputHeadSz = (word32)sizeof(outputHead);
  23459. word32 outputFootSz = (word32)sizeof(outputFoot);
  23460. byte data[FOURK_BUF];
  23461. wc_HashAlg hash;
  23462. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23463. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23464. word32 hashSz = wc_HashGetDigestSize(hashType);
  23465. #ifndef NO_RSA
  23466. #if defined(USE_CERT_BUFFERS_2048)
  23467. byte key[sizeof(client_key_der_2048)];
  23468. byte cert[sizeof(client_cert_der_2048)];
  23469. word32 keySz = (word32)sizeof(key);
  23470. word32 certSz = (word32)sizeof(cert);
  23471. XMEMSET(key, 0, keySz);
  23472. XMEMSET(cert, 0, certSz);
  23473. XMEMCPY(key, client_key_der_2048, keySz);
  23474. XMEMCPY(cert, client_cert_der_2048, certSz);
  23475. #elif defined(USE_CERT_BUFFERS_1024)
  23476. byte key[sizeof_client_key_der_1024];
  23477. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23478. word32 keySz = (word32)sizeof(key);
  23479. word32 certSz = (word32)sizeof(cert);
  23480. XMEMSET(key, 0, keySz);
  23481. XMEMSET(cert, 0, certSz);
  23482. XMEMCPY(key, client_key_der_1024, keySz);
  23483. XMEMCPY(cert, client_cert_der_1024, certSz);
  23484. #else
  23485. unsigned char cert[ONEK_BUF];
  23486. unsigned char key[ONEK_BUF];
  23487. XFILE fp;
  23488. int certSz;
  23489. int keySz;
  23490. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23491. AssertTrue((fp != XBADFILE));
  23492. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23493. XFCLOSE(fp);
  23494. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23495. AssertTrue(fp != XBADFILE);
  23496. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23497. XFCLOSE(fp);
  23498. #endif
  23499. #elif defined(HAVE_ECC)
  23500. #if defined(USE_CERT_BUFFERS_256)
  23501. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23502. unsigned char key[sizeof(ecc_clikey_der_256)];
  23503. int certSz = (int)sizeof(cert);
  23504. int keySz = (int)sizeof(key);
  23505. XMEMSET(cert, 0, certSz);
  23506. XMEMSET(key, 0, keySz);
  23507. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23508. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23509. #else
  23510. unsigned char cert[ONEK_BUF];
  23511. unsigned char key[ONEK_BUF];
  23512. XFILE fp;
  23513. int certSz, keySz;
  23514. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23515. AssertTrue(fp != XBADFILE);
  23516. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  23517. XFCLOSE(fp);
  23518. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23519. AssertTrue(fp != XBADFILE);
  23520. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  23521. XFCLOSE(fp);
  23522. #endif
  23523. #endif
  23524. /* initialize large data with sequence */
  23525. for (i=0; i<(int)sizeof(data); i++)
  23526. data[i] = i & 0xff;
  23527. XMEMSET(outputHead, 0, outputHeadSz);
  23528. XMEMSET(outputFoot, 0, outputFootSz);
  23529. AssertIntEQ(wc_InitRng(&rng), 0);
  23530. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23531. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23532. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23533. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  23534. pkcs7->content = NULL; /* not used for ex */
  23535. pkcs7->contentSz = (word32)sizeof(data);
  23536. pkcs7->privateKey = key;
  23537. pkcs7->privateKeySz = (word32)sizeof(key);
  23538. pkcs7->encryptOID = RSAk;
  23539. pkcs7->hashOID = SHAh;
  23540. pkcs7->rng = &rng;
  23541. /* calculate hash for content */
  23542. ret = wc_HashInit(&hash, hashType);
  23543. if (ret == 0) {
  23544. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  23545. if (ret == 0) {
  23546. ret = wc_HashFinal(&hash, hashType, hashBuf);
  23547. }
  23548. wc_HashFree(&hash, hashType);
  23549. }
  23550. AssertIntEQ(ret, 0);
  23551. /* Perform PKCS7 sign using hash directly */
  23552. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23553. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  23554. AssertIntGT(outputHeadSz, 0);
  23555. AssertIntGT(outputFootSz, 0);
  23556. wc_PKCS7_Free(pkcs7);
  23557. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23558. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23559. /* required parameter even on verify when using _ex */
  23560. pkcs7->contentSz = (word32)sizeof(data);
  23561. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23562. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  23563. wc_PKCS7_Free(pkcs7);
  23564. /* assembly complete PKCS7 sign and use normal verify */
  23565. {
  23566. byte* output = (byte*)XMALLOC(outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23567. word32 outputSz = 0;
  23568. AssertNotNull(output);
  23569. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  23570. outputSz += outputHeadSz;
  23571. XMEMCPY(&output[outputSz], data, sizeof(data));
  23572. outputSz += sizeof(data);
  23573. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  23574. outputSz += outputFootSz;
  23575. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23576. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23577. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23578. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23579. }
  23580. /* Pass in bad args. */
  23581. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23582. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23583. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23584. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23585. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23586. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23587. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23588. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23589. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23590. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23591. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23592. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  23593. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23594. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  23595. pkcs7->hashOID = 0; /* bad hashOID */
  23596. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23597. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23598. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23599. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23600. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23601. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23602. #ifndef NO_PKCS7_STREAM
  23603. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23604. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23605. #else
  23606. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23607. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  23608. #endif
  23609. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23610. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23611. #ifndef NO_PKCS7_STREAM
  23612. /* can pass in 0 buffer length with streaming API */
  23613. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23614. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23615. #else
  23616. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23617. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23618. #endif
  23619. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23620. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  23621. #ifndef NO_PKCS7_STREAM
  23622. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23623. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  23624. #else
  23625. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23626. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  23627. #endif
  23628. printf(resultFmt, passed);
  23629. wc_PKCS7_Free(pkcs7);
  23630. wc_FreeRng(&rng);
  23631. #endif
  23632. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  23633. #if defined(HAVE_PKCS7)
  23634. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  23635. byte* data, word32 dataSz,
  23636. int withAttribs, int detachedSig)
  23637. {
  23638. PKCS7* pkcs7;
  23639. WC_RNG rng;
  23640. static byte messageTypeOid[] =
  23641. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23642. 0x09, 0x02 };
  23643. static byte messageType[] = { 0x13, 2, '1', '9' };
  23644. PKCS7Attrib attribs[] =
  23645. {
  23646. { messageTypeOid, sizeof(messageTypeOid), messageType,
  23647. sizeof(messageType) }
  23648. };
  23649. #ifndef NO_RSA
  23650. #if defined(USE_CERT_BUFFERS_2048)
  23651. byte key[sizeof(client_key_der_2048)];
  23652. byte cert[sizeof(client_cert_der_2048)];
  23653. word32 keySz = (word32)sizeof(key);
  23654. word32 certSz = (word32)sizeof(cert);
  23655. XMEMSET(key, 0, keySz);
  23656. XMEMSET(cert, 0, certSz);
  23657. XMEMCPY(key, client_key_der_2048, keySz);
  23658. XMEMCPY(cert, client_cert_der_2048, certSz);
  23659. #elif defined(USE_CERT_BUFFERS_1024)
  23660. byte key[sizeof_client_key_der_1024];
  23661. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23662. word32 keySz = (word32)sizeof(key);
  23663. word32 certSz = (word32)sizeof(cert);
  23664. XMEMSET(key, 0, keySz);
  23665. XMEMSET(cert, 0, certSz);
  23666. XMEMCPY(key, client_key_der_1024, keySz);
  23667. XMEMCPY(cert, client_cert_der_1024, certSz);
  23668. #else
  23669. unsigned char cert[ONEK_BUF];
  23670. unsigned char key[ONEK_BUF];
  23671. FILE* fp;
  23672. int certSz;
  23673. int keySz;
  23674. fp = fopen("./certs/1024/client-cert.der", "rb");
  23675. AssertNotNull(fp);
  23676. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  23677. fclose(fp);
  23678. fp = fopen("./certs/1024/client-key.der", "rb");
  23679. AssertNotNull(fp);
  23680. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  23681. fclose(fp);
  23682. #endif
  23683. #elif defined(HAVE_ECC)
  23684. #if defined(USE_CERT_BUFFERS_256)
  23685. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23686. unsigned char key[sizeof(ecc_clikey_der_256)];
  23687. int certSz = (int)sizeof(cert);
  23688. int keySz = (int)sizeof(key);
  23689. XMEMSET(cert, 0, certSz);
  23690. XMEMSET(key, 0, keySz);
  23691. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23692. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23693. #else
  23694. unsigned char cert[ONEK_BUF];
  23695. unsigned char key[ONEK_BUF];
  23696. FILE* fp;
  23697. int certSz, keySz;
  23698. fp = fopen("./certs/client-ecc-cert.der", "rb");
  23699. AssertNotNull(fp);
  23700. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  23701. fclose(fp);
  23702. fp = fopen("./certs/client-ecc-key.der", "rb");
  23703. AssertNotNull(fp);
  23704. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  23705. fclose(fp);
  23706. #endif
  23707. #endif
  23708. XMEMSET(output, 0, outputSz);
  23709. AssertIntEQ(wc_InitRng(&rng), 0);
  23710. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23711. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23712. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23713. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  23714. pkcs7->content = data;
  23715. pkcs7->contentSz = dataSz;
  23716. pkcs7->privateKey = key;
  23717. pkcs7->privateKeySz = (word32)sizeof(key);
  23718. pkcs7->encryptOID = RSAk;
  23719. pkcs7->hashOID = SHAh;
  23720. pkcs7->rng = &rng;
  23721. if (withAttribs) {
  23722. /* include a signed attribute */
  23723. pkcs7->signedAttribs = attribs;
  23724. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  23725. }
  23726. if (detachedSig) {
  23727. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  23728. }
  23729. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23730. wc_PKCS7_Free(pkcs7);
  23731. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23732. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23733. if (detachedSig) {
  23734. pkcs7->content = data;
  23735. pkcs7->contentSz = dataSz;
  23736. }
  23737. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23738. wc_PKCS7_Free(pkcs7);
  23739. wc_FreeRng(&rng);
  23740. return outputSz;
  23741. }
  23742. #endif
  23743. /*
  23744. * Testing wc_PKCS_VerifySignedData()
  23745. */
  23746. static void test_wc_PKCS7_VerifySignedData(void)
  23747. {
  23748. #if defined(HAVE_PKCS7)
  23749. PKCS7* pkcs7;
  23750. byte output[FOURK_BUF];
  23751. word32 outputSz = sizeof(output);
  23752. byte data[] = "Test data to encode.";
  23753. byte badOut[1];
  23754. word32 badOutSz = 0;
  23755. byte badContent[] = "This is different content than was signed";
  23756. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23757. (word32)sizeof(data),
  23758. 0, 0)), 0);
  23759. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23760. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23761. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23762. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23763. /* Test bad args. */
  23764. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  23765. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23766. #ifndef NO_PKCS7_STREAM
  23767. /* can pass in 0 buffer length with streaming API */
  23768. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23769. badOutSz), WC_PKCS7_WANT_READ_E);
  23770. #else
  23771. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23772. badOutSz), BAD_FUNC_ARG);
  23773. #endif
  23774. wc_PKCS7_Free(pkcs7);
  23775. /* Invalid content should error, use detached signature so we can
  23776. * easily change content */
  23777. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23778. (word32)sizeof(data),
  23779. 1, 1)), 0);
  23780. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23781. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23782. pkcs7->content = badContent;
  23783. pkcs7->contentSz = sizeof(badContent);
  23784. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), SIG_VERIFY_E);
  23785. wc_PKCS7_Free(pkcs7);
  23786. /* Test success case with detached signature and valid content */
  23787. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23788. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23789. pkcs7->content = data;
  23790. pkcs7->contentSz = sizeof(data);
  23791. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23792. wc_PKCS7_Free(pkcs7);
  23793. printf(resultFmt, passed);
  23794. #endif
  23795. } /* END test_wc_PKCS7_VerifySignedData() */
  23796. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  23797. !defined(NO_AES_256)
  23798. static const byte defKey[] = {
  23799. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23800. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23801. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23802. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  23803. };
  23804. static byte aesHandle[32]; /* simulated hardware key handle */
  23805. /* return 0 on success */
  23806. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  23807. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  23808. byte* in, int inSz, byte* out, void* usrCtx)
  23809. {
  23810. int ret;
  23811. Aes aes;
  23812. if (usrCtx == NULL) {
  23813. /* no simulated handle passed in */
  23814. return -1;
  23815. }
  23816. switch (encryptOID) {
  23817. case AES256CBCb:
  23818. if (ivSz != AES_BLOCK_SIZE)
  23819. return BAD_FUNC_ARG;
  23820. break;
  23821. default:
  23822. WOLFSSL_MSG("Unsupported content cipher type for test");
  23823. return ALGO_ID_E;
  23824. };
  23825. /* simulate using handle to get key */
  23826. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  23827. if (ret == 0) {
  23828. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  23829. if (ret == 0)
  23830. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  23831. wc_AesFree(&aes);
  23832. }
  23833. (void)aad;
  23834. (void)aadSz;
  23835. (void)authTag;
  23836. (void)authTagSz;
  23837. (void)pkcs7;
  23838. return ret;
  23839. }
  23840. /* returns key size on success */
  23841. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  23842. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  23843. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  23844. {
  23845. int ret = -1;
  23846. if (out == NULL)
  23847. return BAD_FUNC_ARG;
  23848. if (keyId[0] != 0x00) {
  23849. return -1;
  23850. }
  23851. if (type != (int)PKCS7_KEKRI) {
  23852. return -1;
  23853. }
  23854. switch (keyWrapAlgo) {
  23855. case AES256_WRAP:
  23856. /* simulate setting a handle for later decryption but use key
  23857. * as handle in the test case here */
  23858. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  23859. aesHandle, sizeof(aesHandle), NULL);
  23860. if (ret < 0)
  23861. return ret;
  23862. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  23863. if (ret < 0)
  23864. return ret;
  23865. /* return key size on success */
  23866. return sizeof(defKey);
  23867. default:
  23868. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  23869. return BAD_KEYWRAP_ALG_E;
  23870. };
  23871. (void)cekSz;
  23872. (void)cek;
  23873. (void)outSz;
  23874. (void)keyIdSz;
  23875. (void)direction;
  23876. (void)orginKey; /* used with KAKRI */
  23877. (void)orginKeySz;
  23878. return ret;
  23879. }
  23880. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  23881. /*
  23882. * Testing wc_PKCS7_EncodeEnvelopedData()
  23883. */
  23884. static void test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  23885. {
  23886. #if defined(HAVE_PKCS7)
  23887. PKCS7* pkcs7;
  23888. #ifdef ECC_TIMING_RESISTANT
  23889. WC_RNG rng;
  23890. #endif
  23891. word32 tempWrd32 = 0;
  23892. byte* tmpBytePtr = NULL;
  23893. const char input[] = "Test data to encode.";
  23894. int i;
  23895. int testSz = 0;
  23896. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  23897. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  23898. byte* rsaCert = NULL;
  23899. byte* rsaPrivKey = NULL;
  23900. word32 rsaCertSz;
  23901. word32 rsaPrivKeySz;
  23902. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  23903. !defined(USE_CERT_BUFFERS_2048) )
  23904. static const char* rsaClientCert = "./certs/client-cert.der";
  23905. static const char* rsaClientKey = "./certs/client-key.der";
  23906. rsaCertSz = (word32)sizeof(rsaClientCert);
  23907. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  23908. #endif
  23909. #endif
  23910. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  23911. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  23912. byte* eccCert = NULL;
  23913. byte* eccPrivKey = NULL;
  23914. word32 eccCertSz;
  23915. word32 eccPrivKeySz;
  23916. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  23917. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  23918. static const char* eccClientKey = "./certs/ecc-client-key.der";
  23919. #endif
  23920. #endif
  23921. /* Generic buffer size. */
  23922. byte output[ONEK_BUF];
  23923. byte decoded[sizeof(input)/sizeof(char)];
  23924. int decodedSz = 0;
  23925. #ifndef NO_FILESYSTEM
  23926. XFILE certFile;
  23927. XFILE keyFile;
  23928. #endif
  23929. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  23930. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  23931. /* RSA certs and keys. */
  23932. #if defined(USE_CERT_BUFFERS_1024)
  23933. /* Allocate buffer space. */
  23934. AssertNotNull(rsaCert =
  23935. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  23936. /* Init buffer. */
  23937. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  23938. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  23939. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  23940. DYNAMIC_TYPE_TMP_BUFFER));
  23941. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  23942. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  23943. #elif defined(USE_CERT_BUFFERS_2048)
  23944. /* Allocate buffer */
  23945. AssertNotNull(rsaCert =
  23946. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  23947. /* Init buffer. */
  23948. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  23949. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  23950. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  23951. DYNAMIC_TYPE_TMP_BUFFER));
  23952. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  23953. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  23954. #else
  23955. /* File system. */
  23956. certFile = XFOPEN(rsaClientCert, "rb");
  23957. AssertTrue(certFile != XBADFILE);
  23958. rsaCertSz = (word32)FOURK_BUF;
  23959. AssertNotNull(rsaCert =
  23960. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  23961. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  23962. XFCLOSE(certFile);
  23963. keyFile = XFOPEN(rsaClientKey, "rb");
  23964. AssertTrue(keyFile != XBADFILE);
  23965. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23966. DYNAMIC_TYPE_TMP_BUFFER));
  23967. rsaPrivKeySz = (word32)FOURK_BUF;
  23968. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  23969. XFCLOSE(keyFile);
  23970. #endif /* USE_CERT_BUFFERS */
  23971. #endif /* NO_RSA */
  23972. /* ECC */
  23973. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  23974. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  23975. #ifdef USE_CERT_BUFFERS_256
  23976. AssertNotNull(eccCert =
  23977. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  23978. /* Init buffer. */
  23979. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  23980. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  23981. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  23982. DYNAMIC_TYPE_TMP_BUFFER));
  23983. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  23984. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  23985. #else /* File system. */
  23986. certFile = XFOPEN(eccClientCert, "rb");
  23987. AssertTrue(certFile != XBADFILE);
  23988. eccCertSz = (word32)FOURK_BUF;
  23989. AssertNotNull(eccCert =
  23990. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  23991. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  23992. XFCLOSE(certFile);
  23993. keyFile = XFOPEN(eccClientKey, "rb");
  23994. AssertTrue(keyFile != XBADFILE);
  23995. eccPrivKeySz = (word32)FOURK_BUF;
  23996. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23997. DYNAMIC_TYPE_TMP_BUFFER));
  23998. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  23999. XFCLOSE(keyFile);
  24000. #endif /* USE_CERT_BUFFERS_256 */
  24001. #endif /* END HAVE_ECC */
  24002. /* Silence. */
  24003. (void)keyFile;
  24004. (void)certFile;
  24005. const pkcs7EnvelopedVector testVectors[] = {
  24006. /* DATA is a global variable defined in the makefile. */
  24007. #if !defined(NO_RSA)
  24008. #ifndef NO_DES3
  24009. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  24010. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24011. #endif /* NO_DES3 */
  24012. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24013. #ifndef NO_AES_128
  24014. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24015. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24016. #endif
  24017. #ifndef NO_AES_192
  24018. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  24019. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24020. #endif
  24021. #ifndef NO_AES_256
  24022. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24023. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24024. #endif
  24025. #endif /* NO_AES && HAVE_AES_CBC */
  24026. #endif /* NO_RSA */
  24027. #if defined(HAVE_ECC)
  24028. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24029. #if !defined(NO_SHA) && !defined(NO_AES_128)
  24030. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24031. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  24032. eccCertSz, eccPrivKey, eccPrivKeySz},
  24033. #endif
  24034. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  24035. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24036. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  24037. eccCertSz, eccPrivKey, eccPrivKeySz},
  24038. #endif
  24039. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  24040. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24041. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  24042. eccCertSz, eccPrivKey, eccPrivKeySz},
  24043. #endif
  24044. #endif /* NO_AES && HAVE_AES_CBC*/
  24045. #endif /* END HAVE_ECC */
  24046. }; /* END pkcs7EnvelopedVector */
  24047. #ifdef ECC_TIMING_RESISTANT
  24048. AssertIntEQ(wc_InitRng(&rng), 0);
  24049. #endif
  24050. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  24051. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24052. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24053. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  24054. for (i = 0; i < testSz; i++) {
  24055. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24056. (word32)(testVectors + i)->certSz), 0);
  24057. #ifdef ECC_TIMING_RESISTANT
  24058. pkcs7->rng = &rng;
  24059. #endif
  24060. pkcs7->content = (byte*)(testVectors + i)->content;
  24061. pkcs7->contentSz = (testVectors + i)->contentSz;
  24062. pkcs7->contentOID = (testVectors + i)->contentOID;
  24063. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24064. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24065. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24066. pkcs7->privateKey = (testVectors + i)->privateKey;
  24067. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24068. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24069. (word32)sizeof(output)), 0);
  24070. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24071. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  24072. AssertIntGE(decodedSz, 0);
  24073. /* Verify the size of each buffer. */
  24074. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24075. /* Don't free the last time through the loop. */
  24076. if (i < testSz - 1 ){
  24077. wc_PKCS7_Free(pkcs7);
  24078. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24079. }
  24080. } /* END test loop. */
  24081. /* Test bad args. */
  24082. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  24083. (word32)sizeof(output)), BAD_FUNC_ARG);
  24084. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  24085. (word32)sizeof(output)), BAD_FUNC_ARG);
  24086. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  24087. printf(resultFmt, passed);
  24088. /* Decode. */
  24089. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  24090. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  24091. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24092. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24093. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24094. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24095. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  24096. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  24097. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24098. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  24099. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24100. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  24101. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  24102. /* only a failure for KARI test cases */
  24103. tempWrd32 = pkcs7->singleCertSz;
  24104. pkcs7->singleCertSz = 0;
  24105. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24106. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24107. pkcs7->singleCertSz = tempWrd32;
  24108. tmpBytePtr = pkcs7->singleCert;
  24109. pkcs7->singleCert = NULL;
  24110. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24111. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24112. pkcs7->singleCert = tmpBytePtr;
  24113. #endif
  24114. tempWrd32 = pkcs7->privateKeySz;
  24115. pkcs7->privateKeySz = 0;
  24116. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24117. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24118. pkcs7->privateKeySz = tempWrd32;
  24119. tmpBytePtr = pkcs7->privateKey;
  24120. pkcs7->privateKey = NULL;
  24121. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24122. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24123. pkcs7->privateKey = tmpBytePtr;
  24124. wc_PKCS7_Free(pkcs7);
  24125. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  24126. /* test of decrypt callback with KEKRI enveloped data */
  24127. {
  24128. int envelopedSz;
  24129. const byte keyId[] = { 0x00 };
  24130. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24131. pkcs7->content = (byte*)input;
  24132. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  24133. pkcs7->contentOID = DATA;
  24134. pkcs7->encryptOID = AES256CBCb;
  24135. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  24136. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  24137. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  24138. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  24139. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24140. (word32)sizeof(output))), 0);
  24141. wc_PKCS7_Free(pkcs7);
  24142. /* decode envelopedData */
  24143. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24144. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  24145. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  24146. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24147. envelopedSz, decoded, sizeof(decoded))), 0);
  24148. wc_PKCS7_Free(pkcs7);
  24149. }
  24150. #endif /* !NO_AES && !NO_AES_256 */
  24151. printf(resultFmt, passed);
  24152. #ifndef NO_RSA
  24153. if (rsaCert) {
  24154. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24155. }
  24156. if (rsaPrivKey) {
  24157. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24158. }
  24159. #endif /*NO_RSA */
  24160. #ifdef HAVE_ECC
  24161. if (eccCert) {
  24162. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24163. }
  24164. if (eccPrivKey) {
  24165. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24166. }
  24167. #endif /* HAVE_ECC */
  24168. #ifdef ECC_TIMING_RESISTANT
  24169. wc_FreeRng(&rng);
  24170. #endif
  24171. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3)
  24172. {
  24173. byte out[7];
  24174. byte *cms;
  24175. word32 cmsSz;
  24176. XFILE cmsFile;
  24177. XMEMSET(out, 0, sizeof(out));
  24178. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24179. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  24180. AssertTrue(cmsFile != XBADFILE);
  24181. cmsSz = (word32)FOURK_BUF;
  24182. AssertNotNull(cms =
  24183. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24184. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  24185. XFCLOSE(cmsFile);
  24186. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  24187. sizeof_client_cert_der_2048), 0);
  24188. pkcs7->privateKey = (byte*)client_key_der_2048;
  24189. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  24190. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24191. sizeof(out)), 0);
  24192. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24193. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  24194. wc_PKCS7_Free(pkcs7);
  24195. }
  24196. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 */
  24197. #endif /* HAVE_PKCS7 */
  24198. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  24199. /*
  24200. * Testing wc_PKCS7_EncodeEncryptedData()
  24201. */
  24202. static void test_wc_PKCS7_EncodeEncryptedData (void)
  24203. {
  24204. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  24205. PKCS7* pkcs7 = NULL;
  24206. byte* tmpBytePtr = NULL;
  24207. byte encrypted[TWOK_BUF];
  24208. byte decoded[TWOK_BUF];
  24209. word32 tmpWrd32 = 0;
  24210. int tmpInt = 0;
  24211. int decodedSz;
  24212. int encryptedSz;
  24213. int testSz;
  24214. int i;
  24215. const byte data[] = { /* Hello World */
  24216. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24217. 0x72,0x6c,0x64
  24218. };
  24219. #ifndef NO_DES3
  24220. byte desKey[] = {
  24221. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  24222. };
  24223. byte des3Key[] = {
  24224. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  24225. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  24226. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  24227. };
  24228. #endif
  24229. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24230. #ifndef NO_AES_128
  24231. byte aes128Key[] = {
  24232. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24233. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24234. };
  24235. #endif
  24236. #ifndef NO_AES_192
  24237. byte aes192Key[] = {
  24238. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24239. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24240. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24241. };
  24242. #endif
  24243. #ifndef NO_AES_256
  24244. byte aes256Key[] = {
  24245. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24246. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24247. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24248. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24249. };
  24250. #endif
  24251. #endif /* !NO_AES && HAVE_AES_CBC */
  24252. const pkcs7EncryptedVector testVectors[] =
  24253. {
  24254. #ifndef NO_DES3
  24255. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  24256. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  24257. #endif /* !NO_DES3 */
  24258. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24259. #ifndef NO_AES_128
  24260. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  24261. sizeof(aes128Key)},
  24262. #endif
  24263. #ifndef NO_AES_192
  24264. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  24265. sizeof(aes192Key)},
  24266. #endif
  24267. #ifndef NO_AES_256
  24268. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  24269. sizeof(aes256Key)},
  24270. #endif
  24271. #endif /* !NO_AES && HAVE_AES_CBC */
  24272. };
  24273. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  24274. for (i = 0; i < testSz; i++) {
  24275. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24276. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24277. pkcs7->content = (byte*)testVectors[i].content;
  24278. pkcs7->contentSz = testVectors[i].contentSz;
  24279. pkcs7->contentOID = testVectors[i].contentOID;
  24280. pkcs7->encryptOID = testVectors[i].encryptOID;
  24281. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  24282. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  24283. pkcs7->heap = HEAP_HINT;
  24284. /* encode encryptedData */
  24285. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24286. sizeof(encrypted));
  24287. AssertIntGT(encryptedSz, 0);
  24288. /* Decode encryptedData */
  24289. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24290. decoded, sizeof(decoded));
  24291. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  24292. /* Keep values for last itr. */
  24293. if (i < testSz - 1) {
  24294. wc_PKCS7_Free(pkcs7);
  24295. }
  24296. }
  24297. if (pkcs7 == NULL || testSz == 0) {
  24298. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24299. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24300. }
  24301. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  24302. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  24303. sizeof(encrypted)),BAD_FUNC_ARG);
  24304. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  24305. sizeof(encrypted)), BAD_FUNC_ARG);
  24306. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24307. 0), BAD_FUNC_ARG);
  24308. /* Testing the struct. */
  24309. tmpBytePtr = pkcs7->content;
  24310. pkcs7->content = NULL;
  24311. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24312. sizeof(encrypted)), BAD_FUNC_ARG);
  24313. pkcs7->content = tmpBytePtr;
  24314. tmpWrd32 = pkcs7->contentSz;
  24315. pkcs7->contentSz = 0;
  24316. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24317. sizeof(encrypted)), BAD_FUNC_ARG);
  24318. pkcs7->contentSz = tmpWrd32;
  24319. tmpInt = pkcs7->encryptOID;
  24320. pkcs7->encryptOID = 0;
  24321. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24322. sizeof(encrypted)), BAD_FUNC_ARG);
  24323. pkcs7->encryptOID = tmpInt;
  24324. tmpBytePtr = pkcs7->encryptionKey;
  24325. pkcs7->encryptionKey = NULL;
  24326. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24327. sizeof(encrypted)), BAD_FUNC_ARG);
  24328. pkcs7->encryptionKey = tmpBytePtr;
  24329. tmpWrd32 = pkcs7->encryptionKeySz;
  24330. pkcs7->encryptionKeySz = 0;
  24331. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24332. sizeof(encrypted)), BAD_FUNC_ARG);
  24333. pkcs7->encryptionKeySz = tmpWrd32;
  24334. printf(resultFmt, passed);
  24335. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  24336. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  24337. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24338. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  24339. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24340. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  24341. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24342. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24343. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  24344. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24345. decoded, 0), BAD_FUNC_ARG);
  24346. /* Test struct fields */
  24347. tmpBytePtr = pkcs7->encryptionKey;
  24348. pkcs7->encryptionKey = NULL;
  24349. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24350. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24351. pkcs7->encryptionKey = tmpBytePtr;
  24352. pkcs7->encryptionKeySz = 0;
  24353. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24354. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24355. printf(resultFmt, passed);
  24356. wc_PKCS7_Free(pkcs7);
  24357. #endif
  24358. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  24359. /*
  24360. * Testing wc_PKCS7_Degenerate()
  24361. */
  24362. static void test_wc_PKCS7_Degenerate(void)
  24363. {
  24364. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24365. PKCS7* pkcs7;
  24366. char fName[] = "./certs/test-degenerate.p7b";
  24367. XFILE f;
  24368. byte der[4096];
  24369. word32 derSz;
  24370. int ret;
  24371. printf(testingFmt, "wc_PKCS7_Degenerate()");
  24372. AssertNotNull(f = XFOPEN(fName, "rb"));
  24373. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24374. derSz = (word32)ret;
  24375. XFCLOSE(f);
  24376. /* test degenerate success */
  24377. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24378. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24379. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24380. #ifndef NO_RSA
  24381. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24382. #else
  24383. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24384. #endif
  24385. wc_PKCS7_Free(pkcs7);
  24386. /* test with turning off degenerate cases */
  24387. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24388. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24389. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24390. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  24391. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  24392. wc_PKCS7_Free(pkcs7);
  24393. printf(resultFmt, passed);
  24394. #endif
  24395. } /* END test_wc_PKCS7_Degenerate() */
  24396. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24397. defined(ASN_BER_TO_DER) && !defined(NO_DES3)
  24398. static byte berContent[] = {
  24399. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24400. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  24401. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  24402. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  24403. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  24404. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  24405. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  24406. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  24407. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  24408. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  24409. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  24410. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  24411. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  24412. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  24413. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  24414. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  24415. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  24416. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  24417. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24418. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24419. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  24420. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24421. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24422. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  24423. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  24424. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  24425. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24426. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  24427. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  24428. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  24429. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  24430. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  24431. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  24432. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  24433. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  24434. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  24435. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  24436. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  24437. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  24438. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  24439. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  24440. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  24441. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  24442. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  24443. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24444. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  24445. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  24446. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  24447. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  24448. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  24449. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  24450. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  24451. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  24452. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  24453. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  24454. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  24455. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  24456. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  24457. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  24458. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  24459. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  24460. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  24461. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  24462. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  24463. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  24464. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  24465. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  24466. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  24467. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  24468. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  24469. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  24470. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  24471. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  24472. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  24473. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  24474. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  24475. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  24476. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  24477. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  24478. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  24479. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  24480. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  24481. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  24482. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  24483. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  24484. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  24485. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  24486. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  24487. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  24488. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  24489. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  24490. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  24491. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  24492. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  24493. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  24494. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  24495. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  24496. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  24497. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  24498. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  24499. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  24500. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  24501. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  24502. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  24503. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  24504. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  24505. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  24506. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  24507. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  24508. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  24509. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  24510. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  24511. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  24512. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  24513. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  24514. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  24515. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  24516. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  24517. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  24518. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  24519. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  24520. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  24521. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  24522. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  24523. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  24524. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  24525. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  24526. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  24527. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  24528. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  24529. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  24530. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  24531. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  24532. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  24533. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  24534. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  24535. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  24536. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  24537. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  24538. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  24539. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  24540. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  24541. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  24542. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  24543. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  24544. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  24545. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  24546. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  24547. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  24548. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  24549. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  24550. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  24551. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  24552. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  24553. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  24554. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  24555. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  24556. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  24557. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  24558. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  24559. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  24560. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  24561. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  24562. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  24563. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  24564. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  24565. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  24566. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  24567. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  24568. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  24569. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  24570. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  24571. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  24572. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  24573. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  24574. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  24575. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  24576. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  24577. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  24578. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  24579. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  24580. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  24581. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  24582. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  24583. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  24584. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  24585. 0x00, 0x00, 0x00, 0x00, 0x00
  24586. };
  24587. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
  24588. /*
  24589. * Testing wc_PKCS7_BER()
  24590. */
  24591. static void test_wc_PKCS7_BER(void)
  24592. {
  24593. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24594. defined(ASN_BER_TO_DER)
  24595. PKCS7* pkcs7;
  24596. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  24597. XFILE f;
  24598. byte der[4096];
  24599. #ifndef NO_DES3
  24600. byte decoded[2048];
  24601. #endif
  24602. word32 derSz;
  24603. int ret;
  24604. printf(testingFmt, "wc_PKCS7_BER()");
  24605. AssertNotNull(f = XFOPEN(fName, "rb"));
  24606. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24607. derSz = (word32)ret;
  24608. XFCLOSE(f);
  24609. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24610. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24611. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24612. #ifndef NO_RSA
  24613. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24614. #else
  24615. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24616. #endif
  24617. wc_PKCS7_Free(pkcs7);
  24618. #ifndef NO_DES3
  24619. /* decode BER content */
  24620. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  24621. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24622. derSz = (word32)ret;
  24623. XFCLOSE(f);
  24624. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24625. #ifndef NO_RSA
  24626. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24627. #else
  24628. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24629. #endif
  24630. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  24631. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24632. derSz = (word32)ret;
  24633. XFCLOSE(f);
  24634. pkcs7->privateKey = der;
  24635. pkcs7->privateKeySz = derSz;
  24636. #ifndef NO_RSA
  24637. #ifdef WOLFSSL_SP_MATH
  24638. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24639. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  24640. #else
  24641. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24642. sizeof(berContent), decoded, sizeof(decoded)), 0);
  24643. #endif
  24644. #else
  24645. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24646. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  24647. #endif
  24648. wc_PKCS7_Free(pkcs7);
  24649. #endif /* !NO_DES3 */
  24650. printf(resultFmt, passed);
  24651. #endif
  24652. } /* END test_wc_PKCS7_BER() */
  24653. static void test_PKCS7_signed_enveloped(void)
  24654. {
  24655. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  24656. !defined(NO_FILESYSTEM)
  24657. XFILE f;
  24658. PKCS7* pkcs7;
  24659. #ifdef HAVE_AES_CBC
  24660. PKCS7* inner;
  24661. #endif
  24662. void* pt;
  24663. WC_RNG rng;
  24664. unsigned char key[FOURK_BUF/2];
  24665. unsigned char cert[FOURK_BUF/2];
  24666. unsigned char env[FOURK_BUF];
  24667. int envSz = FOURK_BUF;
  24668. int keySz;
  24669. int certSz;
  24670. unsigned char sig[FOURK_BUF * 2];
  24671. int sigSz = FOURK_BUF * 2;
  24672. #ifdef HAVE_AES_CBC
  24673. unsigned char decoded[FOURK_BUF];
  24674. int decodedSz = FOURK_BUF;
  24675. #endif
  24676. printf(testingFmt, "PKCS7_signed_enveloped");
  24677. /* load cert */
  24678. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  24679. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  24680. XFCLOSE(f);
  24681. /* load key */
  24682. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  24683. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  24684. XFCLOSE(f);
  24685. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  24686. /* sign cert for envelope */
  24687. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24688. AssertIntEQ(wc_InitRng(&rng), 0);
  24689. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24690. pkcs7->content = cert;
  24691. pkcs7->contentSz = certSz;
  24692. pkcs7->contentOID = DATA;
  24693. pkcs7->privateKey = key;
  24694. pkcs7->privateKeySz = keySz;
  24695. pkcs7->encryptOID = RSAk;
  24696. pkcs7->hashOID = SHA256h;
  24697. pkcs7->rng = &rng;
  24698. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  24699. wc_PKCS7_Free(pkcs7);
  24700. wc_FreeRng(&rng);
  24701. #ifdef HAVE_AES_CBC
  24702. /* create envelope */
  24703. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24704. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24705. pkcs7->content = sig;
  24706. pkcs7->contentSz = sigSz;
  24707. pkcs7->contentOID = DATA;
  24708. pkcs7->encryptOID = AES256CBCb;
  24709. pkcs7->privateKey = key;
  24710. pkcs7->privateKeySz = keySz;
  24711. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  24712. wc_PKCS7_Free(pkcs7);
  24713. #endif
  24714. /* create bad signed enveloped data */
  24715. sigSz = FOURK_BUF * 2;
  24716. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24717. AssertIntEQ(wc_InitRng(&rng), 0);
  24718. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24719. pkcs7->content = env;
  24720. pkcs7->contentSz = envSz;
  24721. pkcs7->contentOID = DATA;
  24722. pkcs7->privateKey = key;
  24723. pkcs7->privateKeySz = keySz;
  24724. pkcs7->encryptOID = RSAk;
  24725. pkcs7->hashOID = SHA256h;
  24726. pkcs7->rng = &rng;
  24727. /* Set no certs in bundle for this test. Hang on to the pointer though to
  24728. * free it later. */
  24729. pt = (void*)pkcs7->certList;
  24730. pkcs7->certList = NULL; /* no certs in bundle */
  24731. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  24732. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  24733. wc_PKCS7_Free(pkcs7);
  24734. /* check verify fails */
  24735. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24736. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24737. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  24738. PKCS7_SIGNEEDS_CHECK);
  24739. /* try verifying the signature manually */
  24740. {
  24741. RsaKey rKey;
  24742. word32 idx = 0;
  24743. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  24744. WC_MAX_DIGEST_SIZE];
  24745. int digestSz;
  24746. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  24747. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  24748. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  24749. digest, sizeof(digest), &rKey);
  24750. AssertIntGT(digestSz, 0);
  24751. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  24752. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  24753. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  24754. /* verify was success */
  24755. }
  24756. wc_PKCS7_Free(pkcs7);
  24757. /* initializing the PKCS7 struct with the signing certificate should pass */
  24758. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24759. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24760. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  24761. wc_PKCS7_Free(pkcs7);
  24762. /* create valid degenerate bundle */
  24763. sigSz = FOURK_BUF * 2;
  24764. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24765. pkcs7->content = env;
  24766. pkcs7->contentSz = envSz;
  24767. pkcs7->contentOID = DATA;
  24768. pkcs7->privateKey = key;
  24769. pkcs7->privateKeySz = keySz;
  24770. pkcs7->encryptOID = RSAk;
  24771. pkcs7->hashOID = SHA256h;
  24772. pkcs7->rng = &rng;
  24773. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  24774. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  24775. wc_PKCS7_Free(pkcs7);
  24776. wc_FreeRng(&rng);
  24777. /* check verify */
  24778. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24779. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24780. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  24781. AssertNotNull(pkcs7->content);
  24782. #ifdef HAVE_AES_CBC
  24783. /* check decode */
  24784. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  24785. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  24786. inner->privateKey = key;
  24787. inner->privateKeySz = keySz;
  24788. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  24789. pkcs7->contentSz, decoded, decodedSz)), 0);
  24790. wc_PKCS7_Free(inner);
  24791. #endif
  24792. wc_PKCS7_Free(pkcs7);
  24793. #ifdef HAVE_AES_CBC
  24794. /* check cert set */
  24795. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24796. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24797. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  24798. AssertNotNull(pkcs7->singleCert);
  24799. AssertIntNE(pkcs7->singleCertSz, 0);
  24800. wc_PKCS7_Free(pkcs7);
  24801. #endif
  24802. printf(resultFmt, passed);
  24803. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  24804. }
  24805. static void test_wc_PKCS7_NoDefaultSignedAttribs (void)
  24806. {
  24807. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  24808. && !defined(NO_AES)
  24809. PKCS7* pkcs7;
  24810. void* heap = NULL;
  24811. printf(testingFmt, "wc_PKCS7_NoDefaultSignedAttribs()");
  24812. pkcs7 = wc_PKCS7_New(heap, devId);
  24813. AssertNotNull(pkcs7);
  24814. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  24815. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  24816. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  24817. wc_PKCS7_Free(pkcs7);
  24818. printf(resultFmt, passed);
  24819. #endif
  24820. }
  24821. static void test_wc_PKCS7_SetOriEncryptCtx (void)
  24822. {
  24823. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  24824. && !defined(NO_AES)
  24825. PKCS7* pkcs7;
  24826. void* heap = NULL;
  24827. WOLFSSL_CTX* ctx;
  24828. ctx = NULL;
  24829. printf(testingFmt, "wc_PKCS7_SetOriEncryptCtx()");
  24830. pkcs7 = wc_PKCS7_New(heap, devId);
  24831. AssertNotNull(pkcs7);
  24832. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  24833. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  24834. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  24835. wc_PKCS7_Free(pkcs7);
  24836. printf(resultFmt, passed);
  24837. #endif
  24838. }
  24839. static void test_wc_PKCS7_SetOriDecryptCtx (void)
  24840. {
  24841. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  24842. && !defined(NO_AES)
  24843. PKCS7* pkcs7;
  24844. void* heap = NULL;
  24845. WOLFSSL_CTX* ctx;
  24846. ctx = NULL;
  24847. printf(testingFmt, "wc_PKCS7_SetOriDecryptCtx()");
  24848. pkcs7 = wc_PKCS7_New(heap, devId);
  24849. AssertNotNull(pkcs7);
  24850. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  24851. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  24852. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  24853. wc_PKCS7_Free(pkcs7);
  24854. printf(resultFmt, passed);
  24855. #endif
  24856. }
  24857. static void test_wc_PKCS7_DecodeCompressedData(void)
  24858. {
  24859. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  24860. && !defined(NO_AES) && defined(HAVE_LIBZ)
  24861. PKCS7* pkcs7;
  24862. void* heap = NULL;
  24863. byte out[4096];
  24864. byte *decompressed;
  24865. int outSz, decompressedSz;
  24866. const char* cert = "./certs/client-cert.pem";
  24867. byte* cert_buf = NULL;
  24868. size_t cert_sz = 0;
  24869. printf(testingFmt, "wc_PKCS7_DecodeCompressedData()");
  24870. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  24871. AssertNotNull((decompressed =
  24872. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  24873. decompressedSz = (int)cert_sz;
  24874. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
  24875. pkcs7->content = (byte*)cert_buf;
  24876. pkcs7->contentSz = (word32)cert_sz;
  24877. pkcs7->contentOID = DATA;
  24878. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  24879. sizeof(out))), 0);
  24880. wc_PKCS7_Free(pkcs7);
  24881. /* compressed key should be smaller than when started */
  24882. AssertIntLT(outSz, cert_sz);
  24883. /* test decompression */
  24884. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
  24885. AssertIntEQ(pkcs7->contentOID, 0);
  24886. /* fail case with out buffer too small */
  24887. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  24888. decompressed, outSz), 0);
  24889. /* success case */
  24890. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  24891. decompressed, decompressedSz), cert_sz);
  24892. AssertIntEQ(pkcs7->contentOID, DATA);
  24893. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  24894. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  24895. decompressed = NULL;
  24896. /* test decompression function with different 'max' inputs */
  24897. outSz = sizeof(out);
  24898. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  24899. 0);
  24900. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  24901. out, outSz, 0, heap), 0);
  24902. AssertNull(decompressed);
  24903. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  24904. out, outSz, 0, heap), 0);
  24905. AssertNotNull(decompressed);
  24906. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  24907. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  24908. decompressed = NULL;
  24909. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  24910. out, outSz, 0, heap), 0);
  24911. AssertNotNull(decompressed);
  24912. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  24913. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  24914. if (cert_buf)
  24915. free(cert_buf);
  24916. wc_PKCS7_Free(pkcs7);
  24917. printf(resultFmt, passed);
  24918. #endif
  24919. }
  24920. static void test_wc_i2d_PKCS12(void)
  24921. {
  24922. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  24923. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  24924. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  24925. WC_PKCS12* pkcs12 = NULL;
  24926. unsigned char der[FOURK_BUF * 2];
  24927. unsigned char* pt;
  24928. int derSz;
  24929. unsigned char out[FOURK_BUF * 2];
  24930. int outSz = FOURK_BUF * 2;
  24931. const char p12_f[] = "./certs/test-servercert.p12";
  24932. XFILE f;
  24933. printf(testingFmt, "wc_i2d_PKCS12");
  24934. f = XFOPEN(p12_f, "rb");
  24935. AssertNotNull(f);
  24936. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  24937. AssertIntGT(derSz, 0);
  24938. XFCLOSE(f);
  24939. AssertNotNull(pkcs12 = wc_PKCS12_new());
  24940. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  24941. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  24942. AssertIntEQ(outSz, derSz);
  24943. outSz = derSz - 1;
  24944. pt = out;
  24945. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  24946. outSz = derSz;
  24947. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  24948. AssertIntEQ((pt == out), 0);
  24949. pt = NULL;
  24950. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  24951. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  24952. wc_PKCS12_free(pkcs12);
  24953. printf(resultFmt, passed);
  24954. #endif
  24955. }
  24956. /* Testing wc_SignatureGetSize() for signature type ECC */
  24957. static int test_wc_SignatureGetSize_ecc(void)
  24958. {
  24959. int ret = 0;
  24960. #ifndef NO_SIG_WRAPPER
  24961. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  24962. enum wc_SignatureType sig_type;
  24963. word32 key_len;
  24964. /* Initialize ECC Key */
  24965. ecc_key ecc;
  24966. const char* qx =
  24967. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  24968. const char* qy =
  24969. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  24970. const char* d =
  24971. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  24972. ret = wc_ecc_init(&ecc);
  24973. if (ret == 0) {
  24974. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  24975. }
  24976. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  24977. if (ret == 0) {
  24978. /* Input for signature type ECC */
  24979. sig_type = WC_SIGNATURE_TYPE_ECC;
  24980. key_len = sizeof(ecc_key);
  24981. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  24982. /* Test bad args */
  24983. if (ret > 0) {
  24984. sig_type = (enum wc_SignatureType) 100;
  24985. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  24986. if (ret == BAD_FUNC_ARG) {
  24987. sig_type = WC_SIGNATURE_TYPE_ECC;
  24988. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  24989. }
  24990. if (ret >= 0) {
  24991. key_len = (word32) 0;
  24992. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  24993. }
  24994. if (ret == BAD_FUNC_ARG) {
  24995. ret = SIG_TYPE_E;
  24996. }
  24997. }
  24998. } else {
  24999. ret = WOLFSSL_FATAL_ERROR;
  25000. }
  25001. wc_ecc_free(&ecc);
  25002. #else
  25003. ret = SIG_TYPE_E;
  25004. #endif
  25005. if (ret == SIG_TYPE_E) {
  25006. ret = 0;
  25007. }
  25008. else {
  25009. ret = WOLFSSL_FATAL_ERROR;
  25010. }
  25011. printf(resultFmt, ret == 0 ? passed : failed);
  25012. #endif /* NO_SIG_WRAPPER */
  25013. return ret;
  25014. }/* END test_wc_SignatureGetSize_ecc() */
  25015. /* Testing wc_SignatureGetSize() for signature type rsa */
  25016. static int test_wc_SignatureGetSize_rsa(void)
  25017. {
  25018. int ret = 0;
  25019. #ifndef NO_SIG_WRAPPER
  25020. #ifndef NO_RSA
  25021. enum wc_SignatureType sig_type;
  25022. word32 key_len;
  25023. word32 idx = 0;
  25024. /* Initialize RSA Key */
  25025. RsaKey rsa_key;
  25026. byte* tmp = NULL;
  25027. size_t bytes;
  25028. #ifdef USE_CERT_BUFFERS_1024
  25029. bytes = (size_t)sizeof_client_key_der_1024;
  25030. if (bytes < (size_t)sizeof_client_key_der_1024)
  25031. bytes = (size_t)sizeof_client_cert_der_1024;
  25032. #elif defined(USE_CERT_BUFFERS_2048)
  25033. bytes = (size_t)sizeof_client_key_der_2048;
  25034. if (bytes < (size_t)sizeof_client_cert_der_2048)
  25035. bytes = (size_t)sizeof_client_cert_der_2048;
  25036. #else
  25037. bytes = FOURK_BUF;
  25038. #endif
  25039. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25040. if (tmp != NULL) {
  25041. #ifdef USE_CERT_BUFFERS_1024
  25042. XMEMCPY(tmp, client_key_der_1024,
  25043. (size_t)sizeof_client_key_der_1024);
  25044. #elif defined(USE_CERT_BUFFERS_2048)
  25045. XMEMCPY(tmp, client_key_der_2048,
  25046. (size_t)sizeof_client_key_der_2048);
  25047. #elif !defined(NO_FILESYSTEM)
  25048. file = XFOPEN(clientKey, "rb");
  25049. if (file != XBADFILE) {
  25050. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  25051. XFCLOSE(file);
  25052. }
  25053. else {
  25054. ret = WOLFSSL_FATAL_ERROR;
  25055. }
  25056. #else
  25057. ret = WOLFSSL_FATAL_ERROR;
  25058. #endif
  25059. } else {
  25060. ret = WOLFSSL_FATAL_ERROR;
  25061. }
  25062. if (ret == 0) {
  25063. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, devId);
  25064. }
  25065. if (ret == 0) {
  25066. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  25067. }
  25068. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  25069. if (ret == 0) {
  25070. /* Input for signature type RSA */
  25071. sig_type = WC_SIGNATURE_TYPE_RSA;
  25072. key_len = sizeof(RsaKey);
  25073. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25074. /* Test bad args */
  25075. if (ret > 0) {
  25076. sig_type = (enum wc_SignatureType) 100;
  25077. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25078. if (ret == BAD_FUNC_ARG) {
  25079. sig_type = WC_SIGNATURE_TYPE_RSA;
  25080. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  25081. }
  25082. #ifndef HAVE_USER_RSA
  25083. if (ret == BAD_FUNC_ARG) {
  25084. #else
  25085. if (ret == 0) {
  25086. #endif
  25087. key_len = (word32)0;
  25088. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25089. }
  25090. if (ret == BAD_FUNC_ARG) {
  25091. ret = SIG_TYPE_E;
  25092. }
  25093. }
  25094. } else {
  25095. ret = WOLFSSL_FATAL_ERROR;
  25096. }
  25097. wc_FreeRsaKey(&rsa_key);
  25098. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25099. #else
  25100. ret = SIG_TYPE_E;
  25101. #endif
  25102. if (ret == SIG_TYPE_E) {
  25103. ret = 0;
  25104. }else {
  25105. ret = WOLFSSL_FATAL_ERROR;
  25106. }
  25107. printf(resultFmt, ret == 0 ? passed : failed);
  25108. #endif /* NO_SIG_WRAPPER */
  25109. return ret;
  25110. }/* END test_wc_SignatureGetSize_rsa(void) */
  25111. /*----------------------------------------------------------------------------*
  25112. | hash.h Tests
  25113. *----------------------------------------------------------------------------*/
  25114. static int test_wc_HashInit(void)
  25115. {
  25116. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  25117. wc_HashAlg hash;
  25118. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25119. enum wc_HashType enumArray[] = {
  25120. #ifndef NO_MD5
  25121. WC_HASH_TYPE_MD5,
  25122. #endif
  25123. #ifndef NO_SHA
  25124. WC_HASH_TYPE_SHA,
  25125. #endif
  25126. #ifndef WOLFSSL_SHA224
  25127. WC_HASH_TYPE_SHA224,
  25128. #endif
  25129. #ifndef NO_SHA256
  25130. WC_HASH_TYPE_SHA256,
  25131. #endif
  25132. #ifndef WOLFSSL_SHA384
  25133. WC_HASH_TYPE_SHA384,
  25134. #endif
  25135. #ifndef WOLFSSL_SHA512
  25136. WC_HASH_TYPE_SHA512,
  25137. #endif
  25138. };
  25139. /* dynamically finds the length */
  25140. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25141. /* For loop to test various arguments... */
  25142. for (i = 0; i < enumlen; i++) {
  25143. /* check for bad args */
  25144. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  25145. ret = 1;
  25146. break;
  25147. }
  25148. wc_HashFree(&hash, enumArray[i]);
  25149. /* check for null ptr */
  25150. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  25151. ret = 1;
  25152. break;
  25153. }
  25154. } /* end of for loop */
  25155. printf(testingFmt, "wc_HashInit()");
  25156. if (ret==0) { /* all tests have passed */
  25157. printf(resultFmt, passed);
  25158. }
  25159. else { /* a test has failed */
  25160. printf(resultFmt, failed);
  25161. }
  25162. return ret;
  25163. } /* end of test_wc_HashInit */
  25164. /*
  25165. * Unit test function for wc_HashSetFlags()
  25166. */
  25167. static int test_wc_HashSetFlags(void)
  25168. {
  25169. int ret = 0;
  25170. #ifdef WOLFSSL_HASH_FLAGS
  25171. wc_HashAlg hash;
  25172. word32 flags = 0;
  25173. int i, j;
  25174. printf(testingFmt, "wc_HashSetFlags()");
  25175. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25176. enum wc_HashType enumArray[] = {
  25177. #ifndef NO_MD5
  25178. WC_HASH_TYPE_MD5,
  25179. #endif
  25180. #ifndef NO_SHA
  25181. WC_HASH_TYPE_SHA,
  25182. #endif
  25183. #ifdef WOLFSSL_SHA224
  25184. WC_HASH_TYPE_SHA224,
  25185. #endif
  25186. #ifndef NO_SHA256
  25187. WC_HASH_TYPE_SHA256,
  25188. #endif
  25189. #ifdef WOLFSSL_SHA384
  25190. WC_HASH_TYPE_SHA384,
  25191. #endif
  25192. #ifdef WOLFSSL_SHA512
  25193. WC_HASH_TYPE_SHA512,
  25194. #endif
  25195. #ifdef WOLFSSL_SHA3
  25196. WC_HASH_TYPE_SHA3_224,
  25197. #endif
  25198. };
  25199. enum wc_HashType notSupported[] = {
  25200. WC_HASH_TYPE_MD5_SHA,
  25201. WC_HASH_TYPE_MD2,
  25202. WC_HASH_TYPE_MD4,
  25203. WC_HASH_TYPE_BLAKE2B,
  25204. WC_HASH_TYPE_BLAKE2S,
  25205. WC_HASH_TYPE_NONE,
  25206. };
  25207. /* dynamically finds the length */
  25208. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25209. /* For loop to test various arguments... */
  25210. for (i = 0; i < enumlen; i++) {
  25211. ret = wc_HashInit(&hash, enumArray[i]);
  25212. if (ret == 0) {
  25213. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  25214. }
  25215. if (ret == 0) {
  25216. if (flags & WC_HASH_FLAG_ISCOPY) {
  25217. ret = 0;
  25218. }
  25219. }
  25220. if (ret == 0) {
  25221. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  25222. if (ret == BAD_FUNC_ARG) {
  25223. ret = 0;
  25224. }
  25225. }
  25226. wc_HashFree(&hash, enumArray[i]);
  25227. }
  25228. /* For loop to test not supported cases */
  25229. int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25230. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  25231. ret = wc_HashInit(&hash, notSupported[j]);
  25232. if (ret == 0) {
  25233. ret = -1;
  25234. }
  25235. else if (ret == BAD_FUNC_ARG){
  25236. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  25237. if (ret == 0) {
  25238. ret = -1;
  25239. }
  25240. else if (ret == BAD_FUNC_ARG) {
  25241. ret = 0;
  25242. }
  25243. }
  25244. if (ret == 0) {
  25245. ret = wc_HashFree(&hash, notSupported[j]);
  25246. if (ret == 0) {
  25247. ret = -1;
  25248. }
  25249. else if (ret == BAD_FUNC_ARG) {
  25250. ret = 0;
  25251. }
  25252. }
  25253. }
  25254. printf(resultFmt, ret == 0 ? passed : failed);
  25255. #endif
  25256. return ret;
  25257. } /* END test_wc_HashSetFlags */
  25258. /*
  25259. * Unit test function for wc_HashGetFlags()
  25260. */
  25261. static int test_wc_HashGetFlags(void)
  25262. {
  25263. int ret = 0;
  25264. #ifdef WOLFSSL_HASH_FLAGS
  25265. wc_HashAlg hash;
  25266. word32 flags = 0;
  25267. int i, j;
  25268. printf(testingFmt, "wc_HashGetFlags()");
  25269. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25270. enum wc_HashType enumArray[] = {
  25271. #ifndef NO_MD5
  25272. WC_HASH_TYPE_MD5,
  25273. #endif
  25274. #ifndef NO_SHA
  25275. WC_HASH_TYPE_SHA,
  25276. #endif
  25277. #ifdef WOLFSSL_SHA224
  25278. WC_HASH_TYPE_SHA224,
  25279. #endif
  25280. #ifndef NO_SHA256
  25281. WC_HASH_TYPE_SHA256,
  25282. #endif
  25283. #ifdef WOLFSSL_SHA384
  25284. WC_HASH_TYPE_SHA384,
  25285. #endif
  25286. #ifdef WOLFSSL_SHA512
  25287. WC_HASH_TYPE_SHA512,
  25288. #endif
  25289. #ifdef WOLFSSL_SHA3
  25290. WC_HASH_TYPE_SHA3_224,
  25291. #endif
  25292. };
  25293. enum wc_HashType notSupported[] = {
  25294. WC_HASH_TYPE_MD5_SHA,
  25295. WC_HASH_TYPE_MD2,
  25296. WC_HASH_TYPE_MD4,
  25297. WC_HASH_TYPE_BLAKE2B,
  25298. WC_HASH_TYPE_BLAKE2S,
  25299. WC_HASH_TYPE_NONE,
  25300. };
  25301. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25302. /* For loop to test various arguments... */
  25303. for (i = 0; i < enumlen; i++) {
  25304. ret = wc_HashInit(&hash, enumArray[i]);
  25305. if (ret == 0) {
  25306. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  25307. }
  25308. if (ret == 0) {
  25309. if (flags & WC_HASH_FLAG_ISCOPY) {
  25310. ret = 0;
  25311. }
  25312. }
  25313. if (ret == 0) {
  25314. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  25315. if (ret == BAD_FUNC_ARG) {
  25316. ret = 0;
  25317. }
  25318. }
  25319. wc_HashFree(&hash, enumArray[i]);
  25320. if (ret != 0) {
  25321. break;
  25322. }
  25323. }
  25324. /* For loop to test not supported cases */
  25325. int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25326. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  25327. ret = wc_HashInit(&hash, notSupported[j]);
  25328. if (ret == 0) {
  25329. ret = -1;
  25330. }
  25331. else if (ret == BAD_FUNC_ARG){
  25332. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  25333. if (ret == 0) {
  25334. ret = -1;
  25335. }
  25336. else if (ret == BAD_FUNC_ARG) {
  25337. ret = 0;
  25338. }
  25339. }
  25340. if (ret == 0) {
  25341. ret = wc_HashFree(&hash, notSupported[j]);
  25342. if (ret == 0) {
  25343. ret = -1;
  25344. }
  25345. if (ret == BAD_FUNC_ARG) {
  25346. ret = 0;
  25347. }
  25348. }
  25349. }
  25350. printf(resultFmt, ret == 0 ? passed : failed);
  25351. #endif
  25352. return ret;
  25353. } /* END test_wc_HashGetFlags */
  25354. /*----------------------------------------------------------------------------*
  25355. | Compatibility Tests
  25356. *----------------------------------------------------------------------------*/
  25357. static void test_wolfSSL_lhash(void)
  25358. {
  25359. #ifdef OPENSSL_ALL
  25360. const char testStr[] = "Like a true nature's child\n"
  25361. "We were born\n"
  25362. "Born to be wild";
  25363. printf(testingFmt, "wolfSSL_LH_strhash()");
  25364. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  25365. printf(resultFmt, passed);
  25366. #endif
  25367. }
  25368. static void test_wolfSSL_X509_NAME(void)
  25369. {
  25370. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  25371. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25372. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  25373. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  25374. defined(OPENSSL_EXTRA))
  25375. X509* x509;
  25376. const unsigned char* c;
  25377. unsigned char buf[4096];
  25378. int bytes;
  25379. XFILE f;
  25380. const X509_NAME* a;
  25381. const X509_NAME* b;
  25382. X509_NAME* d2i_name = NULL;
  25383. int sz;
  25384. unsigned char* tmp;
  25385. char file[] = "./certs/ca-cert.der";
  25386. #ifndef OPENSSL_EXTRA_X509_SMALL
  25387. byte empty[] = { /* CN=empty emailAddress= */
  25388. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  25389. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  25390. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  25391. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25392. 0x01, 0x16, 0x00
  25393. };
  25394. #endif
  25395. printf(testingFmt, "wolfSSL_X509_NAME()");
  25396. #ifndef OPENSSL_EXTRA_X509_SMALL
  25397. /* test compile of deprecated function, returns 0 */
  25398. AssertIntEQ(CRYPTO_thread_id(), 0);
  25399. #endif
  25400. AssertNotNull(a = X509_NAME_new());
  25401. X509_NAME_free((X509_NAME*)a);
  25402. f = XFOPEN(file, "rb");
  25403. AssertTrue(f != XBADFILE);
  25404. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  25405. XFCLOSE(f);
  25406. c = buf;
  25407. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  25408. /* test cmp function */
  25409. AssertNotNull(a = X509_get_issuer_name(x509));
  25410. AssertNotNull(b = X509_get_subject_name(x509));
  25411. #ifndef OPENSSL_EXTRA_X509_SMALL
  25412. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  25413. #endif
  25414. tmp = buf;
  25415. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  25416. if (sz > 0 && tmp == buf) {
  25417. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__); \
  25418. printf(" Expected pointer to be incremented\n");
  25419. abort();
  25420. }
  25421. #ifndef OPENSSL_EXTRA_X509_SMALL
  25422. tmp = buf;
  25423. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  25424. #endif
  25425. /* retry but with the function creating a buffer */
  25426. tmp = NULL;
  25427. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  25428. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  25429. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  25430. #ifndef OPENSSL_EXTRA_X509_SMALL
  25431. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  25432. #endif
  25433. X509_NAME_free((X509_NAME*)b);
  25434. X509_NAME_free(d2i_name);
  25435. X509_free(x509);
  25436. #ifndef OPENSSL_EXTRA_X509_SMALL
  25437. /* test with an empty domain component */
  25438. tmp = empty;
  25439. sz = sizeof(empty);
  25440. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  25441. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  25442. /* size of empty emailAddress will be 0 */
  25443. tmp = buf;
  25444. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  25445. (char*)tmp, sizeof(buf)), 0);
  25446. /* should contain no organization name */
  25447. tmp = buf;
  25448. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  25449. (char*)tmp, sizeof(buf)), -1);
  25450. X509_NAME_free(d2i_name);
  25451. #endif
  25452. printf(resultFmt, passed);
  25453. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  25454. }
  25455. static void test_wolfSSL_X509_NAME_hash(void)
  25456. {
  25457. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  25458. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  25459. BIO* bio;
  25460. X509* x509 = NULL;
  25461. printf(testingFmt, "wolfSSL_X509_NAME_hash");
  25462. AssertNotNull(bio = BIO_new(BIO_s_file()));
  25463. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  25464. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  25465. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0xF6CF410E);
  25466. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0x677DD39A);
  25467. X509_free(x509);
  25468. BIO_free(bio);
  25469. printf(resultFmt, passed);
  25470. #endif
  25471. }
  25472. #ifndef NO_BIO
  25473. static void test_wolfSSL_X509_INFO(void)
  25474. {
  25475. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  25476. STACK_OF(X509_INFO) *info_stack;
  25477. X509_INFO *info;
  25478. BIO *cert;
  25479. int i;
  25480. /* PEM in hex format to avoid null terminator */
  25481. byte data[] = {
  25482. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  25483. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  25484. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  25485. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  25486. 0x2d, 0x2d, 0x2d
  25487. };
  25488. /* PEM in hex format to avoid null terminator */
  25489. byte data2[] = {
  25490. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  25491. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  25492. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  25493. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  25494. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  25495. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  25496. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  25497. };
  25498. printf(testingFmt, "wolfSSL_X509_INFO");
  25499. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  25500. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  25501. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  25502. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  25503. AssertNotNull(info->x509);
  25504. AssertNull(info->crl);
  25505. }
  25506. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  25507. BIO_free(cert);
  25508. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  25509. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  25510. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  25511. BIO_free(cert);
  25512. /* This case should fail due to invalid input. */
  25513. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  25514. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  25515. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  25516. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  25517. BIO_free(cert);
  25518. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  25519. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  25520. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  25521. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  25522. BIO_free(cert);
  25523. printf(resultFmt, passed);
  25524. #endif
  25525. }
  25526. #endif
  25527. static void test_wolfSSL_X509_subject_name_hash(void)
  25528. {
  25529. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25530. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  25531. X509* x509;
  25532. X509_NAME* subjectName = NULL;
  25533. unsigned long ret = 0;
  25534. printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
  25535. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  25536. SSL_FILETYPE_PEM));
  25537. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  25538. ret = X509_subject_name_hash(x509);
  25539. AssertIntNE(ret, 0);
  25540. X509_free(x509);
  25541. printf(resultFmt, passed);
  25542. #endif
  25543. }
  25544. static void test_wolfSSL_X509_issuer_name_hash(void)
  25545. {
  25546. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25547. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  25548. X509* x509;
  25549. X509_NAME* issuertName = NULL;
  25550. unsigned long ret = 0;
  25551. printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
  25552. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  25553. SSL_FILETYPE_PEM));
  25554. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  25555. ret = X509_issuer_name_hash(x509);
  25556. AssertIntNE(ret, 0);
  25557. X509_free(x509);
  25558. printf(resultFmt, passed);
  25559. #endif
  25560. }
  25561. static void test_wolfSSL_X509_check_host(void)
  25562. {
  25563. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25564. && !defined(NO_SHA) && !defined(NO_RSA)
  25565. X509* x509;
  25566. const char altName[] = "example.com";
  25567. printf(testingFmt, "wolfSSL_X509_check_host()");
  25568. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  25569. SSL_FILETYPE_PEM));
  25570. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  25571. WOLFSSL_SUCCESS);
  25572. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  25573. WOLFSSL_FAILURE);
  25574. X509_free(x509);
  25575. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  25576. WOLFSSL_FAILURE);
  25577. printf(resultFmt, passed);
  25578. #endif
  25579. }
  25580. static void test_wolfSSL_X509_check_email(void)
  25581. {
  25582. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  25583. X509* x509;
  25584. const char goodEmail[] = "info@wolfssl.com";
  25585. const char badEmail[] = "disinfo@wolfssl.com";
  25586. printf(testingFmt, "wolfSSL_X509_check_email()");
  25587. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  25588. SSL_FILETYPE_PEM));
  25589. /* Should fail on non-matching email address */
  25590. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  25591. WOLFSSL_FAILURE);
  25592. /* Should succeed on matching email address */
  25593. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  25594. WOLFSSL_SUCCESS);
  25595. /* Should compute length internally when not provided */
  25596. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  25597. WOLFSSL_SUCCESS);
  25598. /* Should fail when email address is NULL */
  25599. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  25600. WOLFSSL_FAILURE);
  25601. X509_free(x509);
  25602. /* Should fail when x509 is NULL */
  25603. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  25604. WOLFSSL_FAILURE);
  25605. printf(resultFmt, passed);
  25606. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  25607. }
  25608. static void test_wolfSSL_DES(void)
  25609. {
  25610. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  25611. const_DES_cblock myDes;
  25612. DES_cblock iv;
  25613. DES_key_schedule key;
  25614. word32 i;
  25615. DES_LONG dl;
  25616. unsigned char msg[] = "hello wolfssl";
  25617. printf(testingFmt, "wolfSSL_DES()");
  25618. DES_check_key(1);
  25619. DES_set_key(&myDes, &key);
  25620. /* check, check of odd parity */
  25621. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  25622. XMEMSET(key, 5, sizeof(DES_key_schedule));
  25623. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  25624. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  25625. /* set odd parity for success case */
  25626. DES_set_odd_parity(&myDes);
  25627. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  25628. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  25629. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  25630. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  25631. AssertIntEQ(key[i], myDes[i]);
  25632. }
  25633. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  25634. /* check weak key */
  25635. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  25636. XMEMSET(key, 5, sizeof(DES_key_schedule));
  25637. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  25638. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  25639. /* now do unchecked copy of a weak key over */
  25640. DES_set_key_unchecked(&myDes, &key);
  25641. /* compare arrays, should be the same */
  25642. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  25643. AssertIntEQ(key[i], myDes[i]);
  25644. }
  25645. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  25646. /* check DES_key_sched API */
  25647. XMEMSET(key, 1, sizeof(DES_key_schedule));
  25648. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  25649. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  25650. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  25651. /* compare arrays, should be the same */
  25652. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  25653. AssertIntEQ(key[i], myDes[i]);
  25654. }
  25655. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  25656. * DES_cbc_encrypt on the input */
  25657. XMEMSET(iv, 0, sizeof(DES_cblock));
  25658. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  25659. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  25660. AssertIntEQ(dl, 480052723);
  25661. printf(resultFmt, passed);
  25662. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  25663. }
  25664. static void test_wc_PemToDer(void)
  25665. {
  25666. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  25667. int ret;
  25668. DerBuffer* pDer = NULL;
  25669. const char* ca_cert = "./certs/server-cert.pem";
  25670. byte* cert_buf = NULL;
  25671. size_t cert_sz = 0;
  25672. int eccKey = 0;
  25673. EncryptedInfo info;
  25674. printf(testingFmt, "wc_PemToDer()");
  25675. XMEMSET(&info, 0, sizeof(info));
  25676. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  25677. if (ret == 0) {
  25678. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  25679. &pDer, NULL, &info, &eccKey);
  25680. AssertIntEQ(ret, 0);
  25681. wc_FreeDer(&pDer);
  25682. }
  25683. if (cert_buf)
  25684. free(cert_buf);
  25685. #ifdef HAVE_ECC
  25686. {
  25687. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  25688. byte key_buf[256] = {0};
  25689. /* Test fail of loading a key with cert type */
  25690. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  25691. key_buf[0] = '\n';
  25692. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  25693. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  25694. &pDer, NULL, &info, &eccKey)), 0);
  25695. #ifdef OPENSSL_EXTRA
  25696. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  25697. &pDer, NULL, &info, &eccKey)), 0);
  25698. #endif
  25699. wc_FreeDer(&pDer);
  25700. if (cert_buf)
  25701. free(cert_buf);
  25702. }
  25703. #endif
  25704. printf(resultFmt, passed);
  25705. #endif
  25706. }
  25707. static void test_wc_AllocDer(void)
  25708. {
  25709. #if !defined(NO_CERTS)
  25710. int ret;
  25711. DerBuffer* pDer = NULL;
  25712. word32 testSize = 1024;
  25713. printf(testingFmt, "wc_AllocDer()");
  25714. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  25715. AssertIntEQ(ret, 0);
  25716. AssertNotNull(pDer);
  25717. wc_FreeDer(&pDer);
  25718. printf(resultFmt, passed);
  25719. #endif
  25720. }
  25721. static void test_wc_CertPemToDer(void)
  25722. {
  25723. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  25724. int ret;
  25725. const char* ca_cert = "./certs/ca-cert.pem";
  25726. byte* cert_buf = NULL;
  25727. size_t cert_sz = 0, cert_dersz = 0;
  25728. byte* cert_der = NULL;
  25729. printf(testingFmt, "wc_CertPemToDer()");
  25730. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  25731. if (ret == 0) {
  25732. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  25733. cert_der = (byte*)malloc(cert_dersz);
  25734. if (cert_der) {
  25735. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  25736. cert_der, (int)cert_dersz, CERT_TYPE);
  25737. AssertIntGE(ret, 0);
  25738. }
  25739. }
  25740. if (cert_der)
  25741. free(cert_der);
  25742. if (cert_buf)
  25743. free(cert_buf);
  25744. #endif
  25745. }
  25746. static void test_wc_PubKeyPemToDer(void)
  25747. {
  25748. #ifdef WOLFSSL_PEM_TO_DER
  25749. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  25750. int ret;
  25751. const char* key = "./certs/ecc-client-keyPub.pem";
  25752. byte* cert_buf = NULL;
  25753. size_t cert_sz = 0, cert_dersz = 0;
  25754. byte* cert_der = NULL;
  25755. printf(testingFmt, "wc_PubKeyPemToDer()");
  25756. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  25757. cert_der, (int)cert_dersz);
  25758. AssertIntGE(ret, BAD_FUNC_ARG);
  25759. ret = load_file(key, &cert_buf, &cert_sz);
  25760. if (ret == 0) {
  25761. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  25762. cert_der = (byte*)malloc(cert_dersz);
  25763. if (cert_der) {
  25764. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  25765. cert_der, (int)cert_dersz);
  25766. AssertIntGE(ret, 0);
  25767. }
  25768. }
  25769. if (cert_der)
  25770. free(cert_der);
  25771. if (cert_buf)
  25772. free(cert_buf);
  25773. #endif
  25774. #endif
  25775. }
  25776. static void test_wc_PemPubKeyToDer(void)
  25777. {
  25778. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  25779. int ret;
  25780. const char* key = "./certs/ecc-client-keyPub.pem";
  25781. size_t cert_dersz = 1024;
  25782. byte* cert_der = (byte*)malloc(cert_dersz);
  25783. printf(testingFmt, "wc_PemPubKeyToDer()");
  25784. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  25785. AssertIntGE(ret, BAD_FUNC_ARG);
  25786. if (cert_der) {
  25787. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  25788. AssertIntGE(ret, 0);
  25789. free(cert_der);
  25790. }
  25791. #endif
  25792. }
  25793. static void test_wolfSSL_certs(void)
  25794. {
  25795. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  25796. !defined(NO_RSA)
  25797. X509* x509ext;
  25798. #ifdef OPENSSL_ALL
  25799. X509* x509;
  25800. WOLFSSL_X509_EXTENSION* ext;
  25801. ASN1_OBJECT* obj;
  25802. #endif
  25803. WOLFSSL* ssl;
  25804. WOLFSSL_CTX* ctx;
  25805. STACK_OF(ASN1_OBJECT)* sk;
  25806. ASN1_STRING* asn1_str;
  25807. AUTHORITY_KEYID* akey;
  25808. BASIC_CONSTRAINTS* bc;
  25809. int crit;
  25810. printf(testingFmt, "wolfSSL_certs()");
  25811. #ifndef NO_WOLFSSL_SERVER
  25812. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  25813. #else
  25814. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  25815. #endif
  25816. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  25817. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  25818. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  25819. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  25820. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  25821. #endif
  25822. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  25823. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  25824. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  25825. #endif
  25826. AssertNotNull(ssl = SSL_new(ctx));
  25827. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  25828. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  25829. #endif
  25830. #ifdef HAVE_PK_CALLBACKS
  25831. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  25832. #endif /* HAVE_PK_CALLBACKS */
  25833. /* create and use x509 */
  25834. #ifdef OPENSSL_ALL
  25835. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  25836. AssertNotNull(x509);
  25837. #endif
  25838. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  25839. AssertNotNull(x509ext);
  25840. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  25841. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  25842. /* with loading in a new cert the check on private key should now fail */
  25843. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  25844. #endif
  25845. #if defined(USE_CERT_BUFFERS_2048)
  25846. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  25847. (unsigned char*)server_cert_der_2048,
  25848. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  25849. #endif
  25850. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  25851. /************* Get Digest of Certificate ******************/
  25852. {
  25853. byte digest[64]; /* max digest size */
  25854. word32 digestSz;
  25855. XMEMSET(digest, 0, sizeof(digest));
  25856. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  25857. WOLFSSL_SUCCESS);
  25858. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  25859. WOLFSSL_SUCCESS);
  25860. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  25861. WOLFSSL_FAILURE);
  25862. }
  25863. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  25864. /* test and checkout X509 extensions */
  25865. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  25866. &crit, NULL);
  25867. AssertNotNull(bc);
  25868. AssertIntEQ(crit, 0);
  25869. #ifdef OPENSSL_ALL
  25870. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  25871. AssertNotNull(ext);
  25872. X509_EXTENSION_free(ext);
  25873. AssertNotNull(ext = X509_EXTENSION_new());
  25874. X509_EXTENSION_set_critical(ext, 1);
  25875. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  25876. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  25877. ASN1_OBJECT_free(obj);
  25878. X509_EXTENSION_free(ext);
  25879. AssertNotNull(ext = X509_EXTENSION_new());
  25880. X509_EXTENSION_set_critical(ext, 0);
  25881. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  25882. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  25883. NULL);
  25884. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  25885. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  25886. * and X509_get_ext_d2i has created new */
  25887. X509_EXTENSION_free(ext);
  25888. #endif
  25889. BASIC_CONSTRAINTS_free(bc);
  25890. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  25891. AssertNotNull(asn1_str);
  25892. AssertIntEQ(crit, 1);
  25893. AssertIntEQ(asn1_str->type, NID_key_usage);
  25894. #ifdef OPENSSL_ALL
  25895. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  25896. AssertNotNull(ext);
  25897. X509_EXTENSION_free(ext);
  25898. #endif
  25899. ASN1_STRING_free(asn1_str);
  25900. #ifdef OPENSSL_ALL
  25901. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  25902. &crit, NULL);
  25903. AssertNotNull(sk);
  25904. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  25905. AssertNotNull(ext);
  25906. X509_EXTENSION_free(ext);
  25907. sk_ASN1_OBJECT_pop_free(sk, NULL);
  25908. #else
  25909. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  25910. &crit, NULL);
  25911. AssertNull(sk);
  25912. #endif
  25913. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  25914. NID_authority_key_identifier, &crit, NULL);
  25915. AssertNotNull(akey);
  25916. #ifdef OPENSSL_ALL
  25917. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  25918. AssertNotNull(ext);
  25919. X509_EXTENSION_free(ext);
  25920. #endif
  25921. wolfSSL_AUTHORITY_KEYID_free(akey);
  25922. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  25923. NID_private_key_usage_period, &crit, NULL);
  25924. /* AssertNotNull(sk); NID not yet supported */
  25925. AssertIntEQ(crit, -1);
  25926. sk_ASN1_OBJECT_free(sk);
  25927. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  25928. &crit, NULL);
  25929. /* AssertNotNull(sk); no alt names set */
  25930. sk_GENERAL_NAME_free(sk);
  25931. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  25932. &crit, NULL);
  25933. /* AssertNotNull(sk); NID not yet supported */
  25934. AssertIntEQ(crit, -1);
  25935. sk_ASN1_OBJECT_free(sk);
  25936. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  25937. NULL);
  25938. /* AssertNotNull(sk); no auth info set */
  25939. sk_ASN1_OBJECT_free(sk);
  25940. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  25941. &crit, NULL);
  25942. /* AssertNotNull(sk); NID not yet supported */
  25943. AssertIntEQ(crit, -1);
  25944. sk_ASN1_OBJECT_free(sk);
  25945. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  25946. &crit, NULL);
  25947. /* AssertNotNull(sk); NID not yet supported */
  25948. AssertIntEQ(crit, -1);
  25949. sk_ASN1_OBJECT_free(sk);
  25950. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  25951. NID_certificate_policies, &crit, NULL);
  25952. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  25953. AssertNull(sk);
  25954. #else
  25955. /* AssertNotNull(sk); no cert policy set */
  25956. #endif
  25957. sk_ASN1_OBJECT_free(sk);
  25958. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  25959. &crit, NULL);
  25960. /* AssertNotNull(sk); NID not yet supported */
  25961. AssertIntEQ(crit, -1);
  25962. sk_ASN1_OBJECT_free(sk);
  25963. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  25964. &crit, NULL);
  25965. /* AssertNotNull(sk); NID not yet supported */
  25966. AssertIntEQ(crit, -1);
  25967. sk_ASN1_OBJECT_free(sk);
  25968. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  25969. &crit, NULL);
  25970. /* AssertNotNull(sk); NID not yet supported */
  25971. AssertIntEQ(crit, -1);
  25972. sk_ASN1_OBJECT_free(sk);
  25973. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  25974. NULL);
  25975. /* AssertNotNull(sk); NID not yet supported */
  25976. AssertIntEQ(crit, -1);
  25977. sk_ASN1_OBJECT_free(sk);
  25978. /* test invalid cases */
  25979. crit = 0;
  25980. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  25981. AssertNull(sk);
  25982. AssertIntEQ(crit, -1);
  25983. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  25984. NULL, NULL);
  25985. AssertNull(sk);
  25986. AssertIntEQ(SSL_get_hit(ssl), 0);
  25987. #ifdef OPENSSL_ALL
  25988. X509_free(x509);
  25989. #endif
  25990. X509_free(x509ext);
  25991. SSL_free(ssl);
  25992. SSL_CTX_free(ctx);
  25993. printf(resultFmt, passed);
  25994. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  25995. }
  25996. static void test_wolfSSL_X509_check_private_key(void)
  25997. {
  25998. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  25999. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  26000. X509* x509;
  26001. EVP_PKEY* pkey = NULL;
  26002. const byte* key;
  26003. printf(testingFmt, "wolfSSL_X509_check_private_key()");
  26004. /* Check with correct key */
  26005. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  26006. SSL_FILETYPE_PEM)));
  26007. key = client_key_der_2048;
  26008. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26009. &key, (long)sizeof_client_key_der_2048));
  26010. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  26011. EVP_PKEY_free(pkey);
  26012. pkey = NULL;
  26013. /* Check with wrong key */
  26014. key = server_key_der_2048;
  26015. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26016. &key, (long)sizeof_server_key_der_2048));
  26017. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  26018. /* test for incorrect parameter */
  26019. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  26020. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  26021. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  26022. EVP_PKEY_free(pkey);
  26023. X509_free(x509);
  26024. printf(resultFmt, passed);
  26025. #endif
  26026. }
  26027. static void test_wolfSSL_ASN1_TIME_print(void)
  26028. {
  26029. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  26030. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  26031. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048)
  26032. BIO* bio;
  26033. X509* x509;
  26034. const unsigned char* der = client_cert_der_2048;
  26035. ASN1_TIME* t;
  26036. unsigned char buf[25];
  26037. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  26038. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26039. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  26040. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  26041. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  26042. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  26043. AssertIntEQ(XMEMCMP(buf, "Feb 10 19:49:52 2021 GMT", sizeof(buf) - 1), 0);
  26044. /* create a bad time and test results */
  26045. AssertNotNull(t = X509_get_notAfter(x509));
  26046. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  26047. t->data[8] = 0;
  26048. t->data[3] = 0;
  26049. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  26050. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  26051. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  26052. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  26053. BIO_free(bio);
  26054. X509_free(x509);
  26055. printf(resultFmt, passed);
  26056. #endif
  26057. }
  26058. static void test_wolfSSL_ASN1_UTCTIME_print(void)
  26059. {
  26060. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  26061. BIO* bio;
  26062. ASN1_UTCTIME* utc = NULL;
  26063. unsigned char buf[25];
  26064. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  26065. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  26066. printf(testingFmt, "ASN1_UTCTIME_print()");
  26067. /* NULL parameter check */
  26068. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26069. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  26070. BIO_free(bio);
  26071. /* Valid date */
  26072. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26073. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  26074. DYNAMIC_TYPE_ASN1));
  26075. utc->type = ASN_UTC_TIME;
  26076. utc->length = ASN_UTC_TIME_SIZE;
  26077. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  26078. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  26079. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  26080. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  26081. XMEMSET(buf, 0, sizeof(buf));
  26082. BIO_free(bio);
  26083. /* Invalid format */
  26084. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26085. utc->type = ASN_UTC_TIME;
  26086. utc->length = ASN_UTC_TIME_SIZE;
  26087. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  26088. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  26089. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  26090. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  26091. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  26092. BIO_free(bio);
  26093. printf(resultFmt, passed);
  26094. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  26095. }
  26096. static void test_wolfSSL_ASN1_TIME_diff(void)
  26097. {
  26098. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  26099. ASN1_TIME* fromTime;
  26100. ASN1_TIME* toTime;
  26101. int daysDiff;
  26102. int secsDiff;
  26103. printf(testingFmt, "test_wolfSSL_ASN1_TIME_diff");
  26104. AssertNotNull((fromTime = ASN1_TIME_new()));
  26105. /* Feb 22, 2003, 21:15:15 */
  26106. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  26107. AssertNotNull((toTime = ASN1_TIME_new()));
  26108. /* Dec 19, 2010, 18:10:11 */
  26109. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  26110. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  26111. AssertIntEQ(daysDiff, 2856);
  26112. AssertIntEQ(secsDiff, 75296);
  26113. ASN1_TIME_free(fromTime);
  26114. ASN1_TIME_free(toTime);
  26115. printf(resultFmt, passed);
  26116. #endif
  26117. }
  26118. static void test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  26119. {
  26120. #if defined(OPENSSL_EXTRA)
  26121. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  26122. unsigned char nullstr[32];
  26123. printf(testingFmt, "test_wolfSSL_ASN1_GENERALIZEDTIME_free");
  26124. XMEMSET(nullstr, 0, 32);
  26125. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  26126. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  26127. DYNAMIC_TYPE_TMP_BUFFER);
  26128. if (asn1_gtime) {
  26129. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  26130. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  26131. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  26132. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26133. }
  26134. printf(resultFmt, passed);
  26135. #endif /* OPENSSL_EXTRA */
  26136. }
  26137. static void test_wolfSSL_private_keys(void)
  26138. {
  26139. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26140. !defined(NO_FILESYSTEM)
  26141. WOLFSSL* ssl;
  26142. WOLFSSL_CTX* ctx;
  26143. EVP_PKEY* pkey = NULL;
  26144. printf(testingFmt, "wolfSSL_private_keys()");
  26145. OpenSSL_add_all_digests();
  26146. OpenSSL_add_all_algorithms();
  26147. #ifndef NO_RSA
  26148. #ifndef NO_WOLFSSL_SERVER
  26149. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26150. #else
  26151. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26152. #endif
  26153. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  26154. /* Have to load a cert before you can check the private key against that
  26155. * certificates public key! */
  26156. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26157. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  26158. #endif
  26159. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  26160. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26161. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  26162. #endif
  26163. AssertNotNull(ssl = SSL_new(ctx));
  26164. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26165. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26166. #endif
  26167. #ifdef USE_CERT_BUFFERS_2048
  26168. {
  26169. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  26170. unsigned char buf[FOURK_BUF];
  26171. word32 bufSz;
  26172. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  26173. (unsigned char*)client_key_der_2048,
  26174. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  26175. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26176. /* Should mismatch now that a different private key loaded */
  26177. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26178. #endif
  26179. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  26180. (unsigned char*)server_key,
  26181. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  26182. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26183. /* After loading back in DER format of original key, should match */
  26184. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26185. #endif
  26186. /* test loading private key to the WOLFSSL_CTX */
  26187. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  26188. (unsigned char*)client_key_der_2048,
  26189. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  26190. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26191. /* Should mismatch now that a different private key loaded */
  26192. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  26193. #endif
  26194. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  26195. (unsigned char*)server_key,
  26196. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  26197. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26198. /* After loading back in DER format of original key, should match */
  26199. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  26200. #endif
  26201. /* pkey not set yet, expecting to fail */
  26202. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  26203. /* set PKEY and test again */
  26204. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26205. &server_key, (long)sizeof_server_key_der_2048));
  26206. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  26207. /* reuse PKEY structure and test
  26208. * this should be checked with a memory management sanity checker */
  26209. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  26210. server_key = (const unsigned char*)server_key_der_2048;
  26211. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26212. &server_key, (long)sizeof_server_key_der_2048));
  26213. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  26214. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  26215. bufSz = FOURK_BUF;
  26216. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  26217. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  26218. RSAk, NULL, 0)), 0);
  26219. server_key = (const unsigned char*)buf;
  26220. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  26221. (long)bufSz));
  26222. }
  26223. #endif
  26224. EVP_PKEY_free(pkey);
  26225. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  26226. SSL_CTX_free(ctx);
  26227. #endif /* end of RSA private key match tests */
  26228. #ifdef HAVE_ECC
  26229. #ifndef NO_WOLFSSL_SERVER
  26230. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26231. #else
  26232. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26233. #endif
  26234. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  26235. WOLFSSL_FILETYPE_PEM));
  26236. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  26237. WOLFSSL_FILETYPE_PEM));
  26238. AssertNotNull(ssl = SSL_new(ctx));
  26239. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26240. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26241. #endif
  26242. SSL_free(ssl);
  26243. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  26244. WOLFSSL_FILETYPE_PEM));
  26245. AssertNotNull(ssl = SSL_new(ctx));
  26246. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26247. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26248. #endif
  26249. SSL_free(ssl);
  26250. SSL_CTX_free(ctx);
  26251. #endif /* end of ECC private key match tests */
  26252. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  26253. #ifndef NO_WOLFSSL_SERVER
  26254. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26255. #else
  26256. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26257. #endif
  26258. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  26259. WOLFSSL_FILETYPE_PEM));
  26260. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  26261. WOLFSSL_FILETYPE_PEM));
  26262. AssertNotNull(ssl = SSL_new(ctx));
  26263. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26264. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26265. #endif
  26266. SSL_free(ssl);
  26267. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  26268. WOLFSSL_FILETYPE_PEM));
  26269. AssertNotNull(ssl = SSL_new(ctx));
  26270. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26271. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26272. #endif
  26273. SSL_free(ssl);
  26274. SSL_CTX_free(ctx);
  26275. #endif /* end of Ed25519 private key match tests */
  26276. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  26277. #ifndef NO_WOLFSSL_SERVER
  26278. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26279. #else
  26280. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26281. #endif
  26282. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  26283. WOLFSSL_FILETYPE_PEM));
  26284. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  26285. WOLFSSL_FILETYPE_PEM));
  26286. AssertNotNull(ssl = SSL_new(ctx));
  26287. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26288. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26289. #endif
  26290. SSL_free(ssl);
  26291. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  26292. WOLFSSL_FILETYPE_PEM));
  26293. AssertNotNull(ssl = SSL_new(ctx));
  26294. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26295. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26296. #endif
  26297. SSL_free(ssl);
  26298. SSL_CTX_free(ctx);
  26299. #endif /* end of Ed448 private key match tests */
  26300. EVP_cleanup();
  26301. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  26302. CONF_modules_free();
  26303. ENGINE_cleanup();
  26304. CONF_modules_unload();
  26305. (void)ssl;
  26306. (void)ctx;
  26307. (void)pkey;
  26308. printf(resultFmt, passed);
  26309. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  26310. }
  26311. static void test_wolfSSL_PEM_read_PrivateKey(void)
  26312. {
  26313. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  26314. && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  26315. XFILE file;
  26316. const char* fname = "./certs/server-key.pem";
  26317. EVP_PKEY* pkey;
  26318. RSA* rsa;
  26319. WOLFSSL_EVP_PKEY_CTX* ctx;
  26320. unsigned char* sig;
  26321. size_t sigLen;
  26322. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  26323. size_t tbsLen = sizeof(tbs);
  26324. printf(testingFmt, "test_wolfSSL_PEM_read_PrivateKey()");
  26325. /* Check error case. */
  26326. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  26327. /* Read in an RSA key. */
  26328. file = XFOPEN(fname, "rb");
  26329. AssertTrue(file != XBADFILE);
  26330. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  26331. XFCLOSE(file);
  26332. /* Make sure the key is usable by signing some data with it. */
  26333. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  26334. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  26335. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  26336. DYNAMIC_TYPE_TMP_BUFFER));
  26337. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  26338. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  26339. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  26340. WOLFSSL_SUCCESS);
  26341. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26342. EVP_PKEY_CTX_free(ctx);
  26343. EVP_PKEY_free(pkey);
  26344. printf(resultFmt, passed);
  26345. #endif
  26346. }
  26347. static void test_wolfSSL_PEM_PrivateKey(void)
  26348. {
  26349. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26350. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  26351. #ifndef NO_BIO
  26352. BIO* bio = NULL;
  26353. #endif
  26354. EVP_PKEY* pkey = NULL;
  26355. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  26356. #ifndef NO_BIO
  26357. /* test creating new EVP_PKEY with bad arg */
  26358. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  26359. /* test loading RSA key using BIO */
  26360. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  26361. {
  26362. XFILE file;
  26363. const char* fname = "./certs/server-key.pem";
  26364. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  26365. size_t sz;
  26366. byte* buf;
  26367. EVP_PKEY* pkey2;
  26368. EVP_PKEY* pkey3;
  26369. RSA* rsa_key = NULL;
  26370. file = XFOPEN(fname, "rb");
  26371. AssertTrue((file != XBADFILE));
  26372. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  26373. sz = XFTELL(file);
  26374. XREWIND(file);
  26375. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  26376. if (buf) {
  26377. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  26378. }
  26379. XFCLOSE(file);
  26380. /* Test using BIO new mem and loading PEM private key */
  26381. bio = BIO_new_mem_buf(buf, (int)sz);
  26382. AssertNotNull(bio);
  26383. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  26384. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  26385. BIO_free(bio);
  26386. bio = NULL;
  26387. AssertNotNull(pkey2 = EVP_PKEY_new());
  26388. pkey2->type = EVP_PKEY_RSA;
  26389. /* Test parameter copy */
  26390. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  26391. EVP_PKEY_free(pkey2);
  26392. EVP_PKEY_free(pkey);
  26393. pkey = NULL;
  26394. /* Qt unit test case : rsa pkcs8 key */
  26395. file = XFOPEN(fname_rsa_p8, "rb");
  26396. AssertTrue((file != XBADFILE));
  26397. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  26398. sz = XFTELL(file);
  26399. XREWIND(file);
  26400. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  26401. if (buf)
  26402. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  26403. XFCLOSE(file);
  26404. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  26405. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  26406. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  26407. BIO_free(bio);
  26408. bio = NULL;
  26409. AssertNotNull(pkey3 = EVP_PKEY_new());
  26410. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  26411. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  26412. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  26413. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  26414. #else
  26415. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  26416. #endif
  26417. RSA_free(rsa_key);
  26418. EVP_PKEY_free(pkey3);
  26419. EVP_PKEY_free(pkey);
  26420. pkey = NULL;
  26421. }
  26422. #endif
  26423. /* test loading ECC key using BIO */
  26424. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  26425. {
  26426. XFILE file;
  26427. const char* fname = "./certs/ecc-key.pem";
  26428. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  26429. size_t sz;
  26430. byte* buf;
  26431. EVP_PKEY* pkey2;
  26432. EVP_PKEY* pkey3;
  26433. EC_KEY* ec_key;
  26434. int nid = 0;
  26435. file = XFOPEN(fname, "rb");
  26436. AssertTrue((file != XBADFILE));
  26437. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  26438. sz = XFTELL(file);
  26439. XREWIND(file);
  26440. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  26441. if (buf)
  26442. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  26443. XFCLOSE(file);
  26444. /* Test using BIO new mem and loading PEM private key */
  26445. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  26446. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  26447. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  26448. BIO_free(bio);
  26449. bio = NULL;
  26450. AssertNotNull(pkey2 = EVP_PKEY_new());
  26451. AssertNotNull(pkey3 = EVP_PKEY_new());
  26452. pkey2->type = EVP_PKEY_EC;
  26453. /* Test parameter copy */
  26454. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  26455. /* Qt unit test case 1*/
  26456. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  26457. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  26458. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  26459. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  26460. #else
  26461. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  26462. #endif
  26463. /* Test default digest */
  26464. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  26465. AssertIntEQ(nid, NID_sha256);
  26466. EC_KEY_free(ec_key);
  26467. EVP_PKEY_free(pkey3);
  26468. EVP_PKEY_free(pkey2);
  26469. EVP_PKEY_free(pkey);
  26470. pkey = NULL;
  26471. /* Qt unit test case ec pkcs8 key */
  26472. file = XFOPEN(fname_ecc_p8, "rb");
  26473. AssertTrue((file != XBADFILE));
  26474. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  26475. sz = XFTELL(file);
  26476. XREWIND(file);
  26477. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  26478. if (buf)
  26479. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  26480. XFCLOSE(file);
  26481. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  26482. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  26483. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  26484. BIO_free(bio);
  26485. bio = NULL;
  26486. AssertNotNull(pkey3 = EVP_PKEY_new());
  26487. /* Qt unit test case */
  26488. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  26489. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  26490. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  26491. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  26492. #else
  26493. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  26494. #endif
  26495. EC_KEY_free(ec_key);
  26496. EVP_PKEY_free(pkey3);
  26497. EVP_PKEY_free(pkey);
  26498. pkey = NULL;
  26499. }
  26500. #endif
  26501. #if !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN))
  26502. {
  26503. #define BIO_PEM_TEST_CHAR 'a'
  26504. EVP_PKEY* pkey2 = NULL;
  26505. unsigned char extra[10];
  26506. int i;
  26507. BIO* pub_bio = NULL;
  26508. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  26509. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  26510. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  26511. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  26512. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  26513. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  26514. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  26515. &server_key, (long)sizeof_server_key_der_2048));
  26516. AssertNull(pkey);
  26517. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26518. &server_key, (long)sizeof_server_key_der_2048));
  26519. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  26520. WOLFSSL_SUCCESS);
  26521. AssertIntGT(BIO_pending(bio), 0);
  26522. AssertIntEQ(BIO_pending(bio), 1679);
  26523. /* Check if the pubkey API writes only the public key */
  26524. #ifdef WOLFSSL_KEY_GEN
  26525. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  26526. AssertIntGT(BIO_pending(pub_bio), 0);
  26527. /* Previously both the private key and the pubkey calls would write
  26528. * out the private key and the PEM header was the only difference.
  26529. * The public PEM should be significantly shorter than the
  26530. * private key versison. */
  26531. AssertIntEQ(BIO_pending(pub_bio), 451);
  26532. #endif
  26533. /* test creating new EVP_PKEY with good args */
  26534. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  26535. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  26536. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  26537. /* test of reuse of EVP_PKEY */
  26538. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  26539. AssertIntEQ(BIO_pending(bio), 0);
  26540. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  26541. SSL_SUCCESS);
  26542. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  26543. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  26544. AssertNotNull(pkey);
  26545. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  26546. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  26547. }
  26548. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  26549. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  26550. for (i = 0; i < 10; i++) {
  26551. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  26552. }
  26553. BIO_free(pub_bio);
  26554. BIO_free(bio);
  26555. bio = NULL;
  26556. EVP_PKEY_free(pkey);
  26557. pkey = NULL;
  26558. EVP_PKEY_free(pkey2);
  26559. }
  26560. #endif
  26561. /* key is DES encrypted */
  26562. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  26563. !defined(NO_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_MD5) && \
  26564. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  26565. {
  26566. XFILE f;
  26567. wc_pem_password_cb* passwd_cb;
  26568. void* passwd_cb_userdata;
  26569. SSL_CTX* ctx;
  26570. char passwd[] = "bad password";
  26571. #ifndef WOLFSSL_NO_TLS12
  26572. #ifndef NO_WOLFSSL_SERVER
  26573. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  26574. #else
  26575. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  26576. #endif
  26577. #else
  26578. #ifndef NO_WOLFSSL_SERVER
  26579. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  26580. #else
  26581. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  26582. #endif
  26583. #endif
  26584. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  26585. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  26586. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  26587. AssertNull(passwd_cb_userdata =
  26588. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  26589. /* fail case with password call back */
  26590. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  26591. (void*)passwd));
  26592. BIO_free(bio);
  26593. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  26594. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  26595. (void*)passwd));
  26596. BIO_free(bio);
  26597. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  26598. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  26599. /* use callback that works */
  26600. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  26601. (void*)"yassl123"));
  26602. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  26603. EVP_PKEY_free(pkey);
  26604. pkey = NULL;
  26605. BIO_free(bio);
  26606. bio = NULL;
  26607. SSL_CTX_free(ctx);
  26608. }
  26609. #endif /* !defined(NO_DES3) */
  26610. #endif /* !NO_BIO */
  26611. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  26612. {
  26613. unsigned char buf[2048];
  26614. size_t bytes;
  26615. XFILE f;
  26616. SSL_CTX* ctx;
  26617. #ifndef WOLFSSL_NO_TLS12
  26618. #ifndef NO_WOLFSSL_SERVER
  26619. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  26620. #else
  26621. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  26622. #endif
  26623. #else
  26624. #ifndef NO_WOLFSSL_SERVER
  26625. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  26626. #else
  26627. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  26628. #endif
  26629. #endif
  26630. f = XFOPEN("./certs/ecc-key.der", "rb");
  26631. AssertTrue((f != XBADFILE));
  26632. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  26633. XFCLOSE(f);
  26634. server_key = buf;
  26635. pkey = NULL;
  26636. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  26637. AssertNull(pkey);
  26638. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  26639. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  26640. EVP_PKEY_free(pkey);
  26641. pkey = NULL;
  26642. SSL_CTX_free(ctx);
  26643. }
  26644. #endif
  26645. printf(resultFmt, passed);
  26646. #ifndef NO_BIO
  26647. (void)bio;
  26648. #endif
  26649. (void)pkey;
  26650. (void)server_key;
  26651. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  26652. }
  26653. #ifndef NO_BIO
  26654. static void test_wolfSSL_PEM_bio_RSAKey(void)
  26655. {
  26656. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  26657. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  26658. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  26659. RSA* rsa = NULL;
  26660. BIO* bio = NULL;
  26661. printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
  26662. /* PrivateKey */
  26663. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  26664. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  26665. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  26666. AssertIntEQ(RSA_size(rsa), 256);
  26667. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  26668. NULL), WOLFSSL_FAILURE);
  26669. BIO_free(bio);
  26670. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  26671. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  26672. NULL), WOLFSSL_SUCCESS);
  26673. BIO_free(bio);
  26674. RSA_free(rsa);
  26675. /* PUBKEY */
  26676. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  26677. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  26678. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  26679. AssertIntEQ(RSA_size(rsa), 256);
  26680. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  26681. BIO_free(bio);
  26682. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  26683. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  26684. BIO_free(bio);
  26685. RSA_free(rsa);
  26686. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  26687. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  26688. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  26689. BIO_free(bio);
  26690. RSA_free(rsa);
  26691. #ifdef HAVE_ECC
  26692. /* ensure that non-rsa keys do not work */
  26693. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  26694. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  26695. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  26696. BIO_free(bio);
  26697. RSA_free(rsa);
  26698. #endif /* HAVE_ECC */
  26699. printf(resultFmt, passed);
  26700. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  26701. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  26702. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  26703. }
  26704. static void test_wolfSSL_PEM_RSAPrivateKey(void)
  26705. {
  26706. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  26707. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26708. RSA* rsa = NULL;
  26709. RSA* rsa_dup = NULL;
  26710. BIO* bio = NULL;
  26711. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  26712. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  26713. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  26714. AssertIntEQ(RSA_size(rsa), 256);
  26715. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  26716. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  26717. AssertPtrNE(rsa_dup, rsa);
  26718. #endif
  26719. /* test if valgrind complains about unreleased memory */
  26720. RSA_up_ref(rsa);
  26721. RSA_free(rsa);
  26722. BIO_free(bio);
  26723. RSA_free(rsa);
  26724. RSA_free(rsa_dup);
  26725. #ifdef HAVE_ECC
  26726. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  26727. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  26728. BIO_free(bio);
  26729. #endif /* HAVE_ECC */
  26730. printf(resultFmt, passed);
  26731. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  26732. }
  26733. static void test_wolfSSL_PEM_bio_DSAKey(void)
  26734. {
  26735. #ifndef HAVE_SELFTEST
  26736. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  26737. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  26738. DSA* dsa = NULL;
  26739. BIO* bio = NULL;
  26740. printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
  26741. /* PrivateKey */
  26742. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  26743. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  26744. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  26745. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  26746. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  26747. WOLFSSL_FAILURE);
  26748. BIO_free(bio);
  26749. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  26750. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  26751. WOLFSSL_SUCCESS);
  26752. BIO_free(bio);
  26753. DSA_free(dsa);
  26754. /* PUBKEY */
  26755. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  26756. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  26757. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  26758. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  26759. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  26760. BIO_free(bio);
  26761. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  26762. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  26763. BIO_free(bio);
  26764. DSA_free(dsa);
  26765. #ifdef HAVE_ECC
  26766. /* ensure that non-dsa keys do not work */
  26767. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  26768. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  26769. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  26770. BIO_free(bio);
  26771. DSA_free(dsa);
  26772. #endif /* HAVE_ECC */
  26773. printf(resultFmt, passed);
  26774. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  26775. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  26776. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  26777. #endif /* HAVE_SELFTEST */
  26778. }
  26779. static void test_wolfSSL_PEM_bio_ECKey(void)
  26780. {
  26781. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  26782. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  26783. EC_KEY* ec = NULL;
  26784. BIO* bio = NULL;
  26785. printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
  26786. /* PrivateKey */
  26787. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  26788. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  26789. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  26790. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  26791. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  26792. NULL),WOLFSSL_FAILURE);
  26793. BIO_free(bio);
  26794. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  26795. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  26796. NULL), WOLFSSL_SUCCESS);
  26797. BIO_free(bio);
  26798. EC_KEY_free(ec);
  26799. /* PUBKEY */
  26800. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  26801. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  26802. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  26803. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  26804. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  26805. BIO_free(bio);
  26806. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  26807. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  26808. BIO_free(bio);
  26809. EC_KEY_free(ec);
  26810. #ifndef NO_RSA
  26811. /* ensure that non-ec keys do not work */
  26812. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  26813. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  26814. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  26815. BIO_free(bio);
  26816. EC_KEY_free(ec);
  26817. #endif /* HAVE_ECC */
  26818. printf(resultFmt, passed);
  26819. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  26820. }
  26821. static void test_wolfSSL_PEM_PUBKEY(void)
  26822. {
  26823. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  26824. BIO* bio = NULL;
  26825. EVP_PKEY* pkey = NULL;
  26826. /* test creating new EVP_PKEY with bad arg */
  26827. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  26828. /* test loading ECC key using BIO */
  26829. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  26830. {
  26831. XFILE file;
  26832. const char* fname = "./certs/ecc-client-keyPub.pem";
  26833. size_t sz;
  26834. byte* buf;
  26835. EVP_PKEY* pkey2;
  26836. EC_KEY* ec_key;
  26837. file = XFOPEN(fname, "rb");
  26838. AssertTrue((file != XBADFILE));
  26839. XFSEEK(file, 0, XSEEK_END);
  26840. sz = XFTELL(file);
  26841. XREWIND(file);
  26842. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  26843. if (buf)
  26844. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  26845. XFCLOSE(file);
  26846. /* Test using BIO new mem and loading PEM private key */
  26847. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  26848. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  26849. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  26850. BIO_free(bio);
  26851. bio = NULL;
  26852. /* Qt unit test case*/
  26853. AssertNotNull(pkey2 = EVP_PKEY_new());
  26854. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  26855. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  26856. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  26857. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  26858. #else
  26859. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  26860. #endif
  26861. EC_KEY_free(ec_key);
  26862. EVP_PKEY_free(pkey2);
  26863. EVP_PKEY_free(pkey);
  26864. pkey = NULL;
  26865. }
  26866. #endif
  26867. (void)bio;
  26868. (void)pkey;
  26869. #endif
  26870. }
  26871. #endif /* !NO_BIO */
  26872. static void test_DSA_do_sign_verify(void)
  26873. {
  26874. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  26875. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  26876. !defined(NO_DSA)
  26877. unsigned char digest[WC_SHA_DIGEST_SIZE];
  26878. DSA_SIG* sig;
  26879. DSA* dsa;
  26880. word32 bytes;
  26881. byte sigBin[DSA_SIG_SIZE];
  26882. int dsacheck;
  26883. #ifdef USE_CERT_BUFFERS_1024
  26884. byte tmp[ONEK_BUF];
  26885. XMEMSET(tmp, 0, sizeof(tmp));
  26886. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  26887. bytes = sizeof_dsa_key_der_1024;
  26888. #elif defined(USE_CERT_BUFFERS_2048)
  26889. byte tmp[TWOK_BUF];
  26890. XMEMSET(tmp, 0, sizeof(tmp));
  26891. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  26892. bytes = sizeof_dsa_key_der_2048;
  26893. #else
  26894. byte tmp[TWOK_BUF];
  26895. XMEMSET(tmp, 0, sizeof(tmp));
  26896. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  26897. if (fp == XBADFILE) {
  26898. return WOLFSSL_BAD_FILE;
  26899. }
  26900. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  26901. XFCLOSE(fp);
  26902. #endif /* END USE_CERT_BUFFERS_1024 */
  26903. printf(testingFmt, "DSA_do_sign_verify()");
  26904. XMEMSET(digest, 202, sizeof(digest));
  26905. AssertNotNull(dsa = DSA_new());
  26906. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  26907. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  26908. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  26909. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  26910. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  26911. DSA_SIG_free(sig);
  26912. DSA_free(dsa);
  26913. #endif
  26914. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  26915. }
  26916. static void test_wolfSSL_tmp_dh(void)
  26917. {
  26918. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26919. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  26920. byte buff[6000];
  26921. char file[] = "./certs/dsaparams.pem";
  26922. XFILE f;
  26923. int bytes;
  26924. DSA* dsa;
  26925. DH* dh;
  26926. #if defined(WOLFSSL_DH_EXTRA) && \
  26927. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  26928. DH* dh2;
  26929. #endif
  26930. BIO* bio;
  26931. SSL* ssl;
  26932. SSL_CTX* ctx;
  26933. printf(testingFmt, "wolfSSL_tmp_dh()");
  26934. #ifndef NO_WOLFSSL_SERVER
  26935. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  26936. #else
  26937. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  26938. #endif
  26939. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  26940. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  26941. AssertNotNull(ssl = SSL_new(ctx));
  26942. f = XFOPEN(file, "rb");
  26943. AssertTrue((f != XBADFILE));
  26944. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  26945. XFCLOSE(f);
  26946. bio = BIO_new_mem_buf((void*)buff, bytes);
  26947. AssertNotNull(bio);
  26948. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  26949. AssertNotNull(dsa);
  26950. dh = wolfSSL_DSA_dup_DH(dsa);
  26951. AssertNotNull(dh);
  26952. #if defined(WOLFSSL_DH_EXTRA) && \
  26953. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  26954. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  26955. #endif
  26956. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  26957. #ifndef NO_WOLFSSL_SERVER
  26958. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  26959. #else
  26960. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  26961. #endif
  26962. BIO_free(bio);
  26963. DSA_free(dsa);
  26964. DH_free(dh);
  26965. #if defined(WOLFSSL_DH_EXTRA) && \
  26966. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  26967. DH_free(dh2);
  26968. #endif
  26969. SSL_free(ssl);
  26970. SSL_CTX_free(ctx);
  26971. printf(resultFmt, passed);
  26972. #endif
  26973. }
  26974. static void test_wolfSSL_ctrl(void)
  26975. {
  26976. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  26977. byte buff[6000];
  26978. BIO* bio;
  26979. int bytes;
  26980. BUF_MEM* ptr = NULL;
  26981. printf(testingFmt, "wolfSSL_crtl()");
  26982. bytes = sizeof(buff);
  26983. bio = BIO_new_mem_buf((void*)buff, bytes);
  26984. AssertNotNull(bio);
  26985. AssertNotNull(BIO_s_socket());
  26986. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  26987. /* needs tested after stubs filled out @TODO
  26988. SSL_ctrl
  26989. SSL_CTX_ctrl
  26990. */
  26991. BIO_free(bio);
  26992. printf(resultFmt, passed);
  26993. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  26994. }
  26995. static void test_wolfSSL_EVP_PKEY_new_mac_key(void)
  26996. {
  26997. #ifdef OPENSSL_EXTRA
  26998. static const unsigned char pw[] = "password";
  26999. static const int pwSz = sizeof(pw) - 1;
  27000. size_t checkPwSz = 0;
  27001. const unsigned char* checkPw = NULL;
  27002. WOLFSSL_EVP_PKEY* key = NULL;
  27003. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  27004. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  27005. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  27006. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  27007. if (key) {
  27008. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  27009. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  27010. AssertIntEQ(key->pkey_sz, pwSz);
  27011. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  27012. }
  27013. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  27014. AssertIntEQ((int)checkPwSz, pwSz);
  27015. if (checkPw) {
  27016. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  27017. }
  27018. wolfSSL_EVP_PKEY_free(key);
  27019. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  27020. if (key) {
  27021. AssertIntEQ(key->pkey_sz, 0);
  27022. }
  27023. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  27024. (void)checkPw;
  27025. AssertIntEQ((int)checkPwSz, 0);
  27026. wolfSSL_EVP_PKEY_free(key);
  27027. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  27028. if (key) {
  27029. AssertIntEQ(key->pkey_sz, 0);
  27030. }
  27031. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  27032. (void)checkPw;
  27033. AssertIntEQ((int)checkPwSz, 0);
  27034. wolfSSL_EVP_PKEY_free(key);
  27035. printf(resultFmt, passed);
  27036. #endif /* OPENSSL_EXTRA */
  27037. }
  27038. static void test_wolfSSL_EVP_Digest(void)
  27039. {
  27040. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  27041. const char* in = "abc";
  27042. int inLen = (int)XSTRLEN(in);
  27043. byte out[WC_SHA256_DIGEST_SIZE];
  27044. unsigned int outLen;
  27045. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  27046. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  27047. "\x15\xAD";
  27048. printf(testingFmt, "wolfSSL_EVP_Digest()");
  27049. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  27050. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  27051. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  27052. printf(resultFmt, passed);
  27053. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  27054. }
  27055. static void test_wolfSSL_EVP_Digest_all(void)
  27056. {
  27057. #ifdef OPENSSL_EXTRA
  27058. const char* digests[] = {
  27059. #ifndef NO_MD5
  27060. "MD5",
  27061. #endif
  27062. #ifndef NO_SHA
  27063. "SHA",
  27064. #endif
  27065. #ifdef WOLFSSL_SHA224
  27066. "SHA224",
  27067. #endif
  27068. #ifndef NO_SHA256
  27069. "SHA256",
  27070. #endif
  27071. #ifdef WOLFSSL_SHA384
  27072. "SHA384",
  27073. #endif
  27074. #ifdef WOLFSSL_SHA512
  27075. "SHA512",
  27076. #endif
  27077. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  27078. "SHA512_224",
  27079. #endif
  27080. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  27081. "SHA512_256",
  27082. #endif
  27083. #ifdef WOLFSSL_SHA3
  27084. #ifndef WOLFSSL_NOSHA3_224
  27085. "SHA3_224",
  27086. #endif
  27087. #ifndef WOLFSSL_NOSHA3_256
  27088. "SHA3_256",
  27089. #endif
  27090. "SHA3_384",
  27091. #ifndef WOLFSSL_NOSHA3_512
  27092. "SHA3_512",
  27093. #endif
  27094. #endif /* WOLFSSL_SHA3 */
  27095. NULL
  27096. };
  27097. const char** d;
  27098. const unsigned char in[] = "abc";
  27099. int inLen = XSTR_SIZEOF(in);
  27100. byte out[WC_MAX_DIGEST_SIZE];
  27101. unsigned int outLen;
  27102. printf(testingFmt, "wolfSSL_EVP_Digest_all");
  27103. for (d = digests; *d != NULL; d++) {
  27104. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  27105. AssertIntGT(outLen, 0);
  27106. AssertIntEQ(EVP_MD_size(*d), outLen);
  27107. }
  27108. printf(resultFmt, passed);
  27109. #endif
  27110. }
  27111. static void test_wolfSSL_EVP_MD_size(void)
  27112. {
  27113. #ifdef OPENSSL_EXTRA
  27114. WOLFSSL_EVP_MD_CTX mdCtx;
  27115. printf(testingFmt, "wolfSSL_EVP_MD_size()");
  27116. #ifdef WOLFSSL_SHA3
  27117. #ifndef WOLFSSL_NOSHA3_224
  27118. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27119. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  27120. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  27121. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  27122. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27123. #endif
  27124. #ifndef WOLFSSL_NOSHA3_256
  27125. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27126. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  27127. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  27128. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  27129. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27130. #endif
  27131. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27132. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  27133. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  27134. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  27135. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27136. #ifndef WOLFSSL_NOSHA3_512
  27137. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27138. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  27139. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  27140. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  27141. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27142. #endif
  27143. #endif /* WOLFSSL_SHA3 */
  27144. #ifndef NO_SHA256
  27145. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27146. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  27147. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  27148. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  27149. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  27150. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  27151. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27152. #endif
  27153. #ifndef NO_MD5
  27154. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27155. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  27156. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  27157. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  27158. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  27159. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  27160. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27161. #endif
  27162. #ifdef WOLFSSL_SHA224
  27163. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27164. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  27165. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  27166. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  27167. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  27168. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  27169. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27170. #endif
  27171. #ifdef WOLFSSL_SHA384
  27172. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27173. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  27174. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  27175. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  27176. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  27177. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  27178. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27179. #endif
  27180. #ifdef WOLFSSL_SHA512
  27181. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27182. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  27183. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  27184. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  27185. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  27186. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  27187. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27188. #endif
  27189. #ifndef NO_SHA
  27190. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27191. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  27192. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  27193. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  27194. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  27195. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  27196. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27197. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27198. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  27199. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  27200. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  27201. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  27202. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  27203. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27204. #endif
  27205. /* error case */
  27206. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27207. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  27208. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  27209. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  27210. /* Cleanup is valid on uninit'ed struct */
  27211. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27212. printf(resultFmt, passed);
  27213. #endif /* OPENSSL_EXTRA */
  27214. }
  27215. static void test_wolfSSL_EVP_MD_pkey_type(void)
  27216. {
  27217. #ifdef OPENSSL_EXTRA
  27218. const WOLFSSL_EVP_MD* md;
  27219. printf(testingFmt, "test_wolfSSL_EVP_MD_pkey_type()");
  27220. #ifndef NO_MD5
  27221. AssertNotNull(md = EVP_md5());
  27222. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  27223. #endif
  27224. #ifndef NO_SHA
  27225. AssertNotNull(md = EVP_sha1());
  27226. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  27227. #endif
  27228. #ifdef WOLFSSL_SHA224
  27229. AssertNotNull(md = EVP_sha224());
  27230. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  27231. #endif
  27232. AssertNotNull(md = EVP_sha256());
  27233. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  27234. #ifdef WOLFSSL_SHA384
  27235. AssertNotNull(md = EVP_sha384());
  27236. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  27237. #endif
  27238. #ifdef WOLFSSL_SHA512
  27239. AssertNotNull(md = EVP_sha512());
  27240. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  27241. #endif
  27242. printf(resultFmt, passed);
  27243. #endif
  27244. }
  27245. #ifdef OPENSSL_EXTRA
  27246. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  27247. size_t testKeySz, const char* testData, size_t testDataSz,
  27248. const byte* testResult, size_t testResultSz)
  27249. {
  27250. unsigned char check[WC_MAX_DIGEST_SIZE];
  27251. size_t checkSz = -1;
  27252. WOLFSSL_EVP_PKEY* key;
  27253. WOLFSSL_EVP_MD_CTX mdCtx;
  27254. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  27255. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  27256. testKey, (int)testKeySz));
  27257. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27258. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  27259. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  27260. (unsigned int)testDataSz), 1);
  27261. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  27262. AssertIntEQ((int)checkSz, (int)testResultSz);
  27263. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27264. AssertIntEQ((int)checkSz,(int)testResultSz);
  27265. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  27266. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27267. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  27268. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  27269. (unsigned int)testDataSz), 1);
  27270. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  27271. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27272. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27273. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  27274. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  27275. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  27276. AssertIntEQ((int)checkSz, (int)testResultSz);
  27277. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27278. AssertIntEQ((int)checkSz,(int)testResultSz);
  27279. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  27280. (unsigned int)testDataSz - 4), 1);
  27281. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27282. AssertIntEQ((int)checkSz,(int)testResultSz);
  27283. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  27284. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27285. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  27286. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  27287. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  27288. (unsigned int)testDataSz - 4), 1);
  27289. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  27290. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27291. wolfSSL_EVP_PKEY_free(key);
  27292. }
  27293. #endif
  27294. static void test_wolfSSL_EVP_MD_hmac_signing(void)
  27295. {
  27296. #ifdef OPENSSL_EXTRA
  27297. static const unsigned char testKey[] =
  27298. {
  27299. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  27300. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  27301. 0x0b, 0x0b, 0x0b, 0x0b
  27302. };
  27303. static const char testData[] = "Hi There";
  27304. #ifdef WOLFSSL_SHA224
  27305. static const unsigned char testResultSha224[] =
  27306. {
  27307. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  27308. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  27309. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  27310. 0x53, 0x68, 0x4b, 0x22
  27311. };
  27312. #endif
  27313. #ifndef NO_SHA256
  27314. static const unsigned char testResultSha256[] =
  27315. {
  27316. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  27317. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  27318. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  27319. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  27320. };
  27321. #endif
  27322. #ifdef WOLFSSL_SHA384
  27323. static const unsigned char testResultSha384[] =
  27324. {
  27325. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  27326. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  27327. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  27328. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  27329. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  27330. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  27331. };
  27332. #endif
  27333. #ifdef WOLFSSL_SHA512
  27334. static const unsigned char testResultSha512[] =
  27335. {
  27336. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  27337. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  27338. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  27339. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  27340. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  27341. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  27342. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  27343. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  27344. };
  27345. #endif
  27346. #ifdef WOLFSSL_SHA3
  27347. #ifndef WOLFSSL_NOSHA3_224
  27348. static const unsigned char testResultSha3_224[] =
  27349. {
  27350. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  27351. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  27352. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  27353. 0xf3, 0xc8, 0x60, 0xf7
  27354. };
  27355. #endif
  27356. #ifndef WOLFSSL_NOSHA3_256
  27357. static const unsigned char testResultSha3_256[] =
  27358. {
  27359. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  27360. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  27361. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  27362. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  27363. };
  27364. #endif
  27365. #ifndef WOLFSSL_NOSHA3_384
  27366. static const unsigned char testResultSha3_384[] =
  27367. {
  27368. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  27369. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  27370. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  27371. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  27372. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  27373. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  27374. };
  27375. #endif
  27376. #ifndef WOLFSSL_NOSHA3_512
  27377. static const unsigned char testResultSha3_512[] =
  27378. {
  27379. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  27380. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  27381. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  27382. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  27383. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  27384. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  27385. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  27386. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  27387. };
  27388. #endif
  27389. #endif
  27390. #ifndef NO_SHA256
  27391. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  27392. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  27393. #endif
  27394. #ifdef WOLFSSL_SHA224
  27395. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  27396. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  27397. #endif
  27398. #ifdef WOLFSSL_SHA384
  27399. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  27400. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  27401. #endif
  27402. #ifdef WOLFSSL_SHA512
  27403. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  27404. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  27405. #endif
  27406. #ifdef WOLFSSL_SHA3
  27407. #ifndef WOLFSSL_NOSHA3_224
  27408. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  27409. testData, XSTRLEN(testData), testResultSha3_224,
  27410. sizeof(testResultSha3_224));
  27411. #endif
  27412. #ifndef WOLFSSL_NOSHA3_256
  27413. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  27414. testData, XSTRLEN(testData), testResultSha3_256,
  27415. sizeof(testResultSha3_256));
  27416. #endif
  27417. #ifndef WOLFSSL_NOSHA3_384
  27418. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  27419. testData, XSTRLEN(testData), testResultSha3_384,
  27420. sizeof(testResultSha3_384));
  27421. #endif
  27422. #ifndef WOLFSSL_NOSHA3_512
  27423. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  27424. testData, XSTRLEN(testData), testResultSha3_512,
  27425. sizeof(testResultSha3_512));
  27426. #endif
  27427. #endif
  27428. printf(resultFmt, passed);
  27429. #endif /* OPENSSL_EXTRA */
  27430. }
  27431. static void test_wolfSSL_EVP_MD_rsa_signing(void)
  27432. {
  27433. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  27434. defined(USE_CERT_BUFFERS_2048)
  27435. WOLFSSL_EVP_PKEY* privKey;
  27436. WOLFSSL_EVP_PKEY* pubKey;
  27437. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  27438. const char testData[] = "Hi There";
  27439. WOLFSSL_EVP_MD_CTX mdCtx;
  27440. size_t checkSz = -1;
  27441. int sz = 2048 / 8;
  27442. const unsigned char* cp;
  27443. const unsigned char* p;
  27444. unsigned char check[2048/8];
  27445. size_t i;
  27446. int paddings[] = {
  27447. RSA_PKCS1_PADDING,
  27448. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  27449. RSA_PKCS1_PSS_PADDING,
  27450. #endif
  27451. };
  27452. printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
  27453. cp = client_key_der_2048;
  27454. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  27455. sizeof_client_key_der_2048)));
  27456. p = client_keypub_der_2048;
  27457. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  27458. sizeof_client_keypub_der_2048)));
  27459. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27460. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  27461. NULL, privKey), 1);
  27462. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  27463. (unsigned int)XSTRLEN(testData)), 1);
  27464. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  27465. AssertIntEQ((int)checkSz, sz);
  27466. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27467. AssertIntEQ((int)checkSz,sz);
  27468. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27469. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27470. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  27471. NULL, pubKey), 1);
  27472. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  27473. (unsigned int)XSTRLEN(testData)),
  27474. 1);
  27475. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  27476. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27477. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27478. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  27479. NULL, privKey), 1);
  27480. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  27481. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  27482. AssertIntEQ((int)checkSz, sz);
  27483. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27484. AssertIntEQ((int)checkSz, sz);
  27485. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  27486. (unsigned int)XSTRLEN(testData) - 4), 1);
  27487. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27488. AssertIntEQ((int)checkSz, sz);
  27489. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27490. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27491. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  27492. NULL, pubKey), 1);
  27493. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  27494. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  27495. (unsigned int)XSTRLEN(testData) - 4),
  27496. 1);
  27497. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  27498. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27499. /* Check all signing padding types */
  27500. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  27501. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27502. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  27503. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  27504. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  27505. paddings[i]), 1);
  27506. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  27507. (unsigned int)XSTRLEN(testData)), 1);
  27508. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  27509. AssertIntEQ((int)checkSz, sz);
  27510. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27511. AssertIntEQ((int)checkSz,sz);
  27512. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27513. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27514. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  27515. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  27516. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  27517. paddings[i]), 1);
  27518. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  27519. (unsigned int)XSTRLEN(testData)), 1);
  27520. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  27521. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27522. }
  27523. wolfSSL_EVP_PKEY_free(pubKey);
  27524. wolfSSL_EVP_PKEY_free(privKey);
  27525. printf(resultFmt, passed);
  27526. #endif
  27527. }
  27528. static void test_wolfSSL_EVP_MD_ecc_signing(void)
  27529. {
  27530. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27531. WOLFSSL_EVP_PKEY* privKey;
  27532. WOLFSSL_EVP_PKEY* pubKey;
  27533. const char testData[] = "Hi There";
  27534. WOLFSSL_EVP_MD_CTX mdCtx;
  27535. size_t checkSz = -1;
  27536. const unsigned char* cp;
  27537. const unsigned char* p;
  27538. unsigned char check[2048/8];
  27539. printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
  27540. cp = ecc_clikey_der_256;
  27541. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  27542. sizeof_ecc_clikey_der_256);
  27543. AssertNotNull(privKey);
  27544. p = ecc_clikeypub_der_256;
  27545. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  27546. sizeof_ecc_clikeypub_der_256)));
  27547. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27548. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  27549. NULL, privKey), 1);
  27550. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  27551. (unsigned int)XSTRLEN(testData)), 1);
  27552. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  27553. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27554. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27555. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27556. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  27557. NULL, pubKey), 1);
  27558. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  27559. (unsigned int)XSTRLEN(testData)),
  27560. 1);
  27561. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  27562. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27563. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27564. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  27565. NULL, privKey), 1);
  27566. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  27567. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  27568. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27569. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  27570. (unsigned int)XSTRLEN(testData) - 4), 1);
  27571. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  27572. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27573. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  27574. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  27575. NULL, pubKey), 1);
  27576. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  27577. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  27578. (unsigned int)XSTRLEN(testData) - 4),
  27579. 1);
  27580. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  27581. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  27582. wolfSSL_EVP_PKEY_free(pubKey);
  27583. wolfSSL_EVP_PKEY_free(privKey);
  27584. printf(resultFmt, passed);
  27585. #endif
  27586. }
  27587. static void test_wolfSSL_CTX_add_extra_chain_cert(void)
  27588. {
  27589. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27590. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  27591. char caFile[] = "./certs/client-ca.pem";
  27592. char clientFile[] = "./certs/client-cert.pem";
  27593. SSL_CTX* ctx;
  27594. X509* x509;
  27595. BIO *bio = NULL;
  27596. X509 *cert = NULL;
  27597. X509 *ca;
  27598. STACK_OF(X509) *chain = NULL;
  27599. STACK_OF(X509) *chain2 = NULL;
  27600. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  27601. #ifndef NO_WOLFSSL_SERVER
  27602. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27603. #else
  27604. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27605. #endif
  27606. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  27607. AssertNotNull(x509);
  27608. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  27609. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  27610. AssertNotNull(x509);
  27611. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  27612. /* additional test of getting EVP_PKEY key size from X509
  27613. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  27614. * allowed with user RSA */
  27615. {
  27616. EVP_PKEY* pkey;
  27617. #if defined(HAVE_ECC)
  27618. X509* ecX509;
  27619. #endif /* HAVE_ECC */
  27620. AssertNotNull(pkey = X509_get_pubkey(x509));
  27621. /* current RSA key is 2048 bit (256 bytes) */
  27622. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  27623. EVP_PKEY_free(pkey);
  27624. #if defined(HAVE_ECC)
  27625. #if defined(USE_CERT_BUFFERS_256)
  27626. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  27627. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  27628. SSL_FILETYPE_ASN1));
  27629. #else
  27630. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  27631. SSL_FILETYPE_PEM));
  27632. #endif
  27633. pkey = X509_get_pubkey(ecX509);
  27634. AssertNotNull(pkey);
  27635. /* current ECC key is 256 bit (32 bytes) */
  27636. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  27637. X509_free(ecX509);
  27638. EVP_PKEY_free(pkey);
  27639. #endif /* HAVE_ECC */
  27640. }
  27641. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  27642. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  27643. #ifdef WOLFSSL_ENCRYPTED_KEYS
  27644. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  27645. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  27646. #endif
  27647. SSL_CTX_free(ctx);
  27648. #ifndef NO_WOLFSSL_SERVER
  27649. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27650. #else
  27651. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27652. #endif
  27653. /* Test haproxy use case */
  27654. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  27655. /* Read Certificate */
  27656. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  27657. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  27658. AssertNotNull(chain = sk_X509_new_null());
  27659. AssertIntEQ(sk_X509_push(chain, ca), 1);
  27660. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  27661. AssertNotNull(ca = sk_X509_shift(chain2));
  27662. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  27663. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  27664. BIO_free(bio);
  27665. X509_free(cert);
  27666. sk_X509_pop_free(chain, X509_free);
  27667. sk_X509_pop_free(chain2, X509_free);
  27668. SSL_CTX_free(ctx);
  27669. printf(resultFmt, passed);
  27670. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27671. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  27672. }
  27673. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  27674. static void test_wolfSSL_ERR_peek_last_error_line(void)
  27675. {
  27676. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27677. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  27678. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  27679. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  27680. tcp_ready ready;
  27681. func_args client_args;
  27682. func_args server_args;
  27683. #ifndef SINGLE_THREADED
  27684. THREAD_TYPE serverThread;
  27685. #endif
  27686. callback_functions client_cb;
  27687. callback_functions server_cb;
  27688. int line = 0;
  27689. int flag = ERR_TXT_STRING;
  27690. const char* file = NULL;
  27691. const char* data = NULL;
  27692. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  27693. /* create a failed connection and inspect the error */
  27694. #ifdef WOLFSSL_TIRTOS
  27695. fdOpenSession(Task_self());
  27696. #endif
  27697. XMEMSET(&client_args, 0, sizeof(func_args));
  27698. XMEMSET(&server_args, 0, sizeof(func_args));
  27699. StartTCP();
  27700. InitTcpReady(&ready);
  27701. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  27702. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  27703. client_cb.method = wolfTLSv1_1_client_method;
  27704. server_cb.method = wolfTLSv1_2_server_method;
  27705. server_args.signal = &ready;
  27706. server_args.callbacks = &server_cb;
  27707. client_args.signal = &ready;
  27708. client_args.callbacks = &client_cb;
  27709. #ifndef SINGLE_THREADED
  27710. start_thread(test_server_nofail, &server_args, &serverThread);
  27711. wait_tcp_ready(&server_args);
  27712. test_client_nofail(&client_args, NULL);
  27713. join_thread(serverThread);
  27714. #endif
  27715. FreeTcpReady(&ready);
  27716. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  27717. AssertNotNull(data);
  27718. /* check clearing error state */
  27719. ERR_remove_state(0);
  27720. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  27721. ERR_peek_last_error_line(NULL, &line);
  27722. AssertIntEQ(line, 0);
  27723. ERR_peek_last_error_line(&file, NULL);
  27724. AssertNull(file);
  27725. /* retry connection to fill error queue */
  27726. XMEMSET(&client_args, 0, sizeof(func_args));
  27727. XMEMSET(&server_args, 0, sizeof(func_args));
  27728. StartTCP();
  27729. InitTcpReady(&ready);
  27730. client_cb.method = wolfTLSv1_1_client_method;
  27731. server_cb.method = wolfTLSv1_2_server_method;
  27732. server_args.signal = &ready;
  27733. server_args.callbacks = &server_cb;
  27734. client_args.signal = &ready;
  27735. client_args.callbacks = &client_cb;
  27736. start_thread(test_server_nofail, &server_args, &serverThread);
  27737. wait_tcp_ready(&server_args);
  27738. test_client_nofail(&client_args, NULL);
  27739. join_thread(serverThread);
  27740. FreeTcpReady(&ready);
  27741. /* check that error code was stored */
  27742. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  27743. ERR_peek_last_error_line(NULL, &line);
  27744. AssertIntNE(line, 0);
  27745. ERR_peek_last_error_line(&file, NULL);
  27746. AssertNotNull(file);
  27747. #ifdef WOLFSSL_TIRTOS
  27748. fdOpenSession(Task_self());
  27749. #endif
  27750. printf(resultFmt, passed);
  27751. printf("\nTesting error print out\n");
  27752. ERR_print_errors_fp(stdout);
  27753. printf("Done testing print out\n\n");
  27754. fflush(stdout);
  27755. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27756. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  27757. }
  27758. #endif
  27759. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27760. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  27761. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  27762. {
  27763. (void) ok;
  27764. (void) ctx;
  27765. printf("ENTER verify_cb\n");
  27766. return SSL_SUCCESS;
  27767. }
  27768. #endif
  27769. static void test_wolfSSL_X509_Name_canon(void)
  27770. {
  27771. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  27772. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  27773. defined(WOLFSSL_CERT_GEN) && \
  27774. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  27775. const long ex_hash1 = 0x0fdb2da4;
  27776. const long ex_hash2 = 0x9f3e8c9e;
  27777. X509_NAME *name = NULL;
  27778. X509 *x509 = NULL;
  27779. FILE* file = NULL;
  27780. unsigned long hash = 0;
  27781. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  27782. byte *pbuf = NULL;
  27783. word32 len = 0;
  27784. (void) ex_hash2;
  27785. printf(testingFmt, "test_wolfSSL_X509_Name_canon()");
  27786. file = XFOPEN(caCertFile, "rb");
  27787. AssertNotNull(file);
  27788. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  27789. AssertNotNull(name = X509_get_issuer_name(x509));
  27790. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  27791. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  27792. hash = (((unsigned long)digest[3] << 24) |
  27793. ((unsigned long)digest[2] << 16) |
  27794. ((unsigned long)digest[1] << 8) |
  27795. ((unsigned long)digest[0]));
  27796. AssertIntEQ(hash, ex_hash1);
  27797. XFCLOSE(file);
  27798. X509_free(x509);
  27799. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  27800. pbuf = NULL;
  27801. file = XFOPEN(cliCertFile, "rb");
  27802. AssertNotNull(file);
  27803. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  27804. AssertNotNull(name = X509_get_issuer_name(x509));
  27805. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  27806. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  27807. hash = (((unsigned long)digest[3] << 24) |
  27808. ((unsigned long)digest[2] << 16) |
  27809. ((unsigned long)digest[1] << 8) |
  27810. ((unsigned long)digest[0]));
  27811. AssertIntEQ(hash, ex_hash2);
  27812. XFCLOSE(file);
  27813. X509_free(x509);
  27814. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  27815. printf(resultFmt, passed);
  27816. #endif
  27817. }
  27818. static void test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  27819. {
  27820. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  27821. const int MAX_DIR = 4;
  27822. const char paths[][32] = {
  27823. "./certs/ed25519",
  27824. "./certs/ecc",
  27825. "./certs/crl",
  27826. "./certs/",
  27827. };
  27828. char CertCrl_path[MAX_FILENAME_SZ];
  27829. char *p;
  27830. X509_STORE* str;
  27831. X509_LOOKUP* lookup;
  27832. WOLFSSL_STACK* sk = NULL;
  27833. int len, total_len, i;
  27834. (void) sk;
  27835. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_hash_dir()");
  27836. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  27837. /* illegal string */
  27838. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  27839. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  27840. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  27841. SSL_FILETYPE_PEM,NULL), 0);
  27842. /* free store */
  27843. X509_STORE_free(str);
  27844. /* short folder string */
  27845. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  27846. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  27847. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  27848. SSL_FILETYPE_PEM,NULL), 1);
  27849. #if defined(WOLFSSL_INT_H)
  27850. /* only available when including internal.h */
  27851. AssertNotNull(sk = lookup->dirs->dir_entry);
  27852. #endif
  27853. /* free store */
  27854. X509_STORE_free(str);
  27855. /* typical function check */
  27856. p = &CertCrl_path[0];
  27857. total_len = 0;
  27858. for(i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  27859. len = (int)XSTRLEN((const char*)&paths[i]);
  27860. total_len += len;
  27861. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  27862. p += len;
  27863. if (i != 0) *(p++) = SEPARATOR_CHAR;
  27864. }
  27865. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  27866. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  27867. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  27868. SSL_FILETYPE_PEM,NULL), 1);
  27869. #if defined(WOLFSSL_INT_H)
  27870. /* only available when including internal.h */
  27871. AssertNotNull(sk = lookup->dirs->dir_entry);
  27872. #endif
  27873. X509_STORE_free(str);
  27874. printf(resultFmt, passed);
  27875. #endif
  27876. }
  27877. static void test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  27878. {
  27879. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  27880. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  27881. defined(WOLFSSL_SIGNER_DER_CERT)
  27882. X509_STORE_CTX* ctx;
  27883. X509_STORE* str;
  27884. X509_LOOKUP* lookup;
  27885. X509* cert1;
  27886. X509* x509Ca;
  27887. X509* x509Svr;
  27888. X509* issuer;
  27889. WOLFSSL_STACK* sk = NULL;
  27890. X509_NAME* caName;
  27891. X509_NAME* issuerName;
  27892. FILE* file1 = NULL;
  27893. int i, cert_count, cmp;
  27894. char der[] = "certs/ca-cert.der";
  27895. #ifdef HAVE_CRL
  27896. char pem[][100] = {
  27897. "./certs/crl/crl.pem",
  27898. "./certs/crl/crl2.pem",
  27899. "./certs/crl/caEccCrl.pem",
  27900. "./certs/crl/eccCliCRL.pem",
  27901. "./certs/crl/eccSrvCRL.pem",
  27902. ""
  27903. };
  27904. #endif
  27905. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_file()");
  27906. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  27907. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  27908. fclose(file1);
  27909. AssertNotNull(ctx = X509_STORE_CTX_new());
  27910. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  27911. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  27912. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  27913. SSL_FILETYPE_PEM,NULL), 1);
  27914. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  27915. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  27916. /* check if CA cert is loaded into the store */
  27917. for (i = 0; i < cert_count; i++) {
  27918. x509Ca = sk_X509_value(sk, i);
  27919. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  27920. }
  27921. AssertNotNull((x509Svr =
  27922. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  27923. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  27924. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  27925. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  27926. AssertNotNull(issuer);
  27927. caName = X509_get_subject_name(x509Ca);
  27928. AssertNotNull(caName);
  27929. issuerName = X509_get_subject_name(issuer);
  27930. AssertNotNull(issuerName);
  27931. cmp = X509_NAME_cmp(caName, issuerName);
  27932. AssertIntEQ(cmp, 0);
  27933. /* load der format */
  27934. X509_free(issuer);
  27935. X509_STORE_CTX_free(ctx);
  27936. X509_STORE_free(str);
  27937. sk_X509_pop_free(sk, NULL);
  27938. X509_free(x509Svr);
  27939. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  27940. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  27941. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  27942. SSL_FILETYPE_ASN1,NULL), 1);
  27943. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  27944. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  27945. /* check if CA cert is loaded into the store */
  27946. for (i = 0; i < cert_count; i++) {
  27947. x509Ca = sk_X509_value(sk, i);
  27948. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  27949. }
  27950. X509_STORE_free(str);
  27951. sk_X509_pop_free(sk, NULL);
  27952. X509_free(cert1);
  27953. #ifdef HAVE_CRL
  27954. AssertNotNull(str = wolfSSL_X509_STORE_new());
  27955. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  27956. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  27957. SSL_FILETYPE_PEM,NULL), 1);
  27958. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  27959. "certs/server-revoked-cert.pem",
  27960. SSL_FILETYPE_PEM,NULL), 1);
  27961. if (str) {
  27962. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  27963. WOLFSSL_FILETYPE_PEM), 1);
  27964. /* since store hasn't yet known the revoked cert*/
  27965. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  27966. "certs/server-revoked-cert.pem",
  27967. WOLFSSL_FILETYPE_PEM), 1);
  27968. }
  27969. for (i = 0; pem[i][0] != '\0'; i++)
  27970. {
  27971. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  27972. SSL_FILETYPE_PEM, NULL), 1);
  27973. }
  27974. if (str) {
  27975. /* since store knows crl list */
  27976. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  27977. "certs/server-revoked-cert.pem",
  27978. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  27979. }
  27980. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  27981. X509_STORE_free(str);
  27982. #endif
  27983. printf(resultFmt, passed);
  27984. #endif
  27985. }
  27986. static void test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  27987. {
  27988. #if defined(OPENSSL_EXTRA)
  27989. printf(testingFmt, "test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup()");
  27990. X509_STORE_CTX_cleanup(NULL);
  27991. X509_STORE_CTX_trusted_stack(NULL, NULL);
  27992. AssertTrue(1); /* to confirm previous call gives no harm */
  27993. printf(resultFmt, passed);
  27994. #endif
  27995. }
  27996. static void test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  27997. {
  27998. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  27999. #ifdef WOLFSSL_SIGNER_DER_CERT
  28000. int cmp;
  28001. #endif
  28002. X509_STORE_CTX* ctx;
  28003. X509_STORE* str;
  28004. X509* x509Ca;
  28005. X509* x509Svr;
  28006. X509* issuer;
  28007. X509_NAME* caName;
  28008. X509_NAME* issuerName;
  28009. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
  28010. AssertNotNull(ctx = X509_STORE_CTX_new());
  28011. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28012. AssertNotNull((x509Ca =
  28013. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  28014. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  28015. AssertNotNull((x509Svr =
  28016. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  28017. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  28018. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  28019. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  28020. AssertNotNull(issuer);
  28021. caName = X509_get_subject_name(x509Ca);
  28022. AssertNotNull(caName);
  28023. issuerName = X509_get_subject_name(issuer);
  28024. #ifdef WOLFSSL_SIGNER_DER_CERT
  28025. AssertNotNull(issuerName);
  28026. cmp = X509_NAME_cmp(caName, issuerName);
  28027. AssertIntEQ(cmp, 0);
  28028. #else
  28029. AssertNotNull(issuerName);
  28030. #endif
  28031. X509_free(issuer);
  28032. X509_STORE_CTX_free(ctx);
  28033. X509_free(x509Svr);
  28034. X509_STORE_free(str);
  28035. X509_free(x509Ca);
  28036. printf(resultFmt, passed);
  28037. #endif
  28038. }
  28039. static void test_wolfSSL_PKCS7_certs(void)
  28040. {
  28041. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  28042. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  28043. STACK_OF(X509)* sk = NULL;
  28044. STACK_OF(X509_INFO)* info_sk = NULL;
  28045. PKCS7 *p7 = NULL;
  28046. BIO* bio;
  28047. const byte* p = NULL;
  28048. int buflen = 0;
  28049. int i;
  28050. printf(testingFmt, "wolfSSL_PKCS7_certs()");
  28051. /* Test twice. Once with d2i and once without to test
  28052. * that everything is free'd correctly. */
  28053. for (i = 0; i < 2; i++) {
  28054. AssertNotNull(p7 = PKCS7_new());
  28055. p7->version = 1;
  28056. p7->hashOID = SHAh;
  28057. AssertNotNull(bio = BIO_new(BIO_s_file()));
  28058. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28059. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  28060. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  28061. AssertNotNull(sk = sk_X509_new_null());
  28062. while (sk_X509_INFO_num(info_sk)) {
  28063. X509_INFO* info;
  28064. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  28065. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  28066. info->x509 = NULL;
  28067. X509_INFO_free(info);
  28068. }
  28069. sk_X509_INFO_free(info_sk);
  28070. BIO_free(bio);
  28071. bio = BIO_new(BIO_s_mem());
  28072. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  28073. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  28074. if (i == 0) {
  28075. PKCS7_free(p7);
  28076. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  28077. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  28078. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  28079. /* PKCS7_free free's the certs */
  28080. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  28081. }
  28082. BIO_free(bio);
  28083. PKCS7_free(p7);
  28084. }
  28085. printf(resultFmt, passed);
  28086. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  28087. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  28088. }
  28089. static void test_wolfSSL_X509_STORE_CTX(void)
  28090. {
  28091. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28092. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28093. X509_STORE_CTX* ctx;
  28094. X509_STORE* str;
  28095. X509* x509;
  28096. #ifdef OPENSSL_ALL
  28097. X509* x5092;
  28098. STACK_OF(X509) *sk, *sk2, *sk3;
  28099. #endif
  28100. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  28101. AssertNotNull(ctx = X509_STORE_CTX_new());
  28102. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28103. AssertNotNull((x509 =
  28104. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  28105. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  28106. #ifdef OPENSSL_ALL
  28107. /* sk_X509_new only in OPENSSL_ALL */
  28108. sk = sk_X509_new();
  28109. AssertNotNull(sk);
  28110. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  28111. #else
  28112. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  28113. #endif
  28114. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  28115. X509_STORE_CTX_set_error(ctx, -5);
  28116. X509_STORE_CTX_set_error(NULL, -5);
  28117. X509_STORE_CTX_free(ctx);
  28118. #ifdef OPENSSL_ALL
  28119. sk_X509_pop_free(sk, NULL);
  28120. #endif
  28121. X509_STORE_free(str);
  28122. X509_free(x509);
  28123. AssertNotNull(ctx = X509_STORE_CTX_new());
  28124. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  28125. X509_STORE_CTX_free(ctx);
  28126. #ifdef OPENSSL_ALL
  28127. /* test X509_STORE_CTX_get(1)_chain */
  28128. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  28129. SSL_FILETYPE_PEM)));
  28130. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  28131. SSL_FILETYPE_PEM)));
  28132. AssertNotNull((sk = sk_X509_new()));
  28133. AssertIntEQ(sk_X509_push(sk, x509), 1);
  28134. AssertNotNull((str = X509_STORE_new()));
  28135. AssertNotNull((ctx = X509_STORE_CTX_new()));
  28136. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  28137. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  28138. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  28139. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  28140. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  28141. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  28142. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  28143. X509_STORE_CTX_free(ctx);
  28144. X509_STORE_free(str);
  28145. /* CTX certs not freed yet */
  28146. X509_free(x5092);
  28147. sk_X509_pop_free(sk, NULL);
  28148. /* sk3 is dup so free here */
  28149. sk_X509_pop_free(sk3, NULL);
  28150. #endif
  28151. /* test X509_STORE_CTX_get/set_ex_data */
  28152. {
  28153. int i = 0, tmpData = 5;
  28154. void* tmpDataRet;
  28155. AssertNotNull(ctx = X509_STORE_CTX_new());
  28156. #ifdef HAVE_EX_DATA
  28157. for (i = 0; i < MAX_EX_DATA; i++) {
  28158. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  28159. WOLFSSL_SUCCESS);
  28160. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  28161. AssertNotNull(tmpDataRet);
  28162. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  28163. }
  28164. #else
  28165. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  28166. WOLFSSL_FAILURE);
  28167. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  28168. AssertNull(tmpDataRet);
  28169. #endif
  28170. X509_STORE_CTX_free(ctx);
  28171. }
  28172. /* test X509_STORE_get/set_ex_data */
  28173. {
  28174. int i = 0, tmpData = 99;
  28175. void* tmpDataRet;
  28176. AssertNotNull(str = X509_STORE_new());
  28177. #ifdef HAVE_EX_DATA
  28178. for (i = 0; i < MAX_EX_DATA; i++) {
  28179. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  28180. WOLFSSL_SUCCESS);
  28181. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  28182. AssertNotNull(tmpDataRet);
  28183. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  28184. }
  28185. #else
  28186. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  28187. WOLFSSL_FAILURE);
  28188. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  28189. AssertNull(tmpDataRet);
  28190. #endif
  28191. X509_STORE_free(str);
  28192. }
  28193. printf(resultFmt, passed);
  28194. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28195. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  28196. }
  28197. static void test_wolfSSL_X509_STORE_set_flags(void)
  28198. {
  28199. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28200. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28201. X509_STORE* store;
  28202. X509* x509;
  28203. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  28204. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  28205. AssertNotNull((x509 =
  28206. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  28207. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  28208. #ifdef HAVE_CRL
  28209. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  28210. #else
  28211. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  28212. NOT_COMPILED_IN);
  28213. #endif
  28214. wolfSSL_X509_free(x509);
  28215. wolfSSL_X509_STORE_free(store);
  28216. printf(resultFmt, passed);
  28217. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28218. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  28219. }
  28220. static void test_wolfSSL_X509_LOOKUP_load_file(void)
  28221. {
  28222. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  28223. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  28224. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  28225. WOLFSSL_X509_STORE* store;
  28226. WOLFSSL_X509_LOOKUP* lookup;
  28227. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  28228. AssertNotNull(store = wolfSSL_X509_STORE_new());
  28229. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  28230. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  28231. X509_FILETYPE_PEM), 1);
  28232. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  28233. X509_FILETYPE_PEM), 1);
  28234. if (store) {
  28235. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  28236. WOLFSSL_FILETYPE_PEM), 1);
  28237. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  28238. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  28239. }
  28240. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  28241. X509_FILETYPE_PEM), 1);
  28242. if (store) {
  28243. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  28244. WOLFSSL_FILETYPE_PEM), 1);
  28245. }
  28246. wolfSSL_X509_STORE_free(store);
  28247. printf(resultFmt, passed);
  28248. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  28249. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  28250. }
  28251. static void test_wolfSSL_X509_STORE_CTX_set_time(void)
  28252. {
  28253. #if defined(OPENSSL_EXTRA)
  28254. WOLFSSL_X509_STORE_CTX* ctx;
  28255. time_t c_time;
  28256. printf(testingFmt, "wolfSSL_X509_set_time()");
  28257. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  28258. c_time = 365*24*60*60;
  28259. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  28260. AssertTrue(
  28261. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  28262. AssertTrue(ctx->param->check_time == c_time);
  28263. wolfSSL_X509_STORE_CTX_free(ctx);
  28264. printf(resultFmt, passed);
  28265. #endif /* OPENSSL_EXTRA */
  28266. }
  28267. static void test_wolfSSL_CTX_get0_set1_param(void)
  28268. {
  28269. #if defined(OPENSSL_EXTRA)
  28270. int ret;
  28271. SSL_CTX* ctx;
  28272. WOLFSSL_X509_VERIFY_PARAM* pParam;
  28273. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  28274. char testIPv4[] = "127.0.0.1";
  28275. char testhostName[] = "foo.hoge.com";
  28276. printf(testingFmt, "wolfSSL_CTX_get0_set1_param()");
  28277. #ifndef NO_WOLFSSL_SERVER
  28278. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28279. #else
  28280. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28281. #endif
  28282. AssertNull(SSL_CTX_get0_param(NULL));
  28283. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  28284. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  28285. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  28286. AssertNotNull(pvpm);
  28287. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  28288. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  28289. (int)XSTRLEN(testhostName));
  28290. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  28291. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  28292. ret = SSL_CTX_set1_param(ctx, pvpm);
  28293. AssertIntEQ(1, ret);
  28294. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  28295. (int)XSTRLEN(testhostName)));
  28296. AssertIntEQ(0x01, pParam->hostFlags);
  28297. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  28298. /* test for incorrect patameter */
  28299. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  28300. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  28301. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  28302. SSL_CTX_free(ctx);
  28303. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  28304. printf(resultFmt, passed);
  28305. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  28306. }
  28307. static void test_wolfSSL_get0_param(void)
  28308. {
  28309. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  28310. SSL_CTX* ctx;
  28311. SSL* ssl;
  28312. WOLFSSL_X509_VERIFY_PARAM* pParam;
  28313. printf(testingFmt, "wolfSSL_get0_param()");
  28314. #ifndef NO_WOLFSSL_SERVER
  28315. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28316. #else
  28317. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28318. #endif
  28319. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28320. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28321. AssertNotNull(ssl = SSL_new(ctx));
  28322. pParam = SSL_get0_param(ssl);
  28323. (void)pParam;
  28324. SSL_free(ssl);
  28325. SSL_CTX_free(ctx);
  28326. printf(resultFmt, passed);
  28327. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  28328. }
  28329. static void test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  28330. {
  28331. #if defined(OPENSSL_EXTRA)
  28332. const char host[] = "www.example.com";
  28333. WOLFSSL_X509_VERIFY_PARAM* pParam;
  28334. printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
  28335. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  28336. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  28337. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  28338. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  28339. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  28340. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  28341. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  28342. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  28343. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  28344. printf(resultFmt, passed);
  28345. #endif /* OPENSSL_EXTRA */
  28346. }
  28347. static void test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  28348. {
  28349. #if defined(OPENSSL_EXTRA)
  28350. unsigned char buf[16] = {0};
  28351. WOLFSSL_X509_VERIFY_PARAM* param;
  28352. printf(testingFmt, "test_wolfSSL_X509_VERIFY_PARAM_set1_ip()");
  28353. AssertNotNull(param = X509_VERIFY_PARAM_new());
  28354. /* test 127.0.0.1 */
  28355. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  28356. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  28357. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  28358. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  28359. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  28360. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  28361. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  28362. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  28363. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28364. AssertIntEQ(XSTRNCMP(param->ipasc,
  28365. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  28366. /* test 2001:db8:: */
  28367. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  28368. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  28369. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  28370. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  28371. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28372. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  28373. /* test ::1234:5678 */
  28374. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  28375. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  28376. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  28377. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  28378. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28379. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  28380. /* test 2001:db8::1234:5678 */
  28381. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  28382. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  28383. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  28384. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  28385. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28386. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  28387. sizeof(param->ipasc)), 0);
  28388. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  28389. /* 2001:db8:1::ab9:c0a8:102 */
  28390. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  28391. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  28392. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  28393. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  28394. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  28395. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  28396. sizeof(param->ipasc)), 0);
  28397. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  28398. printf(resultFmt, passed);
  28399. #endif /* OPENSSL_EXTRA */
  28400. }
  28401. static void test_wolfSSL_X509_STORE_CTX_get0_store(void)
  28402. {
  28403. #if defined(OPENSSL_EXTRA)
  28404. X509_STORE* store;
  28405. X509_STORE_CTX* ctx;
  28406. X509_STORE_CTX* ctx_no_init;
  28407. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
  28408. AssertNotNull((store = X509_STORE_new()));
  28409. AssertNotNull(ctx = X509_STORE_CTX_new());
  28410. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  28411. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  28412. AssertNull(X509_STORE_CTX_get0_store(NULL));
  28413. /* should return NULL if ctx has not bee initialized */
  28414. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  28415. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  28416. wolfSSL_X509_STORE_CTX_free(ctx);
  28417. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  28418. X509_STORE_free(store);
  28419. printf(resultFmt, passed);
  28420. #endif /* OPENSSL_EXTRA */
  28421. }
  28422. static void test_wolfSSL_CTX_set_client_CA_list(void)
  28423. {
  28424. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  28425. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  28426. WOLFSSL_CTX* ctx;
  28427. WOLFSSL* ssl;
  28428. X509_NAME* name = NULL;
  28429. STACK_OF(X509_NAME)* names = NULL;
  28430. STACK_OF(X509_NAME)* ca_list = NULL;
  28431. int i, names_len;
  28432. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  28433. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  28434. /* Send two X501 names in cert request */
  28435. names = SSL_load_client_CA_file(cliCertFile);
  28436. AssertNotNull(names);
  28437. ca_list = SSL_load_client_CA_file(caCertFile);
  28438. AssertNotNull(ca_list);
  28439. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  28440. SSL_CTX_set_client_CA_list(ctx, names);
  28441. /* This should only free the stack structure */
  28442. sk_X509_NAME_free(ca_list);
  28443. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  28444. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  28445. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  28446. for (i=0; i<names_len; i++) {
  28447. AssertNotNull(name = sk_X509_NAME_value(names, i));
  28448. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  28449. }
  28450. /* Needed to be able to create ssl object */
  28451. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28452. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28453. AssertNotNull(ssl = wolfSSL_new(ctx));
  28454. /* load again as old names are responsibility of ctx to free*/
  28455. names = SSL_load_client_CA_file(cliCertFile);
  28456. AssertNotNull(names);
  28457. SSL_set_client_CA_list(ssl, names);
  28458. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  28459. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  28460. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  28461. for (i=0; i<names_len; i++) {
  28462. AssertNotNull(name = sk_X509_NAME_value(names, i));
  28463. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  28464. }
  28465. printf(resultFmt, passed);
  28466. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  28467. {
  28468. tcp_ready ready;
  28469. func_args server_args;
  28470. callback_functions server_cb;
  28471. THREAD_TYPE serverThread;
  28472. WOLFSSL* ssl_client;
  28473. WOLFSSL_CTX* ctx_client;
  28474. SOCKET_T sockfd = 0;
  28475. printf(testingFmt, "wolfSSL_get_client_CA_list() with handshake");
  28476. StartTCP();
  28477. InitTcpReady(&ready);
  28478. XMEMSET(&server_args, 0, sizeof(func_args));
  28479. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  28480. server_args.signal = &ready;
  28481. server_args.callbacks = &server_cb;
  28482. /* we are responsible for free'ing WOLFSSL_CTX */
  28483. server_cb.ctx = ctx;
  28484. server_cb.isSharedCtx = 1;
  28485. AssertIntEQ(WOLFSSL_SUCCESS,
  28486. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  28487. start_thread(test_server_nofail, &server_args, &serverThread);
  28488. wait_tcp_ready(&server_args);
  28489. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  28490. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  28491. AssertIntEQ(WOLFSSL_SUCCESS,
  28492. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  28493. AssertIntEQ(WOLFSSL_SUCCESS,
  28494. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  28495. AssertIntEQ(WOLFSSL_SUCCESS,
  28496. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  28497. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  28498. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  28499. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  28500. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  28501. /* We are expecting two cert names to be sent */
  28502. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  28503. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  28504. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  28505. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  28506. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  28507. }
  28508. wolfSSL_shutdown(ssl_client);
  28509. wolfSSL_free(ssl_client);
  28510. wolfSSL_CTX_free(ctx_client);
  28511. join_thread(serverThread);
  28512. FreeTcpReady(&ready);
  28513. printf(resultFmt, passed);
  28514. }
  28515. #endif
  28516. wolfSSL_free(ssl);
  28517. wolfSSL_CTX_free(ctx);
  28518. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT && !NO_BIO */
  28519. }
  28520. static void test_wolfSSL_CTX_add_client_CA(void)
  28521. {
  28522. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  28523. !defined(NO_WOLFSSL_CLIENT)
  28524. WOLFSSL_CTX* ctx;
  28525. WOLFSSL_X509* x509;
  28526. WOLFSSL_X509* x509_a;
  28527. STACK_OF(X509_NAME)* ca_list;
  28528. int ret = 0;
  28529. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  28530. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28531. /* Add client cert */
  28532. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  28533. AssertNotNull(x509);
  28534. ret = SSL_CTX_add_client_CA(ctx, x509);
  28535. AssertIntEQ(ret, SSL_SUCCESS);
  28536. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  28537. /* Add another client cert */
  28538. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  28539. SSL_FILETYPE_PEM));
  28540. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  28541. /* test for incorrect parameter */
  28542. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  28543. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  28544. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  28545. X509_free(x509);
  28546. X509_free(x509_a);
  28547. SSL_CTX_free(ctx);
  28548. printf(resultFmt, passed);
  28549. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  28550. }
  28551. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  28552. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  28553. {
  28554. callback_functions* callbacks = ((func_args*)args)->callbacks;
  28555. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  28556. WOLFSSL* ssl = NULL;
  28557. SOCKET_T sfd = 0;
  28558. SOCKET_T cfd = 0;
  28559. word16 port;
  28560. char msg[] = "I hear you fa shizzle!";
  28561. int len = (int) XSTRLEN(msg);
  28562. char input[1024];
  28563. int idx;
  28564. int ret, err = 0;
  28565. #ifdef WOLFSSL_TIRTOS
  28566. fdOpenSession(Task_self());
  28567. #endif
  28568. ((func_args*)args)->return_code = TEST_FAIL;
  28569. port = ((func_args*)args)->signal->port;
  28570. AssertIntEQ(WOLFSSL_SUCCESS,
  28571. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  28572. AssertIntEQ(WOLFSSL_SUCCESS,
  28573. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  28574. WOLFSSL_FILETYPE_PEM));
  28575. AssertIntEQ(WOLFSSL_SUCCESS,
  28576. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  28577. WOLFSSL_FILETYPE_PEM));
  28578. if (callbacks->ctx_ready)
  28579. callbacks->ctx_ready(ctx);
  28580. ssl = wolfSSL_new(ctx);
  28581. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  28582. CloseSocket(sfd);
  28583. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  28584. if (callbacks->ssl_ready)
  28585. callbacks->ssl_ready(ssl);
  28586. do {
  28587. err = 0; /* Reset error */
  28588. ret = wolfSSL_accept(ssl);
  28589. if (ret != WOLFSSL_SUCCESS) {
  28590. err = wolfSSL_get_error(ssl, 0);
  28591. }
  28592. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  28593. if (ret != WOLFSSL_SUCCESS) {
  28594. char buff[WOLFSSL_MAX_ERROR_SZ];
  28595. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  28596. }
  28597. else {
  28598. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  28599. input[idx] = 0;
  28600. printf("Client message: %s\n", input);
  28601. }
  28602. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  28603. #ifdef WOLFSSL_TIRTOS
  28604. Task_yield();
  28605. #endif
  28606. ((func_args*)args)->return_code = TEST_SUCCESS;
  28607. }
  28608. if (callbacks->on_result)
  28609. callbacks->on_result(ssl);
  28610. wolfSSL_shutdown(ssl);
  28611. wolfSSL_free(ssl);
  28612. wolfSSL_CTX_free(ctx);
  28613. CloseSocket(cfd);
  28614. #ifdef WOLFSSL_TIRTOS
  28615. fdCloseSession(Task_self());
  28616. #endif
  28617. #ifndef WOLFSSL_TIRTOS
  28618. return 0;
  28619. #endif
  28620. }
  28621. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  28622. {
  28623. AssertNotNull(ssl);
  28624. AssertNotNull(line);
  28625. XFILE fp;
  28626. const byte lf = '\n';
  28627. fp = XFOPEN("./MyKeyLog.txt", "a");
  28628. XFWRITE( line, 1, strlen(line),fp);
  28629. XFWRITE( (void*)&lf,1,1,fp);
  28630. XFCLOSE(fp);
  28631. }
  28632. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  28633. static void test_wolfSSL_CTX_set_keylog_callback(void)
  28634. {
  28635. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  28636. SSL_CTX* ctx;
  28637. printf( testingFmt, "wolfSSL_CTX_set_keylog_callback()");
  28638. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28639. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  28640. SSL_CTX_free(ctx);
  28641. SSL_CTX_set_keylog_callback(NULL, NULL);
  28642. printf(resultFmt, passed);
  28643. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  28644. }
  28645. static void test_wolfSSL_CTX_get_keylog_callback(void)
  28646. {
  28647. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  28648. SSL_CTX* ctx;
  28649. printf( testingFmt, "wolfSSL_CTX_get_keylog_callback()");
  28650. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28651. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  28652. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  28653. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  28654. SSL_CTX_set_keylog_callback(ctx, NULL );
  28655. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  28656. SSL_CTX_free(ctx);
  28657. printf(resultFmt, passed);
  28658. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  28659. }
  28660. static void test_wolfSSL_Tls12_Key_Logging_test(void)
  28661. {
  28662. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  28663. /* This test is intended for checking whether keylog callback is called
  28664. * in client during TLS handshake between the client and a server.
  28665. */
  28666. tcp_ready ready;
  28667. func_args client_args;
  28668. func_args server_args;
  28669. THREAD_TYPE serverThread;
  28670. callback_functions server_cbf;
  28671. callback_functions client_cbf;
  28672. SOCKET_T sockfd = 0;
  28673. WOLFSSL_CTX* ctx;
  28674. WOLFSSL* ssl;
  28675. XFILE fp;
  28676. char msg[64] = "hello wolfssl!";
  28677. char reply[1024];
  28678. int msgSz = (int)XSTRLEN(msg);
  28679. printf(testingFmt, "wolfSSL_Tls12_Key_Logging_test()");
  28680. #ifdef WOLFSSL_TIRTOS
  28681. fdOpenSession(Task_self());
  28682. #endif
  28683. InitTcpReady(&ready);
  28684. ready.port = 22222;
  28685. XMEMSET(&client_args, 0, sizeof(func_args));
  28686. XMEMSET(&server_args, 0, sizeof(func_args));
  28687. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  28688. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  28689. server_cbf.method = wolfTLSv1_2_server_method;
  28690. server_args.callbacks = &server_cbf;
  28691. server_args.signal = &ready;
  28692. /* clean up keylog file */
  28693. fp = XFOPEN("./MyKeyLog.txt", "w");
  28694. XFCLOSE(fp);
  28695. /* start server task */
  28696. start_thread(server_task, &server_args, &serverThread);
  28697. wait_tcp_ready(&server_args);
  28698. /* run as a TLS1.2 client */
  28699. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  28700. AssertIntEQ(WOLFSSL_SUCCESS,
  28701. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  28702. AssertIntEQ(WOLFSSL_SUCCESS,
  28703. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  28704. AssertIntEQ(WOLFSSL_SUCCESS,
  28705. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  28706. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  28707. /* set keylog callback */
  28708. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  28709. /* get connected the server task */
  28710. AssertNotNull(ssl = wolfSSL_new(ctx));
  28711. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  28712. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  28713. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  28714. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  28715. wolfSSL_shutdown(ssl);
  28716. wolfSSL_free(ssl);
  28717. wolfSSL_CTX_free(ctx);
  28718. CloseSocket(sockfd);
  28719. join_thread(serverThread);
  28720. FreeTcpReady(&ready);
  28721. #ifdef WOLFSSL_TIRTOS
  28722. fdOpenSession(Task_self());
  28723. #endif
  28724. /* check if the keylog file exists */
  28725. char buff[300] = {0};
  28726. int found = 0;
  28727. fp = XFOPEN("./MyKeyLog.txt", "r");
  28728. AssertNotNull(fp);
  28729. while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  28730. if(0 == strncmp(buff,"CLIENT_RANDOM ",
  28731. sizeof("CLIENT_RANDOM ")-1)) {
  28732. found = 1;
  28733. break;
  28734. }
  28735. }
  28736. XFCLOSE(fp);
  28737. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  28738. AssertNotNull( found );
  28739. printf(resultFmt, passed);
  28740. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  28741. }
  28742. static void test_wolfSSL_Tls13_Key_Logging_test(void)
  28743. {
  28744. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  28745. defined(HAVE_SECRET_CALLBACK)
  28746. /* This test is intended for checking whether keylog callback is called
  28747. * in client during TLS handshake between the client and a server.
  28748. */
  28749. tcp_ready ready;
  28750. func_args client_args;
  28751. func_args server_args;
  28752. THREAD_TYPE serverThread;
  28753. callback_functions server_cbf;
  28754. callback_functions client_cbf;
  28755. SOCKET_T sockfd = 0;
  28756. WOLFSSL_CTX* ctx;
  28757. WOLFSSL* ssl;
  28758. XFILE fp;
  28759. char msg[64] = "hello wolfssl!";
  28760. char reply[1024];
  28761. int msgSz = (int)XSTRLEN(msg);
  28762. printf(testingFmt, "wolfSSL_Tls13_Key_Logging_test()");
  28763. #ifdef WOLFSSL_TIRTOS
  28764. fdOpenSession(Task_self());
  28765. #endif
  28766. InitTcpReady(&ready);
  28767. ready.port = 22222;
  28768. XMEMSET(&client_args, 0, sizeof(func_args));
  28769. XMEMSET(&server_args, 0, sizeof(func_args));
  28770. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  28771. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  28772. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  28773. server_args.callbacks = &server_cbf;
  28774. server_args.signal = &ready;
  28775. /* clean up keylog file */
  28776. fp = XFOPEN("./MyKeyLog.txt", "w");
  28777. XFCLOSE(fp);
  28778. /* start server task */
  28779. start_thread(server_task, &server_args, &serverThread);
  28780. wait_tcp_ready(&server_args);
  28781. /* run as a TLS1.2 client */
  28782. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  28783. AssertIntEQ(WOLFSSL_SUCCESS,
  28784. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  28785. AssertIntEQ(WOLFSSL_SUCCESS,
  28786. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  28787. AssertIntEQ(WOLFSSL_SUCCESS,
  28788. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  28789. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  28790. /* set keylog callback */
  28791. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  28792. /* get connected the server task */
  28793. AssertNotNull(ssl = wolfSSL_new(ctx));
  28794. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  28795. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  28796. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  28797. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  28798. wolfSSL_free(ssl);
  28799. wolfSSL_CTX_free(ctx);
  28800. join_thread(serverThread);
  28801. FreeTcpReady(&ready);
  28802. #ifdef WOLFSSL_TIRTOS
  28803. fdOpenSession(Task_self());
  28804. #endif
  28805. /* check if the keylog file exists */
  28806. char buff[300] = {0};
  28807. int found[4] = {0};
  28808. fp = XFOPEN("./MyKeyLog.txt", "r");
  28809. AssertNotNull(fp);
  28810. while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  28811. if(0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  28812. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  28813. found[0] = 1;
  28814. continue;
  28815. }
  28816. else if(0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  28817. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  28818. found[1] = 1;
  28819. continue;
  28820. }
  28821. else if(0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  28822. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  28823. found[2] = 1;
  28824. continue;
  28825. }
  28826. else if(0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  28827. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  28828. found[3] = 1;
  28829. continue;
  28830. }
  28831. }
  28832. XFCLOSE(fp);
  28833. int numfnd = 0;
  28834. for( uint i = 0; i < 4; i++) {
  28835. if( found[i] != 0)
  28836. numfnd++;
  28837. }
  28838. AssertIntEQ( numfnd,4 );
  28839. printf(resultFmt, passed);
  28840. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  28841. }
  28842. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28843. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  28844. static void post_auth_version_cb(WOLFSSL* ssl)
  28845. {
  28846. /* do handshake and then test version error */
  28847. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  28848. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  28849. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  28850. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  28851. /* check was added to error queue */
  28852. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  28853. /* check the string matches expected string */
  28854. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  28855. "WRONG_SSL_VERSION");
  28856. #endif
  28857. }
  28858. static void post_auth_cb(WOLFSSL* ssl)
  28859. {
  28860. /* do handshake and then test version error */
  28861. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  28862. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  28863. AssertNull(wolfSSL_get_peer_certificate(ssl));
  28864. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  28865. }
  28866. static void set_post_auth_cb(WOLFSSL* ssl)
  28867. {
  28868. if (!wolfSSL_is_server(ssl)) {
  28869. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  28870. }
  28871. else {
  28872. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  28873. }
  28874. }
  28875. #endif
  28876. static void test_wolfSSL_Tls13_postauth(void)
  28877. {
  28878. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28879. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  28880. tcp_ready ready;
  28881. func_args client_args;
  28882. func_args server_args;
  28883. callback_functions server_cbf;
  28884. callback_functions client_cbf;
  28885. THREAD_TYPE serverThread;
  28886. printf(testingFmt, "wolfSSL_Tls13_postauth()");
  28887. XMEMSET(&client_args, 0, sizeof(func_args));
  28888. XMEMSET(&server_args, 0, sizeof(func_args));
  28889. StartTCP();
  28890. InitTcpReady(&ready);
  28891. #if defined(USE_WINDOWS_API)
  28892. /* use RNG to get random port if using windows */
  28893. ready.port = GetRandomPort();
  28894. #endif
  28895. server_args.signal = &ready;
  28896. client_args.signal = &ready;
  28897. /* test version failure doing post auth with TLS 1.2 connection */
  28898. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  28899. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  28900. server_cbf.method = wolfTLSv1_2_server_method;
  28901. server_cbf.ssl_ready = set_post_auth_cb;
  28902. client_cbf.ssl_ready = set_post_auth_cb;
  28903. server_cbf.on_result = post_auth_version_cb;
  28904. server_args.callbacks = &server_cbf;
  28905. client_args.callbacks = &client_cbf;
  28906. start_thread(test_server_nofail, &server_args, &serverThread);
  28907. wait_tcp_ready(&server_args);
  28908. test_client_nofail(&client_args, NULL);
  28909. join_thread(serverThread);
  28910. /* tests on post auth with TLS 1.3 */
  28911. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  28912. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  28913. server_cbf.method = wolfTLSv1_3_server_method;
  28914. server_cbf.ssl_ready = set_post_auth_cb;
  28915. client_cbf.ssl_ready = set_post_auth_cb;
  28916. server_cbf.on_result = post_auth_cb;
  28917. server_args.callbacks = &server_cbf;
  28918. client_args.callbacks = &client_cbf;
  28919. start_thread(test_server_nofail, &server_args, &serverThread);
  28920. wait_tcp_ready(&server_args);
  28921. test_client_nofail(&client_args, NULL);
  28922. join_thread(serverThread);
  28923. FreeTcpReady(&ready);
  28924. printf(resultFmt, passed);
  28925. #endif
  28926. }
  28927. static void test_wolfSSL_X509_NID(void)
  28928. {
  28929. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  28930. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  28931. int sigType;
  28932. int nameSz;
  28933. X509* cert;
  28934. EVP_PKEY* pubKeyTmp;
  28935. X509_NAME* name;
  28936. char commonName[80];
  28937. char countryName[80];
  28938. char localityName[80];
  28939. char stateName[80];
  28940. char orgName[80];
  28941. char orgUnit[80];
  28942. printf(testingFmt, "wolfSSL_X509_NID()");
  28943. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  28944. /* convert cert from DER to internal WOLFSSL_X509 struct */
  28945. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  28946. sizeof_client_cert_der_2048));
  28947. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  28948. /* extract PUBLIC KEY from cert */
  28949. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  28950. /* extract signatureType */
  28951. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  28952. /* extract subjectName info */
  28953. AssertNotNull(name = X509_get_subject_name(cert));
  28954. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  28955. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  28956. NULL, 0)), 0);
  28957. AssertIntEQ(nameSz, 15);
  28958. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  28959. commonName, sizeof(commonName))), 0);
  28960. AssertIntEQ(nameSz, 15);
  28961. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  28962. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  28963. commonName, 9)), 0);
  28964. AssertIntEQ(nameSz, 8);
  28965. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  28966. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  28967. countryName, sizeof(countryName))), 0);
  28968. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  28969. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  28970. localityName, sizeof(localityName))), 0);
  28971. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  28972. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  28973. stateName, sizeof(stateName))), 0);
  28974. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  28975. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  28976. orgName, sizeof(orgName))), 0);
  28977. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  28978. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  28979. orgUnit, sizeof(orgUnit))), 0);
  28980. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  28981. EVP_PKEY_free(pubKeyTmp);
  28982. X509_free(cert);
  28983. printf(resultFmt, passed);
  28984. #endif
  28985. }
  28986. static void test_wolfSSL_CTX_set_srp_username(void)
  28987. {
  28988. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  28989. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  28990. WOLFSSL_CTX* ctx;
  28991. WOLFSSL* ssl;
  28992. const char *username = "TESTUSER";
  28993. const char *password = "TESTPASSWORD";
  28994. int r;
  28995. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  28996. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  28997. AssertNotNull(ctx);
  28998. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  28999. AssertIntEQ(r,SSL_SUCCESS);
  29000. wolfSSL_CTX_free(ctx);
  29001. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  29002. AssertNotNull(ctx);
  29003. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  29004. AssertIntEQ(r,SSL_SUCCESS);
  29005. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  29006. AssertIntEQ(r,SSL_SUCCESS);
  29007. AssertNotNull(ssl = SSL_new(ctx));
  29008. AssertNotNull(SSL_get_srp_username(ssl));
  29009. AssertStrEQ(SSL_get_srp_username(ssl), username);
  29010. wolfSSL_free(ssl);
  29011. wolfSSL_CTX_free(ctx);
  29012. printf(resultFmt, passed);
  29013. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  29014. /* && !NO_SHA256 && !WC_NO_RNG */
  29015. }
  29016. static void test_wolfSSL_CTX_set_srp_password(void)
  29017. {
  29018. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  29019. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  29020. WOLFSSL_CTX* ctx;
  29021. const char *username = "TESTUSER";
  29022. const char *password = "TESTPASSWORD";
  29023. int r;
  29024. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  29025. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  29026. AssertNotNull(ctx);
  29027. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  29028. AssertIntEQ(r,SSL_SUCCESS);
  29029. wolfSSL_CTX_free(ctx);
  29030. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  29031. AssertNotNull(ctx);
  29032. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  29033. AssertIntEQ(r,SSL_SUCCESS);
  29034. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  29035. AssertIntEQ(r,SSL_SUCCESS);
  29036. wolfSSL_CTX_free(ctx);
  29037. printf(resultFmt, passed);
  29038. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  29039. /* && !NO_SHA256 && !WC_NO_RNG */
  29040. }
  29041. static void test_wolfSSL_X509_STORE(void)
  29042. {
  29043. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29044. X509_STORE *store;
  29045. #ifdef HAVE_CRL
  29046. X509_STORE_CTX *storeCtx;
  29047. X509_CRL *crl;
  29048. X509 *ca, *cert;
  29049. const char crlPem[] = "./certs/crl/crl.revoked";
  29050. const char srvCert[] = "./certs/server-revoked-cert.pem";
  29051. const char caCert[] = "./certs/ca-cert.pem";
  29052. XFILE fp;
  29053. printf(testingFmt, "test_wolfSSL_X509_STORE");
  29054. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29055. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  29056. SSL_FILETYPE_PEM)));
  29057. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  29058. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  29059. SSL_FILETYPE_PEM)));
  29060. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  29061. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  29062. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  29063. X509_STORE_free(store);
  29064. X509_STORE_CTX_free(storeCtx);
  29065. X509_free(cert);
  29066. X509_free(ca);
  29067. /* should fail to verify now after adding in CRL */
  29068. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29069. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  29070. SSL_FILETYPE_PEM)));
  29071. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  29072. fp = XFOPEN(crlPem, "rb");
  29073. AssertTrue((fp != XBADFILE));
  29074. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  29075. NULL, NULL));
  29076. XFCLOSE(fp);
  29077. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  29078. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  29079. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  29080. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  29081. SSL_FILETYPE_PEM)));
  29082. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  29083. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  29084. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  29085. X509_CRL_free(crl);
  29086. X509_STORE_free(store);
  29087. X509_STORE_CTX_free(storeCtx);
  29088. X509_free(cert);
  29089. X509_free(ca);
  29090. #endif /* HAVE_CRL */
  29091. #ifndef WOLFCRYPT_ONLY
  29092. {
  29093. SSL_CTX* ctx;
  29094. SSL* ssl;
  29095. int i;
  29096. for (i = 0; i < 2; i++) {
  29097. #ifndef NO_WOLFSSL_SERVER
  29098. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29099. #else
  29100. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29101. #endif
  29102. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29103. SSL_CTX_set_cert_store(ctx, store);
  29104. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29105. SSL_CTX_set_cert_store(ctx, store);
  29106. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  29107. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29108. SSL_FILETYPE_PEM), SSL_SUCCESS);
  29109. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  29110. SSL_FILETYPE_PEM), SSL_SUCCESS);
  29111. AssertNotNull(ssl = SSL_new(ctx));
  29112. if (i == 0) {
  29113. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  29114. }
  29115. else {
  29116. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  29117. X509_STORE_free(store);
  29118. }
  29119. SSL_free(ssl);
  29120. SSL_CTX_free(ctx);
  29121. }
  29122. }
  29123. #endif
  29124. printf(resultFmt, passed);
  29125. #endif
  29126. return;
  29127. }
  29128. static void test_wolfSSL_X509_STORE_load_locations(void)
  29129. {
  29130. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  29131. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  29132. SSL_CTX *ctx;
  29133. X509_STORE *store;
  29134. const char ca_file[] = "./certs/ca-cert.pem";
  29135. const char client_pem_file[] = "./certs/client-cert.pem";
  29136. const char client_der_file[] = "./certs/client-cert.der";
  29137. const char ecc_file[] = "./certs/ecc-key.pem";
  29138. const char certs_path[] = "./certs/";
  29139. const char bad_path[] = "./bad-path/";
  29140. #ifdef HAVE_CRL
  29141. const char crl_path[] = "./certs/crl/";
  29142. const char crl_file[] = "./certs/crl/crl.pem";
  29143. #endif
  29144. printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
  29145. #ifndef NO_WOLFSSL_SERVER
  29146. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  29147. #else
  29148. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  29149. #endif
  29150. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  29151. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  29152. /* Test bad arguments */
  29153. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  29154. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  29155. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  29156. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  29157. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  29158. #ifdef HAVE_CRL
  29159. /* Test with CRL */
  29160. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  29161. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  29162. #endif
  29163. /* Test with CA */
  29164. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  29165. /* Test with client_cert and certs path */
  29166. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  29167. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  29168. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  29169. /* Clear nodes */
  29170. ERR_clear_error();
  29171. #endif
  29172. SSL_CTX_free(ctx);
  29173. printf(resultFmt, passed);
  29174. #endif
  29175. }
  29176. static void test_X509_STORE_get0_objects(void)
  29177. {
  29178. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  29179. X509_STORE *store;
  29180. X509_STORE *store_cpy;
  29181. SSL_CTX *ctx;
  29182. X509_OBJECT *obj;
  29183. STACK_OF(X509_OBJECT) *objs;
  29184. int i;
  29185. printf(testingFmt, "wolfSSL_X509_STORE_get0_objects");
  29186. /* Setup store */
  29187. #ifndef NO_WOLFSSL_SERVER
  29188. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  29189. #else
  29190. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  29191. #endif
  29192. AssertNotNull(store_cpy = X509_STORE_new());
  29193. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  29194. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  29195. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  29196. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  29197. #ifdef HAVE_CRL
  29198. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  29199. #endif
  29200. /* Store ready */
  29201. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  29202. AssertNotNull(objs = X509_STORE_get0_objects(store));
  29203. #ifdef HAVE_CRL
  29204. #ifdef WOLFSSL_SIGNER_DER_CERT
  29205. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  29206. #else
  29207. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  29208. #endif
  29209. #else
  29210. #ifdef WOLFSSL_SIGNER_DER_CERT
  29211. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  29212. #else
  29213. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  29214. #endif
  29215. #endif
  29216. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  29217. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  29218. switch (X509_OBJECT_get_type(obj)) {
  29219. case X509_LU_X509:
  29220. AssertNotNull(X509_OBJECT_get0_X509(obj));
  29221. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  29222. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  29223. break;
  29224. case X509_LU_CRL:
  29225. #ifdef HAVE_CRL
  29226. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  29227. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  29228. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  29229. break;
  29230. #endif
  29231. case X509_LU_NONE:
  29232. default:
  29233. Fail(("X509_OBJECT_get_type should return x509 or crl "
  29234. "(when built with crl support)"),
  29235. ("Unrecognized X509_OBJECT type or none"));
  29236. }
  29237. }
  29238. X509_STORE_free(store_cpy);
  29239. SSL_CTX_free(ctx);
  29240. printf(resultFmt, passed);
  29241. #endif
  29242. }
  29243. static void test_wolfSSL_BN(void)
  29244. {
  29245. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  29246. BIGNUM* a;
  29247. BIGNUM* b;
  29248. BIGNUM* c;
  29249. BIGNUM* d;
  29250. ASN1_INTEGER* ai;
  29251. printf(testingFmt, "wolfSSL_BN()");
  29252. AssertNotNull(b = BN_new());
  29253. AssertNotNull(c = BN_new());
  29254. AssertNotNull(d = BN_new());
  29255. ai = ASN1_INTEGER_new();
  29256. AssertNotNull(ai);
  29257. /* at the moment hard setting since no set function */
  29258. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  29259. ai->data[1] = 0x01; /* length of integer */
  29260. ai->data[2] = 0x03;
  29261. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  29262. ASN1_INTEGER_free(ai);
  29263. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  29264. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  29265. /* a + 3 = */
  29266. AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
  29267. AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
  29268. /* check result 3 + 3*/
  29269. AssertIntEQ(BN_get_word(a), 6);
  29270. /* set a back to 3 */
  29271. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  29272. /* a - 3 = */
  29273. AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
  29274. AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
  29275. /* check result 3 - 3*/
  29276. AssertIntEQ(BN_get_word(a), 0);
  29277. /* set a back to 3 */
  29278. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  29279. /* a^b mod c = */
  29280. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  29281. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  29282. /* check result 3^2 mod 5 */
  29283. AssertIntEQ(BN_get_word(d), 4);
  29284. /* a*b = */
  29285. AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
  29286. AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
  29287. /* check result 3*2 */
  29288. AssertIntEQ(BN_get_word(d), 6);
  29289. /* c/b = */
  29290. AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
  29291. AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
  29292. /* check result 5/2 */
  29293. AssertIntEQ(BN_get_word(d), 2); /* check quotient */
  29294. AssertIntEQ(BN_get_word(a), 1); /* check remainder */
  29295. /* set a back to 3 */
  29296. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  29297. /* a*b mod c = */
  29298. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  29299. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  29300. /* check result 3*2 mod 5 */
  29301. AssertIntEQ(BN_get_word(d), 1);
  29302. AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
  29303. AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
  29304. #ifdef WOLFSSL_KEY_GEN
  29305. /* gcd of a and b */
  29306. AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
  29307. AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
  29308. /* check result gcd(16, 24) */
  29309. AssertIntEQ(BN_get_word(d), 8);
  29310. #endif /* WOLFSSL_KEY_GEN */
  29311. /* set b back to 2 */
  29312. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  29313. /* BN_mod_inverse test */
  29314. BIGNUM *r = BN_new();
  29315. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  29316. AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
  29317. BN_free(val);
  29318. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  29319. defined(WOLFSSL_SP_INT_NEGATIVE))
  29320. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  29321. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  29322. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  29323. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  29324. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  29325. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  29326. {
  29327. char* ret;
  29328. AssertNotNull(ret = BN_bn2dec(c));
  29329. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  29330. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  29331. }
  29332. #endif
  29333. AssertIntEQ(BN_get_word(c), 4);
  29334. #endif
  29335. BN_free(a);
  29336. BN_free(b);
  29337. BN_free(c);
  29338. BN_clear_free(d);
  29339. /* check that converting NULL and the null string returns an error */
  29340. a = NULL;
  29341. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  29342. AssertIntLE(BN_hex2bn(&a, ""), 0);
  29343. AssertNull(a);
  29344. /* check that getting a string and a bin of the same number are equal,
  29345. * and that the comparison works EQ, LT and GT */
  29346. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  29347. AssertNotNull(b = BN_new());
  29348. AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
  29349. AssertNotNull(c = BN_new());
  29350. AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
  29351. AssertIntEQ(BN_cmp(a, b), 0);
  29352. AssertIntLT(BN_cmp(a, c), 0);
  29353. AssertIntGT(BN_cmp(c, b), 0);
  29354. AssertIntEQ(BN_set_word(a, 0), 1);
  29355. AssertIntEQ(BN_is_zero(a), 1);
  29356. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  29357. AssertIntEQ(BN_is_zero(a), 0);
  29358. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  29359. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  29360. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  29361. AssertIntEQ(BN_is_zero(a), 1);
  29362. BN_free(a);
  29363. BN_free(b);
  29364. BN_free(c);
  29365. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  29366. {
  29367. BIGNUM *ap;
  29368. BIGNUM bv;
  29369. BIGNUM cv;
  29370. BIGNUM dv;
  29371. AssertNotNull(ap = BN_new());
  29372. BN_init(&bv);
  29373. BN_init(&cv);
  29374. BN_init(&dv);
  29375. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  29376. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  29377. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  29378. /* a^b mod c = */
  29379. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  29380. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  29381. /* check result 3^2 mod 5 */
  29382. AssertIntEQ(BN_get_word(&dv), 4);
  29383. /* a*b mod c = */
  29384. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  29385. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  29386. /* check result 3*2 mod 5 */
  29387. AssertIntEQ(BN_get_word(&dv), 1);
  29388. BN_free(ap);
  29389. }
  29390. #endif
  29391. #ifdef WOLFSSL_KEY_GEN
  29392. AssertNotNull(a = BN_new());
  29393. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
  29394. SSL_SUCCESS);
  29395. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
  29396. BN_free(a);
  29397. #endif
  29398. printf(resultFmt, passed);
  29399. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  29400. }
  29401. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29402. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29403. #define TEST_ARG 0x1234
  29404. static void msg_cb(int write_p, int version, int content_type,
  29405. const void *buf, size_t len, SSL *ssl, void *arg)
  29406. {
  29407. (void)write_p;
  29408. (void)version;
  29409. (void)content_type;
  29410. (void)buf;
  29411. (void)len;
  29412. (void)ssl;
  29413. AssertTrue(arg == (void*)TEST_ARG);
  29414. }
  29415. #endif
  29416. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29417. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  29418. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  29419. !defined(NO_WOLFSSL_SERVER)
  29420. #ifndef SINGLE_THREADED
  29421. #if defined(SESSION_CERTS)
  29422. #include "wolfssl/internal.h"
  29423. #endif
  29424. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  29425. {
  29426. (void) ctx;
  29427. (void) ssl;
  29428. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS)
  29429. STACK_OF(X509)* sk;
  29430. X509* x509;
  29431. int i, num;
  29432. BIO* bio;
  29433. #endif
  29434. printf("\n===== msgcb called ====\n");
  29435. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  29436. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  29437. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  29438. AssertNotNull(SSL_get0_verified_chain(ssl));
  29439. #endif
  29440. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS)
  29441. bio = BIO_new(BIO_s_file());
  29442. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  29443. sk = SSL_get_peer_cert_chain(ssl);
  29444. AssertNotNull(sk);
  29445. if (!sk) {
  29446. BIO_free(bio);
  29447. return SSL_FAILURE;
  29448. }
  29449. num = sk_X509_num(sk);
  29450. AssertTrue(num > 0);
  29451. for (i = 0; i < num; i++) {
  29452. x509 = sk_X509_value(sk,i);
  29453. AssertNotNull(x509);
  29454. if (!x509)
  29455. break;
  29456. printf("Certificate at index [%d] = :\n",i);
  29457. X509_print(bio,x509);
  29458. printf("\n\n");
  29459. }
  29460. BIO_free(bio);
  29461. #endif
  29462. return SSL_SUCCESS;
  29463. }
  29464. #endif
  29465. #endif
  29466. static void test_wolfSSL_msgCb(void)
  29467. {
  29468. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29469. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  29470. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  29471. !defined(NO_WOLFSSL_SERVER)
  29472. tcp_ready ready;
  29473. func_args client_args;
  29474. func_args server_args;
  29475. #ifndef SINGLE_THREADED
  29476. THREAD_TYPE serverThread;
  29477. #endif
  29478. callback_functions client_cb;
  29479. callback_functions server_cb;
  29480. printf(testingFmt, "test_wolfSSL_msgCb");
  29481. /* create a failed connection and inspect the error */
  29482. #ifdef WOLFSSL_TIRTOS
  29483. fdOpenSession(Task_self());
  29484. #endif
  29485. XMEMSET(&client_args, 0, sizeof(func_args));
  29486. XMEMSET(&server_args, 0, sizeof(func_args));
  29487. StartTCP();
  29488. InitTcpReady(&ready);
  29489. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29490. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29491. #ifndef WOLFSSL_NO_TLS12
  29492. client_cb.method = wolfTLSv1_2_client_method;
  29493. server_cb.method = wolfTLSv1_2_server_method;
  29494. #else
  29495. client_cb.method = wolfTLSv1_3_client_method;
  29496. server_cb.method = wolfTLSv1_3_server_method;
  29497. #endif
  29498. server_args.signal = &ready;
  29499. server_args.callbacks = &server_cb;
  29500. client_args.signal = &ready;
  29501. client_args.callbacks = &client_cb;
  29502. client_args.return_code = TEST_FAIL;
  29503. #ifndef SINGLE_THREADED
  29504. start_thread(test_server_nofail, &server_args, &serverThread);
  29505. wait_tcp_ready(&server_args);
  29506. test_client_nofail(&client_args, msgCb);
  29507. join_thread(serverThread);
  29508. #endif
  29509. FreeTcpReady(&ready);
  29510. #ifndef SINGLE_THREADED
  29511. AssertTrue(client_args.return_code);
  29512. AssertTrue(server_args.return_code);
  29513. #endif
  29514. #ifdef WOLFSSL_TIRTOS
  29515. fdOpenSession(Task_self());
  29516. #endif
  29517. printf(resultFmt, passed);
  29518. #endif
  29519. }
  29520. static void test_wolfSSL_either_side(void)
  29521. {
  29522. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  29523. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  29524. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  29525. tcp_ready ready;
  29526. func_args client_args;
  29527. func_args server_args;
  29528. #ifndef SINGLE_THREADED
  29529. THREAD_TYPE serverThread;
  29530. #endif
  29531. callback_functions client_cb;
  29532. callback_functions server_cb;
  29533. printf(testingFmt, "test_wolfSSL_either_side");
  29534. /* create a failed connection and inspect the error */
  29535. #ifdef WOLFSSL_TIRTOS
  29536. fdOpenSession(Task_self());
  29537. #endif
  29538. XMEMSET(&client_args, 0, sizeof(func_args));
  29539. XMEMSET(&server_args, 0, sizeof(func_args));
  29540. StartTCP();
  29541. InitTcpReady(&ready);
  29542. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29543. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29544. /* Use different CTX for client and server */
  29545. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  29546. AssertNotNull(client_cb.ctx);
  29547. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  29548. AssertNotNull(server_cb.ctx);
  29549. /* we are responsible for free'ing WOLFSSL_CTX */
  29550. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  29551. server_args.signal = &ready;
  29552. server_args.callbacks = &server_cb;
  29553. client_args.signal = &ready;
  29554. client_args.callbacks = &client_cb;
  29555. client_args.return_code = TEST_FAIL;
  29556. #ifndef SINGLE_THREADED
  29557. start_thread(test_server_nofail, &server_args, &serverThread);
  29558. wait_tcp_ready(&server_args);
  29559. test_client_nofail(&client_args, NULL);
  29560. join_thread(serverThread);
  29561. #endif
  29562. wolfSSL_CTX_free(client_cb.ctx);
  29563. wolfSSL_CTX_free(server_cb.ctx);
  29564. FreeTcpReady(&ready);
  29565. #ifndef SINGLE_THREADED
  29566. AssertTrue(client_args.return_code);
  29567. AssertTrue(server_args.return_code);
  29568. #endif
  29569. #ifdef WOLFSSL_TIRTOS
  29570. fdOpenSession(Task_self());
  29571. #endif
  29572. printf(resultFmt, passed);
  29573. #endif
  29574. }
  29575. static void test_wolfSSL_DTLS_either_side(void)
  29576. {
  29577. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  29578. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  29579. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  29580. defined(WOLFSSL_DTLS)
  29581. tcp_ready ready;
  29582. func_args client_args;
  29583. func_args server_args;
  29584. #ifndef SINGLE_THREADED
  29585. THREAD_TYPE serverThread;
  29586. #endif
  29587. callback_functions client_cb;
  29588. callback_functions server_cb;
  29589. printf(testingFmt, "test_wolfSSL_DTLS_either_side");
  29590. /* create a failed connection and inspect the error */
  29591. #ifdef WOLFSSL_TIRTOS
  29592. fdOpenSession(Task_self());
  29593. #endif
  29594. XMEMSET(&client_args, 0, sizeof(func_args));
  29595. XMEMSET(&server_args, 0, sizeof(func_args));
  29596. StartTCP();
  29597. InitTcpReady(&ready);
  29598. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29599. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29600. /* Use different CTX for client and server */
  29601. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  29602. AssertNotNull(client_cb.ctx);
  29603. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  29604. AssertNotNull(server_cb.ctx);
  29605. /* we are responsible for free'ing WOLFSSL_CTX */
  29606. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  29607. server_args.signal = &ready;
  29608. server_args.callbacks = &server_cb;
  29609. client_args.signal = &ready;
  29610. client_args.callbacks = &client_cb;
  29611. client_args.return_code = TEST_FAIL;
  29612. #ifndef SINGLE_THREADED
  29613. start_thread(test_server_nofail, &server_args, &serverThread);
  29614. wait_tcp_ready(&server_args);
  29615. test_client_nofail(&client_args, NULL);
  29616. join_thread(serverThread);
  29617. #endif
  29618. wolfSSL_CTX_free(client_cb.ctx);
  29619. wolfSSL_CTX_free(server_cb.ctx);
  29620. FreeTcpReady(&ready);
  29621. #ifndef SINGLE_THREADED
  29622. AssertTrue(client_args.return_code);
  29623. AssertTrue(server_args.return_code);
  29624. #endif
  29625. #ifdef WOLFSSL_TIRTOS
  29626. fdOpenSession(Task_self());
  29627. #endif
  29628. printf(resultFmt, passed);
  29629. #endif
  29630. }
  29631. static void test_generate_cookie(void)
  29632. {
  29633. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  29634. SSL_CTX* ctx;
  29635. SSL* ssl;
  29636. byte buf[FOURK_BUF] = {0};
  29637. printf(testingFmt, "test_generate_cookie");
  29638. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  29639. AssertNotNull(ssl = SSL_new(ctx));
  29640. /* Test unconnected */
  29641. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  29642. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  29643. wolfSSL_SetCookieCtx(ssl, ctx);
  29644. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  29645. AssertNull(wolfSSL_GetCookieCtx(NULL));
  29646. SSL_free(ssl);
  29647. SSL_CTX_free(ctx);
  29648. printf(resultFmt, passed);
  29649. #endif
  29650. }
  29651. static void test_wolfSSL_set_options(void)
  29652. {
  29653. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29654. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29655. SSL* ssl;
  29656. SSL_CTX* ctx;
  29657. char appData[] = "extra msg";
  29658. unsigned char protos[] = {
  29659. 7, 't', 'l', 's', '/', '1', '.', '2',
  29660. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  29661. };
  29662. unsigned int len = sizeof(protos);
  29663. void *arg = (void *)TEST_ARG;
  29664. printf(testingFmt, "wolfSSL_set_options()");
  29665. #ifndef NO_WOLFSSL_SERVER
  29666. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29667. #else
  29668. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29669. #endif
  29670. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29671. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29672. AssertTrue(SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  29673. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  29674. AssertIntGT((int)SSL_CTX_set_options(ctx, (SSL_OP_COOKIE_EXCHANGE |
  29675. SSL_OP_NO_SSLv2)), 0);
  29676. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE) &
  29677. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  29678. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2) &
  29679. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  29680. AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION) &
  29681. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  29682. AssertNull((SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION) &
  29683. SSL_OP_NO_COMPRESSION));
  29684. SSL_CTX_free(ctx);
  29685. #ifndef NO_WOLFSSL_SERVER
  29686. ctx = SSL_CTX_new(wolfSSLv23_server_method());
  29687. AssertNotNull(ctx);
  29688. #else
  29689. ctx = SSL_CTX_new(wolfSSLv23_client_method());
  29690. AssertNotNull(ctx);
  29691. #endif
  29692. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29693. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29694. AssertTrue(SSL_CTX_set_msg_callback(ctx, msg_cb) == SSL_SUCCESS);
  29695. AssertNotNull(ssl = SSL_new(ctx));
  29696. #ifdef HAVE_EX_DATA
  29697. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_SUCCESS);
  29698. AssertNotNull(SSL_get_app_data((const WOLFSSL*)ssl));
  29699. if (ssl) {
  29700. AssertIntEQ(XMEMCMP(SSL_get_app_data((const WOLFSSL*)ssl),
  29701. appData, sizeof(appData)), 0);
  29702. }
  29703. #else
  29704. AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_FAILURE);
  29705. AssertNull(SSL_get_app_data((const WOLFSSL*)ssl));
  29706. #endif
  29707. AssertTrue(SSL_set_options(ssl, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
  29708. AssertTrue(SSL_get_options(ssl) == SSL_OP_NO_TLSv1);
  29709. AssertIntGT((int)SSL_set_options(ssl, (SSL_OP_COOKIE_EXCHANGE |
  29710. WOLFSSL_OP_NO_SSLv2)), 0);
  29711. AssertTrue((SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE) &
  29712. SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
  29713. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_TLSv1_2) &
  29714. SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
  29715. AssertTrue((SSL_set_options(ssl, SSL_OP_NO_COMPRESSION) &
  29716. SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
  29717. AssertNull((SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION) &
  29718. SSL_OP_NO_COMPRESSION));
  29719. AssertTrue(SSL_set_msg_callback(ssl, msg_cb) == SSL_SUCCESS);
  29720. SSL_set_msg_callback_arg(ssl, arg);
  29721. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29722. AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  29723. #else
  29724. AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == SSL_SUCCESS);
  29725. #endif
  29726. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  29727. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  29728. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  29729. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  29730. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29731. AssertTrue(SSL_set_alpn_protos(ssl, protos, len) == 0);
  29732. #else
  29733. AssertTrue(SSL_set_alpn_protos(ssl, protos, len) == SSL_SUCCESS);
  29734. #endif
  29735. #endif /* HAVE_ALPN && !NO_BIO */
  29736. #endif
  29737. SSL_free(ssl);
  29738. SSL_CTX_free(ctx);
  29739. printf(resultFmt, passed);
  29740. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29741. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29742. }
  29743. static void test_wolfSSL_sk_SSL_CIPHER(void)
  29744. {
  29745. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29746. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29747. SSL* ssl;
  29748. SSL_CTX* ctx;
  29749. STACK_OF(SSL_CIPHER) *sk, *dup;
  29750. printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
  29751. #ifndef NO_WOLFSSL_SERVER
  29752. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29753. #else
  29754. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29755. #endif
  29756. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29757. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29758. AssertNotNull(ssl = SSL_new(ctx));
  29759. AssertNotNull(sk = SSL_get_ciphers(ssl));
  29760. AssertNotNull(dup = sk_SSL_CIPHER_dup(sk));
  29761. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  29762. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dup));
  29763. /* error case because connection has not been established yet */
  29764. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  29765. sk_SSL_CIPHER_free(dup);
  29766. /* sk is pointer to internal struct that should be free'd in SSL_free */
  29767. SSL_free(ssl);
  29768. SSL_CTX_free(ctx);
  29769. printf(resultFmt, passed);
  29770. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29771. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29772. }
  29773. static void test_wolfSSL_set1_curves_list(void)
  29774. {
  29775. #if defined(OPENSSL_EXTRA) && defined(HAVE_RSA)
  29776. SSL* ssl = NULL;
  29777. SSL_CTX* ctx = NULL;
  29778. #ifndef NO_WOLFSSL_SERVER
  29779. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29780. #else
  29781. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29782. #endif
  29783. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29784. SSL_FILETYPE_PEM));
  29785. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29786. AssertNotNull(ssl = SSL_new(ctx));
  29787. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  29788. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  29789. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  29790. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  29791. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  29792. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  29793. SSL_free(ssl);
  29794. SSL_CTX_free(ctx);
  29795. printf(resultFmt, passed);
  29796. #endif
  29797. }
  29798. static void test_wolfSSL_set1_sigalgs_list(void)
  29799. {
  29800. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  29801. SSL* ssl;
  29802. SSL_CTX* ctx;
  29803. #ifndef NO_WOLFSSL_SERVER
  29804. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29805. #else
  29806. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29807. #endif
  29808. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29809. SSL_FILETYPE_PEM));
  29810. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29811. AssertNotNull(ssl = SSL_new(ctx));
  29812. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  29813. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  29814. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  29815. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  29816. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  29817. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  29818. #ifndef NO_RSA
  29819. #ifndef NO_SHA256
  29820. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  29821. WOLFSSL_FAILURE);
  29822. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  29823. WOLFSSL_FAILURE);
  29824. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  29825. WOLFSSL_SUCCESS);
  29826. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  29827. WOLFSSL_SUCCESS);
  29828. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  29829. WOLFSSL_FAILURE);
  29830. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  29831. WOLFSSL_FAILURE);
  29832. #ifdef WC_RSA_PSS
  29833. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  29834. WOLFSSL_SUCCESS);
  29835. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  29836. WOLFSSL_SUCCESS);
  29837. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  29838. WOLFSSL_SUCCESS);
  29839. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  29840. WOLFSSL_SUCCESS);
  29841. #endif
  29842. #ifdef WOLFSSL_SHA512
  29843. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  29844. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  29845. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  29846. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  29847. #elif defined(WOLFSSL_SHA384)
  29848. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  29849. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  29850. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  29851. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  29852. #endif
  29853. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  29854. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  29855. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  29856. WOLFSSL_FAILURE);
  29857. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  29858. WOLFSSL_FAILURE);
  29859. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  29860. WOLFSSL_FAILURE);
  29861. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  29862. WOLFSSL_FAILURE);
  29863. #endif
  29864. #endif
  29865. #ifdef HAVE_ECC
  29866. #ifndef NO_SHA256
  29867. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  29868. WOLFSSL_SUCCESS);
  29869. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  29870. #ifdef WOLFSSL_SHA512
  29871. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  29872. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  29873. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  29874. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  29875. #elif defined(WOLFSSL_SHA384)
  29876. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  29877. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  29878. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  29879. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  29880. #endif
  29881. #endif
  29882. #endif
  29883. #ifdef HAVE_ED25519
  29884. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  29885. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  29886. #endif
  29887. #ifdef HAVE_ED448
  29888. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  29889. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  29890. #endif
  29891. #ifndef NO_DSA
  29892. #ifndef NO_SHA256
  29893. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  29894. WOLFSSL_SUCCESS);
  29895. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  29896. WOLFSSL_SUCCESS);
  29897. #endif
  29898. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  29899. defined(WOLFSSL_ALLOW_TLS_SHA1))
  29900. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  29901. WOLFSSL_SUCCESS);
  29902. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  29903. WOLFSSL_SUCCESS);
  29904. #endif
  29905. #endif
  29906. SSL_free(ssl);
  29907. SSL_CTX_free(ctx);
  29908. printf(resultFmt, passed);
  29909. #endif
  29910. }
  29911. /* Testing wolfSSL_set_tlsext_status_type function.
  29912. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  29913. */
  29914. static void test_wolfSSL_set_tlsext_status_type(void){
  29915. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  29916. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  29917. SSL* ssl;
  29918. SSL_CTX* ctx;
  29919. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  29920. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29921. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29922. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29923. AssertNotNull(ssl = SSL_new(ctx));
  29924. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  29925. SSL_SUCCESS);
  29926. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  29927. SSL_free(ssl);
  29928. SSL_CTX_free(ctx);
  29929. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  29930. }
  29931. #ifndef NO_BIO
  29932. static void test_wolfSSL_PEM_read_bio(void)
  29933. {
  29934. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29935. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29936. byte buff[6000];
  29937. XFILE f;
  29938. int bytes;
  29939. X509* x509;
  29940. BIO* bio = NULL;
  29941. BUF_MEM* buf;
  29942. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  29943. f = XFOPEN(cliCertFile, "rb");
  29944. AssertTrue((f != XBADFILE));
  29945. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  29946. XFCLOSE(f);
  29947. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  29948. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  29949. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  29950. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  29951. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  29952. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  29953. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  29954. BIO_free(bio);
  29955. BUF_MEM_free(buf);
  29956. X509_free(x509);
  29957. printf(resultFmt, passed);
  29958. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29959. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29960. }
  29961. #if defined(OPENSSL_EXTRA)
  29962. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  29963. long argl, long ret)
  29964. {
  29965. (void)bio;
  29966. (void)cmd;
  29967. (void)argp;
  29968. (void)argi;
  29969. (void)argl;
  29970. return ret;
  29971. }
  29972. #endif
  29973. static void test_wolfSSL_BIO(void)
  29974. {
  29975. #if defined(OPENSSL_EXTRA)
  29976. const unsigned char* p;
  29977. byte buff[20];
  29978. BIO* bio1;
  29979. BIO* bio2;
  29980. BIO* bio3;
  29981. char* bufPt;
  29982. int i;
  29983. printf(testingFmt, "wolfSSL_BIO()");
  29984. for (i = 0; i < 20; i++) {
  29985. buff[i] = i;
  29986. }
  29987. /* test BIO_free with NULL */
  29988. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  29989. /* Creating and testing type BIO_s_bio */
  29990. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  29991. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  29992. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  29993. /* read/write before set up */
  29994. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  29995. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  29996. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  29997. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  29998. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  29999. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  30000. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  30001. XMEMCPY(bufPt, buff, 10);
  30002. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  30003. /* write buffer full */
  30004. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  30005. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  30006. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  30007. /* write the other direction with pair */
  30008. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  30009. XMEMCPY(bufPt, buff, 8);
  30010. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  30011. /* try read */
  30012. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  30013. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  30014. /* try read using ctrl function */
  30015. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  30016. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  30017. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  30018. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  30019. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  30020. for (i = 0; i < 20; i++) {
  30021. AssertIntEQ((int)bufPt[i], i);
  30022. }
  30023. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  30024. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  30025. for (i = 0; i < 8; i++) {
  30026. AssertIntEQ((int)bufPt[i], i);
  30027. }
  30028. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  30029. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  30030. /* new pair */
  30031. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  30032. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  30033. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  30034. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  30035. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  30036. /* test wrap around... */
  30037. AssertIntEQ(BIO_reset(bio1), 0);
  30038. AssertIntEQ(BIO_reset(bio3), 0);
  30039. /* fill write buffer, read only small amount then write again */
  30040. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  30041. XMEMCPY(bufPt, buff, 20);
  30042. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  30043. for (i = 0; i < 4; i++) {
  30044. AssertIntEQ(bufPt[i], i);
  30045. }
  30046. /* try writing over read index */
  30047. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  30048. XMEMSET(bufPt, 0, 4);
  30049. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  30050. /* read and write 0 bytes */
  30051. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  30052. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  30053. /* should read only to end of write buffer then need to read again */
  30054. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  30055. for (i = 0; i < 16; i++) {
  30056. AssertIntEQ(bufPt[i], buff[4 + i]);
  30057. }
  30058. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  30059. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  30060. for (i = 0; i < 4; i++) {
  30061. AssertIntEQ(bufPt[i], 0);
  30062. }
  30063. /* read index should not have advanced with nread0 */
  30064. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  30065. for (i = 0; i < 4; i++) {
  30066. AssertIntEQ(bufPt[i], 0);
  30067. }
  30068. /* write and fill up buffer checking reset of index state */
  30069. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  30070. XMEMCPY(bufPt, buff, 20);
  30071. /* test reset on data in bio1 write buffer */
  30072. AssertIntEQ(BIO_reset(bio1), 0);
  30073. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  30074. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  30075. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  30076. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  30077. AssertNotNull(p);
  30078. XMEMCPY(bufPt, buff, 20);
  30079. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  30080. for (i = 0; i < 6; i++) {
  30081. AssertIntEQ(bufPt[i], i);
  30082. }
  30083. /* test case of writing twice with offset read index */
  30084. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  30085. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  30086. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  30087. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  30088. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  30089. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  30090. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  30091. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  30092. BIO_free(bio1);
  30093. BIO_free(bio3);
  30094. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  30095. {
  30096. BIO* bioA = NULL;
  30097. BIO* bioB = NULL;
  30098. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  30099. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  30100. BIO_free(bioA);
  30101. bioA = NULL;
  30102. BIO_free(bioB);
  30103. bioB = NULL;
  30104. }
  30105. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  30106. /* BIOs with file pointers */
  30107. #if !defined(NO_FILESYSTEM)
  30108. {
  30109. XFILE f1;
  30110. XFILE f2;
  30111. BIO* f_bio1;
  30112. BIO* f_bio2;
  30113. unsigned char cert[300];
  30114. char testFile[] = "tests/bio_write_test.txt";
  30115. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  30116. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  30117. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  30118. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  30119. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  30120. f1 = XFOPEN(svrCertFile, "rwb");
  30121. AssertTrue((f1 != XBADFILE));
  30122. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  30123. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  30124. WOLFSSL_SUCCESS);
  30125. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  30126. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  30127. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  30128. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  30129. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  30130. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  30131. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  30132. AssertIntEQ(BIO_reset(f_bio2), 0);
  30133. AssertIntEQ(BIO_tell(NULL),-1);
  30134. AssertIntEQ(BIO_tell(f_bio2),0);
  30135. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  30136. AssertIntEQ(BIO_tell(f_bio2),4);
  30137. BIO_free(f_bio1);
  30138. BIO_free(f_bio2);
  30139. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  30140. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  30141. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  30142. BIO_free(f_bio1);
  30143. }
  30144. #endif /* !defined(NO_FILESYSTEM) */
  30145. /* BIO info callback */
  30146. {
  30147. const char* testArg = "test";
  30148. BIO* cb_bio;
  30149. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  30150. BIO_set_callback(cb_bio, bioCallback);
  30151. AssertNotNull(BIO_get_callback(cb_bio));
  30152. BIO_set_callback(cb_bio, NULL);
  30153. AssertNull(BIO_get_callback(cb_bio));
  30154. BIO_set_callback_arg(cb_bio, (char*)testArg);
  30155. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  30156. AssertNull(BIO_get_callback_arg(NULL));
  30157. BIO_free(cb_bio);
  30158. }
  30159. /* BIO_vfree */
  30160. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  30161. BIO_vfree(NULL);
  30162. BIO_vfree(bio1);
  30163. printf(resultFmt, passed);
  30164. #endif
  30165. }
  30166. #endif /* !NO_BIO */
  30167. static void test_wolfSSL_ASN1_STRING(void)
  30168. {
  30169. #if defined(OPENSSL_EXTRA)
  30170. ASN1_STRING* str = NULL;
  30171. const char data[] = "hello wolfSSL";
  30172. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  30173. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  30174. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  30175. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  30176. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  30177. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  30178. ASN1_STRING_free(str);
  30179. printf(resultFmt, passed);
  30180. #endif
  30181. }
  30182. static void test_wolfSSL_ASN1_BIT_STRING(void)
  30183. {
  30184. #ifdef OPENSSL_ALL
  30185. ASN1_BIT_STRING* str;
  30186. printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
  30187. AssertNotNull(str = ASN1_BIT_STRING_new());
  30188. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  30189. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  30190. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  30191. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  30192. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  30193. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  30194. ASN1_BIT_STRING_free(str);
  30195. printf(resultFmt, passed);
  30196. #endif
  30197. }
  30198. static void test_wolfSSL_a2i_ASN1_INTEGER(void)
  30199. {
  30200. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  30201. BIO *bio, *out;
  30202. ASN1_INTEGER* ai;
  30203. char buf[] = "123456\n12345\n112345678912345678901234567890\n";
  30204. char tmp[1024];
  30205. int tmpSz;
  30206. const char expected1[] = "123456";
  30207. const char expected2[] = "112345678912345678901234567890";
  30208. printf(testingFmt, "test_wolfSSL_a2i_ASN1_INTEGER()");
  30209. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  30210. AssertNotNull(out = BIO_new(BIO_s_mem()));
  30211. AssertNotNull(ai = ASN1_INTEGER_new());
  30212. /* read first line */
  30213. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  30214. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  30215. XMEMSET(tmp, 0, 1024);
  30216. tmpSz = BIO_read(out, tmp, 1024);
  30217. AssertIntEQ(tmpSz, 6);
  30218. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  30219. /* fail on second line (not % 2) */
  30220. AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  30221. /* read 3rd long line */
  30222. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  30223. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  30224. XMEMSET(tmp, 0, 1024);
  30225. tmpSz = BIO_read(out, tmp, 1024);
  30226. AssertIntEQ(tmpSz, 30);
  30227. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  30228. BIO_free(out);
  30229. BIO_free(bio);
  30230. ASN1_INTEGER_free(ai);
  30231. printf(resultFmt, passed);
  30232. #endif
  30233. }
  30234. static void test_wolfSSL_a2i_IPADDRESS(void)
  30235. {
  30236. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  30237. const unsigned char* data;
  30238. int dataSz = 0;
  30239. ASN1_OCTET_STRING *st;
  30240. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  30241. const unsigned char ipv6_exp[] = {
  30242. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  30243. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  30244. };
  30245. const unsigned char ipv6_home[] = {
  30246. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  30247. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  30248. };
  30249. printf(testingFmt, "test_wolfSSL_a2i_IPADDRESS()");
  30250. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  30251. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  30252. data = ASN1_STRING_get0_data(st);
  30253. dataSz = ASN1_STRING_length(st);
  30254. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  30255. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  30256. ASN1_STRING_free(st);
  30257. AssertNotNull(st = a2i_IPADDRESS("::1"));
  30258. data = ASN1_STRING_get0_data(st);
  30259. dataSz = ASN1_STRING_length(st);
  30260. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  30261. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  30262. ASN1_STRING_free(st);
  30263. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  30264. data = ASN1_STRING_get0_data(st);
  30265. dataSz = ASN1_STRING_length(st);
  30266. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  30267. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  30268. ASN1_STRING_free(st);
  30269. printf(resultFmt, passed);
  30270. #endif
  30271. }
  30272. static void test_wolfSSL_DES_ecb_encrypt(void)
  30273. {
  30274. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  30275. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  30276. WOLFSSL_DES_key_schedule key;
  30277. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  30278. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  30279. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  30280. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  30281. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  30282. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  30283. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  30284. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  30285. /* Encrypt messages */
  30286. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  30287. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  30288. /* Decrypt messages */
  30289. int ret1 = 0;
  30290. int ret2 = 0;
  30291. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  30292. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  30293. AssertIntEQ(ret1,0);
  30294. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  30295. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  30296. AssertIntEQ(ret2,0);
  30297. printf(resultFmt, passed);
  30298. #endif
  30299. }
  30300. static void test_wolfSSL_ASN1_TIME_adj(void)
  30301. {
  30302. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  30303. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  30304. const int year = 365*24*60*60;
  30305. const int day = 24*60*60;
  30306. const int hour = 60*60;
  30307. const int mini = 60;
  30308. const byte asn_utc_time = ASN_UTC_TIME;
  30309. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  30310. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  30311. #endif
  30312. WOLFSSL_ASN1_TIME *asn_time, *s;
  30313. int offset_day;
  30314. long offset_sec;
  30315. char date_str[CTC_DATE_SIZE + 1];
  30316. time_t t;
  30317. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  30318. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  30319. /* UTC notation test */
  30320. /* 2000/2/15 20:30:00 */
  30321. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  30322. offset_day = 7;
  30323. offset_sec = 45 * mini;
  30324. /* offset_sec = -45 * min;*/
  30325. AssertNotNull(asn_time =
  30326. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  30327. AssertTrue(asn_time->type == asn_utc_time);
  30328. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30329. date_str[CTC_DATE_SIZE] = '\0';
  30330. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  30331. /* negative offset */
  30332. offset_sec = -45 * mini;
  30333. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  30334. AssertTrue(asn_time->type == asn_utc_time);
  30335. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30336. date_str[CTC_DATE_SIZE] = '\0';
  30337. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  30338. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  30339. XMEMSET(date_str, 0, sizeof(date_str));
  30340. /* Generalized time will overflow time_t if not long */
  30341. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  30342. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  30343. DYNAMIC_TYPE_OPENSSL);
  30344. /* GeneralizedTime notation test */
  30345. /* 2055/03/01 09:00:00 */
  30346. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  30347. offset_day = 12;
  30348. offset_sec = 10 * mini;
  30349. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  30350. AssertTrue(asn_time->type == asn_gen_time);
  30351. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30352. date_str[CTC_DATE_SIZE] = '\0';
  30353. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  30354. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  30355. XMEMSET(date_str, 0, sizeof(date_str));
  30356. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  30357. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  30358. s = NULL;
  30359. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  30360. offset_day = 7;
  30361. offset_sec = 45 * mini;
  30362. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  30363. AssertTrue(asn_time->type == asn_utc_time);
  30364. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30365. date_str[CTC_DATE_SIZE] = '\0';
  30366. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  30367. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  30368. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  30369. AssertTrue(asn_time->type == asn_utc_time);
  30370. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  30371. date_str[CTC_DATE_SIZE] = '\0';
  30372. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  30373. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  30374. printf(resultFmt, passed);
  30375. #endif
  30376. }
  30377. static void test_wolfSSL_ASN1_TIME_to_tm(void)
  30378. {
  30379. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  30380. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) \
  30381. && !defined(NO_ASN_TIME)
  30382. ASN1_TIME asnTime;
  30383. struct tm tm;
  30384. printf(testingFmt, "wolfSSL_ASN1_TIME_to_tm()");
  30385. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  30386. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  30387. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  30388. AssertIntEQ(tm.tm_sec, 15);
  30389. AssertIntEQ(tm.tm_min, 15);
  30390. AssertIntEQ(tm.tm_hour, 21);
  30391. AssertIntEQ(tm.tm_mday, 22);
  30392. AssertIntEQ(tm.tm_mon, 1);
  30393. AssertIntEQ(tm.tm_year, 100);
  30394. AssertIntEQ(tm.tm_isdst, 0);
  30395. #ifdef XMKTIME
  30396. AssertIntEQ(tm.tm_wday, 2);
  30397. AssertIntEQ(tm.tm_yday, 52);
  30398. #endif
  30399. printf(resultFmt, passed);
  30400. #endif
  30401. }
  30402. static void test_wolfSSL_X509_cmp_time(void)
  30403. {
  30404. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  30405. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  30406. WOLFSSL_ASN1_TIME asn_time;
  30407. time_t t;
  30408. printf(testingFmt, "wolfSSL_X509_cmp_time()");
  30409. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  30410. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  30411. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  30412. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  30413. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  30414. printf(resultFmt, passed);
  30415. #endif
  30416. }
  30417. static void test_wolfSSL_X509_time_adj(void)
  30418. {
  30419. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  30420. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  30421. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  30422. !defined(NO_ASN_TIME)
  30423. X509* x509;
  30424. time_t t, not_before, not_after;
  30425. printf(testingFmt, "wolfSSL_X509_time_adj()");
  30426. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  30427. client_cert_der_2048, sizeof_client_cert_der_2048,
  30428. WOLFSSL_FILETYPE_ASN1));
  30429. t = 0;
  30430. not_before = XTIME(0);
  30431. not_after = XTIME(0) + (60 * 24 * 30); /* 30 days after */
  30432. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  30433. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  30434. /* Check X509_gmtime_adj, too. */
  30435. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  30436. X509_free(x509);
  30437. printf(resultFmt, passed);
  30438. #endif
  30439. }
  30440. static void test_wolfSSL_X509(void)
  30441. {
  30442. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  30443. && !defined(NO_RSA)
  30444. X509* x509;
  30445. #ifndef NO_BIO
  30446. BIO* bio;
  30447. X509_STORE_CTX* ctx;
  30448. X509_STORE* store;
  30449. #endif
  30450. char der[] = "certs/ca-cert.der";
  30451. XFILE fp;
  30452. printf(testingFmt, "wolfSSL_X509()");
  30453. AssertNotNull(x509 = X509_new());
  30454. X509_free(x509);
  30455. #ifndef NO_BIO
  30456. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  30457. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  30458. #ifdef WOLFSSL_CERT_GEN
  30459. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  30460. #endif
  30461. AssertNotNull(ctx = X509_STORE_CTX_new());
  30462. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  30463. AssertNotNull(store = X509_STORE_new());
  30464. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  30465. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  30466. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  30467. X509_STORE_CTX_free(ctx);
  30468. X509_STORE_free(store);
  30469. X509_free(x509);
  30470. BIO_free(bio);
  30471. #endif
  30472. /** d2i_X509_fp test **/
  30473. fp = XFOPEN(der, "rb");
  30474. AssertTrue((fp != XBADFILE));
  30475. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  30476. AssertNotNull(x509);
  30477. X509_free(x509);
  30478. XFCLOSE(fp);
  30479. fp = XFOPEN(der, "rb");
  30480. AssertTrue((fp != XBADFILE));
  30481. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  30482. AssertNotNull(x509);
  30483. X509_free(x509);
  30484. XFCLOSE(fp);
  30485. /* X509_up_ref test */
  30486. AssertIntEQ(X509_up_ref(NULL), 0);
  30487. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  30488. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  30489. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  30490. X509_free(x509); /* refCount = 2 */
  30491. X509_free(x509); /* refCount = 1 */
  30492. X509_free(x509); /* refCount = 0, free */
  30493. printf(resultFmt, passed);
  30494. #endif
  30495. }
  30496. static void test_wolfSSL_X509_get_ext_count(void)
  30497. {
  30498. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30499. !defined(NO_RSA)
  30500. int ret = 0;
  30501. WOLFSSL_X509* x509;
  30502. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  30503. FILE* f;
  30504. printf(testingFmt, "wolfSSL_X509_get_ext_count()");
  30505. /* NULL parameter check */
  30506. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  30507. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  30508. SSL_FILETYPE_PEM));
  30509. AssertIntEQ(X509_get_ext_count(x509), 5);
  30510. wolfSSL_X509_free(x509);
  30511. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  30512. SSL_FILETYPE_PEM));
  30513. AssertIntEQ(X509_get_ext_count(x509), 5);
  30514. wolfSSL_X509_free(x509);
  30515. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  30516. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  30517. fclose(f);
  30518. printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
  30519. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  30520. printf(resultFmt, ret == 4 ? passed : failed);
  30521. printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
  30522. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  30523. printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
  30524. wolfSSL_X509_free(x509);
  30525. printf(resultFmt, passed);
  30526. #endif
  30527. }
  30528. static void test_wolfSSL_X509_sign2(void)
  30529. {
  30530. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30531. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  30532. defined(WOLFSSL_CERT_EXT) && \
  30533. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  30534. WOLFSSL_X509 *x509, *ca;
  30535. const unsigned char *der;
  30536. const unsigned char *pt;
  30537. WOLFSSL_EVP_PKEY *priv;
  30538. WOLFSSL_X509_NAME *name;
  30539. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  30540. int derSz;
  30541. const int year = 365*24*60*60;
  30542. const int day = 24*60*60;
  30543. const int hour = 60*60;
  30544. const int mini = 60;
  30545. time_t t;
  30546. const unsigned char expected[] = {
  30547. #ifdef WOLFSSL_AKID_NAME
  30548. 0x30, 0x82, 0x04, 0xfd, 0x30, 0x82, 0x03, 0xe5, 0xa0, 0x03, 0x02, 0x01,
  30549. 0x02, 0x02, 0x09, 0x00, 0xf1, 0x5c, 0x99, 0x43, 0x66, 0x3d, 0x96, 0x04,
  30550. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  30551. 0x0b, 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
  30552. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06,
  30553. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e,
  30554. 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07,
  30555. 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30, 0x0f, 0x06,
  30556. 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f,
  30557. 0x74, 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c,
  30558. 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31,
  30559. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77,
  30560. 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f,
  30561. 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  30562. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  30563. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e,
  30564. 0x17, 0x0d, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30,
  30565. 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32,
  30566. 0x30, 0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30,
  30567. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10,
  30568. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e,
  30569. 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  30570. 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x15,
  30571. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c, 0x77, 0x6f, 0x6c,
  30572. 0x66, 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30,
  30573. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50, 0x72, 0x6f, 0x67,
  30574. 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32, 0x30, 0x34, 0x38,
  30575. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77,
  30576. 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63,
  30577. 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  30578. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  30579. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30,
  30580. 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  30581. 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30,
  30582. 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc3, 0x03, 0xd1, 0x2b,
  30583. 0xfe, 0x39, 0xa4, 0x32, 0x45, 0x3b, 0x53, 0xc8, 0x84, 0x2b, 0x2a, 0x7c,
  30584. 0x74, 0x9a, 0xbd, 0xaa, 0x2a, 0x52, 0x07, 0x47, 0xd6, 0xa6, 0x36, 0xb2,
  30585. 0x07, 0x32, 0x8e, 0xd0, 0xba, 0x69, 0x7b, 0xc6, 0xc3, 0x44, 0x9e, 0xd4,
  30586. 0x81, 0x48, 0xfd, 0x2d, 0x68, 0xa2, 0x8b, 0x67, 0xbb, 0xa1, 0x75, 0xc8,
  30587. 0x36, 0x2c, 0x4a, 0xd2, 0x1b, 0xf7, 0x8b, 0xba, 0xcf, 0x0d, 0xf9, 0xef,
  30588. 0xec, 0xf1, 0x81, 0x1e, 0x7b, 0x9b, 0x03, 0x47, 0x9a, 0xbf, 0x65, 0xcc,
  30589. 0x7f, 0x65, 0x24, 0x69, 0xa6, 0xe8, 0x14, 0x89, 0x5b, 0xe4, 0x34, 0xf7,
  30590. 0xc5, 0xb0, 0x14, 0x93, 0xf5, 0x67, 0x7b, 0x3a, 0x7a, 0x78, 0xe1, 0x01,
  30591. 0x56, 0x56, 0x91, 0xa6, 0x13, 0x42, 0x8d, 0xd2, 0x3c, 0x40, 0x9c, 0x4c,
  30592. 0xef, 0xd1, 0x86, 0xdf, 0x37, 0x51, 0x1b, 0x0c, 0xa1, 0x3b, 0xf5, 0xf1,
  30593. 0xa3, 0x4a, 0x35, 0xe4, 0xe1, 0xce, 0x96, 0xdf, 0x1b, 0x7e, 0xbf, 0x4e,
  30594. 0x97, 0xd0, 0x10, 0xe8, 0xa8, 0x08, 0x30, 0x81, 0xaf, 0x20, 0x0b, 0x43,
  30595. 0x14, 0xc5, 0x74, 0x67, 0xb4, 0x32, 0x82, 0x6f, 0x8d, 0x86, 0xc2, 0x88,
  30596. 0x40, 0x99, 0x36, 0x83, 0xba, 0x1e, 0x40, 0x72, 0x22, 0x17, 0xd7, 0x52,
  30597. 0x65, 0x24, 0x73, 0xb0, 0xce, 0xef, 0x19, 0xcd, 0xae, 0xff, 0x78, 0x6c,
  30598. 0x7b, 0xc0, 0x12, 0x03, 0xd4, 0x4e, 0x72, 0x0d, 0x50, 0x6d, 0x3b, 0xa3,
  30599. 0x3b, 0xa3, 0x99, 0x5e, 0x9d, 0xc8, 0xd9, 0x0c, 0x85, 0xb3, 0xd9, 0x8a,
  30600. 0xd9, 0x54, 0x26, 0xdb, 0x6d, 0xfa, 0xac, 0xbb, 0xff, 0x25, 0x4c, 0xc4,
  30601. 0xd1, 0x79, 0xf4, 0x71, 0xd3, 0x86, 0x40, 0x18, 0x13, 0xb0, 0x63, 0xb5,
  30602. 0x72, 0x4e, 0x30, 0xc4, 0x97, 0x84, 0x86, 0x2d, 0x56, 0x2f, 0xd7, 0x15,
  30603. 0xf7, 0x7f, 0xc0, 0xae, 0xf5, 0xfc, 0x5b, 0xe5, 0xfb, 0xa1, 0xba, 0xd3,
  30604. 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x44, 0x30, 0x82, 0x01,
  30605. 0x40, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03,
  30606. 0x01, 0x01, 0xff, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x15,
  30607. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  30608. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x30, 0x1d, 0x06,
  30609. 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x33, 0xd8, 0x45, 0x66,
  30610. 0xd7, 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26,
  30611. 0xd7, 0x85, 0x65, 0xc0, 0x30, 0x81, 0xd3, 0x06, 0x03, 0x55, 0x1d, 0x23,
  30612. 0x04, 0x81, 0xcb, 0x30, 0x81, 0xc8, 0x80, 0x14, 0x33, 0xd8, 0x45, 0x66,
  30613. 0xd7, 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26,
  30614. 0xd7, 0x85, 0x65, 0xc0, 0xa1, 0x81, 0xa4, 0xa4, 0x81, 0xa1, 0x30, 0x81,
  30615. 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  30616. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  30617. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  30618. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  30619. 0x61, 0x6e, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  30620. 0x0c, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34,
  30621. 0x38, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10,
  30622. 0x50, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d,
  30623. 0x32, 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  30624. 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73,
  30625. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  30626. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69,
  30627. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  30628. 0x63, 0x6f, 0x6d, 0x82, 0x09, 0x00, 0xf1, 0x5c, 0x99, 0x43, 0x66, 0x3d,
  30629. 0x96, 0x04, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30,
  30630. 0x14, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06,
  30631. 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0d, 0x06,
  30632. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
  30633. 0x03, 0x82, 0x01, 0x01, 0x00, 0x59, 0x2e, 0xd1, 0xec, 0xbc, 0x99, 0xfe,
  30634. 0x50, 0x38, 0x47, 0x47, 0x88, 0x51, 0xcf, 0xe4, 0x88, 0x76, 0xdf, 0x89,
  30635. 0x8f, 0xea, 0x91, 0xbc, 0xd6, 0xc6, 0x91, 0xc9, 0xcc, 0x33, 0x77, 0x5d,
  30636. 0xdd, 0x4b, 0xc9, 0xf6, 0x10, 0x54, 0xe2, 0x04, 0x89, 0x51, 0xdb, 0xe1,
  30637. 0x00, 0x0c, 0x61, 0x03, 0x26, 0x86, 0x35, 0xac, 0x96, 0x23, 0x9d, 0xef,
  30638. 0xd9, 0x95, 0xe4, 0xb4, 0x83, 0x9e, 0x0f, 0x47, 0x30, 0x08, 0x96, 0x28,
  30639. 0x7f, 0x2d, 0xe3, 0x23, 0x30, 0x3b, 0xb0, 0x46, 0xe8, 0x21, 0x78, 0xb4,
  30640. 0xc0, 0xbc, 0x9f, 0x60, 0x02, 0xd4, 0x16, 0x2d, 0xe5, 0x5a, 0x00, 0x65,
  30641. 0x15, 0x95, 0x81, 0x93, 0x80, 0x06, 0x3e, 0xf7, 0xdf, 0x0c, 0x2b, 0x3f,
  30642. 0x14, 0xfc, 0xc3, 0x79, 0xfd, 0x59, 0x5c, 0xa7, 0xc3, 0xe0, 0xa8, 0xd4,
  30643. 0x53, 0x4f, 0x13, 0x0a, 0xa3, 0xfe, 0x1d, 0x63, 0x4e, 0x84, 0xb2, 0x98,
  30644. 0x19, 0x06, 0xe0, 0x60, 0x3a, 0xc9, 0x49, 0x73, 0x00, 0xe3, 0x72, 0x2f,
  30645. 0x68, 0x27, 0x9f, 0x14, 0x18, 0xb7, 0x57, 0xb9, 0x1d, 0xa8, 0xb3, 0x05,
  30646. 0x6c, 0xf5, 0x4b, 0x0e, 0xac, 0x26, 0x7a, 0xfe, 0xc1, 0xab, 0x1f, 0x27,
  30647. 0xf1, 0x1e, 0x21, 0x33, 0x31, 0xb6, 0x43, 0xb0, 0xf8, 0x74, 0x69, 0x6a,
  30648. 0xb1, 0x9b, 0xcb, 0xe4, 0xd3, 0xa2, 0x8e, 0x8a, 0x55, 0xef, 0x81, 0xf3,
  30649. 0x4a, 0x44, 0x90, 0x4d, 0x08, 0xb8, 0x31, 0x90, 0x1a, 0x82, 0x52, 0x56,
  30650. 0xeb, 0xf0, 0x50, 0x5b, 0x9f, 0x87, 0x98, 0x54, 0xfe, 0x6a, 0x60, 0x41,
  30651. 0x16, 0xdb, 0xdc, 0xff, 0x89, 0x4c, 0x98, 0x00, 0xb1, 0x87, 0x6c, 0xe7,
  30652. 0xec, 0xba, 0x3b, 0xa4, 0xfe, 0xa1, 0xfd, 0x26, 0x19, 0x7c, 0x2d, 0x14,
  30653. 0x91, 0x91, 0x61, 0x30, 0x3e, 0xf4, 0x5c, 0x97, 0x4c, 0x06, 0x84, 0xab,
  30654. 0x94, 0xa8, 0x17, 0x6c, 0xec, 0x19, 0xc0, 0x87, 0xd0
  30655. #else
  30656. 0x30, 0x82, 0x04, 0x46, 0x30, 0x82, 0x03, 0x2e, 0xa0, 0x03, 0x02, 0x01,
  30657. 0x02, 0x02, 0x09, 0x00, 0xf1, 0x5c, 0x99, 0x43, 0x66, 0x3d, 0x96, 0x04,
  30658. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  30659. 0x0b, 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
  30660. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06,
  30661. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e,
  30662. 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07,
  30663. 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30, 0x0f, 0x06,
  30664. 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f,
  30665. 0x74, 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c,
  30666. 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31,
  30667. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77,
  30668. 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f,
  30669. 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  30670. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  30671. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e,
  30672. 0x17, 0x0d, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30,
  30673. 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32,
  30674. 0x30, 0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30,
  30675. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10,
  30676. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e,
  30677. 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  30678. 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x15,
  30679. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c, 0x77, 0x6f, 0x6c,
  30680. 0x66, 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30,
  30681. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50, 0x72, 0x6f, 0x67,
  30682. 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32, 0x30, 0x34, 0x38,
  30683. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77,
  30684. 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63,
  30685. 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  30686. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  30687. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30,
  30688. 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  30689. 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30,
  30690. 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc3, 0x03, 0xd1, 0x2b,
  30691. 0xfe, 0x39, 0xa4, 0x32, 0x45, 0x3b, 0x53, 0xc8, 0x84, 0x2b, 0x2a, 0x7c,
  30692. 0x74, 0x9a, 0xbd, 0xaa, 0x2a, 0x52, 0x07, 0x47, 0xd6, 0xa6, 0x36, 0xb2,
  30693. 0x07, 0x32, 0x8e, 0xd0, 0xba, 0x69, 0x7b, 0xc6, 0xc3, 0x44, 0x9e, 0xd4,
  30694. 0x81, 0x48, 0xfd, 0x2d, 0x68, 0xa2, 0x8b, 0x67, 0xbb, 0xa1, 0x75, 0xc8,
  30695. 0x36, 0x2c, 0x4a, 0xd2, 0x1b, 0xf7, 0x8b, 0xba, 0xcf, 0x0d, 0xf9, 0xef,
  30696. 0xec, 0xf1, 0x81, 0x1e, 0x7b, 0x9b, 0x03, 0x47, 0x9a, 0xbf, 0x65, 0xcc,
  30697. 0x7f, 0x65, 0x24, 0x69, 0xa6, 0xe8, 0x14, 0x89, 0x5b, 0xe4, 0x34, 0xf7,
  30698. 0xc5, 0xb0, 0x14, 0x93, 0xf5, 0x67, 0x7b, 0x3a, 0x7a, 0x78, 0xe1, 0x01,
  30699. 0x56, 0x56, 0x91, 0xa6, 0x13, 0x42, 0x8d, 0xd2, 0x3c, 0x40, 0x9c, 0x4c,
  30700. 0xef, 0xd1, 0x86, 0xdf, 0x37, 0x51, 0x1b, 0x0c, 0xa1, 0x3b, 0xf5, 0xf1,
  30701. 0xa3, 0x4a, 0x35, 0xe4, 0xe1, 0xce, 0x96, 0xdf, 0x1b, 0x7e, 0xbf, 0x4e,
  30702. 0x97, 0xd0, 0x10, 0xe8, 0xa8, 0x08, 0x30, 0x81, 0xaf, 0x20, 0x0b, 0x43,
  30703. 0x14, 0xc5, 0x74, 0x67, 0xb4, 0x32, 0x82, 0x6f, 0x8d, 0x86, 0xc2, 0x88,
  30704. 0x40, 0x99, 0x36, 0x83, 0xba, 0x1e, 0x40, 0x72, 0x22, 0x17, 0xd7, 0x52,
  30705. 0x65, 0x24, 0x73, 0xb0, 0xce, 0xef, 0x19, 0xcd, 0xae, 0xff, 0x78, 0x6c,
  30706. 0x7b, 0xc0, 0x12, 0x03, 0xd4, 0x4e, 0x72, 0x0d, 0x50, 0x6d, 0x3b, 0xa3,
  30707. 0x3b, 0xa3, 0x99, 0x5e, 0x9d, 0xc8, 0xd9, 0x0c, 0x85, 0xb3, 0xd9, 0x8a,
  30708. 0xd9, 0x54, 0x26, 0xdb, 0x6d, 0xfa, 0xac, 0xbb, 0xff, 0x25, 0x4c, 0xc4,
  30709. 0xd1, 0x79, 0xf4, 0x71, 0xd3, 0x86, 0x40, 0x18, 0x13, 0xb0, 0x63, 0xb5,
  30710. 0x72, 0x4e, 0x30, 0xc4, 0x97, 0x84, 0x86, 0x2d, 0x56, 0x2f, 0xd7, 0x15,
  30711. 0xf7, 0x7f, 0xc0, 0xae, 0xf5, 0xfc, 0x5b, 0xe5, 0xfb, 0xa1, 0xba, 0xd3,
  30712. 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x81, 0x8e, 0x30, 0x81, 0x8b, 0x30,
  30713. 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01,
  30714. 0xff, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x15, 0x30, 0x13,
  30715. 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f,
  30716. 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x30, 0x1d, 0x06, 0x03, 0x55,
  30717. 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7, 0x68,
  30718. 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7, 0x85,
  30719. 0x65, 0xc0, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30,
  30720. 0x16, 0x80, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7, 0x68, 0x87, 0x18, 0x7e,
  30721. 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7, 0x85, 0x65, 0xc0, 0x30,
  30722. 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06, 0x08,
  30723. 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b, 0x06,
  30724. 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  30725. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01,
  30726. 0x01, 0x00, 0x98, 0x2a, 0x3d, 0x94, 0x37, 0xae, 0xd6, 0x28, 0x12, 0xed,
  30727. 0x6d, 0x95, 0xc9, 0x05, 0x89, 0x4b, 0x5c, 0x5e, 0x88, 0xed, 0x9e, 0x14,
  30728. 0x89, 0x79, 0x65, 0x7b, 0x5c, 0xdb, 0xcd, 0x21, 0xc5, 0xfc, 0x7a, 0x05,
  30729. 0xd2, 0x33, 0x54, 0xa1, 0x1b, 0xb2, 0xc6, 0xd8, 0x3e, 0x88, 0x7d, 0x58,
  30730. 0xfd, 0xd0, 0xca, 0x71, 0x58, 0xd5, 0x37, 0x81, 0xe0, 0xef, 0x65, 0xfc,
  30731. 0x1b, 0xf1, 0x5d, 0xdd, 0x26, 0x68, 0x12, 0xfb, 0x12, 0x24, 0xd5, 0x45,
  30732. 0x4f, 0x41, 0xad, 0xee, 0x3f, 0x16, 0x40, 0xb2, 0x59, 0xe6, 0x5b, 0x76,
  30733. 0xe7, 0x47, 0x11, 0xa4, 0xe1, 0x2f, 0x0d, 0xe8, 0x13, 0x13, 0x49, 0xb0,
  30734. 0x01, 0x11, 0x15, 0xb5, 0xb3, 0x93, 0x4f, 0x28, 0xdc, 0xd0, 0x30, 0x03,
  30735. 0x48, 0x02, 0x95, 0x2d, 0xd9, 0x26, 0x87, 0x1f, 0x19, 0xa1, 0x03, 0x5c,
  30736. 0x7c, 0xde, 0x54, 0xd4, 0x98, 0x85, 0x34, 0xcc, 0x54, 0xf1, 0x24, 0x43,
  30737. 0xa6, 0x87, 0xfa, 0xb6, 0x62, 0xee, 0xa3, 0x4a, 0xb3, 0xce, 0x1c, 0x2e,
  30738. 0xbf, 0x94, 0xef, 0x4c, 0x75, 0x75, 0x55, 0x1d, 0xc9, 0xc2, 0xe4, 0xe5,
  30739. 0x24, 0xb2, 0x0a, 0x93, 0xf0, 0xff, 0x2e, 0x43, 0x99, 0xad, 0x4e, 0x83,
  30740. 0x11, 0x52, 0xf4, 0xb9, 0x92, 0x30, 0xe1, 0x02, 0x2f, 0xa5, 0xf2, 0x21,
  30741. 0xb1, 0xf4, 0xe9, 0x57, 0xbd, 0xba, 0x17, 0x56, 0xd7, 0x31, 0xcb, 0x63,
  30742. 0xa3, 0xd5, 0xcf, 0xc9, 0xd9, 0xa6, 0x4f, 0x51, 0x6c, 0x52, 0x4c, 0x53,
  30743. 0x88, 0x9a, 0x2e, 0xb9, 0x72, 0x02, 0x6e, 0x1b, 0x21, 0x93, 0xa1, 0x88,
  30744. 0x1b, 0x35, 0x0e, 0x9e, 0x2b, 0x63, 0x81, 0xba, 0xb4, 0x6b, 0x28, 0x01,
  30745. 0x56, 0xe1, 0x0e, 0x13, 0x73, 0xf6, 0xd6, 0xa0, 0xd2, 0xfd, 0xc9, 0x4d,
  30746. 0xbd, 0xa8, 0xa9, 0x22, 0x9e, 0xc7, 0x13, 0x76, 0x5a, 0x9c, 0xd3, 0x9a,
  30747. 0xf4, 0x0c, 0x52, 0xe6, 0x47, 0xcb
  30748. #endif
  30749. };
  30750. printf(testingFmt, "wolfSSL_X509_sign2");
  30751. pt = ca_key_der_2048;
  30752. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  30753. sizeof_ca_key_der_2048));
  30754. pt = client_cert_der_2048;
  30755. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  30756. sizeof_client_cert_der_2048));
  30757. pt = ca_cert_der_2048;
  30758. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  30759. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  30760. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  30761. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  30762. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  30763. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  30764. AssertIntEQ(notAfter->length, 13);
  30765. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  30766. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  30767. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  30768. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  30769. AssertIntEQ(derSz, sizeof(expected));
  30770. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  30771. wolfSSL_X509_free(ca);
  30772. wolfSSL_X509_free(x509);
  30773. wolfSSL_EVP_PKEY_free(priv);
  30774. wolfSSL_ASN1_TIME_free(notBefore);
  30775. wolfSSL_ASN1_TIME_free(notAfter);
  30776. printf(resultFmt, passed);
  30777. #endif
  30778. }
  30779. static void test_wolfSSL_X509_sign(void)
  30780. {
  30781. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30782. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  30783. int ret;
  30784. char *cn;
  30785. word32 cnSz;
  30786. X509_NAME *name;
  30787. X509 *x509, *ca;
  30788. DecodedCert dCert;
  30789. EVP_PKEY *pub;
  30790. EVP_PKEY *priv;
  30791. EVP_MD_CTX *mctx;
  30792. #if defined(USE_CERT_BUFFERS_1024)
  30793. const unsigned char* rsaPriv = client_key_der_1024;
  30794. const unsigned char* rsaPub = client_keypub_der_1024;
  30795. const unsigned char* certIssuer = client_cert_der_1024;
  30796. long clientKeySz = (long)sizeof_client_key_der_1024;
  30797. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  30798. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  30799. #elif defined(USE_CERT_BUFFERS_2048)
  30800. const unsigned char* rsaPriv = client_key_der_2048;
  30801. const unsigned char* rsaPub = client_keypub_der_2048;
  30802. const unsigned char* certIssuer = client_cert_der_2048;
  30803. long clientKeySz = (long)sizeof_client_key_der_2048;
  30804. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  30805. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  30806. #endif
  30807. byte sn[16];
  30808. int snSz = sizeof(sn);
  30809. printf(testingFmt, "wolfSSL_X509_sign");
  30810. /* Set X509_NAME fields */
  30811. AssertNotNull(name = X509_NAME_new());
  30812. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  30813. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  30814. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  30815. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  30816. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  30817. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  30818. /* Get private and public keys */
  30819. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  30820. clientKeySz));
  30821. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  30822. AssertNotNull(x509 = X509_new());
  30823. /* Set version 3 */
  30824. AssertIntNE(X509_set_version(x509, 2L), 0);
  30825. /* Set subject name, add pubkey, and sign certificate */
  30826. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  30827. X509_NAME_free(name);
  30828. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  30829. #ifdef WOLFSSL_ALT_NAMES
  30830. /* Add some subject alt names */
  30831. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  30832. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  30833. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  30834. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  30835. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  30836. "sphygmomanometer",
  30837. ASN_DNS_TYPE), SSL_SUCCESS);
  30838. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  30839. "supercalifragilisticexpialidocious",
  30840. ASN_DNS_TYPE), SSL_SUCCESS);
  30841. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  30842. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  30843. ASN_DNS_TYPE), SSL_SUCCESS);
  30844. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  30845. {
  30846. unsigned char ip4_type[] = {127,128,0,255};
  30847. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  30848. 0xff, 0xee, 0x99, 0x88,
  30849. 0x77, 0x66, 0x55, 0x44,
  30850. 0x00, 0x33, 0x22, 0x11};
  30851. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  30852. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  30853. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  30854. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  30855. }
  30856. #endif
  30857. #endif /* WOLFSSL_ALT_NAMES */
  30858. /* test valid sign case */
  30859. ret = X509_sign(x509, priv, EVP_sha256());
  30860. /* test valid X509_sign_ctx case */
  30861. AssertNotNull(mctx = EVP_MD_CTX_new());
  30862. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  30863. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  30864. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  30865. AssertIntEQ(X509_get_ext_count(x509), 1);
  30866. #endif
  30867. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  30868. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  30869. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  30870. #endif
  30871. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  30872. WOLFSSL_SUCCESS);
  30873. DEBUG_WRITE_CERT_X509(x509, "signed.der");
  30874. /* Variation in size depends on ASN.1 encoding when MSB is set */
  30875. #ifndef WOLFSSL_ALT_NAMES
  30876. /* Valid case - size should be 798-797 with 16 byte serial number */
  30877. AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
  30878. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  30879. /* Valid case - size should be 955-956 with 16 byte serial number */
  30880. AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
  30881. #else
  30882. /* Valid case - size should be 926-927 with 16 byte serial number */
  30883. AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
  30884. #endif
  30885. /* check that issuer name is as expected after signature */
  30886. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  30887. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  30888. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  30889. AssertNotNull(name = X509_get_subject_name(ca));
  30890. cnSz = X509_NAME_get_sz(name);
  30891. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  30892. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  30893. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  30894. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30895. #ifdef WOLFSSL_MULTI_ATTRIB
  30896. /* test adding multiple OU's to the signer */
  30897. AssertNotNull(name = X509_get_subject_name(ca));
  30898. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  30899. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  30900. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  30901. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  30902. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  30903. #endif
  30904. AssertNotNull(name = X509_get_subject_name(ca));
  30905. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  30906. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  30907. AssertNotNull(name = X509_get_issuer_name(x509));
  30908. cnSz = X509_NAME_get_sz(name);
  30909. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  30910. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  30911. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  30912. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  30913. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30914. FreeDecodedCert(&dCert);
  30915. /* Test invalid parameters */
  30916. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  30917. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  30918. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  30919. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  30920. EVP_MD_CTX_free(mctx);
  30921. AssertNotNull(mctx = EVP_MD_CTX_new());
  30922. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  30923. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  30924. /* test invalid version number */
  30925. #if defined(OPENSSL_ALL)
  30926. AssertIntNE(X509_set_version(x509, 6L), 0);
  30927. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  30928. /* uses ParseCert which fails on bad version number */
  30929. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  30930. #endif
  30931. EVP_MD_CTX_free(mctx);
  30932. EVP_PKEY_free(priv);
  30933. EVP_PKEY_free(pub);
  30934. X509_free(x509);
  30935. X509_free(ca);
  30936. printf(resultFmt, passed);
  30937. #endif
  30938. }
  30939. static void test_wolfSSL_X509_get0_tbs_sigalg(void)
  30940. {
  30941. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  30942. X509* x509 = NULL;
  30943. const X509_ALGOR* alg;
  30944. printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
  30945. AssertNotNull(x509 = X509_new());
  30946. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  30947. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  30948. X509_free(x509);
  30949. printf(resultFmt, passed);
  30950. #endif
  30951. }
  30952. static void test_wolfSSL_X509_ALGOR_get0(void)
  30953. {
  30954. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  30955. !defined(NO_SHA256) && !defined(NO_RSA)
  30956. X509* x509 = NULL;
  30957. const ASN1_OBJECT* obj = NULL;
  30958. const X509_ALGOR* alg;
  30959. int pptype = 0;
  30960. const void *ppval = NULL;
  30961. printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
  30962. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30963. SSL_FILETYPE_PEM));
  30964. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  30965. /* Invalid case */
  30966. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  30967. AssertNull(obj);
  30968. /* Valid case */
  30969. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  30970. AssertNotNull(obj);
  30971. AssertNull(ppval);
  30972. AssertIntNE(pptype, 0);
  30973. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  30974. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  30975. X509_free(x509);
  30976. printf(resultFmt, passed);
  30977. #endif
  30978. }
  30979. static void test_wolfSSL_X509_VERIFY_PARAM(void)
  30980. {
  30981. #if defined(OPENSSL_EXTRA)
  30982. X509_VERIFY_PARAM *paramTo;
  30983. X509_VERIFY_PARAM *paramFrom;
  30984. int ret;
  30985. char testIPv4[] = "127.0.0.1";
  30986. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  30987. char testhostName1[] = "foo.hoge.com";
  30988. char testhostName2[] = "foobar.hoge.com";
  30989. printf(testingFmt, "wolfSSL_X509()");
  30990. paramTo = X509_VERIFY_PARAM_new();
  30991. AssertNotNull(paramTo);
  30992. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  30993. paramFrom = X509_VERIFY_PARAM_new();
  30994. AssertNotNull(paramFrom);
  30995. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  30996. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  30997. (int)XSTRLEN(testhostName1));
  30998. AssertIntEQ(1, ret);
  30999. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  31000. (int)XSTRLEN(testhostName1)));
  31001. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  31002. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  31003. AssertIntEQ(0x01, paramFrom->hostFlags);
  31004. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  31005. AssertIntEQ(0, ret);
  31006. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  31007. AssertIntEQ(1, ret);
  31008. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  31009. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  31010. AssertIntEQ(1, ret);
  31011. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  31012. AssertIntEQ(1, ret);
  31013. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  31014. /* null pointer */
  31015. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  31016. AssertIntEQ(WOLFSSL_FAILURE, ret);
  31017. /* in the case of "from" null, returns success */
  31018. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  31019. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  31020. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  31021. AssertIntEQ(WOLFSSL_FAILURE, ret);
  31022. /* inherit flags test : VPARAM_DEFAULT */
  31023. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  31024. AssertIntEQ(1, ret);
  31025. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  31026. (int)XSTRLEN(testhostName1)));
  31027. AssertIntEQ(0x01, paramTo->hostFlags);
  31028. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  31029. /* inherit flags test : VPARAM OVERWRITE */
  31030. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  31031. (int)XSTRLEN(testhostName2));
  31032. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  31033. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  31034. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  31035. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  31036. AssertIntEQ(1, ret);
  31037. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  31038. (int)XSTRLEN(testhostName1)));
  31039. AssertIntEQ(0x01, paramTo->hostFlags);
  31040. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  31041. /* inherit flags test : VPARAM_RESET_FLAGS */
  31042. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  31043. (int)XSTRLEN(testhostName2));
  31044. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  31045. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  31046. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  31047. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  31048. AssertIntEQ(1, ret);
  31049. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  31050. (int)XSTRLEN(testhostName1)));
  31051. AssertIntEQ(0x01, paramTo->hostFlags);
  31052. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  31053. /* inherit flags test : VPARAM_LOCKED */
  31054. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  31055. (int)XSTRLEN(testhostName2));
  31056. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  31057. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  31058. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  31059. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  31060. AssertIntEQ(1, ret);
  31061. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  31062. (int)XSTRLEN(testhostName2)));
  31063. AssertIntEQ(0x00, paramTo->hostFlags);
  31064. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  31065. /* test for incorrect parameters */
  31066. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  31067. AssertIntEQ(0, ret);
  31068. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  31069. AssertIntEQ(0, ret);
  31070. /* inherit flags test : VPARAM_ONCE, not testable yet */
  31071. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  31072. AssertIntEQ(1, ret);
  31073. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  31074. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  31075. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  31076. AssertIntEQ(1, ret);
  31077. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  31078. AssertIntEQ(0, ret);
  31079. X509_VERIFY_PARAM_free(paramTo);
  31080. X509_VERIFY_PARAM_free(paramFrom);
  31081. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  31082. printf(resultFmt, passed);
  31083. #endif
  31084. }
  31085. static void test_wolfSSL_X509_get_X509_PUBKEY(void)
  31086. {
  31087. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  31088. X509* x509 = NULL;
  31089. X509_PUBKEY* pubKey;
  31090. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  31091. AssertNotNull(x509 = X509_new());
  31092. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  31093. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  31094. X509_free(x509);
  31095. printf(resultFmt, passed);
  31096. #endif
  31097. }
  31098. static void test_wolfSSL_X509_PUBKEY_RSA(void)
  31099. {
  31100. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  31101. !defined(NO_SHA256) && !defined(NO_RSA)
  31102. X509* x509 = NULL;
  31103. ASN1_OBJECT* obj = NULL;
  31104. const ASN1_OBJECT* pa_oid = NULL;
  31105. X509_PUBKEY* pubKey;
  31106. X509_PUBKEY* pubKey2;
  31107. EVP_PKEY* evpKey;
  31108. const unsigned char *pk;
  31109. int ppklen, pptype;
  31110. X509_ALGOR *pa;
  31111. const void *pval;
  31112. printf(testingFmt, "wolfSSL_X509_PUBKEY_RSA");
  31113. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  31114. SSL_FILETYPE_PEM));
  31115. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  31116. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  31117. AssertNotNull(pk);
  31118. AssertNotNull(pa);
  31119. AssertNotNull(pubKey);
  31120. AssertIntGT(ppklen, 0);
  31121. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  31122. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  31123. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  31124. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  31125. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  31126. AssertNotNull(pk);
  31127. AssertNotNull(pa);
  31128. AssertIntGT(ppklen, 0);
  31129. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  31130. AssertNotNull(pa_oid);
  31131. AssertNull(pval);
  31132. AssertIntEQ(pptype, V_ASN1_NULL);
  31133. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  31134. X509_PUBKEY_free(pubKey2);
  31135. X509_free(x509);
  31136. EVP_PKEY_free(evpKey);
  31137. printf(resultFmt, passed);
  31138. #endif
  31139. }
  31140. static void test_wolfSSL_X509_PUBKEY_EC(void)
  31141. {
  31142. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  31143. X509* x509 = NULL;
  31144. ASN1_OBJECT* obj = NULL;
  31145. ASN1_OBJECT* poid;
  31146. const ASN1_OBJECT* pa_oid = NULL;
  31147. X509_PUBKEY* pubKey;
  31148. X509_PUBKEY* pubKey2;
  31149. EVP_PKEY* evpKey;
  31150. const unsigned char *pk;
  31151. int ppklen, pptype;
  31152. X509_ALGOR *pa;
  31153. const void *pval;
  31154. char buf[50];
  31155. printf(testingFmt, "wolfSSL_X509_PUBKEY_EC");
  31156. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  31157. SSL_FILETYPE_PEM));
  31158. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  31159. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  31160. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  31161. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  31162. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  31163. AssertNotNull(pk);
  31164. AssertNotNull(pa);
  31165. AssertIntGT(ppklen, 0);
  31166. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  31167. AssertNotNull(pa_oid);
  31168. AssertNotNull(pval);
  31169. AssertIntEQ(pptype, V_ASN1_OBJECT);
  31170. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  31171. poid = (ASN1_OBJECT *)pval;
  31172. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  31173. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  31174. X509_PUBKEY_free(pubKey2);
  31175. X509_free(x509);
  31176. EVP_PKEY_free(evpKey);
  31177. printf(resultFmt, passed);
  31178. #endif
  31179. }
  31180. static void test_wolfSSL_X509_PUBKEY_DSA(void)
  31181. {
  31182. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  31183. word32 bytes;
  31184. #ifdef USE_CERT_BUFFERS_1024
  31185. byte tmp[ONEK_BUF];
  31186. XMEMSET(tmp, 0, sizeof(tmp));
  31187. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31188. bytes = sizeof_dsa_key_der_1024;
  31189. #elif defined(USE_CERT_BUFFERS_2048)
  31190. byte tmp[TWOK_BUF];
  31191. XMEMSET(tmp, 0, sizeof(tmp));
  31192. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31193. bytes = sizeof_dsa_key_der_2048;
  31194. #else
  31195. byte tmp[TWOK_BUF];
  31196. XMEMSET(tmp, 0, sizeof(tmp));
  31197. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  31198. if (fp == XBADFILE) {
  31199. return WOLFSSL_BAD_FILE;
  31200. }
  31201. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  31202. XFCLOSE(fp);
  31203. #endif /* END USE_CERT_BUFFERS_1024 */
  31204. const unsigned char* dsaKeyDer = tmp;
  31205. ASN1_OBJECT* obj = NULL;
  31206. ASN1_STRING* str;
  31207. const ASN1_OBJECT* pa_oid = NULL;
  31208. X509_PUBKEY* pubKey = NULL;
  31209. EVP_PKEY* evpKey = NULL;
  31210. const unsigned char *pk;
  31211. int ppklen, pptype;
  31212. X509_ALGOR *pa;
  31213. const void *pval;
  31214. printf(testingFmt, "wolfSSL_X509_PUBKEY_DSA");
  31215. /* Initialize pkey with der format dsa key */
  31216. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  31217. AssertNotNull(pubKey = X509_PUBKEY_new());
  31218. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  31219. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  31220. AssertNotNull(pk);
  31221. AssertNotNull(pa);
  31222. AssertIntGT(ppklen, 0);
  31223. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  31224. AssertNotNull(pa_oid);
  31225. AssertNotNull(pval);
  31226. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  31227. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  31228. str = (ASN1_STRING *)pval;
  31229. #ifdef USE_CERT_BUFFERS_1024
  31230. AssertIntEQ(ASN1_STRING_length(str), 291);
  31231. #else
  31232. AssertIntEQ(ASN1_STRING_length(str), 549);
  31233. #endif /* END USE_CERT_BUFFERS_1024 */
  31234. X509_PUBKEY_free(pubKey);
  31235. EVP_PKEY_free(evpKey);
  31236. printf(resultFmt, passed);
  31237. #endif
  31238. }
  31239. static void test_wolfSSL_RAND(void)
  31240. {
  31241. #if defined(OPENSSL_EXTRA)
  31242. byte seed[16];
  31243. printf(testingFmt, "wolfSSL_RAND()");
  31244. RAND_seed(seed, sizeof(seed));
  31245. AssertIntEQ(RAND_poll(), 1);
  31246. RAND_cleanup();
  31247. AssertIntEQ(RAND_egd(NULL), -1);
  31248. #ifndef NO_FILESYSTEM
  31249. {
  31250. char fname[100];
  31251. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  31252. AssertIntEQ(RAND_write_file(NULL), 0);
  31253. }
  31254. #endif
  31255. printf(resultFmt, passed);
  31256. #endif
  31257. }
  31258. static void test_wolfSSL_BUF(void)
  31259. {
  31260. #if defined(OPENSSL_EXTRA)
  31261. BUF_MEM* buf;
  31262. AssertNotNull(buf = BUF_MEM_new());
  31263. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  31264. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  31265. BUF_MEM_free(buf);
  31266. #endif /* OPENSSL_EXTRA */
  31267. }
  31268. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  31269. static int stub_rand_seed(const void *buf, int num)
  31270. {
  31271. (void)buf;
  31272. (void)num;
  31273. return 123;
  31274. }
  31275. static int stub_rand_bytes(unsigned char *buf, int num)
  31276. {
  31277. (void)buf;
  31278. (void)num;
  31279. return 456;
  31280. }
  31281. static byte* was_stub_rand_cleanup_called(void)
  31282. {
  31283. static byte was_called = 0;
  31284. return &was_called;
  31285. }
  31286. static void stub_rand_cleanup(void)
  31287. {
  31288. byte* was_called = was_stub_rand_cleanup_called();
  31289. *was_called = 1;
  31290. return;
  31291. }
  31292. static byte* was_stub_rand_add_called(void)
  31293. {
  31294. static byte was_called = 0;
  31295. return &was_called;
  31296. }
  31297. static int stub_rand_add(const void *buf, int num, double entropy)
  31298. {
  31299. byte* was_called = was_stub_rand_add_called();
  31300. (void)buf;
  31301. (void)num;
  31302. (void)entropy;
  31303. *was_called = 1;
  31304. return 0;
  31305. }
  31306. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  31307. {
  31308. (void)buf;
  31309. (void)num;
  31310. return 9876;
  31311. }
  31312. static int stub_rand_status(void)
  31313. {
  31314. return 5432;
  31315. }
  31316. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  31317. static void test_wolfSSL_RAND_set_rand_method(void)
  31318. {
  31319. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  31320. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  31321. unsigned char* buf = NULL;
  31322. int num = 0;
  31323. double entropy = 0;
  31324. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  31325. byte* was_add_called = was_stub_rand_add_called();
  31326. printf(testingFmt, "wolfSSL_RAND_set_rand_method()");
  31327. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  31328. DYNAMIC_TYPE_TMP_BUFFER);
  31329. AssertIntNE(wolfSSL_RAND_status(), 5432);
  31330. AssertIntEQ(*was_cleanup_called, 0);
  31331. RAND_cleanup();
  31332. AssertIntEQ(*was_cleanup_called, 0);
  31333. rand_methods.seed = &stub_rand_seed;
  31334. rand_methods.bytes = &stub_rand_bytes;
  31335. rand_methods.cleanup = &stub_rand_cleanup;
  31336. rand_methods.add = &stub_rand_add;
  31337. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  31338. rand_methods.status = &stub_rand_status;
  31339. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  31340. AssertIntEQ(RAND_seed(buf, num), 123);
  31341. AssertIntEQ(RAND_bytes(buf, num), 456);
  31342. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  31343. AssertIntEQ(RAND_status(), 5432);
  31344. AssertIntEQ(*was_add_called, 0);
  31345. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  31346. RAND_add(buf, num, entropy);
  31347. AssertIntEQ(*was_add_called, 1);
  31348. was_add_called = 0;
  31349. AssertIntEQ(*was_cleanup_called, 0);
  31350. RAND_cleanup();
  31351. AssertIntEQ(*was_cleanup_called, 1);
  31352. *was_cleanup_called = 0;
  31353. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  31354. AssertIntNE(RAND_status(), 5432);
  31355. AssertIntEQ(*was_cleanup_called, 0);
  31356. RAND_cleanup();
  31357. AssertIntEQ(*was_cleanup_called, 0);
  31358. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31359. printf(resultFmt, passed);
  31360. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  31361. }
  31362. static void test_wolfSSL_RAND_bytes(void)
  31363. {
  31364. #if defined(OPENSSL_EXTRA)
  31365. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  31366. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  31367. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  31368. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  31369. int max_bufsize;
  31370. byte *my_buf;
  31371. printf(testingFmt, "test_wolfSSL_RAND_bytes()");
  31372. /* sanity check */
  31373. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  31374. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  31375. max_bufsize = size4;
  31376. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  31377. DYNAMIC_TYPE_TMP_BUFFER);
  31378. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  31379. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  31380. AssertNotNull(my_buf);
  31381. XMEMSET(my_buf, 0, max_bufsize);
  31382. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  31383. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  31384. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  31385. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  31386. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31387. printf(resultFmt, passed);
  31388. #endif
  31389. }
  31390. static void test_wolfSSL_BN_rand(void)
  31391. {
  31392. #if defined(OPENSSL_EXTRA)
  31393. BIGNUM* bn;
  31394. printf(testingFmt, "wolfSSL_BN_rand()");
  31395. AssertNotNull(bn = BN_new());
  31396. AssertIntNE(BN_rand(bn, 0, 0, 0), SSL_SUCCESS);
  31397. BN_free(bn);
  31398. AssertNotNull(bn = BN_new());
  31399. AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
  31400. BN_free(bn);
  31401. AssertNotNull(bn = BN_new());
  31402. AssertIntEQ(BN_rand(bn, 64, 0, 0), SSL_SUCCESS);
  31403. BN_free(bn);
  31404. printf(resultFmt, passed);
  31405. #endif
  31406. }
  31407. static void test_wolfSSL_pseudo_rand(void)
  31408. {
  31409. #if defined(OPENSSL_EXTRA)
  31410. BIGNUM* bn;
  31411. unsigned char bin[8];
  31412. int i;
  31413. printf(testingFmt, "wolfSSL_pseudo_rand()");
  31414. /* BN_pseudo_rand returns 1 on success 0 on failure
  31415. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  31416. for (i = 0; i < 10; i++) {
  31417. AssertNotNull(bn = BN_new());
  31418. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  31419. AssertIntGT(BN_bn2bin(bn, bin),0);
  31420. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  31421. BN_free(bn);
  31422. }
  31423. for (i = 0; i < 10; i++) {
  31424. AssertNotNull(bn = BN_new());
  31425. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  31426. AssertIntGT(BN_bn2bin(bn, bin),0);
  31427. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  31428. BN_free(bn);
  31429. }
  31430. printf(resultFmt, passed);
  31431. #endif
  31432. }
  31433. static void test_wolfSSL_PKCS8_Compat(void)
  31434. {
  31435. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31436. #ifndef NO_BIO
  31437. PKCS8_PRIV_KEY_INFO* pt;
  31438. BIO* bio;
  31439. XFILE f;
  31440. int bytes;
  31441. char pkcs8_buffer[512];
  31442. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  31443. EVP_PKEY *pkey = NULL;
  31444. #endif
  31445. printf(testingFmt, "wolfSSL_pkcs8()");
  31446. /* file from wolfssl/certs/ directory */
  31447. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  31448. AssertTrue(f != XBADFILE);
  31449. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  31450. XFCLOSE(f);
  31451. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  31452. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  31453. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  31454. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  31455. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  31456. /* gets PKCS8 pointer to pkey */
  31457. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  31458. EVP_PKEY_free(pkey);
  31459. #endif
  31460. BIO_free(bio);
  31461. PKCS8_PRIV_KEY_INFO_free(pt);
  31462. printf(resultFmt, passed);
  31463. #endif
  31464. #endif
  31465. }
  31466. static void test_wolfSSL_PKCS8_d2i(void)
  31467. {
  31468. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  31469. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  31470. * requires a 12 byte password in FIPS mode. This test ends up
  31471. * trying to use an 8 byte password. */
  31472. #ifndef NO_FILESYSTEM
  31473. unsigned char pkcs8_buffer[2048];
  31474. const unsigned char* p;
  31475. int bytes;
  31476. XFILE file;
  31477. WOLFSSL_EVP_PKEY* pkey = NULL;
  31478. #ifndef NO_BIO
  31479. BIO* bio;
  31480. #if defined(OPENSSL_ALL) && \
  31481. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  31482. defined(HAVE_ECC)) && \
  31483. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31484. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  31485. #endif
  31486. #endif
  31487. #ifndef NO_RSA
  31488. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  31489. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  31490. #ifndef NO_DES3
  31491. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  31492. #endif
  31493. #endif /* NO_RSA */
  31494. #ifdef HAVE_ECC
  31495. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  31496. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  31497. #ifndef NO_DES3
  31498. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  31499. #endif
  31500. #endif /* HAVE_ECC */
  31501. #endif /* !NO_FILESYSTEM */
  31502. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  31503. #ifndef NO_RSA
  31504. #ifdef USE_CERT_BUFFERS_1024
  31505. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  31506. int rsaSz = sizeof_server_key_der_1024;
  31507. #else
  31508. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  31509. int rsaSz = sizeof_server_key_der_2048;
  31510. #endif
  31511. #endif
  31512. #ifdef HAVE_ECC
  31513. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  31514. int ecSz = sizeof_ecc_key_der_256;
  31515. #endif
  31516. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  31517. #ifndef NO_FILESYSTEM
  31518. (void)pkcs8_buffer;
  31519. (void)p;
  31520. (void)bytes;
  31521. (void)file;
  31522. #ifndef NO_BIO
  31523. (void)bio;
  31524. #endif
  31525. #endif
  31526. #ifdef OPENSSL_ALL
  31527. #ifndef NO_RSA
  31528. /* Try to auto-detect normal RSA private key */
  31529. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  31530. EVP_PKEY_free(pkey);
  31531. #endif
  31532. #ifdef HAVE_ECC
  31533. /* Try to auto-detect normal EC private key */
  31534. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  31535. EVP_PKEY_free(pkey);
  31536. #endif
  31537. #endif /* OPENSSL_ALL */
  31538. #ifndef NO_FILESYSTEM
  31539. #ifndef NO_RSA
  31540. /* Get DER encoded RSA PKCS#8 data. */
  31541. file = XFOPEN(rsaDerPkcs8File, "rb");
  31542. AssertTrue(file != XBADFILE);
  31543. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  31544. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  31545. file)), 0);
  31546. XFCLOSE(file);
  31547. p = pkcs8_buffer;
  31548. #ifdef OPENSSL_ALL
  31549. /* Try to decode - auto-detect key type. */
  31550. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  31551. #else
  31552. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  31553. #endif
  31554. /* Get PEM encoded RSA PKCS#8 data. */
  31555. file = XFOPEN(rsaPemPkcs8File, "rb");
  31556. AssertTrue(file != XBADFILE);
  31557. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  31558. file)), 0);
  31559. XFCLOSE(file);
  31560. #if defined(OPENSSL_ALL) && \
  31561. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31562. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31563. /* Write PKCS#8 PEM to BIO. */
  31564. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31565. NULL), bytes);
  31566. /* Compare file and written data */
  31567. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  31568. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  31569. BIO_free(bio);
  31570. #ifndef NO_DES3
  31571. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31572. /* Write Encrypted PKCS#8 PEM to BIO. */
  31573. bytes = 1834;
  31574. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  31575. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  31576. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  31577. (void*)"yassl123"));
  31578. EVP_PKEY_free(evpPkey);
  31579. BIO_free(bio);
  31580. #endif /* !NO_DES3 */
  31581. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  31582. EVP_PKEY_free(pkey);
  31583. /* PKCS#8 encrypted RSA key */
  31584. #ifndef NO_DES3
  31585. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  31586. AssertTrue(file != XBADFILE);
  31587. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  31588. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  31589. file)), 0);
  31590. XFCLOSE(file);
  31591. #if defined(OPENSSL_ALL) && \
  31592. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31593. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  31594. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  31595. (void*)"yassl123"));
  31596. EVP_PKEY_free(pkey);
  31597. BIO_free(bio);
  31598. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  31599. #endif /* !NO_DES3 */
  31600. #endif /* NO_RSA */
  31601. #ifdef HAVE_ECC
  31602. /* PKCS#8 encode EC key */
  31603. file = XFOPEN(ecDerPkcs8File, "rb");
  31604. AssertTrue(file != XBADFILE);
  31605. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  31606. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  31607. file)), 0);
  31608. XFCLOSE(file);
  31609. p = pkcs8_buffer;
  31610. #ifdef OPENSSL_ALL
  31611. /* Try to decode - auto-detect key type. */
  31612. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  31613. #else
  31614. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  31615. #endif
  31616. /* Get PEM encoded RSA PKCS#8 data. */
  31617. file = XFOPEN(ecPemPkcs8File, "rb");
  31618. AssertTrue(file != XBADFILE);
  31619. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  31620. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  31621. file)), 0);
  31622. XFCLOSE(file);
  31623. #if defined(OPENSSL_ALL) && \
  31624. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  31625. defined(HAVE_AES_CBC)
  31626. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31627. /* Write PKCS#8 PEM to BIO. */
  31628. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31629. NULL), bytes);
  31630. /* Compare file and written data */
  31631. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  31632. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  31633. BIO_free(bio);
  31634. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31635. /* Write Encrypted PKCS#8 PEM to BIO. */
  31636. bytes = 379;
  31637. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  31638. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  31639. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  31640. (void*)"yassl123"));
  31641. EVP_PKEY_free(evpPkey);
  31642. BIO_free(bio);
  31643. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  31644. EVP_PKEY_free(pkey);
  31645. /* PKCS#8 encrypted EC key */
  31646. #ifndef NO_DES3
  31647. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  31648. AssertTrue(file != XBADFILE);
  31649. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  31650. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  31651. file)), 0);
  31652. XFCLOSE(file);
  31653. #if defined(OPENSSL_ALL) && \
  31654. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31655. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  31656. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  31657. (void*)"yassl123"));
  31658. EVP_PKEY_free(pkey);
  31659. BIO_free(bio);
  31660. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  31661. #endif /* !NO_DES3 */
  31662. #endif /* HAVE_ECC */
  31663. #endif /* !NO_FILESYSTEM */
  31664. printf(resultFmt, passed);
  31665. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  31666. }
  31667. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  31668. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  31669. #define LOGGING_THREADS 5
  31670. #define ERROR_COUNT 10
  31671. static volatile int loggingThreadsReady;
  31672. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  31673. {
  31674. const char* file;
  31675. int line;
  31676. int err;
  31677. int errorCount = 0;
  31678. int i;
  31679. (void)args;
  31680. while (!loggingThreadsReady);
  31681. for (i = 0; i < ERROR_COUNT; i++)
  31682. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  31683. while ((err = ERR_get_error_line(&file, &line))) {
  31684. AssertIntEQ(err, 990 + errorCount);
  31685. errorCount++;
  31686. }
  31687. AssertIntEQ(errorCount, ERROR_COUNT);
  31688. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  31689. errorCount = 0;
  31690. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  31691. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  31692. while ((err = ERR_get_error_line(&file, &line))) {
  31693. AssertIntEQ(err, 990 + errorCount);
  31694. errorCount++;
  31695. }
  31696. /* test that the 3 errors over the max were dropped */
  31697. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  31698. return 0;
  31699. }
  31700. #endif
  31701. static void test_error_queue_per_thread(void)
  31702. {
  31703. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  31704. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  31705. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  31706. int i;
  31707. printf(testingFmt, "error_queue_per_thread()");
  31708. ERR_clear_error(); /* clear out any error nodes */
  31709. loggingThreadsReady = 0;
  31710. for (i = 0; i < LOGGING_THREADS; i++)
  31711. start_thread(test_logging, NULL, &loggingThreads[i]);
  31712. loggingThreadsReady = 1;
  31713. for (i = 0; i < LOGGING_THREADS; i++)
  31714. join_thread(loggingThreads[i]);
  31715. printf(resultFmt, passed);
  31716. #endif
  31717. }
  31718. static void test_wolfSSL_ERR_put_error(void)
  31719. {
  31720. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  31721. defined(DEBUG_WOLFSSL)
  31722. const char* file;
  31723. int line;
  31724. printf(testingFmt, "wolfSSL_ERR_put_error()");
  31725. ERR_clear_error(); /* clear out any error nodes */
  31726. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  31727. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  31728. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  31729. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  31730. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  31731. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  31732. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  31733. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  31734. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  31735. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  31736. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  31737. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  31738. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  31739. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  31740. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  31741. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  31742. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  31743. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  31744. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  31745. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  31746. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  31747. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  31748. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  31749. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  31750. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  31751. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  31752. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  31753. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  31754. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  31755. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  31756. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  31757. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  31758. #ifdef WOLFSSL_PYTHON
  31759. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  31760. "this file", 100);
  31761. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  31762. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  31763. AssertIntEQ(line, 100);
  31764. AssertIntEQ(wolfSSL_ERR_peek_error(),
  31765. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  31766. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  31767. #endif
  31768. /* try reading past end of error queue */
  31769. file = NULL;
  31770. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  31771. AssertNull(file);
  31772. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  31773. PEMerr(4,4);
  31774. AssertIntEQ(ERR_get_error(), 4);
  31775. /* Empty and free up all error nodes */
  31776. ERR_clear_error();
  31777. /* Verify all nodes are cleared */
  31778. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  31779. ERR_clear_error();
  31780. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  31781. printf(resultFmt, passed);
  31782. #endif
  31783. }
  31784. #ifndef NO_BIO
  31785. static void test_wolfSSL_ERR_print_errors(void)
  31786. {
  31787. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  31788. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  31789. BIO* bio;
  31790. char buf[1024];
  31791. printf(testingFmt, "wolfSSL_ERR_print_errors()");
  31792. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31793. ERR_clear_error(); /* clear out any error nodes */
  31794. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  31795. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  31796. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  31797. ERR_print_errors(bio);
  31798. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  31799. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  31800. buf, 55), 0);
  31801. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  31802. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  31803. buf, 56), 0);
  31804. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  31805. AssertIntEQ(buf[0], '\0');
  31806. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  31807. BIO_free(bio);
  31808. printf(resultFmt, passed);
  31809. #endif
  31810. }
  31811. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  31812. defined(DEBUG_WOLFSSL)
  31813. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  31814. {
  31815. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  31816. return 0;
  31817. }
  31818. #endif
  31819. static void test_wolfSSL_ERR_print_errors_cb(void)
  31820. {
  31821. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  31822. defined(DEBUG_WOLFSSL)
  31823. BIO* bio;
  31824. char buf[1024];
  31825. printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
  31826. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31827. ERR_clear_error(); /* clear out any error nodes */
  31828. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  31829. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  31830. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  31831. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  31832. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  31833. buf, 53), 0);
  31834. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  31835. buf + 53, 55), 0);
  31836. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  31837. BIO_free(bio);
  31838. printf(resultFmt, passed);
  31839. #endif
  31840. }
  31841. /*
  31842. * Testing WOLFSSL_ERROR_MSG
  31843. */
  31844. static int test_WOLFSSL_ERROR_MSG (void)
  31845. {
  31846. int ret = 0;
  31847. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  31848. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  31849. const char* msg = TEST_STRING;
  31850. printf(testingFmt, "WOLFSSL_ERROR_MSG()");
  31851. WOLFSSL_ERROR_MSG(msg);
  31852. printf(resultFmt, ret == 0 ? passed : failed);
  31853. #endif
  31854. return ret;
  31855. }/*End test_WOLFSSL_ERROR_MSG*/
  31856. /*
  31857. * Testing wc_ERR_remove_state
  31858. */
  31859. static int test_wc_ERR_remove_state (void)
  31860. {
  31861. int ret = 0;
  31862. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  31863. printf(testingFmt, "wc_ERR_remove_state()");
  31864. wc_ERR_remove_state();
  31865. printf(resultFmt, ret == 0 ? passed : failed);
  31866. #endif
  31867. return ret;
  31868. }/*End test_wc_ERR_remove_state*/
  31869. /*
  31870. * Testing wc_ERR_print_errors_fp
  31871. */
  31872. static int test_wc_ERR_print_errors_fp (void)
  31873. {
  31874. int ret = 0;
  31875. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  31876. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  31877. long sz;
  31878. printf(testingFmt, "wc_ERR_print_errors_fp()");
  31879. WOLFSSL_ERROR(BAD_FUNC_ARG);
  31880. XFILE fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  31881. wc_ERR_print_errors_fp(fp);
  31882. #if defined(DEBUG_WOLFSSL)
  31883. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  31884. sz = XFTELL(fp);
  31885. #ifdef NO_ERROR_QUEUE
  31886. /* File should be empty when NO_ERROR_QUEUE is defined */
  31887. if (sz != 0) {
  31888. ret = BAD_FUNC_ARG;
  31889. }
  31890. #else
  31891. if (sz == 0) {
  31892. ret = BAD_FUNC_ARG;
  31893. }
  31894. #endif
  31895. #endif
  31896. printf(resultFmt, ret == 0 ? passed : failed);
  31897. XFCLOSE(fp);
  31898. (void)sz;
  31899. #endif
  31900. return ret;
  31901. }/*End test_wc_ERR_print_errors_fp*/
  31902. #ifdef DEBUG_WOLFSSL
  31903. static void Logging_cb(const int logLevel, const char *const logMessage)
  31904. {
  31905. (void)logLevel;
  31906. (void)logMessage;
  31907. }
  31908. #endif
  31909. /*
  31910. * Testing wolfSSL_GetLoggingCb
  31911. */
  31912. static int test_wolfSSL_GetLoggingCb (void)
  31913. {
  31914. int ret = 0;
  31915. printf(testingFmt, "wolfSSL_GetLoggingCb()");
  31916. #ifdef DEBUG_WOLFSSL
  31917. /* Testing without wolfSSL_SetLoggingCb() */
  31918. if (ret == 0) {
  31919. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  31920. ret = 0;
  31921. }
  31922. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  31923. ret = -1;
  31924. }
  31925. }
  31926. /* Testing with wolfSSL_SetLoggingCb() */
  31927. if (ret == 0) {
  31928. ret = wolfSSL_SetLoggingCb(Logging_cb);
  31929. if (ret == 0){
  31930. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  31931. ret = -1;
  31932. }
  31933. if (ret == 0) {
  31934. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  31935. ret = 0;
  31936. }
  31937. }
  31938. /* reset logging callback */
  31939. wolfSSL_SetLoggingCb(NULL);
  31940. }
  31941. }
  31942. #endif
  31943. if (ret == 0) {
  31944. if (wolfSSL_GetLoggingCb() != NULL) {
  31945. ret = -1;
  31946. }
  31947. }
  31948. printf(resultFmt, ret == 0 ? passed : failed);
  31949. return ret;
  31950. }/*End test_wolfSSL_GetLoggingCb*/
  31951. #endif /* !NO_BIO */
  31952. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  31953. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  31954. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  31955. static void test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  31956. {
  31957. static const unsigned char key[] = "simple test key";
  31958. HMAC_CTX* hmac;
  31959. ENGINE* e = NULL;
  31960. unsigned char hash[WC_MAX_DIGEST_SIZE];
  31961. unsigned int len;
  31962. AssertNotNull(hmac = HMAC_CTX_new());
  31963. HMAC_CTX_init(hmac);
  31964. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  31965. SSL_SUCCESS);
  31966. /* re-using test key as data to hash */
  31967. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  31968. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  31969. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  31970. AssertIntEQ(len, md_len);
  31971. AssertIntEQ(HMAC_size(hmac), md_len);
  31972. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  31973. HMAC_cleanup(hmac);
  31974. HMAC_CTX_free(hmac);
  31975. len = 0;
  31976. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  31977. AssertIntEQ(len, md_len);
  31978. }
  31979. #endif
  31980. static void test_wolfSSL_HMAC(void)
  31981. {
  31982. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  31983. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  31984. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  31985. printf(testingFmt, "wolfSSL_HMAC()");
  31986. #ifndef NO_SHA256
  31987. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  31988. #endif
  31989. #ifdef WOLFSSL_SHA224
  31990. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  31991. #endif
  31992. #ifdef WOLFSSL_SHA384
  31993. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  31994. #endif
  31995. #ifdef WOLFSSL_SHA512
  31996. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  31997. #endif
  31998. #ifdef WOLFSSL_SHA3
  31999. #ifndef WOLFSSL_NOSHA3_224
  32000. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  32001. #endif
  32002. #ifndef WOLFSSL_NOSHA3_256
  32003. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  32004. #endif
  32005. #ifndef WOLFSSL_NOSHA3_384
  32006. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  32007. #endif
  32008. #ifndef WOLFSSL_NOSHA3_512
  32009. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  32010. #endif
  32011. #endif
  32012. printf(resultFmt, passed);
  32013. #endif
  32014. }
  32015. static void test_wolfSSL_CMAC(void)
  32016. {
  32017. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  32018. defined(WOLFSSL_AES_DIRECT)
  32019. int i;
  32020. byte key[AES_128_KEY_SIZE];
  32021. CMAC_CTX* cmacCtx = NULL;
  32022. byte out[AES_BLOCK_SIZE];
  32023. size_t outLen = AES_BLOCK_SIZE;
  32024. printf(testingFmt, "test_wolfSSL_CMAC()");
  32025. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  32026. key[i] = i;
  32027. }
  32028. AssertNotNull(cmacCtx = CMAC_CTX_new());
  32029. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  32030. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  32031. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  32032. NULL), SSL_SUCCESS);
  32033. /* re-using test key as data to hash */
  32034. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  32035. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  32036. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  32037. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  32038. CMAC_CTX_free(cmacCtx);
  32039. printf(resultFmt, passed);
  32040. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  32041. }
  32042. static void test_wolfSSL_OBJ(void)
  32043. {
  32044. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  32045. * mode
  32046. */
  32047. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  32048. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  32049. defined(WOLFSSL_CERT_GEN)
  32050. ASN1_OBJECT *obj = NULL;
  32051. char buf[50];
  32052. XFILE fp;
  32053. X509 *x509 = NULL;
  32054. X509_NAME *x509Name;
  32055. X509_NAME_ENTRY *x509NameEntry;
  32056. ASN1_OBJECT *asn1Name = NULL;
  32057. int numNames;
  32058. BIO *bio = NULL;
  32059. int nid;
  32060. int i, j;
  32061. const char *f[] = {
  32062. #ifndef NO_RSA
  32063. "./certs/ca-cert.der",
  32064. #endif
  32065. #ifdef HAVE_ECC
  32066. "./certs/ca-ecc-cert.der",
  32067. "./certs/ca-ecc384-cert.der",
  32068. #endif
  32069. NULL};
  32070. ASN1_OBJECT *field_name_obj = NULL;
  32071. int lastpos = -1;
  32072. int tmp = -1;
  32073. ASN1_STRING *asn1 = NULL;
  32074. unsigned char *buf_dyn = NULL;
  32075. printf(testingFmt, "wolfSSL_OBJ()");
  32076. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  32077. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  32078. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  32079. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  32080. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  32081. ASN1_OBJECT_free(obj);
  32082. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  32083. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  32084. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  32085. #ifdef WOLFSSL_CERT_EXT
  32086. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  32087. #endif
  32088. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  32089. ASN1_OBJECT_free(obj);
  32090. for (i = 0; f[i] != NULL; i++)
  32091. {
  32092. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  32093. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  32094. XFCLOSE(fp);
  32095. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  32096. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  32097. /* Get the Common Name by using OBJ_txt2obj */
  32098. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  32099. do
  32100. {
  32101. lastpos = tmp;
  32102. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  32103. } while (tmp > -1);
  32104. AssertIntNE(lastpos, -1);
  32105. ASN1_OBJECT_free(field_name_obj);
  32106. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  32107. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  32108. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  32109. /*
  32110. * All Common Names should be www.wolfssl.com
  32111. * This makes testing easier as we can test for the expected value.
  32112. */
  32113. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  32114. OPENSSL_free(buf_dyn);
  32115. bio = BIO_new(BIO_s_mem());
  32116. AssertTrue(bio != NULL);
  32117. for (j = 0; j < numNames; j++)
  32118. {
  32119. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  32120. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  32121. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  32122. }
  32123. BIO_free(bio);
  32124. X509_free(x509);
  32125. }
  32126. #ifdef HAVE_PKCS12
  32127. {
  32128. PKCS12 *p12;
  32129. int boolRet;
  32130. EVP_PKEY *pkey = NULL;
  32131. const char *p12_f[] = {
  32132. #if !defined(NO_DES3) && !defined(NO_RSA)
  32133. "./certs/test-servercert.p12",
  32134. #endif
  32135. NULL};
  32136. for (i = 0; p12_f[i] != NULL; i++)
  32137. {
  32138. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  32139. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  32140. XFCLOSE(fp);
  32141. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  32142. &pkey, &x509, NULL)) > 0);
  32143. wc_PKCS12_free(p12);
  32144. EVP_PKEY_free(pkey);
  32145. x509Name = X509_get_issuer_name(x509);
  32146. AssertNotNull(x509Name);
  32147. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  32148. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  32149. for (j = 0; j < numNames; j++)
  32150. {
  32151. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  32152. AssertNotNull(asn1Name =
  32153. X509_NAME_ENTRY_get_object(x509NameEntry));
  32154. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  32155. }
  32156. BIO_free(bio);
  32157. X509_free(x509);
  32158. }
  32159. }
  32160. #endif /* HAVE_PKCS12 */
  32161. printf(resultFmt, passed);
  32162. #endif
  32163. }
  32164. static void test_wolfSSL_i2a_ASN1_OBJECT(void)
  32165. {
  32166. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  32167. ASN1_OBJECT *obj = NULL;
  32168. BIO *bio = NULL;
  32169. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  32170. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  32171. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  32172. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  32173. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  32174. BIO_free(bio);
  32175. ASN1_OBJECT_free(obj);
  32176. #endif
  32177. }
  32178. static void test_wolfSSL_OBJ_cmp(void)
  32179. {
  32180. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  32181. ASN1_OBJECT *obj = NULL;
  32182. ASN1_OBJECT *obj2 = NULL;
  32183. printf(testingFmt, "wolfSSL_OBJ_cmp()");
  32184. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  32185. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  32186. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  32187. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  32188. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  32189. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  32190. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  32191. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  32192. ASN1_OBJECT_free(obj);
  32193. ASN1_OBJECT_free(obj2);
  32194. printf(resultFmt, passed);
  32195. #endif
  32196. }
  32197. static void test_wolfSSL_OBJ_txt2nid(void)
  32198. {
  32199. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  32200. int i;
  32201. static const struct {
  32202. const char* sn;
  32203. const char* ln;
  32204. const char* oid;
  32205. int nid;
  32206. } testVals[] = {
  32207. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  32208. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  32209. NID_id_on_dnsSRV },
  32210. { "msUPN", "Microsoft User Principal Name",
  32211. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  32212. { NULL, NULL, NULL, NID_undef }
  32213. };
  32214. printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
  32215. /* Invalid cases */
  32216. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  32217. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  32218. /* Valid cases */
  32219. for (i = 0; testVals[i].sn != NULL; i++) {
  32220. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  32221. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  32222. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  32223. }
  32224. printf(resultFmt, passed);
  32225. #endif
  32226. }
  32227. static void test_wolfSSL_OBJ_txt2obj(void)
  32228. {
  32229. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  32230. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  32231. int i;
  32232. char buf[50];
  32233. ASN1_OBJECT* obj;
  32234. static const struct {
  32235. const char* oidStr;
  32236. const char* sn;
  32237. const char* ln;
  32238. } objs_list[] = {
  32239. #if defined(WOLFSSL_APACHE_HTTPD)
  32240. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  32241. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  32242. #endif
  32243. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  32244. { NULL, NULL, NULL }
  32245. };
  32246. printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
  32247. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  32248. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  32249. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  32250. /* Test numerical value of oid (oidStr) */
  32251. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  32252. /* Convert object back to text to confirm oid is correct */
  32253. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  32254. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  32255. ASN1_OBJECT_free(obj);
  32256. XMEMSET(buf, 0, sizeof(buf));
  32257. /* Test short name (sn) */
  32258. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  32259. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  32260. /* Convert object back to text to confirm oid is correct */
  32261. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  32262. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  32263. ASN1_OBJECT_free(obj);
  32264. XMEMSET(buf, 0, sizeof(buf));
  32265. /* Test long name (ln) - should fail when no_name = 1 */
  32266. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  32267. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  32268. /* Convert object back to text to confirm oid is correct */
  32269. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  32270. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  32271. ASN1_OBJECT_free(obj);
  32272. XMEMSET(buf, 0, sizeof(buf));
  32273. }
  32274. printf(resultFmt, passed);
  32275. #endif
  32276. }
  32277. static void test_wolfSSL_i2t_ASN1_OBJECT(void)
  32278. {
  32279. #if defined(OPENSSL_EXTRA) && \
  32280. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  32281. char buf[50] = {0};
  32282. ASN1_OBJECT* obj;
  32283. const char* oid = "2.5.29.19";
  32284. const char* ln = "X509v3 Basic Constraints";
  32285. printf(testingFmt, "test_wolfSSL_i2t_ASN1_OBJECT()");
  32286. obj = NULL;
  32287. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
  32288. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
  32289. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
  32290. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  32291. XMEMSET(buf, 0, sizeof(buf));
  32292. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  32293. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  32294. ASN1_OBJECT_free(obj);
  32295. printf(resultFmt, passed);
  32296. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  32297. }
  32298. static void test_wolfSSL_PEM_write_bio_X509(void)
  32299. {
  32300. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_AKID_NAME) && \
  32301. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  32302. /* This test contains the hard coded expected
  32303. * lengths. Update if necessary */
  32304. BIO* input;
  32305. BIO* output;
  32306. X509* x509 = NULL;
  32307. int expectedLen;
  32308. printf(testingFmt, "wolfSSL_PEM_write_bio_X509()");
  32309. AssertNotNull(input = BIO_new_file(
  32310. "certs/test/cert-ext-multiple.pem", "rb"));
  32311. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  32312. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  32313. AssertNotNull(PEM_read_bio_X509(input, &x509, NULL, NULL));
  32314. AssertIntEQ(PEM_write_bio_X509(output, x509), WOLFSSL_SUCCESS);
  32315. #ifdef WOLFSSL_ALT_NAMES
  32316. /* Here we copy the validity struct from the original */
  32317. expectedLen = 2000;
  32318. #else
  32319. /* Only difference is that we generate the validity in generalized
  32320. * time. Generating UTCTime vs Generalized time should be fixed in
  32321. * the future */
  32322. expectedLen = 2004;
  32323. #endif
  32324. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  32325. /* Reset output buffer */
  32326. BIO_free(output);
  32327. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  32328. /* Test forcing the AKID to be generated just from KeyIdentifier */
  32329. if (x509->authKeyIdSrc != NULL) {
  32330. XMEMMOVE(x509->authKeyIdSrc, x509->authKeyId, x509->authKeyIdSz);
  32331. x509->authKeyId = x509->authKeyIdSrc;
  32332. x509->authKeyIdSrc = NULL;
  32333. x509->authKeyIdSrcSz = 0;
  32334. }
  32335. AssertIntEQ(PEM_write_bio_X509(output, x509), WOLFSSL_SUCCESS);
  32336. /* Check that we generate a smaller output since the AKID will
  32337. * only contain the KeyIdentifier without any additional
  32338. * information */
  32339. #ifdef WOLFSSL_ALT_NAMES
  32340. /* Here we copy the validity struct from the original */
  32341. expectedLen = 1688;
  32342. #else
  32343. /* UTCTime vs Generalized time */
  32344. expectedLen = 1692;
  32345. #endif
  32346. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  32347. X509_free(x509);
  32348. BIO_free(input);
  32349. BIO_free(output);
  32350. printf(resultFmt, passed);
  32351. #endif
  32352. }
  32353. static void test_wolfSSL_X509_NAME_ENTRY(void)
  32354. {
  32355. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32356. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  32357. X509* x509;
  32358. #ifndef NO_BIO
  32359. BIO* bio;
  32360. #endif
  32361. X509_NAME* nm;
  32362. X509_NAME_ENTRY* entry;
  32363. unsigned char cn[] = "another name to add";
  32364. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  32365. AssertNotNull(x509 =
  32366. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  32367. #ifndef NO_BIO
  32368. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32369. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  32370. #endif
  32371. #ifdef WOLFSSL_CERT_REQ
  32372. {
  32373. X509_REQ* req;
  32374. #ifndef NO_BIO
  32375. BIO* bReq;
  32376. #endif
  32377. AssertNotNull(req =
  32378. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  32379. #ifndef NO_BIO
  32380. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  32381. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  32382. BIO_free(bReq);
  32383. #endif
  32384. X509_free(req);
  32385. }
  32386. #endif
  32387. AssertNotNull(nm = X509_get_subject_name(x509));
  32388. /* Test add entry */
  32389. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  32390. 0x0c, cn, (int)sizeof(cn)));
  32391. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  32392. #ifdef WOLFSSL_CERT_EXT
  32393. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  32394. (byte*)"support@wolfssl.com", 19, -1,
  32395. 1), WOLFSSL_SUCCESS);
  32396. #endif
  32397. X509_NAME_ENTRY_free(entry);
  32398. #ifdef WOLFSSL_CERT_REQ
  32399. {
  32400. unsigned char srv_pkcs9p[] = "Server";
  32401. char* subject;
  32402. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  32403. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  32404. subject = X509_NAME_oneline(nm, 0, 0);
  32405. #ifdef DEBUG_WOLFSSL
  32406. printf("\n\t%s\n", subject);
  32407. #endif
  32408. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  32409. }
  32410. #endif
  32411. /* Test add entry by text */
  32412. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  32413. 0x0c, cn, (int)sizeof(cn)));
  32414. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  32415. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  32416. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  32417. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  32418. #endif
  32419. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  32420. X509_NAME_ENTRY_free(entry);
  32421. /* Test add entry by NID */
  32422. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  32423. cn, -1, -1, 0), SSL_SUCCESS);
  32424. #ifndef NO_BIO
  32425. BIO_free(bio);
  32426. #endif
  32427. X509_free(x509); /* free's nm */
  32428. printf(resultFmt, passed);
  32429. #endif
  32430. }
  32431. static void test_wolfSSL_X509_set_name(void)
  32432. {
  32433. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32434. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  32435. X509* x509;
  32436. X509_NAME* name;
  32437. printf(testingFmt, "wolfSSL_X509_set_name()");
  32438. AssertNotNull(name = X509_NAME_new());
  32439. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  32440. (byte*)"wolfssl.com", 11, 0, 1),
  32441. WOLFSSL_SUCCESS);
  32442. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  32443. (byte*)"support@wolfssl.com", 19, -1,
  32444. 1), WOLFSSL_SUCCESS);
  32445. AssertNotNull(x509 = X509_new());
  32446. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  32447. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  32448. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  32449. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  32450. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  32451. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  32452. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  32453. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  32454. X509_free(x509);
  32455. X509_NAME_free(name);
  32456. printf(resultFmt, passed);
  32457. #endif /* OPENSSL_ALL && !NO_CERTS */
  32458. }
  32459. static void test_wolfSSL_X509_set_notAfter(void)
  32460. {
  32461. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  32462. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  32463. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  32464. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  32465. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  32466. /* Generalized time will overflow time_t if not long */
  32467. X509* x;
  32468. BIO* bio;
  32469. ASN1_TIME *asn_time, *time_check;
  32470. const int year = 365*24*60*60;
  32471. const int day = 24*60*60;
  32472. const int hour = 60*60;
  32473. const int mini = 60;
  32474. int offset_day;
  32475. unsigned char buf[25];
  32476. time_t t;
  32477. printf(testingFmt, "wolfSSL_X509_set_notAfter()");
  32478. /*
  32479. * Setup asn_time. APACHE HTTPD uses time(NULL)
  32480. */
  32481. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  32482. offset_day = 7;
  32483. /*
  32484. * Free these.
  32485. */
  32486. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  32487. AssertNotNull(asn_time);
  32488. AssertNotNull(x = X509_new());
  32489. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32490. /*
  32491. * Tests
  32492. */
  32493. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  32494. /* time_check is simply (ANS1_TIME*)x->notAfter */
  32495. AssertNotNull(time_check = X509_get_notAfter(x));
  32496. /* ANS1_TIME_check validates by checking if argument can be parsed */
  32497. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  32498. /* Convert to human readable format and compare to intended date */
  32499. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  32500. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  32501. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  32502. /*
  32503. * Cleanup
  32504. */
  32505. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  32506. X509_free(x);
  32507. BIO_free(bio);
  32508. printf(resultFmt, passed);
  32509. #endif
  32510. }
  32511. static void test_wolfSSL_X509_set_notBefore(void)
  32512. {
  32513. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  32514. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  32515. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  32516. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  32517. X509* x;
  32518. BIO* bio;
  32519. ASN1_TIME *asn_time, *time_check;
  32520. const int year = 365*24*60*60;
  32521. const int day = 24*60*60;
  32522. const int hour = 60*60;
  32523. const int mini = 60;
  32524. int offset_day;
  32525. unsigned char buf[25];
  32526. time_t t;
  32527. printf(testingFmt, "wolfSSL_X509_set_notBefore()");
  32528. /*
  32529. * Setup asn_time. APACHE HTTPD uses time(NULL)
  32530. */
  32531. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  32532. offset_day = 7;
  32533. /*
  32534. * Free these.
  32535. */
  32536. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  32537. AssertNotNull(asn_time);
  32538. AssertNotNull(x = X509_new());
  32539. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32540. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  32541. /*
  32542. * Main Tests
  32543. */
  32544. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  32545. /* time_check == (ANS1_TIME*)x->notBefore */
  32546. AssertNotNull(time_check = X509_get_notBefore(x));
  32547. /* ANS1_TIME_check validates by checking if argument can be parsed */
  32548. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  32549. /* Convert to human readable format and compare to intended date */
  32550. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  32551. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  32552. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  32553. /*
  32554. * Cleanup
  32555. */
  32556. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  32557. X509_free(x);
  32558. BIO_free(bio);
  32559. printf(resultFmt, passed);
  32560. #endif
  32561. }
  32562. static void test_wolfSSL_X509_set_version(void)
  32563. {
  32564. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32565. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  32566. X509* x509;
  32567. long v = 2L;
  32568. long maxInt = INT_MAX;
  32569. AssertNotNull(x509 = X509_new());
  32570. /* These should pass. */
  32571. AssertTrue(wolfSSL_X509_set_version(x509, v));
  32572. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  32573. /* Fail Case: When v(long) is greater than x509->version(int). */
  32574. v = maxInt+1;
  32575. AssertFalse(wolfSSL_X509_set_version(x509, v));
  32576. /* Cleanup */
  32577. X509_free(x509);
  32578. printf(resultFmt, passed);
  32579. #endif
  32580. }
  32581. #ifndef NO_BIO
  32582. static void test_wolfSSL_BIO_gets(void)
  32583. {
  32584. #if defined(OPENSSL_EXTRA)
  32585. BIO* bio;
  32586. BIO* bio2;
  32587. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  32588. char emp[] = "";
  32589. char bio_buffer[20];
  32590. int bufferSz = 20;
  32591. printf(testingFmt, "wolfSSL_BIO_gets()");
  32592. /* try with bad args */
  32593. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  32594. /* try with real msg */
  32595. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  32596. XMEMSET(bio_buffer, 0, bufferSz);
  32597. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  32598. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  32599. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  32600. AssertFalse(bio2 != BIO_next(bio));
  32601. /* make buffer filled with no terminating characters */
  32602. XMEMSET(bio_buffer, 1, bufferSz);
  32603. /* BIO_gets reads a line of data */
  32604. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  32605. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  32606. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  32607. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  32608. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  32609. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  32610. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  32611. /* check not null terminated string */
  32612. BIO_free(bio);
  32613. msg[0] = 0x33;
  32614. msg[1] = 0x33;
  32615. msg[2] = 0x33;
  32616. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  32617. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  32618. AssertIntEQ(bio_buffer[0], msg[0]);
  32619. AssertIntEQ(bio_buffer[1], msg[1]);
  32620. AssertIntNE(bio_buffer[2], msg[2]);
  32621. BIO_free(bio);
  32622. msg[3] = 0x33;
  32623. bio_buffer[3] = 0x33;
  32624. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  32625. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  32626. AssertIntEQ(bio_buffer[0], msg[0]);
  32627. AssertIntEQ(bio_buffer[1], msg[1]);
  32628. AssertIntEQ(bio_buffer[2], msg[2]);
  32629. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  32630. /* check reading an empty string */
  32631. BIO_free(bio);
  32632. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  32633. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  32634. AssertStrEQ(emp, bio_buffer);
  32635. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  32636. /* check error cases */
  32637. BIO_free(bio);
  32638. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  32639. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32640. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  32641. #if !defined(NO_FILESYSTEM)
  32642. {
  32643. BIO* f_bio;
  32644. XFILE f;
  32645. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  32646. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  32647. f = XFOPEN(svrCertFile, "rb");
  32648. AssertTrue((f != XBADFILE));
  32649. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  32650. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  32651. BIO_free(f_bio);
  32652. }
  32653. #endif /* NO_FILESYSTEM */
  32654. BIO_free(bio);
  32655. BIO_free(bio2);
  32656. /* try with type BIO */
  32657. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  32658. sizeof(msg));
  32659. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  32660. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  32661. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  32662. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  32663. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  32664. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  32665. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  32666. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  32667. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  32668. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  32669. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  32670. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  32671. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  32672. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  32673. BIO_free(bio);
  32674. BIO_free(bio2);
  32675. /* check reading an empty string */
  32676. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  32677. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  32678. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  32679. AssertStrEQ(emp, bio_buffer);
  32680. BIO_free(bio);
  32681. printf(resultFmt, passed);
  32682. #endif
  32683. }
  32684. static void test_wolfSSL_BIO_puts(void)
  32685. {
  32686. #if defined(OPENSSL_EXTRA)
  32687. BIO* bio;
  32688. char input[] = "hello\0world\n.....ok\n\0";
  32689. char output[128];
  32690. printf(testingFmt, "wolfSSL_BIO_puts()");
  32691. XMEMSET(output, 0, sizeof(output));
  32692. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32693. AssertIntEQ(BIO_puts(bio, input), 5);
  32694. AssertIntEQ(BIO_pending(bio), 5);
  32695. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  32696. AssertIntEQ(BIO_pending(bio), 19);
  32697. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  32698. AssertStrEQ(output, "helloworld\n");
  32699. AssertIntEQ(BIO_pending(bio), 8);
  32700. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  32701. AssertStrEQ(output, ".....ok\n");
  32702. AssertIntEQ(BIO_pending(bio), 0);
  32703. AssertIntEQ(BIO_puts(bio, ""), -1);
  32704. BIO_free(bio);
  32705. printf(resultFmt, passed);
  32706. #endif
  32707. }
  32708. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  32709. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  32710. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  32711. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  32712. {
  32713. (void)ssl;
  32714. (void)buf;
  32715. (void)sz;
  32716. (void)ctx;
  32717. return WOLFSSL_CBIO_ERR_WANT_READ;
  32718. }
  32719. #endif
  32720. static void test_wolfSSL_BIO_should_retry(void)
  32721. {
  32722. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  32723. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  32724. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  32725. tcp_ready ready;
  32726. func_args server_args;
  32727. THREAD_TYPE serverThread;
  32728. SOCKET_T sockfd = 0;
  32729. WOLFSSL_CTX* ctx;
  32730. WOLFSSL* ssl;
  32731. char msg[64] = "hello wolfssl!";
  32732. char reply[1024];
  32733. int msgSz = (int)XSTRLEN(msg);
  32734. int ret;
  32735. BIO* bio;
  32736. printf(testingFmt, "wolfSSL_BIO_should_retry()");
  32737. XMEMSET(&server_args, 0, sizeof(func_args));
  32738. #ifdef WOLFSSL_TIRTOS
  32739. fdOpenSession(Task_self());
  32740. #endif
  32741. StartTCP();
  32742. InitTcpReady(&ready);
  32743. #if defined(USE_WINDOWS_API)
  32744. /* use RNG to get random port if using windows */
  32745. ready.port = GetRandomPort();
  32746. #endif
  32747. server_args.signal = &ready;
  32748. start_thread(test_server_nofail, &server_args, &serverThread);
  32749. wait_tcp_ready(&server_args);
  32750. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32751. AssertIntEQ(WOLFSSL_SUCCESS,
  32752. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  32753. AssertIntEQ(WOLFSSL_SUCCESS,
  32754. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  32755. AssertIntEQ(WOLFSSL_SUCCESS,
  32756. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  32757. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  32758. /* force retry */
  32759. ssl = wolfSSL_new(ctx);
  32760. AssertNotNull(ssl);
  32761. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  32762. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  32763. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  32764. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  32765. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  32766. AssertIntNE(BIO_should_retry(bio), 0);
  32767. /* now perform successful connection */
  32768. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  32769. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  32770. BIO_read(bio, reply, sizeof(reply));
  32771. ret = wolfSSL_get_error(ssl, -1);
  32772. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  32773. AssertIntNE(BIO_should_retry(bio), 0);
  32774. }
  32775. else {
  32776. AssertIntEQ(BIO_should_retry(bio), 0);
  32777. }
  32778. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  32779. XSTRLEN("I hear you fa shizzle!")), 0);
  32780. BIO_free(bio);
  32781. wolfSSL_CTX_free(ctx);
  32782. join_thread(serverThread);
  32783. FreeTcpReady(&ready);
  32784. #ifdef WOLFSSL_TIRTOS
  32785. fdOpenSession(Task_self());
  32786. #endif
  32787. printf(resultFmt, passed);
  32788. #endif
  32789. }
  32790. static void test_wolfSSL_BIO_connect(void)
  32791. {
  32792. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  32793. tcp_ready ready;
  32794. func_args server_args;
  32795. THREAD_TYPE serverThread;
  32796. BIO *tcpBio;
  32797. BIO *sslBio;
  32798. SSL_CTX* ctx;
  32799. SSL *ssl;
  32800. SSL *sslPtr;
  32801. char msg[] = "hello wolfssl!";
  32802. char reply[30];
  32803. char buff[10] = {0};
  32804. printf(testingFmt, "wolfSSL_BIO_new_connect()");
  32805. /* Setup server */
  32806. XMEMSET(&server_args, 0, sizeof(func_args));
  32807. StartTCP();
  32808. InitTcpReady(&ready);
  32809. #if defined(USE_WINDOWS_API)
  32810. /* use RNG to get random port if using windows */
  32811. ready.port = GetRandomPort();
  32812. #endif
  32813. server_args.signal = &ready;
  32814. start_thread(test_server_nofail, &server_args, &serverThread);
  32815. wait_tcp_ready(&server_args);
  32816. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  32817. /* Start the test proper */
  32818. /* Setup the TCP BIO */
  32819. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  32820. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  32821. /* Setup the SSL object */
  32822. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32823. AssertIntEQ(WOLFSSL_SUCCESS,
  32824. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  32825. AssertIntEQ(WOLFSSL_SUCCESS,
  32826. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  32827. AssertIntEQ(WOLFSSL_SUCCESS,
  32828. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  32829. AssertNotNull(ssl = SSL_new(ctx));
  32830. SSL_set_connect_state(ssl);
  32831. /* Setup the SSL BIO */
  32832. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  32833. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  32834. /* Verify that BIO_get_ssl works. */
  32835. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  32836. AssertPtrEq(ssl, sslPtr);
  32837. /* Link BIO's so that sslBio uses tcpBio for IO */
  32838. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  32839. /* Do TCP connect */
  32840. AssertIntEQ(BIO_do_connect(sslBio), 1);
  32841. /* Do TLS handshake */
  32842. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  32843. /* Test writing */
  32844. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  32845. /* Expect length of default wolfSSL reply */
  32846. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  32847. /* Clean it all up */
  32848. BIO_free_all(sslBio);
  32849. /* Server clean up */
  32850. join_thread(serverThread);
  32851. FreeTcpReady(&ready);
  32852. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  32853. * after. */
  32854. XMEMSET(&server_args, 0, sizeof(func_args));
  32855. StartTCP();
  32856. InitTcpReady(&ready);
  32857. #if defined(USE_WINDOWS_API)
  32858. /* use RNG to get random port if using windows */
  32859. ready.port = GetRandomPort();
  32860. #endif
  32861. server_args.signal = &ready;
  32862. start_thread(test_server_nofail, &server_args, &serverThread);
  32863. wait_tcp_ready(&server_args);
  32864. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  32865. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  32866. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  32867. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  32868. AssertIntEQ(BIO_do_connect(sslBio), 1);
  32869. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  32870. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  32871. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  32872. /* Attempt to close the TLS connection gracefully. */
  32873. BIO_ssl_shutdown(sslBio);
  32874. BIO_free_all(sslBio);
  32875. join_thread(serverThread);
  32876. FreeTcpReady(&ready);
  32877. SSL_CTX_free(ctx);
  32878. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  32879. wc_ecc_fp_free(); /* free per thread cache */
  32880. #endif
  32881. printf(resultFmt, passed);
  32882. #endif
  32883. }
  32884. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  32885. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  32886. {
  32887. BIO* clientBio;
  32888. SSL* sslClient;
  32889. SSL_CTX* ctx;
  32890. char connectAddr[20]; /* IP + port */;
  32891. (void)args;
  32892. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  32893. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  32894. AssertIntEQ(BIO_do_connect(clientBio), 1);
  32895. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  32896. AssertNotNull(sslClient = SSL_new(ctx));
  32897. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  32898. SSL_set_bio(sslClient, clientBio, clientBio);
  32899. AssertIntEQ(SSL_connect(sslClient), 1);
  32900. SSL_free(sslClient);
  32901. SSL_CTX_free(ctx);
  32902. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  32903. wc_ecc_fp_free(); /* free per thread cache */
  32904. #endif
  32905. return 0;
  32906. }
  32907. #endif
  32908. static void test_wolfSSL_BIO_accept(void)
  32909. {
  32910. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  32911. BIO* serverBindBio;
  32912. BIO* serverAcceptBio;
  32913. SSL* sslServer;
  32914. SSL_CTX* ctx;
  32915. func_args args;
  32916. THREAD_TYPE thread;
  32917. char port[10]; /* 10 bytes should be enough to store the string
  32918. * representation of the port */
  32919. printf(testingFmt, "wolfSSL_BIO_new_accept()");
  32920. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  32921. AssertNotNull(serverBindBio = BIO_new_accept(port));
  32922. /* First BIO_do_accept binds the port */
  32923. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  32924. XMEMSET(&args, 0, sizeof(func_args));
  32925. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  32926. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  32927. /* Let's plug it into SSL to test */
  32928. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  32929. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  32930. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  32931. AssertNotNull(sslServer = SSL_new(ctx));
  32932. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  32933. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  32934. AssertIntEQ(SSL_accept(sslServer), 1);
  32935. join_thread(thread);
  32936. BIO_free(serverBindBio);
  32937. SSL_free(sslServer);
  32938. SSL_CTX_free(ctx);
  32939. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  32940. wc_ecc_fp_free(); /* free per thread cache */
  32941. #endif
  32942. printf(resultFmt, passed);
  32943. #endif
  32944. }
  32945. static void test_wolfSSL_BIO_write(void)
  32946. {
  32947. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  32948. BIO* bio;
  32949. BIO* bio64;
  32950. BIO* ptr;
  32951. int sz;
  32952. char msg[] = "conversion test";
  32953. char out[40];
  32954. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  32955. void* bufPtr = NULL;
  32956. BUF_MEM* buf = NULL;
  32957. printf(testingFmt, "wolfSSL_BIO_write()");
  32958. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  32959. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  32960. /* now should convert to base64 then write to memory */
  32961. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  32962. BIO_flush(bio);
  32963. /* test BIO chain */
  32964. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  32965. AssertNotNull(buf);
  32966. AssertIntEQ(buf->length, 25);
  32967. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  32968. AssertPtrEq(buf->data, bufPtr);
  32969. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  32970. sz = sizeof(out);
  32971. XMEMSET(out, 0, sz);
  32972. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  32973. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  32974. /* write then read should return the same message */
  32975. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  32976. sz = sizeof(out);
  32977. XMEMSET(out, 0, sz);
  32978. AssertIntEQ(BIO_read(bio, out, sz), 16);
  32979. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  32980. /* now try encoding with no line ending */
  32981. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  32982. #ifdef HAVE_EX_DATA
  32983. BIO_set_ex_data(bio64, 0, (void*) "data");
  32984. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  32985. #endif
  32986. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  32987. BIO_flush(bio);
  32988. sz = sizeof(out);
  32989. XMEMSET(out, 0, sz);
  32990. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  32991. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  32992. BIO_free_all(bio); /* frees bio64 also */
  32993. /* test with more than one bio64 in list */
  32994. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  32995. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  32996. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  32997. /* now should convert to base64 when stored and then decode with read */
  32998. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  32999. BIO_flush(bio);
  33000. sz = sizeof(out);
  33001. XMEMSET(out, 0, sz);
  33002. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  33003. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  33004. BIO_clear_flags(bio64, ~0);
  33005. BIO_set_retry_read(bio);
  33006. BIO_free_all(bio); /* frees bio64s also */
  33007. printf(resultFmt, passed);
  33008. #endif
  33009. }
  33010. static void test_wolfSSL_BIO_printf(void)
  33011. {
  33012. #if defined(OPENSSL_ALL)
  33013. BIO* bio;
  33014. int sz = 7;
  33015. char msg[] = "TLS 1.3 for the world";
  33016. char out[60];
  33017. char expected[] = "TLS 1.3 for the world : sz = 7";
  33018. printf(testingFmt, "wolfSSL_BIO_printf()");
  33019. XMEMSET(out, 0, sizeof(out));
  33020. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33021. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  33022. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  33023. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  33024. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  33025. BIO_free(bio);
  33026. printf(resultFmt, passed);
  33027. #endif
  33028. }
  33029. static void test_wolfSSL_BIO_f_md(void)
  33030. {
  33031. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  33032. BIO *bio, *mem;
  33033. char msg[] = "message to hash";
  33034. char out[60];
  33035. EVP_MD_CTX* ctx;
  33036. const unsigned char testKey[] =
  33037. {
  33038. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  33039. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  33040. 0x0b, 0x0b, 0x0b, 0x0b
  33041. };
  33042. const char testData[] = "Hi There";
  33043. const unsigned char testResult[] =
  33044. {
  33045. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  33046. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  33047. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  33048. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  33049. };
  33050. const unsigned char expectedHash[] =
  33051. {
  33052. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  33053. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  33054. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  33055. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  33056. };
  33057. const unsigned char emptyHash[] =
  33058. {
  33059. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  33060. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  33061. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  33062. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  33063. };
  33064. unsigned char check[sizeof(testResult) + 1];
  33065. size_t checkSz = -1;
  33066. EVP_PKEY* key;
  33067. printf(testingFmt, "wolfSSL_BIO_f_md()");
  33068. XMEMSET(out, 0, sizeof(out));
  33069. AssertNotNull(bio = BIO_new(BIO_f_md()));
  33070. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  33071. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  33072. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  33073. /* should not be able to write/read yet since just digest wrapper and no
  33074. * data is passing through the bio */
  33075. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  33076. AssertIntEQ(BIO_pending(bio), 0);
  33077. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  33078. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  33079. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  33080. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  33081. BIO_reset(bio);
  33082. /* append BIO mem to bio in order to read/write */
  33083. AssertNotNull(bio = BIO_push(bio, mem));
  33084. XMEMSET(out, 0, sizeof(out));
  33085. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  33086. AssertIntEQ(BIO_pending(bio), 16);
  33087. /* this just reads the message and does not hash it (gets calls final) */
  33088. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  33089. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  33090. /* create a message digest using BIO */
  33091. XMEMSET(out, 0, sizeof(out));
  33092. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  33093. AssertIntEQ(BIO_pending(mem), 16);
  33094. AssertIntEQ(BIO_pending(bio), 16);
  33095. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  33096. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  33097. BIO_free(bio);
  33098. BIO_free(mem);
  33099. /* test with HMAC */
  33100. XMEMSET(out, 0, sizeof(out));
  33101. AssertNotNull(bio = BIO_new(BIO_f_md()));
  33102. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  33103. BIO_get_md_ctx(bio, &ctx);
  33104. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  33105. testKey, (int)sizeof(testKey)));
  33106. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  33107. AssertNotNull(bio = BIO_push(bio, mem));
  33108. BIO_write(bio, testData, (int)strlen(testData));
  33109. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  33110. EVP_DigestSignFinal(ctx, check, &checkSz);
  33111. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  33112. EVP_PKEY_free(key);
  33113. BIO_free(bio);
  33114. BIO_free(mem);
  33115. printf(resultFmt, passed);
  33116. #endif
  33117. }
  33118. static void test_wolfSSL_BIO_up_ref(void)
  33119. {
  33120. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  33121. BIO* bio;
  33122. printf(testingFmt, "wolfSSL_BIO_up_ref()");
  33123. AssertNotNull(bio = BIO_new(BIO_f_md()));
  33124. AssertIntEQ(BIO_up_ref(NULL), 0);
  33125. AssertIntEQ(BIO_up_ref(bio), 1);
  33126. BIO_free(bio);
  33127. AssertIntEQ(BIO_up_ref(bio), 1);
  33128. BIO_free(bio);
  33129. BIO_free(bio);
  33130. printf(resultFmt, "passed");
  33131. #endif
  33132. }
  33133. #endif /* !NO_BIO */
  33134. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  33135. /* test that the callback arg is correct */
  33136. static int certCbArg = 0;
  33137. static int clientCertCb(WOLFSSL* ssl, void* arg)
  33138. {
  33139. if (ssl == NULL || arg != &certCbArg)
  33140. return 0;
  33141. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  33142. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  33143. return 0;
  33144. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  33145. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  33146. return 0;
  33147. return 1;
  33148. }
  33149. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  33150. {
  33151. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  33152. }
  33153. /**
  33154. * This is only done because test_client_nofail has no way to stop
  33155. * certificate and key loading
  33156. */
  33157. static void clientCertClearCb(WOLFSSL* ssl)
  33158. {
  33159. /* Clear the loaded certs to force the callbacks to set them up */
  33160. SSL_certs_clear(ssl);
  33161. }
  33162. static int serverCertCb(WOLFSSL* ssl, void* arg)
  33163. {
  33164. if (ssl == NULL || arg != &certCbArg)
  33165. return 0;
  33166. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  33167. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  33168. return 0;
  33169. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  33170. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  33171. return 0;
  33172. return 1;
  33173. }
  33174. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  33175. {
  33176. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  33177. }
  33178. /**
  33179. * This is only done because test_server_nofail has no way to stop
  33180. * certificate and key loading
  33181. */
  33182. static void serverCertClearCb(WOLFSSL* ssl)
  33183. {
  33184. /* Clear the loaded certs to force the callbacks to set them up */
  33185. SSL_certs_clear(ssl);
  33186. }
  33187. #endif
  33188. static void test_wolfSSL_cert_cb(void)
  33189. {
  33190. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  33191. callback_functions func_cb_client;
  33192. callback_functions func_cb_server;
  33193. tcp_ready ready;
  33194. func_args client_args;
  33195. func_args server_args;
  33196. THREAD_TYPE serverThread;
  33197. XMEMSET(&client_args, 0, sizeof(func_args));
  33198. XMEMSET(&server_args, 0, sizeof(func_args));
  33199. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  33200. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  33201. #ifdef WOLFSSL_TIRTOS
  33202. fdOpenSession(Task_self());
  33203. #endif
  33204. StartTCP();
  33205. InitTcpReady(&ready);
  33206. #if defined(USE_WINDOWS_API)
  33207. /* use RNG to get random port if using windows */
  33208. ready.port = GetRandomPort();
  33209. #endif
  33210. server_args.signal = &ready;
  33211. client_args.signal = &ready;
  33212. client_args.callbacks = &func_cb_client;
  33213. server_args.callbacks = &func_cb_server;
  33214. func_cb_client.ctx_ready = clientCertSetupCb;
  33215. func_cb_client.ssl_ready = clientCertClearCb;
  33216. func_cb_server.ctx_ready = serverCertSetupCb;
  33217. func_cb_server.ssl_ready = serverCertClearCb;
  33218. start_thread(test_server_nofail, &server_args, &serverThread);
  33219. wait_tcp_ready(&server_args);
  33220. test_client_nofail(&client_args, NULL);
  33221. join_thread(serverThread);
  33222. AssertTrue(client_args.return_code);
  33223. AssertTrue(server_args.return_code);
  33224. FreeTcpReady(&ready);
  33225. #ifdef WOLFSSL_TIRTOS
  33226. fdOpenSession(Task_self());
  33227. #endif
  33228. #endif
  33229. }
  33230. static void test_wolfSSL_SESSION(void)
  33231. {
  33232. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  33233. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  33234. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  33235. WOLFSSL* ssl;
  33236. WOLFSSL_CTX* ctx;
  33237. WOLFSSL_SESSION* sess;
  33238. WOLFSSL_SESSION* sess_copy;
  33239. unsigned char* sessDer = NULL;
  33240. unsigned char* ptr = NULL;
  33241. #ifdef OPENSSL_EXTRA
  33242. const unsigned char context[] = "user app context";
  33243. unsigned int contextSz = (unsigned int)sizeof(context);
  33244. #endif
  33245. int ret, err, sockfd, sz;
  33246. tcp_ready ready;
  33247. func_args server_args;
  33248. THREAD_TYPE serverThread;
  33249. char msg[80];
  33250. const char* sendGET = "GET";
  33251. printf(testingFmt, "wolfSSL_SESSION()");
  33252. /* TLS v1.3 requires session tickets */
  33253. /* CHACHA and POLY1305 required for myTicketEncCb */
  33254. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  33255. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  33256. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  33257. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33258. #else
  33259. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33260. #endif
  33261. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33262. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33263. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  33264. #ifdef WOLFSSL_ENCRYPTED_KEYS
  33265. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  33266. #endif
  33267. #ifdef HAVE_SESSION_TICKET
  33268. /* Use session tickets, for ticket tests below */
  33269. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  33270. #endif
  33271. XMEMSET(&server_args, 0, sizeof(func_args));
  33272. #ifdef WOLFSSL_TIRTOS
  33273. fdOpenSession(Task_self());
  33274. #endif
  33275. StartTCP();
  33276. InitTcpReady(&ready);
  33277. #if defined(USE_WINDOWS_API)
  33278. /* use RNG to get random port if using windows */
  33279. ready.port = GetRandomPort();
  33280. #endif
  33281. server_args.signal = &ready;
  33282. start_thread(test_server_nofail, &server_args, &serverThread);
  33283. wait_tcp_ready(&server_args);
  33284. /* client connection */
  33285. ssl = wolfSSL_new(ctx);
  33286. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  33287. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), SSL_SUCCESS);
  33288. #ifdef WOLFSSL_ASYNC_CRYPT
  33289. err = 0; /* Reset error */
  33290. #endif
  33291. do {
  33292. #ifdef WOLFSSL_ASYNC_CRYPT
  33293. if (err == WC_PENDING_E) {
  33294. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  33295. if (ret < 0) { break; } else if (ret == 0) { continue; }
  33296. }
  33297. #endif
  33298. ret = wolfSSL_connect(ssl);
  33299. err = wolfSSL_get_error(ssl, 0);
  33300. } while (err == WC_PENDING_E);
  33301. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  33302. #ifdef WOLFSSL_ASYNC_CRYPT
  33303. err = 0; /* Reset error */
  33304. #endif
  33305. do {
  33306. #ifdef WOLFSSL_ASYNC_CRYPT
  33307. if (err == WC_PENDING_E) {
  33308. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  33309. if (ret < 0) { break; } else if (ret == 0) { continue; }
  33310. }
  33311. #endif
  33312. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  33313. err = wolfSSL_get_error(ssl, 0);
  33314. } while (err == WC_PENDING_E);
  33315. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  33316. #ifdef WOLFSSL_ASYNC_CRYPT
  33317. err = 0; /* Reset error */
  33318. #endif
  33319. do {
  33320. #ifdef WOLFSSL_ASYNC_CRYPT
  33321. if (err == WC_PENDING_E) {
  33322. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  33323. if (ret < 0) { break; } else if (ret == 0) { continue; }
  33324. }
  33325. #endif
  33326. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  33327. err = wolfSSL_get_error(ssl, 0);
  33328. } while (err == WC_PENDING_E);
  33329. AssertIntEQ(ret, 23);
  33330. sess = wolfSSL_get_session(ssl);
  33331. #if defined(OPENSSL_EXTRA)
  33332. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  33333. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  33334. #else
  33335. AssertIntEQ(wolfSSL_SESSION_is_resumable(NULL), 0);
  33336. AssertIntEQ(wolfSSL_SESSION_is_resumable(sess), 1);
  33337. #endif
  33338. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  33339. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  33340. #ifdef HAVE_SESSION_TICKET
  33341. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  33342. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  33343. SESSION_TICKET_HINT_DEFAULT);
  33344. #else
  33345. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  33346. #endif
  33347. wolfSSL_shutdown(ssl);
  33348. wolfSSL_free(ssl);
  33349. join_thread(serverThread);
  33350. FreeTcpReady(&ready);
  33351. #ifdef WOLFSSL_TIRTOS
  33352. fdOpenSession(Task_self());
  33353. #endif
  33354. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  33355. {
  33356. X509 *x509;
  33357. char buf[30];
  33358. int bufSz;
  33359. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  33360. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  33361. X509_get_subject_name(x509), NID_organizationalUnitName,
  33362. buf, sizeof(buf))), 0);
  33363. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  33364. if (bufSz == 7) {
  33365. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  33366. }
  33367. if (bufSz == 16) {
  33368. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  33369. }
  33370. }
  33371. #endif
  33372. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  33373. wolfSSL_SESSION_free(sess_copy);
  33374. /* get session from DER and update the timeout */
  33375. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  33376. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  33377. wolfSSL_SESSION_free(sess);
  33378. ptr = sessDer;
  33379. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  33380. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  33381. (const unsigned char**)&ptr, sz));
  33382. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  33383. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  33384. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  33385. /* successful set session test */
  33386. AssertNotNull(ssl = wolfSSL_new(ctx));
  33387. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_SUCCESS);
  33388. #ifdef HAVE_SESSION_TICKET
  33389. /* Test set/get session ticket */
  33390. {
  33391. const char* ticket = "This is a session ticket";
  33392. char buf[64] = {0};
  33393. word32 bufSz = (word32)sizeof(buf);
  33394. AssertIntEQ(SSL_SUCCESS,
  33395. wolfSSL_set_SessionTicket(ssl, (byte *)ticket, (word32)XSTRLEN(ticket)));
  33396. AssertIntEQ(SSL_SUCCESS,
  33397. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  33398. AssertStrEQ(ticket, buf);
  33399. }
  33400. #endif
  33401. #ifdef OPENSSL_EXTRA
  33402. /* session timeout case */
  33403. /* make the session to be expired */
  33404. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  33405. XSLEEP_MS(1200);
  33406. /* SSL_set_session should reject specified session but return success
  33407. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  33408. */
  33409. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  33410. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  33411. #else
  33412. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  33413. #endif
  33414. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  33415. /* fail case with miss match session context IDs (use compatibility API) */
  33416. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  33417. SSL_SUCCESS);
  33418. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  33419. wolfSSL_free(ssl);
  33420. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  33421. SSL_FAILURE);
  33422. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  33423. SSL_SUCCESS);
  33424. AssertNotNull(ssl = wolfSSL_new(ctx));
  33425. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  33426. #endif
  33427. wolfSSL_free(ssl);
  33428. SSL_SESSION_free(sess);
  33429. wolfSSL_CTX_free(ctx);
  33430. printf(resultFmt, passed);
  33431. #endif
  33432. }
  33433. static void test_wolfSSL_ticket_keys(void)
  33434. {
  33435. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  33436. !defined(NO_WOLFSSL_SERVER)
  33437. WOLFSSL_CTX* ctx;
  33438. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  33439. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33440. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  33441. WOLFSSL_FAILURE);
  33442. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  33443. WOLFSSL_FAILURE);
  33444. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  33445. WOLFSSL_FAILURE);
  33446. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  33447. WOLFSSL_FAILURE);
  33448. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  33449. WOLFSSL_FAILURE);
  33450. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  33451. WOLFSSL_FAILURE);
  33452. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  33453. WOLFSSL_FAILURE);
  33454. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  33455. WOLFSSL_FAILURE);
  33456. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  33457. WOLFSSL_FAILURE);
  33458. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  33459. WOLFSSL_FAILURE);
  33460. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  33461. WOLFSSL_FAILURE);
  33462. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  33463. WOLFSSL_FAILURE);
  33464. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  33465. WOLFSSL_FAILURE);
  33466. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  33467. WOLFSSL_FAILURE);
  33468. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  33469. WOLFSSL_SUCCESS);
  33470. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  33471. WOLFSSL_SUCCESS);
  33472. wolfSSL_CTX_free(ctx);
  33473. #endif
  33474. }
  33475. #ifndef NO_BIO
  33476. static void test_wolfSSL_d2i_PUBKEY(void)
  33477. {
  33478. #if defined(OPENSSL_EXTRA)
  33479. BIO* bio;
  33480. EVP_PKEY* pkey;
  33481. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  33482. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33483. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  33484. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  33485. /* RSA PUBKEY test */
  33486. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  33487. sizeof_client_keypub_der_2048), 0);
  33488. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  33489. EVP_PKEY_free(pkey);
  33490. #endif
  33491. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  33492. /* ECC PUBKEY test */
  33493. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  33494. sizeof_ecc_clikeypub_der_256), 0);
  33495. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  33496. EVP_PKEY_free(pkey);
  33497. #endif
  33498. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  33499. /* DSA PUBKEY test */
  33500. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  33501. sizeof_dsa_pub_key_der_2048), 0);
  33502. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  33503. EVP_PKEY_free(pkey);
  33504. #endif
  33505. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  33506. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  33507. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  33508. (HAVE_FIPS_VERSION > 2))
  33509. /* DH PUBKEY test */
  33510. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  33511. sizeof_dh_pub_key_der_2048), 0);
  33512. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  33513. EVP_PKEY_free(pkey);
  33514. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  33515. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  33516. BIO_free(bio);
  33517. (void)pkey;
  33518. printf(resultFmt, passed);
  33519. #endif
  33520. }
  33521. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  33522. static void test_wolfSSL_d2i_PrivateKeys_bio(void)
  33523. {
  33524. BIO* bio = NULL;
  33525. EVP_PKEY* pkey = NULL;
  33526. #ifndef NO_RSA
  33527. #endif
  33528. WOLFSSL_CTX* ctx;
  33529. #if defined(WOLFSSL_KEY_GEN)
  33530. unsigned char buff[4096];
  33531. unsigned char* bufPtr = buff;
  33532. #endif
  33533. printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
  33534. /* test creating new EVP_PKEY with bad arg */
  33535. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  33536. /* test loading RSA key using BIO */
  33537. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  33538. {
  33539. XFILE file;
  33540. const char* fname = "./certs/server-key.der";
  33541. size_t sz;
  33542. byte* buf;
  33543. file = XFOPEN(fname, "rb");
  33544. AssertTrue((file != XBADFILE));
  33545. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  33546. sz = XFTELL(file);
  33547. XREWIND(file);
  33548. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  33549. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  33550. XFCLOSE(file);
  33551. /* Test using BIO new mem and loading DER private key */
  33552. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  33553. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  33554. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  33555. BIO_free(bio);
  33556. bio = NULL;
  33557. EVP_PKEY_free(pkey);
  33558. pkey = NULL;
  33559. }
  33560. #endif
  33561. /* test loading ECC key using BIO */
  33562. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  33563. {
  33564. XFILE file;
  33565. const char* fname = "./certs/ecc-key.der";
  33566. size_t sz;
  33567. byte* buf;
  33568. file = XFOPEN(fname, "rb");
  33569. AssertTrue((file != XBADFILE));
  33570. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  33571. sz = XFTELL(file);
  33572. XREWIND(file);
  33573. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  33574. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  33575. XFCLOSE(file);
  33576. /* Test using BIO new mem and loading DER private key */
  33577. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  33578. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  33579. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  33580. BIO_free(bio);
  33581. bio = NULL;
  33582. EVP_PKEY_free(pkey);
  33583. pkey = NULL;
  33584. }
  33585. #endif
  33586. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33587. #ifndef NO_WOLFSSL_SERVER
  33588. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33589. #else
  33590. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33591. #endif
  33592. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  33593. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  33594. {
  33595. RSA* rsa = NULL;
  33596. /* Tests bad parameters */
  33597. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  33598. /* RSA not set yet, expecting to fail*/
  33599. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  33600. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  33601. /* set RSA using bio*/
  33602. AssertIntGT(BIO_write(bio, client_key_der_2048,
  33603. sizeof_client_key_der_2048), 0);
  33604. AssertNotNull(rsa = d2i_RSAPrivateKey_bio(bio, NULL));
  33605. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  33606. /*i2d RSAprivate key tests */
  33607. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  33608. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  33609. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  33610. sizeof_client_key_der_2048);
  33611. bufPtr = NULL;
  33612. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  33613. sizeof_client_key_der_2048);
  33614. AssertNotNull(bufPtr);
  33615. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  33616. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  33617. RSA_free(rsa);
  33618. }
  33619. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  33620. SSL_CTX_free(ctx);
  33621. ctx = NULL;
  33622. BIO_free(bio);
  33623. bio = NULL;
  33624. printf(resultFmt, passed);
  33625. }
  33626. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  33627. #endif /* !NO_BIO */
  33628. static void test_wolfSSL_sk_GENERAL_NAME(void)
  33629. {
  33630. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  33631. !defined(NO_RSA)
  33632. X509* x509;
  33633. GENERAL_NAME* gn;
  33634. unsigned char buf[4096];
  33635. const unsigned char* bufPt;
  33636. int bytes, i;
  33637. XFILE f;
  33638. STACK_OF(GENERAL_NAME)* sk;
  33639. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  33640. f = XFOPEN(cliCertDerFileExt, "rb");
  33641. AssertTrue((f != XBADFILE));
  33642. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  33643. XFCLOSE(f);
  33644. bufPt = buf;
  33645. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  33646. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  33647. NID_subject_alt_name, NULL, NULL));
  33648. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  33649. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  33650. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  33651. switch (gn->type) {
  33652. case GEN_DNS:
  33653. printf("found type GEN_DNS\n");
  33654. break;
  33655. case GEN_EMAIL:
  33656. printf("found type GEN_EMAIL\n");
  33657. break;
  33658. case GEN_URI:
  33659. printf("found type GEN_URI\n");
  33660. break;
  33661. }
  33662. }
  33663. X509_free(x509);
  33664. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  33665. printf(resultFmt, passed);
  33666. #endif
  33667. }
  33668. static void test_wolfSSL_GENERAL_NAME_print(void)
  33669. {
  33670. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  33671. X509* x509;
  33672. GENERAL_NAME* gn;
  33673. unsigned char buf[4096];
  33674. const unsigned char* bufPt;
  33675. int bytes;
  33676. XFILE f;
  33677. STACK_OF(GENERAL_NAME)* sk;
  33678. BIO* out;
  33679. unsigned char outbuf[128];
  33680. X509_EXTENSION* ext;
  33681. AUTHORITY_INFO_ACCESS* aia;
  33682. ACCESS_DESCRIPTION* ad;
  33683. const unsigned char v4Addr[] = {192,168,53,1};
  33684. const unsigned char v6Addr[] =
  33685. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  33686. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  33687. const unsigned char email[] =
  33688. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  33689. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  33690. const char* dnsStr = "DNS:example.com";
  33691. const char* uriStr = "URI:http://127.0.0.1:22220";
  33692. const char* v4addStr = "IP Address:192.168.53.1";
  33693. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  33694. const char* emailStr = "email:info@wolfssl.com";
  33695. const char* othrStr = "othername:<unsupported>";
  33696. const char* x400Str = "X400Name:<unsupported>";
  33697. const char* ediStr = "EdiPartyName:<unsupported>";
  33698. printf(testingFmt, "test_wolfSSL_GENERAL_NAME_print()");
  33699. /* BIO to output */
  33700. AssertNotNull(out = BIO_new(BIO_s_mem()));
  33701. /* test for NULL param */
  33702. gn = NULL;
  33703. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  33704. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  33705. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  33706. /* test for GEN_DNS */
  33707. f = XFOPEN(cliCertDerFileExt, "rb");
  33708. AssertTrue((f != XBADFILE));
  33709. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  33710. XFCLOSE(f);
  33711. bufPt = buf;
  33712. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  33713. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  33714. NID_subject_alt_name, NULL, NULL));
  33715. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  33716. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  33717. XMEMSET(outbuf,0,sizeof(outbuf));
  33718. BIO_read(out, outbuf, sizeof(outbuf));
  33719. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  33720. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  33721. X509_free(x509);
  33722. /* test for GEN_URI */
  33723. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  33724. AssertTrue((f != XBADFILE));
  33725. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  33726. XFCLOSE(f);
  33727. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  33728. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  33729. AssertNotNull(aia);
  33730. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  33731. gn = ad->location;
  33732. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  33733. XMEMSET(outbuf,0,sizeof(outbuf));
  33734. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  33735. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  33736. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  33737. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  33738. AssertNotNull(aia);
  33739. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  33740. X509_free(x509);
  33741. /* test for GEN_IPADD */
  33742. /* ip v4 address */
  33743. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  33744. gn->type = GEN_IPADD;
  33745. gn->d.iPAddress->length = sizeof(v4Addr);
  33746. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  33747. sizeof(v4Addr)), 1);
  33748. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  33749. XMEMSET(outbuf,0,sizeof(outbuf));
  33750. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  33751. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  33752. GENERAL_NAME_free(gn);
  33753. /* ip v6 address */
  33754. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  33755. gn->type = GEN_IPADD;
  33756. gn->d.iPAddress->length = sizeof(v6Addr);
  33757. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  33758. sizeof(v6Addr)), 1);
  33759. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  33760. XMEMSET(outbuf,0,sizeof(outbuf));
  33761. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  33762. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  33763. GENERAL_NAME_free(gn);
  33764. /* test for GEN_EMAIL */
  33765. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  33766. gn->type = GEN_EMAIL;
  33767. gn->d.rfc822Name->length = sizeof(email);
  33768. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  33769. sizeof(email)), 1);
  33770. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  33771. XMEMSET(outbuf,0,sizeof(outbuf));
  33772. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  33773. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  33774. GENERAL_NAME_free(gn);
  33775. /* test for GEN_OTHERNAME */
  33776. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  33777. gn->type = GEN_OTHERNAME;
  33778. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  33779. XMEMSET(outbuf,0,sizeof(outbuf));
  33780. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  33781. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  33782. GENERAL_NAME_free(gn);
  33783. /* test for GEN_X400 */
  33784. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  33785. gn->type = GEN_X400;
  33786. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  33787. XMEMSET(outbuf,0,sizeof(outbuf));
  33788. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  33789. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  33790. GENERAL_NAME_free(gn);
  33791. /* test for GEN_EDIPARTY */
  33792. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  33793. gn->type = GEN_EDIPARTY;
  33794. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  33795. XMEMSET(outbuf,0,sizeof(outbuf));
  33796. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  33797. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  33798. GENERAL_NAME_free(gn);
  33799. BIO_free(out);
  33800. printf(resultFmt, passed);
  33801. #endif /* OPENSSL_ALL */
  33802. }
  33803. static void test_wolfSSL_sk_DIST_POINT(void)
  33804. {
  33805. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  33806. !defined(NO_RSA)
  33807. X509* x509;
  33808. unsigned char buf[4096];
  33809. const unsigned char* bufPt;
  33810. int bytes, i, j;
  33811. XFILE f;
  33812. DIST_POINT* dp;
  33813. GENERAL_NAME* gn;
  33814. ASN1_IA5STRING* uri;
  33815. STACK_OF(DIST_POINT)* dps;
  33816. STACK_OF(GENERAL_NAME)* gns;
  33817. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  33818. printf(testingFmt, "wolfSSL_sk_DIST_POINT()");
  33819. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  33820. AssertTrue((f != XBADFILE));
  33821. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  33822. XFCLOSE(f);
  33823. bufPt = buf;
  33824. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  33825. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  33826. NID_crl_distribution_points, NULL, NULL));
  33827. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  33828. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  33829. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  33830. gns = dp->distpoint->name.fullname;
  33831. AssertNotNull(gns);
  33832. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  33833. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  33834. gn = sk_GENERAL_NAME_value(gns, j);
  33835. AssertIntEQ(gn->type, GEN_URI);
  33836. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  33837. AssertNotNull(uri->data);
  33838. AssertIntGT(uri->length, 0);
  33839. }
  33840. }
  33841. X509_free(x509);
  33842. CRL_DIST_POINTS_free(dps);
  33843. printf(resultFmt, passed);
  33844. #endif
  33845. }
  33846. static void test_wolfSSL_MD4(void)
  33847. {
  33848. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  33849. MD4_CTX md4;
  33850. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  33851. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  33852. "789012345678901234567890";
  33853. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  33854. "\xcc\x05\x36";
  33855. int msgSz = (int)XSTRLEN(msg);
  33856. printf(testingFmt, "wolfSSL_MD4()");
  33857. XMEMSET(out, 0, sizeof(out));
  33858. MD4_Init(&md4);
  33859. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  33860. MD4_Final(out, &md4);
  33861. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  33862. printf(resultFmt, passed);
  33863. #endif
  33864. }
  33865. static void test_wolfSSL_RSA(void)
  33866. {
  33867. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  33868. defined(WOLFSSL_KEY_GEN)
  33869. RSA* rsa;
  33870. const BIGNUM *n;
  33871. const BIGNUM *e;
  33872. const BIGNUM *d;
  33873. const BIGNUM *p;
  33874. const BIGNUM *q;
  33875. const BIGNUM *dmp1;
  33876. const BIGNUM *dmq1;
  33877. const BIGNUM *iqmp;
  33878. printf(testingFmt, "wolfSSL_RSA()");
  33879. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  33880. AssertIntEQ(RSA_size(rsa), 256);
  33881. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(HAVE_FAST_RSA) && \
  33882. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  33883. (HAVE_FIPS_VERSION >= 2))) && !defined(HAVE_SELFTEST) && \
  33884. !defined(HAVE_INTEL_QA) && !defined(WOLFSSL_NO_RSA_KEY_CHECK)
  33885. AssertIntEQ(RSA_check_key(rsa), WOLFSSL_SUCCESS);
  33886. #endif
  33887. /* sanity check */
  33888. AssertIntEQ(RSA_bits(NULL), 0);
  33889. /* key */
  33890. AssertIntEQ(RSA_bits(rsa), 2048);
  33891. RSA_get0_key(rsa, &n, &e, &d);
  33892. AssertPtrEq(rsa->n, n);
  33893. AssertPtrEq(rsa->e, e);
  33894. AssertPtrEq(rsa->d, d);
  33895. AssertNotNull(n = BN_new());
  33896. AssertNotNull(e = BN_new());
  33897. AssertNotNull(d = BN_new());
  33898. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  33899. AssertPtrEq(rsa->n, n);
  33900. AssertPtrEq(rsa->e, e);
  33901. AssertPtrEq(rsa->d, d);
  33902. /* crt_params */
  33903. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  33904. AssertPtrEq(rsa->dmp1, dmp1);
  33905. AssertPtrEq(rsa->dmq1, dmq1);
  33906. AssertPtrEq(rsa->iqmp, iqmp);
  33907. AssertNotNull(dmp1 = BN_new());
  33908. AssertNotNull(dmq1 = BN_new());
  33909. AssertNotNull(iqmp = BN_new());
  33910. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1, (BIGNUM*)iqmp), 1);
  33911. AssertPtrEq(rsa->dmp1, dmp1);
  33912. AssertPtrEq(rsa->dmq1, dmq1);
  33913. AssertPtrEq(rsa->iqmp, iqmp);
  33914. /* factors */
  33915. RSA_get0_factors(rsa, &p, &q);
  33916. AssertPtrEq(rsa->p, p);
  33917. AssertPtrEq(rsa->q, q);
  33918. AssertNotNull(p = BN_new());
  33919. AssertNotNull(q = BN_new());
  33920. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  33921. AssertPtrEq(rsa->p, p);
  33922. AssertPtrEq(rsa->q, q);
  33923. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  33924. AssertIntEQ(RSA_bits(rsa), 21);
  33925. RSA_free(rsa);
  33926. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  33927. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  33928. AssertIntEQ(RSA_size(rsa), 384);
  33929. AssertIntEQ(RSA_bits(rsa), 3072);
  33930. RSA_free(rsa);
  33931. #endif
  33932. /* remove for now with odd key size until adjusting rsa key size check with
  33933. wc_MakeRsaKey()
  33934. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  33935. RSA_free(rsa);
  33936. */
  33937. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  33938. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  33939. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  33940. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  33941. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  33942. {
  33943. byte buff[FOURK_BUF];
  33944. byte der[FOURK_BUF];
  33945. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  33946. XFILE f;
  33947. int bytes;
  33948. /* test loading encrypted RSA private pem w/o password */
  33949. f = XFOPEN(PrivKeyPemFile, "rb");
  33950. AssertTrue((f != XBADFILE));
  33951. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  33952. XFCLOSE(f);
  33953. XMEMSET(der, 0, sizeof(der));
  33954. /* test that error value is returned with no password */
  33955. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  33956. }
  33957. #endif
  33958. printf(resultFmt, passed);
  33959. #endif
  33960. }
  33961. static void test_wolfSSL_RSA_DER(void)
  33962. {
  33963. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  33964. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  33965. RSA *rsa;
  33966. int i;
  33967. unsigned char *buff = NULL;
  33968. struct tbl_s
  33969. {
  33970. const unsigned char *der;
  33971. int sz;
  33972. } tbl[] = {
  33973. #ifdef USE_CERT_BUFFERS_1024
  33974. {client_key_der_1024, sizeof_client_key_der_1024},
  33975. {server_key_der_1024, sizeof_server_key_der_1024},
  33976. #endif
  33977. #ifdef USE_CERT_BUFFERS_2048
  33978. {client_key_der_2048, sizeof_client_key_der_2048},
  33979. {server_key_der_2048, sizeof_server_key_der_2048},
  33980. #endif
  33981. {NULL, 0}
  33982. };
  33983. /* Public Key DER */
  33984. struct tbl_s pub[] = {
  33985. #ifdef USE_CERT_BUFFERS_1024
  33986. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  33987. #endif
  33988. #ifdef USE_CERT_BUFFERS_2048
  33989. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  33990. #endif
  33991. {NULL, 0}
  33992. };
  33993. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  33994. for (i = 0; tbl[i].der != NULL; i++)
  33995. {
  33996. AssertNotNull(d2i_RSAPublicKey(&rsa, &tbl[i].der, tbl[i].sz));
  33997. AssertNotNull(rsa);
  33998. RSA_free(rsa);
  33999. }
  34000. for (i = 0; tbl[i].der != NULL; i++)
  34001. {
  34002. AssertNotNull(d2i_RSAPrivateKey(&rsa, &tbl[i].der, tbl[i].sz));
  34003. AssertNotNull(rsa);
  34004. RSA_free(rsa);
  34005. }
  34006. for (i = 0; pub[i].der != NULL; i++)
  34007. {
  34008. AssertNotNull(d2i_RSAPublicKey(&rsa, &pub[i].der, pub[i].sz));
  34009. AssertNotNull(rsa);
  34010. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  34011. buff = NULL;
  34012. AssertIntEQ(i2d_RSAPublicKey(rsa, &buff), pub[i].sz);
  34013. AssertNotNull(buff);
  34014. AssertIntEQ(0, memcmp((void *)buff, (void *)pub[i].der, pub[i].sz));
  34015. XFREE((void *)buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  34016. RSA_free(rsa);
  34017. }
  34018. printf(resultFmt, passed);
  34019. #endif
  34020. }
  34021. static void test_wolfSSL_RSA_get0_key(void)
  34022. {
  34023. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  34024. RSA *rsa = NULL;
  34025. const BIGNUM* n = NULL;
  34026. const BIGNUM* e = NULL;
  34027. const BIGNUM* d = NULL;
  34028. const unsigned char* der;
  34029. int derSz;
  34030. #ifdef USE_CERT_BUFFERS_1024
  34031. der = client_key_der_1024;
  34032. derSz = sizeof_client_key_der_1024;
  34033. #elif defined(USE_CERT_BUFFERS_2048)
  34034. der = client_key_der_2048;
  34035. derSz = sizeof_client_key_der_2048;
  34036. #else
  34037. der = NULL;
  34038. derSz = 0;
  34039. #endif
  34040. printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
  34041. if (der != NULL) {
  34042. RSA_get0_key(NULL, NULL, NULL, NULL);
  34043. RSA_get0_key(rsa, NULL, NULL, NULL);
  34044. RSA_get0_key(NULL, &n, &e, &d);
  34045. AssertNull(n);
  34046. AssertNull(e);
  34047. AssertNull(d);
  34048. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  34049. AssertNotNull(rsa);
  34050. RSA_get0_key(rsa, NULL, NULL, NULL);
  34051. RSA_get0_key(rsa, &n, NULL, NULL);
  34052. AssertNotNull(n);
  34053. RSA_get0_key(rsa, NULL, &e, NULL);
  34054. AssertNotNull(e);
  34055. RSA_get0_key(rsa, NULL, NULL, &d);
  34056. AssertNotNull(d);
  34057. RSA_get0_key(rsa, &n, &e, &d);
  34058. AssertNotNull(n);
  34059. AssertNotNull(e);
  34060. AssertNotNull(d);
  34061. RSA_free(rsa);
  34062. }
  34063. printf(resultFmt, passed);
  34064. #endif
  34065. }
  34066. static void test_wolfSSL_RSA_meth(void)
  34067. {
  34068. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  34069. RSA *rsa;
  34070. RSA_METHOD *rsa_meth;
  34071. printf(testingFmt, "test_wolfSSL_RSA_meth");
  34072. #ifdef WOLFSSL_KEY_GEN
  34073. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  34074. RSA_free(rsa);
  34075. #else
  34076. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  34077. #endif
  34078. AssertNotNull(rsa_meth =
  34079. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  34080. #ifndef NO_WOLFSSL_STUB
  34081. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  34082. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  34083. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  34084. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  34085. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  34086. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  34087. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  34088. #endif
  34089. AssertNotNull(rsa = RSA_new());
  34090. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  34091. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  34092. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  34093. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  34094. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  34095. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC | RSA_METHOD_FLAG_NO_CHECK);
  34096. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  34097. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  34098. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  34099. /* rsa_meth is freed here */
  34100. RSA_free(rsa);
  34101. printf(resultFmt, passed);
  34102. #endif
  34103. }
  34104. static void test_wolfSSL_verify_mode(void)
  34105. {
  34106. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  34107. WOLFSSL* ssl;
  34108. WOLFSSL_CTX* ctx;
  34109. printf(testingFmt, "test_wolfSSL_verify()");
  34110. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34111. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34112. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34113. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  34114. AssertNotNull(ssl = SSL_new(ctx));
  34115. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  34116. SSL_free(ssl);
  34117. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  34118. AssertNotNull(ssl = SSL_new(ctx));
  34119. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  34120. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  34121. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  34122. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  34123. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  34124. SSL_free(ssl);
  34125. wolfSSL_CTX_set_verify(ctx,
  34126. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  34127. AssertNotNull(ssl = SSL_new(ctx));
  34128. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  34129. AssertIntEQ(SSL_get_verify_mode(ssl),
  34130. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  34131. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  34132. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  34133. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  34134. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  34135. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  34136. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  34137. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  34138. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  34139. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  34140. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  34141. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  34142. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  34143. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  34144. #endif
  34145. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  34146. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  34147. SSL_free(ssl);
  34148. SSL_CTX_free(ctx);
  34149. printf(resultFmt, passed);
  34150. #endif
  34151. }
  34152. static void test_wolfSSL_verify_depth(void)
  34153. {
  34154. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  34155. WOLFSSL* ssl;
  34156. WOLFSSL_CTX* ctx;
  34157. long depth;
  34158. printf(testingFmt, "test_wolfSSL_verify_depth()");
  34159. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34160. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34161. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34162. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  34163. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  34164. AssertNotNull(ssl = SSL_new(ctx));
  34165. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  34166. SSL_free(ssl);
  34167. SSL_CTX_set_verify_depth(ctx, -1);
  34168. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  34169. SSL_CTX_set_verify_depth(ctx, 2);
  34170. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  34171. AssertNotNull(ssl = SSL_new(ctx));
  34172. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  34173. SSL_free(ssl);
  34174. SSL_CTX_free(ctx);
  34175. printf(resultFmt, passed);
  34176. #endif
  34177. }
  34178. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  34179. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  34180. * buffer of 64 bytes.
  34181. *
  34182. * returns the size of the digest buffer on success and a negative value on
  34183. * failure.
  34184. */
  34185. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  34186. {
  34187. HMAC_CTX ctx1;
  34188. HMAC_CTX ctx2;
  34189. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  34190. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  34191. unsigned char long_key[] =
  34192. "0123456789012345678901234567890123456789"
  34193. "0123456789012345678901234567890123456789"
  34194. "0123456789012345678901234567890123456789"
  34195. "0123456789012345678901234567890123456789";
  34196. unsigned char msg[] = "message to hash";
  34197. unsigned int digestSz = 64;
  34198. int keySz = sizeof(key);
  34199. int long_keySz = sizeof(long_key);
  34200. int msgSz = sizeof(msg);
  34201. unsigned char digest2[64];
  34202. unsigned int digestSz2 = 64;
  34203. HMAC_CTX_init(&ctx1);
  34204. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  34205. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34206. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  34207. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34208. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  34209. HMAC_CTX_cleanup(&ctx1);
  34210. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34211. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  34212. HMAC_CTX_cleanup(&ctx2);
  34213. AssertIntEQ(digestSz, digestSz2);
  34214. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  34215. /* test HMAC_Init with NULL key */
  34216. /* init after copy */
  34217. printf("test HMAC_Init with NULL key (0)\n");
  34218. HMAC_CTX_init(&ctx1);
  34219. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  34220. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34221. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  34222. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  34223. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34224. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34225. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  34226. HMAC_CTX_cleanup(&ctx1);
  34227. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  34228. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34229. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34230. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  34231. HMAC_CTX_cleanup(&ctx2);
  34232. AssertIntEQ(digestSz, digestSz2);
  34233. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  34234. /* long key */
  34235. printf("test HMAC_Init with NULL key (1)\n");
  34236. HMAC_CTX_init(&ctx1);
  34237. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  34238. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34239. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  34240. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  34241. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34242. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34243. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  34244. HMAC_CTX_cleanup(&ctx1);
  34245. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  34246. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34247. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34248. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  34249. HMAC_CTX_cleanup(&ctx2);
  34250. AssertIntEQ(digestSz, digestSz2);
  34251. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  34252. /* init before copy */
  34253. printf("test HMAC_Init with NULL key (2)\n");
  34254. HMAC_CTX_init(&ctx1);
  34255. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  34256. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34257. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  34258. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  34259. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34260. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  34261. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  34262. HMAC_CTX_cleanup(&ctx1);
  34263. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34264. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  34265. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  34266. HMAC_CTX_cleanup(&ctx2);
  34267. AssertIntEQ(digestSz, digestSz2);
  34268. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  34269. return digestSz;
  34270. }
  34271. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  34272. static void test_wolfSSL_HMAC_CTX(void)
  34273. {
  34274. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  34275. unsigned char digest[64];
  34276. int digestSz;
  34277. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  34278. #ifndef NO_SHA
  34279. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  34280. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  34281. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  34282. #endif /* !NO_SHA */
  34283. #ifdef WOLFSSL_SHA224
  34284. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  34285. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  34286. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  34287. "\x02\x0E", digest, digestSz), 0);
  34288. #endif /* WOLFSSL_SHA224 */
  34289. #ifndef NO_SHA256
  34290. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  34291. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  34292. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  34293. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  34294. #endif /* !NO_SHA256 */
  34295. #ifdef WOLFSSL_SHA384
  34296. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  34297. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  34298. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  34299. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  34300. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  34301. digest, digestSz), 0);
  34302. #endif /* WOLFSSL_SHA384 */
  34303. #ifdef WOLFSSL_SHA512
  34304. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  34305. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  34306. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  34307. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  34308. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  34309. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  34310. digest, digestSz), 0);
  34311. #endif /* WOLFSSL_SHA512 */
  34312. #ifndef NO_MD5
  34313. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  34314. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  34315. "\xE4\x98\xDD", digest, digestSz), 0);
  34316. #endif /* !NO_MD5 */
  34317. printf(resultFmt, passed);
  34318. #endif
  34319. }
  34320. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  34321. static void sslMsgCb(int w, int version, int type, const void* buf,
  34322. size_t sz, SSL* ssl, void* arg)
  34323. {
  34324. int i;
  34325. unsigned char* pt = (unsigned char*)buf;
  34326. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  34327. (int)sz, version, type);
  34328. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  34329. printf("\n");
  34330. (void)ssl;
  34331. (void)arg;
  34332. }
  34333. #endif /* OPENSSL_EXTRA */
  34334. static void test_wolfSSL_msg_callback(void)
  34335. {
  34336. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  34337. WOLFSSL* ssl;
  34338. WOLFSSL_CTX* ctx;
  34339. printf(testingFmt, "wolfSSL_msg_callback()");
  34340. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34341. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  34342. SSL_FILETYPE_PEM));
  34343. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  34344. SSL_FILETYPE_PEM));
  34345. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  34346. SSL_SUCCESS);
  34347. AssertNotNull(ssl = SSL_new(ctx));
  34348. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  34349. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  34350. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  34351. SSL_free(ssl);
  34352. SSL_CTX_free(ctx);
  34353. printf(resultFmt, passed);
  34354. #endif
  34355. }
  34356. static void test_wolfSSL_SHA(void)
  34357. {
  34358. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  34359. printf(testingFmt, "wolfSSL_SHA()");
  34360. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES)
  34361. {
  34362. const unsigned char in[] = "abc";
  34363. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  34364. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  34365. unsigned char out[WC_SHA_DIGEST_SIZE];
  34366. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  34367. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  34368. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  34369. /* SHA interface test */
  34370. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  34371. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  34372. AssertNotNull(SHA(in, 0, out));
  34373. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  34374. AssertNotNull(SHA(NULL, 0, out));
  34375. AssertNotNull(SHA(NULL, 0, NULL));
  34376. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  34377. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  34378. }
  34379. #endif
  34380. #if !defined(NO_SHA256)
  34381. {
  34382. const unsigned char in[] = "abc";
  34383. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  34384. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  34385. "\x15\xAD";
  34386. unsigned char out[WC_SHA256_DIGEST_SIZE];
  34387. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  34388. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  34389. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  34390. #else
  34391. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  34392. #endif
  34393. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  34394. }
  34395. #endif
  34396. #if defined(WOLFSSL_SHA384)
  34397. {
  34398. const unsigned char in[] = "abc";
  34399. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  34400. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  34401. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  34402. "\xc8\x25\xa7";
  34403. unsigned char out[WC_SHA384_DIGEST_SIZE];
  34404. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  34405. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  34406. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  34407. #else
  34408. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  34409. #endif
  34410. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  34411. }
  34412. #endif
  34413. #if defined(WOLFSSL_SHA512)
  34414. {
  34415. const unsigned char in[] = "abc";
  34416. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  34417. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  34418. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  34419. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  34420. "\xa5\x4c\xa4\x9f";
  34421. unsigned char out[WC_SHA512_DIGEST_SIZE];
  34422. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  34423. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  34424. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  34425. #else
  34426. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  34427. #endif
  34428. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  34429. }
  34430. #endif
  34431. printf(resultFmt, passed);
  34432. #endif
  34433. }
  34434. static void test_wolfSSL_DH_1536_prime(void)
  34435. {
  34436. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  34437. BIGNUM* bn;
  34438. unsigned char bits[200];
  34439. int sz = 192; /* known binary size */
  34440. const byte expected[] = {
  34441. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  34442. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  34443. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  34444. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  34445. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  34446. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  34447. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  34448. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  34449. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  34450. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  34451. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  34452. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  34453. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  34454. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  34455. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  34456. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  34457. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  34458. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  34459. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  34460. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  34461. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  34462. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  34463. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  34464. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  34465. };
  34466. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  34467. bn = get_rfc3526_prime_1536(NULL);
  34468. AssertNotNull(bn);
  34469. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  34470. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  34471. BN_free(bn);
  34472. printf(resultFmt, passed);
  34473. #endif
  34474. }
  34475. static void test_wolfSSL_PEM_write_DHparams(void)
  34476. {
  34477. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  34478. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  34479. DH* dh;
  34480. BIO* bio;
  34481. XFILE fp;
  34482. byte pem[2048];
  34483. int pemSz;
  34484. const char expected[] =
  34485. "-----BEGIN DH PARAMETERS-----\n\
  34486. MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n\
  34487. v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n\
  34488. nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n\
  34489. joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n\
  34490. wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n\
  34491. tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n\
  34492. -----END DH PARAMETERS-----\n";
  34493. printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
  34494. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  34495. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  34496. XFCLOSE(fp);
  34497. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34498. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  34499. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  34500. BIO_free(bio);
  34501. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  34502. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  34503. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  34504. XFCLOSE(fp);
  34505. DH_free(dh);
  34506. /* check results */
  34507. XMEMSET(pem, 0, sizeof(pem));
  34508. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  34509. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  34510. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  34511. XFCLOSE(fp);
  34512. printf(resultFmt, passed);
  34513. #endif
  34514. }
  34515. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  34516. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  34517. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  34518. static void binary_dump(void *ptr, int size)
  34519. {
  34520. #ifdef WOLFSSL_EVP_PRINT
  34521. int i = 0;
  34522. unsigned char *p = (unsigned char *) ptr;
  34523. printf("{");
  34524. while((p != NULL) && (i < size)) {
  34525. if((i % 8) == 0) {
  34526. printf("\n");
  34527. printf(" ");
  34528. }
  34529. printf("0x%02x, ", p[i]);
  34530. i++;
  34531. }
  34532. printf("\n};\n");
  34533. #else
  34534. (void) ptr;
  34535. (void) size;
  34536. #endif
  34537. }
  34538. static int last_val = 0x0f;
  34539. static int check_result(unsigned char *data, int len)
  34540. {
  34541. int i;
  34542. for( ; len; ) {
  34543. last_val = (last_val + 1) % 16;
  34544. for(i = 0; i < 16; len--, i++, data++)
  34545. if(*data != last_val) {
  34546. return -1;
  34547. }
  34548. }
  34549. return 0;
  34550. }
  34551. static int r_offset;
  34552. static int w_offset;
  34553. static void init_offset(void)
  34554. {
  34555. r_offset = 0;
  34556. w_offset = 0;
  34557. }
  34558. static void get_record(unsigned char *data, unsigned char *buf, int len)
  34559. {
  34560. XMEMCPY(buf, data+r_offset, len);
  34561. r_offset += len;
  34562. }
  34563. static void set_record(unsigned char *data, unsigned char *buf, int len)
  34564. {
  34565. XMEMCPY(data+w_offset, buf, len);
  34566. w_offset += len;
  34567. }
  34568. static void set_plain(unsigned char *plain, int rec)
  34569. {
  34570. int i, j;
  34571. unsigned char *p = plain;
  34572. #define BLOCKSZ 16
  34573. for(i=0; i<(rec/BLOCKSZ); i++){
  34574. for(j=0; j<BLOCKSZ; j++)
  34575. *p++ = (i % 16);
  34576. }
  34577. }
  34578. #endif
  34579. static int test_wolfSSL_EVP_Cipher_extra(void)
  34580. {
  34581. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  34582. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  34583. /* aes128-cbc, keylen=16, ivlen=16 */
  34584. byte aes128_cbc_key[] = {
  34585. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  34586. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  34587. };
  34588. byte aes128_cbc_iv[] = {
  34589. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  34590. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  34591. };
  34592. /* teset data size table */
  34593. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  34594. int test_drive2[] = {8, 3, 8, 512, 0};
  34595. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  34596. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  34597. int test_drive_len[100];
  34598. int ret = 0;
  34599. EVP_CIPHER_CTX *evp = NULL;
  34600. int ilen = 0;
  34601. int klen = 0;
  34602. int i, j;
  34603. const EVP_CIPHER *type;
  34604. byte *iv;
  34605. byte *key;
  34606. int ivlen;
  34607. int keylen;
  34608. #define RECORDS 16
  34609. #define BUFFSZ 512
  34610. byte plain [BUFFSZ * RECORDS];
  34611. byte cipher[BUFFSZ * RECORDS];
  34612. byte inb[BUFFSZ];
  34613. byte outb[BUFFSZ+16];
  34614. int outl, inl;
  34615. iv = aes128_cbc_iv;
  34616. ivlen = sizeof(aes128_cbc_iv);
  34617. key = aes128_cbc_key;
  34618. keylen = sizeof(aes128_cbc_key);
  34619. type = EVP_aes_128_cbc();
  34620. set_plain(plain, BUFFSZ * RECORDS);
  34621. SSL_library_init();
  34622. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  34623. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  34624. klen = EVP_CIPHER_CTX_key_length(evp);
  34625. if (klen > 0 && keylen != klen) {
  34626. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  34627. }
  34628. ilen = EVP_CIPHER_CTX_iv_length(evp);
  34629. if (ilen > 0 && ivlen != ilen) {
  34630. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  34631. }
  34632. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  34633. for (j = 0; j<RECORDS; j++)
  34634. {
  34635. inl = BUFFSZ;
  34636. get_record(plain, inb, inl);
  34637. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  34638. set_record(cipher, outb, outl);
  34639. }
  34640. for (i = 0; test_drive[i]; i++) {
  34641. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  34642. init_offset();
  34643. test_drive_len[i] = 0;
  34644. for (j = 0; test_drive[i][j]; j++)
  34645. {
  34646. inl = test_drive[i][j];
  34647. test_drive_len[i] += inl;
  34648. get_record(plain, inb, inl);
  34649. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  34650. /* output to cipher buffer, so that following Dec test can detect
  34651. if any error */
  34652. set_record(cipher, outb, outl);
  34653. }
  34654. EVP_CipherFinal(evp, outb, &outl);
  34655. if(outl > 0)
  34656. set_record(cipher, outb, outl);
  34657. }
  34658. for (i = 0; test_drive[i]; i++) {
  34659. last_val = 0x0f;
  34660. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  34661. init_offset();
  34662. for (j = 0; test_drive[i][j]; j++){
  34663. inl = test_drive[i][j];
  34664. get_record(cipher, inb, inl);
  34665. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  34666. binary_dump(outb, outl);
  34667. AssertIntEQ((ret = check_result(outb, outl)), 0);
  34668. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  34669. }
  34670. ret = EVP_CipherFinal(evp, outb, &outl);
  34671. binary_dump(outb, outl);
  34672. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  34673. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  34674. AssertTrue(ret);
  34675. }
  34676. EVP_CIPHER_CTX_free(evp);
  34677. #endif /* test_EVP_Cipher */
  34678. return 0;
  34679. }
  34680. static void test_wolfSSL_PEM_read_DHparams(void)
  34681. {
  34682. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  34683. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  34684. DH* dh;
  34685. XFILE fp;
  34686. unsigned char derOut[300];
  34687. unsigned char* derOutBuf = derOut;
  34688. int derOutSz = 0;
  34689. unsigned char derExpected[300];
  34690. int derExpectedSz = 0;
  34691. printf(testingFmt, "wolfSSL_PEM_read_DHparams()");
  34692. XMEMSET(derOut, 0, sizeof(derOut));
  34693. XMEMSET(derExpected, 0, sizeof(derExpected));
  34694. /* open DH param file, read into DH struct */
  34695. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  34696. /* bad args */
  34697. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  34698. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  34699. /* good args */
  34700. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  34701. XFCLOSE(fp);
  34702. /* read in certs/dh2048.der for comparison against exported params */
  34703. fp = XFOPEN("./certs/dh2048.der", "rb");
  34704. AssertTrue(fp != XBADFILE);
  34705. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  34706. XFCLOSE(fp);
  34707. /* export DH back to DER and compare */
  34708. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  34709. AssertIntEQ(derOutSz, derExpectedSz);
  34710. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  34711. /* Test parsing with X9.42 header */
  34712. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  34713. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  34714. XFCLOSE(fp);
  34715. DH_free(dh);
  34716. printf(resultFmt, passed);
  34717. #endif
  34718. }
  34719. static void test_wolfSSL_AES_ecb_encrypt(void)
  34720. {
  34721. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  34722. AES_KEY aes;
  34723. const byte msg[] =
  34724. {
  34725. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  34726. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  34727. };
  34728. const byte verify[] =
  34729. {
  34730. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  34731. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  34732. };
  34733. const byte key[] =
  34734. {
  34735. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  34736. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  34737. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  34738. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  34739. };
  34740. byte out[AES_BLOCK_SIZE];
  34741. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  34742. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  34743. XMEMSET(out, 0, AES_BLOCK_SIZE);
  34744. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  34745. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  34746. #ifdef HAVE_AES_DECRYPT
  34747. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  34748. XMEMSET(out, 0, AES_BLOCK_SIZE);
  34749. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  34750. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  34751. #endif
  34752. /* test bad arguments */
  34753. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  34754. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  34755. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  34756. printf(resultFmt, passed);
  34757. #endif
  34758. }
  34759. static void test_wolfSSL_MD5(void)
  34760. {
  34761. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  34762. byte input1[] = "";
  34763. byte input2[] = "message digest";
  34764. byte hash[WC_MD5_DIGEST_SIZE];
  34765. unsigned char output1[] =
  34766. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  34767. unsigned char output2[] =
  34768. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  34769. WOLFSSL_MD5_CTX md5;
  34770. printf(testingFmt, "wolfSSL_MD5()");
  34771. XMEMSET(&md5, 0, sizeof(md5));
  34772. /* Test cases for illegal parameters */
  34773. AssertIntEQ(MD5_Init(NULL), 0);
  34774. AssertIntEQ(MD5_Init(&md5), 1);
  34775. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  34776. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  34777. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  34778. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  34779. AssertIntEQ(MD5_Final(hash, NULL), 0);
  34780. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  34781. /* Init MD5 CTX */
  34782. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  34783. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  34784. XSTRLEN((const char*)&input1)), 1);
  34785. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  34786. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  34787. /* Init MD5 CTX */
  34788. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  34789. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  34790. (int)XSTRLEN((const char*)input2)), 1);
  34791. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  34792. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  34793. #if !defined(NO_OLD_NAMES) && \
  34794. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  34795. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  34796. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  34797. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  34798. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  34799. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  34800. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  34801. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  34802. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  34803. {
  34804. byte data[] = "Data to be hashed.";
  34805. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  34806. AssertNotNull(MD5(data, sizeof(data), NULL));
  34807. AssertNotNull(MD5(data, sizeof(data), hash));
  34808. AssertNotNull(MD5(NULL, 0, hash));
  34809. AssertNull(MD5(NULL, sizeof(data), hash));
  34810. }
  34811. #endif
  34812. printf(resultFmt, passed);
  34813. #endif
  34814. }
  34815. static void test_wolfSSL_MD5_Transform(void)
  34816. {
  34817. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  34818. byte input1[] = "";
  34819. byte input2[] = "abc";
  34820. byte local[WC_MD5_BLOCK_SIZE];
  34821. word32 sLen = 0;
  34822. #ifdef BIG_ENDIAN_ORDER
  34823. unsigned char output1[] =
  34824. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  34825. unsigned char output2[] =
  34826. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  34827. #else
  34828. unsigned char output1[] =
  34829. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  34830. unsigned char output2[] =
  34831. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  34832. #endif
  34833. MD5_CTX md5;
  34834. printf(testingFmt, "wolfSSL_MD5_Transform()");
  34835. XMEMSET(&md5, 0, sizeof(md5));
  34836. XMEMSET(&local, 0, sizeof(local));
  34837. /* sanity check */
  34838. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  34839. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  34840. AssertIntEQ(MD5_Transform(&md5, NULL), 0);
  34841. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  34842. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  34843. AssertIntEQ(wc_Md5Transform((wc_Md5*)&md5, NULL), BAD_FUNC_ARG);
  34844. /* Init MD5 CTX */
  34845. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  34846. /* Do Transform*/
  34847. sLen = (word32)XSTRLEN((char*)input1);
  34848. XMEMCPY(local, input1, sLen);
  34849. AssertIntEQ(MD5_Transform(&md5, (const byte*)&local[0]), 1);
  34850. AssertIntEQ(XMEMCMP(&((wc_Md5*)&md5)->digest[0], output1,
  34851. WC_MD5_DIGEST_SIZE), 0);
  34852. /* Init MD5 CTX */
  34853. AssertIntEQ(MD5_Init(&md5), 1);
  34854. sLen = (word32)XSTRLEN((char*)input2);
  34855. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  34856. XMEMCPY(local, input2, sLen);
  34857. AssertIntEQ(MD5_Transform(&md5, (const byte*)&local[0]), 1);
  34858. AssertIntEQ(XMEMCMP(&((wc_Md5*)&md5)->digest[0], output2,
  34859. WC_MD5_DIGEST_SIZE), 0);
  34860. printf(resultFmt, passed);
  34861. #endif
  34862. }
  34863. static void test_wolfSSL_SHA224(void)
  34864. {
  34865. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  34866. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  34867. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  34868. unsigned char input[] =
  34869. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  34870. unsigned char output[] =
  34871. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  34872. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  34873. size_t inLen;
  34874. byte hash[WC_SHA224_DIGEST_SIZE];
  34875. printf(testingFmt, "wolfSSL_SHA224()");
  34876. inLen = XSTRLEN((char*)input);
  34877. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  34878. AssertNull(SHA224(NULL, inLen, hash));
  34879. AssertNotNull(SHA224(input, 0, hash));
  34880. AssertNotNull(SHA224(input, inLen, NULL));
  34881. AssertNotNull(SHA224(NULL, 0, hash));
  34882. AssertNotNull(SHA224(NULL, 0, NULL));
  34883. AssertNotNull(SHA224(input, inLen, hash));
  34884. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  34885. printf(resultFmt, passed);
  34886. #endif
  34887. }
  34888. static void test_wolfSSL_SHA_Transform(void)
  34889. {
  34890. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  34891. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  34892. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  34893. byte input1[] = "";
  34894. byte input2[] = "abc";
  34895. byte local[WC_SHA_BLOCK_SIZE];
  34896. word32 sLen = 0;
  34897. #ifdef BIG_ENDIAN_ORDER
  34898. unsigned char output1[] =
  34899. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  34900. "\x09\xf7\x3a\x93";
  34901. unsigned char output2[] =
  34902. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  34903. "\x7c\x6e\x08\xb8";
  34904. #else
  34905. unsigned char output1[] =
  34906. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  34907. "\x93\x3a\xf7\x09";
  34908. unsigned char output2[] =
  34909. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  34910. "\xb8\x08\x6e\x7c";
  34911. #endif
  34912. SHA_CTX sha;
  34913. SHA_CTX sha1;
  34914. printf(testingFmt, "wolfSSL_SHA_Transform()");
  34915. XMEMSET(&sha, 0, sizeof(sha));
  34916. XMEMSET(&local, 0, sizeof(local));
  34917. /* sanity check */
  34918. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  34919. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  34920. AssertIntEQ(SHA_Transform(&sha, NULL), 0);
  34921. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  34922. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  34923. AssertIntEQ(SHA1_Transform(&sha, NULL), 0);
  34924. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  34925. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  34926. AssertIntEQ(wc_ShaTransform((wc_Sha*)&sha, NULL), BAD_FUNC_ARG);
  34927. /* Init SHA CTX */
  34928. AssertIntEQ(SHA_Init(&sha), 1);
  34929. /* Do Transform*/
  34930. sLen = (word32)XSTRLEN((char*)input1);
  34931. XMEMCPY(local, input1, sLen);
  34932. AssertIntEQ(SHA_Transform(&sha, (const byte*)&local[0]), 1);
  34933. AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha)->digest[0], output1,
  34934. WC_SHA_DIGEST_SIZE), 0);
  34935. AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
  34936. /* Init SHA CTX */
  34937. AssertIntEQ(SHA_Init(&sha), 1);
  34938. sLen = (word32)XSTRLEN((char*)input2);
  34939. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  34940. XMEMCPY(local, input2, sLen);
  34941. AssertIntEQ(SHA_Transform(&sha, (const byte*)&local[0]), 1);
  34942. AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha)->digest[0], output2,
  34943. WC_SHA_DIGEST_SIZE), 0);
  34944. AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
  34945. /* SHA1 */
  34946. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  34947. /* Init SHA CTX */
  34948. AssertIntEQ(SHA1_Init(&sha1), 1);
  34949. /* Do Transform*/
  34950. sLen = (word32)XSTRLEN((char*)input1);
  34951. XMEMCPY(local, input1, sLen);
  34952. AssertIntEQ(SHA1_Transform(&sha1, (const byte*)&local[0]), 1);
  34953. AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha1)->digest[0], output1,
  34954. WC_SHA_DIGEST_SIZE), 0);
  34955. AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
  34956. /* Init SHA CTX */
  34957. AssertIntEQ(SHA1_Init(&sha1), 1);
  34958. sLen = (word32)XSTRLEN((char*)input2);
  34959. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  34960. XMEMCPY(local, input2, sLen);
  34961. AssertIntEQ(SHA1_Transform(&sha1, (const byte*)&local[0]), 1);
  34962. AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha1)->digest[0], output2,
  34963. WC_SHA_DIGEST_SIZE), 0);
  34964. AssertIntEQ(SHA_Final(local, &sha), 1); /* frees resources */
  34965. printf(resultFmt, passed);
  34966. #endif
  34967. #endif
  34968. }
  34969. static void test_wolfSSL_SHA256_Transform(void)
  34970. {
  34971. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  34972. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  34973. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  34974. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH)
  34975. byte input1[] = "";
  34976. byte input2[] = "abc";
  34977. byte local[WC_SHA256_BLOCK_SIZE];
  34978. word32 sLen = 0;
  34979. #ifdef BIG_ENDIAN_ORDER
  34980. unsigned char output1[] =
  34981. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  34982. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  34983. unsigned char output2[] =
  34984. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  34985. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  34986. #else
  34987. unsigned char output1[] =
  34988. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  34989. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  34990. unsigned char output2[] =
  34991. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  34992. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  34993. #endif
  34994. SHA256_CTX sha256;
  34995. printf(testingFmt, "wolfSSL_SHA256_Transform()");
  34996. XMEMSET(&sha256, 0, sizeof(sha256));
  34997. XMEMSET(&local, 0, sizeof(local));
  34998. /* sanity check */
  34999. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  35000. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  35001. AssertIntEQ(SHA256_Transform(&sha256, NULL), 0);
  35002. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  35003. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  35004. AssertIntEQ(wc_Sha256Transform((wc_Sha256*)&sha256, NULL), BAD_FUNC_ARG);
  35005. /* Init SHA256 CTX */
  35006. AssertIntEQ(SHA256_Init(&sha256), 1);
  35007. /* Do Transform*/
  35008. sLen = (word32)XSTRLEN((char*)input1);
  35009. XMEMCPY(local, input1, sLen);
  35010. AssertIntEQ(SHA256_Transform(&sha256, (const byte*)&local[0]), 1);
  35011. AssertIntEQ(XMEMCMP(&((wc_Sha256*)&sha256)->digest[0], output1,
  35012. WC_SHA256_DIGEST_SIZE), 0);
  35013. AssertIntEQ(SHA256_Final(local, &sha256), 1); /* frees resources */
  35014. /* Init SHA256 CTX */
  35015. AssertIntEQ(SHA256_Init(&sha256), 1);
  35016. sLen = (word32)XSTRLEN((char*)input2);
  35017. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  35018. XMEMCPY(local, input2, sLen);
  35019. AssertIntEQ(SHA256_Transform(&sha256, (const byte*)&local[0]), 1);
  35020. AssertIntEQ(XMEMCMP(&((wc_Sha256*)&sha256)->digest[0], output2,
  35021. WC_SHA256_DIGEST_SIZE), 0);
  35022. AssertIntEQ(SHA256_Final(local, &sha256), 1); /* frees resources */
  35023. printf(resultFmt, passed);
  35024. #endif
  35025. #endif
  35026. }
  35027. static void test_wolfSSL_SHA256(void)
  35028. {
  35029. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  35030. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  35031. unsigned char input[] =
  35032. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  35033. unsigned char output[] =
  35034. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  35035. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  35036. "\x06\xC1";
  35037. size_t inLen;
  35038. byte hash[WC_SHA256_DIGEST_SIZE];
  35039. printf(testingFmt, "wolfSSL_SHA256()");
  35040. inLen = XSTRLEN((char*)input);
  35041. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  35042. AssertNotNull(SHA256(input, inLen, hash));
  35043. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  35044. printf(resultFmt, passed);
  35045. #endif
  35046. }
  35047. static void test_wolfSSL_SHA512_Transform(void)
  35048. {
  35049. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  35050. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  35051. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  35052. byte input1[] = "";
  35053. byte input2[] = "abc";
  35054. byte local[WC_SHA512_BLOCK_SIZE];
  35055. word32 sLen = 0;
  35056. #ifdef BIG_ENDIAN_ORDER
  35057. unsigned char output1[] =
  35058. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  35059. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  35060. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  35061. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  35062. unsigned char output2[] =
  35063. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  35064. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  35065. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  35066. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  35067. #else
  35068. unsigned char output1[] =
  35069. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  35070. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  35071. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  35072. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  35073. unsigned char output2[] =
  35074. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  35075. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  35076. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  35077. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  35078. #endif
  35079. SHA512_CTX sha512;
  35080. printf(testingFmt, "wolfSSL_SHA512_Transform()");
  35081. XMEMSET(&sha512, 0, sizeof(sha512));
  35082. XMEMSET(&local, 0, sizeof(local));
  35083. /* sanity check */
  35084. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  35085. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  35086. AssertIntEQ(SHA512_Transform(&sha512, NULL), 0);
  35087. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  35088. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  35089. AssertIntEQ(wc_Sha512Transform((wc_Sha512*)&sha512, NULL), BAD_FUNC_ARG);
  35090. /* Init SHA512 CTX */
  35091. AssertIntEQ(wolfSSL_SHA512_Init(&sha512), 1);
  35092. /* Do Transform*/
  35093. sLen = (word32)XSTRLEN((char*)input1);
  35094. XMEMCPY(local, input1, sLen);
  35095. AssertIntEQ(SHA512_Transform(&sha512, (const byte*)&local[0]), 1);
  35096. AssertIntEQ(XMEMCMP(&((wc_Sha512*)&sha512)->digest[0], output1,
  35097. WC_SHA512_DIGEST_SIZE), 0);
  35098. AssertIntEQ(SHA512_Final(local, &sha512), 1); /* frees resources */
  35099. /* Init SHA512 CTX */
  35100. AssertIntEQ(SHA512_Init(&sha512), 1);
  35101. sLen = (word32)XSTRLEN((char*)input2);
  35102. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  35103. XMEMCPY(local, input2, sLen);
  35104. AssertIntEQ(SHA512_Transform(&sha512, (const byte*)&local[0]), 1);
  35105. AssertIntEQ(XMEMCMP(&((wc_Sha512*)&sha512)->digest[0], output2,
  35106. WC_SHA512_DIGEST_SIZE), 0);
  35107. AssertIntEQ(SHA512_Final(local, &sha512), 1); /* frees resources */
  35108. (void)input1;
  35109. printf(resultFmt, passed);
  35110. #endif
  35111. #endif
  35112. }
  35113. static void test_wolfSSL_X509_get_serialNumber(void)
  35114. {
  35115. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35116. ASN1_INTEGER* a;
  35117. BIGNUM* bn;
  35118. X509* x509;
  35119. char *serialHex;
  35120. byte serial[3];
  35121. int serialSz;
  35122. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  35123. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  35124. SSL_FILETYPE_PEM));
  35125. AssertNotNull(a = X509_get_serialNumber(x509));
  35126. /* check on value of ASN1 Integer */
  35127. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  35128. /* test setting serial number and then retrieving it */
  35129. AssertNotNull(a = ASN1_INTEGER_new());
  35130. ASN1_INTEGER_set(a, 3);
  35131. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  35132. serialSz = sizeof(serial);
  35133. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  35134. WOLFSSL_SUCCESS);
  35135. AssertIntEQ(serialSz, 1);
  35136. AssertIntEQ(serial[0], 3);
  35137. ASN1_INTEGER_free(a);
  35138. /* test setting serial number with 0's in it */
  35139. serial[0] = 0x01;
  35140. serial[1] = 0x00;
  35141. serial[2] = 0x02;
  35142. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  35143. a->data[0] = ASN_INTEGER;
  35144. a->data[1] = sizeof(serial);
  35145. XMEMCPY(&a->data[2], serial, sizeof(serial));
  35146. a->length = sizeof(serial) + 2;
  35147. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  35148. XMEMSET(serial, 0, sizeof(serial));
  35149. serialSz = sizeof(serial);
  35150. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  35151. WOLFSSL_SUCCESS);
  35152. AssertIntEQ(serialSz, 3);
  35153. AssertIntEQ(serial[0], 0x01);
  35154. AssertIntEQ(serial[1], 0x00);
  35155. AssertIntEQ(serial[2], 0x02);
  35156. ASN1_INTEGER_free(a);
  35157. X509_free(x509); /* free's a */
  35158. AssertNotNull(serialHex = BN_bn2hex(bn));
  35159. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  35160. AssertStrEQ(serialHex, "01");
  35161. #else
  35162. AssertStrEQ(serialHex, "1");
  35163. #endif
  35164. OPENSSL_free(serialHex);
  35165. AssertIntEQ(BN_get_word(bn), 1);
  35166. BN_free(bn);
  35167. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  35168. a = ASN1_INTEGER_new();
  35169. if (a) {
  35170. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  35171. DYNAMIC_TYPE_OPENSSL));
  35172. a->isDynamic = 1;
  35173. ASN1_INTEGER_free(a);
  35174. }
  35175. printf(resultFmt, passed);
  35176. #endif
  35177. }
  35178. static void test_wolfSSL_OpenSSL_add_all_algorithms(void){
  35179. #if defined(OPENSSL_EXTRA)
  35180. printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
  35181. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  35182. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  35183. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  35184. printf(resultFmt, passed);
  35185. #endif
  35186. }
  35187. static void test_wolfSSL_OPENSSL_hexstr2buf(void)
  35188. {
  35189. #if defined(OPENSSL_EXTRA)
  35190. #define MAX_HEXSTR_BUFSZ 9
  35191. #define NUM_CASES 5
  35192. struct Output {
  35193. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  35194. long ret;
  35195. };
  35196. int i;
  35197. int j;
  35198. const char* inputs[NUM_CASES] = {
  35199. "aabcd1357e",
  35200. "01:12:23:34:a5:b6:c7:d8:e9",
  35201. ":01:02",
  35202. "012",
  35203. ":ab:ac:d"
  35204. };
  35205. struct Output expectedOutputs[NUM_CASES] = {
  35206. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  35207. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  35208. {{0x01, 0x02}, 2},
  35209. {{0x00}, 0},
  35210. {{0x00}, 0}
  35211. };
  35212. long len = 0;
  35213. unsigned char* returnedBuf = NULL;
  35214. printf(testingFmt, "test_wolfSSL_OPENSSL_hexstr2buf()");
  35215. for (i = 0; i < NUM_CASES; ++i) {
  35216. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  35217. if (returnedBuf == NULL) {
  35218. AssertIntEQ(expectedOutputs[i].ret, 0);
  35219. continue;
  35220. }
  35221. AssertIntEQ(expectedOutputs[i].ret, len);
  35222. for (j = 0; j < len; ++j) {
  35223. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  35224. }
  35225. OPENSSL_free(returnedBuf);
  35226. }
  35227. printf(resultFmt, passed);
  35228. #endif
  35229. }
  35230. static void test_wolfSSL_ASN1_STRING_print_ex(void){
  35231. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  35232. #ifndef NO_BIO
  35233. ASN1_STRING* asn_str;
  35234. const char data[] = "Hello wolfSSL!";
  35235. ASN1_STRING* esc_str;
  35236. const char esc_data[] = "a+;<>";
  35237. BIO *bio;
  35238. unsigned long flags;
  35239. int p_len;
  35240. unsigned char rbuf[255];
  35241. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  35242. /* setup */
  35243. XMEMSET(rbuf, 0, 255);
  35244. bio = BIO_new(BIO_s_mem());
  35245. BIO_set_write_buf_size(bio,255);
  35246. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  35247. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  35248. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  35249. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  35250. /* no flags */
  35251. XMEMSET(rbuf, 0, 255);
  35252. flags = 0;
  35253. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  35254. AssertIntEQ(p_len, 15);
  35255. BIO_read(bio, (void*)rbuf, 15);
  35256. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  35257. /* RFC2253 Escape */
  35258. XMEMSET(rbuf, 0, 255);
  35259. flags = ASN1_STRFLGS_ESC_2253;
  35260. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  35261. AssertIntEQ(p_len, 9);
  35262. BIO_read(bio, (void*)rbuf, 9);
  35263. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  35264. /* Show type */
  35265. XMEMSET(rbuf, 0, 255);
  35266. flags = ASN1_STRFLGS_SHOW_TYPE;
  35267. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  35268. AssertIntEQ(p_len, 28);
  35269. BIO_read(bio, (void*)rbuf, 28);
  35270. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  35271. /* Dump All */
  35272. XMEMSET(rbuf, 0, 255);
  35273. flags = ASN1_STRFLGS_DUMP_ALL;
  35274. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  35275. AssertIntEQ(p_len, 31);
  35276. BIO_read(bio, (void*)rbuf, 31);
  35277. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  35278. /* Dump Der */
  35279. XMEMSET(rbuf, 0, 255);
  35280. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  35281. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  35282. AssertIntEQ(p_len, 35);
  35283. BIO_read(bio, (void*)rbuf, 35);
  35284. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  35285. /* Dump All + Show type */
  35286. XMEMSET(rbuf, 0, 255);
  35287. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  35288. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  35289. AssertIntEQ(p_len, 44);
  35290. BIO_read(bio, (void*)rbuf, 44);
  35291. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  35292. BIO_free(bio);
  35293. ASN1_STRING_free(asn_str);
  35294. ASN1_STRING_free(esc_str);
  35295. printf(resultFmt, passed);
  35296. #endif /* !NO_BIO */
  35297. #endif
  35298. }
  35299. static void test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  35300. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  35301. WOLFSSL_ASN1_TIME *t;
  35302. WOLFSSL_ASN1_TIME *out;
  35303. WOLFSSL_ASN1_TIME *gtime;
  35304. int tlen = 0;
  35305. unsigned char *data;
  35306. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  35307. /* UTC Time test */
  35308. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  35309. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  35310. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  35311. t->type = ASN_UTC_TIME;
  35312. t->length = ASN_UTC_TIME_SIZE;
  35313. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  35314. tlen = wolfSSL_ASN1_TIME_get_length(t);
  35315. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  35316. data = wolfSSL_ASN1_TIME_get_data(t);
  35317. AssertStrEQ((char*)data, "050727123456Z");
  35318. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  35319. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  35320. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  35321. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  35322. /* Generalized Time test */
  35323. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  35324. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  35325. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  35326. t->type = ASN_GENERALIZED_TIME;
  35327. t->length = ASN_GENERALIZED_TIME_SIZE;
  35328. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  35329. tlen = wolfSSL_ASN1_TIME_get_length(t);
  35330. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  35331. data = wolfSSL_ASN1_TIME_get_data(t);
  35332. AssertStrEQ((char*)data, "20050727123456Z");
  35333. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  35334. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  35335. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  35336. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  35337. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35338. /* Null parameter test */
  35339. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  35340. gtime = NULL;
  35341. out = NULL;
  35342. t->type = ASN_UTC_TIME;
  35343. t->length = ASN_UTC_TIME_SIZE;
  35344. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  35345. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  35346. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  35347. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  35348. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  35349. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35350. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35351. printf(resultFmt, passed);
  35352. #endif
  35353. }
  35354. static void test_wolfSSL_X509_CA_num(void){
  35355. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35356. defined(HAVE_ECC) && !defined(NO_RSA)
  35357. WOLFSSL_X509_STORE *store;
  35358. WOLFSSL_X509 *x509_1, *x509_2;
  35359. int ca_num = 0;
  35360. printf(testingFmt, "wolfSSL_X509_CA_num()");
  35361. store = wolfSSL_X509_STORE_new();
  35362. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  35363. wolfSSL_X509_STORE_add_cert(store, x509_1);
  35364. ca_num = wolfSSL_X509_CA_num(store);
  35365. AssertIntEQ(ca_num, 1);
  35366. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  35367. wolfSSL_X509_STORE_add_cert(store, x509_2);
  35368. ca_num = wolfSSL_X509_CA_num(store);
  35369. AssertIntEQ(ca_num, 2);
  35370. wolfSSL_X509_free(x509_1);
  35371. wolfSSL_X509_free(x509_2);
  35372. wolfSSL_X509_STORE_free(store);
  35373. printf(resultFmt, passed);
  35374. #endif
  35375. }
  35376. static void test_wolfSSL_X509_check_ca(void){
  35377. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  35378. WOLFSSL_X509 *x509;
  35379. printf(testingFmt, "wolfSSL_X509_check_ca()");
  35380. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  35381. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  35382. wolfSSL_X509_free(x509);
  35383. printf(resultFmt, passed);
  35384. #endif
  35385. }
  35386. static void test_wolfSSL_X509_check_ip_asc(void){
  35387. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  35388. WOLFSSL_X509 *x509;
  35389. printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
  35390. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  35391. #if 0
  35392. /* TODO: add cert gen for testing positive case */
  35393. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  35394. #endif
  35395. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  35396. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  35397. wolfSSL_X509_free(x509);
  35398. printf(resultFmt, passed);
  35399. #endif
  35400. }
  35401. static void test_wolfSSL_DC_cert(void)
  35402. {
  35403. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  35404. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_KEY_GEN) && \
  35405. defined(WOLFSSL_CERT_EXT)
  35406. Cert cert;
  35407. RsaKey key;
  35408. WC_RNG rng;
  35409. byte der[FOURK_BUF];
  35410. int certSz;
  35411. int ret, idx;
  35412. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  35413. const unsigned char* pt;
  35414. X509* x509;
  35415. X509_NAME* x509name;
  35416. X509_NAME_ENTRY* entry;
  35417. ASN1_STRING* entryValue;
  35418. CertName name;
  35419. printf(testingFmt, "wolfSSL Certs with DC");
  35420. XMEMSET(&name, 0, sizeof(CertName));
  35421. /* set up cert name */
  35422. XMEMCPY(name.country, "US", sizeof("US"));
  35423. name.countryEnc = CTC_PRINTABLE;
  35424. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  35425. name.stateEnc = CTC_UTF8;
  35426. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  35427. name.localityEnc = CTC_UTF8;
  35428. XMEMCPY(name.sur, "Test", sizeof("Test"));
  35429. name.surEnc = CTC_UTF8;
  35430. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  35431. name.orgEnc = CTC_UTF8;
  35432. XMEMCPY(name.unit, "Development", sizeof("Development"));
  35433. name.unitEnc = CTC_UTF8;
  35434. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  35435. name.commonNameEnc = CTC_UTF8;
  35436. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  35437. name.serialDevEnc = CTC_PRINTABLE;
  35438. #ifdef WOLFSSL_MULTI_ATTRIB
  35439. #if CTC_MAX_ATTRIB > 2
  35440. {
  35441. NameAttrib* n;
  35442. n = &name.name[0];
  35443. n->id = ASN_DOMAIN_COMPONENT;
  35444. n->type = CTC_UTF8;
  35445. n->sz = sizeof("com");
  35446. XMEMCPY(n->value, "com", sizeof("com"));
  35447. n = &name.name[1];
  35448. n->id = ASN_DOMAIN_COMPONENT;
  35449. n->type = CTC_UTF8;
  35450. n->sz = sizeof("wolfssl");
  35451. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  35452. }
  35453. #endif
  35454. #endif /* WOLFSSL_MULTI_ATTRIB */
  35455. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  35456. #ifndef HAVE_FIPS
  35457. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, devId), 0);
  35458. #else
  35459. AssertIntEQ(wc_InitRng(&rng), 0);
  35460. #endif
  35461. AssertIntEQ(wc_MakeRsaKey(&key, 2048, 3, &rng), 0);
  35462. XMEMSET(&cert, 0 , sizeof(Cert));
  35463. AssertIntEQ(wc_InitCert(&cert), 0);
  35464. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  35465. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  35466. cert.serialSz = (int)sizeof(mySerial);
  35467. cert.isCA = 1;
  35468. #ifndef NO_SHA256
  35469. cert.sigType = CTC_SHA256wRSA;
  35470. #else
  35471. cert.sigType = CTC_SHAwRSA;
  35472. #endif
  35473. /* add SKID from the Public Key */
  35474. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  35475. /* add AKID from the Public Key */
  35476. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  35477. ret = 0;
  35478. do {
  35479. #if defined(WOLFSSL_ASYNC_CRYPT)
  35480. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  35481. #endif
  35482. if (ret >= 0) {
  35483. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  35484. }
  35485. } while (ret == WC_PENDING_E);
  35486. AssertIntGT(ret, 0);
  35487. certSz = ret;
  35488. /* der holds a certificate with DC's now check X509 parsing of it */
  35489. pt = der;
  35490. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  35491. AssertNotNull(x509name = X509_get_subject_name(x509));
  35492. #ifdef WOLFSSL_MULTI_ATTRIB
  35493. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  35494. -1)), 5);
  35495. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  35496. idx)), 6);
  35497. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  35498. idx)), -1);
  35499. #endif /* WOLFSSL_MULTI_ATTRIB */
  35500. /* compare DN at index 0 */
  35501. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  35502. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  35503. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  35504. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  35505. #ifdef WOLFSSL_MULTI_ATTRIB
  35506. /* get first and second DC and compare result */
  35507. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  35508. -1)), 5);
  35509. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  35510. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  35511. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  35512. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  35513. idx)), 6);
  35514. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  35515. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  35516. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  35517. #endif /* WOLFSSL_MULTI_ATTRIB */
  35518. /* try invalid index locations for regression test and sanity check */
  35519. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  35520. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  35521. (void)idx;
  35522. X509_free(x509);
  35523. wc_FreeRsaKey(&key);
  35524. wc_FreeRng(&rng);
  35525. printf(resultFmt, passed);
  35526. #endif
  35527. }
  35528. static void test_wolfSSL_X509_get_version(void){
  35529. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35530. WOLFSSL_X509 *x509;
  35531. printf(testingFmt, "wolfSSL_X509_get_version()");
  35532. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  35533. AssertNotNull(x509);
  35534. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  35535. wolfSSL_X509_free(x509);
  35536. printf(resultFmt, passed);
  35537. #endif
  35538. }
  35539. static void test_wolfSSL_DES_ncbc(void){
  35540. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  35541. const_DES_cblock myDes;
  35542. DES_cblock iv = {1};
  35543. DES_key_schedule key = {0};
  35544. unsigned char msg[] = "hello wolfssl";
  35545. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  35546. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  35547. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  35548. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  35549. printf(testingFmt, "wolfSSL_DES_ncbc()");
  35550. /* partial block test */
  35551. DES_set_key(&key, &myDes);
  35552. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  35553. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  35554. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  35555. DES_set_key(&key, &myDes);
  35556. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  35557. *((byte*)&iv) = 1;
  35558. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  35559. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  35560. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  35561. /* full block test */
  35562. DES_set_key(&key, &myDes);
  35563. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  35564. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  35565. *((byte*)&iv) = 1;
  35566. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  35567. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  35568. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  35569. DES_set_key(&key, &myDes);
  35570. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  35571. *((byte*)&iv) = 1;
  35572. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  35573. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  35574. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  35575. printf(resultFmt, passed);
  35576. #endif
  35577. }
  35578. static void test_wolfSSL_AES_cbc_encrypt(void)
  35579. {
  35580. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  35581. AES_KEY aes;
  35582. AES_KEY* aesN = NULL;
  35583. size_t len = 0;
  35584. size_t lenB = 0;
  35585. int keySz0 = 0;
  35586. int keySzN = -1;
  35587. byte out[AES_BLOCK_SIZE] = {0};
  35588. byte* outN = NULL;
  35589. const int enc1 = AES_ENCRYPT;
  35590. const int enc2 = AES_DECRYPT;
  35591. /* Test vectors retrieved from:
  35592. * <begin URL>
  35593. * https://csrc.nist.gov/
  35594. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  35595. * documents/aes/KAT_AES.zip
  35596. * </end URL>
  35597. */
  35598. const byte* pt128N = NULL;
  35599. byte* key128N = NULL;
  35600. byte* iv128N = NULL;
  35601. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  35602. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  35603. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  35604. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  35605. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  35606. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  35607. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  35608. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  35609. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  35610. len = sizeof(pt128);
  35611. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  35612. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  35613. AssertIntNE(XMEMCMP(b, g, h), i)
  35614. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  35615. printf(testingFmt, "Stressing wolfSSL_AES_cbc_encrypt()");
  35616. STRESS_T(pt128N, out, len, &aes, iv128tmp, enc1, ct128, AES_BLOCK_SIZE, 0);
  35617. STRESS_T(pt128, out, len, &aes, iv128N, enc1, ct128, AES_BLOCK_SIZE, 0);
  35618. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, enc1);
  35619. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  35620. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, enc1);
  35621. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  35622. STRESS_T(pt128, out, lenB, &aes, iv128tmp, enc1, ct128, AES_BLOCK_SIZE, 0);
  35623. printf(resultFmt, "Stress Tests: passed");
  35624. printf(testingFmt, "Stressing wolfSSL_AES_set_encrypt_key");
  35625. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  35626. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  35627. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  35628. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  35629. printf(resultFmt, "Stress Tests: passed");
  35630. printf(testingFmt, "Stressing wolfSSL_AES_set_decrypt_key");
  35631. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  35632. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  35633. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  35634. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  35635. printf(resultFmt, "Stress Tests: passed");
  35636. #ifdef WOLFSSL_AES_128
  35637. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit");
  35638. XMEMSET(out, 0, AES_BLOCK_SIZE);
  35639. RESET_IV(iv128tmp, iv128);
  35640. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  35641. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, enc1);
  35642. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  35643. printf(resultFmt, "passed");
  35644. #ifdef HAVE_AES_DECRYPT
  35645. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode");
  35646. XMEMSET(out, 0, AES_BLOCK_SIZE);
  35647. RESET_IV(iv128tmp, iv128);
  35648. len = sizeof(ct128);
  35649. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  35650. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, enc2);
  35651. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  35652. printf(resultFmt, "passed");
  35653. #endif
  35654. #endif /* WOLFSSL_AES_128 */
  35655. #ifdef WOLFSSL_AES_192
  35656. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  35657. * Appendix F.2.3 */
  35658. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  35659. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  35660. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  35661. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  35662. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  35663. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  35664. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  35665. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  35666. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  35667. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  35668. len = sizeof(pt192);
  35669. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit");
  35670. XMEMSET(out, 0, AES_BLOCK_SIZE);
  35671. RESET_IV(iv192tmp, iv192);
  35672. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  35673. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, enc1);
  35674. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  35675. printf(resultFmt, "passed");
  35676. #ifdef HAVE_AES_DECRYPT
  35677. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode");
  35678. len = sizeof(ct192);
  35679. RESET_IV(iv192tmp, iv192);
  35680. XMEMSET(out, 0, AES_BLOCK_SIZE);
  35681. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  35682. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, enc2);
  35683. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  35684. printf(resultFmt, "passed");
  35685. #endif
  35686. #endif /* WOLFSSL_AES_192 */
  35687. #ifdef WOLFSSL_AES_256
  35688. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  35689. * Appendix F.2.5 */
  35690. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  35691. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  35692. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  35693. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  35694. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  35695. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  35696. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  35697. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  35698. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  35699. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  35700. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  35701. len = sizeof(pt256);
  35702. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit");
  35703. XMEMSET(out, 0, AES_BLOCK_SIZE);
  35704. RESET_IV(iv256tmp, iv256);
  35705. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  35706. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, enc1);
  35707. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  35708. printf(resultFmt, "passed");
  35709. #ifdef HAVE_AES_DECRYPT
  35710. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode");
  35711. len = sizeof(ct256);
  35712. RESET_IV(iv256tmp, iv256);
  35713. XMEMSET(out, 0, AES_BLOCK_SIZE);
  35714. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  35715. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, enc2);
  35716. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  35717. printf(resultFmt, "passed");
  35718. #endif
  35719. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  35720. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  35721. byte wrapPlain[sizeof(key256)] = { 0 };
  35722. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  35723. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit NULL iv");
  35724. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  35725. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  35726. 15), WOLFSSL_FAILURE);
  35727. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  35728. sizeof(key256)), sizeof(wrapCipher));
  35729. printf(resultFmt, "passed");
  35730. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit NULL iv");
  35731. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  35732. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  35733. 23), WOLFSSL_FAILURE);
  35734. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  35735. sizeof(wrapCipher)), sizeof(wrapPlain));
  35736. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  35737. printf(resultFmt, "passed");
  35738. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  35739. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  35740. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit custom iv");
  35741. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  35742. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  35743. sizeof(key256)), sizeof(wrapCipher));
  35744. printf(resultFmt, "passed");
  35745. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit custom iv");
  35746. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  35747. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  35748. sizeof(wrapCipher)), sizeof(wrapPlain));
  35749. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  35750. printf(resultFmt, "passed");
  35751. #endif /* HAVE_AES_KEYWRAP */
  35752. #endif /* WOLFSSL_AES_256 */
  35753. #endif
  35754. }
  35755. #if defined(OPENSSL_ALL)
  35756. #if !defined(NO_ASN)
  35757. static void test_wolfSSL_ASN1_STRING_to_UTF8(void)
  35758. {
  35759. #if !defined(NO_RSA)
  35760. WOLFSSL_X509* x509;
  35761. WOLFSSL_X509_NAME* subject;
  35762. WOLFSSL_X509_NAME_ENTRY* e;
  35763. WOLFSSL_ASN1_STRING* a;
  35764. FILE* file;
  35765. int idx = 0;
  35766. char targetOutput[16] = "www.wolfssl.com";
  35767. unsigned char* actual_output;
  35768. int len = 0;
  35769. int result = 0;
  35770. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  35771. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  35772. fclose(file);
  35773. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName");
  35774. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  35775. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  35776. NID_commonName, -1)), 5);
  35777. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  35778. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  35779. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  35780. result = strncmp((const char*)actual_output, targetOutput, len);
  35781. AssertIntEQ(result, 0);
  35782. printf(resultFmt, result == 0 ? passed : failed);
  35783. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
  35784. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  35785. WOLFSSL_FATAL_ERROR);
  35786. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  35787. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
  35788. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  35789. WOLFSSL_FATAL_ERROR);
  35790. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  35791. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
  35792. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  35793. WOLFSSL_FATAL_ERROR);
  35794. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  35795. wolfSSL_X509_free(x509);
  35796. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35797. #endif
  35798. }
  35799. static void test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  35800. {
  35801. ASN1_STRING* asn1str_test;
  35802. ASN1_STRING* asn1str_answer;
  35803. /* Each character is encoded using 4 bytes */
  35804. char input[] = {
  35805. 0, 0, 0, 'T',
  35806. 0, 0, 0, 'e',
  35807. 0, 0, 0, 's',
  35808. 0, 0, 0, 't',
  35809. };
  35810. char output[] = "Test";
  35811. printf(testingFmt, "test_wolfSSL_ASN1_UNIVERSALSTRING_to_string()");
  35812. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  35813. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  35814. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  35815. AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  35816. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  35817. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  35818. ASN1_STRING_free(asn1str_test);
  35819. ASN1_STRING_free(asn1str_answer);
  35820. printf(resultFmt, "passed");
  35821. }
  35822. #endif /* !defined(NO_ASN) */
  35823. static void test_wolfSSL_sk_CIPHER_description(void)
  35824. {
  35825. #if !defined(NO_RSA)
  35826. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  35827. int i,j,k;
  35828. int numCiphers = 0;
  35829. const SSL_METHOD *method = NULL;
  35830. const SSL_CIPHER *cipher = NULL;
  35831. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  35832. SSL_CTX *ctx = NULL;
  35833. SSL *ssl = NULL;
  35834. char buf[256];
  35835. char test_str[9] = "0000000";
  35836. const char badStr[] = "unknown";
  35837. const char certPath[] = "./certs/client-cert.pem";
  35838. XMEMSET(buf, 0, sizeof(buf));
  35839. printf(testingFmt, "wolfSSL_sk_CIPHER_description");
  35840. AssertNotNull(method = TLSv1_2_client_method());
  35841. AssertNotNull(ctx = SSL_CTX_new(method));
  35842. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  35843. SSL_CTX_set_verify_depth(ctx, 4);
  35844. SSL_CTX_set_options(ctx, flags);
  35845. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  35846. WOLFSSL_SUCCESS);
  35847. AssertNotNull(ssl = SSL_new(ctx));
  35848. /* SSL_get_ciphers returns a stack of all configured ciphers
  35849. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  35850. */
  35851. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  35852. /* loop through the amount of supportedCiphers */
  35853. numCiphers = sk_num(supportedCiphers);
  35854. for (i = 0; i < numCiphers; ++i) {
  35855. /* sk_value increments "sk->data.cipher->cipherOffset".
  35856. * wolfSSL_sk_CIPHER_description sets the description for
  35857. * the cipher based on the provided offset.
  35858. */
  35859. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  35860. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  35861. }
  35862. /* Search cipher description string for "unknown" descriptor */
  35863. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  35864. k = 0;
  35865. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  35866. test_str[k] = badStr[k];
  35867. j++;
  35868. k++;
  35869. }
  35870. }
  35871. /* Fail if test_str == badStr == "unknown" */
  35872. AssertStrNE(test_str,badStr);
  35873. }
  35874. SSL_free(ssl);
  35875. SSL_CTX_free(ctx);
  35876. printf(resultFmt, passed);
  35877. #endif
  35878. }
  35879. static void test_wolfSSL_get_ciphers_compat(void)
  35880. {
  35881. #if !defined(NO_RSA)
  35882. const SSL_METHOD *method = NULL;
  35883. const char certPath[] = "./certs/client-cert.pem";
  35884. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  35885. SSL_CTX *ctx = NULL;
  35886. WOLFSSL *ssl = NULL;
  35887. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  35888. printf(testingFmt, "wolfSSL_get_ciphers_compat");
  35889. method = SSLv23_client_method();
  35890. AssertNotNull(method);
  35891. ctx = SSL_CTX_new(method);
  35892. AssertNotNull(ctx);
  35893. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  35894. SSL_CTX_set_verify_depth(ctx, 4);
  35895. SSL_CTX_set_options(ctx, flags);
  35896. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  35897. WOLFSSL_SUCCESS);
  35898. AssertNotNull(ssl = SSL_new(ctx));
  35899. /* Test Bad NULL input */
  35900. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  35901. /* Test for Good input */
  35902. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  35903. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  35904. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  35905. SSL_free(ssl);
  35906. SSL_CTX_free(ctx);
  35907. printf(resultFmt, passed);
  35908. #endif
  35909. }
  35910. static void test_wolfSSL_X509_PUBKEY_get(void)
  35911. {
  35912. WOLFSSL_X509_PUBKEY pubkey;
  35913. WOLFSSL_X509_PUBKEY* key;
  35914. WOLFSSL_EVP_PKEY evpkey ;
  35915. WOLFSSL_EVP_PKEY* evpPkey;
  35916. WOLFSSL_EVP_PKEY* retEvpPkey;
  35917. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  35918. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  35919. key = &pubkey;
  35920. evpPkey = &evpkey;
  35921. evpPkey->type = WOLFSSL_SUCCESS;
  35922. key->pkey = evpPkey;
  35923. printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
  35924. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  35925. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  35926. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  35927. key->pkey = NULL;
  35928. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  35929. printf(resultFmt,retEvpPkey == NULL ? passed : failed);
  35930. }
  35931. static void test_wolfSSL_d2i_DHparams(void)
  35932. {
  35933. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  35934. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  35935. FILE* f = NULL;
  35936. unsigned char buf[4096];
  35937. const unsigned char* pt = buf;
  35938. #ifdef HAVE_FFDHE_2048
  35939. const char* params1 = "./certs/dh2048.der";
  35940. #endif
  35941. #ifdef HAVE_FFDHE_3072
  35942. const char* params2 = "./certs/dh3072.der";
  35943. #endif
  35944. long len = 0;
  35945. WOLFSSL_DH* dh = NULL;
  35946. XMEMSET(buf, 0, sizeof(buf));
  35947. /* Test 2048 bit parameters */
  35948. #ifdef HAVE_FFDHE_2048
  35949. printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
  35950. f = XFOPEN(params1, "rb");
  35951. AssertTrue(f != XBADFILE);
  35952. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  35953. XFCLOSE(f);
  35954. /* Valid case */
  35955. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  35956. AssertNotNull(dh->p);
  35957. AssertNotNull(dh->g);
  35958. AssertTrue(pt != buf);
  35959. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  35960. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), WOLFSSL_SUCCESS);
  35961. #endif
  35962. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  35963. /* Invalid cases */
  35964. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  35965. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  35966. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  35967. DH_free(dh);
  35968. printf(resultFmt, passed);
  35969. *buf = 0;
  35970. pt = buf;
  35971. #endif /* HAVE_FFDHE_2048 */
  35972. /* Test 3072 bit parameters */
  35973. #ifdef HAVE_FFDHE_3072
  35974. printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
  35975. f = XFOPEN(params2, "rb");
  35976. AssertTrue(f != XBADFILE);
  35977. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  35978. XFCLOSE(f);
  35979. /* Valid case */
  35980. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  35981. AssertNotNull(dh->p);
  35982. AssertNotNull(dh->g);
  35983. AssertTrue(pt != buf);
  35984. AssertIntEQ(DH_generate_key(dh), 1);
  35985. /* Invalid cases */
  35986. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  35987. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  35988. DH_free(dh);
  35989. printf(resultFmt, passed);
  35990. #endif /* HAVE_FFDHE_3072 */
  35991. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  35992. #endif /* !NO_DH */
  35993. }
  35994. static void test_wolfSSL_i2d_DHparams(void)
  35995. {
  35996. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  35997. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  35998. FILE* f;
  35999. unsigned char buf[4096];
  36000. const unsigned char* pt = buf;
  36001. unsigned char* pt2 = buf;
  36002. #ifdef HAVE_FFDHE_2048
  36003. const char* params1 = "./certs/dh2048.der";
  36004. #endif
  36005. #ifdef HAVE_FFDHE_3072
  36006. const char* params2 = "./certs/dh3072.der";
  36007. #endif
  36008. long len;
  36009. WOLFSSL_DH* dh;
  36010. /* Test 2048 bit parameters */
  36011. #ifdef HAVE_FFDHE_2048
  36012. printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
  36013. f = XFOPEN(params1, "rb");
  36014. AssertTrue(f != XBADFILE);
  36015. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  36016. XFCLOSE(f);
  36017. /* Valid case */
  36018. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  36019. AssertTrue(pt != buf);
  36020. AssertIntEQ(DH_generate_key(dh), 1);
  36021. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  36022. /* Invalid case */
  36023. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  36024. /* Return length only */
  36025. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  36026. DH_free(dh);
  36027. printf(resultFmt, passed);
  36028. *buf = 0;
  36029. pt = buf;
  36030. pt2 = buf;
  36031. #endif
  36032. /* Test 3072 bit parameters */
  36033. #ifdef HAVE_FFDHE_3072
  36034. printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
  36035. f = XFOPEN(params2, "rb");
  36036. AssertTrue(f != XBADFILE);
  36037. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  36038. XFCLOSE(f);
  36039. /* Valid case */
  36040. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  36041. AssertTrue(pt != buf);
  36042. AssertIntEQ(DH_generate_key(dh), 1);
  36043. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  36044. /* Invalid case */
  36045. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  36046. /* Return length only */
  36047. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  36048. DH_free(dh);
  36049. printf(resultFmt, passed);
  36050. #endif
  36051. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36052. #endif
  36053. }
  36054. static void test_wolfSSL_EC_KEY_dup(void)
  36055. {
  36056. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  36057. defined(OPENSSL_EXTRA_X509_SMALL))
  36058. WOLFSSL_EC_KEY* ecKey;
  36059. WOLFSSL_EC_KEY* dupKey;
  36060. ecc_key* srcKey;
  36061. ecc_key* destKey;
  36062. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  36063. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  36064. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  36065. /* Valid cases */
  36066. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  36067. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  36068. /* Compare pubkey */
  36069. srcKey = (ecc_key*)ecKey->internal;
  36070. destKey = (ecc_key*)dupKey->internal;
  36071. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  36072. /* compare EC_GROUP */
  36073. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  36074. /* compare EC_POINT */
  36075. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  36076. dupKey->pub_key, NULL), MP_EQ);
  36077. /* compare BIGNUM */
  36078. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  36079. wolfSSL_EC_KEY_free(dupKey);
  36080. /* Invalid cases */
  36081. /* NULL key */
  36082. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  36083. /* NULL ecc_key */
  36084. wc_ecc_free((ecc_key*)ecKey->internal);
  36085. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  36086. ecKey->internal = NULL; /* Set ecc_key to NULL */
  36087. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  36088. wolfSSL_EC_KEY_free(ecKey);
  36089. wolfSSL_EC_KEY_free(dupKey);
  36090. /* NULL Group */
  36091. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  36092. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  36093. wolfSSL_EC_GROUP_free(ecKey->group);
  36094. ecKey->group = NULL; /* Set group to NULL */
  36095. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  36096. wolfSSL_EC_KEY_free(ecKey);
  36097. wolfSSL_EC_KEY_free(dupKey);
  36098. /* NULL public key */
  36099. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  36100. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  36101. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  36102. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  36103. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  36104. wolfSSL_EC_POINT_free(ecKey->pub_key);
  36105. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  36106. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  36107. wolfSSL_EC_KEY_free(ecKey);
  36108. wolfSSL_EC_KEY_free(dupKey);
  36109. /* NULL private key */
  36110. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  36111. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  36112. wolfSSL_BN_free(ecKey->priv_key);
  36113. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  36114. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  36115. wolfSSL_EC_KEY_free(ecKey);
  36116. wolfSSL_EC_KEY_free(dupKey);
  36117. printf(resultFmt, passed);
  36118. #endif
  36119. }
  36120. static void test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  36121. {
  36122. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  36123. DSA *dsa = NULL;
  36124. DSA *setDsa = NULL;
  36125. EVP_PKEY *pkey = NULL;
  36126. EVP_PKEY *set1Pkey = NULL;
  36127. SHA_CTX sha;
  36128. byte signature[DSA_SIG_SIZE];
  36129. byte hash[WC_SHA_DIGEST_SIZE];
  36130. word32 bytes;
  36131. int answer;
  36132. #ifdef USE_CERT_BUFFERS_1024
  36133. const unsigned char* dsaKeyDer = dsa_key_der1024;
  36134. int dsaKeySz = sizeof_dsa_key_der_1024;
  36135. byte tmp[ONEK_BUF];
  36136. XMEMSET(tmp, 0, sizeof(tmp));
  36137. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  36138. bytes = dsa_key_der_sz;
  36139. #elif defined(USE_CERT_BUFFERS_2048)
  36140. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  36141. int dsaKeySz = sizeof_dsa_key_der_2048;
  36142. byte tmp[TWOK_BUF];
  36143. XMEMSET(tmp, 0, sizeof(tmp));
  36144. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  36145. bytes = dsaKeySz;
  36146. #else
  36147. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  36148. int dsaKeySz = sizeof_dsa_key_der_2048;
  36149. byte tmp[TWOK_BUF];
  36150. XMEMSET(tmp, 0, sizeof(tmp));
  36151. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  36152. XFILE fp = XOPEN("./certs/dsa2048.der", "rb");
  36153. if (fp == XBADFILE) {
  36154. return WOLFSSL_BAD_FILE;
  36155. }
  36156. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  36157. XFCLOSE(fp);
  36158. #endif /* END USE_CERT_BUFFERS_1024 */
  36159. printf(testingFmt,
  36160. "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
  36161. /* Create hash to later Sign and Verify */
  36162. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  36163. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  36164. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  36165. /* Initialize pkey with der format dsa key */
  36166. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  36167. &dsaKeyDer ,(long)dsaKeySz));
  36168. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  36169. /* Should Fail: NULL argument */
  36170. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  36171. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  36172. /* Should Pass: Initialized pkey argument */
  36173. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  36174. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  36175. AssertIntEQ(DSA_bits(dsa), 2048);
  36176. /* Sign */
  36177. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  36178. /* Verify. */
  36179. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  36180. WOLFSSL_SUCCESS);
  36181. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  36182. /* Should Fail: set1Pkey not initialized */
  36183. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  36184. /* Initialize set1Pkey */
  36185. set1Pkey = EVP_PKEY_new();
  36186. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  36187. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  36188. WOLFSSL_SUCCESS);
  36189. /* Should Pass: set dsa into set1Pkey */
  36190. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  36191. printf(resultFmt, passed);
  36192. DSA_free(dsa);
  36193. DSA_free(setDsa);
  36194. EVP_PKEY_free(pkey);
  36195. EVP_PKEY_free(set1Pkey);
  36196. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  36197. } /* END test_EVP_PKEY_set1_get1_DSA */
  36198. static void test_wolfSSL_DSA_SIG(void)
  36199. {
  36200. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  36201. !defined(HAVE_FIPS)
  36202. DSA *dsa = NULL;
  36203. DSA *dsa2 = NULL;
  36204. DSA_SIG *sig = NULL;
  36205. const BIGNUM *p = NULL;
  36206. const BIGNUM *q = NULL;
  36207. const BIGNUM *g = NULL;
  36208. const BIGNUM *pub = NULL;
  36209. const BIGNUM *priv = NULL;
  36210. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  36211. printf(testingFmt, "wolfSSL_DSA_SIG");
  36212. AssertNotNull(dsa = DSA_generate_parameters(2048,
  36213. NULL, 0, NULL, NULL, NULL, NULL));
  36214. DSA_free(dsa);
  36215. AssertNotNull(dsa = DSA_new());
  36216. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  36217. NULL, 0, NULL, NULL, NULL), 1);
  36218. AssertIntEQ(DSA_generate_key(dsa), 1);
  36219. DSA_get0_pqg(dsa, &p, &q, &g);
  36220. DSA_get0_key(dsa, &pub, &priv);
  36221. AssertNotNull(p = BN_dup(p));
  36222. AssertNotNull(q = BN_dup(q));
  36223. AssertNotNull(g = BN_dup(g));
  36224. AssertNotNull(pub = BN_dup(pub));
  36225. AssertNotNull(priv = BN_dup(priv));
  36226. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  36227. AssertNotNull(dsa2 = DSA_new());
  36228. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  36229. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  36230. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  36231. printf(resultFmt, passed);
  36232. DSA_free(dsa);
  36233. DSA_free(dsa2);
  36234. DSA_SIG_free(sig);
  36235. #endif
  36236. }
  36237. static void test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  36238. {
  36239. #ifdef HAVE_ECC
  36240. WOLFSSL_EC_KEY *ecKey = NULL;
  36241. WOLFSSL_EC_KEY *ecGet1 = NULL;
  36242. EVP_PKEY *pkey = NULL;
  36243. printf(testingFmt,
  36244. "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
  36245. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  36246. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36247. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  36248. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  36249. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  36250. /* Should fail since ecKey is empty */
  36251. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  36252. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  36253. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  36254. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  36255. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  36256. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  36257. wolfSSL_EC_KEY_free(ecKey);
  36258. wolfSSL_EC_KEY_free(ecGet1);
  36259. EVP_PKEY_free(pkey);
  36260. /* PASSED */
  36261. printf(resultFmt, passed);
  36262. #endif /* HAVE_ECC */
  36263. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  36264. static void test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  36265. {
  36266. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  36267. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  36268. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  36269. DH *dh = NULL;
  36270. DH *setDh = NULL;
  36271. EVP_PKEY *pkey = NULL;
  36272. FILE* f = NULL;
  36273. unsigned char buf[4096];
  36274. const unsigned char* pt = buf;
  36275. const char* dh2048 = "./certs/dh2048.der";
  36276. long len = 0;
  36277. int code = -1;
  36278. printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
  36279. XMEMSET(buf, 0, sizeof(buf));
  36280. f = XFOPEN(dh2048, "rb");
  36281. AssertTrue(f != XBADFILE);
  36282. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  36283. XFCLOSE(f);
  36284. /* Load dh2048.der into DH with internal format */
  36285. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  36286. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  36287. AssertIntEQ(code, 0);
  36288. code = -1;
  36289. pkey = wolfSSL_EVP_PKEY_new();
  36290. /* Set DH into PKEY */
  36291. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  36292. /* Get DH from PKEY */
  36293. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  36294. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  36295. AssertIntEQ(code, 0);
  36296. EVP_PKEY_free(pkey);
  36297. DH_free(setDh);
  36298. DH_free(dh);
  36299. printf(resultFmt, passed);
  36300. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  36301. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36302. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  36303. } /* END test_EVP_PKEY_set1_get1_DH */
  36304. static void test_wolfSSL_CTX_ctrl(void)
  36305. {
  36306. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36307. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36308. char caFile[] = "./certs/client-ca.pem";
  36309. char clientFile[] = "./certs/client-cert.pem";
  36310. SSL_CTX* ctx;
  36311. X509* x509 = NULL;
  36312. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  36313. byte buf[6000];
  36314. char file[] = "./certs/dsaparams.pem";
  36315. XFILE f;
  36316. int bytes;
  36317. BIO* bio;
  36318. DSA* dsa;
  36319. DH* dh;
  36320. #endif
  36321. #ifdef HAVE_ECC
  36322. WOLFSSL_EC_KEY* ecKey;
  36323. #endif
  36324. printf(testingFmt, "wolfSSL_CTX_ctrl");
  36325. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36326. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  36327. AssertNotNull(x509);
  36328. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  36329. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  36330. AssertNotNull(x509);
  36331. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  36332. /* Initialize DH */
  36333. f = XFOPEN(file, "rb");
  36334. AssertTrue((f != XBADFILE));
  36335. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  36336. XFCLOSE(f);
  36337. bio = BIO_new_mem_buf((void*)buf, bytes);
  36338. AssertNotNull(bio);
  36339. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  36340. AssertNotNull(dsa);
  36341. dh = wolfSSL_DSA_dup_DH(dsa);
  36342. AssertNotNull(dh);
  36343. #endif
  36344. #ifdef HAVE_ECC
  36345. /* Initialize WOLFSSL_EC_KEY */
  36346. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  36347. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  36348. #endif
  36349. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  36350. /* additional test of getting EVP_PKEY key size from X509
  36351. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  36352. * allowed with user RSA */
  36353. {
  36354. EVP_PKEY* pkey;
  36355. #if defined(HAVE_ECC)
  36356. X509* ecX509;
  36357. #endif /* HAVE_ECC */
  36358. AssertNotNull(pkey = X509_get_pubkey(x509));
  36359. /* current RSA key is 2048 bit (256 bytes) */
  36360. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  36361. EVP_PKEY_free(pkey);
  36362. #if defined(HAVE_ECC)
  36363. #if defined(USE_CERT_BUFFERS_256)
  36364. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  36365. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  36366. SSL_FILETYPE_ASN1));
  36367. #else
  36368. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  36369. cliEccCertFile, SSL_FILETYPE_PEM));
  36370. #endif
  36371. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  36372. /* current ECC key is 256 bit (32 bytes) */
  36373. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  36374. X509_free(ecX509);
  36375. EVP_PKEY_free(pkey);
  36376. #endif /* HAVE_ECC */
  36377. }
  36378. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  36379. /* Tests should fail with passed in NULL pointer */
  36380. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  36381. SSL_FAILURE);
  36382. #if !defined(NO_DH) && !defined(NO_DSA)
  36383. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  36384. SSL_FAILURE);
  36385. #endif
  36386. #ifdef HAVE_ECC
  36387. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  36388. SSL_FAILURE);
  36389. #endif
  36390. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  36391. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  36392. */
  36393. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  36394. SSL_SUCCESS);
  36395. /* Test with SSL_CTRL_OPTIONS
  36396. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  36397. */
  36398. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  36399. == SSL_OP_NO_TLSv1);
  36400. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  36401. /* Test with SSL_CTRL_SET_TMP_DH
  36402. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  36403. */
  36404. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  36405. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  36406. SSL_SUCCESS);
  36407. #endif
  36408. /* Test with SSL_CTRL_SET_TMP_ECDH
  36409. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  36410. */
  36411. #ifdef HAVE_ECC
  36412. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  36413. SSL_SUCCESS);
  36414. #endif
  36415. #ifdef WOLFSSL_ENCRYPTED_KEYS
  36416. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  36417. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  36418. #endif
  36419. /* Test for min/max proto */
  36420. #ifndef WOLFSSL_NO_TLS12
  36421. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  36422. 0, NULL), SSL_SUCCESS);
  36423. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  36424. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  36425. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  36426. #endif
  36427. #ifdef WOLFSSL_TLS13
  36428. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  36429. 0, NULL), SSL_SUCCESS);
  36430. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  36431. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  36432. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  36433. #ifndef WOLFSSL_NO_TLS12
  36434. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  36435. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  36436. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  36437. #endif
  36438. #endif
  36439. /* Cleanup and Pass */
  36440. #if !defined(NO_DH) && !defined(NO_DSA)
  36441. #ifndef NO_BIO
  36442. BIO_free(bio);
  36443. DSA_free(dsa);
  36444. DH_free(dh);
  36445. #endif
  36446. #endif
  36447. #ifdef HAVE_ECC
  36448. wolfSSL_EC_KEY_free(ecKey);
  36449. #endif
  36450. SSL_CTX_free(ctx);
  36451. printf(resultFmt, passed);
  36452. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36453. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  36454. }
  36455. static void test_wolfSSL_DH_check(void)
  36456. {
  36457. #if !defined(NO_DH) && !defined(NO_DSA)
  36458. #ifndef NO_BIO
  36459. byte buf[6000];
  36460. char file[] = "./certs/dsaparams.pem";
  36461. XFILE f;
  36462. int bytes;
  36463. BIO* bio;
  36464. DSA* dsa;
  36465. DH* dh = NULL;
  36466. WOLFSSL_BIGNUM* pTmp = NULL;
  36467. WOLFSSL_BIGNUM* gTmp = NULL;
  36468. int codes = -1;
  36469. printf(testingFmt, "wolfSSL_DH_check");
  36470. /* Initialize DH */
  36471. f = XFOPEN(file, "rb");
  36472. AssertTrue((f != XBADFILE));
  36473. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  36474. XFCLOSE(f);
  36475. bio = BIO_new_mem_buf((void*)buf, bytes);
  36476. AssertNotNull(bio);
  36477. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  36478. AssertNotNull(dsa);
  36479. dh = wolfSSL_DSA_dup_DH(dsa);
  36480. AssertNotNull(dh);
  36481. /* Test assumed to be valid dh.
  36482. * Should return WOLFSSL_SUCCESS
  36483. * codes should be 0
  36484. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  36485. */
  36486. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_SUCCESS);
  36487. AssertIntEQ(codes, 0);
  36488. /* Test NULL dh: expected BAD_FUNC_ARG */
  36489. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), WOLFSSL_FAILURE);
  36490. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  36491. pTmp = dh->p;
  36492. dh->p = NULL;
  36493. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  36494. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  36495. /* set dh->p back to normal so it wont fail on next tests */
  36496. dh->p = pTmp;
  36497. pTmp = NULL;
  36498. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  36499. gTmp = dh->g;
  36500. dh->g = NULL;
  36501. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  36502. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  36503. dh->g = gTmp;
  36504. gTmp = NULL;
  36505. /* Cleanup and Pass Test */
  36506. BIO_free(bio);
  36507. DSA_free(dsa);
  36508. DH_free(dh);
  36509. printf(resultFmt, passed);
  36510. #endif
  36511. #endif /* !NO_DH && !NO_DSA */
  36512. }
  36513. static void test_wolfSSL_EVP_PKEY_assign(void)
  36514. {
  36515. #if defined(OPENSSL_ALL)
  36516. int type;
  36517. WOLFSSL_EVP_PKEY* pkey;
  36518. #ifndef NO_RSA
  36519. WOLFSSL_RSA* rsa;
  36520. #endif
  36521. #ifndef NO_DSA
  36522. WOLFSSL_DSA* dsa;
  36523. #endif
  36524. #ifdef HAVE_ECC
  36525. WOLFSSL_EC_KEY* ecKey;
  36526. #endif
  36527. (void)pkey;
  36528. printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
  36529. #ifndef NO_RSA
  36530. type = EVP_PKEY_RSA;
  36531. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36532. AssertNotNull(rsa = wolfSSL_RSA_new());
  36533. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  36534. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  36535. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  36536. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  36537. wolfSSL_EVP_PKEY_free(pkey);
  36538. #endif /* NO_RSA */
  36539. #ifndef NO_DSA
  36540. type = EVP_PKEY_DSA;
  36541. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36542. AssertNotNull(dsa = wolfSSL_DSA_new());
  36543. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  36544. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  36545. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  36546. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  36547. wolfSSL_EVP_PKEY_free(pkey);
  36548. #endif /* NO_DSA */
  36549. #ifdef HAVE_ECC
  36550. type = EVP_PKEY_EC;
  36551. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36552. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  36553. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  36554. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  36555. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  36556. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  36557. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  36558. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  36559. wolfSSL_EVP_PKEY_free(pkey);
  36560. #endif /* HAVE_ECC */
  36561. (void)type;
  36562. printf(resultFmt, passed);
  36563. #endif /* OPENSSL_ALL */
  36564. }
  36565. static void test_wolfSSL_EVP_PKEY_base_id(void)
  36566. {
  36567. #if defined(OPENSSL_ALL)
  36568. WOLFSSL_EVP_PKEY* pkey;
  36569. printf(testingFmt, "wolfSSL_EVP_PKEY_base_id");
  36570. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36571. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  36572. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  36573. EVP_PKEY_free(pkey);
  36574. printf(resultFmt, passed);
  36575. #endif
  36576. }
  36577. static void test_wolfSSL_EVP_PKEY_id(void)
  36578. {
  36579. #if defined(OPENSSL_ALL)
  36580. WOLFSSL_EVP_PKEY* pkey;
  36581. printf(testingFmt, "wolfSSL_EVP_PKEY_id");
  36582. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36583. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  36584. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  36585. EVP_PKEY_free(pkey);
  36586. printf(resultFmt, passed);
  36587. #endif
  36588. }
  36589. static void test_wolfSSL_EVP_PKEY_keygen(void)
  36590. {
  36591. #if defined(OPENSSL_ALL)
  36592. WOLFSSL_EVP_PKEY* pkey;
  36593. EVP_PKEY_CTX *ctx;
  36594. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen");
  36595. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36596. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  36597. /* Bad cases */
  36598. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  36599. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  36600. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  36601. /* Good case */
  36602. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  36603. EVP_PKEY_CTX_free(ctx);
  36604. EVP_PKEY_free(pkey);
  36605. printf(resultFmt, passed);
  36606. #endif
  36607. }
  36608. static void test_wolfSSL_EVP_PKEY_keygen_init(void)
  36609. {
  36610. #if defined(OPENSSL_ALL)
  36611. WOLFSSL_EVP_PKEY* pkey;
  36612. EVP_PKEY_CTX *ctx;
  36613. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen_init");
  36614. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36615. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  36616. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  36617. EVP_PKEY_CTX_free(ctx);
  36618. EVP_PKEY_free(pkey);
  36619. printf(resultFmt, passed);
  36620. #endif
  36621. }
  36622. static void test_wolfSSL_EVP_PKEY_missing_parameters(void)
  36623. {
  36624. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  36625. WOLFSSL_EVP_PKEY* pkey;
  36626. printf(testingFmt, "wolfSSL_EVP_PKEY_missing_parameters");
  36627. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36628. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  36629. EVP_PKEY_free(pkey);
  36630. printf(resultFmt, passed);
  36631. #endif
  36632. }
  36633. static void test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  36634. {
  36635. #if defined(OPENSSL_ALL)
  36636. WOLFSSL_EVP_PKEY* pkey;
  36637. EVP_PKEY_CTX *ctx;
  36638. int bits = 2048;
  36639. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits");
  36640. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  36641. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  36642. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  36643. WOLFSSL_SUCCESS);
  36644. EVP_PKEY_CTX_free(ctx);
  36645. EVP_PKEY_free(pkey);
  36646. printf(resultFmt, passed);
  36647. #endif
  36648. }
  36649. static void test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  36650. {
  36651. #if defined(OPENSSL_ALL)
  36652. /* This is large enough to be used for all key sizes */
  36653. byte key[AES_256_KEY_SIZE] = {0};
  36654. byte iv[AES_BLOCK_SIZE] = {0};
  36655. int i, enumlen;
  36656. EVP_CIPHER_CTX *ctx;
  36657. const EVP_CIPHER *init;
  36658. int enumArray[] = {
  36659. #ifdef HAVE_AES_CBC
  36660. NID_aes_128_cbc,
  36661. #endif
  36662. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  36663. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  36664. #ifdef HAVE_AESGCM
  36665. NID_aes_128_gcm,
  36666. #endif
  36667. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  36668. #ifdef WOLFSSL_AES_COUNTER
  36669. NID_aes_128_ctr,
  36670. #endif
  36671. #ifndef NO_DES3
  36672. NID_des_cbc,
  36673. NID_des_ede3_cbc,
  36674. #endif
  36675. #ifdef HAVE_IDEA
  36676. NID_idea_cbc,
  36677. #endif
  36678. };
  36679. int iv_lengths[] = {
  36680. #ifdef HAVE_AES_CBC
  36681. AES_BLOCK_SIZE,
  36682. #endif
  36683. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  36684. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  36685. #ifdef HAVE_AESGCM
  36686. GCM_NONCE_MID_SZ,
  36687. #endif
  36688. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  36689. #ifdef WOLFSSL_AES_COUNTER
  36690. AES_BLOCK_SIZE,
  36691. #endif
  36692. #ifndef NO_DES3
  36693. DES_BLOCK_SIZE,
  36694. DES_BLOCK_SIZE,
  36695. #endif
  36696. #ifdef HAVE_IDEA
  36697. IDEA_BLOCK_SIZE,
  36698. #endif
  36699. };
  36700. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_iv_length");
  36701. enumlen = (sizeof(enumArray)/sizeof(int));
  36702. for(i = 0; i < enumlen; i++)
  36703. {
  36704. ctx = EVP_CIPHER_CTX_new();
  36705. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  36706. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  36707. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  36708. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  36709. EVP_CIPHER_CTX_free(ctx);
  36710. }
  36711. printf(resultFmt, passed);
  36712. #endif
  36713. }
  36714. static void test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  36715. {
  36716. #if defined(OPENSSL_ALL) && !defined(NO_DES3)
  36717. byte key[AES_256_KEY_SIZE] = {0};
  36718. byte iv[AES_BLOCK_SIZE] = {0};
  36719. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  36720. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  36721. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_key_length");
  36722. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  36723. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  36724. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  36725. EVP_CIPHER_CTX_free(ctx);
  36726. printf(resultFmt, passed);
  36727. #endif
  36728. }
  36729. static void test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  36730. {
  36731. #if defined(OPENSSL_ALL) && !defined(NO_DES3)
  36732. byte key[AES_256_KEY_SIZE] = {0};
  36733. byte iv[AES_BLOCK_SIZE] = {0};
  36734. int keylen;
  36735. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  36736. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  36737. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_key_length");
  36738. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  36739. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  36740. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  36741. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  36742. WOLFSSL_SUCCESS);
  36743. EVP_CIPHER_CTX_free(ctx);
  36744. printf(resultFmt, passed);
  36745. #endif
  36746. }
  36747. static void test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  36748. {
  36749. #if defined(OPENSSL_ALL) && defined(HAVE_AESGCM) && !defined(NO_DES3)
  36750. byte key[DES3_KEY_SIZE] = {0};
  36751. byte iv[DES_BLOCK_SIZE] = {0};
  36752. int ivLen, keyLen;
  36753. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  36754. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  36755. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_iv");
  36756. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  36757. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  36758. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  36759. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  36760. /* Bad cases */
  36761. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  36762. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  36763. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  36764. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  36765. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  36766. /* Good case */
  36767. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  36768. EVP_CIPHER_CTX_free(ctx);
  36769. printf(resultFmt, passed);
  36770. #endif
  36771. }
  36772. static void test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  36773. {
  36774. #if defined(OPENSSL_ALL)
  36775. WOLFSSL_ENGINE* e = NULL;
  36776. int id = 0;
  36777. EVP_PKEY_CTX *ctx;
  36778. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_new_id");
  36779. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  36780. EVP_PKEY_CTX_free(ctx);
  36781. printf(resultFmt, passed);
  36782. #endif
  36783. }
  36784. static void test_wolfSSL_EVP_rc4(void)
  36785. {
  36786. #if defined(OPENSSL_ALL) && !defined(NO_RC4)
  36787. printf(testingFmt, "wolfSSL_EVP_rc4");
  36788. AssertNotNull(wolfSSL_EVP_rc4());
  36789. printf(resultFmt, passed);
  36790. #endif
  36791. }
  36792. static void test_wolfSSL_EVP_enc_null(void)
  36793. {
  36794. #if defined(OPENSSL_ALL)
  36795. printf(testingFmt, "wolfSSL_EVP_enc_null");
  36796. AssertNotNull(wolfSSL_EVP_enc_null());
  36797. printf(resultFmt, passed);
  36798. #endif
  36799. }
  36800. static void test_wolfSSL_EVP_rc2_cbc(void)
  36801. {
  36802. #if defined(OPENSSL_ALL) && defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  36803. printf(testingFmt, "wolfSSL_EVP_rc2_cbc");
  36804. AssertNull(wolfSSL_EVP_rc2_cbc());
  36805. printf(resultFmt, passed);
  36806. #endif
  36807. }
  36808. static void test_wolfSSL_EVP_mdc2(void)
  36809. {
  36810. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  36811. printf(testingFmt, "wolfSSL_EVP_mdc2");
  36812. AssertNull(wolfSSL_EVP_mdc2());
  36813. printf(resultFmt, passed);
  36814. #endif
  36815. }
  36816. static void test_wolfSSL_EVP_md4(void)
  36817. {
  36818. #if defined(OPENSSL_ALL) && !defined(NO_MD4)
  36819. printf(testingFmt, "wolfSSL_EVP_md4");
  36820. AssertNotNull(wolfSSL_EVP_md4());
  36821. printf(resultFmt, passed);
  36822. #endif
  36823. }
  36824. static void test_wolfSSL_EVP_aes_256_gcm(void)
  36825. {
  36826. #if defined(OPENSSL_ALL)
  36827. printf(testingFmt, "wolfSSL_EVP_aes_256_gcm");
  36828. AssertNotNull(wolfSSL_EVP_aes_256_gcm());
  36829. printf(resultFmt, passed);
  36830. #endif
  36831. }
  36832. static void test_wolfSSL_EVP_aes_192_gcm(void)
  36833. {
  36834. #if defined(OPENSSL_ALL)
  36835. printf(testingFmt, "wolfSSL_EVP_aes_192_gcm");
  36836. AssertNotNull(wolfSSL_EVP_aes_192_gcm());
  36837. printf(resultFmt, passed);
  36838. #endif
  36839. }
  36840. static void test_wolfSSL_EVP_ripemd160(void)
  36841. {
  36842. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  36843. printf(testingFmt, "wolfSSL_EVP_ripemd160");
  36844. AssertNull(wolfSSL_EVP_ripemd160());
  36845. printf(resultFmt, passed);
  36846. #endif
  36847. }
  36848. static void test_wolfSSL_EVP_get_digestbynid(void)
  36849. {
  36850. #if defined(OPENSSL_ALL)
  36851. printf(testingFmt, "wolfSSL_EVP_get_digestbynid");
  36852. #ifndef NO_MD5
  36853. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  36854. #endif
  36855. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  36856. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  36857. printf(resultFmt, passed);
  36858. #endif
  36859. }
  36860. static void test_wolfSSL_EVP_MD_nid(void)
  36861. {
  36862. #if defined(OPENSSL_ALL)
  36863. printf(testingFmt, "wolfSSL_EVP_MD_nid");
  36864. #ifndef NO_MD5
  36865. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  36866. #endif
  36867. #ifndef NO_SHA
  36868. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  36869. #endif
  36870. #ifndef NO_SHA256
  36871. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  36872. #endif
  36873. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  36874. printf(resultFmt, passed);
  36875. #endif
  36876. }
  36877. static void test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  36878. {
  36879. #if defined(HAVE_ECC) && defined(OPENSSL_ALL)
  36880. WOLFSSL_EVP_PKEY* pkey;
  36881. printf(testingFmt, "wolfSSL_EVP_PKEY_get0_EC_KEY");
  36882. AssertNotNull(pkey = EVP_PKEY_new());
  36883. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  36884. EVP_PKEY_free(pkey);
  36885. printf(resultFmt, passed);
  36886. #endif
  36887. }
  36888. static void test_wolfSSL_EVP_X_STATE(void)
  36889. {
  36890. #if defined(OPENSSL_ALL) && !defined(NO_DES3) && !defined(NO_RC4)
  36891. byte key[DES3_KEY_SIZE] = {0};
  36892. byte iv[DES_IV_SIZE] = {0};
  36893. EVP_CIPHER_CTX *ctx;
  36894. const EVP_CIPHER *init;
  36895. printf(testingFmt, "wolfSSL_EVP_X_STATE");
  36896. /* Bad test cases */
  36897. ctx = EVP_CIPHER_CTX_new();
  36898. init = EVP_des_ede3_cbc();
  36899. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  36900. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  36901. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  36902. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  36903. EVP_CIPHER_CTX_free(ctx);
  36904. /* Good test case */
  36905. ctx = EVP_CIPHER_CTX_new();
  36906. init = wolfSSL_EVP_rc4();
  36907. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  36908. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  36909. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  36910. EVP_CIPHER_CTX_free(ctx);
  36911. printf(resultFmt, passed);
  36912. #endif
  36913. }
  36914. static void test_wolfSSL_EVP_X_STATE_LEN(void)
  36915. {
  36916. #if defined(OPENSSL_ALL) && !defined(NO_DES3) && !defined(NO_RC4)
  36917. byte key[DES3_KEY_SIZE] = {0};
  36918. byte iv[DES_IV_SIZE] = {0};
  36919. EVP_CIPHER_CTX *ctx;
  36920. const EVP_CIPHER *init;
  36921. printf(testingFmt, "wolfSSL_EVP_X_STATE_LEN");
  36922. /* Bad test cases */
  36923. ctx = EVP_CIPHER_CTX_new();
  36924. init = EVP_des_ede3_cbc();
  36925. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  36926. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  36927. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  36928. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  36929. EVP_CIPHER_CTX_free(ctx);
  36930. /* Good test case */
  36931. ctx = EVP_CIPHER_CTX_new();
  36932. init = wolfSSL_EVP_rc4();
  36933. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  36934. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  36935. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  36936. EVP_CIPHER_CTX_free(ctx);
  36937. printf(resultFmt, passed);
  36938. #endif
  36939. }
  36940. static void test_wolfSSL_EVP_CIPHER_block_size(void)
  36941. {
  36942. #if defined(OPENSSL_ALL)
  36943. #ifdef HAVE_AES_CBC
  36944. #ifdef WOLFSSL_AES_128
  36945. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  36946. #endif
  36947. #ifdef WOLFSSL_AES_192
  36948. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  36949. #endif
  36950. #ifdef WOLFSSL_AES_256
  36951. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  36952. #endif
  36953. #endif
  36954. #ifdef HAVE_AES_GCM
  36955. #ifdef WOLFSSL_AES_128
  36956. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  36957. #endif
  36958. #ifdef WOLFSSL_AES_192
  36959. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  36960. #endif
  36961. #ifdef WOLFSSL_AES_256
  36962. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  36963. #endif
  36964. #endif
  36965. #ifdef WOLFSSL_AES_COUNTER
  36966. #ifdef WOLFSSL_AES_128
  36967. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  36968. #endif
  36969. #ifdef WOLFSSL_AES_192
  36970. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  36971. #endif
  36972. #ifdef WOLFSSL_AES_256
  36973. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  36974. #endif
  36975. #endif
  36976. #ifdef HAVE_AES_ECB
  36977. #ifdef WOLFSSL_AES_128
  36978. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  36979. #endif
  36980. #ifdef WOLFSSL_AES_192
  36981. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  36982. #endif
  36983. #ifdef WOLFSSL_AES_256
  36984. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  36985. #endif
  36986. #endif
  36987. #ifdef WOLFSSL_AES_OFB
  36988. #ifdef WOLFSSL_AES_128
  36989. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  36990. #endif
  36991. #ifdef WOLFSSL_AES_192
  36992. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  36993. #endif
  36994. #ifdef WOLFSSL_AES_256
  36995. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  36996. #endif
  36997. #endif
  36998. #ifndef NO_RC4
  36999. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  37000. #endif
  37001. #endif /* OPENSSL_ALL */
  37002. }
  37003. static void test_wolfSSL_EVP_CIPHER_iv_length(void)
  37004. {
  37005. #if defined(OPENSSL_ALL)
  37006. int i, enumlen;
  37007. int enumArray[] = {
  37008. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  37009. #ifdef WOLFSSL_AES_128
  37010. NID_aes_128_cbc,
  37011. #endif
  37012. #ifdef WOLFSSL_AES_192
  37013. NID_aes_192_cbc,
  37014. #endif
  37015. #ifdef WOLFSSL_AES_256
  37016. NID_aes_256_cbc,
  37017. #endif
  37018. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  37019. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  37020. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  37021. #ifdef HAVE_AESGCM
  37022. #ifdef WOLFSSL_AES_128
  37023. NID_aes_128_gcm,
  37024. #endif
  37025. #ifdef WOLFSSL_AES_192
  37026. NID_aes_192_gcm,
  37027. #endif
  37028. #ifdef WOLFSSL_AES_256
  37029. NID_aes_256_gcm,
  37030. #endif
  37031. #endif /* HAVE_AESGCM */
  37032. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  37033. #ifdef WOLFSSL_AES_COUNTER
  37034. #ifdef WOLFSSL_AES_128
  37035. NID_aes_128_ctr,
  37036. #endif
  37037. #ifdef WOLFSSL_AES_192
  37038. NID_aes_192_ctr,
  37039. #endif
  37040. #ifdef WOLFSSL_AES_256
  37041. NID_aes_256_ctr,
  37042. #endif
  37043. #endif
  37044. #ifndef NO_DES3
  37045. NID_des_cbc,
  37046. NID_des_ede3_cbc,
  37047. #endif
  37048. #ifdef HAVE_IDEA
  37049. NID_idea_cbc,
  37050. #endif
  37051. };
  37052. int iv_lengths[] = {
  37053. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  37054. #ifdef WOLFSSL_AES_128
  37055. AES_BLOCK_SIZE,
  37056. #endif
  37057. #ifdef WOLFSSL_AES_192
  37058. AES_BLOCK_SIZE,
  37059. #endif
  37060. #ifdef WOLFSSL_AES_256
  37061. AES_BLOCK_SIZE,
  37062. #endif
  37063. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  37064. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  37065. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  37066. #ifdef HAVE_AESGCM
  37067. #ifdef WOLFSSL_AES_128
  37068. GCM_NONCE_MID_SZ,
  37069. #endif
  37070. #ifdef WOLFSSL_AES_192
  37071. GCM_NONCE_MID_SZ,
  37072. #endif
  37073. #ifdef WOLFSSL_AES_256
  37074. GCM_NONCE_MID_SZ,
  37075. #endif
  37076. #endif /* HAVE_AESGCM */
  37077. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  37078. #ifdef WOLFSSL_AES_COUNTER
  37079. #ifdef WOLFSSL_AES_128
  37080. AES_BLOCK_SIZE,
  37081. #endif
  37082. #ifdef WOLFSSL_AES_192
  37083. AES_BLOCK_SIZE,
  37084. #endif
  37085. #ifdef WOLFSSL_AES_256
  37086. AES_BLOCK_SIZE,
  37087. #endif
  37088. #endif
  37089. #ifndef NO_DES3
  37090. DES_BLOCK_SIZE,
  37091. DES_BLOCK_SIZE,
  37092. #endif
  37093. #ifdef HAVE_IDEA
  37094. IDEA_BLOCK_SIZE,
  37095. #endif
  37096. };
  37097. printf(testingFmt, "wolfSSL_EVP_CIPHER_iv_length");
  37098. enumlen = (sizeof(enumArray)/sizeof(int));
  37099. for(i = 0; i < enumlen; i++)
  37100. {
  37101. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  37102. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  37103. }
  37104. printf(resultFmt, passed);
  37105. #endif
  37106. }
  37107. static void test_wolfSSL_EVP_SignInit_ex(void)
  37108. {
  37109. #if defined(OPENSSL_ALL)
  37110. WOLFSSL_EVP_MD_CTX mdCtx;
  37111. WOLFSSL_ENGINE* e = 0;
  37112. const EVP_MD* md;
  37113. md = "SHA256";
  37114. printf(testingFmt, "wolfSSL_EVP_SignInit_ex");
  37115. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  37116. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  37117. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  37118. printf(resultFmt, passed);
  37119. #endif
  37120. }
  37121. static void test_wolfSSL_EVP_DigestFinal_ex(void)
  37122. {
  37123. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  37124. WOLFSSL_EVP_MD_CTX mdCtx;
  37125. unsigned int s = 0;
  37126. unsigned char md[WC_SHA256_DIGEST_SIZE];
  37127. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  37128. printf(testingFmt, "wolfSSL_EVP_DigestFinal_ex");
  37129. /* Bad Case */
  37130. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  37131. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  37132. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  37133. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  37134. #else
  37135. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  37136. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  37137. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  37138. #endif
  37139. /* Good Case */
  37140. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  37141. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  37142. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  37143. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  37144. printf(resultFmt, passed);
  37145. #endif
  37146. }
  37147. static void test_wolfSSL_EVP_PKEY_assign_DH(void)
  37148. {
  37149. #if defined(OPENSSL_ALL) && !defined(NO_DH) && \
  37150. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  37151. FILE* f = NULL;
  37152. unsigned char buf[4096];
  37153. const unsigned char* pt = buf;
  37154. const char* params1 = "./certs/dh2048.der";
  37155. long len = 0;
  37156. WOLFSSL_DH* dh = NULL;
  37157. WOLFSSL_EVP_PKEY* pkey;
  37158. XMEMSET(buf, 0, sizeof(buf));
  37159. f = XFOPEN(params1, "rb");
  37160. AssertTrue(f != XBADFILE);
  37161. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37162. XFCLOSE(f);
  37163. printf(testingFmt, "wolfSSL_EVP_PKEY_assign_DH");
  37164. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37165. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  37166. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37167. /* Bad cases */
  37168. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  37169. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  37170. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  37171. /* Good case */
  37172. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  37173. EVP_PKEY_free(pkey);
  37174. printf(resultFmt, passed);
  37175. #endif
  37176. }
  37177. static void test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  37178. {
  37179. #if defined(OPENSSL_EXTRA)
  37180. EVP_PKEY* pkey;
  37181. EVP_PKEY_CTX* ctx;
  37182. printf(testingFmt, "test_wolfSSL_QT_EVP_PKEY_CTX_free");
  37183. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37184. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  37185. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  37186. /* void */
  37187. EVP_PKEY_CTX_free(ctx);
  37188. AssertTrue(1);
  37189. #else
  37190. /* int */
  37191. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  37192. #endif
  37193. EVP_PKEY_free(pkey);
  37194. printf(resultFmt, passed);
  37195. #endif
  37196. }
  37197. static void test_wolfSSL_EVP_PKEY_param_check(void)
  37198. {
  37199. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  37200. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  37201. DH *dh = NULL;
  37202. DH *setDh = NULL;
  37203. EVP_PKEY *pkey = NULL;
  37204. EVP_PKEY_CTX* ctx = NULL;
  37205. FILE* f = NULL;
  37206. unsigned char buf[512];
  37207. const unsigned char* pt = buf;
  37208. const char* dh2048 = "./certs/dh2048.der";
  37209. long len = 0;
  37210. int code = -1;
  37211. printf(testingFmt, "test_wolfSSL_EVP_PKEY_param_check");
  37212. XMEMSET(buf, 0, sizeof(buf));
  37213. f = XFOPEN(dh2048, "rb");
  37214. AssertTrue(f != XBADFILE);
  37215. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37216. XFCLOSE(f);
  37217. /* Load dh2048.der into DH with internal format */
  37218. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  37219. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  37220. AssertIntEQ(code, 0);
  37221. code = -1;
  37222. pkey = wolfSSL_EVP_PKEY_new();
  37223. /* Set DH into PKEY */
  37224. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  37225. /* create ctx from pkey */
  37226. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  37227. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  37228. /* */
  37229. /* TO DO invlaid case */
  37230. /* */
  37231. EVP_PKEY_CTX_free(ctx);
  37232. EVP_PKEY_free(pkey);
  37233. DH_free(setDh);
  37234. DH_free(dh);
  37235. printf(resultFmt, passed);
  37236. #endif
  37237. #endif
  37238. }
  37239. static void test_wolfSSL_EVP_BytesToKey(void)
  37240. {
  37241. #if defined(OPENSSL_ALL) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  37242. byte key[AES_BLOCK_SIZE] = {0};
  37243. byte iv[AES_BLOCK_SIZE] = {0};
  37244. int sz = 5;
  37245. int count = 0;
  37246. const EVP_MD* md;
  37247. md = "SHA256";
  37248. const EVP_CIPHER *type;
  37249. const unsigned char *salt = (unsigned char *)"salt1234";
  37250. const byte data[] = {
  37251. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  37252. 0x72,0x6c,0x64
  37253. };
  37254. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  37255. printf(testingFmt, "EVP_BytesToKey");
  37256. /* Bad cases */
  37257. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  37258. 0);
  37259. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  37260. 16);
  37261. md = "2";
  37262. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  37263. WOLFSSL_FAILURE);
  37264. /* Good case */
  37265. md = "SHA256";
  37266. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  37267. 16);
  37268. printf(resultFmt, passed);
  37269. #endif
  37270. }
  37271. static void test_IncCtr(void)
  37272. {
  37273. #if defined(OPENSSL_ALL) && defined(HAVE_AESGCM) && !defined(HAVE_FIPS)
  37274. byte key[AES_128_KEY_SIZE] = {0};
  37275. byte iv[GCM_NONCE_MID_SZ] = {0};
  37276. int type = EVP_CTRL_GCM_IV_GEN;
  37277. int arg = 0;
  37278. void *ptr = NULL;
  37279. printf(testingFmt, "IncCtr");
  37280. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  37281. const EVP_CIPHER *init = EVP_aes_128_gcm();
  37282. AssertNotNull(ctx);
  37283. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  37284. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  37285. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_ctrl(ctx, type, arg, ptr), WOLFSSL_SUCCESS);
  37286. EVP_CIPHER_CTX_free(ctx);
  37287. printf(resultFmt, passed);
  37288. #endif
  37289. }
  37290. static void test_wolfSSL_OBJ_ln(void)
  37291. {
  37292. const int nid_set[] = {
  37293. NID_commonName,
  37294. NID_serialNumber,
  37295. NID_countryName,
  37296. NID_localityName,
  37297. NID_stateOrProvinceName,
  37298. NID_organizationName,
  37299. NID_organizationalUnitName,
  37300. NID_domainComponent,
  37301. NID_businessCategory,
  37302. NID_jurisdictionCountryName,
  37303. NID_jurisdictionStateOrProvinceName,
  37304. NID_emailAddress
  37305. };
  37306. const char* ln_set[] = {
  37307. "commonName",
  37308. "serialNumber",
  37309. "countryName",
  37310. "localityName",
  37311. "stateOrProvinceName",
  37312. "organizationName",
  37313. "organizationalUnitName",
  37314. "domainComponent",
  37315. "businessCategory",
  37316. "jurisdictionCountryName",
  37317. "jurisdictionStateOrProvinceName",
  37318. "emailAddress",
  37319. };
  37320. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  37321. printf(testingFmt, "wolfSSL_OBJ_ln");
  37322. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  37323. #ifdef HAVE_ECC
  37324. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  37325. {
  37326. size_t nCurves = 27;
  37327. EC_builtin_curve r[nCurves];
  37328. nCurves = EC_get_builtin_curves(r,nCurves);
  37329. for (i = 0; i < nCurves; i++) {
  37330. /* skip ECC_CURVE_INVALID */
  37331. if (r[i].nid != ECC_CURVE_INVALID) {
  37332. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  37333. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  37334. }
  37335. }
  37336. }
  37337. #endif
  37338. #endif
  37339. for (i = 0; i < maxIdx; i++) {
  37340. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  37341. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  37342. }
  37343. printf(resultFmt, passed);
  37344. }
  37345. static void test_wolfSSL_OBJ_sn(void)
  37346. {
  37347. int i = 0, maxIdx = 7;
  37348. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  37349. NID_stateOrProvinceName,NID_organizationName,
  37350. NID_organizationalUnitName,NID_emailAddress};
  37351. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  37352. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  37353. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  37354. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  37355. WOLFSSL_EMAIL_ADDR};
  37356. printf(testingFmt, "wolfSSL_OBJ_sn");
  37357. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  37358. for (i = 0; i < maxIdx; i++) {
  37359. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  37360. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  37361. }
  37362. printf(resultFmt, passed);
  37363. }
  37364. #if !defined(NO_BIO)
  37365. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  37366. {
  37367. return lh_strhash(s[3]);
  37368. }
  37369. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  37370. {
  37371. return XSTRCMP(a[3], b[3]);
  37372. }
  37373. #endif
  37374. static void test_wolfSSL_TXT_DB(void)
  37375. {
  37376. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  37377. BIO *bio;
  37378. TXT_DB *db = NULL;
  37379. const int columns = 6;
  37380. const char *fields[6] = {
  37381. "V",
  37382. "320926161116Z",
  37383. "",
  37384. "12BD",
  37385. "unknown",
  37386. "/CN=rsa doe",
  37387. };
  37388. char** fields_copy;
  37389. printf(testingFmt, "wolfSSL_TXT_DB");
  37390. /* Test read */
  37391. AssertNotNull(bio = BIO_new(BIO_s_file()));
  37392. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  37393. AssertNotNull(db = TXT_DB_read(bio, columns));
  37394. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  37395. DYNAMIC_TYPE_OPENSSL));
  37396. XMEMCPY(fields_copy, fields, sizeof(fields));
  37397. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  37398. BIO_free(bio);
  37399. /* Test write */
  37400. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  37401. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  37402. BIO_free(bio);
  37403. /* Test index */
  37404. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  37405. (wolf_sk_compare_cb)TXT_DB_cmp), 1);
  37406. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  37407. fields[3] = "12DA";
  37408. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  37409. fields[3] = "FFFF";
  37410. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  37411. fields[3] = "";
  37412. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  37413. TXT_DB_free(db);
  37414. printf(resultFmt, passed);
  37415. #endif
  37416. }
  37417. static void test_wolfSSL_NCONF(void)
  37418. {
  37419. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  37420. const char* confFile = "./tests/NCONF_test.cnf";
  37421. CONF* conf = NULL;
  37422. long eline = 0;
  37423. long num = 0;
  37424. printf(testingFmt, "wolfSSL_NCONF");
  37425. AssertNotNull(conf = NCONF_new(NULL));
  37426. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  37427. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  37428. AssertIntEQ(num, 1234);
  37429. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  37430. AssertIntEQ(num, 4321);
  37431. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  37432. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  37433. "./test-dir/file1");
  37434. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  37435. "./test-dir/file1:./test-dir/file2:./section1:file2");
  37436. NCONF_free(conf);
  37437. printf(resultFmt, passed);
  37438. #endif
  37439. }
  37440. #endif /* OPENSSL_ALL */
  37441. static void test_wolfSSL_EC_KEY_set_group(void)
  37442. {
  37443. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  37444. defined(OPENSSL_EXTRA)
  37445. EC_KEY *key = NULL;
  37446. EC_GROUP *group = NULL;
  37447. const EC_GROUP *group2 = NULL;
  37448. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  37449. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  37450. AssertNotNull(key = EC_KEY_new());
  37451. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  37452. AssertNotNull(group2 = EC_KEY_get0_group(key));
  37453. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  37454. EC_GROUP_free(group);
  37455. EC_KEY_free(key);
  37456. printf(resultFmt, passed);
  37457. #endif
  37458. }
  37459. static void test_wolfSSL_X509V3_EXT_get(void) {
  37460. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  37461. FILE* f;
  37462. int numOfExt =0;
  37463. int extNid = 0;
  37464. int i = 0;
  37465. WOLFSSL_X509* x509;
  37466. WOLFSSL_X509_EXTENSION* ext;
  37467. const WOLFSSL_v3_ext_method* method;
  37468. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  37469. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  37470. fclose(f);
  37471. printf(testingFmt, "wolfSSL_X509V3_EXT_get() return struct and nid test");
  37472. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  37473. for (i = 0; i < numOfExt; i++) {
  37474. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  37475. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  37476. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  37477. AssertIntEQ(method->ext_nid, extNid);
  37478. }
  37479. printf(resultFmt, "passed");
  37480. printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
  37481. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  37482. printf(resultFmt, "passed");
  37483. wolfSSL_X509_free(x509);
  37484. #endif
  37485. }
  37486. static void test_wolfSSL_X509V3_EXT_nconf(void)
  37487. {
  37488. #if defined (OPENSSL_ALL)
  37489. const char *ext_names[] = {
  37490. "subjectKeyIdentifier",
  37491. "authorityKeyIdentifier",
  37492. "subjectAltName",
  37493. "keyUsage",
  37494. };
  37495. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  37496. int ext_nids[] = {
  37497. NID_subject_key_identifier,
  37498. NID_authority_key_identifier,
  37499. NID_subject_alt_name,
  37500. NID_key_usage,
  37501. };
  37502. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  37503. const char *ext_values[] = {
  37504. "hash",
  37505. "hash",
  37506. "DNS:example.com, IP:127.0.0.1",
  37507. "digitalSignature,keyEncipherment,dataEncipherment",
  37508. };
  37509. size_t i;
  37510. printf(testingFmt, "wolfSSL_X509V3_EXT_nconf()");
  37511. for (i = 0; i < ext_names_count; i++) {
  37512. X509_EXTENSION* ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  37513. ext_values[i]);
  37514. AssertNotNull(ext);
  37515. X509_EXTENSION_free(ext);
  37516. }
  37517. for (i = 0; i < ext_nids_count; i++) {
  37518. X509_EXTENSION* ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  37519. ext_values[i]);
  37520. AssertNotNull(ext);
  37521. X509_EXTENSION_free(ext);
  37522. }
  37523. printf(resultFmt, "passed");
  37524. #endif
  37525. }
  37526. static void test_wolfSSL_X509V3_EXT(void) {
  37527. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  37528. FILE* f;
  37529. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  37530. char* str;
  37531. unsigned char* data;
  37532. const WOLFSSL_v3_ext_method* method;
  37533. WOLFSSL_X509* x509;
  37534. WOLFSSL_X509_EXTENSION* ext;
  37535. WOLFSSL_X509_EXTENSION* ext2;
  37536. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  37537. WOLFSSL_ASN1_STRING* asn1str;
  37538. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  37539. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  37540. WOLFSSL_BASIC_CONSTRAINTS* bc;
  37541. WOLFSSL_ACCESS_DESCRIPTION* ad;
  37542. WOLFSSL_GENERAL_NAME* gn;
  37543. printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
  37544. /* Check NULL argument */
  37545. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  37546. /* Using OCSP cert with X509V3 extensions */
  37547. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  37548. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  37549. fclose(f);
  37550. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  37551. /* Basic Constraints */
  37552. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  37553. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  37554. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  37555. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  37556. AssertIntEQ(bc->ca, 1);
  37557. AssertNull(bc->pathlen);
  37558. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  37559. i++;
  37560. /* Subject Key Identifier */
  37561. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  37562. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  37563. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  37564. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  37565. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  37566. asn1str));
  37567. X509_EXTENSION_free(ext2);
  37568. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  37569. AssertNotNull(method->i2s);
  37570. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  37571. wolfSSL_ASN1_STRING_free(asn1str);
  37572. actual = strcmp(str,
  37573. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  37574. AssertIntEQ(actual, 0);
  37575. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37576. i++;
  37577. /* Authority Key Identifier */
  37578. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  37579. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  37580. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  37581. AssertNotNull(aKeyId =
  37582. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  37583. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  37584. AssertNotNull(asn1str = aKeyId->keyid);
  37585. AssertNotNull(str =
  37586. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  37587. actual = strcmp(str,
  37588. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  37589. AssertIntEQ(actual, 0);
  37590. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37591. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  37592. i++;
  37593. /* Key Usage */
  37594. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  37595. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  37596. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  37597. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  37598. #if defined(WOLFSSL_QT)
  37599. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  37600. #else
  37601. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  37602. #endif
  37603. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  37604. #ifdef BIG_ENDIAN_ORDER
  37605. actual = data[1];
  37606. #else
  37607. actual = data[0];
  37608. #endif
  37609. AssertIntEQ(actual, expected);
  37610. wolfSSL_ASN1_STRING_free(asn1str);
  37611. #if 1
  37612. i++;
  37613. /* Authority Info Access */
  37614. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  37615. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  37616. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  37617. AssertNotNull(aia =
  37618. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  37619. #if defined(WOLFSSL_QT)
  37620. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  37621. #else
  37622. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  37623. #endif
  37624. /* URI entry is an ACCESS_DESCRIPTION type */
  37625. #if defined(WOLFSSL_QT)
  37626. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  37627. #else
  37628. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  37629. #endif
  37630. AssertNotNull(adObj = ad->method);
  37631. /* Make sure nid is OCSP */
  37632. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  37633. /* GENERAL_NAME stores URI as an ASN1_STRING */
  37634. AssertNotNull(gn = ad->location);
  37635. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  37636. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  37637. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  37638. #if defined(WOLFSSL_QT)
  37639. str = (char*)ASN1_STRING_get0_data(asn1str);
  37640. #else
  37641. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  37642. #endif
  37643. actual = strcmp(str, "http://127.0.0.1:22220");
  37644. AssertIntEQ(actual, 0);
  37645. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  37646. #else
  37647. (void) aia; (void) ad; (void) adObj; (void) gn;
  37648. #endif
  37649. wolfSSL_X509_free(x509);
  37650. printf(resultFmt, "passed");
  37651. #endif
  37652. }
  37653. static void test_wolfSSL_X509_get_extension_flags(void)
  37654. {
  37655. #ifdef OPENSSL_ALL
  37656. XFILE f;
  37657. X509* x509;
  37658. unsigned int extFlags;
  37659. unsigned int keyUsageFlags;
  37660. unsigned int extKeyUsageFlags;
  37661. printf(testingFmt, "test_wolfSSL_X509_get_extension_flags");
  37662. /* client-int-cert.pem has the following extension flags. */
  37663. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  37664. /* and the following key usage flags. */
  37665. keyUsageFlags = KU_DIGITAL_SIGNATURE
  37666. | KU_NON_REPUDIATION
  37667. | KU_KEY_ENCIPHERMENT;
  37668. /* and the following extended key usage flags. */
  37669. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  37670. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  37671. AssertTrue(f != XBADFILE);
  37672. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  37673. XFCLOSE(f);
  37674. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  37675. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  37676. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  37677. X509_free(x509);
  37678. /* client-cert-ext.pem has the following extension flags. */
  37679. extFlags = EXFLAG_KUSAGE;
  37680. /* and the following key usage flags. */
  37681. keyUsageFlags = KU_DIGITAL_SIGNATURE
  37682. | KU_KEY_CERT_SIGN
  37683. | KU_CRL_SIGN;
  37684. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  37685. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  37686. XFCLOSE(f);
  37687. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  37688. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  37689. X509_free(x509);
  37690. printf(resultFmt, passed);
  37691. #endif /* OPENSSL_ALL */
  37692. }
  37693. static void test_wolfSSL_X509_get_ext(void){
  37694. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  37695. int ret = 0;
  37696. FILE* f;
  37697. WOLFSSL_X509* x509;
  37698. WOLFSSL_X509_EXTENSION* foundExtension;
  37699. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  37700. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  37701. fclose(f);
  37702. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  37703. printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
  37704. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  37705. printf(resultFmt, "passed");
  37706. printf(testingFmt, "wolfSSL_X509_get_ext() valid x509, idx out of bounds");
  37707. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  37708. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  37709. printf(resultFmt, "passed");
  37710. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, idx out of bounds");
  37711. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  37712. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  37713. printf(resultFmt, "passed");
  37714. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
  37715. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  37716. printf(resultFmt, "passed");
  37717. wolfSSL_X509_free(x509);
  37718. #endif
  37719. }
  37720. static void test_wolfSSL_X509_get_ext_by_NID(void)
  37721. {
  37722. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  37723. int rc;
  37724. FILE* f;
  37725. WOLFSSL_X509* x509;
  37726. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  37727. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  37728. fclose(f);
  37729. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  37730. AssertIntGE(rc, 0);
  37731. /* Start search from last location (should fail) */
  37732. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  37733. AssertIntGE(rc, -1);
  37734. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  37735. AssertIntGE(rc, -1);
  37736. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  37737. AssertIntEQ(rc, -1);
  37738. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  37739. AssertIntEQ(rc, -1);
  37740. wolfSSL_X509_free(x509);
  37741. #endif
  37742. }
  37743. static void test_wolfSSL_X509_get_ext_subj_alt_name(void)
  37744. {
  37745. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  37746. int rc;
  37747. XFILE f;
  37748. WOLFSSL_X509* x509;
  37749. WOLFSSL_X509_EXTENSION* ext;
  37750. WOLFSSL_ASN1_STRING* sanString;
  37751. byte* sanDer;
  37752. const byte expectedDer[] = {
  37753. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  37754. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  37755. printf(testingFmt, "test_wolfSSL_X509_get_ext_subj_alt_name");
  37756. f = XFOPEN("./certs/server-cert.pem", "rb");
  37757. AssertTrue(f != XBADFILE);
  37758. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  37759. fclose(f);
  37760. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  37761. AssertIntNE(rc, -1);
  37762. AssertNotNull(ext = X509_get_ext(x509, rc));
  37763. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  37764. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  37765. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  37766. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  37767. X509_free(x509);
  37768. printf(resultFmt, passed);
  37769. #endif
  37770. }
  37771. static void test_wolfSSL_X509_EXTENSION_new(void)
  37772. {
  37773. #if defined (OPENSSL_ALL)
  37774. WOLFSSL_X509_EXTENSION* ext;
  37775. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  37776. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  37777. ext->obj->nid = WOLFSSL_SUCCESS;
  37778. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  37779. wolfSSL_X509_EXTENSION_free(ext);
  37780. #endif
  37781. }
  37782. static void test_wolfSSL_X509_EXTENSION_get_object(void)
  37783. {
  37784. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  37785. WOLFSSL_X509* x509;
  37786. WOLFSSL_X509_EXTENSION* ext;
  37787. WOLFSSL_ASN1_OBJECT* o;
  37788. FILE* file;
  37789. int nid = 0;
  37790. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  37791. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  37792. fclose(file);
  37793. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() testing ext idx 0");
  37794. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  37795. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  37796. AssertIntEQ(o->nid, 128);
  37797. nid = o->nid;
  37798. printf(resultFmt, nid == 128 ? passed : failed);
  37799. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
  37800. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  37801. printf(resultFmt, passed);
  37802. wolfSSL_X509_free(x509);
  37803. #endif
  37804. }
  37805. static void test_wolfSSL_X509_EXTENSION_get_data(void)
  37806. {
  37807. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  37808. WOLFSSL_X509* x509;
  37809. WOLFSSL_X509_EXTENSION* ext;
  37810. WOLFSSL_ASN1_STRING* str;
  37811. FILE* file;
  37812. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
  37813. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  37814. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  37815. fclose(file);
  37816. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  37817. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  37818. printf(resultFmt, passed);
  37819. wolfSSL_X509_free(x509);
  37820. #endif
  37821. }
  37822. static void test_wolfSSL_X509_EXTENSION_get_critical(void)
  37823. {
  37824. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  37825. WOLFSSL_X509* x509;
  37826. WOLFSSL_X509_EXTENSION* ext;
  37827. FILE* file;
  37828. int crit;
  37829. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
  37830. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  37831. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  37832. fclose(file);
  37833. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  37834. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  37835. AssertIntEQ(crit, 0);
  37836. printf(resultFmt, passed);
  37837. wolfSSL_X509_free(x509);
  37838. #endif
  37839. }
  37840. static void test_wolfSSL_X509V3_EXT_print(void)
  37841. {
  37842. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  37843. !defined(NO_RSA)
  37844. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  37845. {
  37846. FILE* f;
  37847. WOLFSSL_X509* x509;
  37848. X509_EXTENSION * ext = NULL;
  37849. int loc;
  37850. BIO *bio = NULL;
  37851. AssertNotNull(f = fopen(svrCertFile, "rb"));
  37852. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  37853. fclose(f);
  37854. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  37855. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  37856. AssertIntGT(loc, -1);
  37857. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  37858. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  37859. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  37860. AssertIntGT(loc, -1);
  37861. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  37862. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  37863. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  37864. AssertIntGT(loc, -1);
  37865. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  37866. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  37867. wolfSSL_BIO_free(bio);
  37868. wolfSSL_X509_free(x509);
  37869. }
  37870. {
  37871. X509 *x509;
  37872. BIO *bio;
  37873. X509_EXTENSION *ext;
  37874. unsigned int i;
  37875. unsigned int idx;
  37876. /* Some NIDs to test with */
  37877. int nids[] = {
  37878. /* NID_key_usage, currently X509_get_ext returns this as a bit
  37879. * string, which messes up X509V3_EXT_print */
  37880. /* NID_ext_key_usage, */
  37881. NID_subject_alt_name,
  37882. };
  37883. int* n;
  37884. AssertNotNull(bio = BIO_new_fp(stdout, BIO_NOCLOSE));
  37885. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  37886. WOLFSSL_FILETYPE_PEM));
  37887. printf("\nPrinting extension values:\n");
  37888. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  37889. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  37890. * update the index */
  37891. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  37892. AssertNotNull(ext = X509_get_ext(x509, idx));
  37893. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  37894. printf("\n");
  37895. }
  37896. BIO_free(bio);
  37897. X509_free(x509);
  37898. }
  37899. printf(resultFmt, passed);
  37900. #endif
  37901. }
  37902. static void test_wolfSSL_X509_cmp(void)
  37903. {
  37904. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  37905. FILE* file1;
  37906. FILE* file2;
  37907. WOLFSSL_X509* cert1;
  37908. WOLFSSL_X509* cert2;
  37909. int ret = 0;
  37910. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  37911. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  37912. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  37913. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  37914. fclose(file1);
  37915. fclose(file2);
  37916. printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
  37917. ret = wolfSSL_X509_cmp(cert1, cert1);
  37918. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  37919. printf(resultFmt, ret == 0 ? passed : failed);
  37920. printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
  37921. ret = wolfSSL_X509_cmp(cert1, cert2);
  37922. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  37923. printf(resultFmt, ret == -1 ? passed : failed);
  37924. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
  37925. ret = wolfSSL_X509_cmp(NULL, cert2);
  37926. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  37927. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  37928. printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
  37929. ret = wolfSSL_X509_cmp(cert1, NULL);
  37930. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  37931. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  37932. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
  37933. ret = wolfSSL_X509_cmp(NULL, NULL);
  37934. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  37935. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  37936. wolfSSL_X509_free(cert1);
  37937. wolfSSL_X509_free(cert2);
  37938. #endif
  37939. }
  37940. static void test_wolfSSL_PKEY_up_ref(void)
  37941. {
  37942. #if defined(OPENSSL_ALL)
  37943. EVP_PKEY* pkey;
  37944. printf(testingFmt, "wolfSSL_PKEY_up_ref()");
  37945. pkey = EVP_PKEY_new();
  37946. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  37947. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  37948. EVP_PKEY_free(pkey);
  37949. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  37950. EVP_PKEY_free(pkey);
  37951. EVP_PKEY_free(pkey);
  37952. printf(resultFmt, "passed");
  37953. #endif
  37954. }
  37955. static void test_wolfSSL_d2i_and_i2d_PublicKey(void)
  37956. {
  37957. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  37958. EVP_PKEY* pkey;
  37959. const unsigned char* p;
  37960. unsigned char* der = NULL;
  37961. int derLen;
  37962. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_PublicKey()");
  37963. p = client_keypub_der_2048;
  37964. /* Check that key can be successfully decoded. */
  37965. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  37966. sizeof_client_keypub_der_2048));
  37967. /* Check that key can be successfully encoded. */
  37968. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  37969. /* Ensure that the encoded version matches the original. */
  37970. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  37971. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  37972. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  37973. EVP_PKEY_free(pkey);
  37974. printf(resultFmt, passed);
  37975. #endif
  37976. }
  37977. static void test_wolfSSL_d2i_and_i2d_DSAparams(void)
  37978. {
  37979. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  37980. DSA* dsa;
  37981. char file[] = "./certs/dsaparams.der";
  37982. XFILE f;
  37983. int derInLen;
  37984. byte* derIn;
  37985. int derOutLen;
  37986. byte* derOut = NULL;
  37987. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_DSAparams()");
  37988. f = XFOPEN(file, "rb");
  37989. AssertTrue(f != XBADFILE);
  37990. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  37991. derInLen = (int)XFTELL(f);
  37992. XREWIND(f);
  37993. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  37994. DYNAMIC_TYPE_TMP_BUFFER));
  37995. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  37996. XFCLOSE(f);
  37997. /* Check that params can be successfully decoded. */
  37998. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  37999. /* Check that params can be successfully encoded. */
  38000. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  38001. /* Ensure that the encoded version matches the original. */
  38002. AssertIntEQ(derInLen, derOutLen);
  38003. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  38004. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38005. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  38006. DSA_free(dsa);
  38007. printf(resultFmt, passed);
  38008. #endif
  38009. }
  38010. static void test_wolfSSL_i2d_PrivateKey(void)
  38011. {
  38012. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  38013. printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
  38014. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  38015. {
  38016. EVP_PKEY* pkey;
  38017. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  38018. unsigned char buf[FOURK_BUF];
  38019. unsigned char* pt = NULL;
  38020. int bufSz;
  38021. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  38022. (long)sizeof_server_key_der_2048));
  38023. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  38024. pt = buf;
  38025. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  38026. AssertIntNE((pt - buf), 0);
  38027. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  38028. EVP_PKEY_free(pkey);
  38029. }
  38030. #endif
  38031. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  38032. {
  38033. EVP_PKEY* pkey;
  38034. const unsigned char* client_key =
  38035. (const unsigned char*)ecc_clikey_der_256;
  38036. unsigned char buf[FOURK_BUF];
  38037. unsigned char* pt = NULL;
  38038. int bufSz;
  38039. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  38040. sizeof_ecc_clikey_der_256)));
  38041. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  38042. pt = buf;
  38043. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  38044. AssertIntNE((pt - buf), 0);
  38045. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  38046. EVP_PKEY_free(pkey);
  38047. }
  38048. #endif
  38049. printf(resultFmt, "passed");
  38050. #endif
  38051. }
  38052. static void test_wolfSSL_OCSP_id_get0_info(void)
  38053. {
  38054. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  38055. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38056. X509* cert;
  38057. X509* issuer;
  38058. OCSP_CERTID* id;
  38059. OCSP_CERTID* id2;
  38060. ASN1_STRING* name = NULL;
  38061. ASN1_OBJECT* pmd = NULL;
  38062. ASN1_STRING* keyHash = NULL;
  38063. ASN1_INTEGER* serial = NULL;
  38064. ASN1_INTEGER* x509Int;
  38065. printf(testingFmt, "wolfSSL_OCSP_id_get0_info()");
  38066. AssertNotNull(cert =
  38067. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  38068. AssertNotNull(issuer =
  38069. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  38070. id = OCSP_cert_to_id(NULL, cert, issuer);
  38071. AssertNotNull(id);
  38072. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  38073. AssertNotNull(id2);
  38074. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  38075. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  38076. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  38077. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  38078. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  38079. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  38080. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  38081. AssertNotNull(serial);
  38082. /* compare serial number to one in cert, should be equal */
  38083. x509Int = X509_get_serialNumber(cert);
  38084. AssertNotNull(x509Int);
  38085. AssertIntEQ(x509Int->length, serial->length);
  38086. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  38087. /* test OCSP_id_cmp */
  38088. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  38089. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  38090. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  38091. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  38092. id->issuerHash[0] = ~id->issuerHash[0];
  38093. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  38094. OCSP_CERTID_free(id);
  38095. OCSP_CERTID_free(id2);
  38096. X509_free(cert); /* free's x509Int */
  38097. X509_free(issuer);
  38098. printf(resultFmt, "passed");
  38099. #endif
  38100. }
  38101. static void test_wolfSSL_i2d_OCSP_CERTID(void)
  38102. {
  38103. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  38104. WOLFSSL_OCSP_CERTID certId;
  38105. byte* targetBuffer;
  38106. byte* beginTargetBuffer;
  38107. /* OCSP CertID bytes taken from PCAP */
  38108. byte rawCertId[] = {
  38109. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  38110. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  38111. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  38112. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  38113. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  38114. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  38115. 0xcf, 0xbc, 0x91
  38116. };
  38117. int ret, i;
  38118. printf(testingFmt, "wolfSSL_i2d_OCSP_CERTID()");
  38119. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  38120. certId.rawCertId = rawCertId;
  38121. certId.rawCertIdSize = sizeof(rawCertId);
  38122. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38123. beginTargetBuffer = targetBuffer;
  38124. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  38125. /* If target buffer is not null, function increments targetBuffer to point
  38126. just past the end of the encoded data. */
  38127. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  38128. /* Function returns the size of the encoded data. */
  38129. AssertIntEQ(ret, sizeof(rawCertId));
  38130. for (i = 0; i < ret; ++i)
  38131. {
  38132. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  38133. }
  38134. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38135. targetBuffer = NULL;
  38136. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  38137. /* If target buffer is null, function allocates memory for a buffer and
  38138. copies the encoded data into it. targetBuffer then points to the start of
  38139. this newly allocate buffer. */
  38140. AssertIntEQ(ret, sizeof(rawCertId));
  38141. for (i = 0; i < ret; ++i)
  38142. {
  38143. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  38144. }
  38145. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  38146. printf(resultFmt, passed);
  38147. #endif
  38148. }
  38149. static void test_wolfSSL_OCSP_id_cmp(void)
  38150. {
  38151. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  38152. OCSP_CERTID id1;
  38153. OCSP_CERTID id2;
  38154. printf(testingFmt, "wolfSSL_OCSP_id_cmp()");
  38155. XMEMSET(&id1, 0, sizeof(id1));
  38156. XMEMSET(&id2, 0, sizeof(id2));
  38157. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  38158. printf(resultFmt, passed);
  38159. #endif
  38160. }
  38161. static void test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  38162. {
  38163. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  38164. WOLFSSL_OCSP_SINGLERESP single;
  38165. const WOLFSSL_OCSP_CERTID* certId;
  38166. XMEMSET(&single, 0, sizeof(single));
  38167. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  38168. printf(testingFmt, "wolfSSL_OCSP_SINGLERESP_get0_id()");
  38169. AssertPtrEq(&single, certId);
  38170. printf(resultFmt, passed);
  38171. #endif
  38172. }
  38173. static void test_wolfSSL_OCSP_single_get0_status(void)
  38174. {
  38175. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  38176. WOLFSSL_OCSP_SINGLERESP single;
  38177. CertStatus certStatus;
  38178. WOLFSSL_ASN1_TIME* thisDate;
  38179. WOLFSSL_ASN1_TIME* nextDate;
  38180. int ret, i;
  38181. printf(testingFmt, "wolfSSL_OCSP_single_get0_status()");
  38182. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  38183. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  38184. /* Fill the date fields with some dummy data. */
  38185. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  38186. certStatus.thisDateParsed.data[i] = i;
  38187. certStatus.nextDateParsed.data[i] = i;
  38188. }
  38189. certStatus.status = CERT_GOOD;
  38190. single.status = &certStatus;
  38191. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  38192. &nextDate);
  38193. AssertIntEQ(ret, CERT_GOOD);
  38194. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  38195. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  38196. printf(resultFmt, passed);
  38197. #endif
  38198. }
  38199. static void test_wolfSSL_OCSP_resp_count(void)
  38200. {
  38201. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  38202. WOLFSSL_OCSP_BASICRESP basicResp;
  38203. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  38204. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  38205. int count;
  38206. printf(testingFmt, "wolfSSL_OCSP_resp_count()");
  38207. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  38208. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  38209. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  38210. count = wolfSSL_OCSP_resp_count(&basicResp);
  38211. AssertIntEQ(count, 0);
  38212. basicResp.single = &singleRespOne;
  38213. count = wolfSSL_OCSP_resp_count(&basicResp);
  38214. AssertIntEQ(count, 1);
  38215. singleRespOne.next = &singleRespTwo;
  38216. count = wolfSSL_OCSP_resp_count(&basicResp);
  38217. AssertIntEQ(count, 2);
  38218. printf(resultFmt, passed);
  38219. #endif
  38220. }
  38221. static void test_wolfSSL_OCSP_resp_get0(void)
  38222. {
  38223. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  38224. WOLFSSL_OCSP_BASICRESP basicResp;
  38225. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  38226. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  38227. WOLFSSL_OCSP_SINGLERESP* ret;
  38228. printf(testingFmt, "wolfSSL_OCSP_resp_get0()");
  38229. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  38230. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  38231. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  38232. basicResp.single = &singleRespOne;
  38233. singleRespOne.next = &singleRespTwo;
  38234. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  38235. AssertPtrEq(ret, &singleRespOne);
  38236. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  38237. AssertPtrEq(ret, &singleRespTwo);
  38238. printf(resultFmt, passed);
  38239. #endif
  38240. }
  38241. static void test_wolfSSL_EVP_PKEY_derive(void)
  38242. {
  38243. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  38244. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38245. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  38246. printf(testingFmt, "wolfSSL_EVP_PKEY_derive()");
  38247. EVP_PKEY_CTX *ctx;
  38248. unsigned char *skey;
  38249. size_t skeylen;
  38250. EVP_PKEY *pkey, *peerkey;
  38251. const unsigned char* key;
  38252. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  38253. /* DH */
  38254. key = dh_key_der_2048;
  38255. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  38256. sizeof_dh_key_der_2048)));
  38257. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  38258. key = dh_key_der_2048;
  38259. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  38260. sizeof_dh_key_der_2048)));
  38261. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  38262. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38263. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  38264. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  38265. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  38266. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  38267. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  38268. EVP_PKEY_CTX_free(ctx);
  38269. EVP_PKEY_free(peerkey);
  38270. EVP_PKEY_free(pkey);
  38271. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  38272. #endif
  38273. #ifdef HAVE_ECC
  38274. /* ECDH */
  38275. key = ecc_clikey_der_256;
  38276. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  38277. sizeof_ecc_clikey_der_256)));
  38278. key = ecc_clikeypub_der_256;
  38279. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  38280. sizeof_ecc_clikeypub_der_256)));
  38281. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38282. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  38283. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  38284. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  38285. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  38286. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  38287. EVP_PKEY_CTX_free(ctx);
  38288. EVP_PKEY_free(peerkey);
  38289. EVP_PKEY_free(pkey);
  38290. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  38291. #endif /* HAVE_ECC */
  38292. printf(resultFmt, "passed");
  38293. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  38294. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38295. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  38296. }
  38297. static void test_wolfSSL_EVP_PBE_scrypt(void)
  38298. {
  38299. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  38300. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  38301. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  38302. int ret;
  38303. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  38304. int pwdlen = sizeof(pwd);
  38305. const byte salt[] = {'N','a','C','l'};
  38306. int saltlen = sizeof(salt);
  38307. byte key[80];
  38308. word64 numOvr32 = (word64)INT32_MAX + 1;
  38309. /* expected derived key for N:16, r:1, p:1 */
  38310. const byte expectedKey[] = {
  38311. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  38312. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  38313. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  38314. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  38315. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  38316. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  38317. 0xE7, 0xE9, 0xC0, 0x9A};
  38318. printf(testingFmt, "wolfSSL_EVP_PBE_scrypt()");
  38319. /* N r p mx key keylen */
  38320. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  38321. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  38322. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  38323. AssertIntEQ(ret, 0); /* N must be power of 2 */
  38324. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  38325. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  38326. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  38327. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  38328. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  38329. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  38330. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  38331. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  38332. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  38333. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  38334. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  38335. AssertIntEQ(ret, 1); /* should succeed */
  38336. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  38337. key, 64);
  38338. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  38339. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  38340. key, 64);
  38341. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  38342. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  38343. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  38344. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  38345. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  38346. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  38347. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  38348. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  38349. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  38350. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  38351. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  38352. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  38353. AssertIntEQ(ret, 1);
  38354. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  38355. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  38356. printf(resultFmt, "passed");
  38357. #endif /* !NO_PWDBASED && !NO_SHA256 */
  38358. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  38359. }
  38360. #ifndef NO_RSA
  38361. static void test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  38362. {
  38363. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  38364. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38365. RSA *rsa;
  38366. const unsigned char *derBuf = client_key_der_2048;
  38367. unsigned char em[256] = {0}; /* len = 2048/8 */
  38368. /* Random data simulating a hash */
  38369. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  38370. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  38371. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  38372. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  38373. };
  38374. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  38375. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -1), 1);
  38376. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -1), 1);
  38377. RSA_free(rsa);
  38378. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38379. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  38380. }
  38381. #endif
  38382. static void test_wolfSSL_RSA_sign_sha3(void)
  38383. {
  38384. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  38385. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  38386. RSA *rsa;
  38387. const unsigned char *derBuf = client_key_der_2048;
  38388. unsigned char sigRet[256] = {0};
  38389. unsigned int sigLen = sizeof(sigRet);
  38390. /* Random data simulating a hash */
  38391. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  38392. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  38393. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  38394. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  38395. };
  38396. printf(testingFmt, "wolfSSL_RSA_sign_sha3");
  38397. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  38398. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  38399. &sigLen, rsa), 1);
  38400. RSA_free(rsa);
  38401. printf(resultFmt, passed);
  38402. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  38403. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  38404. }
  38405. static void test_wolfSSL_EC_get_builtin_curves(void)
  38406. {
  38407. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  38408. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38409. EC_builtin_curve* curves = NULL;
  38410. size_t crv_len = 0;
  38411. size_t i = 0;
  38412. printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
  38413. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  38414. AssertNotNull(curves = (EC_builtin_curve*)
  38415. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  38416. DYNAMIC_TYPE_TMP_BUFFER));
  38417. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  38418. for (i = 0; i < crv_len; i++)
  38419. {
  38420. if (curves[i].comment != NULL)
  38421. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  38422. }
  38423. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38424. printf(resultFmt, passed);
  38425. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38426. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  38427. }
  38428. static void test_no_op_functions(void)
  38429. {
  38430. #if defined(OPENSSL_EXTRA)
  38431. printf(testingFmt, "no_op_functions()");
  38432. /* this makes sure wolfSSL can compile and run these no-op functions */
  38433. SSL_load_error_strings();
  38434. ENGINE_load_builtin_engines();
  38435. OpenSSL_add_all_ciphers();
  38436. AssertIntEQ(CRYPTO_malloc_init(), 0);
  38437. printf(resultFmt, passed);
  38438. #endif
  38439. }
  38440. static void test_wolfSSL_CRYPTO_memcmp(void)
  38441. {
  38442. #ifdef OPENSSL_EXTRA
  38443. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  38444. "implementation of TLS/SSL for embedded devices to the cloud.";
  38445. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  38446. "implementation of TLS/SSL for embedded devices to the cloud.";
  38447. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  38448. "implementation of TLS/SSL for embedded devices to the cloud!";
  38449. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  38450. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  38451. #endif
  38452. }
  38453. /*----------------------------------------------------------------------------*
  38454. | wolfCrypt ASN
  38455. *----------------------------------------------------------------------------*/
  38456. static void test_wc_CreateEncryptedPKCS8Key(void)
  38457. {
  38458. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  38459. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  38460. WC_RNG rng;
  38461. byte* encKey = NULL;
  38462. word32 encKeySz = 0;
  38463. word32 decKeySz = 0;
  38464. const char password[] = "Lorem ipsum dolor sit amet";
  38465. word32 passwordSz = (word32)XSTRLEN(password);
  38466. word32 tradIdx = 0;
  38467. printf(testingFmt, "test_wc_CreateEncryptedPKCS8Key");
  38468. AssertIntEQ(wc_InitRng(&rng), 0);
  38469. /* Call with NULL for out buffer to get necessary length. */
  38470. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  38471. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  38472. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  38473. LENGTH_ONLY_E);
  38474. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  38475. DYNAMIC_TYPE_TMP_BUFFER));
  38476. /* Call with the allocated out buffer. */
  38477. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  38478. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  38479. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  38480. 0);
  38481. /* Decrypt the encrypted PKCS8 key we just made. */
  38482. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  38483. passwordSz)), 0);
  38484. /* encKey now holds the decrypted key (decrypted in place). */
  38485. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  38486. /* Check that the decrypted key matches the key prior to encryption. */
  38487. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  38488. sizeof_server_key_der_2048), 0);
  38489. if (encKey != NULL)
  38490. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38491. wc_FreeRng(&rng);
  38492. printf(resultFmt, passed);
  38493. #endif
  38494. }
  38495. static void test_wc_GetPkcs8TraditionalOffset(void)
  38496. {
  38497. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  38498. int length, derSz;
  38499. word32 inOutIdx;
  38500. const char* path = "./certs/server-keyPkcs8.der";
  38501. XFILE file;
  38502. byte der[2048];
  38503. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  38504. file = XFOPEN(path, "rb");
  38505. AssertTrue(file != XBADFILE);
  38506. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  38507. XFCLOSE(file);
  38508. /* valid case */
  38509. inOutIdx = 0;
  38510. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  38511. AssertIntGT(length, 0);
  38512. /* inOutIdx > sz */
  38513. inOutIdx = 4000;
  38514. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  38515. AssertIntEQ(length, BAD_FUNC_ARG);
  38516. /* null input */
  38517. inOutIdx = 0;
  38518. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  38519. AssertIntEQ(length, BAD_FUNC_ARG);
  38520. /* invalid input, fill buffer with 1's */
  38521. XMEMSET(der, 1, sizeof(der));
  38522. inOutIdx = 0;
  38523. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  38524. AssertIntEQ(length, ASN_PARSE_E);
  38525. printf(resultFmt, passed);
  38526. #endif /* NO_ASN */
  38527. }
  38528. static void test_wc_SetSubjectRaw(void)
  38529. {
  38530. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  38531. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  38532. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  38533. WOLFSSL_X509* x509;
  38534. int peerCertSz;
  38535. const byte* peerCertBuf;
  38536. Cert forgedCert;
  38537. printf(testingFmt, "test_wc_SetSubjectRaw()");
  38538. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  38539. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  38540. AssertIntEQ(0, wc_InitCert(&forgedCert));
  38541. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  38542. wolfSSL_FreeX509(x509);
  38543. printf(resultFmt, passed);
  38544. #endif
  38545. }
  38546. static void test_wc_GetSubjectRaw(void)
  38547. {
  38548. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  38549. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  38550. Cert cert;
  38551. byte *subjectRaw;
  38552. printf(testingFmt, "test_wc_GetSubjectRaw()");
  38553. AssertIntEQ(0, wc_InitCert(&cert));
  38554. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  38555. printf(resultFmt, passed);
  38556. #endif
  38557. }
  38558. static void test_wc_SetIssuerRaw(void)
  38559. {
  38560. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  38561. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  38562. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  38563. WOLFSSL_X509* x509;
  38564. int peerCertSz;
  38565. const byte* peerCertBuf;
  38566. Cert forgedCert;
  38567. printf(testingFmt, "test_wc_SetIssuerRaw()");
  38568. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  38569. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  38570. AssertIntEQ(0, wc_InitCert(&forgedCert));
  38571. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  38572. wolfSSL_FreeX509(x509);
  38573. printf(resultFmt, passed);
  38574. #endif
  38575. }
  38576. static void test_wc_SetIssueBuffer(void)
  38577. {
  38578. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  38579. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  38580. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  38581. WOLFSSL_X509* x509;
  38582. int peerCertSz;
  38583. const byte* peerCertBuf;
  38584. Cert forgedCert;
  38585. printf(testingFmt, "test_wc_SetIssuerBuffer()");
  38586. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  38587. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  38588. AssertIntEQ(0, wc_InitCert(&forgedCert));
  38589. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  38590. wolfSSL_FreeX509(x509);
  38591. printf(resultFmt, passed);
  38592. #endif
  38593. }
  38594. /*
  38595. * Testing wc_SetSubjectKeyId
  38596. */
  38597. static void test_wc_SetSubjectKeyId(void)
  38598. {
  38599. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  38600. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  38601. Cert cert;
  38602. const char* file = "certs/ecc-client-keyPub.pem";
  38603. printf(testingFmt, "wc_SetSubjectKeyId()");
  38604. AssertIntEQ(0, wc_InitCert(&cert));
  38605. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  38606. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  38607. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  38608. printf(resultFmt, passed);
  38609. #endif
  38610. } /* END test_wc_SetSubjectKeyId */
  38611. /*
  38612. * Testing wc_SetSubject
  38613. */
  38614. static void test_wc_SetSubject(void)
  38615. {
  38616. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  38617. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  38618. Cert cert;
  38619. const char* file = "./certs/ca-ecc-cert.pem";
  38620. printf(testingFmt, "wc_SetSubject()");
  38621. AssertIntEQ(0, wc_InitCert(&cert));
  38622. AssertIntEQ(0, wc_SetSubject(&cert, file));
  38623. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  38624. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  38625. printf(resultFmt, passed);
  38626. #endif
  38627. } /* END test_wc_SetSubject */
  38628. static void test_CheckCertSignature(void)
  38629. {
  38630. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  38631. WOLFSSL_CERT_MANAGER* cm = NULL;
  38632. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  38633. FILE* fp;
  38634. byte cert[4096];
  38635. int certSz;
  38636. #endif
  38637. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  38638. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  38639. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  38640. #ifndef NO_RSA
  38641. #ifdef USE_CERT_BUFFERS_1024
  38642. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  38643. sizeof_server_cert_der_1024, NULL, cm));
  38644. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  38645. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  38646. WOLFSSL_FILETYPE_ASN1));
  38647. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  38648. sizeof_server_cert_der_1024, NULL, cm));
  38649. #elif defined(USE_CERT_BUFFERS_2048)
  38650. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  38651. sizeof_server_cert_der_2048, NULL, cm));
  38652. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  38653. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  38654. WOLFSSL_FILETYPE_ASN1));
  38655. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  38656. sizeof_server_cert_der_2048, NULL, cm));
  38657. #endif
  38658. #endif
  38659. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  38660. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  38661. sizeof_serv_ecc_der_256, NULL, cm));
  38662. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  38663. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  38664. WOLFSSL_FILETYPE_ASN1));
  38665. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  38666. NULL, cm));
  38667. #endif
  38668. #if !defined(NO_FILESYSTEM)
  38669. wolfSSL_CertManagerFree(cm);
  38670. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  38671. #ifndef NO_RSA
  38672. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  38673. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  38674. XFCLOSE(fp);
  38675. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  38676. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  38677. "./certs/ca-cert.pem", NULL));
  38678. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  38679. #endif
  38680. #ifdef HAVE_ECC
  38681. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  38682. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  38683. XFCLOSE(fp);
  38684. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  38685. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  38686. "./certs/ca-ecc-cert.pem", NULL));
  38687. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  38688. #endif
  38689. #endif
  38690. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  38691. (void)fp;
  38692. (void)cert;
  38693. (void)certSz;
  38694. #endif
  38695. wolfSSL_CertManagerFree(cm);
  38696. #endif
  38697. }
  38698. /*----------------------------------------------------------------------------*
  38699. | wolfCrypt ECC
  38700. *----------------------------------------------------------------------------*/
  38701. static void test_wc_ecc_get_curve_size_from_name(void)
  38702. {
  38703. #ifdef HAVE_ECC
  38704. int ret;
  38705. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  38706. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  38707. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  38708. AssertIntEQ(ret, 32);
  38709. #endif
  38710. /* invalid case */
  38711. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  38712. AssertIntEQ(ret, -1);
  38713. /* NULL input */
  38714. ret = wc_ecc_get_curve_size_from_name(NULL);
  38715. AssertIntEQ(ret, BAD_FUNC_ARG);
  38716. printf(resultFmt, passed);
  38717. #endif /* HAVE_ECC */
  38718. }
  38719. static void test_wc_ecc_get_curve_id_from_name(void)
  38720. {
  38721. #ifdef HAVE_ECC
  38722. int id;
  38723. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  38724. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  38725. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  38726. AssertIntEQ(id, ECC_SECP256R1);
  38727. #endif
  38728. /* invalid case */
  38729. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  38730. AssertIntEQ(id, -1);
  38731. /* NULL input */
  38732. id = wc_ecc_get_curve_id_from_name(NULL);
  38733. AssertIntEQ(id, BAD_FUNC_ARG);
  38734. printf(resultFmt, passed);
  38735. #endif /* HAVE_ECC */
  38736. }
  38737. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  38738. !defined(HAVE_SELFTEST) && \
  38739. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  38740. static void test_wc_ecc_get_curve_id_from_dp_params(void)
  38741. {
  38742. int id;
  38743. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  38744. int curve_id;
  38745. ecc_key* key;
  38746. const ecc_set_type* params;
  38747. int ret;
  38748. #endif
  38749. WOLFSSL_EC_KEY *ecKey = NULL;
  38750. printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
  38751. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  38752. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  38753. AssertIntEQ(id, ECC_SECP256R1);
  38754. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  38755. AssertNotNull(ecKey);
  38756. ret = EC_KEY_generate_key(ecKey);
  38757. if (ret == 0) {
  38758. /* normal test */
  38759. key = (ecc_key*)ecKey->internal;
  38760. params = key->dp;
  38761. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  38762. AssertIntEQ(curve_id, id);
  38763. }
  38764. #endif
  38765. /* invalid case, NULL input*/
  38766. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  38767. AssertIntEQ(id, BAD_FUNC_ARG);
  38768. wolfSSL_EC_KEY_free(ecKey);
  38769. printf(resultFmt, passed);
  38770. }
  38771. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  38772. static void test_wc_ecc_get_curve_id_from_params(void)
  38773. {
  38774. #ifdef HAVE_ECC
  38775. int id;
  38776. const byte prime[] =
  38777. {
  38778. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  38779. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38780. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  38781. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  38782. };
  38783. const byte primeInvalid[] =
  38784. {
  38785. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  38786. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38787. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  38788. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  38789. };
  38790. const byte Af[] =
  38791. {
  38792. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  38793. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38794. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  38795. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  38796. };
  38797. const byte Bf[] =
  38798. {
  38799. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  38800. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  38801. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  38802. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  38803. };
  38804. const byte order[] =
  38805. {
  38806. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  38807. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  38808. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  38809. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  38810. };
  38811. const byte Gx[] =
  38812. {
  38813. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  38814. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  38815. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  38816. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  38817. };
  38818. const byte Gy[] =
  38819. {
  38820. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  38821. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  38822. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  38823. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  38824. };
  38825. int cofactor = 1;
  38826. int fieldSize = 256;
  38827. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  38828. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  38829. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  38830. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  38831. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  38832. AssertIntEQ(id, ECC_SECP256R1);
  38833. #endif
  38834. /* invalid case, fieldSize = 0 */
  38835. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  38836. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  38837. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  38838. AssertIntEQ(id, ECC_CURVE_INVALID);
  38839. /* invalid case, NULL prime */
  38840. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  38841. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  38842. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  38843. AssertIntEQ(id, BAD_FUNC_ARG);
  38844. /* invalid case, invalid prime */
  38845. id = wc_ecc_get_curve_id_from_params(fieldSize,
  38846. primeInvalid, sizeof(primeInvalid),
  38847. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  38848. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  38849. AssertIntEQ(id, ECC_CURVE_INVALID);
  38850. printf(resultFmt, passed);
  38851. #endif
  38852. }
  38853. static void test_wolfSSL_EVP_PKEY_encrypt(void)
  38854. {
  38855. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  38856. !defined(HAVE_FAST_RSA)
  38857. WOLFSSL_RSA* rsa = NULL;
  38858. WOLFSSL_EVP_PKEY* pkey = NULL;
  38859. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  38860. const char* in = "What is easy to do is easy not to do.";
  38861. size_t inlen = XSTRLEN(in);
  38862. size_t outEncLen = 0;
  38863. byte* outEnc = NULL;
  38864. byte* outDec = NULL;
  38865. size_t outDecLen = 0;
  38866. size_t rsaKeySz = 2048/8; /* Bytes */
  38867. #ifdef WC_RSA_NO_PADDING
  38868. byte* inTmp = NULL;
  38869. byte* outEncTmp = NULL;
  38870. byte* outDecTmp = NULL;
  38871. #endif
  38872. printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
  38873. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  38874. XMEMSET(outEnc, 0, rsaKeySz);
  38875. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  38876. XMEMSET(outDec, 0, rsaKeySz);
  38877. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  38878. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38879. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  38880. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38881. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  38882. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  38883. WOLFSSL_SUCCESS);
  38884. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  38885. since ctx uses it.*/
  38886. AssertIntEQ(pkey->references, 2);
  38887. EVP_PKEY_free(pkey);
  38888. AssertIntEQ(pkey->references, 1);
  38889. /* Encrypt data */
  38890. /* Check that we can get the required output buffer length by passing in a
  38891. * NULL output buffer. */
  38892. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  38893. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  38894. AssertIntEQ(rsaKeySz, outEncLen);
  38895. /* Now do the actual encryption. */
  38896. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  38897. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  38898. /* Decrypt data */
  38899. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  38900. /* Check that we can get the required output buffer length by passing in a
  38901. * NULL output buffer. */
  38902. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  38903. WOLFSSL_SUCCESS);
  38904. AssertIntEQ(rsaKeySz, outDecLen);
  38905. /* Now do the actual decryption. */
  38906. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  38907. WOLFSSL_SUCCESS);
  38908. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  38909. #ifdef WC_RSA_NO_PADDING
  38910. /* The input length must be the same size as the RSA key.*/
  38911. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  38912. XMEMSET(inTmp, 9, rsaKeySz);
  38913. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  38914. XMEMSET(outEncTmp, 0, rsaKeySz);
  38915. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  38916. XMEMSET(outDecTmp, 0, rsaKeySz);
  38917. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  38918. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  38919. WOLFSSL_SUCCESS);
  38920. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  38921. WOLFSSL_SUCCESS);
  38922. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  38923. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  38924. WOLFSSL_SUCCESS);
  38925. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  38926. #endif
  38927. EVP_PKEY_CTX_free(ctx);
  38928. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38929. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38930. #ifdef WC_RSA_NO_PADDING
  38931. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38932. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38933. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38934. #endif
  38935. printf(resultFmt, passed);
  38936. #endif
  38937. }
  38938. static void test_wolfSSL_EVP_PKEY_sign(void)
  38939. {
  38940. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  38941. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  38942. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38943. WOLFSSL_RSA* rsa = NULL;
  38944. WOLFSSL_EVP_PKEY* pkey = NULL;
  38945. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  38946. const char* in = "What is easy to do is easy not to do.";
  38947. size_t inlen = XSTRLEN(in);
  38948. byte hash[SHA256_DIGEST_LENGTH] = {0};
  38949. SHA256_CTX c;
  38950. byte* sig = NULL;
  38951. byte* sigVerify = NULL;
  38952. size_t siglen = 0;
  38953. size_t rsaKeySz = 2048/8; /* Bytes */
  38954. printf(testingFmt, "wolfSSL_EVP_PKEY_sign()");
  38955. sig = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38956. AssertNotNull(sig);
  38957. XMEMSET(sig, 0, rsaKeySz);
  38958. AssertNotNull(sigVerify = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  38959. XMEMSET(sigVerify, 0, rsaKeySz);
  38960. /* Generate hash */
  38961. SHA256_Init(&c);
  38962. SHA256_Update(&c, in, inlen);
  38963. SHA256_Final(hash, &c);
  38964. #ifdef WOLFSSL_SMALL_STACK_CACHE
  38965. /* workaround for small stack cache case */
  38966. wc_Sha256Free((wc_Sha256*)&c);
  38967. #endif
  38968. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  38969. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38970. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  38971. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38972. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  38973. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  38974. WOLFSSL_SUCCESS);
  38975. /* Sign data */
  38976. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash, SHA256_DIGEST_LENGTH),
  38977. WOLFSSL_SUCCESS);
  38978. /* Verify signature.
  38979. EVP_PKEY_verify() doesn't exist yet, so use RSA_public_decrypt(). */
  38980. AssertIntEQ(RSA_public_decrypt((int)siglen, sig, sigVerify,
  38981. rsa, RSA_PKCS1_PADDING), SHA256_DIGEST_LENGTH);
  38982. AssertIntEQ(XMEMCMP(hash, sigVerify, SHA256_DIGEST_LENGTH), 0);
  38983. /* error cases */
  38984. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  38985. ctx->pkey->type = EVP_PKEY_RSA;
  38986. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  38987. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  38988. WOLFSSL_SUCCESS);
  38989. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  38990. WOLFSSL_SUCCESS);
  38991. EVP_PKEY_free(pkey);
  38992. EVP_PKEY_CTX_free(ctx);
  38993. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38994. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38995. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38996. printf(resultFmt, passed);
  38997. #endif
  38998. }
  38999. static void test_EVP_PKEY_rsa(void)
  39000. {
  39001. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  39002. WOLFSSL_RSA* rsa;
  39003. WOLFSSL_EVP_PKEY* pkey;
  39004. AssertNotNull(rsa = wolfSSL_RSA_new());
  39005. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39006. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  39007. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  39008. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  39009. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  39010. wolfSSL_EVP_PKEY_free(pkey);
  39011. printf(resultFmt, passed);
  39012. #endif
  39013. }
  39014. static void test_EVP_PKEY_ec(void)
  39015. {
  39016. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  39017. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  39018. WOLFSSL_EC_KEY* ecKey;
  39019. WOLFSSL_EVP_PKEY* pkey;
  39020. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  39021. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39022. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  39023. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  39024. /* Should fail since ecKey is empty */
  39025. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  39026. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  39027. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  39028. wolfSSL_EVP_PKEY_free(pkey);
  39029. printf(resultFmt, passed);
  39030. #endif
  39031. #endif
  39032. }
  39033. static void test_EVP_PKEY_cmp(void)
  39034. {
  39035. #if defined(OPENSSL_EXTRA)
  39036. EVP_PKEY *a, *b;
  39037. const unsigned char *in;
  39038. printf(testingFmt, "wolfSSL_EVP_PKEY_cmp()");
  39039. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  39040. in = client_key_der_2048;
  39041. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39042. &in, (long)sizeof_client_key_der_2048));
  39043. in = client_key_der_2048;
  39044. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39045. &in, (long)sizeof_client_key_der_2048));
  39046. /* Test success case RSA */
  39047. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39048. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  39049. #else
  39050. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  39051. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  39052. EVP_PKEY_free(b);
  39053. EVP_PKEY_free(a);
  39054. #endif
  39055. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  39056. in = ecc_clikey_der_256;
  39057. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  39058. &in, (long)sizeof_ecc_clikey_der_256));
  39059. in = ecc_clikey_der_256;
  39060. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  39061. &in, (long)sizeof_ecc_clikey_der_256));
  39062. /* Test success case ECC */
  39063. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39064. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  39065. #else
  39066. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  39067. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  39068. EVP_PKEY_free(b);
  39069. EVP_PKEY_free(a);
  39070. #endif
  39071. /* Test failure cases */
  39072. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  39073. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  39074. in = client_key_der_2048;
  39075. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39076. &in, (long)sizeof_client_key_der_2048));
  39077. in = ecc_clikey_der_256;
  39078. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  39079. &in, (long)sizeof_ecc_clikey_der_256));
  39080. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39081. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  39082. #else
  39083. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  39084. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  39085. EVP_PKEY_free(b);
  39086. EVP_PKEY_free(a);
  39087. #endif
  39088. /* invalid or empty failure cases */
  39089. a = EVP_PKEY_new();
  39090. b = EVP_PKEY_new();
  39091. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  39092. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  39093. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  39094. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  39095. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  39096. #else
  39097. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  39098. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  39099. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  39100. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  39101. #endif
  39102. EVP_PKEY_free(b);
  39103. EVP_PKEY_free(a);
  39104. (void)in;
  39105. printf(resultFmt, passed);
  39106. #endif
  39107. }
  39108. static void test_ERR_load_crypto_strings(void)
  39109. {
  39110. #if defined(OPENSSL_ALL)
  39111. ERR_load_crypto_strings();
  39112. printf(resultFmt, passed);
  39113. #endif
  39114. }
  39115. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  39116. static void free_x509(X509* x)
  39117. {
  39118. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  39119. }
  39120. #endif
  39121. static void test_sk_X509(void)
  39122. {
  39123. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  39124. STACK_OF(X509)* s;
  39125. AssertNotNull(s = sk_X509_new());
  39126. AssertIntEQ(sk_X509_num(s), 0);
  39127. sk_X509_pop_free(s, NULL);
  39128. AssertNotNull(s = sk_X509_new_null());
  39129. AssertIntEQ(sk_X509_num(s), 0);
  39130. sk_X509_pop_free(s, NULL);
  39131. AssertNotNull(s = sk_X509_new());
  39132. sk_X509_push(s, (X509*)1);
  39133. AssertIntEQ(sk_X509_num(s), 1);
  39134. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  39135. sk_X509_push(s, (X509*)2);
  39136. AssertIntEQ(sk_X509_num(s), 2);
  39137. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  39138. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  39139. sk_X509_push(s, (X509*)2);
  39140. sk_X509_pop_free(s, free_x509);
  39141. printf(resultFmt, passed);
  39142. #endif
  39143. }
  39144. static void test_X509_get_signature_nid(void)
  39145. {
  39146. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  39147. X509* x509;
  39148. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  39149. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  39150. SSL_FILETYPE_PEM));
  39151. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  39152. X509_free(x509);
  39153. printf(resultFmt, passed);
  39154. #endif
  39155. }
  39156. static void test_X509_REQ(void)
  39157. {
  39158. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  39159. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  39160. X509_NAME* name;
  39161. #if !defined(NO_RSA) || defined(HAVE_ECC)
  39162. X509_REQ* req;
  39163. EVP_PKEY* priv;
  39164. EVP_PKEY* pub;
  39165. unsigned char* der = NULL;
  39166. #endif
  39167. #ifndef NO_RSA
  39168. EVP_MD_CTX *mctx = NULL;
  39169. EVP_PKEY_CTX *pkctx = NULL;
  39170. #ifdef USE_CERT_BUFFERS_1024
  39171. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  39172. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  39173. #elif defined(USE_CERT_BUFFERS_2048)
  39174. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  39175. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  39176. #endif
  39177. #endif
  39178. #ifdef HAVE_ECC
  39179. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  39180. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  39181. int len;
  39182. #endif
  39183. AssertNotNull(name = X509_NAME_new());
  39184. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  39185. (byte*)"wolfssl.com", 11, 0, 1),
  39186. WOLFSSL_SUCCESS);
  39187. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  39188. (byte*)"support@wolfssl.com", 19, -1,
  39189. 1), WOLFSSL_SUCCESS);
  39190. #ifndef NO_RSA
  39191. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  39192. (long)sizeof_client_key_der_2048));
  39193. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  39194. (long)sizeof_client_keypub_der_2048));
  39195. AssertNotNull(req = X509_REQ_new());
  39196. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  39197. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  39198. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  39199. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  39200. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  39201. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  39202. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  39203. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  39204. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  39205. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  39206. AssertIntEQ(i2d_X509_REQ(req, &der), 643);
  39207. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  39208. der = NULL;
  39209. mctx = EVP_MD_CTX_new();
  39210. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  39211. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  39212. EVP_MD_CTX_free(mctx);
  39213. X509_REQ_free(NULL);
  39214. X509_REQ_free(req);
  39215. EVP_PKEY_free(pub);
  39216. EVP_PKEY_free(priv);
  39217. #endif
  39218. #ifdef HAVE_ECC
  39219. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  39220. sizeof_ecc_clikey_der_256));
  39221. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  39222. sizeof_ecc_clikeypub_der_256));
  39223. AssertNotNull(req = X509_REQ_new());
  39224. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  39225. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  39226. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  39227. /* Signature is random and may be shorter or longer. */
  39228. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  39229. AssertIntLE(len, 253);
  39230. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  39231. X509_REQ_free(req);
  39232. EVP_PKEY_free(pub);
  39233. EVP_PKEY_free(priv);
  39234. #ifdef FP_ECC
  39235. wc_ecc_fp_free();
  39236. #endif
  39237. #endif /* HAVE_ECC */
  39238. X509_NAME_free(name);
  39239. printf(resultFmt, passed);
  39240. #endif
  39241. }
  39242. static void test_wolfssl_PKCS7(void)
  39243. {
  39244. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  39245. PKCS7* pkcs7;
  39246. byte data[FOURK_BUF];
  39247. word32 len = sizeof(data);
  39248. const byte* p = data;
  39249. byte content[] = "Test data to encode.";
  39250. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  39251. BIO* bio;
  39252. byte key[sizeof(client_key_der_2048)];
  39253. word32 keySz = (word32)sizeof(key);
  39254. #endif
  39255. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  39256. (word32)sizeof(content),
  39257. 0, 0)), 0);
  39258. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  39259. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  39260. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  39261. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  39262. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  39263. PKCS7_free(pkcs7);
  39264. /* fail case, without PKCS7_NOVERIFY */
  39265. p = data;
  39266. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  39267. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  39268. 0), WOLFSSL_FAILURE);
  39269. PKCS7_free(pkcs7);
  39270. /* success case, with PKCS7_NOVERIFY */
  39271. p = data;
  39272. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  39273. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  39274. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  39275. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  39276. /* test i2d */
  39277. XMEMCPY(key, client_key_der_2048, keySz);
  39278. pkcs7->privateKey = key;
  39279. pkcs7->privateKeySz = (word32)sizeof(key);
  39280. pkcs7->encryptOID = RSAk;
  39281. pkcs7->hashOID = SHAh;
  39282. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39283. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  39284. BIO_free(bio);
  39285. #endif
  39286. PKCS7_free(NULL);
  39287. PKCS7_free(pkcs7);
  39288. printf(resultFmt, passed);
  39289. #endif
  39290. }
  39291. static void test_wolfSSL_PKCS7_SIGNED_new(void)
  39292. {
  39293. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  39294. PKCS7_SIGNED* pkcs7;
  39295. printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
  39296. pkcs7 = PKCS7_SIGNED_new();
  39297. AssertNotNull(pkcs7);
  39298. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  39299. PKCS7_SIGNED_free(pkcs7);
  39300. printf(resultFmt, passed);
  39301. #endif
  39302. }
  39303. #ifndef NO_BIO
  39304. static void test_wolfSSL_PEM_write_bio_PKCS7(void)
  39305. {
  39306. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  39307. PKCS7* pkcs7 = NULL;
  39308. BIO* bio = NULL;
  39309. const byte* cert_buf = NULL;
  39310. int ret = 0;
  39311. WC_RNG rng;
  39312. const byte data[] = { /* Hello World */
  39313. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39314. 0x72,0x6c,0x64
  39315. };
  39316. #ifndef NO_RSA
  39317. #if defined(USE_CERT_BUFFERS_2048)
  39318. byte key[sizeof(client_key_der_2048)];
  39319. byte cert[sizeof(client_cert_der_2048)];
  39320. word32 keySz = (word32)sizeof(key);
  39321. word32 certSz = (word32)sizeof(cert);
  39322. XMEMSET(key, 0, keySz);
  39323. XMEMSET(cert, 0, certSz);
  39324. XMEMCPY(key, client_key_der_2048, keySz);
  39325. XMEMCPY(cert, client_cert_der_2048, certSz);
  39326. #elif defined(USE_CERT_BUFFERS_1024)
  39327. byte key[sizeof_client_key_der_1024];
  39328. byte cert[sizeof(sizeof_client_cert_der_1024)];
  39329. word32 keySz = (word32)sizeof(key);
  39330. word32 certSz = (word32)sizeof(cert);
  39331. XMEMSET(key, 0, keySz);
  39332. XMEMSET(cert, 0, certSz);
  39333. XMEMCPY(key, client_key_der_1024, keySz);
  39334. XMEMCPY(cert, client_cert_der_1024, certSz);
  39335. #else
  39336. unsigned char cert[ONEK_BUF];
  39337. unsigned char key[ONEK_BUF];
  39338. XFILE fp;
  39339. int certSz;
  39340. int keySz;
  39341. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  39342. AssertTrue((fp != XBADFILE));
  39343. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  39344. XFCLOSE(fp);
  39345. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  39346. AssertTrue(fp != XBADFILE);
  39347. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  39348. XFCLOSE(fp);
  39349. #endif
  39350. #elif defined(HAVE_ECC)
  39351. #if defined(USE_CERT_BUFFERS_256)
  39352. unsigned char cert[sizeof(cliecc_cert_der_256)];
  39353. unsigned char key[sizeof(ecc_clikey_der_256)];
  39354. int certSz = (int)sizeof(cert);
  39355. int keySz = (int)sizeof(key);
  39356. XMEMSET(cert, 0, certSz);
  39357. XMEMSET(key, 0, keySz);
  39358. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  39359. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  39360. #else
  39361. unsigned char cert[ONEK_BUF];
  39362. unsigned char key[ONEK_BUF];
  39363. XFILE fp;
  39364. int certSz, keySz;
  39365. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  39366. AssertTrue(fp != XBADFILE);
  39367. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  39368. XFCLOSE(fp);
  39369. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  39370. AssertTrue(fp != XBADFILE);
  39371. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  39372. XFCLOSE(fp);
  39373. #endif
  39374. #else
  39375. #error PKCS7 requires ECC or RSA
  39376. #endif
  39377. printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
  39378. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  39379. /* initialize with DER encoded cert */
  39380. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  39381. /* init rng */
  39382. AssertIntEQ(wc_InitRng(&rng), 0);
  39383. pkcs7->rng = &rng;
  39384. pkcs7->content = (byte*)data; /* not used for ex */
  39385. pkcs7->contentSz = (word32)sizeof(data);
  39386. pkcs7->contentOID = SIGNED_DATA;
  39387. pkcs7->privateKey = key;
  39388. pkcs7->privateKeySz = (word32)sizeof(key);
  39389. pkcs7->encryptOID = RSAk;
  39390. pkcs7->hashOID = SHAh;
  39391. pkcs7->signedAttribs = NULL;
  39392. pkcs7->signedAttribsSz = 0;
  39393. #ifndef NO_BIO
  39394. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39395. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  39396. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  39397. /* Read PKCS#7 PEM from BIO */
  39398. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  39399. AssertIntGE(ret, 0);
  39400. BIO_free(bio);
  39401. #endif
  39402. wc_PKCS7_Free(pkcs7);
  39403. wc_FreeRng(&rng);
  39404. printf(resultFmt, passed);
  39405. #endif
  39406. }
  39407. #ifdef HAVE_SMIME
  39408. static void test_wolfSSL_SMIME_read_PKCS7(void)
  39409. {
  39410. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  39411. !defined(NO_RSA)
  39412. PKCS7* pkcs7 = NULL;
  39413. BIO* bio = NULL;
  39414. BIO* bcont = NULL;
  39415. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  39416. printf(testingFmt, "wolfSSL_SMIME_read_PKCS7()");
  39417. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  39418. AssertNotNull(bio);
  39419. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  39420. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  39421. AssertNotNull(pkcs7);
  39422. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_SUCCESS);
  39423. XFCLOSE(smimeTestFile);
  39424. if (bcont) BIO_free(bcont);
  39425. wolfSSL_PKCS7_free(pkcs7);
  39426. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  39427. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  39428. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  39429. AssertNotNull(pkcs7);
  39430. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_SUCCESS);
  39431. XFCLOSE(smimeTestFile);
  39432. if (bcont) BIO_free(bcont);
  39433. wolfSSL_PKCS7_free(pkcs7);
  39434. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  39435. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  39436. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  39437. AssertNull(pkcs7);
  39438. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_FAILURE);
  39439. XFCLOSE(smimeTestFile);
  39440. if (bcont) BIO_free(bcont);
  39441. wolfSSL_PKCS7_free(pkcs7);
  39442. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  39443. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  39444. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  39445. AssertNotNull(pkcs7);
  39446. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL, PKCS7_NOVERIFY), SSL_SUCCESS);
  39447. BIO_free(bio);
  39448. if (bcont) BIO_free(bcont);
  39449. wolfSSL_PKCS7_free(pkcs7);
  39450. printf(resultFmt, passed);
  39451. #endif
  39452. }
  39453. #endif /* HAVE_SMIME*/
  39454. #endif /* !NO_BIO */
  39455. /*----------------------------------------------------------------------------*
  39456. | Certificate Failure Checks
  39457. *----------------------------------------------------------------------------*/
  39458. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  39459. !defined(WOLFSSL_NO_CLIENT_AUTH))
  39460. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  39461. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  39462. int type)
  39463. {
  39464. int ret;
  39465. WOLFSSL_CERT_MANAGER* cm = NULL;
  39466. switch (type) {
  39467. case TESTING_RSA:
  39468. #ifdef NO_RSA
  39469. printf("RSA disabled, skipping test\n");
  39470. return ASN_SIG_CONFIRM_E;
  39471. #else
  39472. break;
  39473. #endif
  39474. case TESTING_ECC:
  39475. #ifndef HAVE_ECC
  39476. printf("ECC disabled, skipping test\n");
  39477. return ASN_SIG_CONFIRM_E;
  39478. #else
  39479. break;
  39480. #endif
  39481. default:
  39482. printf("Bad function argument\n");
  39483. return BAD_FUNC_ARG;
  39484. }
  39485. cm = wolfSSL_CertManagerNew();
  39486. if (cm == NULL) {
  39487. printf("wolfSSL_CertManagerNew failed\n");
  39488. return -1;
  39489. }
  39490. #ifndef NO_FILESYSTEM
  39491. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  39492. if (ret != WOLFSSL_SUCCESS) {
  39493. printf("wolfSSL_CertManagerLoadCA failed\n");
  39494. wolfSSL_CertManagerFree(cm);
  39495. return ret;
  39496. }
  39497. #else
  39498. (void)ca;
  39499. #endif
  39500. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  39501. /* Let AssertIntEQ handle return code */
  39502. wolfSSL_CertManagerFree(cm);
  39503. return ret;
  39504. }
  39505. static int test_RsaSigFailure_cm(void)
  39506. {
  39507. int ret = 0;
  39508. const char* ca_cert = "./certs/ca-cert.pem";
  39509. const char* server_cert = "./certs/server-cert.der";
  39510. byte* cert_buf = NULL;
  39511. size_t cert_sz = 0;
  39512. ret = load_file(server_cert, &cert_buf, &cert_sz);
  39513. if (ret == 0) {
  39514. /* corrupt DER - invert last byte, which is signature */
  39515. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  39516. /* test bad cert */
  39517. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  39518. }
  39519. printf("Signature failure test: RSA: Ret %d\n", ret);
  39520. if (cert_buf)
  39521. free(cert_buf);
  39522. return ret;
  39523. }
  39524. static int test_EccSigFailure_cm(void)
  39525. {
  39526. int ret = 0;
  39527. /* self-signed ECC cert, so use server cert as CA */
  39528. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  39529. const char* server_cert = "./certs/server-ecc.der";
  39530. byte* cert_buf = NULL;
  39531. size_t cert_sz = 0;
  39532. ret = load_file(server_cert, &cert_buf, &cert_sz);
  39533. if (ret == 0) {
  39534. /* corrupt DER - invert last byte, which is signature */
  39535. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  39536. /* test bad cert */
  39537. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  39538. }
  39539. printf("Signature failure test: ECC: Ret %d\n", ret);
  39540. if (cert_buf)
  39541. free(cert_buf);
  39542. #ifdef FP_ECC
  39543. wc_ecc_fp_free();
  39544. #endif
  39545. return ret;
  39546. }
  39547. #endif /* NO_CERTS */
  39548. #ifdef WOLFSSL_TLS13
  39549. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  39550. #ifdef WC_SHA384_DIGEST_SIZE
  39551. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  39552. #else
  39553. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  39554. #endif
  39555. #endif
  39556. #ifdef WOLFSSL_EARLY_DATA
  39557. static const char earlyData[] = "Early Data";
  39558. static char earlyDataBuffer[1];
  39559. #endif
  39560. static int test_tls13_apis(void)
  39561. {
  39562. int ret = 0;
  39563. #ifndef WOLFSSL_NO_TLS12
  39564. #ifndef NO_WOLFSSL_CLIENT
  39565. WOLFSSL_CTX* clientTls12Ctx;
  39566. WOLFSSL* clientTls12Ssl;
  39567. #endif
  39568. #ifndef NO_WOLFSSL_SERVER
  39569. WOLFSSL_CTX* serverTls12Ctx;
  39570. WOLFSSL* serverTls12Ssl;
  39571. #endif
  39572. #endif
  39573. #ifndef NO_WOLFSSL_CLIENT
  39574. WOLFSSL_CTX* clientCtx;
  39575. WOLFSSL* clientSsl;
  39576. #endif
  39577. #ifndef NO_WOLFSSL_SERVER
  39578. WOLFSSL_CTX* serverCtx;
  39579. WOLFSSL* serverSsl;
  39580. #ifndef NO_CERTS
  39581. const char* ourCert = svrCertFile;
  39582. const char* ourKey = svrKeyFile;
  39583. #endif
  39584. #endif
  39585. int required;
  39586. #ifdef WOLFSSL_EARLY_DATA
  39587. int outSz;
  39588. #endif
  39589. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  39590. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  39591. #ifdef HAVE_LIBOQS
  39592. WOLFSSL_SABER_LEVEL3
  39593. #else
  39594. WOLFSSL_ECC_SECP256R1
  39595. #endif
  39596. };
  39597. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  39598. int numGroups = 2;
  39599. #endif
  39600. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  39601. char groupList[] =
  39602. #ifndef NO_ECC_SECP
  39603. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  39604. "P-521:"
  39605. #endif
  39606. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  39607. "P-384:"
  39608. #endif
  39609. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  39610. "P-256"
  39611. #ifdef HAVE_LIBOQS
  39612. ":P256_SABER_LEVEL1"
  39613. #endif
  39614. #endif
  39615. #ifdef HAVE_LIBOQS
  39616. ":KYBER_LEVEL1"
  39617. #endif
  39618. "";
  39619. #endif /* !defined(NO_ECC_SECP) */
  39620. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  39621. #ifndef WOLFSSL_NO_TLS12
  39622. #ifndef NO_WOLFSSL_CLIENT
  39623. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  39624. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  39625. #endif
  39626. #ifndef NO_WOLFSSL_SERVER
  39627. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  39628. #ifndef NO_CERTS
  39629. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  39630. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  39631. #endif
  39632. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  39633. #endif
  39634. #endif
  39635. #ifndef NO_WOLFSSL_CLIENT
  39636. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  39637. clientSsl = wolfSSL_new(clientCtx);
  39638. #endif
  39639. #ifndef NO_WOLFSSL_SERVER
  39640. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  39641. #ifndef NO_CERTS
  39642. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  39643. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  39644. #endif
  39645. serverSsl = wolfSSL_new(serverCtx);
  39646. #endif
  39647. #ifdef WOLFSSL_SEND_HRR_COOKIE
  39648. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  39649. #ifndef NO_WOLFSSL_CLIENT
  39650. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  39651. #endif
  39652. #ifndef NO_WOLFSSL_SERVER
  39653. #ifndef WOLFSSL_NO_TLS12
  39654. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  39655. #endif
  39656. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  39657. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  39658. WOLFSSL_SUCCESS);
  39659. #endif
  39660. #endif
  39661. #ifdef HAVE_SUPPORTED_CURVES
  39662. #ifdef HAVE_ECC
  39663. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  39664. #ifndef NO_WOLFSSL_SERVER
  39665. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1),
  39666. WOLFSSL_SUCCESS);
  39667. #endif
  39668. #ifndef NO_WOLFSSL_CLIENT
  39669. #ifndef WOLFSSL_NO_TLS12
  39670. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  39671. WOLFSSL_SUCCESS);
  39672. #endif
  39673. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  39674. WOLFSSL_SUCCESS);
  39675. #endif
  39676. #elif defined(HAVE_CURVE25519)
  39677. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  39678. #ifndef NO_WOLFSSL_SERVER
  39679. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  39680. WOLFSSL_SUCCESS);
  39681. #endif
  39682. #ifndef NO_WOLFSSL_CLIENT
  39683. #ifndef WOLFSSL_NO_TLS12
  39684. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  39685. WOLFSSL_SUCCESS);
  39686. #endif
  39687. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  39688. WOLFSSL_SUCCESS);
  39689. #endif
  39690. #elif defined(HAVE_CURVE448)
  39691. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  39692. #ifndef NO_WOLFSSL_SERVER
  39693. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  39694. WOLFSSL_SUCCESS);
  39695. #endif
  39696. #ifndef NO_WOLFSSL_CLIENT
  39697. #ifndef WOLFSSL_NO_TLS12
  39698. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  39699. WOLFSSL_SUCCESS);
  39700. #endif
  39701. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  39702. WOLFSSL_SUCCESS);
  39703. #endif
  39704. #else
  39705. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  39706. #ifndef NO_WOLFSSL_CLIENT
  39707. #ifndef WOLFSSL_NO_TLS12
  39708. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  39709. NOT_COMPILED_IN);
  39710. #endif
  39711. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  39712. NOT_COMPILED_IN);
  39713. #endif
  39714. #endif
  39715. #if defined(HAVE_LIBOQS)
  39716. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  39717. #ifndef NO_WOLFSSL_SERVER
  39718. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  39719. WOLFSSL_SUCCESS);
  39720. #endif
  39721. #ifndef NO_WOLFSSL_CLIENT
  39722. #ifndef WOLFSSL_NO_TLS12
  39723. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  39724. BAD_FUNC_ARG);
  39725. #endif
  39726. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  39727. WOLFSSL_SUCCESS);
  39728. #endif
  39729. #endif
  39730. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  39731. #ifndef NO_WOLFSSL_SERVER
  39732. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  39733. #endif
  39734. #ifndef NO_WOLFSSL_CLIENT
  39735. #ifndef WOLFSSL_NO_TLS12
  39736. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  39737. #endif
  39738. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  39739. #endif
  39740. #endif /* HAVE_SUPPORTED_CURVES */
  39741. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  39742. #ifndef NO_WOLFSSL_CLIENT
  39743. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  39744. #endif
  39745. #ifndef NO_WOLFSSL_SERVER
  39746. #ifndef WOLFSSL_NO_TLS12
  39747. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  39748. #endif
  39749. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  39750. #endif
  39751. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  39752. #ifndef NO_WOLFSSL_CLIENT
  39753. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  39754. #endif
  39755. #ifndef NO_WOLFSSL_SERVER
  39756. #ifndef WOLFSSL_NO_TLS12
  39757. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  39758. #endif
  39759. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  39760. #endif
  39761. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  39762. #ifndef NO_WOLFSSL_CLIENT
  39763. #ifndef WOLFSSL_NO_TLS12
  39764. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  39765. #endif
  39766. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  39767. #endif
  39768. #ifndef NO_WOLFSSL_SERVER
  39769. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  39770. #endif
  39771. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  39772. #ifndef NO_WOLFSSL_CLIENT
  39773. #ifndef WOLFSSL_NO_TLS12
  39774. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  39775. #endif
  39776. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  39777. #endif
  39778. #ifndef NO_WOLFSSL_SERVER
  39779. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  39780. #endif
  39781. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  39782. #ifndef NO_WOLFSSL_CLIENT
  39783. #ifndef WOLFSSL_NO_TLS12
  39784. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  39785. #endif
  39786. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  39787. #endif
  39788. #ifndef NO_WOLFSSL_SERVER
  39789. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  39790. #endif
  39791. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  39792. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  39793. #ifndef NO_WOLFSSL_CLIENT
  39794. #ifndef WOLFSSL_NO_TLS12
  39795. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  39796. BAD_FUNC_ARG);
  39797. #endif
  39798. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  39799. #endif
  39800. #ifndef NO_WOLFSSL_SERVER
  39801. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  39802. #endif
  39803. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  39804. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  39805. #ifndef NO_WOLFSSL_SERVER
  39806. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  39807. #endif
  39808. #ifndef NO_WOLFSSL_CLIENT
  39809. #ifndef WOLFSSL_NO_TLS12
  39810. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  39811. BAD_FUNC_ARG);
  39812. #endif
  39813. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  39814. #endif
  39815. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  39816. #ifndef NO_WOLFSSL_SERVER
  39817. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  39818. #endif
  39819. #ifndef NO_WOLFSSL_CLIENT
  39820. #ifndef WOLFSSL_NO_TLS12
  39821. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  39822. BAD_FUNC_ARG);
  39823. #endif
  39824. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  39825. #endif
  39826. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  39827. #ifndef NO_WOLFSSL_CLIENT
  39828. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  39829. #endif
  39830. #ifndef NO_WOLFSSL_SERVER
  39831. #ifndef WOLFSSL_NO_TLS12
  39832. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  39833. BAD_FUNC_ARG);
  39834. #endif
  39835. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  39836. #endif
  39837. #endif
  39838. #ifdef HAVE_ECC
  39839. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  39840. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  39841. #ifndef NO_WOLFSSL_SERVER
  39842. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  39843. #endif
  39844. #ifndef NO_WOLFSSL_CLIENT
  39845. #ifndef WOLFSSL_NO_TLS12
  39846. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  39847. #endif
  39848. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  39849. #endif
  39850. #endif
  39851. #ifdef HAVE_SUPPORTED_CURVES
  39852. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  39853. #ifndef NO_WOLFSSL_CLIENT
  39854. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  39855. #endif
  39856. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  39857. #ifndef NO_WOLFSSL_CLIENT
  39858. #ifndef WOLFSSL_NO_TLS12
  39859. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  39860. BAD_FUNC_ARG);
  39861. #endif
  39862. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  39863. WOLFSSL_MAX_GROUP_COUNT + 1),
  39864. BAD_FUNC_ARG);
  39865. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  39866. WOLFSSL_SUCCESS);
  39867. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  39868. BAD_FUNC_ARG);
  39869. #endif
  39870. #ifndef NO_WOLFSSL_SERVER
  39871. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  39872. WOLFSSL_SUCCESS);
  39873. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  39874. BAD_FUNC_ARG);
  39875. #endif
  39876. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  39877. #ifndef NO_WOLFSSL_CLIENT
  39878. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  39879. #endif
  39880. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  39881. #ifndef NO_WOLFSSL_CLIENT
  39882. #ifndef WOLFSSL_NO_TLS12
  39883. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  39884. BAD_FUNC_ARG);
  39885. #endif
  39886. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  39887. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  39888. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  39889. WOLFSSL_SUCCESS);
  39890. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  39891. BAD_FUNC_ARG);
  39892. #endif
  39893. #ifndef NO_WOLFSSL_SERVER
  39894. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  39895. WOLFSSL_SUCCESS);
  39896. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  39897. BAD_FUNC_ARG);
  39898. #endif
  39899. #ifdef OPENSSL_EXTRA
  39900. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  39901. #ifndef NO_WOLFSSL_CLIENT
  39902. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  39903. #endif
  39904. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  39905. #ifndef NO_WOLFSSL_CLIENT
  39906. #ifndef WOLFSSL_NO_TLS12
  39907. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  39908. WOLFSSL_FAILURE);
  39909. #endif
  39910. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  39911. WOLFSSL_SUCCESS);
  39912. #endif
  39913. #ifndef NO_WOLFSSL_SERVER
  39914. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  39915. WOLFSSL_SUCCESS);
  39916. #endif
  39917. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  39918. #ifndef NO_WOLFSSL_CLIENT
  39919. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  39920. #endif
  39921. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  39922. #ifndef NO_WOLFSSL_CLIENT
  39923. #ifndef WOLFSSL_NO_TLS12
  39924. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  39925. WOLFSSL_FAILURE);
  39926. #endif
  39927. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  39928. WOLFSSL_SUCCESS);
  39929. #endif
  39930. #ifndef NO_WOLFSSL_SERVER
  39931. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  39932. WOLFSSL_SUCCESS);
  39933. #endif
  39934. #endif /* OPENSSL_EXTRA */
  39935. #endif /* HAVE_SUPPORTED_CURVES */
  39936. #endif /* HAVE_ECC */
  39937. #ifdef WOLFSSL_EARLY_DATA
  39938. #ifndef OPENSSL_EXTRA
  39939. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  39940. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  39941. #else
  39942. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  39943. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  39944. #endif
  39945. #ifndef NO_WOLFSSL_CLIENT
  39946. #ifndef OPENSSL_EXTRA
  39947. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  39948. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  39949. #else
  39950. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  39951. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  39952. #endif
  39953. #endif
  39954. #ifndef NO_WOLFSSL_SERVER
  39955. #ifndef WOLFSSL_NO_TLS12
  39956. #ifndef OPENSSL_EXTRA
  39957. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  39958. BAD_FUNC_ARG);
  39959. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  39960. #else
  39961. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  39962. BAD_FUNC_ARG);
  39963. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  39964. #endif
  39965. #endif
  39966. #ifndef OPENSSL_EXTRA
  39967. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  39968. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  39969. #else
  39970. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  39971. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  39972. #endif
  39973. #endif
  39974. #ifndef OPENSSL_EXTRA
  39975. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  39976. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  39977. #else
  39978. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  39979. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  39980. #endif
  39981. #ifndef NO_WOLFSSL_CLIENT
  39982. #ifndef OPENSSL_EXTRA
  39983. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  39984. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), SIDE_ERROR);
  39985. #else
  39986. AssertIntEQ(SSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  39987. AssertIntEQ(SSL_get_max_early_data(clientSsl), SIDE_ERROR);
  39988. #endif
  39989. #endif
  39990. #ifndef NO_WOLFSSL_SERVER
  39991. #ifndef WOLFSSL_NO_TLS12
  39992. #ifndef OPENSSL_EXTRA
  39993. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  39994. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  39995. #else
  39996. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  39997. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  39998. #endif
  39999. #endif
  40000. #ifndef OPENSSL_EXTRA
  40001. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  40002. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  40003. #else
  40004. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  40005. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  40006. #endif
  40007. #endif
  40008. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  40009. &outSz), BAD_FUNC_ARG);
  40010. #ifndef NO_WOLFSSL_CLIENT
  40011. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  40012. &outSz), BAD_FUNC_ARG);
  40013. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  40014. BAD_FUNC_ARG);
  40015. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  40016. sizeof(earlyData), NULL),
  40017. BAD_FUNC_ARG);
  40018. #endif
  40019. #ifndef NO_WOLFSSL_SERVER
  40020. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  40021. sizeof(earlyData), &outSz),
  40022. SIDE_ERROR);
  40023. #endif
  40024. #ifndef NO_WOLFSSL_CLIENT
  40025. #ifndef WOLFSSL_NO_TLS12
  40026. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  40027. sizeof(earlyData), &outSz),
  40028. BAD_FUNC_ARG);
  40029. #endif
  40030. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  40031. sizeof(earlyData), &outSz),
  40032. WOLFSSL_FATAL_ERROR);
  40033. #endif
  40034. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  40035. sizeof(earlyDataBuffer), &outSz),
  40036. BAD_FUNC_ARG);
  40037. #ifndef NO_WOLFSSL_SERVER
  40038. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  40039. sizeof(earlyDataBuffer), &outSz),
  40040. BAD_FUNC_ARG);
  40041. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  40042. BAD_FUNC_ARG);
  40043. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  40044. sizeof(earlyDataBuffer), NULL),
  40045. BAD_FUNC_ARG);
  40046. #endif
  40047. #ifndef NO_WOLFSSL_CLIENT
  40048. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  40049. sizeof(earlyDataBuffer), &outSz),
  40050. SIDE_ERROR);
  40051. #endif
  40052. #ifndef NO_WOLFSSL_SERVER
  40053. #ifndef WOLFSSL_NO_TLS12
  40054. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  40055. sizeof(earlyDataBuffer), &outSz),
  40056. BAD_FUNC_ARG);
  40057. #endif
  40058. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  40059. sizeof(earlyDataBuffer), &outSz),
  40060. WOLFSSL_FATAL_ERROR);
  40061. #endif
  40062. #endif
  40063. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  40064. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  40065. #endif
  40066. #ifndef NO_WOLFSSL_SERVER
  40067. wolfSSL_free(serverSsl);
  40068. wolfSSL_CTX_free(serverCtx);
  40069. #endif
  40070. #ifndef NO_WOLFSSL_CLIENT
  40071. wolfSSL_free(clientSsl);
  40072. wolfSSL_CTX_free(clientCtx);
  40073. #endif
  40074. #ifndef WOLFSSL_NO_TLS12
  40075. #ifndef NO_WOLFSSL_SERVER
  40076. wolfSSL_free(serverTls12Ssl);
  40077. wolfSSL_CTX_free(serverTls12Ctx);
  40078. #endif
  40079. #ifndef NO_WOLFSSL_CLIENT
  40080. wolfSSL_free(clientTls12Ssl);
  40081. wolfSSL_CTX_free(clientTls12Ctx);
  40082. #endif
  40083. #endif
  40084. return ret;
  40085. }
  40086. #endif
  40087. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  40088. !defined(NO_OLD_TLS))
  40089. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  40090. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  40091. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  40092. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  40093. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  40094. const unsigned char* priv, unsigned int privSz,
  40095. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  40096. unsigned char* out, unsigned int* outlen,
  40097. void* ctx)
  40098. {
  40099. int result;
  40100. /* Test fail when context associated with WOLFSSL is NULL */
  40101. if (ctx == NULL) {
  40102. return -1;
  40103. }
  40104. (void)ssl;
  40105. /* return 0 on success */
  40106. PRIVATE_KEY_UNLOCK();
  40107. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  40108. PRIVATE_KEY_LOCK();
  40109. return result;
  40110. }
  40111. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  40112. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  40113. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  40114. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  40115. WOLFSSL_SUCCESS);
  40116. #endif
  40117. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  40118. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  40119. WOLFSSL_SUCCESS);
  40120. #endif
  40121. }
  40122. static void test_dh_ssl_setup(WOLFSSL* ssl)
  40123. {
  40124. static int dh_test_ctx = 1;
  40125. int ret;
  40126. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  40127. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  40128. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  40129. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  40130. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  40131. }
  40132. }
  40133. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  40134. {
  40135. int ret;
  40136. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  40137. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  40138. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  40139. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  40140. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  40141. }
  40142. }
  40143. #endif
  40144. static void test_DhCallbacks(void)
  40145. {
  40146. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  40147. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  40148. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  40149. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  40150. WOLFSSL_CTX *ctx;
  40151. WOLFSSL *ssl;
  40152. tcp_ready ready;
  40153. func_args server_args;
  40154. func_args client_args;
  40155. THREAD_TYPE serverThread;
  40156. callback_functions func_cb_client;
  40157. callback_functions func_cb_server;
  40158. int test;
  40159. printf(testingFmt, "test_DhCallbacks");
  40160. #ifndef NO_WOLFSSL_CLIENT
  40161. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40162. #else
  40163. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  40164. #endif
  40165. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  40166. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  40167. /* load client ca cert */
  40168. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  40169. WOLFSSL_SUCCESS);
  40170. /* test with NULL arguments */
  40171. wolfSSL_SetDhAgreeCtx(NULL, &test);
  40172. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  40173. /* test success case */
  40174. test = 1;
  40175. AssertNotNull(ssl = wolfSSL_new(ctx));
  40176. wolfSSL_SetDhAgreeCtx(ssl, &test);
  40177. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  40178. wolfSSL_free(ssl);
  40179. wolfSSL_CTX_free(ctx);
  40180. /* test a connection where callback is used */
  40181. #ifdef WOLFSSL_TIRTOS
  40182. fdOpenSession(Task_self());
  40183. #endif
  40184. XMEMSET(&server_args, 0, sizeof(func_args));
  40185. XMEMSET(&client_args, 0, sizeof(func_args));
  40186. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  40187. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  40188. StartTCP();
  40189. InitTcpReady(&ready);
  40190. #if defined(USE_WINDOWS_API)
  40191. /* use RNG to get random port if using windows */
  40192. ready.port = GetRandomPort();
  40193. #endif
  40194. server_args.signal = &ready;
  40195. client_args.signal = &ready;
  40196. server_args.return_code = TEST_FAIL;
  40197. client_args.return_code = TEST_FAIL;
  40198. /* set callbacks to use DH functions */
  40199. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  40200. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  40201. #ifndef WOLFSSL_NO_TLS12
  40202. func_cb_client.method = wolfTLSv1_2_client_method;
  40203. #else
  40204. func_cb_client.method = wolfTLSv1_3_client_method;
  40205. #endif
  40206. client_args.callbacks = &func_cb_client;
  40207. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  40208. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  40209. #ifndef WOLFSSL_NO_TLS12
  40210. func_cb_server.method = wolfTLSv1_2_server_method;
  40211. #else
  40212. func_cb_server.method = wolfTLSv1_3_server_method;
  40213. #endif
  40214. server_args.callbacks = &func_cb_server;
  40215. start_thread(test_server_nofail, &server_args, &serverThread);
  40216. wait_tcp_ready(&server_args);
  40217. test_client_nofail(&client_args, NULL);
  40218. join_thread(serverThread);
  40219. AssertTrue(client_args.return_code);
  40220. AssertTrue(server_args.return_code);
  40221. FreeTcpReady(&ready);
  40222. #ifdef WOLFSSL_TIRTOS
  40223. fdOpenSession(Task_self());
  40224. #endif
  40225. /* now set user ctx to not be 1 so that the callback returns fail case */
  40226. #ifdef WOLFSSL_TIRTOS
  40227. fdOpenSession(Task_self());
  40228. #endif
  40229. XMEMSET(&server_args, 0, sizeof(func_args));
  40230. XMEMSET(&client_args, 0, sizeof(func_args));
  40231. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  40232. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  40233. StartTCP();
  40234. InitTcpReady(&ready);
  40235. #if defined(USE_WINDOWS_API)
  40236. /* use RNG to get random port if using windows */
  40237. ready.port = GetRandomPort();
  40238. #endif
  40239. server_args.signal = &ready;
  40240. client_args.signal = &ready;
  40241. server_args.return_code = TEST_FAIL;
  40242. client_args.return_code = TEST_FAIL;
  40243. /* set callbacks to use DH functions */
  40244. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  40245. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  40246. #ifndef WOLFSSL_NO_TLS12
  40247. func_cb_client.method = wolfTLSv1_2_client_method;
  40248. #else
  40249. func_cb_client.method = wolfTLSv1_3_client_method;
  40250. #endif
  40251. client_args.callbacks = &func_cb_client;
  40252. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  40253. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  40254. #ifndef WOLFSSL_NO_TLS12
  40255. func_cb_server.method = wolfTLSv1_2_server_method;
  40256. #else
  40257. func_cb_server.method = wolfTLSv1_3_server_method;
  40258. #endif
  40259. server_args.callbacks = &func_cb_server;
  40260. start_thread(test_server_nofail, &server_args, &serverThread);
  40261. wait_tcp_ready(&server_args);
  40262. test_client_nofail(&client_args, NULL);
  40263. join_thread(serverThread);
  40264. AssertIntEQ(client_args.return_code, TEST_FAIL);
  40265. AssertIntEQ(server_args.return_code, TEST_FAIL);
  40266. FreeTcpReady(&ready);
  40267. #ifdef WOLFSSL_TIRTOS
  40268. fdOpenSession(Task_self());
  40269. #endif
  40270. printf(resultFmt, passed);
  40271. #endif
  40272. }
  40273. #endif /* HAVE_PK_CALLBACKS */
  40274. #ifdef HAVE_HASHDRBG
  40275. #ifdef TEST_RESEED_INTERVAL
  40276. static int test_wc_RNG_GenerateBlock_Reseed(void)
  40277. {
  40278. int i, ret;
  40279. WC_RNG rng;
  40280. byte key[32];
  40281. ret = wc_InitRng(&rng);
  40282. if (ret == 0) {
  40283. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  40284. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  40285. if (ret != 0) {
  40286. break;
  40287. }
  40288. }
  40289. }
  40290. wc_FreeRng(&rng);
  40291. return ret;
  40292. }
  40293. #endif /* TEST_RESEED_INTERVAL */
  40294. static int test_wc_RNG_GenerateBlock(void)
  40295. {
  40296. int i, ret;
  40297. WC_RNG rng;
  40298. byte key[32];
  40299. ret = wc_InitRng(&rng);
  40300. if (ret == 0) {
  40301. for(i = 0; i < 10; i++) {
  40302. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  40303. if (ret != 0) {
  40304. break;
  40305. }
  40306. }
  40307. }
  40308. wc_FreeRng(&rng);
  40309. (void)rng; /* for WC_NO_RNG case */
  40310. (void)key;
  40311. return ret;
  40312. }
  40313. #endif
  40314. /*
  40315. * Testing get_rand_digit
  40316. */
  40317. static int test_get_rand_digit (void)
  40318. {
  40319. int ret = 0;
  40320. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  40321. WC_RNG rng;
  40322. mp_digit d;
  40323. printf(testingFmt, "get_rand_digit()");
  40324. ret = wc_InitRng(&rng);
  40325. if (ret == 0) {
  40326. ret = get_rand_digit(&rng, &d);
  40327. }
  40328. if (ret == 0) {
  40329. ret = get_rand_digit(NULL, NULL);
  40330. if (ret == BAD_FUNC_ARG) {
  40331. ret = 0;
  40332. }
  40333. }
  40334. if (ret == 0) {
  40335. ret = get_rand_digit(NULL, &d);
  40336. if (ret == BAD_FUNC_ARG) {
  40337. ret = 0;
  40338. }
  40339. }
  40340. if (ret == 0) {
  40341. ret = get_rand_digit(&rng, NULL);
  40342. if (ret == BAD_FUNC_ARG) {
  40343. ret = 0;
  40344. }
  40345. }
  40346. if (ret == 0) {
  40347. ret = wc_FreeRng(&rng);
  40348. }
  40349. printf(resultFmt, ret == 0 ? passed : failed);
  40350. #endif
  40351. return ret;
  40352. }/* End test_get_rand_digit*/
  40353. /*
  40354. * Testing get_digit_count
  40355. */
  40356. static int test_get_digit_count (void)
  40357. {
  40358. int ret = 0;
  40359. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  40360. mp_int a;
  40361. printf(testingFmt, "get_digit_count()");
  40362. if (mp_init(&a) != MP_OKAY) {
  40363. ret = -1;
  40364. }
  40365. if (ret == 0) {
  40366. ret = get_digit_count(NULL);
  40367. }
  40368. if (ret == 0) {
  40369. ret = get_digit_count(&a);
  40370. }
  40371. printf(resultFmt, ret == 0 ? passed : failed);
  40372. mp_clear(&a);
  40373. #endif
  40374. return ret;
  40375. }/* End test_get_digit_count*/
  40376. /*
  40377. * Testing mp_cond_copy
  40378. */
  40379. static int test_mp_cond_copy (void)
  40380. {
  40381. int ret = 0;
  40382. #if defined(WOLFSSL_PUBLIC_MP)
  40383. mp_int a;
  40384. mp_int b;
  40385. int copy = 0;
  40386. printf(testingFmt, "mp_cond_copy()");
  40387. if (mp_init(&a) != MP_OKAY) {
  40388. ret = -1;
  40389. }
  40390. if (ret == 0) {
  40391. if (mp_init(&b) != MP_OKAY) {
  40392. ret = -1;
  40393. }
  40394. }
  40395. if (ret == 0) {
  40396. ret = mp_cond_copy(NULL, copy, NULL);
  40397. if (ret == BAD_FUNC_ARG) {
  40398. ret = 0;
  40399. }
  40400. }
  40401. if (ret == 0) {
  40402. ret = mp_cond_copy(NULL, copy, &b);
  40403. if (ret == BAD_FUNC_ARG) {
  40404. ret = 0;
  40405. }
  40406. }
  40407. if (ret == 0) {
  40408. ret = mp_cond_copy(&a, copy, NULL);
  40409. if (ret == BAD_FUNC_ARG) {
  40410. ret = 0;
  40411. }
  40412. }
  40413. if (ret == 0) {
  40414. ret = mp_cond_copy(&a, copy, &b);
  40415. }
  40416. printf(resultFmt, ret == 0 ? passed : failed);
  40417. mp_clear(&a);
  40418. mp_clear(&b);
  40419. #endif
  40420. return ret;
  40421. }/* End test_mp_cond_copy*/
  40422. /*
  40423. * Testing mp_rand
  40424. */
  40425. static int test_mp_rand (void)
  40426. {
  40427. int ret = 0;
  40428. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  40429. mp_int a;
  40430. int digits = 1;
  40431. WC_RNG rng;
  40432. printf(testingFmt, "mp_rand()");
  40433. if (mp_init(&a) != MP_OKAY) {
  40434. ret = -1;
  40435. }
  40436. if (ret == 0) {
  40437. ret = wc_InitRng(&rng);
  40438. }
  40439. if (ret == 0) {
  40440. ret = mp_rand(&a, digits, NULL);
  40441. if (ret == MISSING_RNG_E) {
  40442. ret = 0;
  40443. }
  40444. }
  40445. if (ret == 0) {
  40446. ret = mp_rand(NULL, digits, &rng);
  40447. if (ret == BAD_FUNC_ARG) {
  40448. ret = 0;
  40449. }
  40450. }
  40451. if (ret == 0) {
  40452. ret = mp_rand(&a, 0, &rng);
  40453. if (ret == BAD_FUNC_ARG) {
  40454. ret = 0;
  40455. }
  40456. }
  40457. if (ret == 0) {
  40458. ret = mp_rand(&a, digits, &rng);
  40459. }
  40460. printf(resultFmt, ret == 0 ? passed : failed);
  40461. mp_clear(&a);
  40462. wc_FreeRng(&rng);
  40463. #endif
  40464. return ret;
  40465. }/* End test_mp_rand*/
  40466. /*
  40467. * Testing get_digit
  40468. */
  40469. static int test_get_digit (void)
  40470. {
  40471. int ret = 0;
  40472. #if defined(WOLFSSL_PUBLIC_MP)
  40473. mp_int a;
  40474. int n = 0;
  40475. printf(testingFmt, "get_digit()");
  40476. if (mp_init(&a) != MP_OKAY) {
  40477. ret = -1;
  40478. }
  40479. if (ret == 0) {
  40480. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  40481. ret = -1;
  40482. }
  40483. }
  40484. if (ret == 0) {
  40485. if (get_digit(NULL, n) == 0) { /* Should hit this */
  40486. ret = 0;
  40487. }
  40488. }
  40489. if (ret == 0) {
  40490. if (get_digit(&a, n) != 0) { /* Should not hit this */
  40491. ret = -1;
  40492. }
  40493. }
  40494. if (ret == 0) {
  40495. if (get_digit(&a, n) == 0) { /* Should hit this */
  40496. ret = 0;
  40497. }
  40498. }
  40499. printf(resultFmt, ret == 0 ? passed : failed);
  40500. mp_clear(&a);
  40501. #endif
  40502. return ret;
  40503. }/* End test_get_digit*/
  40504. /*
  40505. * Testing wc_export_int
  40506. */
  40507. static int test_wc_export_int(void)
  40508. {
  40509. int ret = 0;
  40510. #if defined(WOLFSSL_PUBLIC_MP)
  40511. mp_int mp;
  40512. byte buf[32];
  40513. word32 keySz = (word32)sizeof(buf);
  40514. word32 len = (word32)sizeof(buf);
  40515. printf(testingFmt, "wc_export_int()");
  40516. if (mp_init(&mp) != MP_OKAY) {
  40517. ret = -1;
  40518. }
  40519. if (ret == 0) {
  40520. ret = mp_set_int(&mp, 1234);
  40521. }
  40522. if (ret == 0) {
  40523. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  40524. if (ret == BAD_FUNC_ARG) {
  40525. ret = 0;
  40526. }
  40527. }
  40528. if (ret == 0) {
  40529. len = sizeof(buf)-1;
  40530. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  40531. if (ret == BUFFER_E) {
  40532. ret = 0;
  40533. }
  40534. }
  40535. if (ret == 0) {
  40536. len = sizeof(buf);
  40537. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  40538. }
  40539. if (ret == 0) {
  40540. len = 4; /* test input too small */
  40541. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  40542. if (ret == BUFFER_E) {
  40543. ret = 0;
  40544. }
  40545. }
  40546. if (ret == 0) {
  40547. len = sizeof(buf);
  40548. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  40549. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  40550. if (ret == 0 && len != 5) {
  40551. ret = BAD_FUNC_ARG;
  40552. }
  40553. }
  40554. printf(resultFmt, ret == 0 ? passed : failed);
  40555. mp_clear(&mp);
  40556. #endif
  40557. return ret;
  40558. }/* End test_wc_export_int*/
  40559. static int test_wc_InitRngNonce(void)
  40560. {
  40561. int ret=0;
  40562. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  40563. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  40564. WC_RNG rng;
  40565. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  40566. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  40567. word32 nonceSz = sizeof(nonce);
  40568. printf(testingFmt, "wc_InitRngNonce()");
  40569. if (ret == 0){
  40570. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  40571. }
  40572. wc_FreeRng(&rng);
  40573. printf(resultFmt, ret == 0 ? passed : failed);
  40574. #endif
  40575. return ret;
  40576. }/* End test_wc_InitRngNonce*/
  40577. /*
  40578. * Testing wc_InitRngNonce_ex
  40579. */
  40580. static int test_wc_InitRngNonce_ex(void)
  40581. {
  40582. int ret=0;
  40583. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  40584. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  40585. WC_RNG rng;
  40586. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  40587. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  40588. word32 nonceSz = sizeof(nonce);
  40589. printf(testingFmt, "wc_InitRngNonce_ex()");
  40590. if (ret == 0){
  40591. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, devId);
  40592. }
  40593. wc_FreeRng(&rng);
  40594. printf(resultFmt, ret == 0 ? passed : failed);
  40595. #endif
  40596. return ret;
  40597. }/*End test_wc_InitRngNonce_ex*/
  40598. static void test_wolfSSL_X509_CRL(void)
  40599. {
  40600. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  40601. X509_CRL *crl;
  40602. char pem[][100] = {
  40603. "./certs/crl/crl.pem",
  40604. "./certs/crl/crl2.pem",
  40605. "./certs/crl/caEccCrl.pem",
  40606. "./certs/crl/eccCliCRL.pem",
  40607. "./certs/crl/eccSrvCRL.pem",
  40608. ""
  40609. };
  40610. #ifndef NO_BIO
  40611. BIO *bio;
  40612. #endif
  40613. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  40614. char der[][100] = {
  40615. "./certs/crl/crl.der",
  40616. "./certs/crl/crl2.der",
  40617. ""};
  40618. #endif
  40619. XFILE fp;
  40620. int i;
  40621. printf(testingFmt, "test_wolfSSL_X509_CRL");
  40622. for (i = 0; pem[i][0] != '\0'; i++)
  40623. {
  40624. fp = XFOPEN(pem[i], "rb");
  40625. AssertTrue((fp != XBADFILE));
  40626. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  40627. AssertNotNull(crl);
  40628. X509_CRL_free(crl);
  40629. XFCLOSE(fp);
  40630. fp = XFOPEN(pem[i], "rb");
  40631. AssertTrue((fp != XBADFILE));
  40632. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  40633. AssertNotNull(crl);
  40634. X509_CRL_free(crl);
  40635. XFCLOSE(fp);
  40636. }
  40637. #ifndef NO_BIO
  40638. for (i = 0; pem[i][0] != '\0'; i++)
  40639. {
  40640. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  40641. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  40642. X509_CRL_free(crl);
  40643. BIO_free(bio);
  40644. }
  40645. #endif
  40646. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  40647. for(i = 0; der[i][0] != '\0'; i++){
  40648. fp = XFOPEN(der[i], "rb");
  40649. AssertTrue((fp != XBADFILE));
  40650. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  40651. AssertNotNull(crl);
  40652. X509_CRL_free(crl);
  40653. XFCLOSE(fp);
  40654. fp = XFOPEN(der[i], "rb");
  40655. AssertTrue((fp != XBADFILE));
  40656. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  40657. AssertNotNull(crl);
  40658. X509_CRL_free(crl);
  40659. XFCLOSE(fp);
  40660. }
  40661. #endif
  40662. printf(resultFmt, passed);
  40663. #endif
  40664. return;
  40665. }
  40666. static void test_wolfSSL_X509_load_crl_file(void)
  40667. {
  40668. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  40669. !defined(NO_RSA) && !defined(NO_BIO)
  40670. int i;
  40671. char pem[][100] = {
  40672. "./certs/crl/crl.pem",
  40673. "./certs/crl/crl2.pem",
  40674. "./certs/crl/caEccCrl.pem",
  40675. "./certs/crl/eccCliCRL.pem",
  40676. "./certs/crl/eccSrvCRL.pem",
  40677. ""
  40678. };
  40679. char der[][100] = {
  40680. "./certs/crl/crl.der",
  40681. "./certs/crl/crl2.der",
  40682. ""
  40683. };
  40684. WOLFSSL_X509_STORE* store;
  40685. WOLFSSL_X509_LOOKUP* lookup;
  40686. printf(testingFmt, "wolfSSL_X509_load_crl_file");
  40687. AssertNotNull(store = wolfSSL_X509_STORE_new());
  40688. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  40689. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  40690. X509_FILETYPE_PEM), 1);
  40691. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  40692. X509_FILETYPE_PEM), 1);
  40693. if (store) {
  40694. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  40695. WOLFSSL_FILETYPE_PEM), 1);
  40696. /* since store hasn't yet known the revoked cert*/
  40697. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  40698. WOLFSSL_FILETYPE_PEM), 1);
  40699. }
  40700. for (i = 0; pem[i][0] != '\0'; i++)
  40701. {
  40702. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  40703. }
  40704. if (store) {
  40705. /* since store knows crl list */
  40706. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  40707. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  40708. }
  40709. /* once feeing store */
  40710. X509_STORE_free(store);
  40711. store = NULL;
  40712. AssertNotNull(store = wolfSSL_X509_STORE_new());
  40713. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  40714. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  40715. X509_FILETYPE_PEM), 1);
  40716. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  40717. X509_FILETYPE_PEM), 1);
  40718. if (store) {
  40719. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  40720. WOLFSSL_FILETYPE_PEM), 1);
  40721. /* since store hasn't yet known the revoked cert*/
  40722. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  40723. WOLFSSL_FILETYPE_PEM), 1);
  40724. }
  40725. for (i = 0; der[i][0] != '\0'; i++)
  40726. {
  40727. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  40728. }
  40729. if (store) {
  40730. /* since store knows crl list */
  40731. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  40732. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  40733. }
  40734. /* test for incorrect parameter */
  40735. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  40736. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  40737. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  40738. X509_STORE_free(store);
  40739. store = NULL;
  40740. printf(resultFmt, passed);
  40741. #endif
  40742. }
  40743. static void test_wolfSSL_d2i_X509_REQ(void)
  40744. {
  40745. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  40746. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA))
  40747. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  40748. * generated by libest
  40749. * ./certs/csr.attr.der contains sample attributes
  40750. * ./certs/csr.ext.der contains sample extensions */
  40751. const char* csrFile = "./certs/csr.signed.der";
  40752. const char* csrPopFile = "./certs/csr.attr.der";
  40753. const char* csrExtFile = "./certs/csr.ext.der";
  40754. /* ./certs/csr.dsa.pem is generated using
  40755. * openssl req -newkey dsa:certs/dsaparams.pem \
  40756. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  40757. * -outform PEM
  40758. * with the passphrase "wolfSSL"
  40759. */
  40760. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  40761. const char* csrDsaFile = "./certs/csr.dsa.pem";
  40762. XFILE f;
  40763. #endif
  40764. BIO* bio = NULL;
  40765. X509* req = NULL;
  40766. EVP_PKEY *pub_key = NULL;
  40767. {
  40768. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  40769. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  40770. /*
  40771. * Extract the public key from the CSR
  40772. */
  40773. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  40774. /*
  40775. * Verify the signature in the CSR
  40776. */
  40777. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  40778. X509_free(req);
  40779. BIO_free(bio);
  40780. EVP_PKEY_free(pub_key);
  40781. }
  40782. {
  40783. #ifdef OPENSSL_ALL
  40784. X509_ATTRIBUTE* attr;
  40785. ASN1_TYPE *at;
  40786. #endif
  40787. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  40788. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  40789. /*
  40790. * Extract the public key from the CSR
  40791. */
  40792. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  40793. /*
  40794. * Verify the signature in the CSR
  40795. */
  40796. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  40797. #ifdef OPENSSL_ALL
  40798. /*
  40799. * Obtain the challenge password from the CSR
  40800. */
  40801. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  40802. NID_pkcs9_challengePassword);
  40803. AssertNotNull(attr = X509_REQ_get_attr(req, NID_pkcs9_challengePassword));
  40804. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  40805. AssertNotNull(at->value.asn1_string);
  40806. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  40807. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  40808. #endif
  40809. X509_free(req);
  40810. BIO_free(bio);
  40811. EVP_PKEY_free(pub_key);
  40812. }
  40813. {
  40814. #ifdef OPENSSL_ALL
  40815. X509_ATTRIBUTE* attr;
  40816. ASN1_TYPE *at;
  40817. STACK_OF(X509_EXTENSION) *exts = NULL;
  40818. #endif
  40819. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  40820. /* This CSR contains an Extension Request attribute so
  40821. * we test extension parsing in a CSR attribute here. */
  40822. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  40823. /*
  40824. * Extract the public key from the CSR
  40825. */
  40826. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  40827. /*
  40828. * Verify the signature in the CSR
  40829. */
  40830. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  40831. #ifdef OPENSSL_ALL
  40832. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  40833. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  40834. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40835. /*
  40836. * Obtain the challenge password from the CSR
  40837. */
  40838. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  40839. NID_pkcs9_challengePassword);
  40840. AssertNotNull(attr = X509_REQ_get_attr(req, NID_pkcs9_challengePassword));
  40841. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  40842. AssertNotNull(at->value.asn1_string);
  40843. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  40844. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  40845. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  40846. #endif
  40847. X509_free(req);
  40848. BIO_free(bio);
  40849. EVP_PKEY_free(pub_key);
  40850. }
  40851. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  40852. {
  40853. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  40854. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  40855. /*
  40856. * Extract the public key from the CSR
  40857. */
  40858. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  40859. /*
  40860. * Verify the signature in the CSR
  40861. */
  40862. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  40863. X509_free(req);
  40864. BIO_free(bio);
  40865. /* Run the same test, but with a file pointer instead of a BIO.
  40866. * (PEM_read_X509_REQ)*/
  40867. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  40868. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  40869. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  40870. X509_free(req);
  40871. EVP_PKEY_free(pub_key);
  40872. }
  40873. #endif /* !NO_DSA && !HAVE_SELFTEST */
  40874. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  40875. }
  40876. static void test_wolfSSL_PEM_read_X509(void)
  40877. {
  40878. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  40879. !defined(NO_RSA)
  40880. X509 *x509 = NULL;
  40881. XFILE fp;
  40882. printf(testingFmt, "wolfSSL_PEM_read_X509");
  40883. fp = XFOPEN(svrCertFile, "rb");
  40884. AssertTrue((fp != XBADFILE));
  40885. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  40886. X509_free(x509);
  40887. XFCLOSE(fp);
  40888. printf(resultFmt, passed);
  40889. #endif
  40890. }
  40891. static void test_wolfSSL_PEM_read(void)
  40892. {
  40893. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40894. const char* filename = "./certs/server-keyEnc.pem";
  40895. XFILE fp;
  40896. char* name = NULL;
  40897. char* header = NULL;
  40898. byte* data = NULL;
  40899. long len;
  40900. EVP_CIPHER_INFO cipher;
  40901. WOLFSSL_BIO* bio;
  40902. byte* fileData;
  40903. size_t fileDataSz;
  40904. byte* out;
  40905. printf(testingFmt, "wolfSSL_PEM_read");
  40906. fp = XFOPEN(filename, "rb");
  40907. AssertTrue((fp != XBADFILE));
  40908. /* Fail cases. */
  40909. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  40910. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  40911. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  40912. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  40913. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  40914. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  40915. AssertIntGT(XSTRLEN(header), 0);
  40916. AssertIntGT(len, 0);
  40917. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  40918. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  40919. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  40920. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  40921. DYNAMIC_TYPE_TMP_BUFFER));
  40922. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  40923. XFCLOSE(fp);
  40924. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  40925. /* Fail cases. */
  40926. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  40927. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  40928. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  40929. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  40930. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  40931. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  40932. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  40933. /* Fail cases. */
  40934. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  40935. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  40936. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  40937. #ifndef NO_DES3
  40938. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  40939. #endif
  40940. /* Fail cases. */
  40941. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  40942. (void*)"yassl123"), WOLFSSL_FAILURE);
  40943. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  40944. (void*)"yassl123"), WOLFSSL_FAILURE);
  40945. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  40946. (void*)"yassl123"), WOLFSSL_FAILURE);
  40947. #if !defined(NO_DES3) && !defined(NO_MD5)
  40948. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  40949. (void*)"yassl123"), WOLFSSL_SUCCESS);
  40950. #endif
  40951. BIO_free(bio);
  40952. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40953. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40954. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40955. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40956. name = NULL;
  40957. header = NULL;
  40958. data = NULL;
  40959. fp = XFOPEN(svrKeyFile, "rb");
  40960. AssertTrue((fp != XBADFILE));
  40961. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  40962. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  40963. AssertIntEQ(XSTRLEN(header), 0);
  40964. AssertIntGT(len, 0);
  40965. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  40966. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  40967. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  40968. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  40969. DYNAMIC_TYPE_TMP_BUFFER));
  40970. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  40971. XFCLOSE(fp);
  40972. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  40973. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  40974. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  40975. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  40976. BIO_free(bio);
  40977. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40978. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40979. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40980. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40981. printf(resultFmt, passed);
  40982. #endif
  40983. }
  40984. static void test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  40985. {
  40986. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  40987. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  40988. const byte iv[12] = { 0 };
  40989. const byte key[16] = { 0 };
  40990. const byte cleartext[16] = { 0 };
  40991. const byte aad[] = {
  40992. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  40993. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  40994. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  40995. 0x4f
  40996. };
  40997. byte out1Part[16];
  40998. byte outTag1Part[16];
  40999. byte out2Part[16];
  41000. byte outTag2Part[16];
  41001. byte decryptBuf[16];
  41002. int len;
  41003. int tlen;
  41004. EVP_CIPHER_CTX* ctx = NULL;
  41005. printf(testingFmt, "wolfssl_EVP_aes_gcm_AAD_2_parts");
  41006. /* ENCRYPT */
  41007. /* Send AAD and data in 1 part */
  41008. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  41009. tlen = 0;
  41010. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  41011. 1);
  41012. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  41013. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  41014. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  41015. sizeof(cleartext)), 1);
  41016. tlen += len;
  41017. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  41018. tlen += len;
  41019. AssertIntEQ(tlen, sizeof(cleartext));
  41020. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  41021. outTag1Part), 1);
  41022. EVP_CIPHER_CTX_free(ctx);
  41023. /* DECRYPT */
  41024. /* Send AAD and data in 1 part */
  41025. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  41026. tlen = 0;
  41027. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  41028. 1);
  41029. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  41030. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  41031. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  41032. sizeof(cleartext)), 1);
  41033. tlen += len;
  41034. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  41035. outTag1Part), 1);
  41036. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  41037. tlen += len;
  41038. AssertIntEQ(tlen, sizeof(cleartext));
  41039. EVP_CIPHER_CTX_free(ctx);
  41040. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  41041. /* ENCRYPT */
  41042. /* Send AAD and data in 2 parts */
  41043. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  41044. tlen = 0;
  41045. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  41046. 1);
  41047. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  41048. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  41049. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  41050. 1);
  41051. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  41052. tlen += len;
  41053. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  41054. sizeof(cleartext) - 1), 1);
  41055. tlen += len;
  41056. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  41057. tlen += len;
  41058. AssertIntEQ(tlen, sizeof(cleartext));
  41059. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  41060. outTag2Part), 1);
  41061. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  41062. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  41063. EVP_CIPHER_CTX_free(ctx);
  41064. /* DECRYPT */
  41065. /* Send AAD and data in 2 parts */
  41066. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  41067. tlen = 0;
  41068. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  41069. 1);
  41070. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  41071. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  41072. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  41073. 1);
  41074. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  41075. tlen += len;
  41076. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  41077. sizeof(cleartext) - 1), 1);
  41078. tlen += len;
  41079. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  41080. outTag1Part), 1);
  41081. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  41082. tlen += len;
  41083. AssertIntEQ(tlen, sizeof(cleartext));
  41084. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  41085. /* Test AAD re-use */
  41086. EVP_CIPHER_CTX_free(ctx);
  41087. printf(resultFmt, passed);
  41088. #endif
  41089. }
  41090. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  41091. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  41092. static void test_wolfssl_EVP_aes_gcm_zeroLen(void)
  41093. {
  41094. /* Zero length plain text */
  41095. byte key[] = {
  41096. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41097. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41098. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41099. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  41100. }; /* align */
  41101. byte iv[] = {
  41102. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  41103. }; /* align */
  41104. byte plaintxt[1];
  41105. int ivSz = 12;
  41106. int plaintxtSz = 0;
  41107. unsigned char tag[16];
  41108. unsigned char tag_kat[] =
  41109. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  41110. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  41111. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  41112. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  41113. int ciphertxtSz = 0;
  41114. int decryptedtxtSz = 0;
  41115. int len = 0;
  41116. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  41117. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  41118. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  41119. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  41120. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  41121. plaintxtSz));
  41122. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  41123. ciphertxtSz += len;
  41124. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  41125. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  41126. AssertIntEQ(0, ciphertxtSz);
  41127. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  41128. EVP_CIPHER_CTX_init(de);
  41129. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  41130. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  41131. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  41132. decryptedtxtSz = len;
  41133. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  41134. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  41135. decryptedtxtSz += len;
  41136. AssertIntEQ(0, decryptedtxtSz);
  41137. EVP_CIPHER_CTX_free(en);
  41138. EVP_CIPHER_CTX_free(de);
  41139. }
  41140. #endif
  41141. static void test_wolfssl_EVP_aes_gcm(void)
  41142. {
  41143. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  41144. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  41145. /* A 256 bit key, AES_128 will use the first 128 bit*/
  41146. byte *key = (byte*)"01234567890123456789012345678901";
  41147. /* A 128 bit IV */
  41148. byte *iv = (byte*)"0123456789012345";
  41149. int ivSz = AES_BLOCK_SIZE;
  41150. /* Message to be encrypted */
  41151. byte *plaintxt = (byte*)"for things to change you have to change";
  41152. /* Additional non-confidential data */
  41153. byte *aad = (byte*)"Don't spend major time on minor things.";
  41154. unsigned char tag[AES_BLOCK_SIZE] = {0};
  41155. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  41156. int aadSz = (int)XSTRLEN((char*)aad);
  41157. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  41158. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  41159. int ciphertxtSz = 0;
  41160. int decryptedtxtSz = 0;
  41161. int len = 0;
  41162. int i = 0;
  41163. EVP_CIPHER_CTX en[2];
  41164. EVP_CIPHER_CTX de[2];
  41165. printf(testingFmt, "wolfssl_EVP_aes_gcm");
  41166. for (i = 0; i < 2; i++) {
  41167. EVP_CIPHER_CTX_init(&en[i]);
  41168. if (i == 0) {
  41169. /* Default uses 96-bits IV length */
  41170. #ifdef WOLFSSL_AES_128
  41171. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  41172. #elif defined(WOLFSSL_AES_192)
  41173. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  41174. #elif defined(WOLFSSL_AES_256)
  41175. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  41176. #endif
  41177. }
  41178. else {
  41179. #ifdef WOLFSSL_AES_128
  41180. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  41181. #elif defined(WOLFSSL_AES_192)
  41182. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  41183. #elif defined(WOLFSSL_AES_256)
  41184. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  41185. #endif
  41186. /* non-default must to set the IV length first */
  41187. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  41188. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  41189. }
  41190. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  41191. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  41192. ciphertxtSz = len;
  41193. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  41194. ciphertxtSz += len;
  41195. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  41196. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  41197. EVP_CIPHER_CTX_init(&de[i]);
  41198. if (i == 0) {
  41199. /* Default uses 96-bits IV length */
  41200. #ifdef WOLFSSL_AES_128
  41201. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  41202. #elif defined(WOLFSSL_AES_192)
  41203. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  41204. #elif defined(WOLFSSL_AES_256)
  41205. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  41206. #endif
  41207. }
  41208. else {
  41209. #ifdef WOLFSSL_AES_128
  41210. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  41211. #elif defined(WOLFSSL_AES_192)
  41212. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  41213. #elif defined(WOLFSSL_AES_256)
  41214. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  41215. #endif
  41216. /* non-default must to set the IV length first */
  41217. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  41218. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  41219. }
  41220. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  41221. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  41222. decryptedtxtSz = len;
  41223. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  41224. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  41225. decryptedtxtSz += len;
  41226. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  41227. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  41228. /* modify tag*/
  41229. tag[AES_BLOCK_SIZE-1]+=0xBB;
  41230. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  41231. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  41232. /* fail due to wrong tag */
  41233. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  41234. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  41235. AssertIntEQ(0, len);
  41236. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  41237. }
  41238. test_wolfssl_EVP_aes_gcm_zeroLen();
  41239. printf(resultFmt, passed);
  41240. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  41241. }
  41242. #ifndef NO_BIO
  41243. static void test_wolfSSL_PEM_X509_INFO_read_bio(void)
  41244. {
  41245. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41246. BIO* bio;
  41247. X509_INFO* info;
  41248. STACK_OF(X509_INFO)* sk;
  41249. char* subject;
  41250. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  41251. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  41252. printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
  41253. AssertNotNull(bio = BIO_new(BIO_s_file()));
  41254. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  41255. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  41256. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  41257. /* using dereference to maintain testing for Apache port*/
  41258. AssertNotNull(info = sk_X509_INFO_pop(sk));
  41259. AssertNotNull(info->x_pkey);
  41260. AssertNotNull(info->x_pkey->dec_pkey);
  41261. AssertIntEQ(EVP_PKEY_bits(info->x_pkey->dec_pkey), 2048);
  41262. AssertNotNull(subject =
  41263. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  41264. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  41265. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  41266. X509_INFO_free(info);
  41267. AssertNotNull(info = sk_X509_INFO_pop(sk));
  41268. AssertNotNull(subject =
  41269. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  41270. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  41271. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  41272. X509_INFO_free(info);
  41273. AssertNull(info = sk_X509_INFO_pop(sk));
  41274. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  41275. BIO_free(bio);
  41276. printf(resultFmt, passed);
  41277. #endif
  41278. }
  41279. #endif /* !NO_BIO */
  41280. static void test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  41281. {
  41282. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41283. X509 *x509;
  41284. X509_NAME* name;
  41285. int idx = 0;
  41286. X509_NAME_ENTRY *ne;
  41287. ASN1_OBJECT *object = NULL;
  41288. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
  41289. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  41290. AssertNotNull(x509);
  41291. name = X509_get_subject_name(x509);
  41292. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  41293. AssertIntGE(idx, 0);
  41294. ne = X509_NAME_get_entry(name, idx);
  41295. AssertNotNull(ne);
  41296. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  41297. X509_free(x509);
  41298. printf(resultFmt, passed);
  41299. #endif
  41300. }
  41301. static void test_wolfSSL_ASN1_INTEGER_get_set(void)
  41302. {
  41303. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  41304. ASN1_INTEGER *a;
  41305. long val;
  41306. int ret;
  41307. printf(testingFmt, "test_wolfSSL_ASN1_INTEGER_get_set");
  41308. a = ASN1_INTEGER_new();
  41309. val = 0;
  41310. ret = ASN1_INTEGER_set(NULL, val);
  41311. AssertIntEQ(ret, 0);
  41312. ASN1_INTEGER_free(a);
  41313. /* 0 */
  41314. a = ASN1_INTEGER_new();
  41315. val = 0;
  41316. ret = ASN1_INTEGER_set(a, val);
  41317. AssertIntEQ(ret, 1);
  41318. AssertIntEQ(ASN1_INTEGER_get(a), val);
  41319. ASN1_INTEGER_free(a);
  41320. /* 40 */
  41321. a = ASN1_INTEGER_new();
  41322. val = 40;
  41323. ret = ASN1_INTEGER_set(a, val);
  41324. AssertIntEQ(ret, 1);
  41325. AssertIntEQ(ASN1_INTEGER_get(a), val);
  41326. ASN1_INTEGER_free(a);
  41327. /* -40 */
  41328. a = ASN1_INTEGER_new();
  41329. val = -40;
  41330. ret = ASN1_INTEGER_set(a, val);
  41331. AssertIntEQ(ret, 1);
  41332. AssertIntEQ(ASN1_INTEGER_get(a), val);
  41333. ASN1_INTEGER_free(a);
  41334. /* 128 */
  41335. a = ASN1_INTEGER_new();
  41336. val = 128;
  41337. ret = ASN1_INTEGER_set(a, val);
  41338. AssertIntEQ(ret, 1);
  41339. AssertIntEQ(ASN1_INTEGER_get(a), val);
  41340. ASN1_INTEGER_free(a);
  41341. /* -128 */
  41342. a = ASN1_INTEGER_new();
  41343. val = -128;
  41344. ret = ASN1_INTEGER_set(a, val);
  41345. AssertIntEQ(ret, 1);
  41346. AssertIntEQ(ASN1_INTEGER_get(a), val);
  41347. ASN1_INTEGER_free(a);
  41348. /* 200 */
  41349. a = ASN1_INTEGER_new();
  41350. val = 200;
  41351. ret = ASN1_INTEGER_set(a, val);
  41352. AssertIntEQ(ret, 1);
  41353. AssertIntEQ(ASN1_INTEGER_get(a), val);
  41354. ASN1_INTEGER_free(a);
  41355. /* int max (2147483647) */
  41356. a = ASN1_INTEGER_new();
  41357. val = 2147483647;
  41358. ret = ASN1_INTEGER_set(a, val);
  41359. AssertIntEQ(ret, 1);
  41360. AssertIntEQ(ASN1_INTEGER_get(a), val);
  41361. ASN1_INTEGER_free(a);
  41362. /* int min (-2147483648) */
  41363. a = ASN1_INTEGER_new();
  41364. val = -2147483647 - 1;
  41365. ret = ASN1_INTEGER_set(a, val);
  41366. AssertIntEQ(ret, 1);
  41367. AssertIntEQ(ASN1_INTEGER_get(a), val);
  41368. ASN1_INTEGER_free(a);
  41369. printf(resultFmt, passed);
  41370. #endif
  41371. }
  41372. static void test_wolfSSL_X509_STORE_get1_certs(void)
  41373. {
  41374. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  41375. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41376. X509_STORE_CTX *storeCtx;
  41377. X509_STORE *store;
  41378. X509 *caX509;
  41379. X509 *svrX509;
  41380. X509_NAME *subject;
  41381. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  41382. printf(testingFmt, "wolfSSL_X509_STORE_get1_certs()");
  41383. AssertNotNull(caX509 =
  41384. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  41385. AssertNotNull((svrX509 =
  41386. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  41387. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  41388. AssertNotNull(store = X509_STORE_new());
  41389. AssertNotNull(subject = X509_get_subject_name(caX509));
  41390. /* Errors */
  41391. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  41392. AssertNull(X509_STORE_get1_certs(NULL, subject));
  41393. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  41394. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  41395. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  41396. /* Should find the cert */
  41397. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  41398. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  41399. sk_X509_pop_free(certs, NULL);
  41400. /* Should not find the cert */
  41401. AssertNotNull(subject = X509_get_subject_name(svrX509));
  41402. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  41403. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  41404. sk_X509_pop_free(certs, NULL);
  41405. X509_STORE_free(store);
  41406. X509_STORE_CTX_free(storeCtx);
  41407. X509_free(svrX509);
  41408. X509_free(caX509);
  41409. printf(resultFmt, passed);
  41410. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  41411. }
  41412. /* Testing code used in dpp.c in hostap */
  41413. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41414. typedef struct {
  41415. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  41416. * as an OID identifying the curve */
  41417. X509_ALGOR *alg;
  41418. /* Compressed format public key per ANSI X9.63 */
  41419. ASN1_BIT_STRING *pub_key;
  41420. } DPP_BOOTSTRAPPING_KEY;
  41421. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  41422. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  41423. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  41424. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  41425. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  41426. #endif
  41427. static void test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  41428. {
  41429. /* Testing code used in dpp.c in hostap */
  41430. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41431. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41432. EC_KEY *eckey;
  41433. EVP_PKEY *key;
  41434. size_t len;
  41435. unsigned char *der = NULL;
  41436. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  41437. const unsigned char *in = ecc_clikey_der_256;
  41438. const EC_GROUP *group;
  41439. const EC_POINT *point;
  41440. int nid;
  41441. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  41442. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  41443. (long)sizeof_ecc_clikey_der_256));
  41444. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  41445. AssertNotNull(group = EC_KEY_get0_group(eckey));
  41446. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  41447. nid = EC_GROUP_get_curve_name(group);
  41448. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  41449. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  41450. #ifdef HAVE_COMP_KEY
  41451. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  41452. NULL, 0, NULL)), 0);
  41453. #else
  41454. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  41455. NULL, 0, NULL)), 0);
  41456. #endif
  41457. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  41458. #ifdef HAVE_COMP_KEY
  41459. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  41460. der, len, NULL), len);
  41461. #else
  41462. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  41463. der, len, NULL), len);
  41464. #endif
  41465. bootstrap->pub_key->data = der;
  41466. bootstrap->pub_key->length = (int)len;
  41467. /* Not actually used */
  41468. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  41469. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  41470. der = NULL;
  41471. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  41472. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  41473. EVP_PKEY_free(key);
  41474. EC_KEY_free(eckey);
  41475. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  41476. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41477. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  41478. }
  41479. static void test_wolfSSL_i2c_ASN1_INTEGER(void)
  41480. {
  41481. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  41482. ASN1_INTEGER *a;
  41483. unsigned char *pp,*tpp;
  41484. int ret;
  41485. printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
  41486. a = wolfSSL_ASN1_INTEGER_new();
  41487. /* 40 */
  41488. a->intData[0] = ASN_INTEGER;
  41489. a->intData[1] = 1;
  41490. a->intData[2] = 40;
  41491. ret = i2c_ASN1_INTEGER(a, NULL);
  41492. AssertIntEQ(ret, 1);
  41493. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  41494. DYNAMIC_TYPE_TMP_BUFFER));
  41495. tpp = pp;
  41496. XMEMSET(pp, 0, ret + 1);
  41497. i2c_ASN1_INTEGER(a, &pp);
  41498. pp--;
  41499. AssertIntEQ(*pp, 40);
  41500. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41501. /* 128 */
  41502. a->intData[0] = ASN_INTEGER;
  41503. a->intData[1] = 1;
  41504. a->intData[2] = 128;
  41505. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  41506. AssertIntEQ(ret, 2);
  41507. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  41508. DYNAMIC_TYPE_TMP_BUFFER));
  41509. tpp = pp;
  41510. XMEMSET(pp, 0, ret + 1);
  41511. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  41512. pp--;
  41513. AssertIntEQ(*(pp--), 128);
  41514. AssertIntEQ(*pp, 0);
  41515. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41516. /* -40 */
  41517. a->intData[0] = ASN_INTEGER;
  41518. a->intData[1] = 1;
  41519. a->intData[2] = 40;
  41520. a->negative = 1;
  41521. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  41522. AssertIntEQ(ret, 1);
  41523. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  41524. DYNAMIC_TYPE_TMP_BUFFER));
  41525. tpp = pp;
  41526. XMEMSET(pp, 0, ret + 1);
  41527. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  41528. pp--;
  41529. AssertIntEQ(*pp, 216);
  41530. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41531. /* -128 */
  41532. a->intData[0] = ASN_INTEGER;
  41533. a->intData[1] = 1;
  41534. a->intData[2] = 128;
  41535. a->negative = 1;
  41536. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  41537. AssertIntEQ(ret, 1);
  41538. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  41539. DYNAMIC_TYPE_TMP_BUFFER));
  41540. tpp = pp;
  41541. XMEMSET(pp, 0, ret + 1);
  41542. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  41543. pp--;
  41544. AssertIntEQ(*pp, 128);
  41545. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41546. /* -200 */
  41547. a->intData[0] = ASN_INTEGER;
  41548. a->intData[1] = 1;
  41549. a->intData[2] = 200;
  41550. a->negative = 1;
  41551. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  41552. AssertIntEQ(ret, 2);
  41553. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  41554. DYNAMIC_TYPE_TMP_BUFFER));
  41555. tpp = pp;
  41556. XMEMSET(pp, 0, ret + 1);
  41557. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  41558. pp--;
  41559. AssertIntEQ(*(pp--), 56);
  41560. AssertIntEQ(*pp, 255);
  41561. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41562. wolfSSL_ASN1_INTEGER_free(a);
  41563. printf(resultFmt, passed);
  41564. #endif /* OPENSSL_EXTRA && !NO_ASN */
  41565. }
  41566. #ifndef NO_INLINE
  41567. #define WOLFSSL_MISC_INCLUDED
  41568. #include <wolfcrypt/src/misc.c>
  41569. #else
  41570. #include <wolfssl/wolfcrypt/misc.h>
  41571. #endif
  41572. static int test_ForceZero(void)
  41573. {
  41574. unsigned char data[32];
  41575. unsigned int i, j, len;
  41576. /* Test case with 0 length */
  41577. ForceZero(data, 0);
  41578. /* Test ForceZero */
  41579. for (i = 0; i < sizeof(data); i++) {
  41580. for (len = 1; len < sizeof(data) - i; len++) {
  41581. for (j = 0; j < sizeof(data); j++)
  41582. data[j] = j + 1;
  41583. ForceZero(data + i, len);
  41584. for (j = 0; j < sizeof(data); j++) {
  41585. if (j < i || j >= i + len) {
  41586. if (data[j] == 0x00)
  41587. return -10200;
  41588. }
  41589. else if (data[j] != 0x00)
  41590. return -10201;
  41591. }
  41592. }
  41593. }
  41594. return 0;
  41595. }
  41596. #ifndef NO_BIO
  41597. static void test_wolfSSL_X509_print(void)
  41598. {
  41599. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  41600. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  41601. X509 *x509;
  41602. BIO *bio;
  41603. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  41604. const X509_ALGOR *cert_sig_alg;
  41605. #endif
  41606. printf(testingFmt, "wolfSSL_X509_print");
  41607. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  41608. AssertNotNull(x509);
  41609. /* print to memory */
  41610. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  41611. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  41612. #if defined(WOLFSSL_QT)
  41613. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3113);
  41614. #else
  41615. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3103);
  41616. #endif
  41617. BIO_free(bio);
  41618. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  41619. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  41620. /* Print signature */
  41621. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  41622. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  41623. #endif
  41624. /* print to stdout */
  41625. #if !defined(NO_WOLFSSL_DIR)
  41626. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  41627. #endif
  41628. /* print again */
  41629. AssertIntEQ(X509_print_fp(stdout, x509), SSL_SUCCESS);
  41630. X509_free(x509);
  41631. BIO_free(bio);
  41632. printf(resultFmt, passed);
  41633. #endif
  41634. }
  41635. static void test_wolfSSL_RSA_print(void)
  41636. {
  41637. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  41638. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41639. !defined(HAVE_FAST_RSA) && !defined(NO_BIO)
  41640. BIO *bio;
  41641. WOLFSSL_RSA* rsa = NULL;
  41642. printf(testingFmt, "wolfSSL_RSA_print");
  41643. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41644. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  41645. AssertIntEQ(RSA_print(bio, rsa, 0), SSL_SUCCESS);
  41646. BIO_free(bio);
  41647. RSA_free(rsa);
  41648. printf(resultFmt, passed);
  41649. #endif
  41650. }
  41651. static void test_wolfSSL_BIO_get_len(void)
  41652. {
  41653. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  41654. BIO *bio = NULL;
  41655. const char txt[] = "Some example text to push to the BIO.";
  41656. printf(testingFmt, "wolfSSL_BIO_get_len");
  41657. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  41658. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  41659. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  41660. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  41661. BIO_free(bio);
  41662. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  41663. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  41664. BIO_free(bio);
  41665. printf(resultFmt, passed);
  41666. #endif
  41667. }
  41668. static void test_wolfSSL_ASN1_STRING_print(void){
  41669. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  41670. ASN1_STRING* asnStr = NULL;
  41671. const char HELLO_DATA[]= \
  41672. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  41673. #define MAX_UNPRINTABLE_CHAR 32
  41674. #define MAX_BUF 255
  41675. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  41676. unsigned char expected[sizeof(unprintableData)+1];
  41677. unsigned char rbuf[MAX_BUF];
  41678. BIO *bio;
  41679. int p_len, i;
  41680. printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
  41681. /* setup */
  41682. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  41683. unprintableData[i] = HELLO_DATA[i];
  41684. expected[i] = HELLO_DATA[i];
  41685. }
  41686. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  41687. unprintableData[sizeof(HELLO_DATA)+i] = i;
  41688. if (i == (int)'\n' || i == (int)'\r')
  41689. expected[sizeof(HELLO_DATA)+i] = i;
  41690. else
  41691. expected[sizeof(HELLO_DATA)+i] = '.';
  41692. }
  41693. unprintableData[sizeof(unprintableData)-1] = '\0';
  41694. expected[sizeof(expected)-1] = '\0';
  41695. XMEMSET(rbuf, 0, MAX_BUF);
  41696. bio = BIO_new(BIO_s_mem());
  41697. BIO_set_write_buf_size(bio, MAX_BUF);
  41698. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  41699. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  41700. (int)sizeof(unprintableData));
  41701. /* test */
  41702. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  41703. AssertIntEQ(p_len, 46);
  41704. BIO_read(bio, (void*)rbuf, 46);
  41705. AssertStrEQ((char*)rbuf, (const char*)expected);
  41706. BIO_free(bio);
  41707. ASN1_STRING_free(asnStr);
  41708. printf(resultFmt, passed);
  41709. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  41710. }
  41711. #endif /* !NO_BIO */
  41712. static void test_wolfSSL_ASN1_get_object(void)
  41713. {
  41714. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41715. const unsigned char* derBuf = cliecc_cert_der_256;
  41716. int len = sizeof_cliecc_cert_der_256;
  41717. long asnLen = 0;
  41718. int tag = 0, cls = 0;
  41719. ASN1_OBJECT *a;
  41720. printf(testingFmt, "wolfSSL_ASN1_get_object()");
  41721. /* Read a couple TLV triplets and make sure they match the expected values */
  41722. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  41723. AssertIntEQ(asnLen, 841);
  41724. AssertIntEQ(tag, 0x10);
  41725. AssertIntEQ(cls, 0);
  41726. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  41727. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  41728. AssertIntEQ(asnLen, 750);
  41729. AssertIntEQ(tag, 0x10);
  41730. AssertIntEQ(cls, 0);
  41731. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  41732. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  41733. AssertIntEQ(asnLen, 3);
  41734. AssertIntEQ(tag, 0);
  41735. AssertIntEQ(cls, 0x80);
  41736. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  41737. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  41738. AssertIntEQ(asnLen, 1);
  41739. AssertIntEQ(tag, 0x2);
  41740. AssertIntEQ(cls, 0);
  41741. derBuf += asnLen;
  41742. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  41743. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  41744. AssertIntEQ(asnLen, 9);
  41745. AssertIntEQ(tag, 0x2);
  41746. AssertIntEQ(cls, 0);
  41747. derBuf += asnLen;
  41748. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  41749. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  41750. AssertIntEQ(asnLen, 10);
  41751. AssertIntEQ(tag, 0x10);
  41752. AssertIntEQ(cls, 0);
  41753. /* Read an ASN OBJECT */
  41754. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  41755. ASN1_OBJECT_free(a);
  41756. printf(resultFmt, passed);
  41757. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  41758. }
  41759. static void test_wolfSSL_RSA_verify(void)
  41760. {
  41761. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  41762. !defined(NO_FILESYSTEM) && defined(HAVE_CRL)
  41763. #ifndef NO_BIO
  41764. XFILE fp;
  41765. RSA *pKey, *pubKey;
  41766. X509 *cert;
  41767. const char *text = "Hello wolfSSL !";
  41768. unsigned char hash[SHA256_DIGEST_LENGTH];
  41769. unsigned char signature[2048/8];
  41770. unsigned int signatureLength;
  41771. byte *buf;
  41772. BIO *bio;
  41773. SHA256_CTX c;
  41774. EVP_PKEY *evpPkey, *evpPubkey;
  41775. size_t sz;
  41776. printf(testingFmt, "wolfSSL_RSA_verify");
  41777. /* generate hash */
  41778. SHA256_Init(&c);
  41779. SHA256_Update(&c, text, strlen(text));
  41780. SHA256_Final(hash, &c);
  41781. #ifdef WOLFSSL_SMALL_STACK_CACHE
  41782. /* workaround for small stack cache case */
  41783. wc_Sha256Free((wc_Sha256*)&c);
  41784. #endif
  41785. /* read privete key file */
  41786. fp = XFOPEN(svrKeyFile, "rb");
  41787. AssertTrue((fp != XBADFILE));
  41788. XFSEEK(fp, 0, XSEEK_END);
  41789. sz = XFTELL(fp);
  41790. XREWIND(fp);
  41791. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  41792. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  41793. XFCLOSE(fp);
  41794. /* read private key and sign hash data */
  41795. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41796. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  41797. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  41798. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  41799. signature, &signatureLength, pKey), SSL_SUCCESS);
  41800. /* read public key and verify signed data */
  41801. fp = XFOPEN(svrCertFile,"rb");
  41802. AssertTrue((fp != XBADFILE));
  41803. cert = PEM_read_X509(fp, 0, 0, 0 );
  41804. XFCLOSE(fp);
  41805. evpPubkey = X509_get_pubkey(cert);
  41806. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  41807. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  41808. signatureLength, pubKey), SSL_SUCCESS);
  41809. RSA_free(pKey);
  41810. EVP_PKEY_free(evpPkey);
  41811. RSA_free(pubKey);
  41812. EVP_PKEY_free(evpPubkey);
  41813. X509_free(cert);
  41814. BIO_free(bio);
  41815. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  41816. printf(resultFmt, passed);
  41817. #endif
  41818. #endif
  41819. }
  41820. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  41821. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  41822. static void test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  41823. {
  41824. X509* x509 = NULL;
  41825. BIGNUM* serial_number = NULL;
  41826. X509_NAME* name = NULL;
  41827. time_t epoch_off = 0;
  41828. ASN1_INTEGER* asn1_serial_number;
  41829. long not_before, not_after;
  41830. AssertNotNull(x509 = X509_new());
  41831. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  41832. AssertNotNull(serial_number = BN_new());
  41833. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  41834. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  41835. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  41836. /* version 3 */
  41837. AssertIntNE(X509_set_version(x509, 2L), 0);
  41838. AssertNotNull(name = X509_NAME_new());
  41839. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  41840. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  41841. AssertIntNE(X509_set_subject_name(x509, name), 0);
  41842. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  41843. not_before = (long)XTIME(NULL);
  41844. not_after = not_before + (365 * 24 * 60 * 60);
  41845. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  41846. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  41847. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  41848. BN_free(serial_number);
  41849. X509_NAME_free(name);
  41850. X509_free(x509);
  41851. }
  41852. #endif
  41853. static void test_openssl_generate_key_and_cert(void)
  41854. {
  41855. #if defined(OPENSSL_EXTRA)
  41856. #if !defined(NO_RSA)
  41857. {
  41858. EVP_PKEY* pkey = EVP_PKEY_new();
  41859. int key_length = 2048;
  41860. BIGNUM* exponent = BN_new();
  41861. RSA* rsa = RSA_new();
  41862. AssertNotNull(pkey);
  41863. AssertNotNull(exponent);
  41864. AssertNotNull(rsa);
  41865. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  41866. #ifndef WOLFSSL_KEY_GEN
  41867. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), WOLFSSL_FAILURE);
  41868. #if defined(USE_CERT_BUFFERS_1024)
  41869. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  41870. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  41871. key_length = 1024;
  41872. #elif defined(USE_CERT_BUFFERS_2048)
  41873. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  41874. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  41875. #else
  41876. RSA_free(rsa);
  41877. rsa = NULL;
  41878. #endif
  41879. #else
  41880. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  41881. #endif
  41882. if (rsa) {
  41883. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  41884. BN_free(exponent);
  41885. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  41886. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  41887. test_openssl_make_self_signed_certificate(pkey);
  41888. #endif
  41889. }
  41890. EVP_PKEY_free(pkey);
  41891. }
  41892. #endif /* !NO_RSA */
  41893. #ifdef HAVE_ECC
  41894. {
  41895. EVP_PKEY* pkey = EVP_PKEY_new();
  41896. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  41897. AssertNotNull(pkey);
  41898. AssertNotNull(ec_key);
  41899. #ifndef NO_WOLFSSL_STUB
  41900. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  41901. #endif
  41902. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  41903. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  41904. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  41905. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  41906. test_openssl_make_self_signed_certificate(pkey);
  41907. #endif
  41908. EVP_PKEY_free(pkey);
  41909. }
  41910. #endif /* HAVE_ECC */
  41911. #endif /* OPENSSL_EXTRA */
  41912. }
  41913. static void test_stubs_are_stubs(void)
  41914. {
  41915. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  41916. WOLFSSL_CTX* ctx = NULL;
  41917. WOLFSSL_CTX* ctxN = NULL;
  41918. #ifndef NO_WOLFSSL_CLIENT
  41919. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  41920. AssertNotNull(ctx);
  41921. #elif !defined(NO_WOLFSSL_SERVER)
  41922. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  41923. AssertNotNull(ctx);
  41924. #else
  41925. return;
  41926. #endif
  41927. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  41928. AssertIntEQ((int) x(z), 0)
  41929. /* test logic, all stubs return same result regardless of ctx being NULL
  41930. * as there are no sanity checks, it's just a stub! If at some
  41931. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  41932. * if invalid inputs are supplied. Test calling both
  41933. * with and without valid inputs, if a stub functionality remains unchanged.
  41934. */
  41935. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  41936. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  41937. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  41938. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  41939. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  41940. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  41941. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  41942. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  41943. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  41944. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  41945. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  41946. wolfSSL_CTX_free(ctx);
  41947. ctx = NULL;
  41948. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  41949. }
  41950. static void test_CONF_modules_xxx(void)
  41951. {
  41952. #if defined(OPENSSL_EXTRA)
  41953. CONF_modules_free();
  41954. AssertTrue(1); /* to confirm previous call gives no harm */
  41955. CONF_modules_unload(0);
  41956. AssertTrue(1);
  41957. CONF_modules_unload(1);
  41958. AssertTrue(1);
  41959. CONF_modules_unload(-1);
  41960. AssertTrue(1);
  41961. #endif /* OPENSSL_EXTRA */
  41962. }
  41963. static void test_CRYPTO_set_dynlock_xxx(void)
  41964. {
  41965. #if defined(OPENSSL_EXTRA)
  41966. printf(testingFmt, "CRYPTO_set_dynlock_xxx()");
  41967. CRYPTO_set_dynlock_create_callback(
  41968. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  41969. CRYPTO_set_dynlock_create_callback(
  41970. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  41971. CRYPTO_set_dynlock_destroy_callback(
  41972. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  41973. CRYPTO_set_dynlock_destroy_callback(
  41974. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  41975. CRYPTO_set_dynlock_lock_callback(
  41976. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  41977. CRYPTO_set_dynlock_lock_callback(
  41978. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  41979. AssertTrue(1); /* to confirm previous call gives no harm */
  41980. printf(resultFmt, passed);
  41981. #endif /* OPENSSL_EXTRA */
  41982. }
  41983. static void test_CRYPTO_THREADID_xxx(void)
  41984. {
  41985. #if defined(OPENSSL_EXTRA)
  41986. printf(testingFmt, "CRYPTO_THREADID_xxx()");
  41987. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  41988. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  41989. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  41990. printf(resultFmt, passed);
  41991. #endif /* OPENSSL_EXTRA */
  41992. }
  41993. static void test_ENGINE_cleanup(void)
  41994. {
  41995. #if defined(OPENSSL_EXTRA)
  41996. printf(testingFmt, "ENGINE_cleanup()");
  41997. ENGINE_cleanup();
  41998. AssertTrue(1); /* to confirm previous call gives no harm */
  41999. printf(resultFmt, passed);
  42000. #endif /* OPENSSL_EXTRA */
  42001. }
  42002. static void test_wolfSSL_CTX_LoadCRL(void)
  42003. {
  42004. #ifdef HAVE_CRL
  42005. WOLFSSL_CTX* ctx = NULL;
  42006. const char* badPath = "dummypath";
  42007. const char* validPath = "./certs/crl";
  42008. int derType = WOLFSSL_FILETYPE_ASN1;
  42009. int pemType = WOLFSSL_FILETYPE_PEM;
  42010. int monitor = WOLFSSL_CRL_MONITOR;
  42011. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  42012. BAD_FUNC_ARG)
  42013. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  42014. WOLFSSL_SUCCESS)
  42015. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  42016. #ifndef NO_WOLFSSL_CLIENT
  42017. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42018. #elif !defined(NO_WOLFSSL_SERVER)
  42019. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  42020. #else
  42021. return;
  42022. #endif
  42023. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  42024. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  42025. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  42026. wolfSSL_CTX_free(ctx);
  42027. ctx = NULL;
  42028. #endif
  42029. }
  42030. static void test_SetTmpEC_DHE_Sz(void)
  42031. {
  42032. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  42033. WOLFSSL_CTX *ctx;
  42034. WOLFSSL *ssl;
  42035. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  42036. AssertNotNull(ctx);
  42037. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  42038. ssl = wolfSSL_new(ctx);
  42039. AssertNotNull(ssl);
  42040. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  42041. wolfSSL_free(ssl);
  42042. wolfSSL_CTX_free(ctx);
  42043. #endif
  42044. }
  42045. static void test_wolfSSL_CTX_get0_privatekey(void)
  42046. {
  42047. #ifdef OPENSSL_ALL
  42048. WOLFSSL_CTX* ctx = NULL;
  42049. printf(testingFmt, "wolfSSL_CTX_get0_privatekey()");
  42050. #ifndef NO_RSA
  42051. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  42052. AssertNull(SSL_CTX_get0_privatekey(ctx));
  42053. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  42054. WOLFSSL_FILETYPE_PEM));
  42055. AssertNull(SSL_CTX_get0_privatekey(ctx));
  42056. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  42057. WOLFSSL_FILETYPE_PEM));
  42058. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  42059. wolfSSL_CTX_free(ctx);
  42060. #endif
  42061. #ifdef HAVE_ECC
  42062. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  42063. AssertNull(SSL_CTX_get0_privatekey(ctx));
  42064. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  42065. WOLFSSL_FILETYPE_PEM));
  42066. AssertNull(SSL_CTX_get0_privatekey(ctx));
  42067. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  42068. WOLFSSL_FILETYPE_PEM));
  42069. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  42070. wolfSSL_CTX_free(ctx);
  42071. #endif
  42072. printf(resultFmt, passed);
  42073. #endif
  42074. }
  42075. static void test_wolfSSL_dtls_set_mtu(void)
  42076. {
  42077. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  42078. defined(WOLFSSL_DTLS)
  42079. WOLFSSL_CTX* ctx = NULL;
  42080. WOLFSSL* ssl = NULL;
  42081. const char* testCertFile;
  42082. const char* testKeyFile;
  42083. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  42084. #ifndef NO_RSA
  42085. testCertFile = svrCertFile;
  42086. testKeyFile = svrKeyFile;
  42087. #elif defined(HAVE_ECC)
  42088. testCertFile = eccCertFile;
  42089. testKeyFile = eccKeyFile;
  42090. #endif
  42091. if (testCertFile != NULL && testKeyFile != NULL) {
  42092. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  42093. WOLFSSL_FILETYPE_PEM));
  42094. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  42095. WOLFSSL_FILETYPE_PEM));
  42096. }
  42097. AssertNotNull(ssl = wolfSSL_new(ctx));
  42098. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  42099. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  42100. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  42101. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  42102. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  42103. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  42104. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  42105. wolfSSL_free(ssl);
  42106. wolfSSL_CTX_free(ctx);
  42107. printf(testingFmt, "wolfSSL_dtls_set_mtu()");
  42108. printf(resultFmt, passed);
  42109. #endif
  42110. }
  42111. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  42112. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  42113. !defined(WOLFSSL_NO_CLIENT_AUTH))
  42114. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  42115. {
  42116. int ret;
  42117. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  42118. printf("loading cert %s failed\n", certA);
  42119. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  42120. return -1;
  42121. }
  42122. return 0;
  42123. }
  42124. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  42125. {
  42126. int ret;
  42127. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  42128. != WOLFSSL_SUCCESS) {
  42129. printf("could not verify the cert: %s\n", certA);
  42130. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  42131. return -1;
  42132. } else {
  42133. printf("successfully verified: %s\n", certA);
  42134. }
  42135. return 0;
  42136. }
  42137. #define LOAD_ONE_CA(a, b, c, d) \
  42138. do { \
  42139. a = load_ca_into_cm(c, d); \
  42140. if (a != 0) \
  42141. return b; \
  42142. else \
  42143. b--; \
  42144. } while(0)
  42145. #define VERIFY_ONE_CERT(a, b, c, d) \
  42146. do { \
  42147. a = verify_cert_with_cm(c, d); \
  42148. if (a != 0) \
  42149. return b; \
  42150. else \
  42151. b--; \
  42152. } while(0)
  42153. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  42154. {
  42155. int ret;
  42156. int i = -1;
  42157. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  42158. char chainGArr[9][50] = {"certs/ca-cert.pem",
  42159. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  42160. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  42161. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  42162. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  42163. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  42164. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  42165. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  42166. "certs/test-pathlen/chainG-entity.pem"};
  42167. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  42168. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  42169. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  42170. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  42171. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  42172. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  42173. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  42174. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  42175. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  42176. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  42177. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  42178. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  42179. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  42180. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  42181. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  42182. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  42183. /* test validating the entity twice, should have no effect on pathLen since
  42184. * entity/leaf cert */
  42185. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  42186. return ret;
  42187. }
  42188. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  42189. {
  42190. int ret;
  42191. int i = -1;
  42192. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  42193. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  42194. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  42195. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  42196. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  42197. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  42198. */
  42199. char chainHArr[6][50] = {"certs/ca-cert.pem",
  42200. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  42201. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  42202. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  42203. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  42204. "certs/test-pathlen/chainH-entity.pem"};
  42205. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  42206. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  42207. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  42208. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  42209. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  42210. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  42211. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  42212. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  42213. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  42214. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  42215. return ret;
  42216. }
  42217. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  42218. {
  42219. int ret;
  42220. int i = -1;
  42221. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  42222. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  42223. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  42224. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  42225. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  42226. */
  42227. char chainIArr[5][50] = {"certs/ca-cert.pem",
  42228. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  42229. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  42230. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  42231. "certs/test-pathlen/chainI-entity.pem"};
  42232. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  42233. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  42234. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  42235. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  42236. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  42237. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  42238. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  42239. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  42240. return ret;
  42241. }
  42242. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  42243. {
  42244. int ret;
  42245. int i = -1;
  42246. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  42247. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  42248. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  42249. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  42250. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  42251. */
  42252. char chainJArr[6][50] = {"certs/ca-cert.pem",
  42253. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  42254. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  42255. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  42256. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  42257. "certs/test-pathlen/chainJ-entity.pem"};
  42258. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  42259. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  42260. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  42261. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  42262. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  42263. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  42264. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  42265. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  42266. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  42267. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  42268. return ret;
  42269. }
  42270. static int test_various_pathlen_chains(void)
  42271. {
  42272. int ret;
  42273. WOLFSSL_CERT_MANAGER* cm;
  42274. /* Test chain G (large chain with varying pathLens) */
  42275. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  42276. printf("cert manager new failed\n");
  42277. return -1;
  42278. }
  42279. AssertIntEQ(test_chainG(cm), 0);
  42280. ret = wolfSSL_CertManagerUnloadCAs(cm);
  42281. if (ret != WOLFSSL_SUCCESS)
  42282. return -1;
  42283. wolfSSL_CertManagerFree(cm);
  42284. /* end test chain G */
  42285. /* Test chain H (5 chain with same pathLens) */
  42286. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  42287. printf("cert manager new failed\n");
  42288. return -1;
  42289. }
  42290. AssertIntLT(test_chainH(cm), 0);
  42291. wolfSSL_CertManagerUnloadCAs(cm);
  42292. wolfSSL_CertManagerFree(cm);
  42293. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  42294. printf("cert manager new failed\n");
  42295. return -1;
  42296. }
  42297. ret = wolfSSL_CertManagerUnloadCAs(cm);
  42298. if (ret != WOLFSSL_SUCCESS)
  42299. return -1;
  42300. wolfSSL_CertManagerFree(cm);
  42301. /* end test chain H */
  42302. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  42303. * followed by 2 ICA's, should pass) */
  42304. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  42305. printf("cert manager new failed\n");
  42306. return -1;
  42307. }
  42308. AssertIntEQ(test_chainI(cm), 0);
  42309. wolfSSL_CertManagerUnloadCAs(cm);
  42310. wolfSSL_CertManagerFree(cm);
  42311. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  42312. printf("cert manager new failed\n");
  42313. return -1;
  42314. }
  42315. ret = wolfSSL_CertManagerUnloadCAs(cm);
  42316. if (ret != WOLFSSL_SUCCESS)
  42317. return -1;
  42318. wolfSSL_CertManagerFree(cm);
  42319. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  42320. * this time followed by 3 ICA's, should fail */
  42321. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  42322. printf("cert manager new failed\n");
  42323. return -1;
  42324. }
  42325. AssertIntLT(test_chainJ(cm), 0);
  42326. wolfSSL_CertManagerUnloadCAs(cm);
  42327. wolfSSL_CertManagerFree(cm);
  42328. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  42329. printf("cert manager new failed\n");
  42330. return -1;
  42331. }
  42332. ret = wolfSSL_CertManagerUnloadCAs(cm);
  42333. wolfSSL_CertManagerFree(cm);
  42334. return ret;
  42335. }
  42336. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  42337. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  42338. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  42339. {
  42340. byte ekm[100] = {0};
  42341. (void)ctx;
  42342. /* Succes Cases */
  42343. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  42344. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  42345. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  42346. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  42347. /* Use some random context */
  42348. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  42349. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  42350. /* Failure cases */
  42351. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  42352. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  42353. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  42354. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  42355. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  42356. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  42357. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  42358. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  42359. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  42360. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  42361. return 0;
  42362. }
  42363. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  42364. {
  42365. wolfSSL_KeepArrays(ssl);
  42366. }
  42367. static void test_export_keying_material(void)
  42368. {
  42369. #ifndef SINGLE_THREADED
  42370. tcp_ready ready;
  42371. callback_functions clientCb;
  42372. func_args client_args;
  42373. func_args server_args;
  42374. THREAD_TYPE serverThread;
  42375. XMEMSET(&client_args, 0, sizeof(func_args));
  42376. XMEMSET(&server_args, 0, sizeof(func_args));
  42377. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  42378. #ifdef WOLFSSL_TIRTOS
  42379. fdOpenSession(Task_self());
  42380. #endif
  42381. StartTCP();
  42382. InitTcpReady(&ready);
  42383. #if defined(USE_WINDOWS_API)
  42384. /* use RNG to get random port if using windows */
  42385. ready.port = GetRandomPort();
  42386. #endif
  42387. server_args.signal = &ready;
  42388. client_args.signal = &ready;
  42389. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  42390. client_args.callbacks = &clientCb;
  42391. start_thread(test_server_nofail, &server_args, &serverThread);
  42392. wait_tcp_ready(&server_args);
  42393. test_client_nofail(&client_args, test_export_keying_material_cb);
  42394. join_thread(serverThread);
  42395. AssertTrue(client_args.return_code);
  42396. AssertTrue(server_args.return_code);
  42397. FreeTcpReady(&ready);
  42398. #ifdef WOLFSSL_TIRTOS
  42399. fdOpenSession(Task_self());
  42400. #endif
  42401. #endif /* !SINGLE_THREADED */
  42402. }
  42403. #endif /* HAVE_KEYING_MATERIAL */
  42404. static int test_wolfSSL_THREADID_hash(void)
  42405. {
  42406. int ret = 0;
  42407. unsigned long res;
  42408. #if defined(OPENSSL_EXTRA)
  42409. CRYPTO_THREADID id;
  42410. printf(testingFmt, "wolfSSL_THREADID_hash");
  42411. CRYPTO_THREADID_current(NULL);
  42412. AssertTrue(1);
  42413. res = CRYPTO_THREADID_hash(NULL);
  42414. AssertTrue( res == 0UL);
  42415. XMEMSET(&id, 0, sizeof(id));
  42416. res = CRYPTO_THREADID_hash(&id);
  42417. AssertTrue( res == 0UL);
  42418. printf(resultFmt, passed);
  42419. #endif /* OPENSSL_EXTRA */
  42420. (void)res;
  42421. return ret;
  42422. }
  42423. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  42424. {
  42425. int ret = 0;
  42426. WOLFSSL_CTX* ctx = NULL;
  42427. #if defined(OPENSSL_EXTRA)
  42428. printf(testingFmt, "SSL_CTX_set_ecdh_auto");
  42429. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  42430. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  42431. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  42432. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  42433. printf(resultFmt, passed);
  42434. #endif /* OPENSSL_EXTRA */
  42435. (void)ctx;
  42436. return ret;
  42437. }
  42438. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42439. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  42440. {
  42441. callback_functions* callbacks = NULL;
  42442. WOLFSSL_CTX* ctx = NULL;
  42443. WOLFSSL* ssl = NULL;
  42444. SOCKET_T sfd = 0;
  42445. SOCKET_T cfd = 0;
  42446. word16 port;
  42447. char msg[] = "I hear you fa shizzle!";
  42448. int len = (int) XSTRLEN(msg);
  42449. char input[1024];
  42450. int ret, err;
  42451. if (!args)
  42452. return 0;
  42453. ((func_args*)args)->return_code = TEST_FAIL;
  42454. callbacks = ((func_args*)args)->callbacks;
  42455. ctx = wolfSSL_CTX_new(callbacks->method());
  42456. #if defined(USE_WINDOWS_API)
  42457. port = ((func_args*)args)->signal->port;
  42458. #else
  42459. /* Let tcp_listen assign port */
  42460. port = 0;
  42461. #endif
  42462. #ifdef WOLFSSL_TIRTOS
  42463. fdOpenSession(Task_self());
  42464. #endif
  42465. AssertIntEQ(WOLFSSL_SUCCESS,
  42466. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  42467. AssertIntEQ(WOLFSSL_SUCCESS,
  42468. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  42469. WOLFSSL_FILETYPE_PEM));
  42470. AssertIntEQ(WOLFSSL_SUCCESS,
  42471. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  42472. WOLFSSL_FILETYPE_PEM));
  42473. if (callbacks->ctx_ready)
  42474. callbacks->ctx_ready(ctx);
  42475. ssl = wolfSSL_new(ctx);
  42476. AssertNotNull(ssl);
  42477. /* listen and accept */
  42478. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  42479. CloseSocket(sfd);
  42480. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  42481. if (callbacks->ssl_ready)
  42482. callbacks->ssl_ready(ssl);
  42483. do {
  42484. err = 0; /* Reset error */
  42485. ret = wolfSSL_accept(ssl);
  42486. if (ret != WOLFSSL_SUCCESS) {
  42487. err = wolfSSL_get_error(ssl, 0);
  42488. }
  42489. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  42490. if (ret != WOLFSSL_SUCCESS) {
  42491. wolfSSL_free(ssl);
  42492. wolfSSL_CTX_free(ctx);
  42493. CloseSocket(cfd);
  42494. ((func_args*)args)->return_code = TEST_FAIL;
  42495. return 0;
  42496. }
  42497. /* read and write data */
  42498. XMEMSET( input, 0, sizeof(input));
  42499. while (1) {
  42500. ret = wolfSSL_read(ssl, input, sizeof(input));
  42501. if (ret > 0) {
  42502. break;
  42503. }
  42504. else {
  42505. err = wolfSSL_get_error(ssl,ret);
  42506. if (err == WOLFSSL_ERROR_WANT_READ) {
  42507. continue;
  42508. }
  42509. break;
  42510. }
  42511. }
  42512. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  42513. do {
  42514. ret = wolfSSL_write(ssl, msg, len);
  42515. if (ret > 0) {
  42516. break;
  42517. }
  42518. } while (ret < 0);
  42519. }
  42520. /* bidirectional shutdown */
  42521. while ((ret = wolfSSL_shutdown(ssl)) != WOLFSSL_SUCCESS) {
  42522. continue;
  42523. }
  42524. /* wait for the peer to disconnect the tcp connection */
  42525. do {
  42526. ret = wolfSSL_read(ssl, input, sizeof(input));
  42527. err = wolfSSL_get_error(ssl, ret);
  42528. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  42529. /* detect TCP disconnect */
  42530. AssertIntLE(ret,WOLFSSL_FAILURE);
  42531. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  42532. ((func_args*)args)->return_code = TEST_SUCCESS;
  42533. wolfSSL_free(ssl);
  42534. wolfSSL_CTX_free(ctx);
  42535. CloseSocket(cfd);
  42536. return 0;
  42537. }
  42538. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  42539. {
  42540. callback_functions* callbacks = NULL;
  42541. WOLFSSL_CTX* ctx = NULL;
  42542. WOLFSSL* ssl = NULL;
  42543. SOCKET_T sfd = 0;
  42544. char msg[] = "hello wolfssl server!";
  42545. int len = (int) XSTRLEN(msg);
  42546. char input[1024];
  42547. int idx;
  42548. int ret, err;
  42549. if (!args)
  42550. return 0;
  42551. ((func_args*)args)->return_code = TEST_FAIL;
  42552. callbacks = ((func_args*)args)->callbacks;
  42553. ctx = wolfSSL_CTX_new(callbacks->method());
  42554. #ifdef WOLFSSL_TIRTOS
  42555. fdOpenSession(Task_self());
  42556. #endif
  42557. AssertIntEQ(WOLFSSL_SUCCESS,
  42558. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  42559. AssertIntEQ(WOLFSSL_SUCCESS,
  42560. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  42561. WOLFSSL_FILETYPE_PEM));
  42562. AssertIntEQ(WOLFSSL_SUCCESS,
  42563. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  42564. WOLFSSL_FILETYPE_PEM));
  42565. AssertNotNull((ssl = wolfSSL_new(ctx)));
  42566. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  42567. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  42568. do {
  42569. err = 0; /* Reset error */
  42570. ret = wolfSSL_connect(ssl);
  42571. if (ret != WOLFSSL_SUCCESS) {
  42572. err = wolfSSL_get_error(ssl, 0);
  42573. }
  42574. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  42575. ret = wolfSSL_write(ssl, msg, len);
  42576. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  42577. input[idx] = 0;
  42578. }
  42579. ret = wolfSSL_shutdown(ssl);
  42580. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  42581. ret = wolfSSL_shutdown(ssl);
  42582. }
  42583. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42584. ((func_args*)args)->return_code = TEST_SUCCESS;
  42585. wolfSSL_free(ssl);
  42586. wolfSSL_CTX_free(ctx);
  42587. CloseSocket(sfd);
  42588. return 0;
  42589. }
  42590. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL */
  42591. /* This test is to check wolfSSL_read behaves as same as
  42592. * openSSL when it is called after SSL_shutdown completes.
  42593. */
  42594. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  42595. {
  42596. int ret = 0;
  42597. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42598. tcp_ready ready;
  42599. func_args client_args;
  42600. func_args server_args;
  42601. THREAD_TYPE serverThread;
  42602. THREAD_TYPE clientThread;
  42603. callback_functions server_cbf;
  42604. callback_functions client_cbf;
  42605. printf(testingFmt, "wolfSSL_read_detect_TCP_disconnect()");
  42606. #ifdef WOLFSSL_TIRTOS
  42607. fdOpenSession(Task_self());
  42608. #endif
  42609. StartTCP();
  42610. InitTcpReady(&ready);
  42611. #if defined(USE_WINDOWS_API)
  42612. /* use RNG to get random port if using windows */
  42613. ready.port = GetRandomPort();
  42614. #endif
  42615. XMEMSET(&client_args, 0, sizeof(func_args));
  42616. XMEMSET(&server_args, 0, sizeof(func_args));
  42617. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  42618. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  42619. server_cbf.method = wolfTLSv1_2_server_method;
  42620. client_cbf.method = wolfTLSv1_2_client_method;
  42621. server_args.callbacks = &server_cbf;
  42622. client_args.callbacks = &client_cbf;
  42623. server_args.signal = &ready;
  42624. client_args.signal = &ready;
  42625. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  42626. wait_tcp_ready(&server_args);
  42627. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  42628. join_thread(clientThread);
  42629. join_thread(serverThread);
  42630. AssertTrue(client_args.return_code);
  42631. AssertTrue(server_args.return_code);
  42632. FreeTcpReady(&ready);
  42633. printf(resultFmt, passed);
  42634. #endif
  42635. return ret;
  42636. }
  42637. static void test_wolfSSL_CTX_get_min_proto_version(void)
  42638. {
  42639. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  42640. WOLFSSL_CTX *ctx;
  42641. (void)ctx;
  42642. printf(testingFmt, "wolfSSL_CTX_get_min_proto_version()");
  42643. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  42644. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  42645. #ifdef WOLFSSL_ALLOW_SSLV3
  42646. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  42647. #else
  42648. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  42649. #endif
  42650. wolfSSL_CTX_free(ctx);
  42651. #ifdef WOLFSSL_ALLOW_TLSV10
  42652. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  42653. #else
  42654. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  42655. #endif
  42656. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  42657. #ifdef WOLFSSL_ALLOW_TLSV10
  42658. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  42659. #else
  42660. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  42661. #endif
  42662. wolfSSL_CTX_free(ctx);
  42663. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  42664. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  42665. #ifndef NO_OLD_TLS
  42666. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  42667. #else
  42668. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  42669. #endif
  42670. wolfSSL_CTX_free(ctx);
  42671. #ifndef WOLFSSL_NO_TLS12
  42672. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  42673. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  42674. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  42675. wolfSSL_CTX_free(ctx);
  42676. #endif
  42677. #ifdef WOLFSSL_TLS13
  42678. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  42679. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  42680. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  42681. wolfSSL_CTX_free(ctx);
  42682. #endif
  42683. printf(resultFmt, passed);
  42684. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  42685. }
  42686. static void test_wolfSSL_security_level(void)
  42687. {
  42688. #if defined(OPENSSL_EXTRA)
  42689. printf(testingFmt, "test_wolfSSL_security_level()");
  42690. SSL_CTX *ctx;
  42691. #ifdef WOLFSSL_TLS13
  42692. #ifdef NO_WOLFSSL_SERVER
  42693. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  42694. #else
  42695. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  42696. #endif
  42697. SSL_CTX_set_security_level(ctx, 1);
  42698. AssertTrue(1);
  42699. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  42700. SSL_CTX_free(ctx);
  42701. #else
  42702. (void)ctx;
  42703. #endif
  42704. printf(resultFmt, passed);
  42705. #endif
  42706. }
  42707. static void test_wolfSSL_SSL_in_init(void)
  42708. {
  42709. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  42710. printf(testingFmt, "test_wolfSSL_SSL_in_init()");
  42711. SSL_CTX* ctx;
  42712. SSL* ssl;
  42713. const char* testCertFile;
  42714. const char* testKeyFile;
  42715. #ifdef WOLFSSL_TLS13
  42716. #ifdef NO_WOLFSSL_SERVER
  42717. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  42718. #else
  42719. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  42720. #endif
  42721. #ifndef NO_RSA
  42722. testCertFile = svrCertFile;
  42723. testKeyFile = svrKeyFile;
  42724. #elif defined(HAVE_ECC)
  42725. testCertFile = eccCertFile;
  42726. testKeyFile = eccKeyFile;
  42727. #else
  42728. testCertFile = NULL;
  42729. testKeyFile = NULL;
  42730. #endif
  42731. if (testCertFile != NULL && testKeyFile != NULL) {
  42732. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  42733. SSL_FILETYPE_PEM));
  42734. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  42735. SSL_FILETYPE_PEM));
  42736. }
  42737. ssl = SSL_new(ctx);
  42738. AssertNotNull(ssl);
  42739. AssertIntEQ(SSL_in_init(ssl), 1);
  42740. SSL_CTX_free(ctx);
  42741. SSL_free(ssl);
  42742. #else
  42743. (void)ctx;
  42744. (void)ssl;
  42745. (void)testCertFile;
  42746. (void)testKeyFile;
  42747. #endif
  42748. printf(resultFmt, passed);
  42749. #endif
  42750. }
  42751. static void test_wolfSSL_EC_curve(void)
  42752. {
  42753. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42754. printf(testingFmt, "test_wolfSSL_EC_curve()");
  42755. int nid = NID_secp160k1;
  42756. const char* nid_name;
  42757. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  42758. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  42759. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  42760. printf(resultFmt, passed);
  42761. #endif
  42762. }
  42763. static void test_wolfSSL_CTX_set_timeout(void)
  42764. {
  42765. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  42766. int timeout;
  42767. (void)timeout;
  42768. printf(testingFmt, "test_wolfSSL_CTX_set_timeout()");
  42769. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  42770. AssertNotNull(ctx);
  42771. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42772. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  42773. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  42774. */
  42775. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  42776. /* giving 0 as timeout value sets default timeout */
  42777. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  42778. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  42779. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  42780. #else
  42781. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  42782. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  42783. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  42784. #endif
  42785. wolfSSL_CTX_free(ctx);
  42786. printf(resultFmt, passed);
  42787. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  42788. }
  42789. static void test_wolfSSL_OpenSSL_version(void)
  42790. {
  42791. #if defined(OPENSSL_EXTRA)
  42792. printf(testingFmt, "test_wolfSSL_OpenSSL_version()");
  42793. const char* ver;
  42794. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  42795. AssertNotNull(ver = OpenSSL_version(0));
  42796. #else
  42797. AssertNotNull(ver = OpenSSL_version());
  42798. #endif
  42799. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  42800. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  42801. printf(resultFmt, passed);
  42802. #endif
  42803. }
  42804. static void test_CONF_CTX_CMDLINE(void)
  42805. {
  42806. #if defined(OPENSSL_ALL)
  42807. printf(testingFmt, "test_CONF_CTX_CMDLINE");
  42808. SSL_CTX* ctx = NULL;
  42809. SSL_CONF_CTX* cctx = NULL;
  42810. AssertNotNull(cctx = SSL_CONF_CTX_new());
  42811. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  42812. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  42813. AssertTrue(1);
  42814. /* set flags */
  42815. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  42816. WOLFSSL_CONF_FLAG_CMDLINE);
  42817. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  42818. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  42819. /* cmd invalid command */
  42820. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  42821. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  42822. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  42823. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  42824. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  42825. /* cmd Certificate and Private Key*/
  42826. {
  42827. #if !defined(NO_CERTS) && !defined(NO_RSA)
  42828. const char* ourCert = svrCertFile;
  42829. const char* ourKey = svrKeyFile;
  42830. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  42831. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  42832. WOLFSSL_SUCCESS);
  42833. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  42834. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  42835. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  42836. #endif
  42837. }
  42838. /* cmd curves */
  42839. {
  42840. #if defined(HAVE_ECC)
  42841. const char* curve = "secp256r1";
  42842. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  42843. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  42844. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  42845. #endif
  42846. }
  42847. /* cmd CipherString */
  42848. {
  42849. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  42850. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  42851. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  42852. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  42853. }
  42854. /* cmd DH parameter */
  42855. {
  42856. #if !defined(NO_DH) && !defined(NO_BIO)
  42857. const char* ourdhcert = "./certs/dh2048.pem";
  42858. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  42859. -3);
  42860. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  42861. WOLFSSL_SUCCESS);
  42862. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  42863. #endif
  42864. }
  42865. SSL_CTX_free(ctx);
  42866. SSL_CONF_CTX_free(cctx);
  42867. printf(resultFmt, passed);
  42868. #endif /* OPENSSL_EXTRA */
  42869. }
  42870. static void test_CONF_CTX_FILE(void)
  42871. {
  42872. #if defined(OPENSSL_ALL)
  42873. printf(testingFmt, "test_CONF_CTX_FILE");
  42874. SSL_CTX* ctx = NULL;
  42875. SSL_CONF_CTX* cctx = NULL;
  42876. AssertNotNull(cctx = SSL_CONF_CTX_new());
  42877. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  42878. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  42879. AssertTrue(1);
  42880. /* set flags */
  42881. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  42882. WOLFSSL_CONF_FLAG_FILE);
  42883. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  42884. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  42885. /* sanity check */
  42886. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  42887. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  42888. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  42889. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  42890. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  42891. /* cmd Certificate and Private Key*/
  42892. {
  42893. #if !defined(NO_CERTS) && !defined(NO_RSA)
  42894. const char* ourCert = svrCertFile;
  42895. const char* ourKey = svrKeyFile;
  42896. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  42897. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  42898. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  42899. WOLFSSL_SUCCESS);
  42900. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  42901. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  42902. #endif
  42903. }
  42904. /* cmd curves */
  42905. {
  42906. #if defined(HAVE_ECC)
  42907. const char* curve = "secp256r1";
  42908. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  42909. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  42910. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  42911. #endif
  42912. }
  42913. /* cmd CipherString */
  42914. {
  42915. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  42916. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  42917. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  42918. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  42919. }
  42920. /* cmd DH parameter */
  42921. {
  42922. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  42923. const char* ourdhcert = "./certs/dh3072.pem";
  42924. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  42925. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  42926. WOLFSSL_SUCCESS);
  42927. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  42928. #endif
  42929. }
  42930. SSL_CTX_free(ctx);
  42931. SSL_CONF_CTX_free(cctx);
  42932. printf(resultFmt, passed);
  42933. #endif /* OPENSSL_EXTRA */
  42934. }
  42935. static void test_wolfSSL_CRYPTO_get_ex_new_index(void)
  42936. {
  42937. #ifdef HAVE_EX_DATA
  42938. int idx1,idx2;
  42939. printf(testingFmt, "test_wolfSSL_CRYPTO_get_ex_new_index()");
  42940. /* test for unsupported flass index */
  42941. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION,
  42942. 0,NULL, NULL, NULL, NULL ), -1);
  42943. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE,
  42944. 0,NULL, NULL, NULL, NULL ), -1);
  42945. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX,
  42946. 0,NULL, NULL, NULL, NULL ), -1);
  42947. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH,
  42948. 0,NULL, NULL, NULL, NULL ), -1);
  42949. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA,
  42950. 0,NULL, NULL, NULL, NULL ), -1);
  42951. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY,
  42952. 0,NULL, NULL, NULL, NULL ), -1);
  42953. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA,
  42954. 0,NULL, NULL, NULL, NULL ), -1);
  42955. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE,
  42956. 0,NULL, NULL, NULL, NULL ), -1);
  42957. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI,
  42958. 0,NULL, NULL, NULL, NULL ), -1);
  42959. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO,
  42960. 0,NULL, NULL, NULL, NULL ), -1);
  42961. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_APP,
  42962. 0,NULL, NULL, NULL, NULL ), -1);
  42963. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_UI_METHOD,
  42964. 0,NULL, NULL, NULL, NULL ), -1);
  42965. AssertIntEQ(CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DRBG,
  42966. 0,NULL, NULL, NULL, NULL ), -1);
  42967. AssertIntEQ(CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  42968. /* test for supported class index */
  42969. idx1 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL,
  42970. 0,NULL, NULL, NULL, NULL );
  42971. idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL,
  42972. 0,NULL, NULL, NULL, NULL );
  42973. AssertIntNE(idx1, -1);
  42974. AssertIntNE(idx2, -1);
  42975. AssertIntNE(idx1, idx2);
  42976. idx1 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX,
  42977. 0,NULL, NULL, NULL, NULL );
  42978. idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX,
  42979. 0,NULL, NULL, NULL, NULL );
  42980. AssertIntNE(idx1, -1);
  42981. AssertIntNE(idx2, -1);
  42982. AssertIntNE(idx1, idx2);
  42983. idx1 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509,
  42984. 0,NULL, NULL, NULL, NULL );
  42985. idx2 = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509,
  42986. 0,NULL, NULL, NULL, NULL );
  42987. AssertIntNE(idx1, -1);
  42988. AssertIntNE(idx2, -1);
  42989. AssertIntNE(idx1, idx2);
  42990. printf(resultFmt, "passed");
  42991. #endif /* HAVE_EX_DATA */
  42992. }
  42993. static void test_wolfSSL_set_psk_use_session_callback(void)
  42994. {
  42995. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  42996. printf(testingFmt, "test_wolfSSL_set_psk_use_session_callback()");
  42997. SSL_CTX* ctx;
  42998. SSL* ssl;
  42999. const char* testCertFile;
  43000. const char* testKeyFile;
  43001. #ifdef WOLFSSL_TLS13
  43002. #ifdef NO_WOLFSSL_SERVER
  43003. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  43004. #else
  43005. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  43006. #endif
  43007. #ifndef NO_RSA
  43008. testCertFile = svrCertFile;
  43009. testKeyFile = svrKeyFile;
  43010. #elif defined(HAVE_ECC)
  43011. testCertFile = eccCertFile;
  43012. testKeyFile = eccKeyFile;
  43013. #else
  43014. testCertFile = NULL;
  43015. testKeyFile = NULL;
  43016. #endif
  43017. if (testCertFile != NULL && testKeyFile != NULL) {
  43018. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  43019. SSL_FILETYPE_PEM));
  43020. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  43021. SSL_FILETYPE_PEM));
  43022. }
  43023. ssl = SSL_new(ctx);
  43024. AssertNotNull(ssl);
  43025. SSL_set_psk_use_session_callback(ssl,
  43026. my_psk_use_session_cb);
  43027. AssertTrue(1);
  43028. SSL_CTX_free(ctx);
  43029. SSL_free(ssl);
  43030. #else
  43031. (void)ctx;
  43032. (void)ssl;
  43033. (void)testCertFile;
  43034. (void)testKeyFile;
  43035. #endif
  43036. printf(resultFmt, passed);
  43037. #endif
  43038. }
  43039. static void test_wolfSSL_DH(void)
  43040. {
  43041. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  43042. DH *dh = NULL;
  43043. BIGNUM* p;
  43044. BIGNUM* q;
  43045. BIGNUM* g;
  43046. BIGNUM* pub;
  43047. BIGNUM* priv;
  43048. (void)dh;
  43049. (void)p;
  43050. (void)q;
  43051. (void)g;
  43052. (void)pub;
  43053. (void)priv;
  43054. #if defined(OPENSSL_ALL) && defined(WOLFSSL_KEY_GEN)
  43055. #if !defined(HAVE_FIPS) || \
  43056. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43057. FILE* f = NULL;
  43058. unsigned char buf[268];
  43059. const unsigned char* pt = buf;
  43060. long len = 0;
  43061. dh = NULL;
  43062. XMEMSET(buf, 0, sizeof(buf));
  43063. /* Test 2048 bit parameters */
  43064. f = XFOPEN("./certs/dh2048.der", "rb");
  43065. AssertTrue(f != XBADFILE);
  43066. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  43067. XFCLOSE(f);
  43068. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  43069. AssertNotNull(dh->p);
  43070. AssertNotNull(dh->g);
  43071. AssertTrue(pt != buf);
  43072. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  43073. DH_get0_pqg(dh, (const BIGNUM**)&p,
  43074. (const BIGNUM**)&q,
  43075. (const BIGNUM**) &g);
  43076. AssertPtrEq(p, dh->p);
  43077. AssertPtrEq(q, dh->q);
  43078. AssertPtrEq(g, dh->g);
  43079. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  43080. AssertPtrEq(pub, dh->pub_key);
  43081. AssertPtrEq(priv, dh->priv_key);
  43082. AssertNotNull(pub = BN_new());
  43083. AssertNotNull(priv = BN_new());
  43084. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  43085. AssertPtrEq(pub, dh->pub_key);
  43086. AssertPtrEq(priv, dh->priv_key);
  43087. DH_free(dh);
  43088. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  43089. DH_free(dh);
  43090. #endif
  43091. #endif
  43092. printf(testingFmt, "test_wolfSSL_DH");
  43093. dh = wolfSSL_DH_new();
  43094. AssertNotNull(dh);
  43095. /* invalid parameters test */
  43096. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  43097. (const BIGNUM**)&q,
  43098. (const BIGNUM**)&g);
  43099. DH_get0_pqg(dh, NULL,
  43100. (const BIGNUM**)&q,
  43101. (const BIGNUM**)&g);
  43102. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  43103. DH_get0_pqg(dh, NULL, NULL, NULL);
  43104. AssertTrue(1);
  43105. DH_get0_pqg(dh, (const BIGNUM**)&p,
  43106. (const BIGNUM**)&q,
  43107. (const BIGNUM**)&g);
  43108. AssertPtrEq(p, NULL);
  43109. AssertPtrEq(q, NULL);
  43110. AssertPtrEq(g, NULL);
  43111. DH_free(dh);
  43112. printf(resultFmt, passed);
  43113. #endif /* OPENSSL_EXTRA && !NO_DH */
  43114. }
  43115. static void test_wolfSSL_ERR_strings(void)
  43116. {
  43117. const char* err1 = "unsupported cipher suite";
  43118. const char* err2 = "wolfSSL PEM routines";
  43119. const char* err = NULL;
  43120. (void)err;
  43121. (void)err1;
  43122. (void)err2;
  43123. #if !defined(NO_ERROR_STRINGS)
  43124. printf(testingFmt, "test_wolfSSL_ERR_strings");
  43125. #if defined(OPENSSL_EXTRA)
  43126. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  43127. AssertTrue(err != NULL);
  43128. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  43129. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  43130. AssertTrue(err != NULL);
  43131. AssertIntEQ((*err == '\0'), 1);
  43132. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  43133. AssertTrue(err != NULL);
  43134. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  43135. #else
  43136. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  43137. AssertTrue(err != NULL);
  43138. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  43139. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  43140. AssertTrue(err != NULL);
  43141. AssertIntEQ((*err == '\0'), 1);
  43142. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  43143. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  43144. AssertTrue(err != NULL);
  43145. AssertIntEQ((*err == '\0'), 1);
  43146. #endif
  43147. printf(resultFmt, passed);
  43148. #endif
  43149. }
  43150. static void test_wolfSSL_EVP_shake128(void)
  43151. {
  43152. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  43153. defined(WOLFSSL_SHAKE128)
  43154. printf(testingFmt, "test_wolfSSL_EVP_shake128");
  43155. const EVP_MD* md = NULL;
  43156. md = EVP_shake128();
  43157. AssertTrue(md != NULL);
  43158. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  43159. printf(resultFmt, passed);
  43160. #endif
  43161. }
  43162. static void test_wolfSSL_EVP_shake256(void)
  43163. {
  43164. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  43165. defined(WOLFSSL_SHAKE256)
  43166. printf(testingFmt, "test_wolfSSL_EVP_shake256");
  43167. const EVP_MD* md = NULL;
  43168. md = EVP_shake256();
  43169. AssertTrue(md != NULL);
  43170. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  43171. printf(resultFmt, passed);
  43172. #endif
  43173. }
  43174. static void test_EVP_blake2(void)
  43175. {
  43176. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  43177. printf(testingFmt, "test_EVP_blake2");
  43178. const EVP_MD* md = NULL;
  43179. (void)md;
  43180. #if defined(HAVE_BLAKE2)
  43181. md = EVP_blake2b512();
  43182. AssertTrue(md != NULL);
  43183. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  43184. #endif
  43185. #if defined(HAVE_BLAKE2S)
  43186. md = EVP_blake2s256();
  43187. AssertTrue(md != NULL);
  43188. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  43189. #endif
  43190. printf(resultFmt, passed);
  43191. #endif
  43192. }
  43193. #if defined(OPENSSL_EXTRA)
  43194. static void list_md_fn(const EVP_MD* m, const char* from,
  43195. const char* to, void* arg)
  43196. {
  43197. const char* mn;
  43198. BIO *bio;
  43199. (void) from;
  43200. (void) to;
  43201. (void) arg;
  43202. (void) mn;
  43203. (void) bio;
  43204. if (!m) {
  43205. /* alias */
  43206. AssertNull(m);
  43207. AssertNotNull(to);
  43208. }
  43209. else {
  43210. AssertNotNull(m);
  43211. AssertNull(to);
  43212. }
  43213. AssertNotNull(from);
  43214. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  43215. mn = EVP_get_digestbyname(from);
  43216. /* print to stdout */
  43217. AssertNotNull(arg);
  43218. bio = BIO_new(BIO_s_file());
  43219. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  43220. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  43221. BIO_free(bio);
  43222. #endif
  43223. }
  43224. #endif
  43225. static void test_EVP_MD_do_all(void)
  43226. {
  43227. #if defined(OPENSSL_EXTRA)
  43228. printf(testingFmt, "test_EVP_MD_do_all");
  43229. EVP_MD_do_all(NULL, stdout);
  43230. /* to confirm previous call gives no harm */
  43231. AssertTrue(1);
  43232. EVP_MD_do_all(list_md_fn, stdout);
  43233. /* to confirm previous call gives no harm */
  43234. AssertTrue(1);
  43235. printf(resultFmt, passed);
  43236. #endif
  43237. }
  43238. #if defined(OPENSSL_EXTRA)
  43239. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  43240. {
  43241. (void)arg;
  43242. (void)nm;
  43243. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  43244. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  43245. /* print to stdout */
  43246. AssertNotNull(arg);
  43247. bio = BIO_new(BIO_s_file());
  43248. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  43249. BIO_printf(bio, "%s\n", mn);
  43250. BIO_free(bio);
  43251. #endif
  43252. }
  43253. #endif
  43254. static void test_OBJ_NAME_do_all(void)
  43255. {
  43256. #if defined(OPENSSL_EXTRA)
  43257. printf(testingFmt, "test_OBJ_NAME_do_all");
  43258. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  43259. /* to confirm previous call gives no harm */
  43260. AssertTrue(1);
  43261. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stdout);
  43262. /* to confirm previous call gives no harm */
  43263. AssertTrue(1);
  43264. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stdout);
  43265. AssertTrue(1);
  43266. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stdout);
  43267. AssertTrue(1);
  43268. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stdout);
  43269. AssertTrue(1);
  43270. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stdout);
  43271. AssertTrue(1);
  43272. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stdout);
  43273. AssertTrue(1);
  43274. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stdout);
  43275. AssertTrue(1);
  43276. OBJ_NAME_do_all(-1, obj_name_t, stdout);
  43277. AssertTrue(1);
  43278. printf(resultFmt, passed);
  43279. #endif
  43280. }
  43281. static void test_SSL_CIPHER_get_xxx(void)
  43282. {
  43283. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  43284. !defined(NO_FILESYSTEM)
  43285. printf(testingFmt, "test_SSL_CIPHER_get_xxx");
  43286. const SSL_CIPHER* cipher = NULL;
  43287. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43288. int i, numCiphers = 0;
  43289. SSL_CTX* ctx = NULL;
  43290. SSL* ssl = NULL;
  43291. const char* testCertFile;
  43292. const char* testKeyFile;
  43293. char buf[256] = {0};
  43294. const char* cipher_id = NULL;
  43295. int expect_nid1 = NID_undef;
  43296. int expect_nid2 = NID_undef;
  43297. int expect_nid3 = NID_undef;
  43298. int expect_nid4 = NID_undef;
  43299. int expect_nid5 = 0;
  43300. const char* cipher_id2 = NULL;
  43301. int expect_nid21 = NID_undef;
  43302. int expect_nid22 = NID_undef;
  43303. int expect_nid23 = NID_undef;
  43304. int expect_nid24 = NID_undef;
  43305. int expect_nid25 = 0;
  43306. (void)cipher;
  43307. (void)supportedCiphers;
  43308. (void)i;
  43309. (void)numCiphers;
  43310. (void)ctx;
  43311. (void)ssl;
  43312. (void)testCertFile;
  43313. (void)testKeyFile;
  43314. #if defined(WOLFSSL_TLS13)
  43315. cipher_id = "TLS13-AES128-GCM-SHA256";
  43316. expect_nid1 = NID_auth_rsa;
  43317. expect_nid2 = NID_aes_128_gcm;
  43318. expect_nid3 = NID_sha256;
  43319. expect_nid4 = NID_kx_any;
  43320. expect_nid5 = 1;
  43321. #if !defined(WOLFSSL_NO_TLS12)
  43322. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  43323. expect_nid21 = NID_auth_rsa;
  43324. expect_nid22 = NID_aes_256_gcm;
  43325. expect_nid23 = NID_sha384;
  43326. expect_nid24 = NID_kx_ecdhe;
  43327. expect_nid25 = 1;
  43328. #endif
  43329. #endif
  43330. #ifdef NO_WOLFSSL_SERVER
  43331. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43332. #else
  43333. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  43334. #endif
  43335. if (cipher_id) {
  43336. #ifndef NO_RSA
  43337. testCertFile = svrCertFile;
  43338. testKeyFile = svrKeyFile;
  43339. #elif defined(HAVE_ECC)
  43340. testCertFile = eccCertFile;
  43341. testKeyFile = eccKeyFile;
  43342. #else
  43343. testCertFile = NULL;
  43344. testKeyFile = NULL;
  43345. #endif
  43346. if (testCertFile != NULL && testKeyFile != NULL) {
  43347. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  43348. SSL_FILETYPE_PEM));
  43349. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  43350. SSL_FILETYPE_PEM));
  43351. }
  43352. ssl = SSL_new(ctx);
  43353. AssertNotNull(ssl);
  43354. AssertIntEQ(SSL_in_init(ssl), 1);
  43355. supportedCiphers = SSL_get_ciphers(ssl);
  43356. numCiphers = sk_num(supportedCiphers);
  43357. for (i = 0; i < numCiphers; ++i) {
  43358. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  43359. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  43360. }
  43361. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  43362. break;
  43363. }
  43364. }
  43365. /* test case for */
  43366. if (i != numCiphers) {
  43367. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  43368. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  43369. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  43370. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  43371. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  43372. }
  43373. if (cipher_id2) {
  43374. for (i = 0; i < numCiphers; ++i) {
  43375. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  43376. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  43377. }
  43378. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  43379. break;
  43380. }
  43381. }
  43382. /* test case for */
  43383. if (i != numCiphers) {
  43384. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  43385. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  43386. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  43387. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  43388. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  43389. }
  43390. }
  43391. }
  43392. if (ctx)
  43393. SSL_CTX_free(ctx);
  43394. if(ssl)
  43395. SSL_free(ssl);
  43396. printf(resultFmt, passed);
  43397. #endif
  43398. }
  43399. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  43400. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  43401. int* keyFormat)
  43402. {
  43403. int ret;
  43404. byte* key_buf = NULL;
  43405. size_t key_sz = 0;
  43406. EncryptedInfo encInfo;
  43407. XMEMSET(&encInfo, 0, sizeof(encInfo));
  43408. ret = load_file(privKeyFile, &key_buf, &key_sz);
  43409. if (ret == 0) {
  43410. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  43411. NULL, &encInfo, keyFormat);
  43412. }
  43413. if (key_buf != NULL) {
  43414. free(key_buf); key_buf = NULL;
  43415. }
  43416. (void)encInfo; /* not used in this test */
  43417. #ifdef DEBUG_WOLFSSL
  43418. printf("%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  43419. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  43420. (*pDer)->length);
  43421. #endif
  43422. return ret;
  43423. }
  43424. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  43425. {
  43426. int ret = CRYPTOCB_UNAVAILABLE;
  43427. const char* privKeyFile = (const char*)ctx;
  43428. DerBuffer* pDer = NULL;
  43429. int keyFormat = 0;
  43430. if (info->algo_type == WC_ALGO_TYPE_PK) {
  43431. #ifdef DEBUG_WOLFSSL
  43432. printf("test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  43433. #endif
  43434. #ifndef NO_RSA
  43435. if (info->pk.type == WC_PK_TYPE_RSA) {
  43436. switch (info->pk.rsa.type) {
  43437. case RSA_PUBLIC_ENCRYPT:
  43438. case RSA_PUBLIC_DECRYPT:
  43439. /* perform software based RSA public op */
  43440. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  43441. break;
  43442. case RSA_PRIVATE_ENCRYPT:
  43443. case RSA_PRIVATE_DECRYPT:
  43444. {
  43445. RsaKey key;
  43446. /* perform software based RSA private op */
  43447. #ifdef DEBUG_WOLFSSL
  43448. printf("test_CryptoCb_Func: RSA Priv\n");
  43449. #endif
  43450. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  43451. &keyFormat);
  43452. if (ret != 0) {
  43453. return ret;
  43454. }
  43455. ret = wc_InitRsaKey(&key, NULL);
  43456. if (ret == 0) {
  43457. word32 keyIdx = 0;
  43458. /* load RSA private key and perform private transform */
  43459. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  43460. &key, pDer->length);
  43461. if (ret == 0) {
  43462. ret = wc_RsaFunction(
  43463. info->pk.rsa.in, info->pk.rsa.inLen,
  43464. info->pk.rsa.out, info->pk.rsa.outLen,
  43465. info->pk.rsa.type, &key, info->pk.rsa.rng);
  43466. }
  43467. else {
  43468. /* if decode fails, then fall-back to software based crypto */
  43469. printf("test_CryptoCb_Func: RSA private key decode "
  43470. "failed %d, falling back to software\n", ret);
  43471. ret = CRYPTOCB_UNAVAILABLE;
  43472. }
  43473. wc_FreeRsaKey(&key);
  43474. }
  43475. wc_FreeDer(&pDer); pDer = NULL;
  43476. break;
  43477. }
  43478. }
  43479. #ifdef DEBUG_WOLFSSL
  43480. printf("test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  43481. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  43482. #endif
  43483. }
  43484. #endif /* !NO_RSA */
  43485. #ifdef HAVE_ECC
  43486. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  43487. /* mark this key as ephemeral */
  43488. if (info->pk.eckg.key != NULL) {
  43489. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  43490. sizeof(info->pk.eckg.key->label));
  43491. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  43492. }
  43493. }
  43494. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  43495. ecc_key key;
  43496. /* perform software based ECC sign */
  43497. #ifdef DEBUG_WOLFSSL
  43498. printf("test_CryptoCb_Func: ECC Sign\n");
  43499. #endif
  43500. if (info->pk.eccsign.key != NULL &&
  43501. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  43502. /* this is an empheral key */
  43503. #ifdef DEBUG_WOLFSSL
  43504. printf("test_CryptoCb_Func: skipping signing op on ephemeral key\n");
  43505. #endif
  43506. return CRYPTOCB_UNAVAILABLE;
  43507. }
  43508. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  43509. if (ret != 0) {
  43510. return ret;
  43511. }
  43512. ret = wc_ecc_init(&key);
  43513. if (ret == 0) {
  43514. word32 keyIdx = 0;
  43515. /* load ECC private key and perform private transform */
  43516. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  43517. &key, pDer->length);
  43518. if (ret == 0) {
  43519. ret = wc_ecc_sign_hash(
  43520. info->pk.eccsign.in, info->pk.eccsign.inlen,
  43521. info->pk.eccsign.out, info->pk.eccsign.outlen,
  43522. info->pk.eccsign.rng, &key);
  43523. }
  43524. else {
  43525. /* if decode fails, then fall-back to software based crypto */
  43526. printf("test_CryptoCb_Func: ECC private key decode "
  43527. "failed %d, falling back to software\n", ret);
  43528. ret = CRYPTOCB_UNAVAILABLE;
  43529. }
  43530. wc_ecc_free(&key);
  43531. }
  43532. wc_FreeDer(&pDer); pDer = NULL;
  43533. #ifdef DEBUG_WOLFSSL
  43534. printf("test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  43535. ret, *info->pk.eccsign.outlen);
  43536. #endif
  43537. }
  43538. #endif /* HAVE_ECC */
  43539. #ifdef HAVE_ED25519
  43540. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  43541. ed25519_key key;
  43542. /* perform software based ED25519 sign */
  43543. #ifdef DEBUG_WOLFSSL
  43544. printf("test_CryptoCb_Func: ED25519 Sign\n");
  43545. #endif
  43546. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  43547. if (ret != 0) {
  43548. return ret;
  43549. }
  43550. ret = wc_ed25519_init(&key);
  43551. if (ret == 0) {
  43552. word32 keyIdx = 0;
  43553. /* load ED25519 private key and perform private transform */
  43554. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  43555. &key, pDer->length);
  43556. if (ret == 0) {
  43557. /* calculate public key */
  43558. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  43559. if (ret == 0) {
  43560. key.pubKeySet = 1;
  43561. ret = wc_ed25519_sign_msg_ex(
  43562. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  43563. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  43564. &key, info->pk.ed25519sign.type,
  43565. info->pk.ed25519sign.context,
  43566. info->pk.ed25519sign.contextLen);
  43567. }
  43568. }
  43569. else {
  43570. /* if decode fails, then fall-back to software based crypto */
  43571. printf("test_CryptoCb_Func: ED25519 private key decode "
  43572. "failed %d, falling back to software\n", ret);
  43573. ret = CRYPTOCB_UNAVAILABLE;
  43574. }
  43575. wc_ed25519_free(&key);
  43576. }
  43577. wc_FreeDer(&pDer); pDer = NULL;
  43578. #ifdef DEBUG_WOLFSSL
  43579. printf("test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  43580. ret, *info->pk.ed25519sign.outLen);
  43581. #endif
  43582. }
  43583. #endif /* HAVE_ED25519 */
  43584. }
  43585. (void)thisDevId;
  43586. (void)keyFormat;
  43587. return ret;
  43588. }
  43589. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  43590. static void test_wc_CryptoCb_TLS(int tlsVer,
  43591. const char* cliCaPemFile, const char* cliCertPemFile,
  43592. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  43593. const char* svrCaPemFile, const char* svrCertPemFile,
  43594. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  43595. {
  43596. callback_functions client_cbf;
  43597. callback_functions server_cbf;
  43598. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  43599. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  43600. if (tlsVer == WOLFSSL_TLSV1_3) {
  43601. #ifdef WOLFSSL_TLS13
  43602. server_cbf.method = wolfTLSv1_3_server_method;
  43603. client_cbf.method = wolfTLSv1_3_client_method;
  43604. #endif
  43605. }
  43606. else if (tlsVer == WOLFSSL_TLSV1_2) {
  43607. #ifndef WOLFSSL_NO_TLS12
  43608. server_cbf.method = wolfTLSv1_2_server_method;
  43609. client_cbf.method = wolfTLSv1_2_client_method;
  43610. #endif
  43611. }
  43612. else if (tlsVer == WOLFSSL_TLSV1_1) {
  43613. #ifndef NO_OLD_TLS
  43614. server_cbf.method = wolfTLSv1_1_server_method;
  43615. client_cbf.method = wolfTLSv1_1_client_method;
  43616. #endif
  43617. }
  43618. else if (tlsVer == WOLFSSL_TLSV1) {
  43619. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  43620. server_cbf.method = wolfTLSv1_server_method;
  43621. client_cbf.method = wolfTLSv1_client_method;
  43622. #endif
  43623. }
  43624. else if (tlsVer == WOLFSSL_SSLV3) {
  43625. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  43626. defined(WOLFSSL_STATIC_RSA)
  43627. server_cbf.method = wolfSSLv3_server_method;
  43628. client_cbf.method = wolfSSLv3_client_method;
  43629. #endif
  43630. }
  43631. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  43632. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  43633. server_cbf.method = wolfDTLSv1_2_server_method;
  43634. client_cbf.method = wolfDTLSv1_2_client_method;
  43635. #endif
  43636. }
  43637. else if (tlsVer == WOLFSSL_DTLSV1) {
  43638. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  43639. server_cbf.method = wolfDTLSv1_server_method;
  43640. client_cbf.method = wolfDTLSv1_client_method;
  43641. #endif
  43642. }
  43643. if (server_cbf.method == NULL) {
  43644. /* not enabled */
  43645. return;
  43646. }
  43647. /* Setup the keys for the TLS test */
  43648. client_cbf.certPemFile = cliCertPemFile;
  43649. client_cbf.keyPemFile = cliPubKeyPemFile;
  43650. client_cbf.caPemFile = cliCaPemFile;
  43651. server_cbf.certPemFile = svrCertPemFile;
  43652. server_cbf.keyPemFile = svrPubKeyPemFile;
  43653. server_cbf.caPemFile = svrCaPemFile;
  43654. /* Setup a crypto callback with pointer to private key file for testing */
  43655. client_cbf.devId = 1;
  43656. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  43657. (void*)cliPrivKeyPemFile);
  43658. server_cbf.devId = 2;
  43659. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  43660. (void*)svrPrivKeyPemFile);
  43661. /* Perform TLS server and client test */
  43662. /* First test is at WOLFSSL_CTX level */
  43663. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  43664. /* Check for success */
  43665. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43666. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43667. /* Second test is a WOLFSSL object level */
  43668. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  43669. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  43670. /* Check for success */
  43671. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43672. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43673. /* Un register the devId's */
  43674. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  43675. client_cbf.devId = INVALID_DEVID;
  43676. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  43677. server_cbf.devId = INVALID_DEVID;
  43678. }
  43679. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  43680. static void test_wc_CryptoCb(void)
  43681. {
  43682. #ifdef WOLF_CRYPTO_CB
  43683. /* TODO: Add crypto callback API tests */
  43684. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  43685. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  43686. int tlsVer;
  43687. #endif
  43688. #ifndef NO_RSA
  43689. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  43690. test_wc_CryptoCb_TLS(tlsVer,
  43691. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  43692. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  43693. }
  43694. #endif
  43695. #ifdef HAVE_ECC
  43696. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  43697. test_wc_CryptoCb_TLS(tlsVer,
  43698. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  43699. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  43700. }
  43701. #endif
  43702. #ifdef HAVE_ED25519
  43703. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  43704. if (tlsVer == WOLFSSL_DTLSV1) continue;
  43705. test_wc_CryptoCb_TLS(tlsVer,
  43706. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  43707. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  43708. }
  43709. #endif
  43710. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  43711. #endif /* WOLF_CRYPTO_CB */
  43712. }
  43713. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  43714. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  43715. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  43716. const char* cliCaPemFile, const char* cliCertPemFile,
  43717. const char* cliPrivKeyPemFile,
  43718. const char* svrCaPemFile, const char* svrCertPemFile,
  43719. const char* svrPrivKeyPemFile,
  43720. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  43721. {
  43722. callback_functions client_cbf;
  43723. callback_functions server_cbf;
  43724. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  43725. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  43726. if (tlsVer == WOLFSSL_TLSV1_3) {
  43727. #ifdef WOLFSSL_TLS13
  43728. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  43729. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  43730. #endif
  43731. }
  43732. else if (tlsVer == WOLFSSL_TLSV1_2) {
  43733. #ifndef WOLFSSL_NO_TLS12
  43734. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  43735. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  43736. #endif
  43737. }
  43738. else if (tlsVer == WOLFSSL_TLSV1_1) {
  43739. #ifndef NO_OLD_TLS
  43740. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  43741. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  43742. #endif
  43743. }
  43744. else if (tlsVer == WOLFSSL_TLSV1) {
  43745. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  43746. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  43747. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  43748. #endif
  43749. }
  43750. else if (tlsVer == WOLFSSL_SSLV3) {
  43751. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  43752. defined(WOLFSSL_STATIC_RSA)
  43753. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  43754. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  43755. #endif
  43756. }
  43757. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  43758. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  43759. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  43760. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  43761. #endif
  43762. }
  43763. else if (tlsVer == WOLFSSL_DTLSV1) {
  43764. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  43765. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  43766. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  43767. #endif
  43768. }
  43769. if (server_cbf.method_ex == NULL) {
  43770. /* not enabled */
  43771. return;
  43772. }
  43773. /* Setup the keys for the TLS test */
  43774. client_cbf.certPemFile = cliCertPemFile;
  43775. client_cbf.keyPemFile = cliPrivKeyPemFile;
  43776. client_cbf.caPemFile = cliCaPemFile;
  43777. server_cbf.certPemFile = svrCertPemFile;
  43778. server_cbf.keyPemFile = svrPrivKeyPemFile;
  43779. server_cbf.caPemFile = svrCaPemFile;
  43780. client_cbf.mem = cliMem;
  43781. client_cbf.memSz = cliMemSz;
  43782. server_cbf.mem = svrMem;
  43783. server_cbf.memSz = svrMemSz;
  43784. client_cbf.devId = INVALID_DEVID;
  43785. server_cbf.devId = INVALID_DEVID;
  43786. /* Perform TLS server and client test */
  43787. /* First test is at WOLFSSL_CTX level */
  43788. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  43789. /* Check for success */
  43790. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43791. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43792. /* Second test is a WOLFSSL object level */
  43793. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  43794. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  43795. /* Check for success */
  43796. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43797. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43798. }
  43799. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  43800. #ifdef WOLFSSL_STATIC_MEMORY
  43801. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  43802. defined(SESSION_CERTS)
  43803. #ifdef OPENSSL_EXTRA
  43804. #define TEST_TLS_STATIC_MEMSZ (400000)
  43805. #else
  43806. #define TEST_TLS_STATIC_MEMSZ (320000)
  43807. #endif
  43808. #else
  43809. #define TEST_TLS_STATIC_MEMSZ (80000)
  43810. #endif
  43811. static void test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  43812. {
  43813. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  43814. WOLFSSL_MEM_STATS mem_stats;
  43815. WOLFSSL_MEM_CONN_STATS ssl_stats;
  43816. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  43817. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  43818. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  43819. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  43820. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  43821. #endif
  43822. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  43823. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  43824. /* this should fail because kMaxCtxClients == 2 */
  43825. AssertNull((ssl3 = wolfSSL_new(ctx)));
  43826. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  43827. #ifdef DEBUG_WOLFSSL
  43828. wolfSSL_PrintStatsConn(&ssl_stats);
  43829. #endif
  43830. (void)ssl_stats;
  43831. }
  43832. /* display collected statistics */
  43833. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  43834. #ifdef DEBUG_WOLFSSL
  43835. wolfSSL_PrintStats(&mem_stats);
  43836. #endif
  43837. (void)mem_stats;
  43838. }
  43839. wolfSSL_free(ssl1);
  43840. wolfSSL_free(ssl2);
  43841. }
  43842. #endif /* WOLFSSL_STATIC_MEMORY */
  43843. static void test_wolfSSL_CTX_StaticMemory(void)
  43844. {
  43845. #ifdef WOLFSSL_STATIC_MEMORY
  43846. wolfSSL_method_func method_func;
  43847. WOLFSSL_CTX* ctx;
  43848. const int kMaxCtxClients = 2;
  43849. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  43850. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  43851. int tlsVer;
  43852. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  43853. #endif
  43854. #endif
  43855. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  43856. printf(testingFmt, "test_wolfSSL_CTX_StaticMemory()");
  43857. #ifndef NO_WOLFSSL_SERVER
  43858. #ifndef WOLFSSL_NO_TLS12
  43859. method_func = wolfTLSv1_2_server_method_ex;
  43860. #else
  43861. method_func = wolfTLSv1_3_server_method_ex;
  43862. #endif
  43863. #else
  43864. #ifndef WOLFSSL_NO_TLS12
  43865. method_func = wolfTLSv1_2_client_method_ex;
  43866. #else
  43867. method_func = wolfTLSv1_3_client_method_ex;
  43868. #endif
  43869. #endif
  43870. /* Test creating CTX directly from static memory pool */
  43871. ctx = NULL;
  43872. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  43873. &ctx, method_func, svrMem, sizeof(svrMem),
  43874. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  43875. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  43876. wolfSSL_CTX_free(ctx);
  43877. ctx = NULL;
  43878. /* Test for heap allocated CTX, then assigning static pool to it */
  43879. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  43880. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  43881. NULL, svrMem, sizeof(svrMem),
  43882. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  43883. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  43884. wolfSSL_CTX_free(ctx);
  43885. /* TLS Level Tests using static memory */
  43886. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  43887. #ifndef NO_RSA
  43888. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  43889. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  43890. svrCertFile, cliCertFile, cliKeyFile,
  43891. cliCertFile, svrCertFile, svrKeyFile,
  43892. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  43893. }
  43894. #endif
  43895. #ifdef HAVE_ECC
  43896. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  43897. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  43898. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  43899. cliEccCertFile, eccCertFile, eccKeyFile,
  43900. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  43901. }
  43902. #endif
  43903. #ifdef HAVE_ED25519
  43904. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  43905. if (tlsVer == WOLFSSL_DTLSV1) continue;
  43906. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  43907. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  43908. cliEdCertFile, edCertFile, edKeyFile,
  43909. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  43910. }
  43911. #endif
  43912. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  43913. printf(resultFmt, passed);
  43914. #endif
  43915. }
  43916. /*----------------------------------------------------------------------------*
  43917. | Main
  43918. *----------------------------------------------------------------------------*/
  43919. void ApiTest(void)
  43920. {
  43921. printf("\n-----------------Porting tests------------------\n");
  43922. AssertTrue(test_fileAccess());
  43923. printf(" Begin API Tests\n");
  43924. AssertIntEQ(test_wolfSSL_Init(), WOLFSSL_SUCCESS);
  43925. /* wolfcrypt initialization tests */
  43926. test_wolfSSL_Method_Allocators();
  43927. #ifndef NO_WOLFSSL_SERVER
  43928. test_wolfSSL_CTX_new(wolfSSLv23_server_method());
  43929. #endif
  43930. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  43931. (!defined(NO_RSA) || defined(HAVE_ECC))
  43932. test_for_double_Free();
  43933. #endif
  43934. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  43935. test_wolfSSL_get_finished();
  43936. #endif
  43937. test_SSL_CIPHER_get_xxx();
  43938. test_wolfSSL_ERR_strings();
  43939. test_wolfSSL_EVP_shake128();
  43940. test_wolfSSL_EVP_shake256();
  43941. test_EVP_blake2();
  43942. test_EVP_MD_do_all();
  43943. test_OBJ_NAME_do_all();
  43944. test_wolfSSL_CTX_use_certificate_file();
  43945. AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS);
  43946. test_wolfSSL_CTX_use_PrivateKey_file();
  43947. test_wolfSSL_CTX_load_verify_locations();
  43948. test_wolfSSL_CertManagerCheckOCSPResponse();
  43949. test_wolfSSL_CertManagerLoadCABuffer();
  43950. test_wolfSSL_CertManagerGetCerts();
  43951. test_wolfSSL_CertManagerSetVerify();
  43952. test_wolfSSL_CertManagerNameConstraint();
  43953. test_wolfSSL_CertManagerNameConstraint2();
  43954. test_wolfSSL_CertManagerNameConstraint3();
  43955. test_wolfSSL_CertManagerNameConstraint4();
  43956. test_wolfSSL_CertManagerNameConstraint5();
  43957. test_wolfSSL_CertManagerCRL();
  43958. test_wolfSSL_CTX_load_verify_locations_ex();
  43959. test_wolfSSL_CTX_load_verify_buffer_ex();
  43960. test_wolfSSL_CTX_load_verify_chain_buffer_format();
  43961. test_wolfSSL_CTX_add1_chain_cert();
  43962. test_wolfSSL_CTX_use_certificate_chain_file_format();
  43963. test_wolfSSL_CTX_trust_peer_cert();
  43964. test_wolfSSL_CTX_SetTmpDH_file();
  43965. test_wolfSSL_CTX_SetTmpDH_buffer();
  43966. test_wolfSSL_CTX_SetMinMaxDhKey_Sz();
  43967. test_wolfSSL_CTX_der_load_verify_locations();
  43968. test_wolfSSL_CTX_enable_disable();
  43969. test_wolfSSL_CTX_ticket_API();
  43970. test_server_wolfSSL_new();
  43971. test_client_wolfSSL_new();
  43972. test_wolfSSL_SetTmpDH_file();
  43973. test_wolfSSL_SetTmpDH_buffer();
  43974. test_wolfSSL_SetMinMaxDhKey_Sz();
  43975. test_SetTmpEC_DHE_Sz();
  43976. test_wolfSSL_CTX_get0_privatekey();
  43977. test_wolfSSL_dtls_set_mtu();
  43978. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  43979. defined(HAVE_IO_TESTS_DEPENDENCIES)
  43980. test_wolfSSL_read_write();
  43981. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  43982. test_wolfSSL_reuse_WOLFSSLobj();
  43983. #endif
  43984. test_wolfSSL_CTX_verifyDepth_ServerClient();
  43985. test_wolfSSL_dtls_export();
  43986. test_wolfSSL_tls_export();
  43987. #endif
  43988. AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS);
  43989. AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);
  43990. /* TLS extensions tests */
  43991. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  43992. test_wolfSSL_UseSNI();
  43993. #endif
  43994. test_wolfSSL_UseTrustedCA();
  43995. test_wolfSSL_UseMaxFragment();
  43996. test_wolfSSL_UseTruncatedHMAC();
  43997. test_wolfSSL_UseSupportedCurve();
  43998. test_wolfSSL_UseALPN();
  43999. test_wolfSSL_DisableExtendedMasterSecret();
  44000. test_wolfSSL_wolfSSL_UseSecureRenegotiation();
  44001. /* X509 tests */
  44002. test_wolfSSL_X509_NAME_get_entry();
  44003. test_wolfSSL_PKCS12();
  44004. test_wolfSSL_no_password_cb();
  44005. test_wolfSSL_PKCS8();
  44006. test_wolfSSL_PKCS8_ED25519();
  44007. test_wolfSSL_PKCS8_ED448();
  44008. test_wolfSSL_PKCS5();
  44009. test_wolfSSL_URI();
  44010. test_wolfSSL_TBS();
  44011. test_wolfSSL_X509_verify();
  44012. test_wolfSSL_X509_TLS_version();
  44013. test_wc_PemToDer();
  44014. test_wc_AllocDer();
  44015. test_wc_CertPemToDer();
  44016. test_wc_PubKeyPemToDer();
  44017. test_wc_PemPubKeyToDer();
  44018. /*OCSP Stapling. */
  44019. AssertIntEQ(test_wolfSSL_UseOCSPStapling(), WOLFSSL_SUCCESS);
  44020. AssertIntEQ(test_wolfSSL_UseOCSPStaplingV2(), WOLFSSL_SUCCESS);
  44021. /* Multicast */
  44022. test_wolfSSL_mcast();
  44023. /* compatibility tests */
  44024. test_wolfSSL_lhash();
  44025. test_wolfSSL_X509_NAME();
  44026. test_wolfSSL_X509_NAME_hash();
  44027. #ifndef NO_BIO
  44028. test_wolfSSL_X509_INFO();
  44029. #endif
  44030. test_wolfSSL_X509_subject_name_hash();
  44031. test_wolfSSL_X509_issuer_name_hash();
  44032. test_wolfSSL_X509_check_host();
  44033. test_wolfSSL_X509_check_email();
  44034. test_wolfSSL_DES();
  44035. test_wolfSSL_certs();
  44036. test_wolfSSL_X509_check_private_key();
  44037. test_wolfSSL_ASN1_TIME_print();
  44038. test_wolfSSL_ASN1_UTCTIME_print();
  44039. test_wolfSSL_ASN1_TIME_diff();
  44040. test_wolfSSL_ASN1_GENERALIZEDTIME_free();
  44041. test_wolfSSL_private_keys();
  44042. test_wolfSSL_PEM_read_PrivateKey();
  44043. test_wolfSSL_PEM_PrivateKey();
  44044. #ifndef NO_BIO
  44045. test_wolfSSL_PEM_bio_RSAKey();
  44046. test_wolfSSL_PEM_bio_DSAKey();
  44047. test_wolfSSL_PEM_bio_ECKey();
  44048. test_wolfSSL_PEM_RSAPrivateKey();
  44049. test_wolfSSL_PEM_PUBKEY();
  44050. #endif
  44051. test_DSA_do_sign_verify();
  44052. test_wolfSSL_tmp_dh();
  44053. test_wolfSSL_ctrl();
  44054. test_wolfSSL_EVP_MD_size();
  44055. test_wolfSSL_EVP_MD_pkey_type();
  44056. test_wolfSSL_EVP_Digest();
  44057. test_wolfSSL_EVP_Digest_all();
  44058. test_wolfSSL_EVP_PKEY_new_mac_key();
  44059. test_wolfSSL_EVP_MD_hmac_signing();
  44060. test_wolfSSL_EVP_MD_rsa_signing();
  44061. test_wolfSSL_EVP_MD_ecc_signing();
  44062. test_wolfSSL_EVP_PKEY_print_public();
  44063. test_wolfSSL_EVP_ENCODE_CTX_new();
  44064. test_wolfSSL_EVP_ENCODE_CTX_free();
  44065. test_wolfSSL_EVP_EncodeInit();
  44066. test_wolfSSL_EVP_EncodeUpdate();
  44067. test_wolfSSL_EVP_EncodeFinal();
  44068. test_wolfSSL_EVP_DecodeInit();
  44069. test_wolfSSL_EVP_DecodeUpdate();
  44070. test_wolfSSL_EVP_DecodeFinal();
  44071. test_wolfSSL_CTX_add_extra_chain_cert();
  44072. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  44073. test_wolfSSL_ERR_peek_last_error_line();
  44074. #endif
  44075. #ifndef NO_BIO
  44076. test_wolfSSL_ERR_print_errors_cb();
  44077. AssertFalse(test_wolfSSL_GetLoggingCb());
  44078. AssertFalse(test_WOLFSSL_ERROR_MSG());
  44079. AssertFalse(test_wc_ERR_remove_state());
  44080. AssertFalse(test_wc_ERR_print_errors_fp());
  44081. #endif
  44082. test_wolfSSL_set_options();
  44083. test_wolfSSL_sk_SSL_CIPHER();
  44084. test_wolfSSL_set1_curves_list();
  44085. test_wolfSSL_set1_sigalgs_list();
  44086. test_wolfSSL_PKCS7_certs();
  44087. test_wolfSSL_X509_STORE_CTX();
  44088. test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup();
  44089. test_wolfSSL_X509_STORE_CTX_get0_current_issuer();
  44090. test_wolfSSL_msgCb();
  44091. test_wolfSSL_either_side();
  44092. test_wolfSSL_DTLS_either_side();
  44093. test_generate_cookie();
  44094. test_wolfSSL_X509_STORE_set_flags();
  44095. test_wolfSSL_X509_LOOKUP_load_file();
  44096. test_wolfSSL_X509_Name_canon();
  44097. test_wolfSSL_X509_LOOKUP_ctrl_file();
  44098. test_wolfSSL_X509_LOOKUP_ctrl_hash_dir();
  44099. test_wolfSSL_X509_NID();
  44100. test_wolfSSL_X509_STORE_CTX_set_time();
  44101. test_wolfSSL_get0_param();
  44102. test_wolfSSL_X509_VERIFY_PARAM_set1_host();
  44103. test_wolfSSL_X509_VERIFY_PARAM_set1_ip();
  44104. test_wolfSSL_X509_STORE_CTX_get0_store();
  44105. test_wolfSSL_X509_STORE();
  44106. test_wolfSSL_X509_STORE_load_locations();
  44107. test_X509_STORE_get0_objects();
  44108. test_wolfSSL_X509_load_crl_file();
  44109. test_wolfSSL_BN();
  44110. test_wolfSSL_CTX_get0_set1_param();
  44111. #ifndef NO_BIO
  44112. test_wolfSSL_PEM_read_bio();
  44113. test_wolfSSL_BIO();
  44114. #endif
  44115. test_wolfSSL_ASN1_STRING();
  44116. test_wolfSSL_ASN1_BIT_STRING();
  44117. test_wolfSSL_a2i_ASN1_INTEGER();
  44118. test_wolfSSL_a2i_IPADDRESS();
  44119. test_wolfSSL_X509();
  44120. test_wolfSSL_X509_VERIFY_PARAM();
  44121. test_wolfSSL_X509_sign();
  44122. test_wolfSSL_X509_sign2();
  44123. test_wolfSSL_X509_get0_tbs_sigalg();
  44124. test_wolfSSL_X509_ALGOR_get0();
  44125. test_wolfSSL_X509_get_X509_PUBKEY();
  44126. test_wolfSSL_X509_PUBKEY_RSA();
  44127. test_wolfSSL_X509_PUBKEY_EC();
  44128. test_wolfSSL_X509_PUBKEY_DSA();
  44129. test_wolfSSL_RAND();
  44130. test_wolfSSL_BUF();
  44131. test_wolfSSL_set_tlsext_status_type();
  44132. test_wolfSSL_ASN1_TIME_adj();
  44133. test_wolfSSL_ASN1_TIME_to_tm();
  44134. test_wolfSSL_X509_cmp_time();
  44135. test_wolfSSL_X509_time_adj();
  44136. test_wolfSSL_CTX_set_client_CA_list();
  44137. test_wolfSSL_CTX_add_client_CA();
  44138. test_wolfSSL_CTX_set_srp_username();
  44139. test_wolfSSL_CTX_set_srp_password();
  44140. test_wolfSSL_CTX_set_keylog_callback();
  44141. test_wolfSSL_CTX_get_keylog_callback();
  44142. test_wolfSSL_Tls12_Key_Logging_test();
  44143. test_wolfSSL_Tls13_Key_Logging_test();
  44144. test_wolfSSL_Tls13_postauth();
  44145. test_wolfSSL_CTX_set_ecdh_auto();
  44146. test_wolfSSL_set_minmax_proto_version();
  44147. test_wolfSSL_THREADID_hash();
  44148. test_wolfSSL_RAND_set_rand_method();
  44149. test_wolfSSL_RAND_bytes();
  44150. test_wolfSSL_BN_rand();
  44151. test_wolfSSL_pseudo_rand();
  44152. test_wolfSSL_PKCS8_Compat();
  44153. test_wolfSSL_PKCS8_d2i();
  44154. test_error_queue_per_thread();
  44155. test_wolfSSL_ERR_put_error();
  44156. #ifndef NO_BIO
  44157. test_wolfSSL_ERR_print_errors();
  44158. #endif
  44159. test_wolfSSL_HMAC();
  44160. test_wolfSSL_CMAC();
  44161. test_wolfSSL_OBJ();
  44162. test_wolfSSL_i2a_ASN1_OBJECT();
  44163. test_wolfSSL_OBJ_cmp();
  44164. test_wolfSSL_OBJ_txt2nid();
  44165. test_wolfSSL_OBJ_txt2obj();
  44166. test_wolfSSL_i2t_ASN1_OBJECT();
  44167. test_wolfSSL_PEM_write_bio_X509();
  44168. test_wolfSSL_X509_NAME_ENTRY();
  44169. test_wolfSSL_X509_set_name();
  44170. test_wolfSSL_X509_set_notAfter();
  44171. test_wolfSSL_X509_set_notBefore();
  44172. test_wolfSSL_X509_set_version();
  44173. #ifndef NO_BIO
  44174. test_wolfSSL_BIO_gets();
  44175. test_wolfSSL_BIO_puts();
  44176. test_wolfSSL_BIO_should_retry();
  44177. test_wolfSSL_d2i_PUBKEY();
  44178. test_wolfSSL_BIO_write();
  44179. test_wolfSSL_BIO_connect();
  44180. test_wolfSSL_BIO_accept();
  44181. test_wolfSSL_BIO_printf();
  44182. test_wolfSSL_BIO_f_md();
  44183. test_wolfSSL_BIO_up_ref();
  44184. #endif
  44185. test_wolfSSL_cert_cb();
  44186. test_wolfSSL_SESSION();
  44187. test_wolfSSL_ticket_keys();
  44188. test_wolfSSL_DES_ecb_encrypt();
  44189. test_wolfSSL_sk_GENERAL_NAME();
  44190. test_wolfSSL_GENERAL_NAME_print();
  44191. test_wolfSSL_sk_DIST_POINT();
  44192. test_wolfSSL_MD4();
  44193. test_wolfSSL_RSA();
  44194. test_wolfSSL_RSA_DER();
  44195. test_wolfSSL_RSA_get0_key();
  44196. test_wolfSSL_RSA_meth();
  44197. test_wolfSSL_verify_mode();
  44198. test_wolfSSL_verify_depth();
  44199. test_wolfSSL_HMAC_CTX();
  44200. test_wolfSSL_msg_callback();
  44201. test_wolfSSL_SHA();
  44202. test_wolfSSL_DH_1536_prime();
  44203. test_wolfSSL_PEM_write_DHparams();
  44204. test_wolfSSL_PEM_read_DHparams();
  44205. test_wolfSSL_AES_ecb_encrypt();
  44206. test_wolfSSL_MD5();
  44207. test_wolfSSL_MD5_Transform();
  44208. test_wolfSSL_SHA_Transform();
  44209. test_wolfSSL_SHA256();
  44210. test_wolfSSL_SHA256_Transform();
  44211. test_wolfSSL_SHA224();
  44212. test_wolfSSL_SHA512_Transform();
  44213. test_wolfSSL_X509_get_serialNumber();
  44214. test_wolfSSL_X509_CRL();
  44215. test_wolfSSL_d2i_X509_REQ();
  44216. test_wolfSSL_PEM_read_X509();
  44217. test_wolfSSL_PEM_read();
  44218. #ifndef NO_BIO
  44219. test_wolfSSL_PEM_X509_INFO_read_bio();
  44220. test_wolfSSL_PEM_read_bio_ECPKParameters();
  44221. #endif
  44222. test_wolfSSL_X509_STORE_get1_certs();
  44223. test_wolfSSL_X509_NAME_ENTRY_get_object();
  44224. test_wolfSSL_OpenSSL_add_all_algorithms();
  44225. test_wolfSSL_OPENSSL_hexstr2buf();
  44226. test_wolfSSL_ASN1_STRING_print_ex();
  44227. test_wolfSSL_ASN1_TIME_to_generalizedtime();
  44228. test_wolfSSL_ASN1_INTEGER_get_set();
  44229. test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS();
  44230. test_wolfSSL_i2c_ASN1_INTEGER();
  44231. test_wolfSSL_X509_check_ca();
  44232. test_wolfSSL_X509_check_ip_asc();
  44233. test_wolfSSL_DC_cert();
  44234. test_wolfSSL_DES_ncbc();
  44235. test_wolfSSL_AES_cbc_encrypt();
  44236. test_wolfssl_EVP_aes_gcm_AAD_2_parts();
  44237. test_wolfssl_EVP_aes_gcm();
  44238. test_wolfSSL_PKEY_up_ref();
  44239. test_wolfSSL_EVP_Cipher_extra();
  44240. test_wolfSSL_d2i_and_i2d_PublicKey();
  44241. test_wolfSSL_d2i_and_i2d_DSAparams();
  44242. test_wolfSSL_i2d_PrivateKey();
  44243. test_wolfSSL_OCSP_id_get0_info();
  44244. test_wolfSSL_i2d_OCSP_CERTID();
  44245. test_wolfSSL_OCSP_id_cmp();
  44246. test_wolfSSL_OCSP_SINGLERESP_get0_id();
  44247. test_wolfSSL_OCSP_single_get0_status();
  44248. test_wolfSSL_OCSP_resp_count();
  44249. test_wolfSSL_OCSP_resp_get0();
  44250. test_wolfSSL_EVP_PKEY_derive();
  44251. test_wolfSSL_EVP_PBE_scrypt();
  44252. #ifndef NO_RSA
  44253. test_wolfSSL_RSA_padding_add_PKCS1_PSS();
  44254. #endif
  44255. test_wolfSSL_RSA_sign_sha3();
  44256. test_CONF_modules_xxx();
  44257. test_CRYPTO_set_dynlock_xxx();
  44258. test_CRYPTO_THREADID_xxx();
  44259. test_ENGINE_cleanup();
  44260. test_wolfSSL_EC_KEY_set_group();
  44261. #if defined(OPENSSL_ALL)
  44262. test_wolfSSL_X509_PUBKEY_get();
  44263. test_wolfSSL_sk_CIPHER_description();
  44264. test_wolfSSL_get_ciphers_compat();
  44265. test_wolfSSL_d2i_DHparams();
  44266. test_wolfSSL_i2d_DHparams();
  44267. test_wolfSSL_ASN1_STRING_to_UTF8();
  44268. test_wolfSSL_ASN1_UNIVERSALSTRING_to_string();
  44269. test_wolfSSL_EC_KEY_dup();
  44270. test_wolfSSL_EVP_PKEY_set1_get1_DSA();
  44271. test_wolfSSL_DSA_SIG();
  44272. test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY();
  44273. test_wolfSSL_EVP_PKEY_set1_get1_DH();
  44274. test_wolfSSL_CTX_ctrl();
  44275. test_wolfSSL_DH_check();
  44276. test_wolfSSL_EVP_PKEY_assign();
  44277. test_wolfSSL_EVP_PKEY_base_id();
  44278. test_wolfSSL_EVP_PKEY_id();
  44279. test_wolfSSL_EVP_PKEY_keygen();
  44280. test_wolfSSL_EVP_PKEY_keygen_init();
  44281. test_wolfSSL_EVP_PKEY_missing_parameters();
  44282. test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits();
  44283. test_wolfSSL_EVP_CIPHER_CTX_iv_length();
  44284. test_wolfSSL_EVP_CIPHER_CTX_key_length();
  44285. test_wolfSSL_EVP_CIPHER_CTX_set_key_length();
  44286. test_wolfSSL_EVP_CIPHER_CTX_set_iv();
  44287. test_wolfSSL_EVP_PKEY_CTX_new_id();
  44288. test_wolfSSL_EVP_rc4();
  44289. test_wolfSSL_EVP_enc_null();
  44290. test_wolfSSL_EVP_rc2_cbc();
  44291. test_wolfSSL_EVP_mdc2();
  44292. test_wolfSSL_EVP_md4();
  44293. test_wolfSSL_EVP_aes_256_gcm();
  44294. test_wolfSSL_EVP_aes_192_gcm();
  44295. test_wolfSSL_EVP_ripemd160();
  44296. test_wolfSSL_EVP_get_digestbynid();
  44297. test_wolfSSL_EVP_MD_nid();
  44298. test_wolfSSL_EVP_PKEY_get0_EC_KEY();
  44299. test_wolfSSL_EVP_X_STATE();
  44300. test_wolfSSL_EVP_X_STATE_LEN();
  44301. test_wolfSSL_EVP_CIPHER_block_size();
  44302. test_wolfSSL_EVP_CIPHER_iv_length();
  44303. test_wolfSSL_EVP_SignInit_ex();
  44304. test_wolfSSL_EVP_DigestFinal_ex();
  44305. test_wolfSSL_EVP_PKEY_assign_DH();
  44306. test_wolfSSL_EVP_BytesToKey();
  44307. test_wolfSSL_EVP_PKEY_param_check();
  44308. test_wolfSSL_QT_EVP_PKEY_CTX_free();
  44309. test_IncCtr();
  44310. test_wolfSSL_OBJ_ln();
  44311. test_wolfSSL_OBJ_sn();
  44312. test_wolfSSL_TXT_DB();
  44313. test_wolfSSL_NCONF();
  44314. #endif /* OPENSSL_ALL */
  44315. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  44316. AssertIntEQ(test_wolfSSL_CTX_use_certificate_ASN1(), WOLFSSL_SUCCESS);
  44317. #ifndef NO_BIO
  44318. test_wolfSSL_d2i_PrivateKeys_bio();
  44319. #endif
  44320. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  44321. test_wolfSSL_X509_CA_num();
  44322. test_wolfSSL_X509_get_version();
  44323. #ifndef NO_BIO
  44324. test_wolfSSL_X509_print();
  44325. test_wolfSSL_BIO_get_len();
  44326. #endif
  44327. test_wolfSSL_RSA_verify();
  44328. test_wolfSSL_X509V3_EXT_get();
  44329. test_wolfSSL_X509V3_EXT_nconf();
  44330. test_wolfSSL_X509V3_EXT();
  44331. test_wolfSSL_X509_get_extension_flags();
  44332. test_wolfSSL_X509_get_ext();
  44333. test_wolfSSL_X509_get_ext_by_NID();
  44334. test_wolfSSL_X509_get_ext_subj_alt_name();
  44335. test_wolfSSL_X509_get_ext_count();
  44336. test_wolfSSL_X509_EXTENSION_new();
  44337. test_wolfSSL_X509_EXTENSION_get_object();
  44338. test_wolfSSL_X509_EXTENSION_get_data();
  44339. test_wolfSSL_X509_EXTENSION_get_critical();
  44340. test_wolfSSL_X509V3_EXT_print();
  44341. test_wolfSSL_X509_cmp();
  44342. #ifndef NO_BIO
  44343. test_wolfSSL_RSA_print();
  44344. test_wolfSSL_ASN1_STRING_print();
  44345. #endif
  44346. test_wolfSSL_ASN1_get_object();
  44347. test_openssl_generate_key_and_cert();
  44348. test_wolfSSL_EC_get_builtin_curves();
  44349. test_wolfSSL_CRYPTO_memcmp();
  44350. test_wolfSSL_read_detect_TCP_disconnect();
  44351. /* test the no op functions for compatibility */
  44352. test_no_op_functions();
  44353. /* OpenSSL EVP_PKEY API tests */
  44354. test_EVP_PKEY_rsa();
  44355. test_wolfSSL_EVP_PKEY_encrypt();
  44356. test_wolfSSL_EVP_PKEY_sign();
  44357. test_EVP_PKEY_ec();
  44358. test_EVP_PKEY_cmp();
  44359. /* OpenSSL error API tests */
  44360. test_ERR_load_crypto_strings();
  44361. /* OpenSSL sk_X509 API test */
  44362. test_sk_X509();
  44363. /* OpenSSL X509 API test */
  44364. test_X509_get_signature_nid();
  44365. /* OpenSSL X509 REQ API test */
  44366. test_X509_REQ();
  44367. /* OpenSSL PKCS7 API test */
  44368. test_wolfssl_PKCS7();
  44369. test_wolfSSL_PKCS7_SIGNED_new();
  44370. #ifndef NO_BIO
  44371. test_wolfSSL_PEM_write_bio_PKCS7();
  44372. #ifdef HAVE_SMIME
  44373. test_wolfSSL_SMIME_read_PKCS7();
  44374. #endif
  44375. #endif
  44376. /* wolfCrypt ASN tests */
  44377. test_wc_CreateEncryptedPKCS8Key();
  44378. test_wc_GetPkcs8TraditionalOffset();
  44379. test_wc_SetSubjectRaw();
  44380. test_wc_GetSubjectRaw();
  44381. test_wc_SetIssuerRaw();
  44382. test_wc_SetIssueBuffer();
  44383. test_wc_SetSubjectKeyId();
  44384. test_wc_SetSubject();
  44385. test_CheckCertSignature();
  44386. /* wolfCrypt ECC tests */
  44387. test_wc_ecc_get_curve_size_from_name();
  44388. test_wc_ecc_get_curve_id_from_name();
  44389. test_wc_ecc_get_curve_id_from_params();
  44390. #ifdef WOLFSSL_TLS13
  44391. /* TLS v1.3 API tests */
  44392. test_tls13_apis();
  44393. #endif
  44394. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  44395. !defined(WOLFSSL_NO_CLIENT_AUTH))
  44396. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  44397. /* Bad certificate signature tests */
  44398. AssertIntEQ(test_EccSigFailure_cm(), ASN_SIG_CONFIRM_E);
  44399. AssertIntEQ(test_RsaSigFailure_cm(), ASN_SIG_CONFIRM_E);
  44400. #endif /* NO_CERTS */
  44401. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  44402. !defined(NO_OLD_TLS))
  44403. /* public key callback tests */
  44404. test_DhCallbacks();
  44405. #endif
  44406. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  44407. test_export_keying_material();
  44408. #endif /* HAVE_KEYING_MATERIAL */
  44409. test_wolfSSL_CTX_get_min_proto_version();
  44410. test_wolfSSL_security_level();
  44411. test_wolfSSL_SSL_in_init();
  44412. test_wolfSSL_EC_curve();
  44413. test_wolfSSL_CTX_set_timeout();
  44414. test_wolfSSL_OpenSSL_version();
  44415. test_wolfSSL_set_psk_use_session_callback();
  44416. test_CONF_CTX_FILE();
  44417. test_CONF_CTX_CMDLINE();
  44418. test_wolfSSL_CRYPTO_get_ex_new_index();
  44419. test_wolfSSL_DH();
  44420. /*wolfcrypt */
  44421. printf("\n-----------------wolfcrypt unit tests------------------\n");
  44422. AssertFalse(test_wolfCrypt_Init());
  44423. AssertFalse(test_wc_InitMd5());
  44424. AssertFalse(test_wc_Md5Update());
  44425. AssertFalse(test_wc_Md5Final());
  44426. AssertFalse(test_wc_InitSha());
  44427. AssertFalse(test_wc_ShaUpdate());
  44428. AssertFalse(test_wc_ShaFinal());
  44429. AssertFalse(test_wc_InitSha256());
  44430. AssertFalse(test_wc_Sha256Update());
  44431. AssertFalse(test_wc_Sha256Final());
  44432. AssertFalse(test_wc_Sha256FinalRaw());
  44433. AssertFalse(test_wc_Sha256GetFlags());
  44434. AssertFalse(test_wc_Sha256Free());
  44435. AssertFalse(test_wc_Sha256GetHash());
  44436. AssertFalse(test_wc_Sha256Copy());
  44437. AssertFalse(test_wc_InitSha512());
  44438. AssertFalse(test_wc_Sha512Update());
  44439. AssertFalse(test_wc_Sha512Final());
  44440. AssertFalse(test_wc_Sha512GetFlags());
  44441. AssertFalse(test_wc_Sha512FinalRaw());
  44442. AssertFalse(test_wc_Sha512Free());
  44443. AssertFalse(test_wc_Sha512GetHash());
  44444. AssertFalse(test_wc_Sha512Copy());
  44445. AssertFalse(test_wc_InitSha512_224());
  44446. AssertFalse(test_wc_Sha512_224Update());
  44447. AssertFalse(test_wc_Sha512_224Final());
  44448. AssertFalse(test_wc_Sha512_224GetFlags());
  44449. AssertFalse(test_wc_Sha512_224FinalRaw());
  44450. AssertFalse(test_wc_Sha512_224Free());
  44451. AssertFalse(test_wc_Sha512_224GetHash());
  44452. AssertFalse(test_wc_Sha512_224Copy());
  44453. AssertFalse(test_wc_InitSha512_256());
  44454. AssertFalse(test_wc_Sha512_256Update());
  44455. AssertFalse(test_wc_Sha512_256Final());
  44456. AssertFalse(test_wc_Sha512_256GetFlags());
  44457. AssertFalse(test_wc_Sha512_256FinalRaw());
  44458. AssertFalse(test_wc_Sha512_256Free());
  44459. AssertFalse(test_wc_Sha512_256GetHash());
  44460. AssertFalse(test_wc_Sha512_256Copy());
  44461. AssertFalse(test_wc_InitSha384());
  44462. AssertFalse(test_wc_Sha384Update());
  44463. AssertFalse(test_wc_Sha384Final());
  44464. AssertFalse(test_wc_Sha384GetFlags());
  44465. AssertFalse(test_wc_Sha384FinalRaw());
  44466. AssertFalse(test_wc_Sha384Free());
  44467. AssertFalse(test_wc_Sha384GetHash());
  44468. AssertFalse(test_wc_Sha384Copy());
  44469. AssertFalse(test_wc_InitSha224());
  44470. AssertFalse(test_wc_Sha224Update());
  44471. AssertFalse(test_wc_Sha224Final());
  44472. AssertFalse(test_wc_Sha224SetFlags());
  44473. AssertFalse(test_wc_Sha224GetFlags());
  44474. AssertFalse(test_wc_Sha224Free());
  44475. AssertFalse(test_wc_Sha224GetHash());
  44476. AssertFalse(test_wc_Sha224Copy());
  44477. AssertFalse(test_wc_InitBlake2b());
  44478. AssertFalse(test_wc_InitBlake2b_WithKey());
  44479. AssertFalse(test_wc_InitBlake2s_WithKey());
  44480. AssertFalse(test_wc_InitRipeMd());
  44481. AssertFalse(test_wc_RipeMdUpdate());
  44482. AssertFalse(test_wc_RipeMdFinal());
  44483. AssertIntEQ(test_wc_InitSha3(), 0);
  44484. AssertIntEQ(testing_wc_Sha3_Update(), 0);
  44485. AssertIntEQ(test_wc_Sha3_224_Final(), 0);
  44486. AssertIntEQ(test_wc_Sha3_256_Final(), 0);
  44487. AssertIntEQ(test_wc_Sha3_384_Final(), 0);
  44488. AssertIntEQ(test_wc_Sha3_512_Final(), 0);
  44489. AssertIntEQ(test_wc_Sha3_224_Copy(), 0);
  44490. AssertIntEQ(test_wc_Sha3_256_Copy(), 0);
  44491. AssertIntEQ(test_wc_Sha3_384_Copy(), 0);
  44492. AssertIntEQ(test_wc_Sha3_512_Copy(), 0);
  44493. AssertIntEQ(test_wc_Sha3_GetFlags(), 0);
  44494. AssertIntEQ(test_wc_InitShake256(), 0);
  44495. AssertIntEQ(testing_wc_Shake256_Update(), 0);
  44496. AssertIntEQ(test_wc_Shake256_Final(), 0);
  44497. AssertIntEQ(test_wc_Shake256_Copy(), 0);
  44498. AssertIntEQ(test_wc_Shake256Hash(), 0);
  44499. AssertFalse(test_wc_Md5HmacSetKey());
  44500. AssertFalse(test_wc_Md5HmacUpdate());
  44501. AssertFalse(test_wc_Md5HmacFinal());
  44502. AssertFalse(test_wc_ShaHmacSetKey());
  44503. AssertFalse(test_wc_ShaHmacUpdate());
  44504. AssertFalse(test_wc_ShaHmacFinal());
  44505. AssertFalse(test_wc_Sha224HmacSetKey());
  44506. AssertFalse(test_wc_Sha224HmacUpdate());
  44507. AssertFalse(test_wc_Sha224HmacFinal());
  44508. AssertFalse(test_wc_Sha256HmacSetKey());
  44509. AssertFalse(test_wc_Sha256HmacUpdate());
  44510. AssertFalse(test_wc_Sha256HmacFinal());
  44511. AssertFalse(test_wc_Sha384HmacSetKey());
  44512. AssertFalse(test_wc_Sha384HmacUpdate());
  44513. AssertFalse(test_wc_Sha384HmacFinal());
  44514. AssertIntEQ(test_wc_HashInit(), 0);
  44515. AssertIntEQ(test_wc_HashSetFlags(), 0);
  44516. AssertIntEQ(test_wc_HashGetFlags(), 0);
  44517. AssertIntEQ(test_wc_InitCmac(), 0);
  44518. AssertIntEQ(test_wc_CmacUpdate(), 0);
  44519. AssertIntEQ(test_wc_CmacFinal(), 0);
  44520. AssertIntEQ(test_wc_AesCmacGenerate(), 0);
  44521. AssertIntEQ(test_wc_AesGcmStream(), 0);
  44522. AssertIntEQ(test_wc_Des3_SetIV(), 0);
  44523. AssertIntEQ(test_wc_Des3_SetKey(), 0);
  44524. AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
  44525. AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
  44526. AssertIntEQ(test_wc_Des3_EcbEncrypt(), 0);
  44527. AssertIntEQ(test_wc_IdeaSetKey(), 0);
  44528. AssertIntEQ(test_wc_IdeaSetIV(), 0);
  44529. AssertIntEQ(test_wc_IdeaCipher(), 0);
  44530. AssertIntEQ(test_wc_IdeaCbcEncyptDecrypt(), 0);
  44531. AssertIntEQ(test_wc_Chacha_SetKey(), 0);
  44532. AssertIntEQ(test_wc_Chacha_Process(), 0);
  44533. AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0);
  44534. AssertIntEQ(test_wc_Poly1305SetKey(), 0);
  44535. AssertIntEQ(test_wc_CamelliaSetKey(), 0);
  44536. AssertIntEQ(test_wc_CamelliaSetIV(), 0);
  44537. AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0);
  44538. AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0);
  44539. AssertIntEQ(test_wc_RabbitSetKey(), 0);
  44540. AssertIntEQ(test_wc_RabbitProcess(), 0);
  44541. AssertIntEQ(test_wc_Arc4SetKey(), 0);
  44542. AssertIntEQ(test_wc_Arc4Process(), 0);
  44543. AssertIntEQ(test_wc_Rc2SetKey(), 0);
  44544. AssertIntEQ(test_wc_Rc2SetIV(), 0);
  44545. AssertIntEQ(test_wc_Rc2EcbEncryptDecrypt(), 0);
  44546. AssertIntEQ(test_wc_Rc2CbcEncryptDecrypt(), 0);
  44547. AssertIntEQ(test_wc_AesSetKey(), 0);
  44548. AssertIntEQ(test_wc_AesSetIV(), 0);
  44549. AssertIntEQ(test_wc_AesCbcEncryptDecrypt(), 0);
  44550. AssertIntEQ(test_wc_AesCtrEncryptDecrypt(), 0);
  44551. AssertIntEQ(test_wc_AesGcmSetKey(), 0);
  44552. AssertIntEQ(test_wc_AesGcmEncryptDecrypt(), 0);
  44553. AssertIntEQ(test_wc_GmacSetKey(), 0);
  44554. AssertIntEQ(test_wc_GmacUpdate(), 0);
  44555. AssertIntEQ(test_wc_InitRsaKey(), 0);
  44556. AssertIntEQ(test_wc_RsaPrivateKeyDecode(), 0);
  44557. AssertIntEQ(test_wc_RsaPublicKeyDecode(), 0);
  44558. AssertIntEQ(test_wc_RsaPublicKeyDecodeRaw(), 0);
  44559. AssertIntEQ(test_wc_MakeRsaKey(), 0);
  44560. AssertIntEQ(test_wc_SetKeyUsage (), 0);
  44561. AssertIntEQ(test_wc_CheckProbablePrime (), 0);
  44562. AssertIntEQ(test_wc_RsaPSS_Verify (), 0);
  44563. AssertIntEQ(test_wc_RsaPSS_VerifyCheck (), 0);
  44564. AssertIntEQ(test_wc_RsaPSS_VerifyCheckInline (), 0);
  44565. AssertIntEQ(test_wc_SetMutexCb(), 0);
  44566. AssertIntEQ(test_wc_LockMutex_ex(), 0);
  44567. AssertIntEQ(test_wc_RsaKeyToDer(), 0);
  44568. AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0);
  44569. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0);
  44570. AssertIntEQ(test_wc_RsaPublicEncryptDecrypt_ex(), 0);
  44571. AssertIntEQ(test_wc_RsaEncryptSize(), 0);
  44572. AssertIntEQ(test_wc_RsaSSL_SignVerify(), 0);
  44573. AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0);
  44574. AssertIntEQ(test_RsaDecryptBoundsCheck(), 0);
  44575. AssertIntEQ(test_wc_AesCcmSetKey(), 0);
  44576. AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0);
  44577. AssertIntEQ(test_wc_Hc128_SetKey(), 0);
  44578. AssertIntEQ(test_wc_Hc128_Process(), 0);
  44579. AssertIntEQ(test_wc_InitDsaKey(), 0);
  44580. AssertIntEQ(test_wc_DsaSignVerify(), 0);
  44581. AssertIntEQ(test_wc_DsaPublicPrivateKeyDecode(), 0);
  44582. AssertIntEQ(test_wc_MakeDsaKey(), 0);
  44583. AssertIntEQ(test_wc_DsaKeyToDer(), 0);
  44584. AssertIntEQ(test_wc_DsaKeyToPublicDer(), 0);
  44585. AssertIntEQ(test_wc_DsaImportParamsRaw(), 0);
  44586. AssertIntEQ(test_wc_DsaImportParamsRawCheck(), 0);
  44587. AssertIntEQ(test_wc_DsaExportParamsRaw(), 0);
  44588. AssertIntEQ(test_wc_DsaExportKeyRaw(), 0);
  44589. AssertIntEQ(test_wc_SignatureGetSize_ecc(), 0);
  44590. AssertIntEQ(test_wc_SignatureGetSize_rsa(), 0);
  44591. wolfCrypt_Cleanup();
  44592. #ifdef OPENSSL_EXTRA
  44593. /*wolfSSL_EVP_get_cipherbynid test*/
  44594. test_wolfSSL_EVP_get_cipherbynid();
  44595. test_wolfSSL_EVP_CIPHER_CTX();
  44596. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44597. test_wolfSSL_EC();
  44598. #endif
  44599. test_wolfSSL_ECDSA_SIG();
  44600. test_ECDSA_size_sign();
  44601. test_ED25519();
  44602. test_ED448();
  44603. test_EC_i2d();
  44604. #endif
  44605. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  44606. !defined(HAVE_SELFTEST) && \
  44607. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  44608. test_wc_ecc_get_curve_id_from_dp_params();
  44609. #endif
  44610. #ifdef HAVE_HASHDRBG
  44611. #ifdef TEST_RESEED_INTERVAL
  44612. AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0);
  44613. #endif
  44614. AssertIntEQ(test_wc_RNG_GenerateBlock(), 0);
  44615. #endif
  44616. AssertIntEQ(test_get_rand_digit(), 0);
  44617. AssertIntEQ(test_get_digit_count(), 0);
  44618. AssertIntEQ(test_mp_cond_copy(), 0);
  44619. AssertIntEQ(test_mp_rand(), 0);
  44620. AssertIntEQ(test_get_digit(), 0);
  44621. AssertIntEQ(test_wc_export_int(), 0);
  44622. AssertIntEQ(test_wc_InitRngNonce(), 0);
  44623. AssertIntEQ(test_wc_InitRngNonce_ex(), 0);
  44624. AssertIntEQ(test_wc_ed25519_make_key(), 0);
  44625. AssertIntEQ(test_wc_ed25519_init(), 0);
  44626. AssertIntEQ(test_wc_ed25519_sign_msg(), 0);
  44627. AssertIntEQ(test_wc_ed25519_import_public(), 0);
  44628. AssertIntEQ(test_wc_ed25519_import_private_key(), 0);
  44629. AssertIntEQ(test_wc_ed25519_export(), 0);
  44630. AssertIntEQ(test_wc_ed25519_size(), 0);
  44631. AssertIntEQ(test_wc_ed25519_exportKey(), 0);
  44632. AssertIntEQ(test_wc_Ed25519PublicKeyToDer(), 0);
  44633. AssertIntEQ(test_wc_curve25519_init(), 0);
  44634. AssertIntEQ(test_wc_curve25519_size(), 0);
  44635. AssertIntEQ(test_wc_curve25519_export_key_raw(), 0);
  44636. AssertIntEQ(test_wc_curve25519_export_key_raw_ex(), 0);
  44637. AssertIntEQ(test_wc_curve25519_size (), 0);
  44638. AssertIntEQ(test_wc_curve25519_make_key (), 0);
  44639. AssertIntEQ(test_wc_curve25519_shared_secret_ex (), 0);
  44640. AssertIntEQ(test_wc_curve25519_make_pub (), 0);
  44641. AssertIntEQ(test_wc_curve25519_export_public_ex (), 0);
  44642. AssertIntEQ(test_wc_curve25519_export_private_raw_ex (), 0);
  44643. AssertIntEQ(test_wc_curve25519_import_private_raw_ex (), 0);
  44644. AssertIntEQ(test_wc_curve25519_import_private (), 0);
  44645. AssertIntEQ(test_wc_ed448_make_key(), 0);
  44646. AssertIntEQ(test_wc_ed448_init(), 0);
  44647. AssertIntEQ(test_wc_ed448_sign_msg(), 0);
  44648. AssertIntEQ(test_wc_ed448_import_public(), 0);
  44649. AssertIntEQ(test_wc_ed448_import_private_key(), 0);
  44650. AssertIntEQ(test_wc_ed448_export(), 0);
  44651. AssertIntEQ(test_wc_ed448_size(), 0);
  44652. AssertIntEQ(test_wc_ed448_exportKey(), 0);
  44653. AssertIntEQ(test_wc_Ed448PublicKeyToDer(), 0);
  44654. AssertIntEQ(test_wc_curve448_make_key (), 0);
  44655. AssertIntEQ(test_wc_curve448_shared_secret_ex (), 0);
  44656. AssertIntEQ(test_wc_curve448_export_public_ex (), 0);
  44657. AssertIntEQ(test_wc_curve448_export_private_raw_ex (), 0);
  44658. AssertIntEQ(test_wc_curve448_export_key_raw (), 0);
  44659. AssertIntEQ(test_wc_curve448_import_private_raw_ex (), 0);
  44660. AssertIntEQ(test_wc_curve448_import_private (), 0);
  44661. AssertIntEQ(test_wc_curve448_init(), 0);
  44662. AssertIntEQ(test_wc_curve448_size (), 0);
  44663. AssertIntEQ(test_wc_ecc_make_key(), 0);
  44664. AssertIntEQ(test_wc_ecc_init(), 0);
  44665. AssertIntEQ(test_wc_ecc_check_key(), 0);
  44666. AssertIntEQ(test_wc_ecc_get_generator(), 0);
  44667. AssertIntEQ(test_wc_ecc_size(), 0);
  44668. test_wc_ecc_params();
  44669. AssertIntEQ(test_wc_ecc_signVerify_hash(), 0);
  44670. PRIVATE_KEY_UNLOCK();
  44671. AssertIntEQ(test_wc_ecc_shared_secret(), 0);
  44672. AssertIntEQ(test_wc_ecc_export_x963(), 0);
  44673. PRIVATE_KEY_LOCK();
  44674. AssertIntEQ(test_wc_ecc_export_x963_ex(), 0);
  44675. AssertIntEQ(test_wc_ecc_import_x963(), 0);
  44676. AssertIntEQ(ecc_import_private_key(), 0);
  44677. AssertIntEQ(test_wc_ecc_export_private_only(), 0);
  44678. AssertIntEQ(test_wc_ecc_rs_to_sig(), 0);
  44679. AssertIntEQ(test_wc_ecc_import_raw(), 0);
  44680. AssertIntEQ(test_wc_ecc_import_unsigned(), 0);
  44681. AssertIntEQ(test_wc_ecc_sig_size(), 0);
  44682. AssertIntEQ(test_wc_ecc_ctx_new(), 0);
  44683. AssertIntEQ(test_wc_ecc_ctx_reset(), 0);
  44684. AssertIntEQ(test_wc_ecc_ctx_set_peer_salt(), 0);
  44685. AssertIntEQ(test_wc_ecc_ctx_set_info(), 0);
  44686. AssertIntEQ(test_wc_ecc_encryptDecrypt(), 0);
  44687. AssertIntEQ(test_wc_ecc_del_point(), 0);
  44688. AssertIntEQ(test_wc_ecc_pointFns(), 0);
  44689. AssertIntEQ(test_wc_ecc_shared_secret_ssh(), 0);
  44690. AssertIntEQ(test_wc_ecc_verify_hash_ex(), 0);
  44691. AssertIntEQ(test_wc_ecc_mulmod(), 0);
  44692. AssertIntEQ(test_wc_ecc_is_valid_idx(), 0);
  44693. AssertIntEQ(test_wc_ecc_get_curve_id_from_oid(), 0);
  44694. AssertIntEQ(test_wc_ecc_sig_size_calc(), 0);
  44695. AssertIntEQ(test_ToTraditional(), 0);
  44696. AssertIntEQ(test_wc_EccPrivateKeyToDer(), 0);
  44697. AssertIntEQ(test_wc_DhPublicKeyDecode(), 0);
  44698. AssertIntEQ(test_wc_Ed25519KeyToDer(), 0);
  44699. AssertIntEQ(test_wc_Ed25519PrivateKeyToDer(), 0);
  44700. AssertIntEQ(test_wc_Ed448KeyToDer(), 0);
  44701. AssertIntEQ(test_wc_Ed448PrivateKeyToDer(), 0);
  44702. AssertIntEQ(test_wc_SetAuthKeyIdFromPublicKey_ex(), 0);
  44703. AssertIntEQ(test_wc_SetSubjectBuffer(), 0);
  44704. AssertIntEQ(test_wc_SetSubjectKeyIdFromPublicKey_ex(), 0);
  44705. test_wc_PKCS7_New();
  44706. test_wc_PKCS7_Init();
  44707. test_wc_PKCS7_InitWithCert();
  44708. test_wc_PKCS7_EncodeData();
  44709. test_wc_PKCS7_EncodeSignedData();
  44710. test_wc_PKCS7_EncodeSignedData_ex();
  44711. test_wc_PKCS7_VerifySignedData();
  44712. test_wc_PKCS7_EncodeDecodeEnvelopedData();
  44713. test_wc_PKCS7_EncodeEncryptedData();
  44714. test_wc_PKCS7_Degenerate();
  44715. test_wc_PKCS7_BER();
  44716. test_PKCS7_signed_enveloped();
  44717. test_wc_PKCS7_NoDefaultSignedAttribs();
  44718. test_wc_PKCS7_SetOriEncryptCtx();
  44719. test_wc_PKCS7_SetOriDecryptCtx();
  44720. test_wc_PKCS7_DecodeCompressedData();
  44721. test_wc_i2d_PKCS12();
  44722. test_wolfSSL_CTX_LoadCRL();
  44723. test_wc_CryptoCb();
  44724. test_wolfSSL_CTX_StaticMemory();
  44725. AssertIntEQ(test_ForceZero(), 0);
  44726. AssertIntEQ(test_wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  44727. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  44728. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  44729. !defined(WOLFSSL_NO_CLIENT_AUTH))
  44730. AssertIntEQ(test_various_pathlen_chains(), WOLFSSL_SUCCESS);
  44731. #endif
  44732. /* If at some point a stub get implemented this test should fail indicating
  44733. * a need to implement a new test case
  44734. */
  44735. test_stubs_are_stubs();
  44736. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  44737. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  44738. wc_ecc_fp_free(); /* free per thread cache */
  44739. #endif
  44740. wolfSSL_Cleanup();
  44741. (void)devId;
  44742. printf(" End API Tests\n");
  44743. }