api.c 1.8 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #endif /* WOLFSSL_STATIC_MEMORY */
  45. #ifndef HEAP_HINT
  46. #define HEAP_HINT NULL
  47. #endif /* WOLFSSL_STAIC_MEMORY */
  48. #ifdef WOLFSSL_ASNC_CRYPT
  49. #include <wolfssl/wolfcrypt/async.h>
  50. #endif
  51. #ifdef HAVE_ECC
  52. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  53. #ifndef ECC_ASN963_MAX_BUF_SZ
  54. #define ECC_ASN963_MAX_BUF_SZ 133
  55. #endif
  56. #ifndef ECC_PRIV_KEY_BUF
  57. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  58. #endif
  59. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  60. /* logic to choose right key ECC size */
  61. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  62. #define KEY14 14
  63. #else
  64. #define KEY14 32
  65. #endif
  66. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  67. #define KEY16 16
  68. #else
  69. #define KEY16 32
  70. #endif
  71. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  72. #define KEY20 20
  73. #else
  74. #define KEY20 32
  75. #endif
  76. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  77. #define KEY24 24
  78. #else
  79. #define KEY24 32
  80. #endif
  81. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  82. #define KEY28 28
  83. #else
  84. #define KEY28 32
  85. #endif
  86. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  87. #define KEY30 30
  88. #else
  89. #define KEY30 32
  90. #endif
  91. #define KEY32 32
  92. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  93. #define KEY40 40
  94. #else
  95. #define KEY40 32
  96. #endif
  97. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  98. #define KEY48 48
  99. #else
  100. #define KEY48 32
  101. #endif
  102. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  103. #define KEY64 64
  104. #else
  105. #define KEY64 32
  106. #endif
  107. #if !defined(HAVE_COMP_KEY)
  108. #if !defined(NOCOMP)
  109. #define NOCOMP 0
  110. #endif
  111. #else
  112. #if !defined(COMP)
  113. #define COMP 1
  114. #endif
  115. #endif
  116. #if !defined(DER_SZ)
  117. #define DER_SZ(ks) ((ks) * 2 + 1)
  118. #endif
  119. #endif
  120. #ifndef NO_ASN
  121. #include <wolfssl/wolfcrypt/asn_public.h>
  122. #endif
  123. #include <wolfssl/error-ssl.h>
  124. #include <stdlib.h>
  125. #include <wolfssl/ssl.h> /* compatibility layer */
  126. #include <wolfssl/test.h>
  127. #include <tests/unit.h>
  128. #include "examples/server/server.h"
  129. /* for testing compatibility layer callbacks */
  130. #ifndef NO_MD5
  131. #include <wolfssl/wolfcrypt/md5.h>
  132. #endif
  133. #ifndef NO_SHA
  134. #include <wolfssl/wolfcrypt/sha.h>
  135. #endif
  136. #ifndef NO_SHA256
  137. #include <wolfssl/wolfcrypt/sha256.h>
  138. #endif
  139. #ifdef WOLFSSL_SHA512
  140. #include <wolfssl/wolfcrypt/sha512.h>
  141. #endif
  142. #ifdef WOLFSSL_SHA384
  143. #include <wolfssl/wolfcrypt/sha512.h>
  144. #endif
  145. #ifdef WOLFSSL_SHA3
  146. #include <wolfssl/wolfcrypt/sha3.h>
  147. #ifndef HEAP_HINT
  148. #define HEAP_HINT NULL
  149. #endif
  150. #endif
  151. #ifndef NO_AES
  152. #include <wolfssl/wolfcrypt/aes.h>
  153. #ifdef HAVE_AES_DECRYPT
  154. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  155. #endif
  156. #endif
  157. #ifdef WOLFSSL_RIPEMD
  158. #include <wolfssl/wolfcrypt/ripemd.h>
  159. #endif
  160. #ifndef NO_DES3
  161. #include <wolfssl/wolfcrypt/des3.h>
  162. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  163. #endif
  164. #ifdef WC_RC2
  165. #include <wolfssl/wolfcrypt/rc2.h>
  166. #endif
  167. #ifndef NO_HMAC
  168. #include <wolfssl/wolfcrypt/hmac.h>
  169. #endif
  170. #ifdef HAVE_CHACHA
  171. #include <wolfssl/wolfcrypt/chacha.h>
  172. #endif
  173. #ifdef HAVE_POLY1305
  174. #include <wolfssl/wolfcrypt/poly1305.h>
  175. #endif
  176. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  177. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  178. #endif
  179. #ifdef HAVE_CAMELLIA
  180. #include <wolfssl/wolfcrypt/camellia.h>
  181. #endif
  182. #ifndef NO_RC4
  183. #include <wolfssl/wolfcrypt/arc4.h>
  184. #endif
  185. #ifdef HAVE_BLAKE2
  186. #include <wolfssl/wolfcrypt/blake2.h>
  187. #endif
  188. #include <wolfssl/wolfcrypt/hash.h>
  189. #ifndef NO_RSA
  190. #include <wolfssl/wolfcrypt/rsa.h>
  191. #define FOURK_BUF 4096
  192. #define GEN_BUF 294
  193. #ifndef USER_CRYPTO_ERROR
  194. #define USER_CRYPTO_ERROR (-101) /* error returned by IPP lib. */
  195. #endif
  196. #endif
  197. #ifndef NO_SIG_WRAPPER
  198. #include <wolfssl/wolfcrypt/signature.h>
  199. #endif
  200. #ifdef HAVE_AESCCM
  201. #include <wolfssl/wolfcrypt/aes.h>
  202. #endif
  203. #ifdef HAVE_PKCS7
  204. #include <wolfssl/wolfcrypt/pkcs7.h>
  205. #include <wolfssl/wolfcrypt/asn.h>
  206. #ifdef HAVE_LIBZ
  207. #include <wolfssl/wolfcrypt/compress.h>
  208. #endif
  209. #endif
  210. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  211. #include <wolfssl/wolfcrypt/asn.h>
  212. #endif
  213. #ifndef NO_DSA
  214. #include <wolfssl/wolfcrypt/dsa.h>
  215. #ifndef ONEK_BUF
  216. #define ONEK_BUF 1024
  217. #endif
  218. #ifndef TWOK_BUF
  219. #define TWOK_BUF 2048
  220. #endif
  221. #ifndef FOURK_BUF
  222. #define FOURK_BUF 4096
  223. #endif
  224. #ifndef DSA_SIG_SIZE
  225. #define DSA_SIG_SIZE 40
  226. #endif
  227. #ifndef MAX_DSA_PARAM_SIZE
  228. #define MAX_DSA_PARAM_SIZE 256
  229. #endif
  230. #endif
  231. #ifdef WOLFSSL_CMAC
  232. #include <wolfssl/wolfcrypt/cmac.h>
  233. #endif
  234. #ifdef HAVE_ED25519
  235. #include <wolfssl/wolfcrypt/ed25519.h>
  236. #endif
  237. #ifdef HAVE_CURVE25519
  238. #include <wolfssl/wolfcrypt/curve25519.h>
  239. #endif
  240. #ifdef HAVE_ED448
  241. #include <wolfssl/wolfcrypt/ed448.h>
  242. #endif
  243. #ifdef HAVE_CURVE448
  244. #include <wolfssl/wolfcrypt/curve448.h>
  245. #endif
  246. #ifdef HAVE_PKCS12
  247. #include <wolfssl/wolfcrypt/pkcs12.h>
  248. #endif
  249. #include <wolfssl/wolfcrypt/logging.h>
  250. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  251. #include <wolfssl/openssl/ssl.h>
  252. #ifndef NO_ASN
  253. /* for ASN_COMMON_NAME DN_tags enum */
  254. #include <wolfssl/wolfcrypt/asn.h>
  255. #endif
  256. #ifdef HAVE_OCSP
  257. #include <wolfssl/openssl/ocsp.h>
  258. #endif
  259. #endif
  260. #ifdef OPENSSL_EXTRA
  261. #include <wolfssl/openssl/cmac.h>
  262. #include <wolfssl/openssl/x509v3.h>
  263. #include <wolfssl/openssl/asn1.h>
  264. #include <wolfssl/openssl/crypto.h>
  265. #include <wolfssl/openssl/pkcs12.h>
  266. #include <wolfssl/openssl/evp.h>
  267. #include <wolfssl/openssl/dh.h>
  268. #include <wolfssl/openssl/bn.h>
  269. #include <wolfssl/openssl/buffer.h>
  270. #include <wolfssl/openssl/pem.h>
  271. #include <wolfssl/openssl/ec.h>
  272. #include <wolfssl/openssl/engine.h>
  273. #include <wolfssl/openssl/hmac.h>
  274. #include <wolfssl/openssl/objects.h>
  275. #include <wolfssl/openssl/rand.h>
  276. #include <wolfssl/openssl/modes.h>
  277. #include <wolfssl/openssl/fips_rand.h>
  278. #include <wolfssl/openssl/kdf.h>
  279. #ifdef OPENSSL_ALL
  280. #include <wolfssl/openssl/txt_db.h>
  281. #include <wolfssl/openssl/lhash.h>
  282. #endif
  283. #ifndef NO_AES
  284. #include <wolfssl/openssl/aes.h>
  285. #endif
  286. #ifndef NO_DES3
  287. #include <wolfssl/openssl/des.h>
  288. #endif
  289. #ifdef HAVE_ECC
  290. #include <wolfssl/openssl/ecdsa.h>
  291. #endif
  292. #ifdef HAVE_PKCS7
  293. #include <wolfssl/openssl/pkcs7.h>
  294. #endif
  295. #ifdef HAVE_ED25519
  296. #include <wolfssl/openssl/ed25519.h>
  297. #endif
  298. #ifdef HAVE_ED448
  299. #include <wolfssl/openssl/ed448.h>
  300. #endif
  301. #endif /* OPENSSL_EXTRA */
  302. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  303. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  304. #include <wolfssl/wolfcrypt/srp.h>
  305. #endif
  306. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  307. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  308. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  309. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS)
  310. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  311. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  312. * number tracking */
  313. #include "wolfssl/internal.h"
  314. #endif
  315. /* force enable test buffers */
  316. #ifndef USE_CERT_BUFFERS_2048
  317. #define USE_CERT_BUFFERS_2048
  318. #endif
  319. #ifndef USE_CERT_BUFFERS_256
  320. #define USE_CERT_BUFFERS_256
  321. #endif
  322. #include <wolfssl/certs_test.h>
  323. typedef struct testVector {
  324. const char* input;
  325. const char* output;
  326. size_t inLen;
  327. size_t outLen;
  328. } testVector;
  329. #if defined(HAVE_PKCS7)
  330. typedef struct {
  331. const byte* content;
  332. word32 contentSz;
  333. int contentOID;
  334. int encryptOID;
  335. int keyWrapOID;
  336. int keyAgreeOID;
  337. byte* cert;
  338. size_t certSz;
  339. byte* privateKey;
  340. word32 privateKeySz;
  341. } pkcs7EnvelopedVector;
  342. #ifndef NO_PKCS7_ENCRYPTED_DATA
  343. typedef struct {
  344. const byte* content;
  345. word32 contentSz;
  346. int contentOID;
  347. int encryptOID;
  348. byte* encryptionKey;
  349. word32 encryptionKeySz;
  350. } pkcs7EncryptedVector;
  351. #endif
  352. #endif /* HAVE_PKCS7 */
  353. /*----------------------------------------------------------------------------*
  354. | Constants
  355. *----------------------------------------------------------------------------*/
  356. #define TEST_SUCCESS (1)
  357. #define TEST_FAIL (0)
  358. #define testingFmt " %s:"
  359. #define resultFmt " %s\n"
  360. static const char* passed = "passed";
  361. static const char* failed = "failed";
  362. #define TEST_STRING "Everyone gets Friday off."
  363. #define TEST_STRING_SZ 25
  364. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  365. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  366. #define TEST_RSA_BITS 1024
  367. #else
  368. #define TEST_RSA_BITS 2048
  369. #endif
  370. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  371. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  372. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  373. static const char* bogusFile =
  374. #ifdef _WIN32
  375. "NUL"
  376. #else
  377. "/dev/null"
  378. #endif
  379. ;
  380. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  381. enum {
  382. TESTING_RSA = 1,
  383. TESTING_ECC = 2
  384. };
  385. #ifdef WOLFSSL_QNX_CAAM
  386. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  387. static int devId = WOLFSSL_CAAM_DEVID;
  388. #else
  389. static int devId = INVALID_DEVID;
  390. #endif
  391. /*----------------------------------------------------------------------------*
  392. | Setup
  393. *----------------------------------------------------------------------------*/
  394. static int test_wolfSSL_Init(void)
  395. {
  396. int result;
  397. printf(testingFmt, "wolfSSL_Init()");
  398. result = wolfSSL_Init();
  399. printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
  400. if (result == WOLFSSL_SUCCESS) {
  401. result = 0;
  402. }
  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. if (result == WOLFSSL_SUCCESS) {
  412. result = 0;
  413. }
  414. return result;
  415. }
  416. /* Initialize the wolfCrypt state.
  417. * POST: 0 success.
  418. */
  419. static int test_wolfCrypt_Init(void)
  420. {
  421. int result;
  422. printf(testingFmt, "wolfCrypt_Init()");
  423. result = wolfCrypt_Init();
  424. printf(resultFmt, result == 0 ? passed : failed);
  425. return result;
  426. } /* END test_wolfCrypt_Init */
  427. static int test_wolfCrypt_Cleanup(void)
  428. {
  429. int result;
  430. printf(testingFmt, "wolfCrypt_Cleanup()");
  431. result = wolfCrypt_Cleanup();
  432. printf(resultFmt, result == 0 ? passed : failed);
  433. return result;
  434. }
  435. /*----------------------------------------------------------------------------*
  436. | Platform dependent function test
  437. *----------------------------------------------------------------------------*/
  438. static int test_fileAccess(void)
  439. {
  440. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  441. const char *fname[] = {
  442. svrCertFile, svrKeyFile, caCertFile,
  443. eccCertFile, eccKeyFile, eccRsaCertFile,
  444. cliCertFile, cliCertDerFile, cliKeyFile,
  445. dhParamFile,
  446. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  447. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  448. NULL
  449. };
  450. const char derfile[] = "./certs/server-cert.der";
  451. XFILE f;
  452. size_t sz;
  453. byte *buff;
  454. int i;
  455. printf(testingFmt, "test_fileAccess()");
  456. AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  457. for(i=0; fname[i] != NULL ; i++){
  458. AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  459. XFCLOSE(f);
  460. }
  461. AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  462. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  463. sz = (size_t) XFTELL(f);
  464. XREWIND(f);
  465. AssertTrue(sz == sizeof_server_cert_der_2048);
  466. AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
  467. AssertTrue(XFREAD(buff, 1, sz, f) == sz);
  468. XMEMCMP(server_cert_der_2048, buff, sz);
  469. printf(resultFmt, passed);
  470. #endif
  471. return 0;
  472. }
  473. /*----------------------------------------------------------------------------*
  474. | Method Allocators
  475. *----------------------------------------------------------------------------*/
  476. static int test_wolfSSL_Method_Allocators(void)
  477. {
  478. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  479. do { \
  480. WOLFSSL_METHOD *method; \
  481. condition(method = allocator()); \
  482. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  483. } while(0)
  484. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  485. TEST_METHOD_ALLOCATOR(a, AssertNotNull)
  486. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  487. TEST_METHOD_ALLOCATOR(a, AssertNull)
  488. #ifndef NO_OLD_TLS
  489. #ifdef WOLFSSL_ALLOW_SSLV3
  490. #ifndef NO_WOLFSSL_SERVER
  491. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  492. #endif
  493. #ifndef NO_WOLFSSL_CLIENT
  494. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  495. #endif
  496. #endif
  497. #ifdef WOLFSSL_ALLOW_TLSV10
  498. #ifndef NO_WOLFSSL_SERVER
  499. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  500. #endif
  501. #ifndef NO_WOLFSSL_CLIENT
  502. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  503. #endif
  504. #endif
  505. #ifndef NO_WOLFSSL_SERVER
  506. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  507. #endif
  508. #ifndef NO_WOLFSSL_CLIENT
  509. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  510. #endif
  511. #endif /* !NO_OLD_TLS */
  512. #ifndef WOLFSSL_NO_TLS12
  513. #ifndef NO_WOLFSSL_SERVER
  514. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  515. #endif
  516. #ifndef NO_WOLFSSL_CLIENT
  517. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  518. #endif
  519. #endif /* !WOLFSSL_NO_TLS12 */
  520. #ifdef WOLFSSL_TLS13
  521. #ifndef NO_WOLFSSL_SERVER
  522. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  523. #endif
  524. #ifndef NO_WOLFSSL_CLIENT
  525. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  526. #endif
  527. #endif /* WOLFSSL_TLS13 */
  528. #ifndef NO_WOLFSSL_SERVER
  529. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  530. #endif
  531. #ifndef NO_WOLFSSL_CLIENT
  532. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  533. #endif
  534. #ifdef WOLFSSL_DTLS
  535. #ifndef NO_OLD_TLS
  536. #ifndef NO_WOLFSSL_SERVER
  537. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  538. #endif
  539. #ifndef NO_WOLFSSL_CLIENT
  540. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  541. #endif
  542. #endif
  543. #ifndef WOLFSSL_NO_TLS12
  544. #ifndef NO_WOLFSSL_SERVER
  545. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  546. #endif
  547. #ifndef NO_WOLFSSL_CLIENT
  548. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  549. #endif
  550. #endif
  551. #endif /* WOLFSSL_DTLS */
  552. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  553. /* Stubs */
  554. #ifndef NO_WOLFSSL_SERVER
  555. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  556. #endif
  557. #ifndef NO_WOLFSSL_CLIENT
  558. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  559. #endif
  560. #endif
  561. /* Test Either Method (client or server) */
  562. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  563. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  564. #ifndef NO_OLD_TLS
  565. #ifdef WOLFSSL_ALLOW_TLSV10
  566. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  567. #endif
  568. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  569. #endif /* !NO_OLD_TLS */
  570. #ifndef WOLFSSL_NO_TLS12
  571. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  572. #endif /* !WOLFSSL_NO_TLS12 */
  573. #ifdef WOLFSSL_TLS13
  574. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  575. #endif /* WOLFSSL_TLS13 */
  576. #ifdef WOLFSSL_DTLS
  577. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  578. #ifndef NO_OLD_TLS
  579. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  580. #endif /* !NO_OLD_TLS */
  581. #ifndef WOLFSSL_NO_TLS12
  582. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  583. #endif /* !WOLFSSL_NO_TLS12 */
  584. #endif /* WOLFSSL_DTLS */
  585. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  586. return 0;
  587. }
  588. /*----------------------------------------------------------------------------*
  589. | Context
  590. *----------------------------------------------------------------------------*/
  591. #ifndef NO_WOLFSSL_SERVER
  592. static int test_wolfSSL_CTX_new(void)
  593. {
  594. WOLFSSL_CTX *ctx;
  595. WOLFSSL_METHOD* method;
  596. AssertNull(ctx = wolfSSL_CTX_new(NULL));
  597. AssertNotNull(method = wolfSSLv23_server_method());
  598. AssertNotNull(ctx = wolfSSL_CTX_new(method));
  599. wolfSSL_CTX_free(ctx);
  600. return 0;
  601. }
  602. #endif
  603. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  604. (!defined(NO_RSA) || defined(HAVE_ECC))
  605. static int test_for_double_Free(void)
  606. {
  607. WOLFSSL_CTX* ctx;
  608. WOLFSSL* ssl;
  609. int skipTest = 0;
  610. const char* testCertFile;
  611. const char* testKeyFile;
  612. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  613. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  614. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  615. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  616. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  617. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  618. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  619. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  620. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  621. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  622. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  623. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  624. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  625. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  626. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  627. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  628. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  629. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  630. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  631. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  632. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  633. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  634. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  635. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  636. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  637. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  638. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  639. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  640. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  641. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  642. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  643. #ifdef OPENSSL_EXTRA
  644. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  645. "!SRP:!kRSA:!aNULL:!eNULL";
  646. #endif
  647. #ifndef NO_RSA
  648. testCertFile = svrCertFile;
  649. testKeyFile = svrKeyFile;
  650. #elif defined(HAVE_ECC)
  651. testCertFile = eccCertFile;
  652. testKeyFile = eccKeyFile;
  653. #else
  654. skipTest = 1;
  655. #endif
  656. if (skipTest != 1) {
  657. #ifndef NO_WOLFSSL_SERVER
  658. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  659. AssertNotNull(ctx);
  660. #else
  661. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  662. AssertNotNull(ctx);
  663. #endif
  664. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  665. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  666. ssl = wolfSSL_new(ctx);
  667. AssertNotNull(ssl);
  668. /* First test freeing SSL, then CTX */
  669. wolfSSL_free(ssl);
  670. wolfSSL_CTX_free(ctx);
  671. #ifndef NO_WOLFSSL_CLIENT
  672. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  673. AssertNotNull(ctx);
  674. #else
  675. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  676. AssertNotNull(ctx);
  677. #endif
  678. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  679. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  680. ssl = wolfSSL_new(ctx);
  681. AssertNotNull(ssl);
  682. /* Next test freeing CTX then SSL */
  683. wolfSSL_CTX_free(ctx);
  684. wolfSSL_free(ssl);
  685. #ifndef NO_WOLFSSL_SERVER
  686. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  687. AssertNotNull(ctx);
  688. #else
  689. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  690. AssertNotNull(ctx);
  691. #endif
  692. /* Test setting ciphers at ctx level */
  693. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  694. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  695. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  696. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  697. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  698. /* only update TLSv13 suites */
  699. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  700. #endif
  701. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  702. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  703. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  704. /* only update pre-TLSv13 suites */
  705. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
  706. #endif
  707. #ifdef OPENSSL_EXTRA
  708. AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  709. #endif
  710. AssertNotNull(ssl = wolfSSL_new(ctx));
  711. wolfSSL_CTX_free(ctx);
  712. wolfSSL_free(ssl);
  713. #ifndef NO_WOLFSSL_CLIENT
  714. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  715. AssertNotNull(ctx);
  716. #else
  717. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  718. AssertNotNull(ctx);
  719. #endif
  720. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
  721. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
  722. ssl = wolfSSL_new(ctx);
  723. AssertNotNull(ssl);
  724. /* test setting ciphers at SSL level */
  725. AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  726. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  727. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  728. /* only update TLSv13 suites */
  729. AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  730. #endif
  731. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  732. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  733. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  734. /* only update pre-TLSv13 suites */
  735. AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  736. #endif
  737. wolfSSL_CTX_free(ctx);
  738. wolfSSL_free(ssl);
  739. }
  740. return 0;
  741. }
  742. #endif
  743. static int test_wolfSSL_CTX_use_certificate_file(void)
  744. {
  745. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  746. WOLFSSL_CTX *ctx;
  747. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  748. /* invalid context */
  749. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  750. WOLFSSL_FILETYPE_PEM));
  751. /* invalid cert file */
  752. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  753. WOLFSSL_FILETYPE_PEM));
  754. /* invalid cert type */
  755. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  756. #ifdef NO_RSA
  757. /* rsa needed */
  758. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  759. #else
  760. /* success */
  761. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  762. #endif
  763. wolfSSL_CTX_free(ctx);
  764. #endif
  765. return 0;
  766. }
  767. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  768. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  769. {
  770. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  771. WOLFSSL_CTX* ctx;
  772. int ret;
  773. printf(testingFmt, "wolfSSL_CTX_use_certificate_ASN1()");
  774. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  775. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  776. server_cert_der_2048);
  777. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  778. wolfSSL_CTX_free(ctx);
  779. if (ret == WOLFSSL_SUCCESS) {
  780. ret = 0;
  781. }
  782. return ret;
  783. #else
  784. return 0;
  785. #endif
  786. }
  787. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  788. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  789. * context using buffer.
  790. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  791. * --enable-testcert flag.
  792. */
  793. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  794. {
  795. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  796. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  797. WOLFSSL_CTX* ctx;
  798. int ret;
  799. printf(testingFmt, "wolfSSL_CTX_use_certificate_buffer()");
  800. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  801. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  802. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  803. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  804. wolfSSL_CTX_free(ctx);
  805. if (ret == WOLFSSL_SUCCESS) {
  806. ret = 0;
  807. }
  808. return ret;
  809. #else
  810. return 0;
  811. #endif
  812. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  813. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  814. {
  815. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  816. WOLFSSL_CTX *ctx;
  817. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  818. /* invalid context */
  819. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  820. WOLFSSL_FILETYPE_PEM));
  821. /* invalid key file */
  822. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  823. WOLFSSL_FILETYPE_PEM));
  824. /* invalid key type */
  825. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  826. /* success */
  827. #ifdef NO_RSA
  828. /* rsa needed */
  829. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  830. #else
  831. /* success */
  832. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  833. #endif
  834. wolfSSL_CTX_free(ctx);
  835. #endif
  836. return 0;
  837. }
  838. /* test both file and buffer versions along with unloading trusted peer certs */
  839. static int test_wolfSSL_CTX_trust_peer_cert(void)
  840. {
  841. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  842. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  843. WOLFSSL_CTX *ctx;
  844. WOLFSSL* ssl;
  845. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  846. AssertNotNull(ssl = wolfSSL_new(ctx));
  847. #if !defined(NO_FILESYSTEM)
  848. /* invalid file */
  849. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  850. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  851. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  852. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  853. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  854. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  855. /* success */
  856. AssertIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  857. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  858. /* unload cert */
  859. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  860. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  861. /* invalid file */
  862. AssertIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  863. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  864. AssertIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  865. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  866. AssertIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  867. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  868. /* success */
  869. AssertIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  870. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  871. #ifdef WOLFSSL_LOCAL_X509_STORE
  872. /* unload cert */
  873. AssertIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  874. AssertIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  875. #endif
  876. #endif
  877. /* Test of loading certs from buffers */
  878. /* invalid buffer */
  879. AssertIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  880. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  881. /* success */
  882. #ifdef USE_CERT_BUFFERS_1024
  883. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  884. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  885. #endif
  886. #ifdef USE_CERT_BUFFERS_2048
  887. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  888. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  889. #endif
  890. /* unload cert */
  891. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  892. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  893. wolfSSL_free(ssl);
  894. wolfSSL_CTX_free(ctx);
  895. #endif
  896. return 0;
  897. }
  898. static int test_wolfSSL_CTX_load_verify_locations(void)
  899. {
  900. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  901. WOLFSSL_CTX *ctx;
  902. #ifndef NO_RSA
  903. WOLFSSL_CERT_MANAGER* cm;
  904. #ifdef PERSIST_CERT_CACHE
  905. int cacheSz;
  906. #endif
  907. #endif
  908. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  909. const char* load_certs_path = "./certs/external";
  910. const char* load_no_certs_path = "./examples";
  911. const char* load_expired_path = "./certs/test/expired";
  912. #endif
  913. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  914. /* invalid arguments */
  915. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  916. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  917. /* invalid ca file */
  918. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  919. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  920. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  921. (defined(WOLFSSL_QT) && \
  922. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  923. /* invalid path */
  924. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  925. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  926. #endif
  927. /* load ca cert */
  928. #ifdef NO_RSA
  929. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  930. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  931. #else /* Skip the following test without RSA certs. */
  932. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  933. #ifdef PERSIST_CERT_CACHE
  934. /* Get cert cache size */
  935. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  936. #endif
  937. /* Test unloading CA's */
  938. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  939. #ifdef PERSIST_CERT_CACHE
  940. /* Verify no certs (result is less than cacheSz) */
  941. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  942. #endif
  943. /* load ca cert again */
  944. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  945. /* Test getting CERT_MANAGER */
  946. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  947. /* Test unloading CA's using CM */
  948. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  949. #ifdef PERSIST_CERT_CACHE
  950. /* Verify no certs (result is less than cacheSz) */
  951. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  952. #endif
  953. #endif
  954. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  955. /* Test loading CA certificates using a path */
  956. #ifdef NO_RSA
  957. /* failure here okay since certs in external directory are RSA */
  958. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  959. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  960. #else
  961. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  962. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  963. #endif
  964. /* Test loading path with no files */
  965. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  966. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  967. /* Test loading expired CA certificates */
  968. #ifdef NO_RSA
  969. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  970. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  971. WOLFSSL_SUCCESS);
  972. #else
  973. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  974. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  975. WOLFSSL_SUCCESS);
  976. #endif
  977. /* Test loading CA certificates and ignoring all errors */
  978. #ifdef NO_RSA
  979. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  980. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  981. #else
  982. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  983. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  984. #endif
  985. #endif
  986. wolfSSL_CTX_free(ctx);
  987. #endif
  988. return 0;
  989. }
  990. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  991. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  992. {
  993. int ret;
  994. WOLFSSL_CERT_MANAGER* cm;
  995. cm = wolfSSL_CertManagerNew();
  996. if (cm == NULL) {
  997. printf("test_cm_load_ca failed\n");
  998. return -1;
  999. }
  1000. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1001. wolfSSL_CertManagerFree(cm);
  1002. return ret;
  1003. }
  1004. static int test_cm_load_ca_file(const char* ca_cert_file)
  1005. {
  1006. int ret = 0;
  1007. byte* cert_buf = NULL;
  1008. size_t cert_sz = 0;
  1009. #if defined(WOLFSSL_PEM_TO_DER)
  1010. DerBuffer* pDer = NULL;
  1011. #endif
  1012. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1013. if (ret == 0) {
  1014. /* normal test */
  1015. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1016. if (ret == WOLFSSL_SUCCESS) {
  1017. /* test including null terminator in length */
  1018. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1019. if (tmp == NULL) {
  1020. ret = MEMORY_E;
  1021. }
  1022. else {
  1023. cert_buf = tmp;
  1024. cert_buf[cert_sz] = '\0';
  1025. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1026. WOLFSSL_FILETYPE_PEM);
  1027. }
  1028. }
  1029. #if defined(WOLFSSL_PEM_TO_DER)
  1030. if (ret == WOLFSSL_SUCCESS) {
  1031. /* test loading DER */
  1032. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1033. if (ret == 0 && pDer != NULL) {
  1034. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1035. WOLFSSL_FILETYPE_ASN1);
  1036. wc_FreeDer(&pDer);
  1037. }
  1038. }
  1039. #endif
  1040. }
  1041. free(cert_buf);
  1042. return ret;
  1043. }
  1044. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1045. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  1046. {
  1047. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  1048. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  1049. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  1050. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1051. defined(HAVE_LIGHTY)
  1052. WOLFSSL_CERT_MANAGER* cm = NULL;
  1053. /* Raw OCSP response bytes captured using the following setup:
  1054. * - Run responder with
  1055. * openssl ocsp -port 9999 -ndays 9999
  1056. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1057. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1058. * -rkey certs/ocsp/ocsp-responder-key.pem
  1059. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1060. * - Run client with
  1061. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1062. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1063. * -cert certs/ocsp/server1-cert.pem
  1064. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1065. * - Copy raw response from Wireshark.
  1066. */
  1067. byte response[] = {
  1068. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1069. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1070. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1071. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1072. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1073. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1074. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1075. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1076. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1077. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1078. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1079. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1080. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1081. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1082. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1083. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1084. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1085. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1086. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1087. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1088. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1089. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1090. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1091. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1092. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1093. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1094. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1095. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1096. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1097. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1098. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1099. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1100. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1101. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1102. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1103. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1104. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1105. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1106. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1107. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1108. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1109. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1110. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1111. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1112. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1113. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1114. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1115. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1116. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1117. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1118. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1119. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1120. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1121. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1122. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1123. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1124. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1125. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1126. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1127. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1128. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1129. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1130. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1131. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1132. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1133. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1134. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1135. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1136. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1137. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1138. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1139. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1140. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1141. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1142. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1143. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1144. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1145. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1146. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1147. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1148. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1149. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1150. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1151. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1152. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1153. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1154. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1155. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1156. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1157. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1158. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1159. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1160. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1161. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1162. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1163. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1164. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1165. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1166. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1167. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1168. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1169. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1170. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1171. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1172. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1173. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1174. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1175. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1176. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1177. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1178. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1179. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1180. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1181. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1182. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1183. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1184. 0x59, 0x59, 0xa0, 0x07
  1185. };
  1186. OcspEntry entry[1];
  1187. CertStatus status[1];
  1188. OcspRequest* request;
  1189. byte serial[] = {0x05};
  1190. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1191. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1192. printf(testingFmt, "wolfSSL_CertManagerCheckOCSPResponse()");
  1193. XMEMSET(entry, 0, sizeof(OcspEntry));
  1194. XMEMSET(status, 0, sizeof(CertStatus));
  1195. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1196. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1197. DYNAMIC_TYPE_OCSP_REQUEST);
  1198. AssertNotNull(request->serial);
  1199. request->serialSz = sizeof(serial);
  1200. XMEMCPY(request->serial, serial, sizeof(serial));
  1201. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1202. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1203. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1204. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1205. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1206. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1207. /* Response should be valid. */
  1208. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1209. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1210. /* Flip a byte in the request serial number, response should be invalid
  1211. * now. */
  1212. request->serial[0] ^= request->serial[0];
  1213. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1214. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1215. wolfSSL_OCSP_REQUEST_free(request);
  1216. wolfSSL_CertManagerFree(cm);
  1217. printf(resultFmt, passed);
  1218. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1219. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1220. #endif /* HAVE_OCSP */
  1221. return 0;
  1222. }
  1223. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1224. {
  1225. int ret;
  1226. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1227. const char* ca_cert = "./certs/ca-cert.pem";
  1228. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1229. ret = test_cm_load_ca_file(ca_cert);
  1230. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1231. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1232. #elif defined(NO_RSA)
  1233. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1234. #else
  1235. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1236. #endif
  1237. ret = test_cm_load_ca_file(ca_expired_cert);
  1238. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1239. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1240. if (ret == WOLFSSL_FATAL_ERROR)
  1241. #elif defined(NO_RSA)
  1242. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1243. if (ret == ASN_UNKNOWN_OID_E)
  1244. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1245. !defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1246. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1247. if (ret == ASN_AFTER_DATE_E)
  1248. #else
  1249. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1250. if (ret == WOLFSSL_SUCCESS)
  1251. #endif
  1252. #endif
  1253. {
  1254. ret = 0;
  1255. }
  1256. return ret;
  1257. }
  1258. static int test_wolfSSL_CertManagerGetCerts(void)
  1259. {
  1260. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1261. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1262. defined(WOLFSSL_SIGNER_DER_CERT)
  1263. WOLFSSL_CERT_MANAGER* cm = NULL;
  1264. WOLFSSL_STACK* sk = NULL;
  1265. X509* x509 = NULL;
  1266. X509* cert1 = NULL;
  1267. FILE* file1 = NULL;
  1268. #ifdef DEBUG_WOLFSSL_VERBOSE
  1269. WOLFSSL_BIO* bio = NULL;
  1270. #endif
  1271. int i = 0;
  1272. printf(testingFmt, "wolfSSL_CertManagerGetCerts()");
  1273. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1274. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1275. fclose(file1);
  1276. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1277. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1278. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1279. "./certs/ca-cert.pem", NULL));
  1280. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1281. for (i = 0; i < sk_X509_num(sk); i++) {
  1282. x509 = sk_X509_value(sk, i);
  1283. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1284. #ifdef DEBUG_WOLFSSL_VERBOSE
  1285. bio = BIO_new(wolfSSL_BIO_s_file());
  1286. if (bio != NULL) {
  1287. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  1288. X509_print(bio, x509);
  1289. BIO_free(bio);
  1290. }
  1291. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1292. }
  1293. wolfSSL_X509_free(cert1);
  1294. sk_X509_pop_free(sk, NULL);
  1295. wolfSSL_CertManagerFree(cm);
  1296. printf(resultFmt, passed);
  1297. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1298. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1299. defined(WOLFSSL_SIGNER_DER_CERT) */
  1300. return 0;
  1301. }
  1302. static int test_wolfSSL_CertManagerSetVerify(void)
  1303. {
  1304. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1305. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1306. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1307. int ret = 0;
  1308. WOLFSSL_CERT_MANAGER* cm;
  1309. int tmp = myVerifyAction;
  1310. const char* ca_cert = "./certs/ca-cert.pem";
  1311. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1312. cm = wolfSSL_CertManagerNew();
  1313. AssertNotNull(cm);
  1314. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1315. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1316. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1317. AssertIntEQ(ret, -1);
  1318. #else
  1319. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1320. #endif
  1321. /* Use the test CB that always accepts certs */
  1322. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1323. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1324. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1325. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1326. {
  1327. const char* verifyCert = "./certs/server-cert.pem";
  1328. /* Use the test CB that always fails certs */
  1329. myVerifyAction = VERIFY_FORCE_FAIL;
  1330. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1331. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1332. }
  1333. #endif
  1334. wolfSSL_CertManagerFree(cm);
  1335. myVerifyAction = tmp;
  1336. #endif
  1337. return 0;
  1338. }
  1339. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1340. defined(DEBUG_UNIT_TEST_CERTS)
  1341. /* Used when debugging name constraint tests. Not static to allow use in
  1342. * multiple locations with complex define guards. */
  1343. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1344. {
  1345. BIO* out = BIO_new_file(fileName, "wb");
  1346. if (out != NULL) {
  1347. PEM_write_bio_X509(out, x509);
  1348. BIO_free(out);
  1349. }
  1350. }
  1351. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1352. {
  1353. BIO* out = BIO_new_file(fileName, "wb");
  1354. if (out != NULL) {
  1355. BIO_write(out, der, derSz);
  1356. BIO_free(out);
  1357. }
  1358. }
  1359. #else
  1360. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1361. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1362. #endif
  1363. static int test_wolfSSL_CertManagerNameConstraint(void)
  1364. {
  1365. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1366. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1367. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1368. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1369. !defined(NO_SHA256)
  1370. WOLFSSL_CERT_MANAGER* cm;
  1371. WOLFSSL_EVP_PKEY *priv;
  1372. WOLFSSL_X509_NAME* name;
  1373. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1374. const char* server_cert = "./certs/test/server-goodcn.pem";
  1375. int i = 0;
  1376. static const byte extNameConsOid[] = {85, 29, 30};
  1377. RsaKey key;
  1378. WC_RNG rng;
  1379. byte *der;
  1380. int derSz;
  1381. word32 idx = 0;
  1382. byte *pt;
  1383. WOLFSSL_X509 *x509, *ca;
  1384. wc_InitRng(&rng);
  1385. /* load in CA private key for signing */
  1386. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, devId), 0);
  1387. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1388. sizeof_server_key_der_2048), 0);
  1389. /* get ca certificate then alter it */
  1390. AssertNotNull(der =
  1391. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1392. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1393. WOLFSSL_FILETYPE_ASN1));
  1394. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1395. XMEMCPY(der, pt, derSz);
  1396. /* find the name constraint extension and alter it */
  1397. pt = der;
  1398. for (i = 0; i < derSz - 3; i++) {
  1399. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1400. pt += 3;
  1401. break;
  1402. }
  1403. pt++;
  1404. }
  1405. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1406. /* go to the length value and set it to 0 */
  1407. while (i < derSz && *pt != 0x81) {
  1408. pt++;
  1409. i++;
  1410. }
  1411. AssertIntNE(i, derSz); /* did not place to alter */
  1412. pt++;
  1413. *pt = 0x00;
  1414. /* resign the altered certificate */
  1415. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1416. FOURK_BUF, &key, NULL, &rng)), 0);
  1417. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1418. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1419. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1420. wolfSSL_CertManagerFree(cm);
  1421. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1422. wolfSSL_X509_free(x509);
  1423. wc_FreeRsaKey(&key);
  1424. wc_FreeRng(&rng);
  1425. /* add email alt name to satisfy constraint */
  1426. pt = (byte*)server_key_der_2048;
  1427. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1428. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1429. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1430. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1431. WOLFSSL_FILETYPE_ASN1));
  1432. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1433. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1434. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1435. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1436. /* Good cert test with proper alt email name */
  1437. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1438. WOLFSSL_FILETYPE_PEM));
  1439. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1440. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1441. AssertNotNull(name = X509_NAME_new());
  1442. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1443. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1444. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1445. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1446. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1447. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1448. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1449. X509_NAME_free(name);
  1450. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1451. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1452. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1453. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1454. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1455. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1456. wolfSSL_X509_free(x509);
  1457. /* Cert with bad alt name list */
  1458. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1459. WOLFSSL_FILETYPE_PEM));
  1460. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1461. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1462. AssertNotNull(name = X509_NAME_new());
  1463. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1464. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1465. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1466. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1467. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1468. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1469. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1470. X509_NAME_free(name);
  1471. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1472. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1473. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1474. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1475. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1476. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1477. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1478. wolfSSL_CertManagerFree(cm);
  1479. wolfSSL_X509_free(x509);
  1480. wolfSSL_X509_free(ca);
  1481. wolfSSL_EVP_PKEY_free(priv);
  1482. #endif
  1483. return 0;
  1484. }
  1485. static int test_wolfSSL_CertManagerNameConstraint2(void)
  1486. {
  1487. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1488. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1489. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1490. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1491. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  1492. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  1493. const char* server_cert = "./certs/server-cert.pem";
  1494. WOLFSSL_CERT_MANAGER* cm;
  1495. WOLFSSL_X509 *x509, *ca;
  1496. const unsigned char *der;
  1497. const unsigned char *pt;
  1498. WOLFSSL_EVP_PKEY *priv;
  1499. WOLFSSL_X509_NAME* name;
  1500. int derSz;
  1501. /* C=US*/
  1502. char altName[] = {
  1503. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1504. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  1505. };
  1506. /* C=ID */
  1507. char altNameFail[] = {
  1508. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1509. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  1510. };
  1511. /* C=US ST=California*/
  1512. char altNameExc[] = {
  1513. 0x30, 0x22,
  1514. 0x31, 0x0B,
  1515. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  1516. 0x31, 0x13,
  1517. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  1518. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  1519. };
  1520. /* load in CA private key for signing */
  1521. pt = ca_key_der_2048;
  1522. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  1523. sizeof_ca_key_der_2048));
  1524. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1525. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1526. WOLFSSL_FILETYPE_ASN1));
  1527. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1528. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1529. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1530. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1531. WOLFSSL_FILETYPE_PEM));
  1532. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1533. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1534. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1535. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1536. #else
  1537. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1538. #endif
  1539. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1540. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1541. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1542. /* add in matching DIR alt name and resign */
  1543. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1544. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1545. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1546. #else
  1547. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1548. #endif
  1549. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1550. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1551. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1552. wolfSSL_X509_free(x509);
  1553. /* check verify fail */
  1554. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1555. WOLFSSL_FILETYPE_PEM));
  1556. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1557. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1558. /* add in miss matching DIR alt name and resign */
  1559. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1560. ASN_DIR_TYPE);
  1561. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1562. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1563. #else
  1564. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1565. #endif
  1566. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1567. #ifndef WOLFSSL_NO_ASN_STRICT
  1568. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1569. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1570. #else
  1571. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1572. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1573. #endif
  1574. /* check that it still fails if one bad altname and one good altname is in
  1575. * the certificate */
  1576. wolfSSL_X509_free(x509);
  1577. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1578. WOLFSSL_FILETYPE_PEM));
  1579. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1580. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1581. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1582. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1583. ASN_DIR_TYPE);
  1584. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1585. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1586. #else
  1587. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1588. #endif
  1589. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1590. #ifndef WOLFSSL_NO_ASN_STRICT
  1591. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1592. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1593. #else
  1594. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1595. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1596. #endif
  1597. /* check it fails with switching position of bad altname */
  1598. wolfSSL_X509_free(x509);
  1599. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1600. WOLFSSL_FILETYPE_PEM));
  1601. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1602. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1603. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1604. ASN_DIR_TYPE);
  1605. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1606. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1607. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1608. #else
  1609. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1610. #endif
  1611. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1612. #ifndef WOLFSSL_NO_ASN_STRICT
  1613. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1614. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1615. #else
  1616. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1617. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1618. #endif
  1619. wolfSSL_CertManagerFree(cm);
  1620. wolfSSL_X509_free(x509);
  1621. wolfSSL_X509_free(ca);
  1622. /* now test with excluded name constraint */
  1623. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1624. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  1625. WOLFSSL_FILETYPE_ASN1));
  1626. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1627. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1628. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1629. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1630. WOLFSSL_FILETYPE_PEM));
  1631. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  1632. ASN_DIR_TYPE);
  1633. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1634. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1635. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1636. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1637. #else
  1638. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1639. #endif
  1640. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1641. #ifndef WOLFSSL_NO_ASN_STRICT
  1642. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1643. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1644. #else
  1645. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1646. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1647. #endif
  1648. wolfSSL_CertManagerFree(cm);
  1649. wolfSSL_X509_free(x509);
  1650. wolfSSL_X509_free(ca);
  1651. wolfSSL_EVP_PKEY_free(priv);
  1652. #endif
  1653. return 0;
  1654. }
  1655. static int test_wolfSSL_CertManagerNameConstraint3(void)
  1656. {
  1657. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1658. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1659. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1660. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1661. !defined(NO_SHA256)
  1662. WOLFSSL_CERT_MANAGER* cm;
  1663. WOLFSSL_EVP_PKEY *priv;
  1664. WOLFSSL_X509_NAME* name;
  1665. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  1666. const char* server_cert = "./certs/test/server-goodcn.pem";
  1667. byte *der;
  1668. int derSz;
  1669. byte *pt;
  1670. WOLFSSL_X509 *x509, *ca;
  1671. pt = (byte*)server_key_der_2048;
  1672. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1673. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1674. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1675. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1676. WOLFSSL_FILETYPE_ASN1));
  1677. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1678. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1679. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1680. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1681. /* check satisfying .wolfssl.com constraint passes */
  1682. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1683. WOLFSSL_FILETYPE_PEM));
  1684. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1685. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1686. AssertNotNull(name = X509_NAME_new());
  1687. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1688. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1689. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1690. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1691. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1692. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1693. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1694. X509_NAME_free(name);
  1695. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1696. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1697. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  1698. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1699. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1700. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1701. wolfSSL_X509_free(x509);
  1702. /* check satisfying .random.com constraint passes */
  1703. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1704. WOLFSSL_FILETYPE_PEM));
  1705. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1706. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1707. AssertNotNull(name = X509_NAME_new());
  1708. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1709. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1710. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1711. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1712. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1713. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  1714. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1715. X509_NAME_free(name);
  1716. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  1717. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1718. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  1719. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1720. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1721. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1722. wolfSSL_X509_free(x509);
  1723. /* check fail case when neither constraint is matched */
  1724. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1725. WOLFSSL_FILETYPE_PEM));
  1726. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1727. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1728. AssertNotNull(name = X509_NAME_new());
  1729. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1730. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1731. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1732. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1733. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1734. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  1735. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1736. X509_NAME_free(name);
  1737. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1738. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1739. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1740. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1741. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1742. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1743. wolfSSL_CertManagerFree(cm);
  1744. wolfSSL_X509_free(x509);
  1745. wolfSSL_X509_free(ca);
  1746. wolfSSL_EVP_PKEY_free(priv);
  1747. #endif
  1748. return 0;
  1749. }
  1750. static int test_wolfSSL_CertManagerNameConstraint4(void)
  1751. {
  1752. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1753. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1754. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1755. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1756. !defined(NO_SHA256)
  1757. WOLFSSL_CERT_MANAGER* cm;
  1758. WOLFSSL_EVP_PKEY *priv;
  1759. WOLFSSL_X509_NAME* name;
  1760. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  1761. const char* server_cert = "./certs/test/server-goodcn.pem";
  1762. byte *der;
  1763. int derSz;
  1764. byte *pt;
  1765. WOLFSSL_X509 *x509, *ca;
  1766. pt = (byte*)server_key_der_2048;
  1767. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1768. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1769. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1770. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1771. WOLFSSL_FILETYPE_ASN1));
  1772. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1773. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1774. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1775. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1776. /* check satisfying wolfssl.com constraint passes */
  1777. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1778. WOLFSSL_FILETYPE_PEM));
  1779. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1780. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1781. AssertNotNull(name = X509_NAME_new());
  1782. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1783. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1784. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1785. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1786. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1787. X509_NAME_free(name);
  1788. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1789. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1790. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  1791. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1792. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1793. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1794. wolfSSL_X509_free(x509);
  1795. /* check satisfying example.com constraint passes */
  1796. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1797. WOLFSSL_FILETYPE_PEM));
  1798. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1799. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1800. AssertNotNull(name = X509_NAME_new());
  1801. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1802. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1803. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1804. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  1805. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1806. X509_NAME_free(name);
  1807. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  1808. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1809. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  1810. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1811. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1812. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1813. wolfSSL_X509_free(x509);
  1814. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  1815. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1816. WOLFSSL_FILETYPE_PEM));
  1817. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1818. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1819. AssertNotNull(name = X509_NAME_new());
  1820. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1821. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1822. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1823. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1824. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1825. X509_NAME_free(name);
  1826. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1827. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  1828. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  1829. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1830. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  1831. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1832. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1833. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1834. wolfSSL_X509_free(x509);
  1835. /* check fail when one DNS in the list is bad */
  1836. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1837. WOLFSSL_FILETYPE_PEM));
  1838. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1839. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1840. AssertNotNull(name = X509_NAME_new());
  1841. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1842. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1843. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1844. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1845. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1846. X509_NAME_free(name);
  1847. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1848. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  1849. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  1850. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1851. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  1852. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1853. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1854. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1855. wolfSSL_X509_free(x509);
  1856. /* check fail case when neither constraint is matched */
  1857. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1858. WOLFSSL_FILETYPE_PEM));
  1859. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1860. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1861. AssertNotNull(name = X509_NAME_new());
  1862. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1863. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1864. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1865. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  1866. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1867. X509_NAME_free(name);
  1868. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  1869. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1870. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1871. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1872. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1873. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1874. wolfSSL_CertManagerFree(cm);
  1875. wolfSSL_X509_free(x509);
  1876. wolfSSL_X509_free(ca);
  1877. wolfSSL_EVP_PKEY_free(priv);
  1878. #endif
  1879. return 0;
  1880. }
  1881. static int test_wolfSSL_CertManagerNameConstraint5(void)
  1882. {
  1883. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1884. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1885. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1886. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1887. !defined(NO_SHA256)
  1888. WOLFSSL_CERT_MANAGER* cm;
  1889. WOLFSSL_EVP_PKEY *priv;
  1890. WOLFSSL_X509_NAME* name;
  1891. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  1892. const char* server_cert = "./certs/test/server-goodcn.pem";
  1893. byte *der;
  1894. int derSz;
  1895. byte *pt;
  1896. WOLFSSL_X509 *x509, *ca;
  1897. pt = (byte*)server_key_der_2048;
  1898. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1899. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1900. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1901. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1902. WOLFSSL_FILETYPE_ASN1));
  1903. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1904. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1905. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1906. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1907. /* check satisfying wolfssl.com constraint passes */
  1908. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1909. WOLFSSL_FILETYPE_PEM));
  1910. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1911. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1912. AssertNotNull(name = X509_NAME_new());
  1913. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1914. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1915. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1916. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  1917. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1918. X509_NAME_free(name);
  1919. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  1920. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  1921. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1922. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1923. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1924. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1925. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1926. wolfSSL_X509_free(x509);
  1927. /* fail with DNS check because of common name */
  1928. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1929. WOLFSSL_FILETYPE_PEM));
  1930. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1931. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1932. AssertNotNull(name = X509_NAME_new());
  1933. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1934. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1935. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1936. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1937. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1938. X509_NAME_free(name);
  1939. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1940. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  1941. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1942. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  1943. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1944. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1945. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1946. wolfSSL_X509_free(x509);
  1947. /* fail on permitted DNS name constraint */
  1948. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1949. WOLFSSL_FILETYPE_PEM));
  1950. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1951. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1952. AssertNotNull(name = X509_NAME_new());
  1953. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1954. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1955. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1956. X509_NAME_free(name);
  1957. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  1958. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  1959. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  1960. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1961. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  1962. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1963. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1964. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1965. wolfSSL_X509_free(x509);
  1966. /* fail on permitted email name constraint */
  1967. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1968. WOLFSSL_FILETYPE_PEM));
  1969. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1970. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1971. AssertNotNull(name = X509_NAME_new());
  1972. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1973. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1974. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1975. X509_NAME_free(name);
  1976. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1977. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  1978. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  1979. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1980. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  1981. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1982. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1983. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1984. wolfSSL_X509_free(x509);
  1985. /* success with empty email name */
  1986. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1987. WOLFSSL_FILETYPE_PEM));
  1988. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1989. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1990. AssertNotNull(name = X509_NAME_new());
  1991. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1992. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1993. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1994. X509_NAME_free(name);
  1995. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1996. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1997. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  1998. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1999. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2000. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2001. wolfSSL_X509_free(x509);
  2002. wolfSSL_CertManagerFree(cm);
  2003. wolfSSL_X509_free(ca);
  2004. wolfSSL_EVP_PKEY_free(priv);
  2005. #endif
  2006. return 0;
  2007. }
  2008. static int test_wolfSSL_FPKI(void)
  2009. {
  2010. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2011. XFILE f;
  2012. const char* fpkiCert = "./certs/fpki-cert.der";
  2013. DecodedCert cert;
  2014. byte buf[4096];
  2015. byte* uuid;
  2016. byte* fascn;
  2017. word32 fascnSz;
  2018. word32 uuidSz;
  2019. int bytes;
  2020. printf(testingFmt, "test_wolfSSL_FPKI");
  2021. f = XFOPEN(fpkiCert, "rb");
  2022. AssertTrue((f != XBADFILE));
  2023. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2024. XFCLOSE(f);
  2025. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2026. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2027. AssertIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  2028. fascn = (byte*)XMALLOC(fascnSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2029. AssertNotNull(fascn);
  2030. AssertIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  2031. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2032. AssertIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  2033. uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2034. AssertNotNull(uuid);
  2035. AssertIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  2036. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2037. wc_FreeDecodedCert(&cert);
  2038. printf(resultFmt, passed);
  2039. #endif
  2040. return 0;
  2041. }
  2042. static int test_wolfSSL_CertRsaPss(void)
  2043. {
  2044. /* FIPS v2 and below don't support long salts. */
  2045. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  2046. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  2047. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  2048. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  2049. XFILE f;
  2050. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  2051. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  2052. #ifdef WOLFSSL_SHA384
  2053. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  2054. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  2055. #endif
  2056. DecodedCert cert;
  2057. byte buf[4096];
  2058. int bytes;
  2059. WOLFSSL_CERT_MANAGER* cm;
  2060. printf(testingFmt, "test_CertRsaPss");
  2061. cm = wolfSSL_CertManagerNew();
  2062. AssertNotNull(cm);
  2063. AssertIntEQ(WOLFSSL_SUCCESS,
  2064. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  2065. #ifdef WOLFSSL_SHA384
  2066. AssertIntEQ(WOLFSSL_SUCCESS,
  2067. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  2068. #endif
  2069. f = XFOPEN(rsaPssSha256Cert, "rb");
  2070. AssertTrue((f != XBADFILE));
  2071. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2072. XFCLOSE(f);
  2073. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2074. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2075. wc_FreeDecodedCert(&cert);
  2076. #ifdef WOLFSSL_SHA384
  2077. f = XFOPEN(rsaPssSha384Cert, "rb");
  2078. AssertTrue((f != XBADFILE));
  2079. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2080. XFCLOSE(f);
  2081. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2082. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2083. wc_FreeDecodedCert(&cert);
  2084. #endif
  2085. wolfSSL_CertManagerFree(cm);
  2086. printf(resultFmt, passed);
  2087. #endif
  2088. return 0;
  2089. }
  2090. static int test_wolfSSL_CertManagerCRL(void)
  2091. {
  2092. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2093. !defined(NO_RSA)
  2094. const char* ca_cert = "./certs/ca-cert.pem";
  2095. const char* crl1 = "./certs/crl/crl.pem";
  2096. const char* crl2 = "./certs/crl/crl2.pem";
  2097. WOLFSSL_CERT_MANAGER* cm = NULL;
  2098. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2099. AssertIntEQ(WOLFSSL_SUCCESS,
  2100. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2101. AssertIntEQ(WOLFSSL_SUCCESS,
  2102. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2103. AssertIntEQ(WOLFSSL_SUCCESS,
  2104. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2105. wolfSSL_CertManagerFreeCRL(cm);
  2106. AssertIntEQ(WOLFSSL_SUCCESS,
  2107. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2108. AssertIntEQ(WOLFSSL_SUCCESS,
  2109. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2110. wolfSSL_CertManagerFree(cm);
  2111. #endif
  2112. return 0;
  2113. }
  2114. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  2115. {
  2116. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2117. !defined(NO_WOLFSSL_CLIENT)
  2118. WOLFSSL_CTX* ctx;
  2119. const char* ca_cert = "./certs/ca-cert.pem";
  2120. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2121. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2122. AssertNotNull(ctx);
  2123. /* test good CA */
  2124. AssertTrue(WOLFSSL_SUCCESS ==
  2125. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  2126. WOLFSSL_LOAD_FLAG_NONE));
  2127. /* test expired CA */
  2128. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2129. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2130. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2131. #else
  2132. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2133. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2134. #endif
  2135. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2136. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2137. wolfSSL_CTX_free(ctx);
  2138. #endif
  2139. return 0;
  2140. }
  2141. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2142. {
  2143. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2144. defined(USE_CERT_BUFFERS_2048)
  2145. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  2146. WOLFSSL_CTX* ctx;
  2147. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2148. byte ca_expired_cert[TWOK_BUF];
  2149. word32 sizeof_ca_expired_cert;
  2150. XFILE fp;
  2151. #ifndef NO_WOLFSSL_CLIENT
  2152. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2153. #else
  2154. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2155. #endif
  2156. AssertNotNull(ctx);
  2157. /* test good CA */
  2158. AssertTrue(WOLFSSL_SUCCESS ==
  2159. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2160. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2161. WOLFSSL_LOAD_FLAG_NONE));
  2162. /* load expired CA */
  2163. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2164. fp = XFOPEN(ca_expired_cert_file, "rb");
  2165. AssertTrue(fp != XBADFILE);
  2166. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2167. sizeof(ca_expired_cert), fp);
  2168. XFCLOSE(fp);
  2169. /* test expired CA failure */
  2170. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2171. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2172. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2173. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2174. #else
  2175. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2176. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2177. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2178. #endif
  2179. /* test expired CA success */
  2180. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2181. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2182. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2183. wolfSSL_CTX_free(ctx);
  2184. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  2185. #endif
  2186. return 0;
  2187. }
  2188. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2189. {
  2190. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2191. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  2192. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2193. WOLFSSL_CTX* ctx;
  2194. #ifndef NO_WOLFSSL_CLIENT
  2195. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2196. #else
  2197. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2198. #endif
  2199. AssertTrue(WOLFSSL_SUCCESS ==
  2200. wolfSSL_CTX_load_verify_chain_buffer_format(ctx, ca_cert_chain_der,
  2201. sizeof_ca_cert_chain_der,
  2202. WOLFSSL_FILETYPE_ASN1));
  2203. wolfSSL_CTX_free(ctx);
  2204. #endif
  2205. return 0;
  2206. }
  2207. static int test_wolfSSL_CTX_add1_chain_cert(void)
  2208. {
  2209. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2210. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  2211. WOLFSSL_CTX* ctx;
  2212. WOLFSSL* ssl;
  2213. const char *certChain[] = {
  2214. "./certs/intermediate/client-int-cert.pem",
  2215. "./certs/intermediate/ca-int2-cert.pem",
  2216. "./certs/intermediate/ca-int-cert.pem",
  2217. "./certs/ca-cert.pem",
  2218. NULL
  2219. };
  2220. const char** cert;
  2221. WOLFSSL_X509* x509;
  2222. WOLF_STACK_OF(X509)* chain = NULL;
  2223. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2224. AssertNotNull(ssl = wolfSSL_new(ctx));
  2225. for (cert = certChain; *cert != NULL; cert++) {
  2226. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2227. AssertNotNull(x509);
  2228. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2229. X509_free(x509);
  2230. }
  2231. for (cert = certChain; *cert != NULL; cert++) {
  2232. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2233. AssertNotNull(x509);
  2234. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2235. X509_free(x509);
  2236. }
  2237. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2238. AssertIntEQ(sk_X509_num(chain), 3);
  2239. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2240. AssertIntEQ(sk_X509_num(chain), 3);
  2241. SSL_free(ssl);
  2242. SSL_CTX_free(ctx);
  2243. #endif
  2244. return 0;
  2245. }
  2246. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2247. {
  2248. int ret = 0;
  2249. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2250. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2251. const char* server_chain_der = "./certs/server-cert-chain.der";
  2252. const char* client_single_pem = "./certs/client-cert.pem";
  2253. WOLFSSL_CTX* ctx;
  2254. (void)server_chain_der;
  2255. (void)client_single_pem;
  2256. (void)ctx;
  2257. #ifndef NO_WOLFSSL_CLIENT
  2258. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2259. AssertNotNull(ctx);
  2260. #else
  2261. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2262. AssertNotNull(ctx);
  2263. #endif
  2264. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2265. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2266. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2267. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2268. wolfSSL_CTX_free(ctx);
  2269. #endif
  2270. return ret;
  2271. }
  2272. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  2273. {
  2274. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2275. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2276. WOLFSSL_CTX *ctx;
  2277. (void)ctx;
  2278. #ifndef NO_WOLFSSL_CLIENT
  2279. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2280. #else
  2281. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2282. #endif
  2283. /* invalid context */
  2284. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2285. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2286. /* invalid dhParamFile file */
  2287. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2288. NULL, WOLFSSL_FILETYPE_PEM));
  2289. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2290. bogusFile, WOLFSSL_FILETYPE_PEM));
  2291. /* success */
  2292. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2293. WOLFSSL_FILETYPE_PEM));
  2294. wolfSSL_CTX_free(ctx);
  2295. #endif
  2296. return 0;
  2297. }
  2298. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2299. {
  2300. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2301. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2302. WOLFSSL_CTX *ctx;
  2303. (void)ctx;
  2304. #ifndef NO_WOLFSSL_CLIENT
  2305. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2306. #else
  2307. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2308. #endif
  2309. /* invalid context */
  2310. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2311. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2312. /* invalid dhParamFile file */
  2313. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2314. 0, WOLFSSL_FILETYPE_ASN1));
  2315. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2316. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2317. /* success */
  2318. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2319. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2320. wolfSSL_CTX_free(ctx);
  2321. #endif
  2322. return 0;
  2323. }
  2324. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2325. {
  2326. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2327. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2328. WOLFSSL_CTX *ctx;
  2329. (void)ctx;
  2330. #ifndef NO_WOLFSSL_CLIENT
  2331. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2332. AssertNotNull(ctx);
  2333. #else
  2334. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2335. AssertNotNull(ctx);
  2336. #endif
  2337. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2338. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2339. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2340. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2341. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2342. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2343. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2344. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2345. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2346. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2347. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2348. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2349. wolfSSL_CTX_free(ctx);
  2350. #endif
  2351. return 0;
  2352. }
  2353. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  2354. {
  2355. #if defined(WOLFSSL_DER_LOAD) && \
  2356. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2357. WOLFSSL_CTX* ctx = NULL;
  2358. const char* derCert = "./certs/server-cert.der";
  2359. const char* nullPath = NULL;
  2360. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2361. const char* emptyPath = "";
  2362. /* der load Case 1 ctx NULL */
  2363. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2364. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2365. #ifndef NO_WOLFSSL_CLIENT
  2366. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2367. #else
  2368. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2369. #endif
  2370. /* Case 2 filePath NULL */
  2371. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2372. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2373. /* Case 3 invalid format */
  2374. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2375. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2376. /* Case 4 filePath not valid */
  2377. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2378. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2379. /* Case 5 filePath empty */
  2380. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2381. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2382. #ifndef NO_RSA
  2383. /* Case 6 success case */
  2384. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2385. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2386. #endif
  2387. wolfSSL_CTX_free(ctx);
  2388. #endif
  2389. return 0;
  2390. }
  2391. static int test_wolfSSL_CTX_enable_disable(void)
  2392. {
  2393. #ifndef NO_CERTS
  2394. WOLFSSL_CTX* ctx = NULL;
  2395. #ifdef HAVE_CRL
  2396. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2397. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2398. #endif
  2399. #ifdef HAVE_OCSP
  2400. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2401. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2402. #endif
  2403. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2404. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2405. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  2406. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  2407. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2408. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2409. #endif
  2410. #ifndef NO_WOLFSSL_CLIENT
  2411. #ifdef HAVE_EXTENDED_MASTER
  2412. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  2413. #endif
  2414. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2415. AssertNotNull(ctx);
  2416. #ifdef HAVE_EXTENDED_MASTER
  2417. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  2418. #endif
  2419. #elif !defined(NO_WOLFSSL_SERVER)
  2420. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2421. #else
  2422. return 0;
  2423. #endif
  2424. #ifdef HAVE_CRL
  2425. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  2426. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  2427. #endif
  2428. #ifdef HAVE_OCSP
  2429. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  2430. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  2431. WOLFSSL_SUCCESS);
  2432. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  2433. WOLFSSL_SUCCESS);
  2434. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  2435. WOLFSSL_SUCCESS);
  2436. #endif
  2437. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2438. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2439. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2440. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2441. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2442. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2443. #endif
  2444. wolfSSL_CTX_free(ctx);
  2445. #endif /* NO_CERTS */
  2446. return 0;
  2447. }
  2448. static int test_wolfSSL_CTX_ticket_API(void)
  2449. {
  2450. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  2451. WOLFSSL_CTX* ctx = NULL;
  2452. void *userCtx = (void*)"this is my ctx";
  2453. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2454. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  2455. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  2456. wolfSSL_CTX_free(ctx);
  2457. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  2458. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  2459. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  2460. return 0;
  2461. }
  2462. static int test_wolfSSL_set_minmax_proto_version(void)
  2463. {
  2464. #ifdef OPENSSL_EXTRA
  2465. WOLFSSL_CTX *ctx;
  2466. WOLFSSL *ssl;
  2467. int ret;
  2468. (void)ret;
  2469. (void)ssl;
  2470. printf(testingFmt, "test_wolfSSL_set_minmax_proto_version");
  2471. #ifndef NO_WOLFSSL_CLIENT
  2472. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2473. AssertNotNull(ssl = wolfSSL_new(ctx));
  2474. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2475. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2476. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2477. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2478. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2479. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  2480. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2481. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  2482. wolfSSL_free(ssl);
  2483. wolfSSL_CTX_free(ctx);
  2484. #endif
  2485. #ifndef NO_WOLFSSL_SERVER
  2486. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2487. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2488. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2489. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2490. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2491. wolfSSL_CTX_free(ctx);
  2492. #endif
  2493. printf(resultFmt, passed);
  2494. #endif
  2495. return 0;
  2496. }
  2497. /*----------------------------------------------------------------------------*
  2498. | SSL
  2499. *----------------------------------------------------------------------------*/
  2500. static int test_server_wolfSSL_new(void)
  2501. {
  2502. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2503. !defined(NO_WOLFSSL_SERVER)
  2504. WOLFSSL_CTX *ctx;
  2505. WOLFSSL_CTX *ctx_nocert;
  2506. WOLFSSL *ssl;
  2507. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2508. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2509. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  2510. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  2511. /* invalid context */
  2512. AssertNull(ssl = wolfSSL_new(NULL));
  2513. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  2514. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  2515. #endif
  2516. /* success */
  2517. AssertNotNull(ssl = wolfSSL_new(ctx));
  2518. wolfSSL_free(ssl);
  2519. wolfSSL_CTX_free(ctx);
  2520. wolfSSL_CTX_free(ctx_nocert);
  2521. #endif
  2522. return 0;
  2523. }
  2524. static int test_client_wolfSSL_new(void)
  2525. {
  2526. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2527. !defined(NO_WOLFSSL_CLIENT)
  2528. WOLFSSL_CTX *ctx;
  2529. WOLFSSL_CTX *ctx_nocert;
  2530. WOLFSSL *ssl;
  2531. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2532. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2533. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  2534. /* invalid context */
  2535. AssertNull(ssl = wolfSSL_new(NULL));
  2536. /* success */
  2537. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  2538. wolfSSL_free(ssl);
  2539. /* success */
  2540. AssertNotNull(ssl = wolfSSL_new(ctx));
  2541. wolfSSL_free(ssl);
  2542. wolfSSL_CTX_free(ctx);
  2543. wolfSSL_CTX_free(ctx_nocert);
  2544. #endif
  2545. return 0;
  2546. }
  2547. static int test_wolfSSL_SetTmpDH_file(void)
  2548. {
  2549. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2550. !defined(NO_WOLFSSL_SERVER)
  2551. WOLFSSL_CTX *ctx;
  2552. WOLFSSL *ssl;
  2553. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2554. #ifndef NO_RSA
  2555. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2556. WOLFSSL_FILETYPE_PEM));
  2557. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2558. WOLFSSL_FILETYPE_PEM));
  2559. #elif defined(HAVE_ECC)
  2560. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  2561. WOLFSSL_FILETYPE_PEM));
  2562. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  2563. WOLFSSL_FILETYPE_PEM));
  2564. #elif defined(HAVE_ED25519)
  2565. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  2566. WOLFSSL_FILETYPE_PEM));
  2567. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  2568. WOLFSSL_FILETYPE_PEM));
  2569. #elif defined(HAVE_ED448)
  2570. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  2571. WOLFSSL_FILETYPE_PEM));
  2572. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  2573. WOLFSSL_FILETYPE_PEM));
  2574. #endif
  2575. AssertNotNull(ssl = wolfSSL_new(ctx));
  2576. /* invalid ssl */
  2577. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  2578. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2579. /* invalid dhParamFile file */
  2580. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2581. NULL, WOLFSSL_FILETYPE_PEM));
  2582. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2583. bogusFile, WOLFSSL_FILETYPE_PEM));
  2584. /* success */
  2585. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  2586. WOLFSSL_FILETYPE_PEM));
  2587. wolfSSL_free(ssl);
  2588. wolfSSL_CTX_free(ctx);
  2589. #endif
  2590. return 0;
  2591. }
  2592. static int test_wolfSSL_SetTmpDH_buffer(void)
  2593. {
  2594. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  2595. WOLFSSL_CTX *ctx;
  2596. WOLFSSL *ssl;
  2597. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2598. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  2599. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2600. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2601. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2602. AssertNotNull(ssl = wolfSSL_new(ctx));
  2603. /* invalid ssl */
  2604. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2605. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2606. /* invalid dhParamFile file */
  2607. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  2608. 0, WOLFSSL_FILETYPE_ASN1));
  2609. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  2610. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2611. /* success */
  2612. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2613. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2614. wolfSSL_free(ssl);
  2615. wolfSSL_CTX_free(ctx);
  2616. #endif
  2617. return 0;
  2618. }
  2619. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  2620. {
  2621. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  2622. WOLFSSL_CTX *ctx, *ctx2;
  2623. WOLFSSL *ssl, *ssl2;
  2624. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2625. AssertNotNull(ctx);
  2626. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  2627. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2628. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2629. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2630. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2631. ssl = wolfSSL_new(ctx);
  2632. AssertNotNull(ssl);
  2633. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2634. AssertNotNull(ctx2);
  2635. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  2636. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2637. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  2638. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2639. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2640. ssl2 = wolfSSL_new(ctx2);
  2641. AssertNotNull(ssl2);
  2642. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2643. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2644. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  2645. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2646. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2647. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  2648. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2649. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2650. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  2651. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2652. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  2653. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  2654. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2655. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  2656. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2657. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2658. wolfSSL_free(ssl2);
  2659. wolfSSL_CTX_free(ctx2);
  2660. wolfSSL_free(ssl);
  2661. wolfSSL_CTX_free(ctx);
  2662. #endif
  2663. return 0;
  2664. }
  2665. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  2666. * allowed.
  2667. * POST: return 1 on success.
  2668. */
  2669. static int test_wolfSSL_SetMinVersion(void)
  2670. {
  2671. int failFlag = WOLFSSL_SUCCESS;
  2672. #ifndef NO_WOLFSSL_CLIENT
  2673. WOLFSSL_CTX* ctx;
  2674. WOLFSSL* ssl;
  2675. int itr;
  2676. #ifndef NO_OLD_TLS
  2677. const int versions[] = {
  2678. #ifdef WOLFSSL_ALLOW_TLSV10
  2679. WOLFSSL_TLSV1,
  2680. #endif
  2681. WOLFSSL_TLSV1_1,
  2682. WOLFSSL_TLSV1_2};
  2683. #elif !defined(WOLFSSL_NO_TLS12)
  2684. const int versions[] = { WOLFSSL_TLSV1_2 };
  2685. #else
  2686. const int versions[] = { WOLFSSL_TLSV1_3 };
  2687. #endif
  2688. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2689. ssl = wolfSSL_new(ctx);
  2690. printf(testingFmt, "wolfSSL_SetMinVersion()");
  2691. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  2692. if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
  2693. failFlag = WOLFSSL_FAILURE;
  2694. }
  2695. }
  2696. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  2697. wolfSSL_free(ssl);
  2698. wolfSSL_CTX_free(ctx);
  2699. #endif
  2700. if (failFlag == WOLFSSL_SUCCESS) {
  2701. failFlag = 0;
  2702. }
  2703. return failFlag;
  2704. } /* END test_wolfSSL_SetMinVersion */
  2705. /*----------------------------------------------------------------------------*
  2706. | EC
  2707. *----------------------------------------------------------------------------*/
  2708. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  2709. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  2710. */
  2711. #ifdef OPENSSL_EXTRA
  2712. static int test_wolfSSL_EC(void)
  2713. {
  2714. #if !defined(WOLFSSL_SP_MATH) && \
  2715. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  2716. #if defined(HAVE_ECC)
  2717. BN_CTX *ctx;
  2718. EC_GROUP *group;
  2719. EC_GROUP *group2;
  2720. EC_POINT *Gxy, *new_point, *set_point;
  2721. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  2722. BIGNUM *X, *Y;
  2723. BIGNUM *set_point_bn;
  2724. char* hexStr;
  2725. int group_bits;
  2726. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  2727. /* NISTP256R1 Gx/Gy */
  2728. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2729. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2730. #ifndef HAVE_SELFTEST
  2731. EC_POINT *tmp;
  2732. size_t bin_len;
  2733. unsigned char* buf = NULL;
  2734. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2735. const unsigned char binUncompG[] = {
  2736. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2737. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2738. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2739. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  2740. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  2741. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  2742. };
  2743. #ifdef HAVE_COMP_KEY
  2744. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2745. const unsigned char binCompG[] = {
  2746. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2747. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2748. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2749. };
  2750. #endif
  2751. #endif
  2752. AssertNotNull(ctx = BN_CTX_new());
  2753. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  2754. AssertNotNull(group2 = EC_GROUP_dup(group));
  2755. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  2756. AssertNotNull(Gxy = EC_POINT_new(group));
  2757. AssertNotNull(new_point = EC_POINT_new(group));
  2758. AssertNotNull(set_point = EC_POINT_new(group));
  2759. AssertNotNull(X = BN_new());
  2760. AssertNotNull(Y = BN_new());
  2761. AssertNotNull(set_point_bn = BN_new());
  2762. /* load test values */
  2763. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  2764. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  2765. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  2766. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  2767. /* populate coordinates for input point */
  2768. Gxy->X = Gx;
  2769. Gxy->Y = Gy;
  2770. Gxy->Z = Gz;
  2771. #ifndef HAVE_SELFTEST
  2772. /* perform point multiplication */
  2773. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
  2774. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
  2775. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2776. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2777. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2778. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  2779. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2780. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2781. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2782. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
  2783. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2784. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2785. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2786. #else
  2787. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  2788. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2789. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2790. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2791. #endif
  2792. /* check if point X coordinate is zero */
  2793. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2794. #ifdef USE_ECC_B_PARAM
  2795. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  2796. #endif /* USE_ECC_B_PARAM */
  2797. /* Force non-affine coordinates */
  2798. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2799. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  2800. new_point->inSet = 0;
  2801. /* extract the coordinates from point */
  2802. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2803. /* check if point X coordinate is zero */
  2804. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  2805. /* set the same X and Y points in another object */
  2806. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2807. /* compare points as they should be the same */
  2808. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  2809. /* Test copying */
  2810. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  2811. /* Test inverting */
  2812. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  2813. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  2814. set_point_bn, ctx), set_point_bn);
  2815. /* check bn2hex */
  2816. hexStr = BN_bn2hex(k);
  2817. AssertStrEQ(hexStr, kTest);
  2818. #ifndef NO_FILESYSTEM
  2819. BN_print_fp(stdout, k);
  2820. printf("\n");
  2821. #endif
  2822. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2823. hexStr = BN_bn2hex(Gx);
  2824. AssertStrEQ(hexStr, kGx);
  2825. #ifndef NO_FILESYSTEM
  2826. BN_print_fp(stdout, Gx);
  2827. printf("\n");
  2828. #endif
  2829. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2830. hexStr = BN_bn2hex(Gy);
  2831. AssertStrEQ(hexStr, kGy);
  2832. #ifndef NO_FILESYSTEM
  2833. BN_print_fp(stdout, Gy);
  2834. printf("\n");
  2835. #endif
  2836. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2837. #ifndef HAVE_SELFTEST
  2838. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  2839. AssertStrEQ(hexStr, uncompG);
  2840. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2841. #ifdef HAVE_COMP_KEY
  2842. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  2843. AssertStrEQ(hexStr, compG);
  2844. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2845. #endif
  2846. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  2847. AssertIntEQ(bin_len, sizeof(binUncompG));
  2848. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2849. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  2850. bin_len, ctx), bin_len);
  2851. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  2852. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2853. #ifdef HAVE_COMP_KEY
  2854. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  2855. AssertIntEQ(bin_len, sizeof(binCompG));
  2856. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2857. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  2858. bin_len, ctx), bin_len);
  2859. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  2860. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2861. #endif
  2862. AssertNotNull(tmp = EC_POINT_new(group));
  2863. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  2864. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2865. EC_POINT_free(tmp);
  2866. #ifdef HAVE_COMP_KEY
  2867. AssertNotNull(tmp = EC_POINT_new(group));
  2868. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  2869. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2870. EC_POINT_free(tmp);
  2871. #endif
  2872. #endif
  2873. /* test BN_mod_add */
  2874. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2875. (WOLFSSL_BIGNUM*)BN_value_one(),
  2876. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  2877. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  2878. /* cleanup */
  2879. BN_free(X);
  2880. BN_free(Y);
  2881. BN_free(k);
  2882. BN_free(set_point_bn);
  2883. EC_POINT_free(new_point);
  2884. EC_POINT_free(set_point);
  2885. EC_POINT_free(Gxy);
  2886. EC_GROUP_free(group);
  2887. EC_GROUP_free(group2);
  2888. BN_CTX_free(ctx);
  2889. #endif /* HAVE_ECC */
  2890. #endif /* OPENSSL_EXTRA && !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  2891. return 0;
  2892. }
  2893. #endif /* OPENSSL_EXTRA */
  2894. #ifndef NO_BIO
  2895. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  2896. {
  2897. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  2898. EC_GROUP *group;
  2899. BIO* bio;
  2900. AssertNotNull(bio = BIO_new(BIO_s_file()));
  2901. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  2902. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  2903. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  2904. EC_GROUP_free(group);
  2905. BIO_free(bio);
  2906. #endif /* HAVE_ECC */
  2907. return 0;
  2908. }
  2909. #endif /* !NO_BIO */
  2910. # if defined(OPENSSL_EXTRA)
  2911. static int test_wolfSSL_ECDSA_SIG(void)
  2912. {
  2913. #ifdef HAVE_ECC
  2914. WOLFSSL_ECDSA_SIG* sig = NULL;
  2915. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  2916. const unsigned char* cp;
  2917. unsigned char* p;
  2918. unsigned char outSig[8];
  2919. unsigned char sigData[8] =
  2920. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  2921. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  2922. AssertNull(sig);
  2923. cp = sigData;
  2924. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  2925. AssertIntEQ((cp == sigData + 8), 1);
  2926. cp = sigData;
  2927. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  2928. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  2929. AssertIntEQ((sig == sig2), 1);
  2930. cp = outSig;
  2931. p = outSig;
  2932. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  2933. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  2934. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  2935. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  2936. AssertIntEQ((p == outSig + 8), 1);
  2937. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  2938. wolfSSL_ECDSA_SIG_free(sig);
  2939. #endif /* HAVE_ECC */
  2940. return 0;
  2941. }
  2942. static int test_EC_i2d(void)
  2943. {
  2944. #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
  2945. EC_KEY *key;
  2946. EC_KEY *copy;
  2947. int len;
  2948. unsigned char *buf = NULL;
  2949. const unsigned char *tmp = NULL;
  2950. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2951. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2952. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  2953. AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
  2954. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2955. buf = NULL;
  2956. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  2957. AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
  2958. tmp = buf;
  2959. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  2960. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2961. buf = NULL;
  2962. AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
  2963. tmp = buf;
  2964. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  2965. AssertIntEQ(EC_KEY_check_key(key), 1);
  2966. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  2967. EC_KEY_free(key);
  2968. EC_KEY_free(copy);
  2969. #endif /* HAVE_ECC */
  2970. return 0;
  2971. }
  2972. static int test_ECDSA_size_sign(void)
  2973. {
  2974. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  2975. EC_KEY *key;
  2976. int id;
  2977. byte hash[WC_MAX_DIGEST_SIZE];
  2978. byte sig[ECC_MAX_SIG_SIZE];
  2979. unsigned int sigSz = sizeof(sig);
  2980. XMEMSET(hash, 123, sizeof(hash));
  2981. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  2982. AssertIntEQ(id, ECC_SECP256R1);
  2983. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2984. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2985. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  2986. AssertIntGE(ECDSA_size(key), sigSz);
  2987. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  2988. EC_KEY_free(key);
  2989. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  2990. return 0;
  2991. }
  2992. static int test_ED25519(void)
  2993. {
  2994. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  2995. defined(WOLFSSL_KEY_GEN)
  2996. byte priv[ED25519_PRV_KEY_SIZE];
  2997. unsigned int privSz = (unsigned int)sizeof(priv);
  2998. byte pub[ED25519_PUB_KEY_SIZE];
  2999. unsigned int pubSz = (unsigned int)sizeof(pub);
  3000. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3001. const char* msg = TEST_STRING;
  3002. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3003. byte sig[ED25519_SIG_SIZE];
  3004. unsigned int sigSz = (unsigned int)sizeof(sig);
  3005. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3006. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3007. WOLFSSL_SUCCESS);
  3008. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3009. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3010. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3011. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3012. &sigSz), WOLFSSL_SUCCESS);
  3013. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  3014. #ifdef HAVE_ED25519_VERIFY
  3015. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3016. sigSz), WOLFSSL_SUCCESS);
  3017. #endif /* HAVE_ED25519_VERIFY */
  3018. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3019. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3020. return 0;
  3021. }
  3022. static int test_ED448(void)
  3023. {
  3024. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3025. defined(WOLFSSL_KEY_GEN)
  3026. byte priv[ED448_PRV_KEY_SIZE];
  3027. unsigned int privSz = (unsigned int)sizeof(priv);
  3028. byte pub[ED448_PUB_KEY_SIZE];
  3029. unsigned int pubSz = (unsigned int)sizeof(pub);
  3030. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3031. const char* msg = TEST_STRING;
  3032. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3033. byte sig[ED448_SIG_SIZE];
  3034. unsigned int sigSz = (unsigned int)sizeof(sig);
  3035. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3036. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3037. WOLFSSL_SUCCESS);
  3038. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3039. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3040. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3041. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3042. &sigSz), WOLFSSL_SUCCESS);
  3043. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  3044. #ifdef HAVE_ED448_VERIFY
  3045. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3046. sigSz), WOLFSSL_SUCCESS);
  3047. #endif /* HAVE_ED448_VERIFY */
  3048. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3049. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3050. return 0;
  3051. }
  3052. #endif /* OPENSSL_EXTRA */
  3053. #include <wolfssl/openssl/pem.h>
  3054. /*----------------------------------------------------------------------------*
  3055. | EVP
  3056. *----------------------------------------------------------------------------*/
  3057. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3058. {
  3059. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3060. WOLFSSL_BIO* rbio = NULL;
  3061. WOLFSSL_BIO* wbio = NULL;
  3062. WOLFSSL_EVP_PKEY* pkey = NULL;
  3063. char line[256] = { 0 };
  3064. char line1[256] = { 0 };
  3065. int i;
  3066. printf(testingFmt, "EVP_PKEY_print_public()");
  3067. /* test error cases */
  3068. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3069. /*
  3070. * test RSA public key print
  3071. * in this test, pass '3' for indent
  3072. */
  3073. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3074. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3075. sizeof_client_keypub_der_1024);
  3076. AssertNotNull(rbio);
  3077. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3078. AssertNotNull(pkey);
  3079. wbio = BIO_new(BIO_s_mem());
  3080. AssertNotNull(wbio);
  3081. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3082. BIO_gets(wbio, line, sizeof(line));
  3083. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3084. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3085. BIO_gets(wbio, line, sizeof(line));
  3086. strcpy(line1, " Modulus:\n");
  3087. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3088. BIO_gets(wbio, line, sizeof(line));
  3089. strcpy(line1, " 00:BC:73:0E:A8:49:F3:74:A2:A9:EF:18:A5:DA:55:\n");
  3090. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3091. /* skip to the end of modulus element*/
  3092. for( i = 0; i < 8 ;i++) {
  3093. BIO_gets(wbio, line, sizeof(line));
  3094. }
  3095. BIO_gets(wbio, line, sizeof(line));
  3096. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3097. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3098. /* should reach EOF */
  3099. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3100. EVP_PKEY_free(pkey);
  3101. pkey = NULL;
  3102. BIO_free(rbio);
  3103. BIO_free(wbio);
  3104. rbio = NULL;
  3105. wbio = NULL;
  3106. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3107. /*
  3108. * test DSA public key print
  3109. */
  3110. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3111. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3112. sizeof_dsa_pub_key_der_2048);
  3113. AssertNotNull(rbio);
  3114. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3115. AssertNotNull(pkey);
  3116. wbio = BIO_new(BIO_s_mem());
  3117. AssertNotNull(wbio);
  3118. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3119. BIO_gets(wbio, line, sizeof(line));
  3120. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3121. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3122. BIO_gets(wbio, line, sizeof(line));
  3123. strcpy(line1, "pub:\n");
  3124. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3125. BIO_gets(wbio, line, sizeof(line));
  3126. strcpy(line1,
  3127. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3128. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3129. /* skip to the end of pub element*/
  3130. for( i = 0; i < 17 ;i++) {
  3131. BIO_gets(wbio, line, sizeof(line));
  3132. }
  3133. BIO_gets(wbio, line, sizeof(line));
  3134. strcpy(line1, "P:\n");
  3135. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3136. /* skip to the end of P element*/
  3137. for( i = 0; i < 18 ;i++) {
  3138. BIO_gets(wbio, line, sizeof(line));
  3139. }
  3140. BIO_gets(wbio, line, sizeof(line));
  3141. strcpy(line1, "Q:\n");
  3142. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3143. /* skip to the end of Q element*/
  3144. for( i = 0; i < 3 ;i++) {
  3145. BIO_gets(wbio, line, sizeof(line));
  3146. }
  3147. BIO_gets(wbio, line, sizeof(line));
  3148. strcpy(line1, "G:\n");
  3149. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3150. /* skip to the end of G element*/
  3151. for( i = 0; i < 18 ;i++) {
  3152. BIO_gets(wbio, line, sizeof(line));
  3153. }
  3154. /* should reach EOF */
  3155. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3156. EVP_PKEY_free(pkey);
  3157. pkey = NULL;
  3158. BIO_free(rbio);
  3159. BIO_free(wbio);
  3160. rbio = NULL;
  3161. wbio = NULL;
  3162. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  3163. /*
  3164. * test ECC public key print
  3165. */
  3166. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  3167. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  3168. sizeof_ecc_clikeypub_der_256);
  3169. AssertNotNull(rbio);
  3170. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3171. AssertNotNull(pkey);
  3172. wbio = BIO_new(BIO_s_mem());
  3173. AssertNotNull(wbio);
  3174. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3175. BIO_gets(wbio, line, sizeof(line));
  3176. strcpy(line1, "Public-Key: (256 bit)\n");
  3177. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3178. BIO_gets(wbio, line, sizeof(line));
  3179. strcpy(line1, "pub:\n");
  3180. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3181. BIO_gets(wbio, line, sizeof(line));
  3182. strcpy(line1,
  3183. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3184. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3185. /* skip to the end of pub element*/
  3186. for( i = 0; i < 4 ;i++) {
  3187. BIO_gets(wbio, line, sizeof(line));
  3188. }
  3189. BIO_gets(wbio, line, sizeof(line));
  3190. strcpy(line1, "ASN1 OID: prime256v1\n");
  3191. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3192. BIO_gets(wbio, line, sizeof(line));
  3193. strcpy(line1, "NIST CURVE: P-256\n");
  3194. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3195. /* should reach EOF */
  3196. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3197. EVP_PKEY_free(pkey);
  3198. pkey = NULL;
  3199. BIO_free(rbio);
  3200. BIO_free(wbio);
  3201. rbio = NULL;
  3202. wbio = NULL;
  3203. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3204. /*
  3205. * test DH public key print
  3206. */
  3207. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3208. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3209. sizeof_dh_pub_key_der_2048);
  3210. AssertNotNull(rbio);
  3211. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3212. AssertNotNull(pkey);
  3213. wbio = BIO_new(BIO_s_mem());
  3214. AssertNotNull(wbio);
  3215. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3216. BIO_gets(wbio, line, sizeof(line));
  3217. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3218. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3219. BIO_gets(wbio, line, sizeof(line));
  3220. strcpy(line1, "public-key:\n");
  3221. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3222. BIO_gets(wbio, line, sizeof(line));
  3223. strcpy(line1,
  3224. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3225. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3226. /* skip to the end of public-key element*/
  3227. for( i = 0; i < 17 ;i++) {
  3228. BIO_gets(wbio, line, sizeof(line));
  3229. }
  3230. BIO_gets(wbio, line, sizeof(line));
  3231. strcpy(line1, "prime:\n");
  3232. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3233. BIO_gets(wbio, line, sizeof(line));
  3234. strcpy(line1,
  3235. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3236. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3237. /* skip to the end of prime element*/
  3238. for( i = 0; i < 17 ;i++) {
  3239. BIO_gets(wbio, line, sizeof(line));
  3240. }
  3241. BIO_gets(wbio, line, sizeof(line));
  3242. strcpy(line1, "generator: 2 (0x02)\n");
  3243. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3244. /* should reach EOF */
  3245. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3246. EVP_PKEY_free(pkey);
  3247. pkey = NULL;
  3248. BIO_free(rbio);
  3249. BIO_free(wbio);
  3250. rbio = NULL;
  3251. wbio = NULL;
  3252. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3253. /* to prevent "unused variable" warning */
  3254. (void)pkey;
  3255. (void)wbio;
  3256. (void)rbio;
  3257. (void)line;
  3258. (void)line1;
  3259. (void)i;
  3260. printf(resultFmt, passed);
  3261. #endif /* OPENSSL_EXTRA */
  3262. return 0;
  3263. }
  3264. /* Test functions for base64 encode/decode */
  3265. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3266. {
  3267. #if defined(OPENSSL_EXTRA) && \
  3268. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3269. EVP_ENCODE_CTX* ctx = NULL;
  3270. printf(testingFmt, "EVP_ENCODE_CTX_new()");
  3271. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3272. AssertIntEQ( ctx->remaining,0);
  3273. AssertIntEQ( ctx->data[0],0);
  3274. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3275. EVP_ENCODE_CTX_free(ctx);
  3276. printf(resultFmt, passed);
  3277. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3278. return 0;
  3279. }
  3280. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3281. {
  3282. #if defined(OPENSSL_EXTRA) && \
  3283. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3284. EVP_ENCODE_CTX* ctx = NULL;
  3285. printf(testingFmt, "EVP_ENCODE_CTX_free()");
  3286. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3287. EVP_ENCODE_CTX_free(ctx);
  3288. printf(resultFmt, passed);
  3289. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3290. return 0;
  3291. }
  3292. static int test_wolfSSL_EVP_EncodeInit(void)
  3293. {
  3294. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3295. EVP_ENCODE_CTX* ctx = NULL;
  3296. printf(testingFmt, "EVP_EncodeInit()");
  3297. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3298. AssertIntEQ( ctx->remaining,0);
  3299. AssertIntEQ( ctx->data[0],0);
  3300. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3301. /* make ctx dirty */
  3302. ctx->remaining = 10;
  3303. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3304. EVP_EncodeInit(ctx);
  3305. AssertIntEQ( ctx->remaining,0);
  3306. AssertIntEQ( ctx->data[0],0);
  3307. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3308. EVP_ENCODE_CTX_free(ctx);
  3309. printf(resultFmt, passed);
  3310. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3311. return 0;
  3312. }
  3313. static int test_wolfSSL_EVP_EncodeUpdate(void)
  3314. {
  3315. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3316. int outl;
  3317. int total;
  3318. const unsigned char plain0[] = {"Th"};
  3319. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3320. const unsigned char plain2[] = {"This is additional data."};
  3321. const unsigned char enc0[] = {"VGg=\n"};
  3322. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3323. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3324. const unsigned char enc2[] =
  3325. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3326. unsigned char encOutBuff[300];
  3327. EVP_ENCODE_CTX* ctx = NULL;
  3328. printf(testingFmt, "EVP_EncodeUpdate()");
  3329. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3330. EVP_EncodeInit(ctx);
  3331. /* illegal parameter test */
  3332. AssertIntEQ(
  3333. EVP_EncodeUpdate(
  3334. NULL, /* pass NULL as ctx */
  3335. encOutBuff,
  3336. &outl,
  3337. plain1,
  3338. sizeof(plain1)-1),
  3339. 0 /* expected result code 0: fail */
  3340. );
  3341. AssertIntEQ(
  3342. EVP_EncodeUpdate(
  3343. ctx,
  3344. NULL, /* pass NULL as out buff */
  3345. &outl,
  3346. plain1,
  3347. sizeof(plain1)-1),
  3348. 0 /* expected result code 0: fail */
  3349. );
  3350. AssertIntEQ(
  3351. EVP_EncodeUpdate(
  3352. ctx,
  3353. encOutBuff,
  3354. NULL, /* pass NULL as outl */
  3355. plain1,
  3356. sizeof(plain1)-1),
  3357. 0 /* expected result code 0: fail */
  3358. );
  3359. AssertIntEQ(
  3360. EVP_EncodeUpdate(
  3361. ctx,
  3362. encOutBuff,
  3363. &outl,
  3364. NULL, /* pass NULL as in */
  3365. sizeof(plain1)-1),
  3366. 0 /* expected result code 0: fail */
  3367. );
  3368. AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3369. /* meaningless parameter test */
  3370. AssertIntEQ(
  3371. EVP_EncodeUpdate(
  3372. ctx,
  3373. encOutBuff,
  3374. &outl,
  3375. plain1,
  3376. 0), /* pass zero input */
  3377. 1 /* expected result code 1: success */
  3378. );
  3379. /* very small data encoding test */
  3380. EVP_EncodeInit(ctx);
  3381. AssertIntEQ(
  3382. EVP_EncodeUpdate(
  3383. ctx,
  3384. encOutBuff,
  3385. &outl,
  3386. plain0,
  3387. sizeof(plain0)-1),
  3388. 1 /* expected result code 1: success */
  3389. );
  3390. AssertIntEQ(outl,0);
  3391. EVP_EncodeFinal(
  3392. ctx,
  3393. encOutBuff + outl,
  3394. &outl);
  3395. AssertIntEQ( outl, sizeof(enc0)-1);
  3396. AssertIntEQ(
  3397. XSTRNCMP(
  3398. (const char*)encOutBuff,
  3399. (const char*)enc0,sizeof(enc0) ),
  3400. 0);
  3401. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3402. AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3403. sizeof(enc0)-1);
  3404. AssertIntEQ(
  3405. XSTRNCMP(
  3406. (const char*)encOutBuff,
  3407. (const char*)enc0,sizeof(enc0) ),
  3408. 0);
  3409. /* pass small size( < 48bytes ) input, then make sure they are not
  3410. * encoded and just stored in ctx
  3411. */
  3412. EVP_EncodeInit(ctx);
  3413. total = 0;
  3414. outl = 0;
  3415. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3416. AssertIntEQ(
  3417. EVP_EncodeUpdate(
  3418. ctx,
  3419. encOutBuff, /* buffer for output */
  3420. &outl, /* size of output */
  3421. plain1, /* input */
  3422. sizeof(plain1)-1), /* size of input */
  3423. 1); /* expected result code 1:success */
  3424. total += outl;
  3425. AssertIntEQ(outl, 0); /* no output expected */
  3426. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3427. AssertTrue(
  3428. XSTRNCMP((const char*)(ctx->data),
  3429. (const char*)plain1,
  3430. ctx->remaining) ==0 );
  3431. AssertTrue(encOutBuff[0] == 0);
  3432. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3433. * the stored data and the new input together
  3434. */
  3435. AssertIntEQ(
  3436. EVP_EncodeUpdate(
  3437. ctx,
  3438. encOutBuff + outl, /* buffer for output */
  3439. &outl, /* size of output */
  3440. plain2, /* additional input */
  3441. sizeof(plain2) -1), /* size of additional input */
  3442. 1); /* expected result code 1:success */
  3443. total += outl;
  3444. AssertIntNE(outl, 0); /* some output is expected this time*/
  3445. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3446. AssertIntEQ(
  3447. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3448. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3449. EVP_EncodeFinal(
  3450. ctx,
  3451. encOutBuff + outl,
  3452. &outl);
  3453. total += outl;
  3454. AssertIntNE(total,0);
  3455. AssertIntNE(outl,0);
  3456. AssertIntEQ(XSTRNCMP(
  3457. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3458. /* test with illeagal parameters */
  3459. outl = 1;
  3460. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3461. AssertIntEQ(outl, 0);
  3462. outl = 1;
  3463. EVP_EncodeFinal(ctx, NULL, &outl);
  3464. AssertIntEQ(outl, 0);
  3465. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3466. EVP_EncodeFinal(NULL, NULL, NULL);
  3467. EVP_ENCODE_CTX_free(ctx);
  3468. printf(resultFmt, passed);
  3469. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3470. return 0;
  3471. }
  3472. static int test_wolfSSL_EVP_EncodeFinal(void)
  3473. {
  3474. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3475. printf(testingFmt, "wolfSSL_EVP_EncodeFinal()");
  3476. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3477. * test_wolfSSL_EVP_EncodeUpdate
  3478. */
  3479. printf(resultFmt, passed);
  3480. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3481. return 0;
  3482. }
  3483. static int test_wolfSSL_EVP_DecodeInit(void)
  3484. {
  3485. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3486. EVP_ENCODE_CTX* ctx = NULL;
  3487. printf(testingFmt, "EVP_DecodeInit()");
  3488. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3489. AssertIntEQ( ctx->remaining,0);
  3490. AssertIntEQ( ctx->data[0],0);
  3491. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3492. /* make ctx dirty */
  3493. ctx->remaining = 10;
  3494. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3495. EVP_DecodeInit(ctx);
  3496. AssertIntEQ( ctx->remaining,0);
  3497. AssertIntEQ( ctx->data[0],0);
  3498. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3499. EVP_ENCODE_CTX_free(ctx);
  3500. printf(resultFmt, passed);
  3501. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3502. return 0;
  3503. }
  3504. static int test_wolfSSL_EVP_DecodeUpdate(void)
  3505. {
  3506. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3507. int outl;
  3508. unsigned char decOutBuff[300];
  3509. EVP_ENCODE_CTX* ctx;
  3510. static const unsigned char enc1[] =
  3511. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3512. /* const unsigned char plain1[] =
  3513. {"This is a base64 decoding test."} */
  3514. printf(testingFmt, "EVP_DecodeUpdate()");
  3515. ctx = EVP_ENCODE_CTX_new();
  3516. EVP_DecodeInit(ctx);
  3517. /* illegal parameter tests */
  3518. /* pass NULL as ctx */
  3519. AssertIntEQ(
  3520. EVP_DecodeUpdate(
  3521. NULL, /* pass NULL as ctx */
  3522. decOutBuff,
  3523. &outl,
  3524. enc1,
  3525. sizeof(enc1)-1),
  3526. -1 /* expected result code -1: fail */
  3527. );
  3528. AssertIntEQ( outl, 0);
  3529. /* pass NULL as output */
  3530. AssertIntEQ(
  3531. EVP_DecodeUpdate(
  3532. ctx,
  3533. NULL, /* pass NULL as out buff */
  3534. &outl,
  3535. enc1,
  3536. sizeof(enc1)-1),
  3537. -1 /* expected result code -1: fail */
  3538. );
  3539. AssertIntEQ( outl, 0);
  3540. /* pass NULL as outl */
  3541. AssertIntEQ(
  3542. EVP_DecodeUpdate(
  3543. ctx,
  3544. decOutBuff,
  3545. NULL, /* pass NULL as outl */
  3546. enc1,
  3547. sizeof(enc1)-1),
  3548. -1 /* expected result code -1: fail */
  3549. );
  3550. /* pass NULL as input */
  3551. AssertIntEQ(
  3552. EVP_DecodeUpdate(
  3553. ctx,
  3554. decOutBuff,
  3555. &outl,
  3556. NULL, /* pass NULL as in */
  3557. sizeof(enc1)-1),
  3558. -1 /* expected result code -1: fail */
  3559. );
  3560. AssertIntEQ( outl, 0);
  3561. AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  3562. /* pass zero length input */
  3563. AssertIntEQ(
  3564. EVP_DecodeUpdate(
  3565. ctx,
  3566. decOutBuff,
  3567. &outl,
  3568. enc1,
  3569. 0), /* pass zero as input len */
  3570. 1 /* expected result code 1: success */
  3571. );
  3572. /* decode correct base64 string */
  3573. {
  3574. static const unsigned char enc2[] =
  3575. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3576. static const unsigned char plain2[] =
  3577. {"This is a base64 decoding test."};
  3578. EVP_EncodeInit(ctx);
  3579. AssertIntEQ(
  3580. EVP_DecodeUpdate(
  3581. ctx,
  3582. decOutBuff,
  3583. &outl,
  3584. enc2,
  3585. sizeof(enc2)-1),
  3586. 0 /* expected result code 0: success */
  3587. );
  3588. AssertIntEQ(outl,sizeof(plain2) -1);
  3589. AssertIntEQ(
  3590. EVP_DecodeFinal(
  3591. ctx,
  3592. decOutBuff + outl,
  3593. &outl),
  3594. 1 /* expected result code 1: success */
  3595. );
  3596. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  3597. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3598. sizeof(plain2) -1 ),0);
  3599. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  3600. sizeof(plain2)-1);
  3601. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3602. sizeof(plain2) -1 ),0);
  3603. }
  3604. /* decode correct base64 string which does not have '\n' in its last*/
  3605. {
  3606. static const unsigned char enc3[] =
  3607. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  3608. static const unsigned char plain3[] =
  3609. {"This is a base64 decoding test."}; /* 31 chars */
  3610. EVP_EncodeInit(ctx);
  3611. AssertIntEQ(
  3612. EVP_DecodeUpdate(
  3613. ctx,
  3614. decOutBuff,
  3615. &outl,
  3616. enc3,
  3617. sizeof(enc3)-1),
  3618. 0 /* expected result code 0: success */
  3619. );
  3620. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  3621. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3622. sizeof(plain3) -1 ),0);
  3623. AssertIntEQ(
  3624. EVP_DecodeFinal(
  3625. ctx,
  3626. decOutBuff + outl,
  3627. &outl),
  3628. 1 /* expected result code 1: success */
  3629. );
  3630. AssertIntEQ(outl,0 );
  3631. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  3632. sizeof(plain3)-1);
  3633. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3634. sizeof(plain3) -1 ),0);
  3635. }
  3636. /* decode string which has a padding char ('=') in the illegal position*/
  3637. {
  3638. static const unsigned char enc4[] =
  3639. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  3640. EVP_EncodeInit(ctx);
  3641. AssertIntEQ(
  3642. EVP_DecodeUpdate(
  3643. ctx,
  3644. decOutBuff,
  3645. &outl,
  3646. enc4,
  3647. sizeof(enc4)-1),
  3648. -1 /* expected result code -1: error */
  3649. );
  3650. AssertIntEQ(outl,0);
  3651. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  3652. }
  3653. /* small data decode test */
  3654. {
  3655. static const unsigned char enc00[] = {"VG"};
  3656. static const unsigned char enc01[] = {"g=\n"};
  3657. static const unsigned char plain4[] = {"Th"};
  3658. EVP_EncodeInit(ctx);
  3659. AssertIntEQ(
  3660. EVP_DecodeUpdate(
  3661. ctx,
  3662. decOutBuff,
  3663. &outl,
  3664. enc00,
  3665. sizeof(enc00)-1),
  3666. 1 /* expected result code 1: success */
  3667. );
  3668. AssertIntEQ(outl,0);
  3669. AssertIntEQ(
  3670. EVP_DecodeUpdate(
  3671. ctx,
  3672. decOutBuff + outl,
  3673. &outl,
  3674. enc01,
  3675. sizeof(enc01)-1),
  3676. 0 /* expected result code 0: success */
  3677. );
  3678. AssertIntEQ(outl,sizeof(plain4)-1);
  3679. /* test with illegal parameters */
  3680. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  3681. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  3682. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  3683. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  3684. EVP_DecodeFinal(
  3685. ctx,
  3686. decOutBuff + outl,
  3687. &outl);
  3688. AssertIntEQ( outl, 0);
  3689. AssertIntEQ(
  3690. XSTRNCMP(
  3691. (const char*)decOutBuff,
  3692. (const char*)plain4,sizeof(plain4)-1 ),
  3693. 0);
  3694. }
  3695. EVP_ENCODE_CTX_free(ctx);
  3696. printf(resultFmt, passed);
  3697. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3698. return 0;
  3699. }
  3700. static int test_wolfSSL_EVP_DecodeFinal(void)
  3701. {
  3702. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3703. printf(testingFmt, "EVP_DecodeFinal()");
  3704. /* tests for wolfSSL_EVP_DecodeFinal are included in
  3705. * test_wolfSSL_EVP_DecodeUpdate
  3706. */
  3707. printf(resultFmt, passed);
  3708. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3709. return 0;
  3710. }
  3711. /* Test function for wolfSSL_EVP_get_cipherbynid.
  3712. */
  3713. #ifdef OPENSSL_EXTRA
  3714. static int test_wolfSSL_EVP_get_cipherbynid(void)
  3715. {
  3716. #ifndef NO_AES
  3717. const WOLFSSL_EVP_CIPHER* c;
  3718. c = wolfSSL_EVP_get_cipherbynid(419);
  3719. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3720. defined(WOLFSSL_AES_128)
  3721. AssertNotNull(c);
  3722. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  3723. #else
  3724. AssertNull(c);
  3725. #endif
  3726. c = wolfSSL_EVP_get_cipherbynid(423);
  3727. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3728. defined(WOLFSSL_AES_192)
  3729. AssertNotNull(c);
  3730. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  3731. #else
  3732. AssertNull(c);
  3733. #endif
  3734. c = wolfSSL_EVP_get_cipherbynid(427);
  3735. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3736. defined(WOLFSSL_AES_256)
  3737. AssertNotNull(c);
  3738. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  3739. #else
  3740. AssertNull(c);
  3741. #endif
  3742. c = wolfSSL_EVP_get_cipherbynid(904);
  3743. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  3744. AssertNotNull(c);
  3745. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  3746. #else
  3747. AssertNull(c);
  3748. #endif
  3749. c = wolfSSL_EVP_get_cipherbynid(905);
  3750. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  3751. AssertNotNull(c);
  3752. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  3753. #else
  3754. AssertNull(c);
  3755. #endif
  3756. c = wolfSSL_EVP_get_cipherbynid(906);
  3757. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  3758. AssertNotNull(c);
  3759. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  3760. #else
  3761. AssertNull(c);
  3762. #endif
  3763. c = wolfSSL_EVP_get_cipherbynid(418);
  3764. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  3765. AssertNotNull(c);
  3766. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  3767. #else
  3768. AssertNull(c);
  3769. #endif
  3770. c = wolfSSL_EVP_get_cipherbynid(422);
  3771. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  3772. AssertNotNull(c);
  3773. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  3774. #else
  3775. AssertNull(c);
  3776. #endif
  3777. c = wolfSSL_EVP_get_cipherbynid(426);
  3778. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  3779. AssertNotNull(c);
  3780. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  3781. #else
  3782. AssertNull(c);
  3783. #endif
  3784. #endif /* !NO_AES */
  3785. #ifndef NO_DES3
  3786. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  3787. #ifdef WOLFSSL_DES_ECB
  3788. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  3789. #endif
  3790. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  3791. #ifdef WOLFSSL_DES_ECB
  3792. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  3793. #endif
  3794. #endif /* !NO_DES3 */
  3795. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  3796. AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  3797. #endif
  3798. /* test for nid is out of range */
  3799. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  3800. return 0;
  3801. }
  3802. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  3803. {
  3804. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  3805. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  3806. const EVP_CIPHER *init = EVP_aes_128_cbc();
  3807. const EVP_CIPHER *test;
  3808. byte key[AES_BLOCK_SIZE] = {0};
  3809. byte iv[AES_BLOCK_SIZE] = {0};
  3810. AssertNotNull(ctx);
  3811. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  3812. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  3813. test = EVP_CIPHER_CTX_cipher(ctx);
  3814. AssertTrue(init == test);
  3815. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  3816. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  3817. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  3818. EVP_CIPHER_CTX_free(ctx);
  3819. /* test EVP_CIPHER_CTX_cleanup with NULL */
  3820. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  3821. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  3822. return 0;
  3823. }
  3824. #endif /* OPENSSL_EXTRA */
  3825. /*----------------------------------------------------------------------------*
  3826. | IO
  3827. *----------------------------------------------------------------------------*/
  3828. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  3829. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  3830. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  3831. #define HAVE_IO_TESTS_DEPENDENCIES
  3832. #endif
  3833. /* helper functions */
  3834. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  3835. #ifdef WOLFSSL_SESSION_EXPORT
  3836. #ifdef WOLFSSL_DTLS
  3837. /* set up function for sending session information */
  3838. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  3839. {
  3840. WOLFSSL_CTX* ctx = NULL;
  3841. WOLFSSL* ssl = NULL;
  3842. AssertNotNull(inSsl);
  3843. AssertNotNull(buf);
  3844. AssertIntNE(0, sz);
  3845. /* Set ctx to DTLS 1.2 */
  3846. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  3847. AssertNotNull(ctx);
  3848. ssl = wolfSSL_new(ctx);
  3849. AssertNotNull(ssl);
  3850. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  3851. wolfSSL_free(ssl);
  3852. wolfSSL_CTX_free(ctx);
  3853. (void)userCtx;
  3854. return 0;
  3855. }
  3856. #endif
  3857. /* returns negative value on fail and positive (including 0) on success */
  3858. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  3859. {
  3860. int ret, err, loop_count, count, timeout = 10;
  3861. char msg[] = "I hear you fa shizzle!";
  3862. char input[1024];
  3863. loop_count = ((func_args*)args)->argc;
  3864. #ifdef WOLFSSL_ASYNC_CRYPT
  3865. err = 0; /* Reset error */
  3866. #endif
  3867. do {
  3868. #ifdef WOLFSSL_ASYNC_CRYPT
  3869. if (err == WC_PENDING_E) {
  3870. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3871. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3872. }
  3873. #endif
  3874. ret = wolfSSL_accept(ssl);
  3875. err = wolfSSL_get_error(ssl, 0);
  3876. if (err == WOLFSSL_ERROR_WANT_READ ||
  3877. err == WOLFSSL_ERROR_WANT_WRITE) {
  3878. int select_ret;
  3879. err = WC_PENDING_E;
  3880. select_ret = tcp_select(*sockfd, timeout);
  3881. if (select_ret == TEST_TIMEOUT) {
  3882. return WOLFSSL_FATAL_ERROR;
  3883. }
  3884. }
  3885. } while (err == WC_PENDING_E);
  3886. if (ret != WOLFSSL_SUCCESS) {
  3887. char buff[WOLFSSL_MAX_ERROR_SZ];
  3888. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3889. return ret;
  3890. }
  3891. for (count = 0; count < loop_count; count++) {
  3892. int select_ret;
  3893. select_ret = tcp_select(*sockfd, timeout);
  3894. if (select_ret == TEST_TIMEOUT) {
  3895. ret = WOLFSSL_FATAL_ERROR;
  3896. break;
  3897. }
  3898. do {
  3899. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  3900. if (ret > 0) {
  3901. input[ret] = '\0';
  3902. printf("Client message: %s\n", input);
  3903. }
  3904. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3905. do {
  3906. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  3907. return WOLFSSL_FATAL_ERROR;
  3908. }
  3909. err = wolfSSL_get_error(ssl, ret);
  3910. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3911. }
  3912. return ret;
  3913. }
  3914. #endif /* WOLFSSL_SESSION_EXPORT */
  3915. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  3916. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  3917. defined(HAVE_AES_CBC)
  3918. typedef struct openssl_key_ctx {
  3919. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  3920. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  3921. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  3922. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  3923. } openssl_key_ctx;
  3924. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  3925. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  3926. static WC_INLINE int OpenSSLTicketInit(void)
  3927. {
  3928. int ret = wc_InitRng(&myOpenSSLKey_rng);
  3929. if (ret != 0) return ret;
  3930. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  3931. sizeof(myOpenSSLKey_ctx.name));
  3932. if (ret != 0) return ret;
  3933. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  3934. sizeof(myOpenSSLKey_ctx.key));
  3935. if (ret != 0) return ret;
  3936. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  3937. sizeof(myOpenSSLKey_ctx.hmacKey));
  3938. if (ret != 0) return ret;
  3939. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  3940. sizeof(myOpenSSLKey_ctx.iv));
  3941. if (ret != 0) return ret;
  3942. return 0;
  3943. }
  3944. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  3945. byte name[WOLFSSL_TICKET_NAME_SZ],
  3946. byte iv[WOLFSSL_TICKET_IV_SZ],
  3947. WOLFSSL_EVP_CIPHER_CTX *ectx,
  3948. WOLFSSL_HMAC_CTX *hctx, int enc) {
  3949. (void)ssl;
  3950. if (enc) {
  3951. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  3952. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  3953. }
  3954. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  3955. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  3956. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  3957. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  3958. return 0;
  3959. }
  3960. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  3961. if (enc)
  3962. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3963. else
  3964. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3965. return 1;
  3966. }
  3967. static WC_INLINE void OpenSSLTicketCleanup(void)
  3968. {
  3969. wc_FreeRng(&myOpenSSLKey_rng);
  3970. }
  3971. #endif
  3972. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  3973. #ifdef WC_SHA512_DIGEST_SIZE
  3974. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  3975. #else
  3976. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  3977. #endif
  3978. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  3979. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  3980. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  3981. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  3982. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  3983. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  3984. {
  3985. SOCKET_T sockfd = 0;
  3986. SOCKET_T clientfd = 0;
  3987. word16 port;
  3988. callback_functions* cbf;
  3989. WOLFSSL_CTX* ctx = 0;
  3990. WOLFSSL* ssl = 0;
  3991. func_args* opts = (func_args*)args;
  3992. char msg[] = "I hear you fa shizzle!";
  3993. char input[1024];
  3994. int idx;
  3995. int ret, err = 0;
  3996. int sharedCtx = 0;
  3997. int doUdp = 0;
  3998. SOCKADDR_IN_T cliAddr;
  3999. socklen_t cliLen;
  4000. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4001. size_t msg_len = 0;
  4002. #endif
  4003. #ifdef WOLFSSL_TIRTOS
  4004. fdOpenSession(Task_self());
  4005. #endif
  4006. opts->return_code = TEST_FAIL;
  4007. cbf = opts->callbacks;
  4008. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4009. if (cbf != NULL && cbf->ctx) {
  4010. ctx = cbf->ctx;
  4011. sharedCtx = 1;
  4012. }
  4013. else
  4014. #endif
  4015. {
  4016. WOLFSSL_METHOD* method = NULL;
  4017. if (cbf != NULL && cbf->method != NULL) {
  4018. method = cbf->method();
  4019. }
  4020. else {
  4021. method = wolfSSLv23_server_method();
  4022. }
  4023. ctx = wolfSSL_CTX_new(method);
  4024. }
  4025. if (ctx == NULL) {
  4026. goto done;
  4027. }
  4028. if (cbf == NULL || !cbf->ticNoInit) {
  4029. #if defined(HAVE_SESSION_TICKET) && \
  4030. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4031. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4032. OpenSSLTicketInit();
  4033. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  4034. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4035. TicketInit();
  4036. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  4037. #endif
  4038. #endif
  4039. }
  4040. #if defined(USE_WINDOWS_API)
  4041. port = opts->signal->port;
  4042. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4043. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4044. /* Let tcp_listen assign port */
  4045. port = 0;
  4046. #else
  4047. /* Use default port */
  4048. port = wolfSSLPort;
  4049. #endif
  4050. if (cbf != NULL)
  4051. doUdp = cbf->doUdp;
  4052. /* do it here to detect failure */
  4053. tcp_accept(
  4054. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  4055. if (doUdp) {
  4056. cliLen = sizeof(cliAddr);
  4057. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  4058. (struct sockaddr*)&cliAddr, &cliLen);
  4059. AssertIntGT(idx, 0);
  4060. }
  4061. else {
  4062. CloseSocket(sockfd);
  4063. }
  4064. wolfSSL_CTX_set_verify(ctx,
  4065. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4066. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4067. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4068. #endif
  4069. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4070. != WOLFSSL_SUCCESS) {
  4071. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4072. goto done;
  4073. }
  4074. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4075. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4076. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4077. #else
  4078. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4079. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4080. #endif
  4081. /*err_sys("can't load server cert chain file, "
  4082. "Please run from wolfSSL home dir");*/
  4083. goto done;
  4084. }
  4085. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4086. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4087. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4088. #else
  4089. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4090. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4091. #endif
  4092. /*err_sys("can't load server key file, "
  4093. "Please run from wolfSSL home dir");*/
  4094. goto done;
  4095. }
  4096. /* call ctx setup callback */
  4097. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4098. cbf->ctx_ready(ctx);
  4099. }
  4100. ssl = wolfSSL_new(ctx);
  4101. if (ssl == NULL) {
  4102. goto done;
  4103. }
  4104. if (doUdp) {
  4105. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4106. if (err != WOLFSSL_SUCCESS)
  4107. goto done;
  4108. }
  4109. #ifdef WOLFSSL_SESSION_EXPORT
  4110. /* only add in more complex nonblocking case with session export tests */
  4111. if (args && opts->argc > 0) {
  4112. /* set as nonblock and time out for waiting on read/write */
  4113. tcp_set_nonblocking(&clientfd);
  4114. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  4115. }
  4116. #endif
  4117. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4118. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4119. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4120. #else
  4121. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  4122. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4123. #endif
  4124. /*err_sys("can't load server cert chain file, "
  4125. "Please run from wolfSSL home dir");*/
  4126. goto done;
  4127. }
  4128. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4129. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4130. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4131. #else
  4132. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4133. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4134. #endif
  4135. /*err_sys("can't load server key file, "
  4136. "Please run from wolfSSL home dir");*/
  4137. goto done;
  4138. }
  4139. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4140. /*err_sys("SSL_set_fd failed");*/
  4141. goto done;
  4142. }
  4143. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4144. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4145. #elif !defined(NO_DH)
  4146. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4147. #endif
  4148. /* call ssl setup callback */
  4149. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4150. cbf->ssl_ready(ssl);
  4151. }
  4152. #ifdef WOLFSSL_SESSION_EXPORT
  4153. /* only add in more complex nonblocking case with session export tests */
  4154. if (opts->argc > 0) {
  4155. ret = nonblocking_accept_read(args, ssl, &clientfd);
  4156. if (ret >= 0) {
  4157. opts->return_code = TEST_SUCCESS;
  4158. }
  4159. #ifdef WOLFSSL_TIRTOS
  4160. Task_yield();
  4161. #endif
  4162. goto done;
  4163. }
  4164. #endif
  4165. #ifdef WOLFSSL_ASYNC_CRYPT
  4166. err = 0; /* Reset error */
  4167. #endif
  4168. do {
  4169. #ifdef WOLFSSL_ASYNC_CRYPT
  4170. if (err == WC_PENDING_E) {
  4171. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4172. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4173. }
  4174. #endif
  4175. ret = wolfSSL_accept(ssl);
  4176. err = wolfSSL_get_error(ssl, 0);
  4177. } while (err == WC_PENDING_E);
  4178. if (ret != WOLFSSL_SUCCESS) {
  4179. char buff[WOLFSSL_MAX_ERROR_SZ];
  4180. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4181. /*err_sys("SSL_accept failed");*/
  4182. goto done;
  4183. }
  4184. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4185. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  4186. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  4187. AssertIntGE(msg_len, 0);
  4188. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  4189. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  4190. AssertIntGE(msg_len, 0);
  4191. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4192. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4193. if (idx > 0) {
  4194. input[idx] = '\0';
  4195. printf("Client message: %s\n", input);
  4196. }
  4197. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4198. /*err_sys("SSL_write failed");*/
  4199. #ifdef WOLFSSL_TIRTOS
  4200. return;
  4201. #else
  4202. return 0;
  4203. #endif
  4204. }
  4205. if (cbf != NULL && cbf->on_result != NULL)
  4206. cbf->on_result(ssl);
  4207. #ifdef WOLFSSL_TIRTOS
  4208. Task_yield();
  4209. #endif
  4210. opts->return_code = TEST_SUCCESS;
  4211. done:
  4212. wolfSSL_shutdown(ssl);
  4213. wolfSSL_free(ssl);
  4214. if (!sharedCtx)
  4215. wolfSSL_CTX_free(ctx);
  4216. CloseSocket(clientfd);
  4217. #ifdef WOLFSSL_TIRTOS
  4218. fdCloseSession(Task_self());
  4219. #endif
  4220. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4221. && defined(HAVE_THREAD_LS)
  4222. wc_ecc_fp_free(); /* free per thread cache */
  4223. #endif
  4224. if (cbf == NULL || !cbf->ticNoInit) {
  4225. #if defined(HAVE_SESSION_TICKET) && \
  4226. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4227. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4228. OpenSSLTicketCleanup();
  4229. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4230. TicketCleanup();
  4231. #endif
  4232. #endif
  4233. }
  4234. #ifndef WOLFSSL_TIRTOS
  4235. return 0;
  4236. #endif
  4237. }
  4238. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4239. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  4240. {
  4241. SOCKET_T sockfd = 0;
  4242. SOCKET_T clientfd = 0;
  4243. word16 port;
  4244. callback_functions* cbf;
  4245. WOLFSSL_CTX* ctx = 0;
  4246. WOLFSSL* ssl = 0;
  4247. char msg[] = "I hear you fa shizzle!";
  4248. char input[1024];
  4249. int idx;
  4250. int ret, err = 0;
  4251. int sharedCtx = 0;
  4252. int loop_count = ((func_args*)args)->argc;
  4253. int count = 0;
  4254. #ifdef WOLFSSL_TIRTOS
  4255. fdOpenSession(Task_self());
  4256. #endif
  4257. ((func_args*)args)->return_code = TEST_FAIL;
  4258. cbf = ((func_args*)args)->callbacks;
  4259. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4260. if (cbf != NULL && cbf->ctx) {
  4261. ctx = cbf->ctx;
  4262. sharedCtx = 1;
  4263. }
  4264. else
  4265. #endif
  4266. {
  4267. WOLFSSL_METHOD* method = NULL;
  4268. if (cbf != NULL && cbf->method != NULL) {
  4269. method = cbf->method();
  4270. }
  4271. else {
  4272. method = wolfSSLv23_server_method();
  4273. }
  4274. ctx = wolfSSL_CTX_new(method);
  4275. }
  4276. #if defined(USE_WINDOWS_API)
  4277. port = ((func_args*)args)->signal->port;
  4278. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4279. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4280. /* Let tcp_listen assign port */
  4281. port = 0;
  4282. #else
  4283. /* Use default port */
  4284. port = wolfSSLPort;
  4285. #endif
  4286. wolfSSL_CTX_set_verify(ctx,
  4287. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4288. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4289. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4290. #endif
  4291. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4292. != WOLFSSL_SUCCESS) {
  4293. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4294. goto done;
  4295. }
  4296. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4297. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4298. /*err_sys("can't load server cert chain file, "
  4299. "Please run from wolfSSL home dir");*/
  4300. goto done;
  4301. }
  4302. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4303. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4304. /*err_sys("can't load server key file, "
  4305. "Please run from wolfSSL home dir");*/
  4306. goto done;
  4307. }
  4308. /* call ctx setup callback */
  4309. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4310. cbf->ctx_ready(ctx);
  4311. }
  4312. while(count != loop_count) {
  4313. ssl = wolfSSL_new(ctx);
  4314. if (ssl == NULL) {
  4315. goto done;
  4316. }
  4317. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4318. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4319. /*err_sys("can't load server cert chain file, "
  4320. "Please run from wolfSSL home dir");*/
  4321. goto done;
  4322. }
  4323. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4324. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4325. /*err_sys("can't load server key file, "
  4326. "Please run from wolfSSL home dir");*/
  4327. goto done;
  4328. }
  4329. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4330. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4331. #elif !defined(NO_DH)
  4332. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4333. #endif
  4334. /* call ssl setup callback */
  4335. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4336. cbf->ssl_ready(ssl);
  4337. }
  4338. /* do it here to detect failure */
  4339. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4340. CloseSocket(sockfd);
  4341. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4342. /*err_sys("SSL_set_fd failed");*/
  4343. goto done;
  4344. }
  4345. #ifdef WOLFSSL_ASYNC_CRYPT
  4346. err = 0; /* Reset error */
  4347. #endif
  4348. do {
  4349. #ifdef WOLFSSL_ASYNC_CRYPT
  4350. if (err == WC_PENDING_E) {
  4351. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4352. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4353. }
  4354. #endif
  4355. ret = wolfSSL_accept(ssl);
  4356. err = wolfSSL_get_error(ssl, 0);
  4357. } while (err == WC_PENDING_E);
  4358. if (ret != WOLFSSL_SUCCESS) {
  4359. char buff[WOLFSSL_MAX_ERROR_SZ];
  4360. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4361. /*err_sys("SSL_accept failed");*/
  4362. goto done;
  4363. }
  4364. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4365. if (idx > 0) {
  4366. input[idx] = '\0';
  4367. printf("Client message: %s\n", input);
  4368. }
  4369. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4370. /*err_sys("SSL_write failed");*/
  4371. #ifdef WOLFSSL_TIRTOS
  4372. return;
  4373. #else
  4374. return 0;
  4375. #endif
  4376. }
  4377. /* free ssl for this connection */
  4378. wolfSSL_shutdown(ssl);
  4379. wolfSSL_free(ssl); ssl = NULL;
  4380. CloseSocket(clientfd);
  4381. count++;
  4382. }
  4383. #ifdef WOLFSSL_TIRTOS
  4384. Task_yield();
  4385. #endif
  4386. ((func_args*)args)->return_code = TEST_SUCCESS;
  4387. done:
  4388. if(ssl != NULL) {
  4389. wolfSSL_shutdown(ssl);
  4390. wolfSSL_free(ssl);
  4391. }
  4392. if (!sharedCtx)
  4393. wolfSSL_CTX_free(ctx);
  4394. CloseSocket(clientfd);
  4395. #ifdef WOLFSSL_TIRTOS
  4396. fdCloseSession(Task_self());
  4397. #endif
  4398. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4399. && defined(HAVE_THREAD_LS)
  4400. wc_ecc_fp_free(); /* free per thread cache */
  4401. #endif
  4402. #ifndef WOLFSSL_TIRTOS
  4403. return 0;
  4404. #endif
  4405. }
  4406. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4407. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4408. static int test_client_nofail(void* args, cbType cb)
  4409. {
  4410. #if !defined(NO_WOLFSSL_CLIENT)
  4411. SOCKET_T sockfd = 0;
  4412. callback_functions* cbf;
  4413. WOLFSSL_CTX* ctx = 0;
  4414. WOLFSSL* ssl = 0;
  4415. WOLFSSL_CIPHER* cipher;
  4416. char msg[64] = "hello wolfssl!";
  4417. char reply[1024];
  4418. int input;
  4419. int msgSz = (int)XSTRLEN(msg);
  4420. int ret, err = 0;
  4421. int cipherSuite;
  4422. int sharedCtx = 0;
  4423. const char* cipherName1, *cipherName2;
  4424. #ifdef WOLFSSL_TIRTOS
  4425. fdOpenSession(Task_self());
  4426. #endif
  4427. ((func_args*)args)->return_code = TEST_FAIL;
  4428. cbf = ((func_args*)args)->callbacks;
  4429. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4430. if (cbf != NULL && cbf->ctx) {
  4431. ctx = cbf->ctx;
  4432. sharedCtx = cbf->isSharedCtx;
  4433. }
  4434. else
  4435. #endif
  4436. {
  4437. WOLFSSL_METHOD* method = NULL;
  4438. if (cbf != NULL && cbf->method != NULL) {
  4439. method = cbf->method();
  4440. }
  4441. else {
  4442. method = wolfSSLv23_client_method();
  4443. }
  4444. ctx = wolfSSL_CTX_new(method);
  4445. }
  4446. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4447. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4448. #endif
  4449. /* Do connect here so server detects failures */
  4450. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4451. 0, 0, NULL);
  4452. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4453. {
  4454. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4455. goto done;
  4456. }
  4457. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4458. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4459. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4460. #else
  4461. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4462. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4463. #endif
  4464. /*err_sys("can't load client cert file, "
  4465. "Please run from wolfSSL home dir");*/
  4466. goto done;
  4467. }
  4468. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4469. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4470. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4471. #else
  4472. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4473. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4474. #endif
  4475. /*err_sys("can't load client key file, "
  4476. "Please run from wolfSSL home dir");*/
  4477. goto done;
  4478. }
  4479. /* call ctx setup callback */
  4480. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4481. cbf->ctx_ready(ctx);
  4482. }
  4483. ssl = wolfSSL_new(ctx);
  4484. if (ssl == NULL) {
  4485. goto done;
  4486. }
  4487. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4488. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4489. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4490. #else
  4491. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4492. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4493. #endif
  4494. /*err_sys("can't load client cert file, "
  4495. "Please run from wolfSSL home dir");*/
  4496. goto done;
  4497. }
  4498. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4499. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4500. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4501. #else
  4502. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4503. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4504. #endif
  4505. /*err_sys("can't load client key file, "
  4506. "Please run from wolfSSL home dir");*/
  4507. goto done;
  4508. }
  4509. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4510. /*err_sys("SSL_set_fd failed");*/
  4511. goto done;
  4512. }
  4513. /* call ssl setup callback */
  4514. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4515. cbf->ssl_ready(ssl);
  4516. }
  4517. #ifdef WOLFSSL_ASYNC_CRYPT
  4518. err = 0; /* Reset error */
  4519. #endif
  4520. do {
  4521. #ifdef WOLFSSL_ASYNC_CRYPT
  4522. if (err == WC_PENDING_E) {
  4523. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4524. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4525. }
  4526. #endif
  4527. ret = wolfSSL_connect(ssl);
  4528. err = wolfSSL_get_error(ssl, 0);
  4529. } while (err == WC_PENDING_E);
  4530. if (ret != WOLFSSL_SUCCESS) {
  4531. char buff[WOLFSSL_MAX_ERROR_SZ];
  4532. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4533. /*err_sys("SSL_connect failed");*/
  4534. goto done;
  4535. }
  4536. /* test the various get cipher methods */
  4537. /* Internal cipher suite names */
  4538. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  4539. cipherName1 = wolfSSL_get_cipher_name(ssl);
  4540. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  4541. (cipherSuite >> 8), cipherSuite & 0xFF);
  4542. AssertStrEQ(cipherName1, cipherName2);
  4543. /* IANA Cipher Suites Names */
  4544. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  4545. then it's the internal cipher suite name */
  4546. cipher = wolfSSL_get_current_cipher(ssl);
  4547. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  4548. cipherName2 = wolfSSL_get_cipher(ssl);
  4549. AssertStrEQ(cipherName1, cipherName2);
  4550. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  4551. !defined(WOLFSSL_QT)
  4552. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  4553. (cipherSuite >> 8), cipherSuite & 0xFF);
  4554. AssertStrEQ(cipherName1, cipherName2);
  4555. #endif
  4556. if (cb != NULL)
  4557. (cb)(ctx, ssl);
  4558. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4559. /*err_sys("SSL_write failed");*/
  4560. goto done;
  4561. }
  4562. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4563. if (input > 0) {
  4564. reply[input] = '\0';
  4565. printf("Server response: %s\n", reply);
  4566. }
  4567. if (cbf != NULL && cbf->on_result != NULL)
  4568. cbf->on_result(ssl);
  4569. ((func_args*)args)->return_code = TEST_SUCCESS;
  4570. done:
  4571. wolfSSL_free(ssl);
  4572. if (!sharedCtx)
  4573. wolfSSL_CTX_free(ctx);
  4574. CloseSocket(sockfd);
  4575. #ifdef WOLFSSL_TIRTOS
  4576. fdCloseSession(Task_self());
  4577. #endif
  4578. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4579. && defined(HAVE_THREAD_LS)
  4580. wc_ecc_fp_free(); /* free per thread cache */
  4581. #endif
  4582. #else
  4583. (void)args;
  4584. (void)cb;
  4585. #endif /* !NO_WOLFSSL_CLIENT */
  4586. return 0;
  4587. }
  4588. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  4589. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT)
  4590. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  4591. {
  4592. SOCKET_T sockfd = 0;
  4593. callback_functions* cbf;
  4594. WOLFSSL_CTX* ctx = 0;
  4595. WOLFSSL* ssl = 0;
  4596. WOLFSSL_SESSION* session = NULL;
  4597. char msg[64] = "hello wolfssl!";
  4598. char reply[1024];
  4599. int input;
  4600. int msgSz = (int)XSTRLEN(msg);
  4601. int ret, err = 0;
  4602. int sharedCtx = 0;
  4603. #ifdef WOLFSSL_TIRTOS
  4604. fdOpenSession(Task_self());
  4605. #endif
  4606. ((func_args*)args)->return_code = TEST_FAIL;
  4607. cbf = ((func_args*)args)->callbacks;
  4608. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4609. if (cbf != NULL && cbf->ctx) {
  4610. ctx = cbf->ctx;
  4611. sharedCtx = 1;
  4612. }
  4613. else
  4614. #endif
  4615. {
  4616. WOLFSSL_METHOD* method = NULL;
  4617. if (cbf != NULL && cbf->method != NULL) {
  4618. method = cbf->method();
  4619. }
  4620. else {
  4621. method = wolfSSLv23_client_method();
  4622. }
  4623. ctx = wolfSSL_CTX_new(method);
  4624. }
  4625. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4626. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4627. #endif
  4628. /* Do connect here so server detects failures */
  4629. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4630. 0, 0, NULL);
  4631. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4632. {
  4633. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4634. goto done;
  4635. }
  4636. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4637. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4638. /*err_sys("can't load client cert file, "
  4639. "Please run from wolfSSL home dir");*/
  4640. goto done;
  4641. }
  4642. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4643. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4644. /*err_sys("can't load client key file, "
  4645. "Please run from wolfSSL home dir");*/
  4646. goto done;
  4647. }
  4648. /* call ctx setup callback */
  4649. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4650. cbf->ctx_ready(ctx);
  4651. }
  4652. ssl = wolfSSL_new(ctx);
  4653. if (ssl == NULL) {
  4654. goto done;
  4655. }
  4656. /* keep handshakre resources for re-using WOLFSSL obj */
  4657. wolfSSL_KeepArrays(ssl);
  4658. if(wolfSSL_KeepHandshakeResources(ssl)) {
  4659. /* err_sys("SSL_KeepHandshakeResources failed"); */
  4660. goto done;
  4661. }
  4662. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4663. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4664. /*err_sys("can't load client cert file, "
  4665. "Please run from wolfSSL home dir");*/
  4666. goto done;
  4667. }
  4668. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4669. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4670. /*err_sys("can't load client key file, "
  4671. "Please run from wolfSSL home dir");*/
  4672. goto done;
  4673. }
  4674. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4675. /*err_sys("SSL_set_fd failed");*/
  4676. goto done;
  4677. }
  4678. /* call ssl setup callback */
  4679. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4680. cbf->ssl_ready(ssl);
  4681. }
  4682. #ifdef WOLFSSL_ASYNC_CRYPT
  4683. err = 0; /* Reset error */
  4684. #endif
  4685. do {
  4686. #ifdef WOLFSSL_ASYNC_CRYPT
  4687. if (err == WC_PENDING_E) {
  4688. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4689. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4690. }
  4691. #endif
  4692. ret = wolfSSL_connect(ssl);
  4693. err = wolfSSL_get_error(ssl, 0);
  4694. } while (err == WC_PENDING_E);
  4695. if (ret != WOLFSSL_SUCCESS) {
  4696. char buff[WOLFSSL_MAX_ERROR_SZ];
  4697. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4698. /*err_sys("SSL_connect failed");*/
  4699. goto done;
  4700. }
  4701. /* Build first session */
  4702. if (cb != NULL)
  4703. ((cbType)cb)(ctx, ssl);
  4704. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4705. /*err_sys("SSL_write failed");*/
  4706. goto done;
  4707. }
  4708. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4709. if (input > 0) {
  4710. reply[input] = '\0';
  4711. printf("Server response: %s\n", reply);
  4712. }
  4713. /* Session Resumption by re-using WOLFSSL object */
  4714. wolfSSL_set_quiet_shutdown(ssl, 1);
  4715. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  4716. /* err_sys ("SSL shutdown failed"); */
  4717. goto done;
  4718. }
  4719. session = wolfSSL_get1_session(ssl);
  4720. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  4721. /* err_sys ("SSL_clear failed"); */
  4722. goto done;
  4723. }
  4724. wolfSSL_set_session(ssl, session);
  4725. wolfSSL_SESSION_free(session);
  4726. session = NULL;
  4727. /* close socket once */
  4728. CloseSocket(sockfd);
  4729. sockfd = 0;
  4730. /* wait until server ready */
  4731. wait_tcp_ready((func_args*)server_args);
  4732. printf("session resumption\n");
  4733. /* Do re-connect */
  4734. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4735. 0, 0, NULL);
  4736. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4737. /*err_sys("SSL_set_fd failed");*/
  4738. goto done;
  4739. }
  4740. #ifdef WOLFSSL_ASYNC_CRYPT
  4741. err = 0; /* Reset error */
  4742. #endif
  4743. do {
  4744. #ifdef WOLFSSL_ASYNC_CRYPT
  4745. if (err == WC_PENDING_E) {
  4746. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4747. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4748. }
  4749. #endif
  4750. ret = wolfSSL_connect(ssl);
  4751. err = wolfSSL_get_error(ssl, 0);
  4752. } while (err == WC_PENDING_E);
  4753. if (ret != WOLFSSL_SUCCESS) {
  4754. char buff[WOLFSSL_MAX_ERROR_SZ];
  4755. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4756. /*err_sys("SSL_connect failed");*/
  4757. goto done;
  4758. }
  4759. /* Build first session */
  4760. if (cb != NULL)
  4761. ((cbType)cb)(ctx, ssl);
  4762. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4763. /*err_sys("SSL_write failed");*/
  4764. goto done;
  4765. }
  4766. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4767. if (input > 0) {
  4768. reply[input] = '\0';
  4769. printf("Server response: %s\n", reply);
  4770. }
  4771. ((func_args*)args)->return_code = TEST_SUCCESS;
  4772. done:
  4773. wolfSSL_free(ssl);
  4774. if (!sharedCtx)
  4775. wolfSSL_CTX_free(ctx);
  4776. CloseSocket(sockfd);
  4777. #ifdef WOLFSSL_TIRTOS
  4778. fdCloseSession(Task_self());
  4779. #endif
  4780. return;
  4781. }
  4782. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  4783. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  4784. static int test_client_verifyDepth(void* args)
  4785. {
  4786. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  4787. SOCKET_T sockfd = 0;
  4788. callback_functions* cbf;
  4789. WOLFSSL_CTX* ctx = 0;
  4790. WOLFSSL* ssl = 0;
  4791. char msg[64] = "hello wolfssl!";
  4792. char reply[1024];
  4793. int input;
  4794. int msgSz = (int)XSTRLEN(msg);
  4795. int ret, err = 0;
  4796. int verify_depth = ((func_args*)args)->argc;
  4797. ((func_args*)args)->return_code = TEST_FAIL;
  4798. cbf = ((func_args*)args)->callbacks;
  4799. {
  4800. WOLFSSL_METHOD* method = NULL;
  4801. if (cbf != NULL && cbf->method != NULL) {
  4802. method = cbf->method();
  4803. }
  4804. else {
  4805. method = wolfSSLv23_client_method();
  4806. }
  4807. ctx = wolfSSL_CTX_new(method);
  4808. }
  4809. /* Do connect here so server detects failures */
  4810. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4811. 0, 0, NULL);
  4812. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  4813. != WOLFSSL_SUCCESS)
  4814. {
  4815. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4816. goto done;
  4817. }
  4818. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4819. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4820. /*err_sys("can't load client cert file, "
  4821. "Please run from wolfSSL home dir");*/
  4822. goto done;
  4823. }
  4824. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4825. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4826. /*err_sys("can't load client key file, "
  4827. "Please run from wolfSSL home dir");*/
  4828. goto done;
  4829. }
  4830. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  4831. /* set verify depth */
  4832. if (verify_depth == 0) {
  4833. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  4834. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4835. } else if (verify_depth == -1) {
  4836. myVerifyAction = VERIFY_USE_PREVERFIY;
  4837. SSL_CTX_set_verify_depth(ctx, 0);
  4838. } else if (verify_depth > 0) {
  4839. myVerifyAction = VERIFY_USE_PREVERFIY;
  4840. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4841. }
  4842. ssl = wolfSSL_new(ctx);
  4843. if (ssl == NULL) {
  4844. goto done;
  4845. }
  4846. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4847. /*err_sys("SSL_set_fd failed");*/
  4848. goto done;
  4849. }
  4850. #ifdef WOLFSSL_ASYNC_CRYPT
  4851. err = 0; /* Reset error */
  4852. #endif
  4853. do {
  4854. #ifdef WOLFSSL_ASYNC_CRYPT
  4855. if (err == WC_PENDING_E) {
  4856. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4857. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4858. }
  4859. #endif
  4860. ret = wolfSSL_connect(ssl);
  4861. err = wolfSSL_get_error(ssl, 0);
  4862. } while (err == WC_PENDING_E);
  4863. if (ret != WOLFSSL_SUCCESS) {
  4864. char buff[WOLFSSL_MAX_ERROR_SZ];
  4865. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4866. goto done;
  4867. }
  4868. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4869. goto done;
  4870. }
  4871. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4872. if (input > 0) {
  4873. reply[input] = '\0';
  4874. printf("Server response: %s\n", reply);
  4875. }
  4876. ((func_args*)args)->return_code = TEST_SUCCESS;
  4877. done:
  4878. wolfSSL_free(ssl);
  4879. wolfSSL_CTX_free(ctx);
  4880. CloseSocket(sockfd);
  4881. #else
  4882. (void)args;
  4883. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  4884. return 0;
  4885. }
  4886. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  4887. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  4888. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  4889. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  4890. #define HAVE_ALPN_PROTOS_SUPPORT
  4891. #endif
  4892. /* Generic TLS client / server with callbacks for API unit tests
  4893. * Used by SNI / ALPN / crypto callback helper functions */
  4894. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  4895. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  4896. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  4897. #define ENABLE_TLS_CALLBACK_TEST
  4898. #endif
  4899. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  4900. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  4901. /* TLS server for API unit testing - generic */
  4902. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  4903. {
  4904. callback_functions* callbacks = ((func_args*)args)->callbacks;
  4905. WOLFSSL_CTX* ctx = NULL;
  4906. WOLFSSL* ssl = NULL;
  4907. SOCKET_T sfd = 0;
  4908. SOCKET_T cfd = 0;
  4909. word16 port;
  4910. char msg[] = "I hear you fa shizzle!";
  4911. int len = (int) XSTRLEN(msg);
  4912. char input[1024];
  4913. int idx;
  4914. int ret, err = 0;
  4915. ((func_args*)args)->return_code = TEST_FAIL;
  4916. #ifdef WOLFSSL_STATIC_MEMORY
  4917. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  4918. callbacks->memSz > 0) {
  4919. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  4920. callbacks->mem, callbacks->memSz, 0, 1);
  4921. if (ret != WOLFSSL_SUCCESS) {
  4922. printf("CTX static new failed %d\n", ret);
  4923. return 0;
  4924. }
  4925. }
  4926. #endif
  4927. if (ctx == NULL) {
  4928. ctx = wolfSSL_CTX_new(callbacks->method());
  4929. }
  4930. if (ctx == NULL) {
  4931. printf("CTX new failed\n");
  4932. return 0;
  4933. }
  4934. /* set defaults */
  4935. if (callbacks->caPemFile == NULL)
  4936. callbacks->caPemFile = cliCertFile;
  4937. if (callbacks->certPemFile == NULL)
  4938. callbacks->certPemFile = svrCertFile;
  4939. if (callbacks->keyPemFile == NULL)
  4940. callbacks->keyPemFile = svrKeyFile;
  4941. #ifdef WOLFSSL_TIRTOS
  4942. fdOpenSession(Task_self());
  4943. #endif
  4944. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  4945. #if defined(USE_WINDOWS_API)
  4946. port = ((func_args*)args)->signal->port;
  4947. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4948. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4949. /* Let tcp_listen assign port */
  4950. port = 0;
  4951. #else
  4952. /* Use default port */
  4953. port = wolfSSLPort;
  4954. #endif
  4955. wolfSSL_CTX_set_verify(ctx,
  4956. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4957. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4958. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4959. #endif
  4960. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  4961. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  4962. #endif
  4963. AssertIntEQ(WOLFSSL_SUCCESS,
  4964. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  4965. AssertIntEQ(WOLFSSL_SUCCESS,
  4966. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  4967. WOLFSSL_FILETYPE_PEM));
  4968. AssertIntEQ(WOLFSSL_SUCCESS,
  4969. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  4970. WOLFSSL_FILETYPE_PEM));
  4971. if (callbacks->ctx_ready)
  4972. callbacks->ctx_ready(ctx);
  4973. ssl = wolfSSL_new(ctx);
  4974. if (ssl == NULL) {
  4975. printf("SSL new failed\n");
  4976. wolfSSL_CTX_free(ctx);
  4977. return 0;
  4978. }
  4979. if (wolfSSL_dtls(ssl)) {
  4980. SOCKADDR_IN_T cliAddr;
  4981. socklen_t cliLen;
  4982. cliLen = sizeof(cliAddr);
  4983. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  4984. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  4985. (struct sockaddr*)&cliAddr, &cliLen);
  4986. AssertIntGT(idx, 0);
  4987. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4988. }
  4989. else {
  4990. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4991. CloseSocket(sfd);
  4992. }
  4993. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  4994. if (callbacks->loadToSSL) {
  4995. wolfSSL_SetDevId(ssl, callbacks->devId);
  4996. AssertIntEQ(WOLFSSL_SUCCESS,
  4997. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  4998. WOLFSSL_FILETYPE_PEM));
  4999. AssertIntEQ(WOLFSSL_SUCCESS,
  5000. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5001. WOLFSSL_FILETYPE_PEM));
  5002. }
  5003. #ifdef NO_PSK
  5004. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5005. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5006. #elif !defined(NO_DH)
  5007. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5008. #endif
  5009. #endif
  5010. if (callbacks->ssl_ready)
  5011. callbacks->ssl_ready(ssl);
  5012. #ifdef WOLFSSL_ASYNC_CRYPT
  5013. err = 0; /* Reset error */
  5014. #endif
  5015. do {
  5016. #ifdef WOLFSSL_ASYNC_CRYPT
  5017. if (err == WC_PENDING_E) {
  5018. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5019. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5020. }
  5021. #endif
  5022. ret = wolfSSL_accept(ssl);
  5023. err = wolfSSL_get_error(ssl, 0);
  5024. } while (err == WC_PENDING_E);
  5025. if (ret != WOLFSSL_SUCCESS) {
  5026. char buff[WOLFSSL_MAX_ERROR_SZ];
  5027. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5028. /*err_sys("SSL_accept failed");*/
  5029. }
  5030. else {
  5031. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  5032. input[idx] = 0;
  5033. printf("Client message: %s\n", input);
  5034. }
  5035. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  5036. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  5037. defined(WOLFSSL_DTLS)
  5038. if (wolfSSL_dtls(ssl)) {
  5039. byte* import;
  5040. word32 sz;
  5041. wolfSSL_dtls_export(ssl, NULL, &sz);
  5042. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5043. AssertNotNull(import);
  5044. idx = wolfSSL_dtls_export(ssl, import, &sz);
  5045. AssertIntGE(idx, 0);
  5046. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  5047. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5048. }
  5049. #endif
  5050. #ifdef WOLFSSL_TIRTOS
  5051. Task_yield();
  5052. #endif
  5053. ((func_args*)args)->return_code = TEST_SUCCESS;
  5054. }
  5055. if (callbacks->on_result)
  5056. callbacks->on_result(ssl);
  5057. wolfSSL_shutdown(ssl);
  5058. wolfSSL_free(ssl);
  5059. wolfSSL_CTX_free(ctx);
  5060. CloseSocket(cfd);
  5061. #ifdef WOLFSSL_TIRTOS
  5062. fdCloseSession(Task_self());
  5063. #endif
  5064. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5065. && defined(HAVE_THREAD_LS)
  5066. wc_ecc_fp_free(); /* free per thread cache */
  5067. #endif
  5068. #ifndef WOLFSSL_TIRTOS
  5069. return 0;
  5070. #endif
  5071. }
  5072. /* TLS Client for API unit testing - generic */
  5073. static void run_wolfssl_client(void* args)
  5074. {
  5075. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5076. WOLFSSL_CTX* ctx = NULL;
  5077. WOLFSSL* ssl = NULL;
  5078. SOCKET_T sfd = 0;
  5079. char msg[] = "hello wolfssl server!";
  5080. int len = (int) XSTRLEN(msg);
  5081. char input[1024];
  5082. int ret, err = 0;
  5083. ((func_args*)args)->return_code = TEST_FAIL;
  5084. /* set defaults */
  5085. if (callbacks->caPemFile == NULL)
  5086. callbacks->caPemFile = caCertFile;
  5087. if (callbacks->certPemFile == NULL)
  5088. callbacks->certPemFile = cliCertFile;
  5089. if (callbacks->keyPemFile == NULL)
  5090. callbacks->keyPemFile = cliKeyFile;
  5091. #ifdef WOLFSSL_STATIC_MEMORY
  5092. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5093. callbacks->memSz > 0) {
  5094. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5095. callbacks->mem, callbacks->memSz, 0, 1);
  5096. if (ret != WOLFSSL_SUCCESS) {
  5097. printf("CTX static new failed %d\n", ret);
  5098. return;
  5099. }
  5100. }
  5101. #endif
  5102. if (ctx == NULL) {
  5103. ctx = wolfSSL_CTX_new(callbacks->method());
  5104. }
  5105. if (ctx == NULL) {
  5106. printf("CTX new failed\n");
  5107. return;
  5108. }
  5109. #ifdef WOLFSSL_TIRTOS
  5110. fdOpenSession(Task_self());
  5111. #endif
  5112. if (!callbacks->loadToSSL) {
  5113. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5114. }
  5115. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5116. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5117. #endif
  5118. AssertIntEQ(WOLFSSL_SUCCESS,
  5119. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5120. if (!callbacks->loadToSSL) {
  5121. AssertIntEQ(WOLFSSL_SUCCESS,
  5122. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5123. WOLFSSL_FILETYPE_PEM));
  5124. AssertIntEQ(WOLFSSL_SUCCESS,
  5125. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5126. WOLFSSL_FILETYPE_PEM));
  5127. }
  5128. if (callbacks->ctx_ready)
  5129. callbacks->ctx_ready(ctx);
  5130. ssl = wolfSSL_new(ctx);
  5131. if (wolfSSL_dtls(ssl)) {
  5132. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5133. 1, 0, ssl);
  5134. }
  5135. else {
  5136. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5137. 0, 0, ssl);
  5138. }
  5139. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  5140. if (callbacks->loadToSSL) {
  5141. wolfSSL_SetDevId(ssl, callbacks->devId);
  5142. AssertIntEQ(WOLFSSL_SUCCESS,
  5143. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5144. WOLFSSL_FILETYPE_PEM));
  5145. AssertIntEQ(WOLFSSL_SUCCESS,
  5146. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5147. WOLFSSL_FILETYPE_PEM));
  5148. }
  5149. if (callbacks->ssl_ready)
  5150. callbacks->ssl_ready(ssl);
  5151. #ifdef WOLFSSL_ASYNC_CRYPT
  5152. err = 0; /* Reset error */
  5153. #endif
  5154. do {
  5155. #ifdef WOLFSSL_ASYNC_CRYPT
  5156. if (err == WC_PENDING_E) {
  5157. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5158. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5159. }
  5160. #endif
  5161. ret = wolfSSL_connect(ssl);
  5162. err = wolfSSL_get_error(ssl, 0);
  5163. } while (err == WC_PENDING_E);
  5164. if (ret != WOLFSSL_SUCCESS) {
  5165. char buff[WOLFSSL_MAX_ERROR_SZ];
  5166. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5167. /*err_sys("SSL_connect failed");*/
  5168. }
  5169. else {
  5170. #ifdef WOLFSSL_ASYNC_CRYPT
  5171. err = 0; /* Reset error */
  5172. #endif
  5173. do {
  5174. #ifdef WOLFSSL_ASYNC_CRYPT
  5175. if (err == WC_PENDING_E) {
  5176. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5177. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5178. }
  5179. #endif
  5180. ret = wolfSSL_write(ssl, msg, len);
  5181. err = wolfSSL_get_error(ssl, 0);
  5182. } while (err == WC_PENDING_E);
  5183. AssertIntEQ(len, ret);
  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_read(ssl, input, sizeof(input)-1);
  5195. err = wolfSSL_get_error(ssl, 0);
  5196. } while (err == WC_PENDING_E);
  5197. if (ret > 0) {
  5198. input[ret] = '\0'; /* null term */
  5199. printf("Server response: %s\n", input);
  5200. }
  5201. ((func_args*)args)->return_code = TEST_SUCCESS;
  5202. }
  5203. if (callbacks->on_result)
  5204. callbacks->on_result(ssl);
  5205. wolfSSL_free(ssl);
  5206. wolfSSL_CTX_free(ctx);
  5207. CloseSocket(sfd);
  5208. #ifdef WOLFSSL_TIRTOS
  5209. fdCloseSession(Task_self());
  5210. #endif
  5211. }
  5212. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5213. static int test_wolfSSL_read_write(void)
  5214. {
  5215. /* The unit testing for read and write shall happen simultaneously, since
  5216. * one can't do anything with one without the other. (Except for a failure
  5217. * test case.) This function will call all the others that will set up,
  5218. * execute, and report their test findings.
  5219. *
  5220. * Set up the success case first. This function will become the template
  5221. * for the other tests. This should eventually be renamed
  5222. *
  5223. * The success case isn't interesting, how can this fail?
  5224. * - Do not give the client context a CA certificate. The connect should
  5225. * fail. Do not need server for this?
  5226. * - Using NULL for the ssl object on server. Do not need client for this.
  5227. * - Using NULL for the ssl object on client. Do not need server for this.
  5228. * - Good ssl objects for client and server. Client write() without server
  5229. * read().
  5230. * - Good ssl objects for client and server. Server write() without client
  5231. * read().
  5232. * - Forgetting the password callback?
  5233. */
  5234. tcp_ready ready;
  5235. func_args client_args;
  5236. func_args server_args;
  5237. THREAD_TYPE serverThread;
  5238. XMEMSET(&client_args, 0, sizeof(func_args));
  5239. XMEMSET(&server_args, 0, sizeof(func_args));
  5240. #ifdef WOLFSSL_TIRTOS
  5241. fdOpenSession(Task_self());
  5242. #endif
  5243. StartTCP();
  5244. InitTcpReady(&ready);
  5245. #if defined(USE_WINDOWS_API)
  5246. /* use RNG to get random port if using windows */
  5247. ready.port = GetRandomPort();
  5248. #endif
  5249. server_args.signal = &ready;
  5250. client_args.signal = &ready;
  5251. start_thread(test_server_nofail, &server_args, &serverThread);
  5252. wait_tcp_ready(&server_args);
  5253. test_client_nofail(&client_args, NULL);
  5254. join_thread(serverThread);
  5255. AssertTrue(client_args.return_code);
  5256. AssertTrue(server_args.return_code);
  5257. FreeTcpReady(&ready);
  5258. #ifdef WOLFSSL_TIRTOS
  5259. fdOpenSession(Task_self());
  5260. #endif
  5261. return 0;
  5262. }
  5263. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  5264. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  5265. {
  5266. /* The unit test for session resumption by re-using WOLFSSL object.
  5267. * WOLFSSL object is not cleared after first session. It re-use the obeject
  5268. * for second connection.
  5269. */
  5270. tcp_ready ready;
  5271. func_args client_args;
  5272. func_args server_args;
  5273. THREAD_TYPE serverThread;
  5274. XMEMSET(&client_args, 0, sizeof(func_args));
  5275. XMEMSET(&server_args, 0, sizeof(func_args));
  5276. #ifdef WOLFSSL_TIRTOS
  5277. fdOpenSession(Task_self());
  5278. #endif
  5279. StartTCP();
  5280. InitTcpReady(&ready);
  5281. #if defined(USE_WINDOWS_API)
  5282. /* use RNG to get random port if using windows */
  5283. ready.port = GetRandomPort();
  5284. #endif
  5285. server_args.signal = &ready;
  5286. client_args.signal = &ready;
  5287. /* the var is used for loop number */
  5288. server_args.argc = 2;
  5289. start_thread(test_server_loop, &server_args, &serverThread);
  5290. wait_tcp_ready(&server_args);
  5291. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5292. join_thread(serverThread);
  5293. AssertTrue(client_args.return_code);
  5294. AssertTrue(server_args.return_code);
  5295. FreeTcpReady(&ready);
  5296. #ifdef WOLFSSL_TIRTOS
  5297. fdOpenSession(Task_self());
  5298. #endif
  5299. return 0;
  5300. }
  5301. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5302. static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5303. {
  5304. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5305. /* This unit test is to check set verify Depth */
  5306. tcp_ready ready;
  5307. func_args client_args;
  5308. func_args server_args;
  5309. THREAD_TYPE serverThread;
  5310. callback_functions client_cbf;
  5311. XMEMSET(&client_args, 0, sizeof(func_args));
  5312. XMEMSET(&server_args, 0, sizeof(func_args));
  5313. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5314. printf(testingFmt, "test_wolfSSL_CTX_verifyDepth_ServerClient()\n");
  5315. #ifdef WOLFSSL_TLS13
  5316. client_cbf.method = wolfTLSv1_3_client_method;
  5317. #endif /* WOLFSSL_TLS13 */
  5318. client_args.callbacks = &client_cbf;
  5319. StartTCP();
  5320. InitTcpReady(&ready);
  5321. #if defined(USE_WINDOWS_API)
  5322. /* use RNG to get random port if using windows */
  5323. ready.port = GetRandomPort();
  5324. #endif
  5325. server_args.signal = &ready;
  5326. client_args.signal = &ready;
  5327. /* the var is used for loop number */
  5328. server_args.argc = 1;
  5329. /* test case 1 verify depth is equal to peer chain */
  5330. {
  5331. start_thread(test_server_nofail, &server_args, &serverThread);
  5332. wait_tcp_ready(&server_args);
  5333. /* the var is used for verify depth */
  5334. client_args.argc = 2;
  5335. test_client_verifyDepth(&client_args);
  5336. join_thread(serverThread);
  5337. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5338. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5339. }
  5340. /* test case 2
  5341. * verify depth is zero, number of peer's chain is 2.
  5342. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5343. * callback.
  5344. */
  5345. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5346. {
  5347. start_thread(test_server_nofail, &server_args, &serverThread);
  5348. wait_tcp_ready(&server_args);
  5349. client_args.argc = 0;
  5350. test_client_verifyDepth(&client_args);
  5351. join_thread(serverThread);
  5352. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5353. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5354. }
  5355. /* test case 3
  5356. * verify depth is zero, number of peer's chain is 2
  5357. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5358. * therefore, handshake becomes failure.
  5359. */
  5360. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5361. {
  5362. start_thread(test_server_nofail, &server_args, &serverThread);
  5363. wait_tcp_ready(&server_args);
  5364. client_args.argc = -1;
  5365. test_client_verifyDepth(&client_args);
  5366. join_thread(serverThread);
  5367. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5368. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5369. }
  5370. FreeTcpReady(&ready);
  5371. printf(resultFmt, passed);
  5372. #else
  5373. (void)test_client_verifyDepth;
  5374. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5375. return 0;
  5376. }
  5377. static int test_client_get_finished(void* args, cbType cb)
  5378. {
  5379. #if defined(WOLFSSL_HAVE_TLS_UNIQUE) && !defined(NO_WOLFSSL_CLIENT)
  5380. SOCKET_T sockfd = 0;
  5381. callback_functions* cbf;
  5382. WOLFSSL_CTX* ctx = 0;
  5383. WOLFSSL* ssl = 0;
  5384. char msg[64] = "hello wolfssl!";
  5385. char reply[1024];
  5386. int msgSz = (int)XSTRLEN(msg);
  5387. int ret, err = 0;
  5388. WOLFSSL_METHOD* method = NULL;
  5389. size_t msg_len = 0;
  5390. (void) args;
  5391. (void) cb;
  5392. ((func_args*)args)->return_code = TEST_FAIL;
  5393. cbf = ((func_args*)args)->callbacks;
  5394. if (cbf != NULL && cbf->method != NULL) {
  5395. method = cbf->method();
  5396. }
  5397. else {
  5398. method = wolfSSLv23_client_method();
  5399. }
  5400. ctx = wolfSSL_CTX_new(method);
  5401. /* Do connect here so server detects failures */
  5402. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5403. 0, 0, NULL);
  5404. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5405. {
  5406. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5407. goto done;
  5408. }
  5409. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5410. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5411. goto done;
  5412. }
  5413. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5414. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5415. goto done;
  5416. }
  5417. /* call ctx setup callback */
  5418. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5419. cbf->ctx_ready(ctx);
  5420. }
  5421. ssl = wolfSSL_new(ctx);
  5422. if (ssl == NULL) {
  5423. goto done;
  5424. }
  5425. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5426. goto done;
  5427. }
  5428. /* call ssl setup callback */
  5429. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5430. cbf->ssl_ready(ssl);
  5431. }
  5432. #ifdef WOLFSSL_ASYNC_CRYPT
  5433. err = 0; /* Reset error */
  5434. #endif
  5435. do {
  5436. #ifdef WOLFSSL_ASYNC_CRYPT
  5437. if (err == WC_PENDING_E) {
  5438. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5439. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5440. }
  5441. #endif
  5442. ret = wolfSSL_connect(ssl);
  5443. err = wolfSSL_get_error(ssl, 0);
  5444. } while (err == WC_PENDING_E);
  5445. if (ret != WOLFSSL_SUCCESS) {
  5446. char buff[WOLFSSL_MAX_ERROR_SZ];
  5447. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5448. goto done;
  5449. }
  5450. /* get_finished test */
  5451. /* 1. get own sent message */
  5452. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  5453. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  5454. AssertIntGE(msg_len, 0);
  5455. /* 2. get peer message */
  5456. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  5457. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  5458. AssertIntGE(msg_len, 0);
  5459. if (cb != NULL)
  5460. (cb)(ctx, ssl);
  5461. #ifdef WOLFSSL_ASYNC_CRYPT
  5462. err = 0; /* Reset error */
  5463. #endif
  5464. do {
  5465. #ifdef WOLFSSL_ASYNC_CRYPT
  5466. if (err == WC_PENDING_E) {
  5467. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5468. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5469. }
  5470. #endif
  5471. ret = wolfSSL_write(ssl, msg, msgSz);
  5472. err = wolfSSL_get_error(ssl, 0);
  5473. } while (err == WC_PENDING_E);
  5474. if (ret != msgSz) {
  5475. /*err_sys("SSL_write failed");*/
  5476. goto done;
  5477. }
  5478. #ifdef WOLFSSL_ASYNC_CRYPT
  5479. err = 0; /* Reset error */
  5480. #endif
  5481. do {
  5482. #ifdef WOLFSSL_ASYNC_CRYPT
  5483. if (err == WC_PENDING_E) {
  5484. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5485. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5486. }
  5487. #endif
  5488. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5489. err = wolfSSL_get_error(ssl, 0);
  5490. } while (err == WC_PENDING_E);
  5491. if (ret > 0) {
  5492. reply[ret] = '\0';
  5493. printf("Server response: %s\n", reply);
  5494. }
  5495. ((func_args*)args)->return_code = TEST_SUCCESS;
  5496. done:
  5497. wolfSSL_free(ssl);
  5498. wolfSSL_CTX_free(ctx);
  5499. CloseSocket(sockfd);
  5500. #else
  5501. (void)args;
  5502. (void)cb;
  5503. #endif /* WOLFSSL_HAVE_TLS_UNIQUE && !NO_WOLFSSL_CLIENT */
  5504. return 0;
  5505. }
  5506. static int test_wolfSSL_get_finished(void)
  5507. {
  5508. #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
  5509. tcp_ready ready;
  5510. func_args client_args;
  5511. func_args server_args;
  5512. THREAD_TYPE serverThread;
  5513. XMEMSET(&client_args, 0, sizeof(func_args));
  5514. XMEMSET(&server_args, 0, sizeof(func_args));
  5515. StartTCP();
  5516. InitTcpReady(&ready);
  5517. #if defined(USE_WINDOWS_API)
  5518. /* use RNG to get random port if using windows */
  5519. ready.port = GetRandomPort();
  5520. #endif
  5521. server_args.signal = &ready;
  5522. client_args.signal = &ready;
  5523. start_thread(test_server_nofail, &server_args, &serverThread);
  5524. wait_tcp_ready(&server_args);
  5525. test_client_get_finished(&client_args, NULL);
  5526. join_thread(serverThread);
  5527. AssertTrue(client_args.return_code);
  5528. AssertTrue(server_args.return_code);
  5529. /* test received msg vs sent msg */
  5530. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  5531. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  5532. FreeTcpReady(&ready);
  5533. #else
  5534. (void)test_client_get_finished;
  5535. #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
  5536. return 0;
  5537. }
  5538. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  5539. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  5540. !defined(NO_SESSION_CACHE)
  5541. /* Sessions to restore/store */
  5542. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  5543. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  5544. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  5545. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  5546. {
  5547. /* Don't store sessions. Lookup is still enabled. */
  5548. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  5549. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  5550. /* Require both peers to provide certs */
  5551. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  5552. }
  5553. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  5554. {
  5555. WOLFSSL_SESSION** sess;
  5556. if (wolfSSL_is_server(ssl))
  5557. sess = &test_wolfSSL_CTX_add_session_server_sess;
  5558. else
  5559. sess = &test_wolfSSL_CTX_add_session_client_sess;
  5560. if (*sess == NULL) {
  5561. #ifdef NO_SESSION_CACHE_REF
  5562. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  5563. #else
  5564. /* Test for backwards compatibility */
  5565. if (wolfSSL_is_server(ssl)) {
  5566. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  5567. }
  5568. else {
  5569. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  5570. }
  5571. #endif
  5572. /* Now save the session in the internal store to make it available
  5573. * for lookup. For TLS 1.3, we can't save the session without
  5574. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  5575. * session from cache. */
  5576. if (wolfSSL_is_server(ssl)
  5577. #ifndef WOLFSSL_TICKET_HAVE_ID
  5578. && wolfSSL_version(ssl) != TLS1_3_VERSION
  5579. #endif
  5580. )
  5581. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  5582. *sess), WOLFSSL_SUCCESS);
  5583. }
  5584. else {
  5585. /* If we have a session retrieved then remaining connections should be
  5586. * resuming on that session */
  5587. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  5588. }
  5589. /* Save CTX to be able to decrypt tickets */
  5590. if (wolfSSL_is_server(ssl) &&
  5591. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  5592. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  5593. = wolfSSL_get_SSL_CTX(ssl));
  5594. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  5595. WOLFSSL_SUCCESS);
  5596. }
  5597. #ifdef SESSION_CERTS
  5598. #ifndef WOLFSSL_TICKET_HAVE_ID
  5599. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  5600. wolfSSL_session_reused(ssl))
  5601. #endif
  5602. {
  5603. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  5604. * for all connections. TLS 1.3 only has tickets so if we don't
  5605. * include the session id in the ticket then the certificates
  5606. * will not be available on resumption. */
  5607. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  5608. AssertNotNull(peer);
  5609. wolfSSL_X509_free(peer);
  5610. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  5611. AssertNotNull(wolfSSL_SESSION_get0_peer(*sess));
  5612. }
  5613. #endif
  5614. }
  5615. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  5616. {
  5617. /* Set the session to reuse for the client */
  5618. AssertIntEQ(wolfSSL_set_session(ssl,
  5619. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  5620. }
  5621. #endif
  5622. static int test_wolfSSL_CTX_add_session(void)
  5623. {
  5624. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  5625. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  5626. !defined(NO_SESSION_CACHE)
  5627. tcp_ready ready;
  5628. func_args client_args;
  5629. func_args server_args;
  5630. THREAD_TYPE serverThread;
  5631. callback_functions client_cb;
  5632. callback_functions server_cb;
  5633. method_provider methods[][2] = {
  5634. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  5635. !defined(NO_DES3))
  5636. /* Without AES there are almost no ciphersuites available. This leads
  5637. * to no ciphersuites being available and an error. */
  5638. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  5639. #endif
  5640. #ifndef WOLFSSL_NO_TLS12
  5641. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  5642. #endif
  5643. /* Needs the default ticket callback since it is tied to the
  5644. * connection context and this makes it easy to carry over the ticket
  5645. * crypto context between connections */
  5646. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  5647. defined(HAVE_SESSION_TICKET)
  5648. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  5649. #endif
  5650. };
  5651. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  5652. size_t i, j;
  5653. printf(testingFmt, "wolfSSL_CTX_add_session()");
  5654. for (i = 0; i < methodsLen; i++) {
  5655. /* First run creates a connection while the second+ run will attempt
  5656. * to resume the connection. The trick is that the internal cache
  5657. * is turned off. wolfSSL_CTX_add_session should put the session in
  5658. * the cache anyway. */
  5659. test_wolfSSL_CTX_add_session_client_sess = NULL;
  5660. test_wolfSSL_CTX_add_session_server_sess = NULL;
  5661. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  5662. for (j = 0; j < 5; j++) {
  5663. #ifdef WOLFSSL_TIRTOS
  5664. fdOpenSession(Task_self());
  5665. #endif
  5666. StartTCP();
  5667. InitTcpReady(&ready);
  5668. XMEMSET(&client_args, 0, sizeof(func_args));
  5669. XMEMSET(&server_args, 0, sizeof(func_args));
  5670. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  5671. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  5672. client_cb.method = methods[i][0];
  5673. server_cb.method = methods[i][1];
  5674. server_args.signal = &ready;
  5675. server_args.callbacks = &server_cb;
  5676. client_args.signal = &ready;
  5677. client_args.callbacks = &client_cb;
  5678. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  5679. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  5680. server_cb.isSharedCtx = 1;
  5681. }
  5682. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  5683. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  5684. if (j != 0)
  5685. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  5686. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  5687. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  5688. server_cb.ticNoInit = 1; /* Use default builtin */
  5689. start_thread(test_server_nofail, &server_args, &serverThread);
  5690. wait_tcp_ready(&server_args);
  5691. test_client_nofail(&client_args, NULL);
  5692. join_thread(serverThread);
  5693. AssertTrue(client_args.return_code);
  5694. AssertTrue(server_args.return_code);
  5695. FreeTcpReady(&ready);
  5696. }
  5697. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  5698. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  5699. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  5700. }
  5701. printf(resultFmt, passed);
  5702. #endif
  5703. return 0;
  5704. }
  5705. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  5706. /* canned export of a session using older version 3 */
  5707. static unsigned char version_3[] = {
  5708. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  5709. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  5710. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  5711. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  5712. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  5713. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5714. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  5715. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  5716. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  5717. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5718. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  5719. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  5720. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5721. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  5722. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  5723. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  5724. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  5725. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  5726. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  5727. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  5728. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5729. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5730. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5731. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5732. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5733. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5734. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5735. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5736. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5737. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5738. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5739. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  5740. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  5741. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  5742. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  5743. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  5744. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  5745. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  5746. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  5747. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  5748. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  5749. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5750. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  5751. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  5752. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5753. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  5754. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  5755. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  5756. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  5757. 0x2E, 0x31, 0xED, 0x4F
  5758. };
  5759. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  5760. static int test_wolfSSL_dtls_export(void)
  5761. {
  5762. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  5763. tcp_ready ready;
  5764. func_args client_args;
  5765. func_args server_args;
  5766. THREAD_TYPE serverThread;
  5767. callback_functions server_cbf;
  5768. callback_functions client_cbf;
  5769. #ifdef WOLFSSL_TIRTOS
  5770. fdOpenSession(Task_self());
  5771. #endif
  5772. InitTcpReady(&ready);
  5773. #if defined(USE_WINDOWS_API)
  5774. /* use RNG to get random port if using windows */
  5775. ready.port = GetRandomPort();
  5776. #endif
  5777. /* set using dtls */
  5778. XMEMSET(&client_args, 0, sizeof(func_args));
  5779. XMEMSET(&server_args, 0, sizeof(func_args));
  5780. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  5781. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5782. server_cbf.method = wolfDTLSv1_2_server_method;
  5783. client_cbf.method = wolfDTLSv1_2_client_method;
  5784. server_args.callbacks = &server_cbf;
  5785. client_args.callbacks = &client_cbf;
  5786. server_args.signal = &ready;
  5787. client_args.signal = &ready;
  5788. start_thread(run_wolfssl_server, &server_args, &serverThread);
  5789. wait_tcp_ready(&server_args);
  5790. run_wolfssl_client(&client_args);
  5791. join_thread(serverThread);
  5792. AssertTrue(client_args.return_code);
  5793. AssertTrue(server_args.return_code);
  5794. FreeTcpReady(&ready);
  5795. #ifdef WOLFSSL_TIRTOS
  5796. fdOpenSession(Task_self());
  5797. #endif
  5798. {
  5799. SOCKET_T sockfd = 0;
  5800. WOLFSSL_CTX* ctx;
  5801. WOLFSSL* ssl;
  5802. char msg[64] = "hello wolfssl!";
  5803. char reply[1024];
  5804. int msgSz = (int)XSTRLEN(msg);
  5805. byte *session, *window;
  5806. unsigned int sessionSz, windowSz;
  5807. #ifndef TEST_IPV6
  5808. struct sockaddr_in peerAddr;
  5809. #else
  5810. struct sockaddr_in6 peerAddr;
  5811. #endif /* TEST_IPV6 */
  5812. int i;
  5813. /* Set ctx to DTLS 1.2 */
  5814. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  5815. AssertNotNull(ssl = wolfSSL_new(ctx));
  5816. /* test importing version 3 */
  5817. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5818. /* test importing bad length and bad version */
  5819. version_3[2] += 1;
  5820. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5821. version_3[2] -= 1; version_3[1] = 0XA0;
  5822. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5823. wolfSSL_free(ssl);
  5824. wolfSSL_CTX_free(ctx);
  5825. /* check storing client state after connection and storing window only */
  5826. #ifdef WOLFSSL_TIRTOS
  5827. fdOpenSession(Task_self());
  5828. #endif
  5829. InitTcpReady(&ready);
  5830. #if defined(USE_WINDOWS_API)
  5831. /* use RNG to get random port if using windows */
  5832. ready.port = GetRandomPort();
  5833. #endif
  5834. /* set using dtls */
  5835. XMEMSET(&server_args, 0, sizeof(func_args));
  5836. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  5837. server_cbf.method = wolfDTLSv1_2_server_method;
  5838. server_cbf.doUdp = 1;
  5839. server_args.callbacks = &server_cbf;
  5840. server_args.argc = 3; /* set loop_count to 3 */
  5841. server_args.signal = &ready;
  5842. start_thread(test_server_nofail, &server_args, &serverThread);
  5843. wait_tcp_ready(&server_args);
  5844. /* create and connect with client */
  5845. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  5846. AssertIntEQ(WOLFSSL_SUCCESS,
  5847. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  5848. AssertIntEQ(WOLFSSL_SUCCESS,
  5849. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  5850. AssertIntEQ(WOLFSSL_SUCCESS,
  5851. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  5852. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  5853. AssertNotNull(ssl = wolfSSL_new(ctx));
  5854. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  5855. /* store server information connected too */
  5856. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  5857. #ifndef TEST_IPV6
  5858. peerAddr.sin_family = AF_INET;
  5859. AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  5860. peerAddr.sin_port = XHTONS(server_args.signal->port);
  5861. #else
  5862. peerAddr.sin6_family = AF_INET6;
  5863. AssertIntEQ(
  5864. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  5865. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  5866. #endif
  5867. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  5868. WOLFSSL_SUCCESS);
  5869. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  5870. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  5871. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5872. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  5873. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  5874. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  5875. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  5876. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5877. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  5878. wolfSSL_free(ssl);
  5879. for (i = 1; i < server_args.argc; i++) {
  5880. /* restore state */
  5881. AssertNotNull(ssl = wolfSSL_new(ctx));
  5882. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  5883. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  5884. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  5885. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  5886. WOLFSSL_SUCCESS);
  5887. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  5888. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  5889. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  5890. wolfSSL_free(ssl);
  5891. }
  5892. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5893. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5894. wolfSSL_CTX_free(ctx);
  5895. printf("done and waiting for server\n");
  5896. join_thread(serverThread);
  5897. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5898. FreeTcpReady(&ready);
  5899. #ifdef WOLFSSL_TIRTOS
  5900. fdOpenSession(Task_self());
  5901. #endif
  5902. }
  5903. printf(testingFmt, "wolfSSL_dtls_export()");
  5904. printf(resultFmt, passed);
  5905. #endif
  5906. return 0;
  5907. }
  5908. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  5909. #ifdef WOLFSSL_TLS13
  5910. static const byte canned_client_tls13_session[] = {
  5911. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  5912. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  5913. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  5914. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5915. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  5916. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5917. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  5918. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  5919. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  5920. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5921. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5922. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  5923. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  5924. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  5925. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  5926. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  5927. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  5928. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  5929. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  5930. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  5931. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  5932. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  5933. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  5934. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  5935. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  5936. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  5937. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  5938. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  5939. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5940. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  5941. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  5942. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  5943. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  5944. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  5945. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5946. 0x00, 0x03
  5947. };
  5948. static const byte canned_server_tls13_session[] = {
  5949. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  5950. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  5951. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  5952. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5953. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5954. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5955. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  5956. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  5957. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  5958. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5959. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5960. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  5961. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  5962. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  5963. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  5964. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  5965. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  5966. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  5967. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  5968. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  5969. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  5970. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  5971. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  5972. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  5973. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  5974. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  5975. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  5976. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  5977. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5978. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  5979. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  5980. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  5981. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  5982. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  5983. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5984. 0x00, 0x04
  5985. };
  5986. #endif /* WOLFSSL_TLS13 */
  5987. static const byte canned_client_session[] = {
  5988. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  5989. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  5990. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  5991. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5992. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  5993. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5994. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  5995. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  5996. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  5997. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5998. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5999. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  6000. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6001. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6002. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6003. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6004. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6005. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6006. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6007. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6008. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6009. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6010. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6011. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6012. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6013. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6014. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6015. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6016. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6017. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6018. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6019. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6020. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6021. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6022. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6023. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6024. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6025. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6026. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6027. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6028. 0x00, 0x03
  6029. };
  6030. static const byte canned_server_session[] = {
  6031. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6032. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6033. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6034. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6035. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6036. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6037. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6038. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  6039. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6040. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6041. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6042. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  6043. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6044. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6045. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6046. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6047. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6048. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6049. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6050. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6051. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6052. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6053. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6054. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6055. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6056. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6057. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6058. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6059. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6060. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6061. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6062. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6063. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6064. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6065. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6066. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6067. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6068. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6069. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6070. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6071. 0x00, 0x04
  6072. };
  6073. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  6074. {
  6075. SOCKET_T sockfd = 0;
  6076. SOCKET_T clientfd = 0;
  6077. word16 port;
  6078. callback_functions* cbf;
  6079. WOLFSSL_CTX* ctx = 0;
  6080. WOLFSSL* ssl = 0;
  6081. char msg[] = "I hear you fa shizzle!";
  6082. char input[1024];
  6083. int idx;
  6084. #ifdef WOLFSSL_TIRTOS
  6085. fdOpenSession(Task_self());
  6086. #endif
  6087. ((func_args*)args)->return_code = TEST_FAIL;
  6088. cbf = ((func_args*)args)->callbacks;
  6089. {
  6090. WOLFSSL_METHOD* method = NULL;
  6091. if (cbf != NULL && cbf->method != NULL) {
  6092. method = cbf->method();
  6093. }
  6094. else {
  6095. method = wolfTLSv1_2_server_method();
  6096. }
  6097. ctx = wolfSSL_CTX_new(method);
  6098. }
  6099. if (ctx == NULL) {
  6100. goto done;
  6101. }
  6102. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6103. #if defined(USE_WINDOWS_API)
  6104. port = ((func_args*)args)->signal->port;
  6105. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6106. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6107. /* Let tcp_listen assign port */
  6108. port = 0;
  6109. #else
  6110. /* Use default port */
  6111. port = wolfSSLPort;
  6112. #endif
  6113. /* do it here to detect failure */
  6114. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6115. CloseSocket(sockfd);
  6116. /* call ctx setup callback */
  6117. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6118. cbf->ctx_ready(ctx);
  6119. }
  6120. ssl = wolfSSL_new(ctx);
  6121. if (ssl == NULL) {
  6122. goto done;
  6123. }
  6124. wolfSSL_set_fd(ssl, clientfd);
  6125. /* call ssl setup callback */
  6126. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6127. cbf->ssl_ready(ssl);
  6128. }
  6129. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6130. if (idx > 0) {
  6131. input[idx] = '\0';
  6132. printf("Client message export/import: %s\n", input);
  6133. }
  6134. else {
  6135. printf("ret = %d error = %d\n", idx, wolfSSL_get_error(ssl, idx));
  6136. goto done;
  6137. }
  6138. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6139. /*err_sys("SSL_write failed");*/
  6140. #ifdef WOLFSSL_TIRTOS
  6141. return;
  6142. #else
  6143. return 0;
  6144. #endif
  6145. }
  6146. #ifdef WOLFSSL_TIRTOS
  6147. Task_yield();
  6148. #endif
  6149. ((func_args*)args)->return_code = TEST_SUCCESS;
  6150. done:
  6151. wolfSSL_shutdown(ssl);
  6152. wolfSSL_free(ssl);
  6153. wolfSSL_CTX_free(ctx);
  6154. CloseSocket(clientfd);
  6155. #ifdef WOLFSSL_TIRTOS
  6156. fdCloseSession(Task_self());
  6157. #endif
  6158. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6159. && defined(HAVE_THREAD_LS)
  6160. wc_ecc_fp_free(); /* free per thread cache */
  6161. #endif
  6162. #if defined(HAVE_SESSION_TICKET) && \
  6163. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6164. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  6165. OpenSSLTicketCleanup();
  6166. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6167. TicketCleanup();
  6168. #endif
  6169. #endif
  6170. #ifndef WOLFSSL_TIRTOS
  6171. return 0;
  6172. #endif
  6173. }
  6174. static void load_tls12_canned_server(WOLFSSL* ssl)
  6175. {
  6176. int clientfd = wolfSSL_get_fd(ssl);
  6177. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  6178. sizeof(canned_server_session)), sizeof(canned_server_session));
  6179. wolfSSL_set_fd(ssl, clientfd);
  6180. }
  6181. #ifdef WOLFSSL_TLS13
  6182. static void load_tls13_canned_server(WOLFSSL* ssl)
  6183. {
  6184. int clientfd = wolfSSL_get_fd(ssl);
  6185. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  6186. sizeof(canned_server_tls13_session)),
  6187. sizeof(canned_server_tls13_session));
  6188. wolfSSL_set_fd(ssl, clientfd);
  6189. }
  6190. #endif
  6191. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  6192. static int test_wolfSSL_tls_export_run(int v)
  6193. {
  6194. SOCKET_T sockfd = 0;
  6195. WOLFSSL_CTX* ctx = 0;
  6196. WOLFSSL* ssl = 0;
  6197. char msg[64] = "hello wolfssl!";
  6198. char reply[1024];
  6199. word32 replySz;
  6200. int msgSz = (int)XSTRLEN(msg);
  6201. const byte* clientSession = NULL;
  6202. int clientSessionSz = 0;
  6203. tcp_ready ready;
  6204. func_args server_args;
  6205. THREAD_TYPE serverThread;
  6206. callback_functions server_cbf;
  6207. #ifdef WOLFSSL_TIRTOS
  6208. fdOpenSession(Task_self());
  6209. #endif
  6210. InitTcpReady(&ready);
  6211. #if defined(USE_WINDOWS_API)
  6212. /* use RNG to get random port if using windows */
  6213. ready.port = GetRandomPort();
  6214. #endif
  6215. XMEMSET(&server_args, 0, sizeof(func_args));
  6216. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6217. switch (v) {
  6218. case WOLFSSL_TLSV1_2:
  6219. server_cbf.method = wolfTLSv1_2_server_method;
  6220. server_cbf.ssl_ready = load_tls12_canned_server;
  6221. /* setup the client side */
  6222. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  6223. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6224. clientSession = canned_client_session;
  6225. clientSessionSz = sizeof(canned_client_session);
  6226. break;
  6227. #ifdef WOLFSSL_TLS13
  6228. case WOLFSSL_TLSV1_3:
  6229. server_cbf.method = wolfTLSv1_3_server_method;
  6230. server_cbf.ssl_ready = load_tls13_canned_server;
  6231. /* setup the client side */
  6232. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  6233. clientSession = canned_client_tls13_session;
  6234. clientSessionSz = sizeof(canned_client_tls13_session);
  6235. break;
  6236. #endif
  6237. }
  6238. server_args.callbacks = &server_cbf;
  6239. server_args.signal = &ready;
  6240. start_thread(tls_export_server, &server_args, &serverThread);
  6241. wait_tcp_ready(&server_args);
  6242. #ifdef WOLFSSL_TIRTOS
  6243. fdOpenSession(Task_self());
  6244. #endif
  6245. AssertNotNull(ssl = wolfSSL_new(ctx));
  6246. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  6247. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  6248. clientSessionSz);
  6249. replySz = sizeof(reply);
  6250. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  6251. #if !defined(NO_PSK) && defined(HAVE_ANON)
  6252. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  6253. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  6254. #endif
  6255. wolfSSL_set_fd(ssl, sockfd);
  6256. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6257. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  6258. wolfSSL_free(ssl);
  6259. wolfSSL_CTX_free(ctx);
  6260. CloseSocket(sockfd);
  6261. #ifdef WOLFSSL_TIRTOS
  6262. fdCloseSession(Task_self());
  6263. #endif
  6264. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6265. && defined(HAVE_THREAD_LS)
  6266. wc_ecc_fp_free(); /* free per thread cache */
  6267. #endif
  6268. join_thread(serverThread);
  6269. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6270. FreeTcpReady(&ready);
  6271. #ifdef WOLFSSL_TIRTOS
  6272. fdOpenSession(Task_self());
  6273. #endif
  6274. return 0;
  6275. }
  6276. #endif
  6277. static int test_wolfSSL_tls_export(void)
  6278. {
  6279. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6280. printf(testingFmt, "wolfSSL_tls_export()");
  6281. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  6282. #ifdef WOLFSSL_TLS13
  6283. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  6284. #endif
  6285. printf(resultFmt, passed);
  6286. #endif
  6287. return 0;
  6288. }
  6289. /*----------------------------------------------------------------------------*
  6290. | TLS extensions tests
  6291. *----------------------------------------------------------------------------*/
  6292. #ifdef ENABLE_TLS_CALLBACK_TEST
  6293. /* Connection test runner - generic */
  6294. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  6295. callback_functions* server_callbacks)
  6296. {
  6297. tcp_ready ready;
  6298. func_args client_args;
  6299. func_args server_args;
  6300. THREAD_TYPE serverThread;
  6301. XMEMSET(&client_args, 0, sizeof(func_args));
  6302. XMEMSET(&server_args, 0, sizeof(func_args));
  6303. StartTCP();
  6304. client_args.callbacks = client_callbacks;
  6305. server_args.callbacks = server_callbacks;
  6306. #ifdef WOLFSSL_TIRTOS
  6307. fdOpenSession(Task_self());
  6308. #endif
  6309. /* RUN Server side */
  6310. InitTcpReady(&ready);
  6311. #if defined(USE_WINDOWS_API)
  6312. /* use RNG to get random port if using windows */
  6313. ready.port = GetRandomPort();
  6314. #endif
  6315. server_args.signal = &ready;
  6316. client_args.signal = &ready;
  6317. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6318. wait_tcp_ready(&server_args);
  6319. /* RUN Client side */
  6320. run_wolfssl_client(&client_args);
  6321. join_thread(serverThread);
  6322. FreeTcpReady(&ready);
  6323. #ifdef WOLFSSL_TIRTOS
  6324. fdCloseSession(Task_self());
  6325. #endif
  6326. client_callbacks->return_code = client_args.return_code;
  6327. server_callbacks->return_code = server_args.return_code;
  6328. }
  6329. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6330. #ifdef HAVE_SNI
  6331. static int test_wolfSSL_UseSNI_params(void)
  6332. {
  6333. #if !defined(NO_WOLFSSL_CLIENT)
  6334. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6335. WOLFSSL *ssl = wolfSSL_new(ctx);
  6336. AssertNotNull(ctx);
  6337. AssertNotNull(ssl);
  6338. /* invalid [ctx|ssl] */
  6339. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  6340. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  6341. /* invalid type */
  6342. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  6343. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  6344. /* invalid data */
  6345. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  6346. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  6347. /* success case */
  6348. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  6349. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  6350. wolfSSL_free(ssl);
  6351. wolfSSL_CTX_free(ctx);
  6352. #endif /* !NO_WOLFSSL_CLIENT */
  6353. return 0;
  6354. }
  6355. /* BEGIN of connection tests callbacks */
  6356. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6357. {
  6358. AssertIntEQ(WOLFSSL_SUCCESS,
  6359. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6360. }
  6361. static void use_SNI_at_ssl(WOLFSSL* ssl)
  6362. {
  6363. AssertIntEQ(WOLFSSL_SUCCESS,
  6364. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6365. }
  6366. static void different_SNI_at_ssl(WOLFSSL* ssl)
  6367. {
  6368. AssertIntEQ(WOLFSSL_SUCCESS,
  6369. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  6370. }
  6371. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  6372. {
  6373. use_SNI_at_ssl(ssl);
  6374. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6375. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  6376. }
  6377. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  6378. {
  6379. use_SNI_at_ssl(ssl);
  6380. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6381. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  6382. }
  6383. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6384. {
  6385. use_SNI_at_ctx(ctx);
  6386. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6387. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6388. }
  6389. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  6390. {
  6391. use_SNI_at_ssl(ssl);
  6392. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6393. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6394. }
  6395. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6396. {
  6397. use_SNI_at_ctx(ctx);
  6398. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6399. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6400. }
  6401. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  6402. {
  6403. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  6404. }
  6405. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  6406. {
  6407. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  6408. }
  6409. static void verify_SNI_no_matching(WOLFSSL* ssl)
  6410. {
  6411. byte type = WOLFSSL_SNI_HOST_NAME;
  6412. char* request = (char*) &type; /* to be overwritten */
  6413. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  6414. AssertNotNull(request);
  6415. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  6416. AssertNull(request);
  6417. }
  6418. static void verify_SNI_real_matching(WOLFSSL* ssl)
  6419. {
  6420. byte type = WOLFSSL_SNI_HOST_NAME;
  6421. char* request = NULL;
  6422. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  6423. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  6424. AssertNotNull(request);
  6425. AssertStrEQ("www.wolfssl.com", request);
  6426. }
  6427. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  6428. {
  6429. byte type = WOLFSSL_SNI_HOST_NAME;
  6430. char* request = NULL;
  6431. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  6432. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
  6433. AssertNotNull(request);
  6434. AssertStrEQ("ww2.wolfssl.com", request);
  6435. }
  6436. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  6437. {
  6438. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  6439. }
  6440. /* END of connection tests callbacks */
  6441. static int test_wolfSSL_UseSNI_connection(void)
  6442. {
  6443. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  6444. callback_functions client_cb;
  6445. callback_functions server_cb;
  6446. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6447. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6448. client_cb.method = wolfSSLv23_client_method;
  6449. server_cb.method = wolfSSLv23_server_method;
  6450. client_cb.devId = devId;
  6451. server_cb.devId = devId;
  6452. /* success case at ctx */
  6453. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6454. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  6455. test_wolfSSL_client_server(&client_cb, &server_cb);
  6456. /* success case at ssl */
  6457. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  6458. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  6459. test_wolfSSL_client_server(&client_cb, &server_cb);
  6460. /* default mismatch behavior */
  6461. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6462. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  6463. test_wolfSSL_client_server(&client_cb, &server_cb);
  6464. /* continue on mismatch */
  6465. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6466. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  6467. test_wolfSSL_client_server(&client_cb, &server_cb);
  6468. /* fake answer on mismatch */
  6469. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6470. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  6471. test_wolfSSL_client_server(&client_cb, &server_cb);
  6472. /* sni abort - success */
  6473. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6474. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  6475. test_wolfSSL_client_server(&client_cb, &server_cb);
  6476. /* sni abort - abort when absent (ctx) */
  6477. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6478. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6479. test_wolfSSL_client_server(&client_cb, &server_cb);
  6480. /* sni abort - abort when absent (ssl) */
  6481. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6482. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6483. test_wolfSSL_client_server(&client_cb, &server_cb);
  6484. /* sni abort - success when overwritten */
  6485. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6486. 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;
  6487. test_wolfSSL_client_server(&client_cb, &server_cb);
  6488. /* sni abort - success when allowing mismatches */
  6489. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6490. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  6491. test_wolfSSL_client_server(&client_cb, &server_cb);
  6492. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  6493. return 0;
  6494. }
  6495. static int test_wolfSSL_SNI_GetFromBuffer(void)
  6496. {
  6497. byte buff[] = { /* www.paypal.com */
  6498. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  6499. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  6500. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  6501. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  6502. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  6503. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  6504. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  6505. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  6506. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  6507. };
  6508. byte buff2[] = { /* api.textmate.org */
  6509. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  6510. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  6511. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  6512. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  6513. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  6514. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  6515. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  6516. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  6517. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  6518. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  6519. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  6520. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  6521. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  6522. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  6523. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  6524. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  6525. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  6526. };
  6527. byte buff3[] = { /* no sni extension */
  6528. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  6529. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  6530. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  6531. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  6532. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  6533. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  6534. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  6535. };
  6536. byte buff4[] = { /* last extension has zero size */
  6537. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  6538. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  6539. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  6540. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  6541. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  6542. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  6543. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  6544. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  6545. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  6546. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  6547. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  6548. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  6549. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  6550. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  6551. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  6552. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  6553. 0x12, 0x00, 0x00
  6554. };
  6555. byte buff5[] = { /* SSL v2.0 client hello */
  6556. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  6557. /* dummy bytes bellow, just to pass size check */
  6558. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  6559. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  6560. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  6561. };
  6562. byte result[32] = {0};
  6563. word32 length = 32;
  6564. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  6565. 0, result, &length));
  6566. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  6567. 0, result, &length));
  6568. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  6569. 1, result, &length));
  6570. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6571. 0, result, &length));
  6572. buff[0] = 0x16;
  6573. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6574. 0, result, &length));
  6575. buff[1] = 0x03;
  6576. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  6577. sizeof(buff), 0, result, &length));
  6578. buff[2] = 0x03;
  6579. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  6580. sizeof(buff), 0, result, &length));
  6581. buff[4] = 0x64;
  6582. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6583. 0, result, &length));
  6584. result[length] = 0;
  6585. AssertStrEQ("www.paypal.com", (const char*) result);
  6586. length = 32;
  6587. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  6588. 0, result, &length));
  6589. result[length] = 0;
  6590. AssertStrEQ("api.textmate.org", (const char*) result);
  6591. /* SSL v2.0 tests */
  6592. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  6593. sizeof(buff5), 0, result, &length));
  6594. buff5[2] = 0x02;
  6595. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6596. sizeof(buff5), 0, result, &length));
  6597. buff5[2] = 0x01; buff5[6] = 0x08;
  6598. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6599. sizeof(buff5), 0, result, &length));
  6600. buff5[6] = 0x09; buff5[8] = 0x01;
  6601. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6602. sizeof(buff5), 0, result, &length));
  6603. return 0;
  6604. }
  6605. #endif /* HAVE_SNI */
  6606. static int test_wolfSSL_UseSNI(void)
  6607. {
  6608. #ifdef HAVE_SNI
  6609. test_wolfSSL_UseSNI_params();
  6610. test_wolfSSL_UseSNI_connection();
  6611. test_wolfSSL_SNI_GetFromBuffer();
  6612. #endif
  6613. return 0;
  6614. }
  6615. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  6616. static int test_wolfSSL_UseTrustedCA(void)
  6617. {
  6618. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  6619. && !defined(NO_RSA)
  6620. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  6621. WOLFSSL_CTX *ctx;
  6622. WOLFSSL *ssl;
  6623. byte id[20];
  6624. #ifndef NO_WOLFSSL_SERVER
  6625. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  6626. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6627. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6628. #else
  6629. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  6630. #endif
  6631. AssertNotNull((ssl = wolfSSL_new(ctx)));
  6632. XMEMSET(id, 0, sizeof(id));
  6633. /* error cases */
  6634. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  6635. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6636. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  6637. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6638. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  6639. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6640. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  6641. #ifdef NO_SHA
  6642. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6643. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  6644. #endif
  6645. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6646. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  6647. /* success cases */
  6648. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6649. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  6650. #ifndef NO_SHA
  6651. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6652. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  6653. #endif
  6654. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6655. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  6656. wolfSSL_free(ssl);
  6657. wolfSSL_CTX_free(ctx);
  6658. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  6659. #endif /* HAVE_TRUSTED_CA */
  6660. return 0;
  6661. }
  6662. static int test_wolfSSL_UseMaxFragment(void)
  6663. {
  6664. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  6665. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  6666. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  6667. #ifndef NO_WOLFSSL_SERVER
  6668. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  6669. #else
  6670. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6671. #endif
  6672. WOLFSSL *ssl;
  6673. #ifdef OPENSSL_EXTRA
  6674. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  6675. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  6676. #else
  6677. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  6678. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  6679. #endif
  6680. #ifndef NO_WOLFSSL_SERVER
  6681. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6682. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6683. #endif
  6684. AssertNotNull(ctx);
  6685. ssl = wolfSSL_new(ctx);
  6686. AssertNotNull(ssl);
  6687. #ifdef OPENSSL_EXTRA
  6688. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  6689. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  6690. #else
  6691. UseMaxFragment = wolfSSL_UseMaxFragment;
  6692. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  6693. #endif
  6694. /* error cases */
  6695. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  6696. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  6697. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  6698. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  6699. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  6700. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  6701. /* success case */
  6702. #ifdef OPENSSL_EXTRA
  6703. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  6704. #else
  6705. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  6706. #endif
  6707. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  6708. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  6709. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  6710. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  6711. #ifdef OPENSSL_EXTRA
  6712. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  6713. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  6714. #else
  6715. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  6716. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  6717. #endif
  6718. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  6719. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  6720. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  6721. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  6722. #ifdef OPENSSL_EXTRA
  6723. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  6724. #else
  6725. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  6726. #endif
  6727. wolfSSL_free(ssl);
  6728. wolfSSL_CTX_free(ctx);
  6729. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  6730. #endif
  6731. return 0;
  6732. }
  6733. static int test_wolfSSL_UseTruncatedHMAC(void)
  6734. {
  6735. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  6736. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  6737. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  6738. #ifndef NO_WOLFSSL_SERVER
  6739. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  6740. #else
  6741. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6742. #endif
  6743. WOLFSSL *ssl;
  6744. AssertNotNull(ctx);
  6745. #ifndef NO_WOLFSSL_SERVER
  6746. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6747. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6748. #endif
  6749. ssl = wolfSSL_new(ctx);
  6750. AssertNotNull(ssl);
  6751. /* error cases */
  6752. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  6753. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  6754. /* success case */
  6755. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  6756. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  6757. wolfSSL_free(ssl);
  6758. wolfSSL_CTX_free(ctx);
  6759. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  6760. #endif
  6761. return 0;
  6762. }
  6763. static int test_wolfSSL_UseSupportedCurve(void)
  6764. {
  6765. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  6766. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6767. WOLFSSL *ssl = wolfSSL_new(ctx);
  6768. AssertNotNull(ctx);
  6769. AssertNotNull(ssl);
  6770. /* error cases */
  6771. AssertIntNE(WOLFSSL_SUCCESS,
  6772. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  6773. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  6774. AssertIntNE(WOLFSSL_SUCCESS,
  6775. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  6776. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  6777. /* success case */
  6778. AssertIntEQ(WOLFSSL_SUCCESS,
  6779. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  6780. AssertIntEQ(WOLFSSL_SUCCESS,
  6781. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  6782. wolfSSL_free(ssl);
  6783. wolfSSL_CTX_free(ctx);
  6784. #endif
  6785. return 0;
  6786. }
  6787. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  6788. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  6789. {
  6790. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  6791. }
  6792. static void use_ALPN_all(WOLFSSL* ssl)
  6793. {
  6794. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6795. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6796. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6797. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6798. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6799. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  6800. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6801. }
  6802. static void use_ALPN_all_continue(WOLFSSL* ssl)
  6803. {
  6804. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6805. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6806. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6807. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6808. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6809. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  6810. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6811. }
  6812. static void use_ALPN_one(WOLFSSL* ssl)
  6813. {
  6814. /* spdy/2 */
  6815. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6816. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6817. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6818. }
  6819. static void use_ALPN_unknown(WOLFSSL* ssl)
  6820. {
  6821. /* http/2.0 */
  6822. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  6823. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6824. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6825. }
  6826. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  6827. {
  6828. /* http/2.0 */
  6829. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  6830. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6831. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6832. }
  6833. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  6834. {
  6835. /* spdy/3 */
  6836. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6837. char *proto = NULL;
  6838. word16 protoSz = 0;
  6839. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6840. /* check value */
  6841. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  6842. if (proto) {
  6843. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  6844. }
  6845. }
  6846. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  6847. {
  6848. char *proto = NULL;
  6849. word16 protoSz = 0;
  6850. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  6851. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6852. /* check value */
  6853. AssertIntEQ(1, (0 == protoSz));
  6854. AssertIntEQ(1, (NULL == proto));
  6855. }
  6856. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  6857. {
  6858. /* http/1.1 */
  6859. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6860. char *proto;
  6861. word16 protoSz = 0;
  6862. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6863. /* check value */
  6864. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6865. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6866. }
  6867. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  6868. {
  6869. /* spdy/2 */
  6870. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6871. char *proto;
  6872. word16 protoSz = 0;
  6873. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6874. /* check value */
  6875. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6876. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6877. }
  6878. static void verify_ALPN_client_list(WOLFSSL* ssl)
  6879. {
  6880. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6881. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6882. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6883. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6884. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6885. char *clist = NULL;
  6886. word16 clistSz = 0;
  6887. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  6888. &clistSz));
  6889. /* check value */
  6890. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  6891. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  6892. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  6893. }
  6894. static int test_wolfSSL_UseALPN_connection(void)
  6895. {
  6896. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  6897. callback_functions client_cb;
  6898. callback_functions server_cb;
  6899. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6900. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6901. client_cb.method = wolfSSLv23_client_method;
  6902. server_cb.method = wolfSSLv23_server_method;
  6903. client_cb.devId = devId;
  6904. server_cb.devId = devId;
  6905. /* success case same list */
  6906. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6907. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  6908. test_wolfSSL_client_server(&client_cb, &server_cb);
  6909. /* success case only one for server */
  6910. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6911. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  6912. test_wolfSSL_client_server(&client_cb, &server_cb);
  6913. /* success case only one for client */
  6914. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  6915. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  6916. test_wolfSSL_client_server(&client_cb, &server_cb);
  6917. /* success case none for client */
  6918. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6919. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  6920. test_wolfSSL_client_server(&client_cb, &server_cb);
  6921. /* success case mismatch behavior but option 'continue' set */
  6922. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  6923. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  6924. test_wolfSSL_client_server(&client_cb, &server_cb);
  6925. /* success case read protocol send by client */
  6926. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6927. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  6928. test_wolfSSL_client_server(&client_cb, &server_cb);
  6929. /* mismatch behavior with same list
  6930. * the first and only this one must be taken */
  6931. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6932. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  6933. test_wolfSSL_client_server(&client_cb, &server_cb);
  6934. /* default mismatch behavior */
  6935. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6936. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  6937. test_wolfSSL_client_server(&client_cb, &server_cb);
  6938. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  6939. return 0;
  6940. }
  6941. static int test_wolfSSL_UseALPN_params(void)
  6942. {
  6943. #ifndef NO_WOLFSSL_CLIENT
  6944. /* "http/1.1" */
  6945. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6946. /* "spdy/1" */
  6947. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  6948. /* "spdy/2" */
  6949. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6950. /* "spdy/3" */
  6951. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6952. char buff[256];
  6953. word32 idx;
  6954. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6955. WOLFSSL *ssl = wolfSSL_new(ctx);
  6956. AssertNotNull(ctx);
  6957. AssertNotNull(ssl);
  6958. /* error cases */
  6959. AssertIntNE(WOLFSSL_SUCCESS,
  6960. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  6961. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6962. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  6963. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6964. /* success case */
  6965. /* http1 only */
  6966. AssertIntEQ(WOLFSSL_SUCCESS,
  6967. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  6968. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6969. /* http1, spdy1 */
  6970. XMEMCPY(buff, http1, sizeof(http1));
  6971. idx = sizeof(http1);
  6972. buff[idx++] = ',';
  6973. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  6974. idx += sizeof(spdy1);
  6975. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  6976. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6977. /* http1, spdy2, spdy1 */
  6978. XMEMCPY(buff, http1, sizeof(http1));
  6979. idx = sizeof(http1);
  6980. buff[idx++] = ',';
  6981. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  6982. idx += sizeof(spdy2);
  6983. buff[idx++] = ',';
  6984. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  6985. idx += sizeof(spdy1);
  6986. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  6987. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6988. /* spdy3, http1, spdy2, spdy1 */
  6989. XMEMCPY(buff, spdy3, sizeof(spdy3));
  6990. idx = sizeof(spdy3);
  6991. buff[idx++] = ',';
  6992. XMEMCPY(buff+idx, http1, sizeof(http1));
  6993. idx += sizeof(http1);
  6994. buff[idx++] = ',';
  6995. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  6996. idx += sizeof(spdy2);
  6997. buff[idx++] = ',';
  6998. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  6999. idx += sizeof(spdy1);
  7000. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7001. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7002. wolfSSL_free(ssl);
  7003. wolfSSL_CTX_free(ctx);
  7004. #endif
  7005. return 0;
  7006. }
  7007. #endif /* HAVE_ALPN */
  7008. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  7009. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  7010. {
  7011. unsigned char p[] = {
  7012. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7013. 6, 's', 'p', 'd', 'y', '/', '2',
  7014. 6, 's', 'p', 'd', 'y', '/', '1',
  7015. };
  7016. unsigned char p_len = sizeof(p);
  7017. int ret;
  7018. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  7019. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7020. AssertIntEQ(ret, 0);
  7021. #else
  7022. AssertIntEQ(ret, SSL_SUCCESS);
  7023. #endif
  7024. }
  7025. static void set_alpn_protos(SSL* ssl)
  7026. {
  7027. unsigned char p[] = {
  7028. 6, 's', 'p', 'd', 'y', '/', '3',
  7029. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7030. 6, 's', 'p', 'd', 'y', '/', '2',
  7031. 6, 's', 'p', 'd', 'y', '/', '1',
  7032. };
  7033. unsigned char p_len = sizeof(p);
  7034. int ret;
  7035. ret = SSL_set_alpn_protos(ssl, p, p_len);
  7036. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7037. AssertIntEQ(ret, 0);
  7038. #else
  7039. AssertIntEQ(ret, SSL_SUCCESS);
  7040. #endif
  7041. }
  7042. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  7043. {
  7044. /* "spdy/3" */
  7045. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7046. const unsigned char *proto;
  7047. unsigned int protoSz = 0;
  7048. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7049. /* check value */
  7050. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7051. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7052. }
  7053. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  7054. {
  7055. /* "http/1.1" */
  7056. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7057. const unsigned char *proto;
  7058. unsigned int protoSz = 0;
  7059. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7060. /* check value */
  7061. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7062. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7063. }
  7064. static int test_wolfSSL_set_alpn_protos(void)
  7065. {
  7066. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7067. callback_functions client_cb;
  7068. callback_functions server_cb;
  7069. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7070. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7071. client_cb.method = wolfSSLv23_client_method;
  7072. server_cb.method = wolfSSLv23_server_method;
  7073. client_cb.devId = devId;
  7074. server_cb.devId = devId;
  7075. /* use CTX_alpn_protos */
  7076. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7077. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  7078. test_wolfSSL_client_server(&client_cb, &server_cb);
  7079. /* use set_alpn_protos */
  7080. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  7081. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  7082. test_wolfSSL_client_server(&client_cb, &server_cb);
  7083. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7084. return 0;
  7085. }
  7086. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  7087. static int test_wolfSSL_UseALPN(void)
  7088. {
  7089. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
  7090. defined(HAVE_IO_TESTS_DEPENDENCIES)
  7091. test_wolfSSL_UseALPN_connection();
  7092. test_wolfSSL_UseALPN_params();
  7093. #endif
  7094. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  7095. test_wolfSSL_set_alpn_protos();
  7096. #endif
  7097. return 0;
  7098. }
  7099. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  7100. {
  7101. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  7102. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7103. WOLFSSL *ssl = wolfSSL_new(ctx);
  7104. AssertNotNull(ctx);
  7105. AssertNotNull(ssl);
  7106. /* error cases */
  7107. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  7108. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  7109. /* success cases */
  7110. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  7111. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  7112. wolfSSL_free(ssl);
  7113. wolfSSL_CTX_free(ctx);
  7114. #endif
  7115. return 0;
  7116. }
  7117. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  7118. {
  7119. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  7120. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7121. WOLFSSL *ssl = wolfSSL_new(ctx);
  7122. AssertNotNull(ctx);
  7123. AssertNotNull(ssl);
  7124. /* error cases */
  7125. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  7126. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  7127. /* success cases */
  7128. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  7129. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  7130. wolfSSL_free(ssl);
  7131. wolfSSL_CTX_free(ctx);
  7132. #endif
  7133. return 0;
  7134. }
  7135. /*----------------------------------------------------------------------------*
  7136. | X509 Tests
  7137. *----------------------------------------------------------------------------*/
  7138. static int test_wolfSSL_X509_NAME_get_entry(void)
  7139. {
  7140. #if !defined(NO_CERTS) && !defined(NO_RSA)
  7141. #if defined(OPENSSL_ALL) || \
  7142. (defined(OPENSSL_EXTRA) && \
  7143. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  7144. printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
  7145. {
  7146. /* use openssl like name to test mapping */
  7147. X509_NAME_ENTRY* ne;
  7148. X509_NAME* name;
  7149. X509* x509;
  7150. #ifndef NO_FILESYSTEM
  7151. ASN1_STRING* asn;
  7152. char* subCN = NULL;
  7153. #endif
  7154. int idx;
  7155. ASN1_OBJECT *object = NULL;
  7156. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  7157. #ifndef NO_BIO
  7158. BIO* bio;
  7159. #endif
  7160. #endif
  7161. #ifndef NO_FILESYSTEM
  7162. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  7163. AssertNotNull(x509);
  7164. name = X509_get_subject_name(x509);
  7165. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7166. AssertIntGE(idx, 0);
  7167. ne = X509_NAME_get_entry(name, idx);
  7168. AssertNotNull(ne);
  7169. asn = X509_NAME_ENTRY_get_data(ne);
  7170. AssertNotNull(asn);
  7171. subCN = (char*)ASN1_STRING_data(asn);
  7172. AssertNotNull(subCN);
  7173. wolfSSL_FreeX509(x509);
  7174. #endif
  7175. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  7176. AssertNotNull(x509);
  7177. name = X509_get_subject_name(x509);
  7178. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7179. AssertIntGE(idx, 0);
  7180. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  7181. #ifndef NO_BIO
  7182. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7183. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  7184. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7185. AssertIntEQ(X509_NAME_print_ex_fp(stdout, name, 4,
  7186. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7187. BIO_free(bio);
  7188. #endif
  7189. #endif
  7190. ne = X509_NAME_get_entry(name, idx);
  7191. AssertNotNull(ne);
  7192. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  7193. wolfSSL_FreeX509(x509);
  7194. }
  7195. printf(resultFmt, passed);
  7196. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  7197. #endif /* !NO_CERTS && !NO_RSA */
  7198. return 0;
  7199. }
  7200. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  7201. static int test_wolfSSL_PKCS12(void)
  7202. {
  7203. /* .p12 file is encrypted with DES3 */
  7204. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  7205. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  7206. * Password Key
  7207. */
  7208. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  7209. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  7210. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  7211. byte buf[6000];
  7212. char file[] = "./certs/test-servercert.p12";
  7213. char order[] = "./certs/ecc-rsa-server.p12";
  7214. #ifdef WC_RC2
  7215. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  7216. #endif
  7217. char pass[] = "a password";
  7218. const char goodPsw[] = "wolfSSL test";
  7219. const char badPsw[] = "bad";
  7220. #ifdef HAVE_ECC
  7221. WOLFSSL_X509_NAME* subject;
  7222. WOLFSSL_X509 *x509;
  7223. #endif
  7224. XFILE f;
  7225. int bytes, ret, goodPswLen, badPswLen;
  7226. WOLFSSL_BIO *bio;
  7227. WOLFSSL_EVP_PKEY *pkey;
  7228. WC_PKCS12 *pkcs12;
  7229. WC_PKCS12 *pkcs12_2;
  7230. WOLFSSL_X509 *cert;
  7231. WOLFSSL_X509 *tmp;
  7232. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  7233. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7234. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7235. WOLFSSL_CTX *ctx;
  7236. WOLFSSL *ssl;
  7237. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  7238. #endif
  7239. printf(testingFmt, "wolfSSL_PKCS12()");
  7240. f = XFOPEN(file, "rb");
  7241. AssertTrue((f != XBADFILE));
  7242. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7243. XFCLOSE(f);
  7244. goodPswLen = (int)XSTRLEN(goodPsw);
  7245. badPswLen = (int)XSTRLEN(badPsw);
  7246. bio = BIO_new_mem_buf((void*)buf, bytes);
  7247. AssertNotNull(bio);
  7248. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  7249. AssertNotNull(pkcs12);
  7250. PKCS12_free(pkcs12);
  7251. AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  7252. d2i_PKCS12_bio(bio, &pkcs12);
  7253. AssertNotNull(pkcs12);
  7254. BIO_free(bio);
  7255. /* check verify MAC directly */
  7256. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  7257. AssertIntEQ(ret, 1);
  7258. /* check verify MAC fail case directly */
  7259. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  7260. AssertIntEQ(ret, 0);
  7261. /* check verify MAC fail case */
  7262. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7263. AssertIntEQ(ret, 0);
  7264. AssertNull(pkey);
  7265. AssertNull(cert);
  7266. /* check parse with no extra certs kept */
  7267. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7268. AssertIntEQ(ret, 1);
  7269. AssertNotNull(pkey);
  7270. AssertNotNull(cert);
  7271. wolfSSL_EVP_PKEY_free(pkey);
  7272. wolfSSL_X509_free(cert);
  7273. /* check parse with extra certs kept */
  7274. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7275. AssertIntEQ(ret, 1);
  7276. AssertNotNull(pkey);
  7277. AssertNotNull(cert);
  7278. AssertNotNull(ca);
  7279. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7280. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7281. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  7282. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7283. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  7284. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7285. #else
  7286. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7287. #endif
  7288. /* Copy stack structure */
  7289. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  7290. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  7291. /* CTX now owns the tmp_ca stack structure */
  7292. tmp_ca = NULL;
  7293. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  7294. AssertNotNull(tmp_ca);
  7295. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  7296. /* Check that the main cert is also set */
  7297. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  7298. AssertNotNull(ssl = SSL_new(ctx));
  7299. AssertNotNull(SSL_get_certificate(ssl));
  7300. SSL_free(ssl);
  7301. SSL_CTX_free(ctx);
  7302. #endif
  7303. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7304. /* should be 2 other certs on stack */
  7305. tmp = sk_X509_pop(ca);
  7306. AssertNotNull(tmp);
  7307. X509_free(tmp);
  7308. tmp = sk_X509_pop(ca);
  7309. AssertNotNull(tmp);
  7310. X509_free(tmp);
  7311. AssertNull(sk_X509_pop(ca));
  7312. EVP_PKEY_free(pkey);
  7313. X509_free(cert);
  7314. sk_X509_pop_free(ca, X509_free);
  7315. /* check PKCS12_create */
  7316. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  7317. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  7318. SSL_SUCCESS);
  7319. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  7320. -1, -1, 100, -1, 0)));
  7321. EVP_PKEY_free(pkey);
  7322. X509_free(cert);
  7323. sk_X509_pop_free(ca, NULL);
  7324. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7325. SSL_SUCCESS);
  7326. PKCS12_free(pkcs12_2);
  7327. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  7328. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  7329. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  7330. 2000, 1, 0)));
  7331. EVP_PKEY_free(pkey);
  7332. X509_free(cert);
  7333. sk_X509_pop_free(ca, NULL);
  7334. /* convert to DER then back and parse */
  7335. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7336. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  7337. PKCS12_free(pkcs12_2);
  7338. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  7339. BIO_free(bio);
  7340. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7341. SSL_SUCCESS);
  7342. /* should be 2 other certs on stack */
  7343. tmp = sk_X509_pop(ca);
  7344. AssertNotNull(tmp);
  7345. X509_free(tmp);
  7346. tmp = sk_X509_pop(ca);
  7347. AssertNotNull(tmp);
  7348. X509_free(tmp);
  7349. AssertNull(sk_X509_pop(ca));
  7350. #ifndef NO_RC4
  7351. PKCS12_free(pkcs12_2);
  7352. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  7353. NID_pbe_WithSHA1And128BitRC4,
  7354. NID_pbe_WithSHA1And128BitRC4,
  7355. 2000, 1, 0)));
  7356. EVP_PKEY_free(pkey);
  7357. X509_free(cert);
  7358. sk_X509_pop_free(ca, NULL);
  7359. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7360. SSL_SUCCESS);
  7361. #endif /* NO_RC4 */
  7362. EVP_PKEY_free(pkey);
  7363. X509_free(cert);
  7364. PKCS12_free(pkcs12);
  7365. PKCS12_free(pkcs12_2);
  7366. sk_X509_pop_free(ca, NULL);
  7367. #ifdef HAVE_ECC
  7368. /* test order of parsing */
  7369. f = XFOPEN(order, "rb");
  7370. AssertTrue(f != XBADFILE);
  7371. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7372. XFCLOSE(f);
  7373. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  7374. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  7375. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  7376. WOLFSSL_SUCCESS);
  7377. /* check use of pkey after parse */
  7378. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7379. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7380. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7381. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  7382. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7383. #else
  7384. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7385. #endif
  7386. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  7387. SSL_CTX_free(ctx);
  7388. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7389. #endif
  7390. AssertNotNull(pkey);
  7391. AssertNotNull(cert);
  7392. AssertNotNull(ca);
  7393. /* compare subject lines of certificates */
  7394. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  7395. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  7396. SSL_FILETYPE_PEM));
  7397. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7398. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7399. X509_free(x509);
  7400. /* test expected fail case */
  7401. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  7402. SSL_FILETYPE_PEM));
  7403. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7404. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7405. X509_free(x509);
  7406. X509_free(cert);
  7407. /* get subject line from ca stack */
  7408. AssertNotNull(cert = sk_X509_pop(ca));
  7409. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  7410. /* compare subject from certificate in ca to expected */
  7411. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  7412. SSL_FILETYPE_PEM));
  7413. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7414. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7415. EVP_PKEY_free(pkey);
  7416. X509_free(x509);
  7417. X509_free(cert);
  7418. BIO_free(bio);
  7419. PKCS12_free(pkcs12);
  7420. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  7421. /* test order of parsing */
  7422. f = XFOPEN(file, "rb");
  7423. AssertTrue(f != XBADFILE);
  7424. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  7425. XFCLOSE(f);
  7426. /* check verify MAC fail case */
  7427. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7428. AssertIntEQ(ret, 0);
  7429. AssertNull(pkey);
  7430. AssertNull(cert);
  7431. /* check parse with no extra certs kept */
  7432. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7433. AssertIntEQ(ret, 1);
  7434. AssertNotNull(pkey);
  7435. AssertNotNull(cert);
  7436. wolfSSL_EVP_PKEY_free(pkey);
  7437. wolfSSL_X509_free(cert);
  7438. /* check parse with extra certs kept */
  7439. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7440. AssertIntEQ(ret, 1);
  7441. AssertNotNull(pkey);
  7442. AssertNotNull(cert);
  7443. AssertNotNull(ca);
  7444. wolfSSL_EVP_PKEY_free(pkey);
  7445. wolfSSL_X509_free(cert);
  7446. sk_X509_pop_free(ca, NULL);
  7447. PKCS12_free(pkcs12);
  7448. #endif /* HAVE_ECC */
  7449. #ifdef WC_RC2
  7450. /* test PKCS#12 with RC2 encryption */
  7451. f = XFOPEN(rc2p12, "rb");
  7452. AssertTrue(f != XBADFILE);
  7453. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7454. XFCLOSE(f);
  7455. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  7456. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  7457. /* check verify MAC fail case */
  7458. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7459. AssertIntEQ(ret, 0);
  7460. AssertNull(pkey);
  7461. AssertNull(cert);
  7462. /* check parse iwth not extra certs kept */
  7463. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7464. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  7465. AssertNotNull(pkey);
  7466. AssertNotNull(cert);
  7467. /* check parse with extra certs kept */
  7468. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7469. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  7470. AssertNotNull(pkey);
  7471. AssertNotNull(cert);
  7472. AssertNotNull(ca);
  7473. wolfSSL_EVP_PKEY_free(pkey);
  7474. wolfSSL_X509_free(cert);
  7475. sk_X509_pop_free(ca, NULL);
  7476. BIO_free(bio);
  7477. PKCS12_free(pkcs12);
  7478. #endif /* WC_RC2 */
  7479. /* Test i2d_PKCS12_bio */
  7480. f = XFOPEN(file, "rb");
  7481. AssertTrue((f != XBADFILE));
  7482. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  7483. XFCLOSE(f);
  7484. bio = BIO_new(BIO_s_mem());
  7485. AssertNotNull(bio);
  7486. ret = i2d_PKCS12_bio(bio, pkcs12);
  7487. AssertIntEQ(ret, 1);
  7488. ret = i2d_PKCS12_bio(NULL, pkcs12);
  7489. AssertIntEQ(ret, 0);
  7490. ret = i2d_PKCS12_bio(bio, NULL);
  7491. AssertIntEQ(ret, 0);
  7492. PKCS12_free(pkcs12);
  7493. BIO_free(bio);
  7494. (void)order;
  7495. printf(resultFmt, passed);
  7496. #endif /* OPENSSL_EXTRA */
  7497. #endif /* HAVE_FIPS */
  7498. return 0;
  7499. }
  7500. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  7501. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  7502. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  7503. #define TEST_PKCS8_ENC
  7504. #endif
  7505. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  7506. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  7507. /* used to keep track if FailTestCallback was called */
  7508. static int failTestCallbackCalled = 0;
  7509. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  7510. {
  7511. (void)passwd;
  7512. (void)sz;
  7513. (void)rw;
  7514. (void)userdata;
  7515. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  7516. failTestCallbackCalled = 1;
  7517. return -1;
  7518. }
  7519. #endif
  7520. static int test_wolfSSL_no_password_cb(void)
  7521. {
  7522. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  7523. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  7524. WOLFSSL_CTX* ctx;
  7525. byte buff[FOURK_BUF];
  7526. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  7527. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  7528. XFILE f;
  7529. int bytes;
  7530. printf(testingFmt, "test_wolfSSL_no_password_cb()");
  7531. #ifndef NO_WOLFSSL_CLIENT
  7532. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  7533. #else
  7534. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  7535. #endif
  7536. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  7537. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  7538. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7539. XFCLOSE(f);
  7540. AssertIntLE(bytes, sizeof(buff));
  7541. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7542. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7543. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  7544. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7545. XFCLOSE(f);
  7546. AssertIntLE(bytes, sizeof(buff));
  7547. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7548. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7549. wolfSSL_CTX_free(ctx);
  7550. if (failTestCallbackCalled != 0) {
  7551. Fail(("Password callback should not be called by default"),
  7552. ("Password callback was called without attempting "
  7553. "to first decipher private key without password."));
  7554. }
  7555. printf(resultFmt, passed);
  7556. #endif
  7557. return 0;
  7558. }
  7559. #ifdef TEST_PKCS8_ENC
  7560. /* for PKCS8 test case */
  7561. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  7562. {
  7563. int flag = 0;
  7564. (void)rw;
  7565. if (userdata != NULL) {
  7566. flag = *((int*)userdata); /* user set data */
  7567. }
  7568. switch (flag) {
  7569. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  7570. * can be anything the user wishes to be passed to the callback
  7571. * associated with the WOLFSSL_CTX */
  7572. XSTRNCPY(passwd, "yassl123", sz);
  7573. return 8;
  7574. default:
  7575. return BAD_FUNC_ARG;
  7576. }
  7577. }
  7578. #endif /* TEST_PKCS8_ENC */
  7579. /* Testing functions dealing with PKCS8 */
  7580. static int test_wolfSSL_PKCS8(void)
  7581. {
  7582. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  7583. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7584. byte buff[FOURK_BUF];
  7585. byte der[FOURK_BUF];
  7586. #ifndef NO_RSA
  7587. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  7588. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  7589. #endif
  7590. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  7591. #ifdef HAVE_ECC
  7592. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  7593. #endif
  7594. XFILE f;
  7595. int bytes;
  7596. WOLFSSL_CTX* ctx;
  7597. #if defined(HAVE_ECC) && !defined(NO_CODING)
  7598. int ret;
  7599. ecc_key key;
  7600. word32 x = 0;
  7601. #endif
  7602. #ifdef TEST_PKCS8_ENC
  7603. #if !defined(NO_RSA) && !defined(NO_SHA)
  7604. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  7605. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  7606. #endif
  7607. #if defined(HAVE_ECC) && !defined(NO_SHA)
  7608. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  7609. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  7610. #endif
  7611. int flag;
  7612. #endif
  7613. (void)der;
  7614. printf(testingFmt, "wolfSSL_PKCS8()");
  7615. #ifndef NO_WOLFSSL_CLIENT
  7616. #ifndef WOLFSSL_NO_TLS12
  7617. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  7618. #else
  7619. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  7620. #endif
  7621. #else
  7622. #ifndef WOLFSSL_NO_TLS12
  7623. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  7624. #else
  7625. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  7626. #endif
  7627. #endif
  7628. #ifdef TEST_PKCS8_ENC
  7629. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  7630. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  7631. flag = 1; /* used by password callback as return code */
  7632. #if !defined(NO_RSA) && !defined(NO_SHA)
  7633. /* test loading PEM PKCS8 encrypted file */
  7634. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  7635. AssertTrue((f != XBADFILE));
  7636. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7637. XFCLOSE(f);
  7638. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7639. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7640. /* this next case should fail because of password callback return code */
  7641. flag = 0; /* used by password callback as return code */
  7642. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7643. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7644. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  7645. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7646. "yassl123"), 0);
  7647. /* test that error value is returned with a bad password */
  7648. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7649. "bad"), 0);
  7650. /* test loading PEM PKCS8 encrypted file */
  7651. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  7652. AssertTrue((f != XBADFILE));
  7653. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7654. XFCLOSE(f);
  7655. flag = 1; /* used by password callback as return code */
  7656. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7657. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7658. /* this next case should fail because of password callback return code */
  7659. flag = 0; /* used by password callback as return code */
  7660. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7661. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7662. #endif /* !NO_RSA && !NO_SHA */
  7663. #if defined(HAVE_ECC) && !defined(NO_SHA)
  7664. /* test loading PEM PKCS8 encrypted ECC Key file */
  7665. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  7666. AssertTrue((f != XBADFILE));
  7667. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7668. XFCLOSE(f);
  7669. flag = 1; /* used by password callback as return code */
  7670. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7671. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7672. /* this next case should fail because of password callback return code */
  7673. flag = 0; /* used by password callback as return code */
  7674. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7675. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7676. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  7677. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7678. "yassl123"), 0);
  7679. /* test that error value is returned with a bad password */
  7680. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7681. "bad"), 0);
  7682. /* test loading DER PKCS8 encrypted ECC Key file */
  7683. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  7684. AssertTrue((f != XBADFILE));
  7685. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7686. XFCLOSE(f);
  7687. flag = 1; /* used by password callback as return code */
  7688. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7689. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7690. /* this next case should fail because of password callback return code */
  7691. flag = 0; /* used by password callback as return code */
  7692. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7693. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7694. /* leave flag as "okay" */
  7695. flag = 1;
  7696. #endif /* HAVE_ECC && !NO_SHA */
  7697. #endif /* TEST_PKCS8_ENC */
  7698. #ifndef NO_RSA
  7699. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  7700. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  7701. AssertTrue((f != XBADFILE));
  7702. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7703. XFCLOSE(f);
  7704. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7705. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7706. /* test loading PEM PKCS8 private key file (not encrypted) */
  7707. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  7708. AssertTrue((f != XBADFILE));
  7709. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7710. XFCLOSE(f);
  7711. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7712. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7713. #endif /* !NO_RSA */
  7714. /* Test PKCS8 PEM ECC key no crypt */
  7715. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  7716. AssertTrue((f != XBADFILE));
  7717. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7718. XFCLOSE(f);
  7719. #ifdef HAVE_ECC
  7720. /* Test PKCS8 PEM ECC key no crypt */
  7721. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7722. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7723. #ifndef NO_CODING
  7724. /* decrypt PKCS8 PEM to key in DER format */
  7725. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  7726. (word32)sizeof(der), NULL)), 0);
  7727. ret = wc_ecc_init(&key);
  7728. if (ret == 0) {
  7729. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  7730. wc_ecc_free(&key);
  7731. }
  7732. AssertIntEQ(ret, 0);
  7733. #endif
  7734. /* Test PKCS8 DER ECC key no crypt */
  7735. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  7736. AssertTrue((f != XBADFILE));
  7737. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7738. XFCLOSE(f);
  7739. /* Test using a PKCS8 ECC PEM */
  7740. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7741. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7742. #else
  7743. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  7744. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  7745. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  7746. #endif /* HAVE_ECC */
  7747. wolfSSL_CTX_free(ctx);
  7748. printf(resultFmt, passed);
  7749. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7750. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  7751. return 0;
  7752. }
  7753. static int test_wolfSSL_PKCS8_ED25519(void)
  7754. {
  7755. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  7756. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  7757. defined(HAVE_ED25519_KEY_IMPORT)
  7758. const byte encPrivKey[] = \
  7759. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  7760. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  7761. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  7762. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  7763. "2L9W4v7swXkV+X+a1ww=\n"
  7764. "-----END ENCRYPTED PRIVATE KEY-----\n";
  7765. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  7766. byte der[FOURK_BUF];
  7767. WOLFSSL_CTX* ctx;
  7768. int bytes;
  7769. XMEMSET(der, 0, sizeof(der));
  7770. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  7771. (word32)sizeof(der), password)), 0);
  7772. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7773. #ifndef NO_WOLFSSL_SERVER
  7774. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7775. #else
  7776. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7777. #endif
  7778. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  7779. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7780. wolfSSL_CTX_free(ctx);
  7781. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7782. #endif
  7783. return 0;
  7784. }
  7785. static int test_wolfSSL_PKCS8_ED448(void)
  7786. {
  7787. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  7788. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  7789. defined(HAVE_ED448_KEY_IMPORT)
  7790. const byte encPrivKey[] = \
  7791. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  7792. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  7793. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  7794. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  7795. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  7796. "-----END ENCRYPTED PRIVATE KEY-----\n";
  7797. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  7798. byte der[FOURK_BUF];
  7799. WOLFSSL_CTX* ctx;
  7800. int bytes;
  7801. XMEMSET(der, 0, sizeof(der));
  7802. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  7803. (word32)sizeof(der), password)), 0);
  7804. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7805. #ifndef NO_WOLFSSL_SERVER
  7806. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7807. #else
  7808. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7809. #endif
  7810. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  7811. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7812. wolfSSL_CTX_free(ctx);
  7813. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7814. #endif
  7815. return 0;
  7816. }
  7817. /* Testing functions dealing with PKCS5 */
  7818. static int test_wolfSSL_PKCS5(void)
  7819. {
  7820. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  7821. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  7822. const char* passwd = "myfipsPa$$W0rd";
  7823. #else
  7824. const char *passwd = "pass1234";
  7825. #endif
  7826. const unsigned char *salt = (unsigned char *)"salt1234";
  7827. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  7828. DYNAMIC_TYPE_TMP_BUFFER);
  7829. int ret = 0;
  7830. AssertNotNull(out);
  7831. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  7832. (int)XSTRLEN((const char *) salt), 10,
  7833. WC_SHA_DIGEST_SIZE,out);
  7834. AssertIntEQ(ret, SSL_SUCCESS);
  7835. #ifdef WOLFSSL_SHA512
  7836. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  7837. (int)XSTRLEN((const char *) salt), 10,
  7838. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  7839. AssertIntEQ(ret, SSL_SUCCESS);
  7840. #endif
  7841. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7842. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  7843. return 0;
  7844. }
  7845. /* test parsing URI from certificate */
  7846. static int test_wolfSSL_URI(void)
  7847. {
  7848. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7849. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  7850. defined(OPENSSL_EXTRA))
  7851. WOLFSSL_X509* x509;
  7852. const char uri[] = "./certs/client-uri-cert.pem";
  7853. const char badUri[] = "./certs/client-relative-uri.pem";
  7854. printf(testingFmt, "wolfSSL URI parse");
  7855. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  7856. AssertNotNull(x509);
  7857. wolfSSL_FreeX509(x509);
  7858. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  7859. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  7860. && !defined(WOLFSSL_FPKI)
  7861. AssertNull(x509);
  7862. #else
  7863. AssertNotNull(x509);
  7864. wolfSSL_FreeX509(x509);
  7865. #endif
  7866. printf(resultFmt, passed);
  7867. #endif
  7868. return 0;
  7869. }
  7870. static int test_wolfSSL_TBS(void)
  7871. {
  7872. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7873. && defined(OPENSSL_EXTRA)
  7874. WOLFSSL_X509* x509;
  7875. const unsigned char* tbs;
  7876. int tbsSz;
  7877. printf(testingFmt, "wolfSSL TBS");
  7878. AssertNotNull(x509 =
  7879. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  7880. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  7881. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  7882. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  7883. AssertIntEQ(tbsSz, 1003);
  7884. wolfSSL_FreeX509(x509);
  7885. printf(resultFmt, passed);
  7886. #endif
  7887. return 0;
  7888. }
  7889. static int test_wolfSSL_X509_verify(void)
  7890. {
  7891. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7892. && defined(OPENSSL_EXTRA)
  7893. WOLFSSL_X509* ca;
  7894. WOLFSSL_X509* serv;
  7895. WOLFSSL_EVP_PKEY* pkey;
  7896. unsigned char buf[2048];
  7897. const unsigned char* pt = NULL;
  7898. int bufSz;
  7899. printf(testingFmt, "wolfSSL X509 verify");
  7900. AssertNotNull(ca =
  7901. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  7902. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  7903. WOLFSSL_SUCCESS);
  7904. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  7905. WOLFSSL_SUCCESS);
  7906. AssertIntEQ(bufSz, 294);
  7907. bufSz = 2048;
  7908. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  7909. WOLFSSL_SUCCESS);
  7910. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  7911. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  7912. AssertNotNull(serv =
  7913. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  7914. /* success case */
  7915. pt = buf;
  7916. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  7917. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  7918. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  7919. wolfSSL_EVP_PKEY_free(pkey);
  7920. /* fail case */
  7921. bufSz = 2048;
  7922. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  7923. WOLFSSL_SUCCESS);
  7924. pt = buf;
  7925. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  7926. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  7927. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  7928. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  7929. wolfSSL_EVP_PKEY_free(pkey);
  7930. wolfSSL_FreeX509(ca);
  7931. wolfSSL_FreeX509(serv);
  7932. printf(resultFmt, passed);
  7933. #endif
  7934. return 0;
  7935. }
  7936. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  7937. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  7938. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  7939. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  7940. /* create certificate with version 2 */
  7941. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  7942. {
  7943. WOLFSSL_X509 *x509, *x509v2;
  7944. WOLFSSL_EVP_PKEY *priv, *pub;
  7945. unsigned char *der = NULL, *key = NULL, *pt;
  7946. char *header, *name;
  7947. int derSz;
  7948. long keySz;
  7949. XFILE fp;
  7950. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  7951. time_t t;
  7952. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  7953. WOLFSSL_FILETYPE_PEM));
  7954. fp = XFOPEN(cliKeyFile, "rb");
  7955. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  7956. WOLFSSL_SUCCESS);
  7957. XFCLOSE(fp);
  7958. pt = key;
  7959. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  7960. (const unsigned char**)&pt, keySz));
  7961. /* create the version 2 certificate */
  7962. AssertNotNull(x509v2 = X509_new());
  7963. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  7964. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  7965. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  7966. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  7967. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  7968. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  7969. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  7970. t = time(NULL);
  7971. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  7972. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  7973. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  7974. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  7975. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  7976. derSz = wolfSSL_i2d_X509(x509v2, &der);
  7977. AssertIntGT(derSz, 0);
  7978. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  7979. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7980. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  7981. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7982. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7983. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7984. wolfSSL_X509_free(x509);
  7985. wolfSSL_X509_free(x509v2);
  7986. wolfSSL_EVP_PKEY_free(priv);
  7987. wolfSSL_EVP_PKEY_free(pub);
  7988. wolfSSL_ASN1_TIME_free(notBefore);
  7989. wolfSSL_ASN1_TIME_free(notAfter);
  7990. }
  7991. /* override certificate version error */
  7992. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  7993. {
  7994. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  7995. AssertIntEQ(store->error, ASN_VERSION_E);
  7996. #else
  7997. AssertIntEQ(store->error, 0);
  7998. #endif
  7999. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  8000. (void)preverify;
  8001. return 1;
  8002. }
  8003. /* set verify callback that will override bad certificate version */
  8004. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  8005. {
  8006. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  8007. }
  8008. #endif
  8009. static int test_wolfSSL_X509_TLS_version(void)
  8010. {
  8011. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8012. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8013. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8014. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  8015. tcp_ready ready;
  8016. func_args server_args;
  8017. func_args client_args;
  8018. THREAD_TYPE serverThread;
  8019. callback_functions func_cb_client;
  8020. callback_functions func_cb_server;
  8021. printf(testingFmt, "test_wolfSSL_X509_TLS_version");
  8022. /* test server rejects a client certificate that is not version 3 */
  8023. #ifdef WOLFSSL_TIRTOS
  8024. fdOpenSession(Task_self());
  8025. #endif
  8026. XMEMSET(&server_args, 0, sizeof(func_args));
  8027. XMEMSET(&client_args, 0, sizeof(func_args));
  8028. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8029. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8030. StartTCP();
  8031. InitTcpReady(&ready);
  8032. #if defined(USE_WINDOWS_API)
  8033. /* use RNG to get random port if using windows */
  8034. ready.port = GetRandomPort();
  8035. #endif
  8036. server_args.signal = &ready;
  8037. client_args.signal = &ready;
  8038. server_args.return_code = TEST_FAIL;
  8039. client_args.return_code = TEST_FAIL;
  8040. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8041. #ifndef WOLFSSL_NO_TLS12
  8042. func_cb_client.method = wolfTLSv1_2_client_method;
  8043. #else
  8044. func_cb_client.method = wolfTLSv1_3_client_method;
  8045. #endif
  8046. client_args.callbacks = &func_cb_client;
  8047. #ifndef WOLFSSL_NO_TLS12
  8048. func_cb_server.method = wolfTLSv1_2_server_method;
  8049. #else
  8050. func_cb_server.method = wolfTLSv1_3_server_method;
  8051. #endif
  8052. server_args.callbacks = &func_cb_server;
  8053. start_thread(test_server_nofail, &server_args, &serverThread);
  8054. wait_tcp_ready(&server_args);
  8055. test_client_nofail(&client_args, NULL);
  8056. join_thread(serverThread);
  8057. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8058. AssertIntEQ(client_args.return_code, TEST_FAIL);
  8059. AssertIntEQ(server_args.return_code, TEST_FAIL);
  8060. #else
  8061. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8062. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8063. #endif
  8064. FreeTcpReady(&ready);
  8065. #ifdef WOLFSSL_TIRTOS
  8066. fdCloseSession(Task_self());
  8067. #endif
  8068. /* Now re run but override the bad X509 version */
  8069. #ifdef WOLFSSL_TIRTOS
  8070. fdOpenSession(Task_self());
  8071. #endif
  8072. XMEMSET(&server_args, 0, sizeof(func_args));
  8073. XMEMSET(&client_args, 0, sizeof(func_args));
  8074. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8075. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8076. StartTCP();
  8077. InitTcpReady(&ready);
  8078. #if defined(USE_WINDOWS_API)
  8079. /* use RNG to get random port if using windows */
  8080. ready.port = GetRandomPort();
  8081. #endif
  8082. server_args.signal = &ready;
  8083. client_args.signal = &ready;
  8084. server_args.return_code = TEST_FAIL;
  8085. client_args.return_code = TEST_FAIL;
  8086. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8087. func_cb_server.ctx_ready = &test_set_override_x509;
  8088. #ifndef WOLFSSL_NO_TLS12
  8089. func_cb_client.method = wolfTLSv1_2_client_method;
  8090. #else
  8091. func_cb_client.method = wolfTLSv1_3_client_method;
  8092. #endif
  8093. client_args.callbacks = &func_cb_client;
  8094. #ifndef WOLFSSL_NO_TLS12
  8095. func_cb_server.method = wolfTLSv1_2_server_method;
  8096. #else
  8097. func_cb_server.method = wolfTLSv1_3_server_method;
  8098. #endif
  8099. server_args.callbacks = &func_cb_server;
  8100. start_thread(test_server_nofail, &server_args, &serverThread);
  8101. wait_tcp_ready(&server_args);
  8102. test_client_nofail(&client_args, NULL);
  8103. join_thread(serverThread);
  8104. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8105. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8106. FreeTcpReady(&ready);
  8107. #ifdef WOLFSSL_TIRTOS
  8108. fdCloseSession(Task_self());
  8109. #endif
  8110. printf(resultFmt, passed);
  8111. #endif
  8112. return 0;
  8113. }
  8114. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  8115. * version allowed.
  8116. * POST: 1 on success.
  8117. */
  8118. static int test_wolfSSL_CTX_SetMinVersion(void)
  8119. {
  8120. int failFlag = WOLFSSL_SUCCESS;
  8121. #ifndef NO_WOLFSSL_CLIENT
  8122. WOLFSSL_CTX* ctx;
  8123. int itr;
  8124. #ifndef NO_OLD_TLS
  8125. const int versions[] = {
  8126. #ifdef WOLFSSL_ALLOW_TLSV10
  8127. WOLFSSL_TLSV1,
  8128. #endif
  8129. WOLFSSL_TLSV1_1,
  8130. WOLFSSL_TLSV1_2 };
  8131. #elif !defined(WOLFSSL_NO_TLS12)
  8132. const int versions[] = { WOLFSSL_TLSV1_2 };
  8133. #elif defined(WOLFSSL_TLS13)
  8134. const int versions[] = { WOLFSSL_TLSV1_3 };
  8135. #else
  8136. const int versions[0];
  8137. #endif
  8138. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8139. printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
  8140. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  8141. if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
  8142. failFlag = WOLFSSL_FAILURE;
  8143. }
  8144. }
  8145. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  8146. wolfSSL_CTX_free(ctx);
  8147. #endif
  8148. if (failFlag == WOLFSSL_SUCCESS) {
  8149. failFlag = 0;
  8150. }
  8151. return failFlag;
  8152. } /* END test_wolfSSL_CTX_SetMinVersion */
  8153. /*----------------------------------------------------------------------------*
  8154. | OCSP Stapling
  8155. *----------------------------------------------------------------------------*/
  8156. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  8157. * need to contact the CA, lowering the cost of cert revocation checking.
  8158. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  8159. * POST: 1 returned for success.
  8160. */
  8161. static int test_wolfSSL_UseOCSPStapling(void)
  8162. {
  8163. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  8164. !defined(NO_WOLFSSL_CLIENT)
  8165. int ret;
  8166. WOLFSSL_CTX* ctx;
  8167. WOLFSSL* ssl;
  8168. #ifndef NO_WOLFSSL_CLIENT
  8169. #ifndef WOLFSSL_NO_TLS12
  8170. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8171. #else
  8172. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8173. #endif
  8174. #else
  8175. #ifndef WOLFSSL_NO_TLS12
  8176. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8177. #else
  8178. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8179. #endif
  8180. #endif
  8181. ssl = wolfSSL_new(ctx);
  8182. printf(testingFmt, "wolfSSL_UseOCSPStapling()");
  8183. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  8184. WOLFSSL_CSR2_OCSP_USE_NONCE);
  8185. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  8186. wolfSSL_free(ssl);
  8187. wolfSSL_CTX_free(ctx);
  8188. if (ret == WOLFSSL_SUCCESS) {
  8189. ret = 0;
  8190. }
  8191. return ret;
  8192. #else
  8193. return 0;
  8194. #endif
  8195. } /*END test_wolfSSL_UseOCSPStapling */
  8196. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  8197. * stapling eliminates the need to contact the CA and lowers cert revocation
  8198. * check.
  8199. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  8200. */
  8201. static int test_wolfSSL_UseOCSPStaplingV2(void)
  8202. {
  8203. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  8204. !defined(NO_WOLFSSL_CLIENT)
  8205. int ret;
  8206. WOLFSSL_CTX* ctx;
  8207. WOLFSSL* ssl;
  8208. #ifndef NO_WOLFSSL_CLIENT
  8209. #ifndef WOLFSSL_NO_TLS12
  8210. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8211. #else
  8212. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8213. #endif
  8214. #else
  8215. #ifndef WOLFSSL_NO_TLS12
  8216. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8217. #else
  8218. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8219. #endif
  8220. #endif
  8221. ssl = wolfSSL_new(ctx);
  8222. printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
  8223. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  8224. WOLFSSL_CSR2_OCSP_USE_NONCE );
  8225. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  8226. wolfSSL_free(ssl);
  8227. wolfSSL_CTX_free(ctx);
  8228. if (ret == WOLFSSL_SUCCESS) {
  8229. ret = 0;
  8230. }
  8231. return ret;
  8232. #else
  8233. return 0;
  8234. #endif
  8235. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  8236. /*----------------------------------------------------------------------------*
  8237. | Multicast Tests
  8238. *----------------------------------------------------------------------------*/
  8239. static int test_wolfSSL_mcast(void)
  8240. {
  8241. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  8242. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  8243. WOLFSSL_CTX* ctx;
  8244. WOLFSSL* ssl;
  8245. int result;
  8246. byte preMasterSecret[512];
  8247. byte clientRandom[32];
  8248. byte serverRandom[32];
  8249. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  8250. byte buf[256];
  8251. word16 newId;
  8252. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  8253. AssertNotNull(ctx);
  8254. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  8255. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8256. ssl = wolfSSL_new(ctx);
  8257. AssertNotNull(ssl);
  8258. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  8259. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  8260. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  8261. result = wolfSSL_set_secret(ssl, 23,
  8262. preMasterSecret, sizeof(preMasterSecret),
  8263. clientRandom, serverRandom, suite);
  8264. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8265. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  8266. AssertIntLE(result, 0);
  8267. AssertIntLE(newId, 100);
  8268. wolfSSL_free(ssl);
  8269. wolfSSL_CTX_free(ctx);
  8270. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 || WOLFSSL_SNIFFER) */
  8271. return 0;
  8272. }
  8273. /*----------------------------------------------------------------------------*
  8274. | Wolfcrypt
  8275. *----------------------------------------------------------------------------*/
  8276. /*
  8277. * Unit test for the wc_InitBlake2b()
  8278. */
  8279. static int test_wc_InitBlake2b(void)
  8280. {
  8281. int ret = 0;
  8282. #ifdef HAVE_BLAKE2
  8283. Blake2b blake;
  8284. printf(testingFmt, "wc_InitBlake2B()");
  8285. /* Test good arg. */
  8286. ret = wc_InitBlake2b(&blake, 64);
  8287. if (ret != 0) {
  8288. ret = WOLFSSL_FATAL_ERROR;
  8289. }
  8290. /* Test bad arg. */
  8291. if (!ret) {
  8292. ret = wc_InitBlake2b(NULL, 64);
  8293. if (ret == 0) {
  8294. ret = WOLFSSL_FATAL_ERROR;
  8295. } else {
  8296. ret = 0;
  8297. }
  8298. }
  8299. if (!ret) {
  8300. ret = wc_InitBlake2b(NULL, 128);
  8301. if (ret == 0) {
  8302. ret = WOLFSSL_FATAL_ERROR;
  8303. } else {
  8304. ret = 0;
  8305. }
  8306. }
  8307. if (!ret) {
  8308. ret = wc_InitBlake2b(&blake, 128);
  8309. if (ret == 0) {
  8310. ret = WOLFSSL_FATAL_ERROR;
  8311. } else {
  8312. ret = 0;
  8313. }
  8314. }
  8315. if (!ret) {
  8316. ret = wc_InitBlake2b(NULL, 0);
  8317. if (ret == 0) {
  8318. ret = WOLFSSL_FATAL_ERROR;
  8319. } else {
  8320. ret = 0;
  8321. }
  8322. }
  8323. if (!ret) {
  8324. ret = wc_InitBlake2b(&blake, 0);
  8325. if (ret == 0) {
  8326. ret = WOLFSSL_FATAL_ERROR;
  8327. } else {
  8328. ret = 0;
  8329. }
  8330. }
  8331. printf(resultFmt, ret == 0 ? passed : failed);
  8332. #endif
  8333. return ret;
  8334. } /*END test_wc_InitBlake2b*/
  8335. /*
  8336. * Unit test for the wc_InitBlake2b_WithKey()
  8337. */
  8338. static int test_wc_InitBlake2b_WithKey(void)
  8339. {
  8340. int ret = 0;
  8341. #ifdef HAVE_BLAKE2
  8342. Blake2b blake;
  8343. word32 digestSz = BLAKE2B_KEYBYTES;
  8344. byte key[BLAKE2B_KEYBYTES];
  8345. word32 keylen = BLAKE2B_KEYBYTES;
  8346. printf(testingFmt, "wc_InitBlake2b_WithKey()");
  8347. /* Test good arg. */
  8348. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  8349. if (ret != 0) {
  8350. ret = WOLFSSL_FATAL_ERROR;
  8351. }
  8352. /* Test bad args. */
  8353. if (ret == 0) {
  8354. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  8355. if (ret == BAD_FUNC_ARG) {
  8356. ret = 0;
  8357. }
  8358. }
  8359. if (ret == 0) {
  8360. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  8361. if (ret == BAD_FUNC_ARG) {
  8362. ret = 0;
  8363. }
  8364. }
  8365. if (ret == 0) {
  8366. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  8367. }
  8368. printf(resultFmt, ret == 0 ? passed : failed);
  8369. #endif
  8370. return ret;
  8371. } /*END wc_InitBlake2b_WithKey*/
  8372. /*
  8373. * Unit test for the wc_InitBlake2s_WithKey()
  8374. */
  8375. static int test_wc_InitBlake2s_WithKey(void)
  8376. {
  8377. int ret = 0;
  8378. #ifdef HAVE_BLAKE2S
  8379. Blake2s blake;
  8380. word32 digestSz = BLAKE2S_KEYBYTES;
  8381. byte *key = (byte*)"01234567890123456789012345678901";
  8382. word32 keylen = BLAKE2S_KEYBYTES;
  8383. printf(testingFmt, "wc_InitBlake2s_WithKey()");
  8384. /* Test good arg. */
  8385. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  8386. if (ret != 0) {
  8387. ret = WOLFSSL_FATAL_ERROR;
  8388. }
  8389. /* Test bad args. */
  8390. if (ret == 0) {
  8391. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  8392. if (ret == BAD_FUNC_ARG) {
  8393. ret = 0;
  8394. }
  8395. }
  8396. if (ret == 0) {
  8397. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  8398. if (ret == BAD_FUNC_ARG) {
  8399. ret = 0;
  8400. }
  8401. }
  8402. if (ret == 0) {
  8403. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  8404. }
  8405. printf(resultFmt, ret == 0 ? passed : failed);
  8406. #endif
  8407. return ret;
  8408. } /*END wc_InitBlake2s_WithKey*/
  8409. /*
  8410. * Unit test for the wc_InitMd5()
  8411. */
  8412. static int test_wc_InitMd5(void)
  8413. {
  8414. int flag = 0;
  8415. #ifndef NO_MD5
  8416. wc_Md5 md5;
  8417. int ret;
  8418. printf(testingFmt, "wc_InitMd5()");
  8419. /* Test good arg. */
  8420. ret = wc_InitMd5(&md5);
  8421. if (ret != 0) {
  8422. flag = WOLFSSL_FATAL_ERROR;
  8423. }
  8424. /* Test bad arg. */
  8425. if (!flag) {
  8426. ret = wc_InitMd5(NULL);
  8427. if (ret != BAD_FUNC_ARG) {
  8428. flag = WOLFSSL_FATAL_ERROR;
  8429. }
  8430. }
  8431. wc_Md5Free(&md5);
  8432. printf(resultFmt, flag == 0 ? passed : failed);
  8433. #endif
  8434. return flag;
  8435. } /* END test_wc_InitMd5 */
  8436. /*
  8437. * Testing wc_UpdateMd5()
  8438. */
  8439. static int test_wc_Md5Update(void)
  8440. {
  8441. int flag = 0;
  8442. #ifndef NO_MD5
  8443. wc_Md5 md5;
  8444. byte hash[WC_MD5_DIGEST_SIZE];
  8445. testVector a, b, c;
  8446. int ret;
  8447. ret = wc_InitMd5(&md5);
  8448. if (ret != 0) {
  8449. flag = ret;
  8450. }
  8451. printf(testingFmt, "wc_Md5Update()");
  8452. /* Input */
  8453. if (!flag) {
  8454. a.input = "a";
  8455. a.inLen = XSTRLEN(a.input);
  8456. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  8457. if (ret != 0) {
  8458. flag = ret;
  8459. }
  8460. }
  8461. if (!flag) {
  8462. ret = wc_Md5Final(&md5, hash);
  8463. if (ret != 0) {
  8464. flag = ret;
  8465. }
  8466. }
  8467. /* Update input. */
  8468. if (!flag) {
  8469. a.input = "abc";
  8470. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  8471. "\x72";
  8472. a.inLen = XSTRLEN(a.input);
  8473. a.outLen = XSTRLEN(a.output);
  8474. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  8475. if (ret != 0) {
  8476. flag = ret;
  8477. }
  8478. }
  8479. if (!flag) {
  8480. ret = wc_Md5Final(&md5, hash);
  8481. if (ret != 0) {
  8482. flag = ret;
  8483. }
  8484. }
  8485. if (!flag) {
  8486. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  8487. flag = WOLFSSL_FATAL_ERROR;
  8488. }
  8489. }
  8490. /*Pass in bad values. */
  8491. if (!flag) {
  8492. b.input = NULL;
  8493. b.inLen = 0;
  8494. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  8495. if (ret != 0) {
  8496. flag = ret;
  8497. }
  8498. }
  8499. if (!flag) {
  8500. c.input = NULL;
  8501. c.inLen = WC_MD5_DIGEST_SIZE;
  8502. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  8503. if (ret != BAD_FUNC_ARG) {
  8504. flag = WOLFSSL_FATAL_ERROR;
  8505. }
  8506. }
  8507. if (!flag) {
  8508. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  8509. if (ret != BAD_FUNC_ARG) {
  8510. flag = WOLFSSL_FATAL_ERROR;
  8511. }
  8512. }
  8513. wc_Md5Free(&md5);
  8514. printf(resultFmt, flag == 0 ? passed : failed);
  8515. #endif
  8516. return flag;
  8517. } /* END test_wc_Md5Update() */
  8518. /*
  8519. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  8520. */
  8521. static int test_wc_Md5Final(void)
  8522. {
  8523. int flag = 0;
  8524. #ifndef NO_MD5
  8525. /* Instantiate */
  8526. wc_Md5 md5;
  8527. byte* hash_test[3];
  8528. byte hash1[WC_MD5_DIGEST_SIZE];
  8529. byte hash2[2*WC_MD5_DIGEST_SIZE];
  8530. byte hash3[5*WC_MD5_DIGEST_SIZE];
  8531. int times, i, ret;
  8532. /* Initialize */
  8533. ret = wc_InitMd5(&md5);
  8534. if (ret != 0) {
  8535. flag = ret;
  8536. }
  8537. if (!flag) {
  8538. hash_test[0] = hash1;
  8539. hash_test[1] = hash2;
  8540. hash_test[2] = hash3;
  8541. }
  8542. times = sizeof(hash_test)/sizeof(byte*);
  8543. /* Test good args. */
  8544. printf(testingFmt, "wc_Md5Final()");
  8545. for (i = 0; i < times; i++) {
  8546. if (!flag) {
  8547. ret = wc_Md5Final(&md5, hash_test[i]);
  8548. if (ret != 0) {
  8549. flag = WOLFSSL_FATAL_ERROR;
  8550. }
  8551. }
  8552. }
  8553. /* Test bad args. */
  8554. if (!flag) {
  8555. ret = wc_Md5Final(NULL, NULL);
  8556. if (ret != BAD_FUNC_ARG) {
  8557. flag = WOLFSSL_FATAL_ERROR;
  8558. }
  8559. }
  8560. if (!flag) {
  8561. ret = wc_Md5Final(NULL, hash1);
  8562. if (ret != BAD_FUNC_ARG) {
  8563. flag = WOLFSSL_FATAL_ERROR;
  8564. }
  8565. }
  8566. if (!flag) {
  8567. ret = wc_Md5Final(&md5, NULL);
  8568. if (ret != BAD_FUNC_ARG) {
  8569. flag = WOLFSSL_FATAL_ERROR;
  8570. }
  8571. }
  8572. wc_Md5Free(&md5);
  8573. printf(resultFmt, flag == 0 ? passed : failed);
  8574. #endif
  8575. return flag;
  8576. }
  8577. /*
  8578. * Unit test for the wc_InitSha()
  8579. */
  8580. static int test_wc_InitSha(void)
  8581. {
  8582. int flag = 0;
  8583. #ifndef NO_SHA
  8584. wc_Sha sha;
  8585. int ret;
  8586. printf(testingFmt, "wc_InitSha()");
  8587. /* Test good arg. */
  8588. ret = wc_InitSha(&sha);
  8589. if (ret != 0) {
  8590. flag = WOLFSSL_FATAL_ERROR;
  8591. }
  8592. /* Test bad arg. */
  8593. if (!flag) {
  8594. ret = wc_InitSha(NULL);
  8595. if (ret != BAD_FUNC_ARG) {
  8596. flag = WOLFSSL_FATAL_ERROR;
  8597. }
  8598. }
  8599. wc_ShaFree(&sha);
  8600. printf(resultFmt, flag == 0 ? passed : failed);
  8601. #endif
  8602. return flag;
  8603. } /* END test_wc_InitSha */
  8604. /*
  8605. * Tesing wc_ShaUpdate()
  8606. */
  8607. static int test_wc_ShaUpdate(void)
  8608. {
  8609. int flag = 0;
  8610. #ifndef NO_SHA
  8611. wc_Sha sha;
  8612. byte hash[WC_SHA_DIGEST_SIZE];
  8613. testVector a, b, c;
  8614. int ret;
  8615. ret = wc_InitSha(&sha);
  8616. if (ret != 0) {
  8617. flag = ret;
  8618. }
  8619. printf(testingFmt, "wc_ShaUpdate()");
  8620. /* Input. */
  8621. if (!flag) {
  8622. a.input = "a";
  8623. a.inLen = XSTRLEN(a.input);
  8624. ret = wc_ShaUpdate(&sha, NULL, 0);
  8625. if (ret != 0) {
  8626. flag = ret;
  8627. }
  8628. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  8629. if (ret != 0) {
  8630. flag = ret;
  8631. }
  8632. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  8633. if (ret != 0) {
  8634. flag = ret;
  8635. }
  8636. }
  8637. if (!flag) {
  8638. ret = wc_ShaFinal(&sha, hash);
  8639. if (ret != 0) {
  8640. flag = ret;
  8641. }
  8642. }
  8643. /* Update input. */
  8644. if (!flag) {
  8645. a.input = "abc";
  8646. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  8647. "\x6C\x9C\xD0\xD8\x9D";
  8648. a.inLen = XSTRLEN(a.input);
  8649. a.outLen = XSTRLEN(a.output);
  8650. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  8651. if (ret != 0) {
  8652. flag = ret;
  8653. }
  8654. }
  8655. if (!flag) {
  8656. ret = wc_ShaFinal(&sha, hash);
  8657. if (ret !=0) {
  8658. flag = ret;
  8659. }
  8660. }
  8661. if (!flag) {
  8662. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  8663. flag = WOLFSSL_FATAL_ERROR;
  8664. }
  8665. }
  8666. /* Try passing in bad values. */
  8667. if (!flag) {
  8668. b.input = NULL;
  8669. b.inLen = 0;
  8670. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  8671. if (ret != 0) {
  8672. flag = ret;
  8673. }
  8674. }
  8675. if (!flag) {
  8676. c.input = NULL;
  8677. c.inLen = WC_SHA_DIGEST_SIZE;
  8678. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  8679. if (ret != BAD_FUNC_ARG) {
  8680. flag = WOLFSSL_FATAL_ERROR;
  8681. }
  8682. }
  8683. if (!flag) {
  8684. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8685. if (ret != BAD_FUNC_ARG) {
  8686. flag = WOLFSSL_FATAL_ERROR;
  8687. }
  8688. }
  8689. wc_ShaFree(&sha);
  8690. /* If not returned then the unit test passed test vectors. */
  8691. printf(resultFmt, flag == 0 ? passed : failed);
  8692. #endif
  8693. return flag;
  8694. } /* END test_wc_ShaUpdate() */
  8695. /*
  8696. * Unit test on wc_ShaFinal
  8697. */
  8698. static int test_wc_ShaFinal(void)
  8699. {
  8700. int flag = 0;
  8701. #ifndef NO_SHA
  8702. wc_Sha sha;
  8703. byte* hash_test[3];
  8704. byte hash1[WC_SHA_DIGEST_SIZE];
  8705. byte hash2[2*WC_SHA_DIGEST_SIZE];
  8706. byte hash3[5*WC_SHA_DIGEST_SIZE];
  8707. int times, i, ret;
  8708. /*Initialize*/
  8709. ret = wc_InitSha(&sha);
  8710. if (ret) {
  8711. flag = ret;
  8712. }
  8713. if (!flag) {
  8714. hash_test[0] = hash1;
  8715. hash_test[1] = hash2;
  8716. hash_test[2] = hash3;
  8717. }
  8718. times = sizeof(hash_test)/sizeof(byte*);
  8719. /* Good test args. */
  8720. printf(testingFmt, "wc_ShaFinal()");
  8721. for (i = 0; i < times; i++) {
  8722. if (!flag) {
  8723. ret = wc_ShaFinal(&sha, hash_test[i]);
  8724. if (ret != 0) {
  8725. flag = WOLFSSL_FATAL_ERROR;
  8726. }
  8727. }
  8728. }
  8729. /* Test bad args. */
  8730. if (!flag) {
  8731. ret = wc_ShaFinal(NULL, NULL);
  8732. if (ret != BAD_FUNC_ARG) {
  8733. flag = WOLFSSL_FATAL_ERROR;
  8734. }
  8735. }
  8736. if (!flag) {
  8737. ret = wc_ShaFinal(NULL, hash1);
  8738. if (ret != BAD_FUNC_ARG) {
  8739. flag = WOLFSSL_FATAL_ERROR;
  8740. }
  8741. }
  8742. if (!flag) {
  8743. ret = wc_ShaFinal(&sha, NULL);
  8744. if (ret != BAD_FUNC_ARG) {
  8745. flag = WOLFSSL_FATAL_ERROR;
  8746. }
  8747. }
  8748. wc_ShaFree(&sha);
  8749. printf(resultFmt, flag == 0 ? passed : failed);
  8750. #endif
  8751. return flag;
  8752. } /* END test_wc_ShaFinal */
  8753. /*
  8754. * Unit test for wc_InitSha256()
  8755. */
  8756. static int test_wc_InitSha256(void)
  8757. {
  8758. int flag = 0;
  8759. #ifndef NO_SHA256
  8760. wc_Sha256 sha256;
  8761. int ret;
  8762. printf(testingFmt, "wc_InitSha256()");
  8763. /* Test good arg. */
  8764. ret = wc_InitSha256(&sha256);
  8765. if (ret != 0) {
  8766. flag = WOLFSSL_FATAL_ERROR;
  8767. }
  8768. /* Test bad arg. */
  8769. if (!flag) {
  8770. ret = wc_InitSha256(NULL);
  8771. if (ret != BAD_FUNC_ARG) {
  8772. flag = WOLFSSL_FATAL_ERROR;
  8773. }
  8774. }
  8775. wc_Sha256Free(&sha256);
  8776. printf(resultFmt, flag == 0 ? passed : failed);
  8777. #endif
  8778. return flag;
  8779. } /* END test_wc_InitSha256 */
  8780. /*
  8781. * Unit test for wc_Sha256Update()
  8782. */
  8783. static int test_wc_Sha256Update(void)
  8784. {
  8785. int flag = 0;
  8786. #ifndef NO_SHA256
  8787. wc_Sha256 sha256;
  8788. byte hash[WC_SHA256_DIGEST_SIZE];
  8789. testVector a, b, c;
  8790. int ret;
  8791. ret = wc_InitSha256(&sha256);
  8792. if (ret != 0) {
  8793. flag = ret;
  8794. }
  8795. printf(testingFmt, "wc_Sha256Update()");
  8796. /* Input. */
  8797. if (!flag) {
  8798. a.input = "a";
  8799. a.inLen = XSTRLEN(a.input);
  8800. ret = wc_Sha256Update(&sha256, NULL, 0);
  8801. if (ret != 0) {
  8802. flag = ret;
  8803. }
  8804. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  8805. if (ret != 0) {
  8806. flag = ret;
  8807. }
  8808. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  8809. if (ret != 0) {
  8810. flag = ret;
  8811. }
  8812. }
  8813. if (!flag) {
  8814. ret = wc_Sha256Final(&sha256, hash);
  8815. if (ret != 0) {
  8816. flag = ret;
  8817. }
  8818. }
  8819. /* Update input. */
  8820. if (!flag) {
  8821. a.input = "abc";
  8822. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  8823. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  8824. "\x15\xAD";
  8825. a.inLen = XSTRLEN(a.input);
  8826. a.outLen = XSTRLEN(a.output);
  8827. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  8828. if (ret != 0) {
  8829. flag = ret;
  8830. }
  8831. }
  8832. if (!flag) {
  8833. ret = wc_Sha256Final(&sha256, hash);
  8834. if (ret != 0) {
  8835. flag = ret;
  8836. }
  8837. }
  8838. if (!flag) {
  8839. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  8840. flag = WOLFSSL_FATAL_ERROR;
  8841. }
  8842. }
  8843. /* Try passing in bad values */
  8844. if (!flag) {
  8845. b.input = NULL;
  8846. b.inLen = 0;
  8847. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  8848. if (ret != 0) {
  8849. flag = ret;
  8850. }
  8851. }
  8852. if (!flag) {
  8853. c.input = NULL;
  8854. c.inLen = WC_SHA256_DIGEST_SIZE;
  8855. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  8856. if (ret != BAD_FUNC_ARG) {
  8857. flag = WOLFSSL_FATAL_ERROR;
  8858. }
  8859. }
  8860. if (!flag) {
  8861. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  8862. if (ret != BAD_FUNC_ARG) {
  8863. flag = WOLFSSL_FATAL_ERROR;
  8864. }
  8865. }
  8866. wc_Sha256Free(&sha256);
  8867. /* If not returned then the unit test passed. */
  8868. printf(resultFmt, flag == 0 ? passed : failed);
  8869. #endif
  8870. return flag;
  8871. } /* END test_wc_Sha256Update */
  8872. /*
  8873. * Unit test function for wc_Sha256Final()
  8874. */
  8875. static int test_wc_Sha256Final(void)
  8876. {
  8877. int flag = 0;
  8878. #ifndef NO_SHA256
  8879. wc_Sha256 sha256;
  8880. byte* hash_test[3];
  8881. byte hash1[WC_SHA256_DIGEST_SIZE];
  8882. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  8883. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  8884. int times, i, ret;
  8885. /* Initialize */
  8886. ret = wc_InitSha256(&sha256);
  8887. if (ret != 0) {
  8888. flag = ret;
  8889. }
  8890. if (!flag) {
  8891. hash_test[0] = hash1;
  8892. hash_test[1] = hash2;
  8893. hash_test[2] = hash3;
  8894. }
  8895. times = sizeof(hash_test) / sizeof(byte*);
  8896. /* Good test args. */
  8897. printf(testingFmt, "wc_Sha256Final()");
  8898. for (i = 0; i < times; i++) {
  8899. if (!flag) {
  8900. ret = wc_Sha256Final(&sha256, hash_test[i]);
  8901. if (ret != 0) {
  8902. flag = WOLFSSL_FATAL_ERROR;
  8903. }
  8904. }
  8905. }
  8906. /* Test bad args. */
  8907. if (!flag ) {
  8908. ret = wc_Sha256Final(NULL, NULL);
  8909. if (ret != BAD_FUNC_ARG) {
  8910. flag = WOLFSSL_FATAL_ERROR;
  8911. }
  8912. }
  8913. if (!flag) {
  8914. ret = wc_Sha256Final(NULL, hash1);
  8915. if (ret != BAD_FUNC_ARG) {
  8916. flag = WOLFSSL_FATAL_ERROR;
  8917. }
  8918. }
  8919. if (!flag) {
  8920. ret = wc_Sha256Final(&sha256, NULL);
  8921. if (ret != BAD_FUNC_ARG) {
  8922. flag = WOLFSSL_FATAL_ERROR;
  8923. }
  8924. }
  8925. wc_Sha256Free(&sha256);
  8926. printf(resultFmt, flag == 0 ? passed : failed);
  8927. #endif
  8928. return flag;
  8929. } /* END test_wc_Sha256Final */
  8930. /*
  8931. * Unit test function for wc_Sha256FinalRaw()
  8932. */
  8933. static int test_wc_Sha256FinalRaw(void)
  8934. {
  8935. int flag = 0;
  8936. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  8937. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  8938. !defined(WOLFSSL_NO_HASH_RAW)
  8939. wc_Sha256 sha256;
  8940. byte* hash_test[3];
  8941. byte hash1[WC_SHA256_DIGEST_SIZE];
  8942. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  8943. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  8944. int times, i, ret;
  8945. /* Initialize */
  8946. ret = wc_InitSha256(&sha256);
  8947. if (ret != 0) {
  8948. flag = ret;
  8949. }
  8950. if (!flag) {
  8951. hash_test[0] = hash1;
  8952. hash_test[1] = hash2;
  8953. hash_test[2] = hash3;
  8954. }
  8955. times = sizeof(hash_test) / sizeof(byte*);
  8956. /* Good test args. */
  8957. printf(testingFmt, "wc_Sha256FinalRaw()");
  8958. for (i = 0; i < times; i++) {
  8959. if (!flag) {
  8960. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  8961. if (ret != 0) {
  8962. flag = WOLFSSL_FATAL_ERROR;
  8963. }
  8964. }
  8965. }
  8966. /* Test bad args. */
  8967. if (!flag ) {
  8968. ret = wc_Sha256FinalRaw(NULL, NULL);
  8969. if (ret != BAD_FUNC_ARG) {
  8970. flag = WOLFSSL_FATAL_ERROR;
  8971. }
  8972. }
  8973. if (!flag) {
  8974. ret = wc_Sha256FinalRaw(NULL, hash1);
  8975. if (ret != BAD_FUNC_ARG) {
  8976. flag = WOLFSSL_FATAL_ERROR;
  8977. }
  8978. }
  8979. if (!flag) {
  8980. ret = wc_Sha256FinalRaw(&sha256, NULL);
  8981. if (ret != BAD_FUNC_ARG) {
  8982. flag = WOLFSSL_FATAL_ERROR;
  8983. }
  8984. }
  8985. wc_Sha256Free(&sha256);
  8986. printf(resultFmt, flag == 0 ? passed : failed);
  8987. #endif
  8988. return flag;
  8989. } /* END test_wc_Sha256FinalRaw */
  8990. /*
  8991. * Unit test function for wc_Sha256GetFlags()
  8992. */
  8993. static int test_wc_Sha256GetFlags(void)
  8994. {
  8995. int flag = 0;
  8996. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  8997. wc_Sha256 sha256;
  8998. word32 flags = 0;
  8999. printf(testingFmt, "wc_Sha256GetFlags()");
  9000. /* Initialize */
  9001. flag = wc_InitSha256(&sha256);
  9002. if (flag == 0) {
  9003. flag = wc_Sha256GetFlags(&sha256, &flags);
  9004. }
  9005. if (flag == 0) {
  9006. if (flags & WC_HASH_FLAG_ISCOPY) {
  9007. flag = 0;
  9008. }
  9009. }
  9010. wc_Sha256Free(&sha256);
  9011. printf(resultFmt, flag == 0 ? passed : failed);
  9012. #endif
  9013. return flag;
  9014. } /* END test_wc_Sha256GetFlags */
  9015. /*
  9016. * Unit test function for wc_Sha256Free()
  9017. */
  9018. static int test_wc_Sha256Free(void)
  9019. {
  9020. int flag = 0;
  9021. #ifndef NO_SHA256
  9022. printf(testingFmt, "wc_Sha256Free()");
  9023. wc_Sha256Free(NULL);
  9024. printf(resultFmt, flag == 0 ? passed : failed);
  9025. #endif
  9026. return flag;
  9027. } /* END test_wc_Sha256Free */
  9028. /*
  9029. * Unit test function for wc_Sha256GetHash()
  9030. */
  9031. static int test_wc_Sha256GetHash(void)
  9032. {
  9033. int flag = 0;
  9034. #ifndef NO_SHA256
  9035. wc_Sha256 sha256;
  9036. byte hash1[WC_SHA256_DIGEST_SIZE];
  9037. printf(testingFmt, "wc_Sha256GetHash()");
  9038. /* Initialize */
  9039. flag = wc_InitSha256(&sha256);
  9040. if (flag == 0) {
  9041. flag = wc_Sha256GetHash(&sha256, hash1);
  9042. }
  9043. /*test bad arguments*/
  9044. if (flag == 0) {
  9045. flag = wc_Sha256GetHash(NULL, NULL);
  9046. if (flag == BAD_FUNC_ARG) {
  9047. flag = 0;
  9048. }
  9049. }
  9050. if (flag == 0) {
  9051. flag = wc_Sha256GetHash(NULL, hash1);
  9052. if (flag == BAD_FUNC_ARG) {
  9053. flag = 0;
  9054. }
  9055. }
  9056. if (flag == 0) {
  9057. flag = wc_Sha256GetHash(&sha256, NULL);
  9058. if (flag == BAD_FUNC_ARG) {
  9059. flag = 0;
  9060. }
  9061. }
  9062. wc_Sha256Free(&sha256);
  9063. printf(resultFmt, flag == 0 ? passed : failed);
  9064. #endif
  9065. return flag;
  9066. } /* END test_wc_Sha256GetHash */
  9067. /*
  9068. * Unit test function for wc_Sha256Copy()
  9069. */
  9070. static int test_wc_Sha256Copy(void)
  9071. {
  9072. int flag = 0;
  9073. #ifndef NO_SHA256
  9074. wc_Sha256 sha256;
  9075. wc_Sha256 temp;
  9076. printf(testingFmt, "wc_Sha256Copy()");
  9077. /* Initialize */
  9078. flag = wc_InitSha256(&sha256);
  9079. if (flag == 0) {
  9080. flag = wc_InitSha256(&temp);
  9081. }
  9082. if (flag == 0) {
  9083. flag = wc_Sha256Copy(&sha256, &temp);
  9084. }
  9085. /*test bad arguments*/
  9086. if (flag == 0) {
  9087. flag = wc_Sha256Copy(NULL, NULL);
  9088. if (flag == BAD_FUNC_ARG) {
  9089. flag = 0;
  9090. }
  9091. }
  9092. if (flag == 0) {
  9093. flag = wc_Sha256Copy(NULL, &temp);
  9094. if (flag == BAD_FUNC_ARG) {
  9095. flag = 0;
  9096. }
  9097. }
  9098. if (flag == 0) {
  9099. flag = wc_Sha256Copy(&sha256, NULL);
  9100. if (flag == BAD_FUNC_ARG) {
  9101. flag = 0;
  9102. }
  9103. }
  9104. wc_Sha256Free(&sha256);
  9105. wc_Sha256Free(&temp);
  9106. printf(resultFmt, flag == 0 ? passed : failed);
  9107. #endif
  9108. return flag;
  9109. } /* END test_wc_Sha256Copy */
  9110. /*
  9111. * Testing wc_InitSha512()
  9112. */
  9113. static int test_wc_InitSha512(void)
  9114. {
  9115. int flag = 0;
  9116. #ifdef WOLFSSL_SHA512
  9117. wc_Sha512 sha512;
  9118. int ret;
  9119. printf(testingFmt, "wc_InitSha512()");
  9120. /* Test good arg. */
  9121. ret = wc_InitSha512(&sha512);
  9122. if (ret != 0) {
  9123. flag = WOLFSSL_FATAL_ERROR;
  9124. }
  9125. /* Test bad arg. */
  9126. if (!flag) {
  9127. ret = wc_InitSha512(NULL);
  9128. if (ret != BAD_FUNC_ARG) {
  9129. flag = WOLFSSL_FATAL_ERROR;
  9130. }
  9131. }
  9132. wc_Sha512Free(&sha512);
  9133. printf(resultFmt, flag == 0 ? passed : failed);
  9134. #endif
  9135. return flag;
  9136. } /* END test_wc_InitSha512 */
  9137. /*
  9138. * wc_Sha512Update() test.
  9139. */
  9140. static int test_wc_Sha512Update(void)
  9141. {
  9142. int flag = 0;
  9143. #ifdef WOLFSSL_SHA512
  9144. wc_Sha512 sha512;
  9145. byte hash[WC_SHA512_DIGEST_SIZE];
  9146. testVector a, b, c;
  9147. int ret;
  9148. ret = wc_InitSha512(&sha512);
  9149. if (ret != 0) {
  9150. flag = ret;
  9151. }
  9152. printf(testingFmt, "wc_Sha512Update()");
  9153. /* Input. */
  9154. if (!flag) {
  9155. a.input = "a";
  9156. a.inLen = XSTRLEN(a.input);
  9157. ret = wc_Sha512Update(&sha512, NULL, 0);
  9158. if (ret != 0) {
  9159. flag = ret;
  9160. }
  9161. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  9162. if (ret != 0) {
  9163. flag = ret;
  9164. }
  9165. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9166. if (ret != 0) {
  9167. flag = ret;
  9168. }
  9169. ret = wc_Sha512Final(&sha512, hash);
  9170. if (ret != 0) {
  9171. flag = ret;
  9172. }
  9173. }
  9174. /* Update input. */
  9175. if (!flag) {
  9176. a.input = "abc";
  9177. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  9178. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  9179. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  9180. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  9181. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  9182. a.inLen = XSTRLEN(a.input);
  9183. a.outLen = XSTRLEN(a.output);
  9184. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9185. if (ret != 0) {
  9186. flag = ret;
  9187. }
  9188. }
  9189. if (!flag) {
  9190. ret = wc_Sha512Final(&sha512, hash);
  9191. if (ret != 0) {
  9192. flag = ret;
  9193. }
  9194. }
  9195. if (!flag) {
  9196. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  9197. flag = WOLFSSL_FATAL_ERROR;
  9198. }
  9199. }
  9200. /* Try passing in bad values */
  9201. if (!flag) {
  9202. b.input = NULL;
  9203. b.inLen = 0;
  9204. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  9205. if (ret != 0) {
  9206. flag = ret;
  9207. }
  9208. }
  9209. if (!flag) {
  9210. c.input = NULL;
  9211. c.inLen = WC_SHA512_DIGEST_SIZE;
  9212. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9213. if (ret != BAD_FUNC_ARG) {
  9214. flag = WOLFSSL_FATAL_ERROR;
  9215. }
  9216. }
  9217. if (!flag) {
  9218. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  9219. if (ret != BAD_FUNC_ARG) {
  9220. flag = WOLFSSL_FATAL_ERROR;
  9221. }
  9222. }
  9223. wc_Sha512Free(&sha512);
  9224. /* If not returned then the unit test passed test vectors. */
  9225. printf(resultFmt, flag == 0 ? passed : failed);
  9226. #endif
  9227. return flag;
  9228. } /* END test_wc_Sha512Update */
  9229. #ifdef WOLFSSL_SHA512
  9230. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9231. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9232. /* Perfoms test for
  9233. * - wc_Sha512Final/wc_Sha512FinalRaw
  9234. * - wc_Sha512_224Final/wc_Sha512_224Final
  9235. * - wc_Sha512_256Final/wc_Sha512_256Final
  9236. * parameter:
  9237. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  9238. * WC_HASH_TYPE_SHA512_256
  9239. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  9240. *return 0 on success
  9241. */
  9242. static int test_Sha512_Family_Final(int type, int isRaw)
  9243. {
  9244. wc_Sha512 sha512;
  9245. byte* hash_test[3];
  9246. byte hash1[WC_SHA512_DIGEST_SIZE];
  9247. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9248. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9249. int times, i, ret;
  9250. int(*initFp)(wc_Sha512*);
  9251. int(*finalFp)(wc_Sha512*, byte*);
  9252. void(*freeFp)(wc_Sha512*);
  9253. if (type == WC_HASH_TYPE_SHA512) {
  9254. initFp = wc_InitSha512;
  9255. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9256. !defined(WOLFSSL_NO_HASH_RAW)
  9257. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  9258. #else
  9259. finalFp = (isRaw)? NULL : wc_Sha512Final;
  9260. #endif
  9261. freeFp = wc_Sha512Free;
  9262. }
  9263. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9264. #if !defined(WOLFSSL_NOSHA512_224)
  9265. else if (type == WC_HASH_TYPE_SHA512_224) {
  9266. initFp = wc_InitSha512_224;
  9267. #if !defined(WOLFSSL_NO_HASH_RAW)
  9268. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  9269. #else
  9270. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  9271. #endif
  9272. freeFp = wc_Sha512_224Free;
  9273. }
  9274. #endif
  9275. #if !defined(WOLFSSL_NOSHA512_256)
  9276. else if (type == WC_HASH_TYPE_SHA512_256) {
  9277. initFp = wc_InitSha512_256;
  9278. #if !defined(WOLFSSL_NO_HASH_RAW)
  9279. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  9280. #else
  9281. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  9282. #endif
  9283. freeFp = wc_Sha512_256Free;
  9284. }
  9285. #endif
  9286. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9287. else
  9288. return BAD_FUNC_ARG;
  9289. /* Initialize */
  9290. ret = initFp(&sha512);
  9291. if (!ret) {
  9292. hash_test[0] = hash1;
  9293. hash_test[1] = hash2;
  9294. hash_test[2] = hash3;
  9295. }
  9296. times = sizeof(hash_test) / sizeof(byte *);
  9297. /* Good test args. */
  9298. for (i = 0; i < times && ret == 0; i++) {
  9299. ret = finalFp(&sha512, hash_test[i]);
  9300. }
  9301. /* Test bad args. */
  9302. if (!ret) {
  9303. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  9304. ret = WOLFSSL_FATAL_ERROR;
  9305. }
  9306. }
  9307. if (!ret) {
  9308. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  9309. ret = WOLFSSL_FATAL_ERROR;
  9310. }
  9311. }
  9312. if (!ret) {
  9313. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  9314. ret = WOLFSSL_FATAL_ERROR;
  9315. }
  9316. }
  9317. freeFp(&sha512);
  9318. return ret;
  9319. }
  9320. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  9321. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  9322. #endif /* WOLFSSL_SHA512 */
  9323. /*
  9324. * Unit test function for wc_Sha512Final()
  9325. */
  9326. static int test_wc_Sha512Final(void)
  9327. {
  9328. int flag = 0;
  9329. #ifdef WOLFSSL_SHA512
  9330. wc_Sha512 sha512;
  9331. byte* hash_test[3];
  9332. byte hash1[WC_SHA512_DIGEST_SIZE];
  9333. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9334. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9335. int times, i, ret;
  9336. /* Initialize */
  9337. ret = wc_InitSha512(&sha512);
  9338. if (ret != 0) {
  9339. flag = ret;
  9340. }
  9341. if (!flag) {
  9342. hash_test[0] = hash1;
  9343. hash_test[1] = hash2;
  9344. hash_test[2] = hash3;
  9345. }
  9346. times = sizeof(hash_test) / sizeof(byte *);
  9347. /* Good test args. */
  9348. printf(testingFmt, "wc_Sha512Final()");
  9349. for (i = 0; i < times; i++) {
  9350. if (!flag) {
  9351. ret = wc_Sha512Final(&sha512, hash_test[i]);
  9352. if (ret != 0) {
  9353. flag = WOLFSSL_FATAL_ERROR;
  9354. }
  9355. }
  9356. }
  9357. /* Test bad args. */
  9358. if (!flag) {
  9359. ret = wc_Sha512Final(NULL, NULL);
  9360. if (ret != BAD_FUNC_ARG) {
  9361. flag = WOLFSSL_FATAL_ERROR;
  9362. }
  9363. if (!flag) {}
  9364. ret = wc_Sha512Final(NULL, hash1);
  9365. if (ret != BAD_FUNC_ARG) {
  9366. flag = WOLFSSL_FATAL_ERROR;
  9367. }
  9368. }
  9369. if (!flag) {
  9370. ret = wc_Sha512Final(&sha512, NULL);
  9371. if (ret != BAD_FUNC_ARG) {
  9372. flag = WOLFSSL_FATAL_ERROR;
  9373. }
  9374. }
  9375. wc_Sha512Free(&sha512);
  9376. printf(resultFmt, flag == 0 ? passed : failed);
  9377. #endif
  9378. return flag;
  9379. } /* END test_wc_Sha512Final */
  9380. /*
  9381. * Unit test function for wc_Sha512GetFlags()
  9382. */
  9383. static int test_wc_Sha512GetFlags(void)
  9384. {
  9385. int flag = 0;
  9386. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  9387. wc_Sha512 sha512;
  9388. word32 flags = 0;
  9389. printf(testingFmt, "wc_Sha512GetFlags()");
  9390. /* Initialize */
  9391. flag = wc_InitSha512(&sha512);
  9392. if (flag == 0) {
  9393. flag = wc_Sha512GetFlags(&sha512, &flags);
  9394. }
  9395. if (flag == 0) {
  9396. if (flags & WC_HASH_FLAG_ISCOPY) {
  9397. flag = 0;
  9398. }
  9399. }
  9400. wc_Sha512Free(&sha512);
  9401. printf(resultFmt, flag == 0 ? passed : failed);
  9402. #endif
  9403. return flag;
  9404. } /* END test_wc_Sha512GetFlags */
  9405. /*
  9406. * Unit test function for wc_Sha512FinalRaw()
  9407. */
  9408. static int test_wc_Sha512FinalRaw(void)
  9409. {
  9410. int flag = 0;
  9411. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  9412. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  9413. !defined(WOLFSSL_NO_HASH_RAW)
  9414. wc_Sha512 sha512;
  9415. byte* hash_test[3];
  9416. byte hash1[WC_SHA512_DIGEST_SIZE];
  9417. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9418. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9419. int times, i, ret;
  9420. /* Initialize */
  9421. ret = wc_InitSha512(&sha512);
  9422. if (ret != 0) {
  9423. flag = ret;
  9424. }
  9425. if (!flag) {
  9426. hash_test[0] = hash1;
  9427. hash_test[1] = hash2;
  9428. hash_test[2] = hash3;
  9429. }
  9430. times = sizeof(hash_test) / sizeof(byte*);
  9431. /* Good test args. */
  9432. printf(testingFmt, "wc_Sha512FinalRaw()");
  9433. for (i = 0; i < times; i++) {
  9434. if (!flag) {
  9435. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  9436. if (ret != 0) {
  9437. flag = WOLFSSL_FATAL_ERROR;
  9438. }
  9439. }
  9440. }
  9441. /* Test bad args. */
  9442. if (!flag ) {
  9443. ret = wc_Sha512FinalRaw(NULL, NULL);
  9444. if (ret != BAD_FUNC_ARG) {
  9445. flag = WOLFSSL_FATAL_ERROR;
  9446. }
  9447. }
  9448. if (!flag) {
  9449. ret = wc_Sha512FinalRaw(NULL, hash1);
  9450. if (ret != BAD_FUNC_ARG) {
  9451. flag = WOLFSSL_FATAL_ERROR;
  9452. }
  9453. }
  9454. if (!flag) {
  9455. ret = wc_Sha512FinalRaw(&sha512, NULL);
  9456. if (ret != BAD_FUNC_ARG) {
  9457. flag = WOLFSSL_FATAL_ERROR;
  9458. }
  9459. }
  9460. wc_Sha512Free(&sha512);
  9461. printf(resultFmt, flag == 0 ? passed : failed);
  9462. #endif
  9463. return flag;
  9464. } /* END test_wc_Sha512FinalRaw */
  9465. /*
  9466. * Unit test function for wc_Sha512Free()
  9467. */
  9468. static int test_wc_Sha512Free(void)
  9469. {
  9470. int flag = 0;
  9471. #ifdef WOLFSSL_SHA512
  9472. printf(testingFmt, "wc_Sha512Free()");
  9473. wc_Sha512Free(NULL);
  9474. printf(resultFmt, flag == 0 ? passed : failed);
  9475. #endif
  9476. return flag;
  9477. } /* END test_wc_Sha512Free */
  9478. #ifdef WOLFSSL_SHA512
  9479. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9480. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9481. static int test_Sha512_Family_GetHash(int type )
  9482. {
  9483. int flag = 0;
  9484. int(*initFp)(wc_Sha512*);
  9485. int(*ghashFp)(wc_Sha512*, byte*);
  9486. wc_Sha512 sha512;
  9487. byte hash1[WC_SHA512_DIGEST_SIZE];
  9488. if (type == WC_HASH_TYPE_SHA512) {
  9489. initFp = wc_InitSha512;
  9490. ghashFp = wc_Sha512GetHash;
  9491. }
  9492. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9493. #if !defined(WOLFSSL_NOSHA512_224)
  9494. else if (type == WC_HASH_TYPE_SHA512_224) {
  9495. initFp = wc_InitSha512_224;
  9496. ghashFp = wc_Sha512_224GetHash;
  9497. }
  9498. #endif
  9499. #if !defined(WOLFSSL_NOSHA512_256)
  9500. else if (type == WC_HASH_TYPE_SHA512_256) {
  9501. initFp = wc_InitSha512_256;
  9502. ghashFp = wc_Sha512_256GetHash;
  9503. }
  9504. #endif
  9505. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9506. else {
  9507. initFp = NULL;
  9508. ghashFp = NULL;
  9509. }
  9510. if (initFp == NULL || ghashFp == NULL)
  9511. return WOLFSSL_FATAL_ERROR;
  9512. if (!flag) {
  9513. flag = initFp(&sha512);
  9514. }
  9515. if (!flag) {
  9516. flag = ghashFp(&sha512, hash1);
  9517. }
  9518. /*test bad arguments*/
  9519. if (!flag) {
  9520. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  9521. flag = WOLFSSL_FATAL_ERROR;
  9522. }
  9523. if (!flag) {
  9524. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  9525. flag = WOLFSSL_FATAL_ERROR;
  9526. }
  9527. if (!flag) {
  9528. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  9529. flag = WOLFSSL_FATAL_ERROR;
  9530. }
  9531. wc_Sha512Free(&sha512);
  9532. return flag;
  9533. }
  9534. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  9535. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  9536. #endif /* WOLFSSL_SHA512 */
  9537. /*
  9538. * Unit test function for wc_Sha512GetHash()
  9539. */
  9540. static int test_wc_Sha512GetHash(void)
  9541. {
  9542. int flag = 0;
  9543. #ifdef WOLFSSL_SHA512
  9544. wc_Sha512 sha512;
  9545. byte hash1[WC_SHA512_DIGEST_SIZE];
  9546. printf(testingFmt, "wc_Sha512GetHash()");
  9547. /* Initialize */
  9548. flag = wc_InitSha512(&sha512);
  9549. if (flag == 0) {
  9550. flag = wc_Sha512GetHash(&sha512, hash1);
  9551. }
  9552. /*test bad arguments*/
  9553. if (flag == 0) {
  9554. flag = wc_Sha512GetHash(NULL, NULL);
  9555. if (flag == BAD_FUNC_ARG) {
  9556. flag = 0;
  9557. }
  9558. }
  9559. if (flag == 0) {
  9560. flag = wc_Sha512GetHash(NULL, hash1);
  9561. if (flag == BAD_FUNC_ARG) {
  9562. flag = 0;
  9563. }
  9564. }
  9565. if (flag == 0) {
  9566. flag = wc_Sha512GetHash(&sha512, NULL);
  9567. if (flag == BAD_FUNC_ARG) {
  9568. flag = 0;
  9569. }
  9570. }
  9571. wc_Sha512Free(&sha512);
  9572. printf(resultFmt, flag == 0 ? passed : failed);
  9573. #endif
  9574. return flag;
  9575. } /* END test_wc_Sha512GetHash */
  9576. /*
  9577. * Unit test function for wc_Sha512Copy()
  9578. */
  9579. static int test_wc_Sha512Copy(void)
  9580. {
  9581. int flag = 0;
  9582. #ifdef WOLFSSL_SHA512
  9583. wc_Sha512 sha512;
  9584. wc_Sha512 temp;
  9585. printf(testingFmt, "wc_Sha512Copy()");
  9586. /* Initialize */
  9587. flag = wc_InitSha512(&sha512);
  9588. if (flag == 0) {
  9589. flag = wc_InitSha512(&temp);
  9590. }
  9591. if (flag == 0) {
  9592. flag = wc_Sha512Copy(&sha512, &temp);
  9593. }
  9594. /*test bad arguments*/
  9595. if (flag == 0) {
  9596. flag = wc_Sha512Copy(NULL, NULL);
  9597. if (flag == BAD_FUNC_ARG) {
  9598. flag = 0;
  9599. }
  9600. }
  9601. if (flag == 0) {
  9602. flag = wc_Sha512Copy(NULL, &temp);
  9603. if (flag == BAD_FUNC_ARG) {
  9604. flag = 0;
  9605. }
  9606. }
  9607. if (flag == 0) {
  9608. flag = wc_Sha512Copy(&sha512, NULL);
  9609. if (flag == BAD_FUNC_ARG) {
  9610. flag = 0;
  9611. }
  9612. }
  9613. wc_Sha512Free(&sha512);
  9614. wc_Sha512Free(&temp);
  9615. printf(resultFmt, flag == 0 ? passed : failed);
  9616. #endif
  9617. return flag;
  9618. } /* END test_wc_Sha512Copy */
  9619. static int test_wc_InitSha512_224(void)
  9620. {
  9621. int flag = 0;
  9622. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9623. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9624. wc_Sha512 sha512;
  9625. int ret;
  9626. printf(testingFmt, "wc_InitSha512_224()");
  9627. /* Test good arg. */
  9628. ret = wc_InitSha512_224(&sha512);
  9629. if (ret != 0) {
  9630. flag = WOLFSSL_FATAL_ERROR;
  9631. }
  9632. /* Test bad arg. */
  9633. if (!flag) {
  9634. ret = wc_InitSha512_224(NULL);
  9635. if (ret != BAD_FUNC_ARG) {
  9636. flag = WOLFSSL_FATAL_ERROR;
  9637. }
  9638. }
  9639. wc_Sha512_224Free(&sha512);
  9640. printf(resultFmt, flag == 0 ? passed : failed);
  9641. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9642. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9643. return flag;
  9644. }
  9645. static int test_wc_Sha512_224Update(void)
  9646. {
  9647. int flag = 0;
  9648. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9649. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9650. wc_Sha512 sha512;
  9651. byte hash[WC_SHA512_DIGEST_SIZE];
  9652. testVector a, c;
  9653. int ret;
  9654. ret = wc_InitSha512_224(&sha512);
  9655. if (ret != 0) {
  9656. flag = ret;
  9657. }
  9658. printf(testingFmt, "wc_Sha512_224Update()");
  9659. /* Input. */
  9660. if (!flag) {
  9661. a.input = "a";
  9662. a.inLen = XSTRLEN(a.input);
  9663. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  9664. if (ret != 0) {
  9665. flag = ret;
  9666. }
  9667. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  9668. if (ret != 0) {
  9669. flag = ret;
  9670. }
  9671. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9672. if (ret != 0) {
  9673. flag = ret;
  9674. }
  9675. ret = wc_Sha512_224Final(&sha512, hash);
  9676. if (ret != 0) {
  9677. flag = ret;
  9678. }
  9679. }
  9680. /* Update input. */
  9681. if (!flag) {
  9682. a.input = "abc";
  9683. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  9684. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  9685. a.inLen = XSTRLEN(a.input);
  9686. a.outLen = XSTRLEN(a.output);
  9687. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9688. if (ret != 0) {
  9689. flag = ret;
  9690. }
  9691. }
  9692. if (!flag) {
  9693. ret = wc_Sha512_224Final(&sha512, hash);
  9694. if (ret != 0) {
  9695. flag = ret;
  9696. }
  9697. }
  9698. if (!flag) {
  9699. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  9700. flag = WOLFSSL_FATAL_ERROR;
  9701. }
  9702. }
  9703. if (!flag) {
  9704. c.input = NULL;
  9705. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  9706. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9707. if (ret != BAD_FUNC_ARG) {
  9708. flag = WOLFSSL_FATAL_ERROR;
  9709. }
  9710. }
  9711. if (!flag) {
  9712. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  9713. if (ret != BAD_FUNC_ARG) {
  9714. flag = WOLFSSL_FATAL_ERROR;
  9715. }
  9716. }
  9717. wc_Sha512_224Free(&sha512);
  9718. /* If not returned then the unit test passed test vectors. */
  9719. printf(resultFmt, flag == 0 ? passed : failed);
  9720. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9721. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9722. return flag;
  9723. }
  9724. static int test_wc_Sha512_224Final(void)
  9725. {
  9726. int flag = 0;
  9727. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9728. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9729. printf(testingFmt, "wc_Sha512_224Final()");
  9730. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  9731. printf(resultFmt, flag == 0 ? passed : failed);
  9732. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9733. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9734. return flag;
  9735. }
  9736. static int test_wc_Sha512_224GetFlags(void)
  9737. {
  9738. int flag = 0;
  9739. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9740. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  9741. wc_Sha512 sha512, copy;
  9742. word32 flags = 0;
  9743. printf(testingFmt, "wc_Sha512_224GetFlags()");
  9744. /* Initialize */
  9745. flag = wc_InitSha512_224(&sha512);
  9746. if (!flag) {
  9747. flag = wc_InitSha512_224(&copy);
  9748. }
  9749. if (!flag) {
  9750. flag = wc_Sha512_224Copy(&sha512, &copy);
  9751. }
  9752. if (!flag) {
  9753. flag = wc_Sha512_224GetFlags(&copy, &flags);
  9754. }
  9755. if (!flag) {
  9756. if (flags & WC_HASH_FLAG_ISCOPY)
  9757. flag = 0;
  9758. else
  9759. flag = WOLFSSL_FATAL_ERROR;
  9760. }
  9761. wc_Sha512_224Free(&copy);
  9762. wc_Sha512_224Free(&sha512);
  9763. printf(resultFmt, flag == 0 ? passed : failed);
  9764. #endif
  9765. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9766. return flag;
  9767. }
  9768. static int test_wc_Sha512_224FinalRaw(void)
  9769. {
  9770. int flag = 0;
  9771. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9772. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  9773. !defined(WOLFSSL_NO_HASH_RAW)
  9774. printf(testingFmt, "wc_Sha512_224FinalRaw()");
  9775. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  9776. printf(resultFmt, flag == 0 ? passed : failed);
  9777. #endif
  9778. return flag;
  9779. }
  9780. static int test_wc_Sha512_224Free(void)
  9781. {
  9782. int flag = 0;
  9783. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9784. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9785. printf(testingFmt, "wc_Sha512_224Free()");
  9786. wc_Sha512_224Free(NULL);
  9787. printf(resultFmt, passed);
  9788. #endif
  9789. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9790. return flag;
  9791. }
  9792. static int test_wc_Sha512_224GetHash(void)
  9793. {
  9794. int flag = 0;
  9795. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9796. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9797. printf(testingFmt, "wc_Sha512_224GetHash()");
  9798. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  9799. printf(resultFmt, flag == 0 ? passed : failed);
  9800. #endif
  9801. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9802. return flag;
  9803. }
  9804. static int test_wc_Sha512_224Copy(void)
  9805. {
  9806. int flag = 0;
  9807. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9808. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9809. wc_Sha512 sha512;
  9810. wc_Sha512 temp;
  9811. printf(testingFmt, "wc_Sha512_224Copy()");
  9812. /* Initialize */
  9813. flag = wc_InitSha512_224(&sha512);
  9814. if (flag == 0) {
  9815. flag = wc_InitSha512_224(&temp);
  9816. }
  9817. if (flag == 0) {
  9818. flag = wc_Sha512_224Copy(&sha512, &temp);
  9819. }
  9820. /*test bad arguments*/
  9821. if (flag == 0) {
  9822. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  9823. flag = WOLFSSL_FATAL_ERROR;
  9824. }
  9825. if (flag == 0) {
  9826. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  9827. flag = WOLFSSL_FATAL_ERROR;
  9828. }
  9829. if (flag == 0) {
  9830. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  9831. flag = WOLFSSL_FATAL_ERROR;
  9832. }
  9833. wc_Sha512_224Free(&sha512);
  9834. wc_Sha512_224Free(&temp);
  9835. printf(resultFmt, flag == 0 ? passed : failed);
  9836. #endif
  9837. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9838. return flag;
  9839. }
  9840. static int test_wc_InitSha512_256(void)
  9841. {
  9842. int flag = 0;
  9843. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9844. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9845. wc_Sha512 sha512;
  9846. int ret;
  9847. printf(testingFmt, "wc_InitSha512_256()");
  9848. /* Test good arg. */
  9849. ret = wc_InitSha512_256(&sha512);
  9850. if (ret != 0) {
  9851. flag = WOLFSSL_FATAL_ERROR;
  9852. }
  9853. /* Test bad arg. */
  9854. if (!flag) {
  9855. ret = wc_InitSha512_256(NULL);
  9856. if (ret != BAD_FUNC_ARG) {
  9857. flag = WOLFSSL_FATAL_ERROR;
  9858. }
  9859. }
  9860. wc_Sha512_256Free(&sha512);
  9861. printf(resultFmt, flag == 0 ? passed : failed);
  9862. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9863. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9864. return flag;
  9865. }
  9866. static int test_wc_Sha512_256Update(void)
  9867. {
  9868. int flag = 0;
  9869. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9870. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9871. wc_Sha512 sha512;
  9872. byte hash[WC_SHA512_DIGEST_SIZE];
  9873. testVector a, c;
  9874. int ret;
  9875. ret = wc_InitSha512_256(&sha512);
  9876. if (ret != 0) {
  9877. flag = ret;
  9878. }
  9879. printf(testingFmt, "wc_Sha512_256Update()");
  9880. /* Input. */
  9881. if (!flag) {
  9882. a.input = "a";
  9883. a.inLen = XSTRLEN(a.input);
  9884. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  9885. if (ret != 0) {
  9886. flag = ret;
  9887. }
  9888. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  9889. if (ret != 0) {
  9890. flag = ret;
  9891. }
  9892. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9893. if (ret != 0) {
  9894. flag = ret;
  9895. }
  9896. ret = wc_Sha512_256Final(&sha512, hash);
  9897. if (ret != 0) {
  9898. flag = ret;
  9899. }
  9900. }
  9901. /* Update input. */
  9902. if (!flag) {
  9903. a.input = "abc";
  9904. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  9905. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  9906. "\x07\xe7\xaf\x23";
  9907. a.inLen = XSTRLEN(a.input);
  9908. a.outLen = XSTRLEN(a.output);
  9909. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9910. if (ret != 0) {
  9911. flag = ret;
  9912. }
  9913. }
  9914. if (!flag) {
  9915. ret = wc_Sha512_256Final(&sha512, hash);
  9916. if (ret != 0) {
  9917. flag = ret;
  9918. }
  9919. }
  9920. if (!flag) {
  9921. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  9922. flag = WOLFSSL_FATAL_ERROR;
  9923. }
  9924. }
  9925. if (!flag) {
  9926. c.input = NULL;
  9927. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  9928. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9929. if (ret != BAD_FUNC_ARG) {
  9930. flag = WOLFSSL_FATAL_ERROR;
  9931. }
  9932. }
  9933. if (!flag) {
  9934. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  9935. if (ret != BAD_FUNC_ARG) {
  9936. flag = WOLFSSL_FATAL_ERROR;
  9937. }
  9938. }
  9939. wc_Sha512_256Free(&sha512);
  9940. /* If not returned then the unit test passed test vectors. */
  9941. printf(resultFmt, flag == 0 ? passed : failed);
  9942. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9943. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9944. return flag;
  9945. }
  9946. static int test_wc_Sha512_256Final(void)
  9947. {
  9948. int flag = 0;
  9949. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9950. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9951. printf(testingFmt, "wc_Sha512_256Final()");
  9952. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  9953. printf(resultFmt, flag == 0 ? passed : failed);
  9954. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9955. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9956. return flag;
  9957. }
  9958. static int test_wc_Sha512_256GetFlags(void)
  9959. {
  9960. int flag = 0;
  9961. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9962. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  9963. wc_Sha512 sha512, copy;
  9964. word32 flags = 0;
  9965. printf(testingFmt, "wc_Sha512_256GetFlags()");
  9966. /* Initialize */
  9967. flag = wc_InitSha512_256(&sha512);
  9968. if (!flag ) {
  9969. flag = wc_InitSha512_256(&copy);
  9970. }
  9971. if (!flag ) {
  9972. flag = wc_Sha512_256Copy(&sha512, &copy);
  9973. }
  9974. if (!flag ) {
  9975. flag = wc_Sha512_256GetFlags(&copy, &flags);
  9976. }
  9977. if (!flag) {
  9978. if (flags & WC_HASH_FLAG_ISCOPY)
  9979. flag = 0;
  9980. else
  9981. flag = WOLFSSL_FATAL_ERROR;
  9982. }
  9983. wc_Sha512_256Free(&sha512);
  9984. printf(resultFmt, flag == 0 ? passed : failed);
  9985. #endif
  9986. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9987. return flag;
  9988. }
  9989. static int test_wc_Sha512_256FinalRaw(void)
  9990. {
  9991. int flag = 0;
  9992. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9993. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  9994. !defined(WOLFSSL_NO_HASH_RAW)
  9995. printf(testingFmt, "wc_Sha512_256FinalRaw()");
  9996. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  9997. printf(resultFmt, flag == 0 ? passed : failed);
  9998. #endif
  9999. return flag;
  10000. }
  10001. static int test_wc_Sha512_256Free(void)
  10002. {
  10003. int flag = 0;
  10004. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10005. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10006. printf(testingFmt, "wc_Sha512_256Free()");
  10007. wc_Sha512_256Free(NULL);
  10008. printf(resultFmt, passed);
  10009. #endif
  10010. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10011. return flag;
  10012. }
  10013. static int test_wc_Sha512_256GetHash(void)
  10014. {
  10015. int flag = 0;
  10016. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10017. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10018. printf(testingFmt, "wc_Sha512_256GetHash()");
  10019. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  10020. printf(resultFmt, flag == 0 ? passed : failed);
  10021. #endif
  10022. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10023. return flag;
  10024. }
  10025. static int test_wc_Sha512_256Copy(void)
  10026. {
  10027. int flag = 0;
  10028. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10029. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10030. wc_Sha512 sha512;
  10031. wc_Sha512 temp;
  10032. printf(testingFmt, "wc_Sha512_256Copy()");
  10033. /* Initialize */
  10034. flag = wc_InitSha512_256(&sha512);
  10035. if (flag == 0) {
  10036. flag = wc_InitSha512_256(&temp);
  10037. }
  10038. if (flag == 0) {
  10039. flag = wc_Sha512_256Copy(&sha512, &temp);
  10040. }
  10041. /*test bad arguments*/
  10042. if (flag == 0) {
  10043. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  10044. flag = WOLFSSL_FATAL_ERROR;
  10045. }
  10046. if (flag == 0) {
  10047. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  10048. flag = WOLFSSL_FATAL_ERROR;
  10049. }
  10050. if (flag == 0) {
  10051. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10052. flag = WOLFSSL_FATAL_ERROR;
  10053. }
  10054. wc_Sha512_256Free(&sha512);
  10055. wc_Sha512_256Free(&temp);
  10056. printf(resultFmt, flag == 0 ? passed : failed);
  10057. #endif
  10058. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10059. return flag;
  10060. }
  10061. /*
  10062. * Testing wc_InitSha384()
  10063. */
  10064. static int test_wc_InitSha384(void)
  10065. {
  10066. int flag = 0;
  10067. #ifdef WOLFSSL_SHA384
  10068. wc_Sha384 sha384;
  10069. int ret;
  10070. printf(testingFmt, "wc_InitSha384()");
  10071. /* Test good arg. */
  10072. ret = wc_InitSha384(&sha384);
  10073. if (ret != 0) {
  10074. flag = WOLFSSL_FATAL_ERROR;
  10075. }
  10076. /* Test bad arg. */
  10077. if (!flag) {
  10078. ret = wc_InitSha384(NULL);
  10079. if (ret != BAD_FUNC_ARG) {
  10080. flag = WOLFSSL_FATAL_ERROR;
  10081. }
  10082. }
  10083. wc_Sha384Free(&sha384);
  10084. printf(resultFmt, flag == 0 ? passed : failed);
  10085. #endif
  10086. return flag;
  10087. } /* END test_wc_InitSha384 */
  10088. /*
  10089. * test wc_Sha384Update()
  10090. */
  10091. static int test_wc_Sha384Update(void)
  10092. {
  10093. int flag = 0;
  10094. #ifdef WOLFSSL_SHA384
  10095. wc_Sha384 sha384;
  10096. byte hash[WC_SHA384_DIGEST_SIZE];
  10097. testVector a, b, c;
  10098. int ret;
  10099. ret = wc_InitSha384(&sha384);
  10100. if (ret != 0) {
  10101. flag = ret;
  10102. }
  10103. printf(testingFmt, "wc_Sha384Update()");
  10104. /* Input */
  10105. if (!flag) {
  10106. a.input = "a";
  10107. a.inLen = XSTRLEN(a.input);
  10108. ret = wc_Sha384Update(&sha384, NULL, 0);
  10109. if (ret != 0) {
  10110. flag = ret;
  10111. }
  10112. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  10113. if (ret != 0) {
  10114. flag = ret;
  10115. }
  10116. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10117. if (ret != 0) {
  10118. flag = ret;
  10119. }
  10120. }
  10121. if (!flag) {
  10122. ret = wc_Sha384Final(&sha384, hash);
  10123. if (ret != 0) {
  10124. flag = ret;
  10125. }
  10126. }
  10127. /* Update input. */
  10128. if (!flag) {
  10129. a.input = "abc";
  10130. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  10131. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  10132. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  10133. "\xc8\x25\xa7";
  10134. a.inLen = XSTRLEN(a.input);
  10135. a.outLen = XSTRLEN(a.output);
  10136. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10137. if (ret != 0) {
  10138. flag = ret;
  10139. }
  10140. }
  10141. if (!flag) {
  10142. ret = wc_Sha384Final(&sha384, hash);
  10143. if (ret != 0) {
  10144. flag = ret;
  10145. }
  10146. }
  10147. if (!flag) {
  10148. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  10149. flag = WOLFSSL_FATAL_ERROR;
  10150. }
  10151. }
  10152. /* Pass in bad values. */
  10153. if (!flag) {
  10154. b.input = NULL;
  10155. b.inLen = 0;
  10156. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  10157. if (ret != 0) {
  10158. flag = ret;
  10159. }
  10160. }
  10161. if (!flag) {
  10162. c.input = NULL;
  10163. c.inLen = WC_SHA384_DIGEST_SIZE;
  10164. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  10165. if (ret != BAD_FUNC_ARG) {
  10166. flag = WOLFSSL_FATAL_ERROR;
  10167. }
  10168. }
  10169. if (!flag) {
  10170. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  10171. if (ret != BAD_FUNC_ARG) {
  10172. flag = WOLFSSL_FATAL_ERROR;
  10173. }
  10174. }
  10175. wc_Sha384Free(&sha384);
  10176. /* If not returned then the unit test passed test vectors. */
  10177. printf(resultFmt, flag == 0 ? passed : failed);
  10178. #endif
  10179. return flag;
  10180. } /* END test_wc_Sha384Update */
  10181. /*
  10182. * Unit test function for wc_Sha384Final();
  10183. */
  10184. static int test_wc_Sha384Final(void)
  10185. {
  10186. int flag = 0;
  10187. #ifdef WOLFSSL_SHA384
  10188. wc_Sha384 sha384;
  10189. byte* hash_test[3];
  10190. byte hash1[WC_SHA384_DIGEST_SIZE];
  10191. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10192. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10193. int times, i, ret;
  10194. /* Initialize */
  10195. ret = wc_InitSha384(&sha384);
  10196. if (ret) {
  10197. flag = ret;
  10198. }
  10199. if (!flag) {
  10200. hash_test[0] = hash1;
  10201. hash_test[1] = hash2;
  10202. hash_test[2] = hash3;
  10203. }
  10204. times = sizeof(hash_test) / sizeof(byte*);
  10205. /* Good test args. */
  10206. printf(testingFmt, "wc_Sha384Final()");
  10207. for (i = 0; i < times; i++) {
  10208. if (!flag) {
  10209. ret = wc_Sha384Final(&sha384, hash_test[i]);
  10210. if (ret != 0) {
  10211. flag = WOLFSSL_FATAL_ERROR;
  10212. }
  10213. }
  10214. }
  10215. /* Test bad args. */
  10216. if (!flag) {
  10217. ret = wc_Sha384Final(NULL, NULL);
  10218. if (ret != BAD_FUNC_ARG) {
  10219. flag = WOLFSSL_FATAL_ERROR;
  10220. }
  10221. }
  10222. if (!flag) {
  10223. ret = wc_Sha384Final(NULL, hash1);
  10224. if (ret != BAD_FUNC_ARG) {
  10225. flag = WOLFSSL_FATAL_ERROR;
  10226. }
  10227. }
  10228. if (!flag) {
  10229. ret = wc_Sha384Final(&sha384, NULL);
  10230. if (ret != BAD_FUNC_ARG) {
  10231. flag = WOLFSSL_FATAL_ERROR;
  10232. }
  10233. }
  10234. wc_Sha384Free(&sha384);
  10235. printf(resultFmt, flag == 0 ? passed : failed);
  10236. #endif
  10237. return flag;
  10238. } /* END test_wc_Sha384Final */
  10239. /*
  10240. * Unit test function for wc_Sha384GetFlags()
  10241. */
  10242. static int test_wc_Sha384GetFlags(void)
  10243. {
  10244. int flag = 0;
  10245. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  10246. wc_Sha384 sha384;
  10247. word32 flags = 0;
  10248. printf(testingFmt, "wc_Sha384GetFlags()");
  10249. /* Initialize */
  10250. flag = wc_InitSha384(&sha384);
  10251. if (flag == 0) {
  10252. flag = wc_Sha384GetFlags(&sha384, &flags);
  10253. }
  10254. if (flag == 0) {
  10255. if (flags & WC_HASH_FLAG_ISCOPY) {
  10256. flag = 0;
  10257. }
  10258. }
  10259. wc_Sha384Free(&sha384);
  10260. printf(resultFmt, flag == 0 ? passed : failed);
  10261. #endif
  10262. return flag;
  10263. } /* END test_wc_Sha384GetFlags */
  10264. /*
  10265. * Unit test function for wc_Sha384FinalRaw()
  10266. */
  10267. static int test_wc_Sha384FinalRaw(void)
  10268. {
  10269. int flag = 0;
  10270. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10271. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  10272. !defined(WOLFSSL_NO_HASH_RAW)
  10273. wc_Sha384 sha384;
  10274. byte* hash_test[3];
  10275. byte hash1[WC_SHA384_DIGEST_SIZE];
  10276. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10277. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10278. int times, i, ret;
  10279. /* Initialize */
  10280. ret = wc_InitSha384(&sha384);
  10281. if (ret != 0) {
  10282. flag = ret;
  10283. }
  10284. if (!flag) {
  10285. hash_test[0] = hash1;
  10286. hash_test[1] = hash2;
  10287. hash_test[2] = hash3;
  10288. }
  10289. times = sizeof(hash_test) / sizeof(byte*);
  10290. /* Good test args. */
  10291. printf(testingFmt, "wc_Sha384FinalRaw()");
  10292. for (i = 0; i < times; i++) {
  10293. if (!flag) {
  10294. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  10295. if (ret != 0) {
  10296. flag = WOLFSSL_FATAL_ERROR;
  10297. }
  10298. }
  10299. }
  10300. /* Test bad args. */
  10301. if (!flag ) {
  10302. ret = wc_Sha384FinalRaw(NULL, NULL);
  10303. if (ret != BAD_FUNC_ARG) {
  10304. flag = WOLFSSL_FATAL_ERROR;
  10305. }
  10306. }
  10307. if (!flag) {
  10308. ret = wc_Sha384FinalRaw(NULL, hash1);
  10309. if (ret != BAD_FUNC_ARG) {
  10310. flag = WOLFSSL_FATAL_ERROR;
  10311. }
  10312. }
  10313. if (!flag) {
  10314. ret = wc_Sha384FinalRaw(&sha384, NULL);
  10315. if (ret != BAD_FUNC_ARG) {
  10316. flag = WOLFSSL_FATAL_ERROR;
  10317. }
  10318. }
  10319. wc_Sha384Free(&sha384);
  10320. printf(resultFmt, flag == 0 ? passed : failed);
  10321. #endif
  10322. return flag;
  10323. } /* END test_wc_Sha384FinalRaw */
  10324. /*
  10325. * Unit test function for wc_Sha384Free()
  10326. */
  10327. static int test_wc_Sha384Free(void)
  10328. {
  10329. int flag = 0;
  10330. #ifdef WOLFSSL_SHA384
  10331. printf(testingFmt, "wc_Sha384Free()");
  10332. wc_Sha384Free(NULL);
  10333. printf(resultFmt, flag == 0 ? passed : failed);
  10334. #endif
  10335. return flag;
  10336. } /* END test_wc_Sha384Free */
  10337. /*
  10338. * Unit test function for wc_Sha384GetHash()
  10339. */
  10340. static int test_wc_Sha384GetHash(void)
  10341. {
  10342. int flag = 0;
  10343. #ifdef WOLFSSL_SHA384
  10344. wc_Sha384 sha384;
  10345. byte hash1[WC_SHA384_DIGEST_SIZE];
  10346. printf(testingFmt, "wc_Sha384GetHash()");
  10347. /* Initialize */
  10348. flag = wc_InitSha384(&sha384);
  10349. if (flag == 0) {
  10350. flag = wc_Sha384GetHash(&sha384, hash1);
  10351. }
  10352. /*test bad arguments*/
  10353. if (flag == 0) {
  10354. flag = wc_Sha384GetHash(NULL, NULL);
  10355. if (flag == BAD_FUNC_ARG) {
  10356. flag = 0;
  10357. }
  10358. }
  10359. if (flag == 0) {
  10360. flag = wc_Sha384GetHash(NULL, hash1);
  10361. if (flag == BAD_FUNC_ARG) {
  10362. flag = 0;
  10363. }
  10364. }
  10365. if (flag == 0) {
  10366. flag = wc_Sha384GetHash(&sha384, NULL);
  10367. if (flag == BAD_FUNC_ARG) {
  10368. flag = 0;
  10369. }
  10370. }
  10371. wc_Sha384Free(&sha384);
  10372. printf(resultFmt, flag == 0 ? passed : failed);
  10373. #endif
  10374. return flag;
  10375. } /* END test_wc_Sha384GetHash */
  10376. /*
  10377. * Unit test function for wc_Sha384Copy()
  10378. */
  10379. static int test_wc_Sha384Copy(void)
  10380. {
  10381. int flag = 0;
  10382. #ifdef WOLFSSL_SHA384
  10383. wc_Sha384 sha384;
  10384. wc_Sha384 temp;
  10385. printf(testingFmt, "wc_Sha384Copy()");
  10386. /* Initialize */
  10387. flag = wc_InitSha384(&sha384);
  10388. if (flag == 0) {
  10389. flag = wc_InitSha384(&temp);
  10390. }
  10391. if (flag == 0) {
  10392. flag = wc_Sha384Copy(&sha384, &temp);
  10393. }
  10394. /*test bad arguments*/
  10395. if (flag == 0) {
  10396. flag = wc_Sha384Copy(NULL, NULL);
  10397. if (flag == BAD_FUNC_ARG) {
  10398. flag = 0;
  10399. }
  10400. }
  10401. if (flag == 0) {
  10402. flag = wc_Sha384Copy(NULL, &temp);
  10403. if (flag == BAD_FUNC_ARG) {
  10404. flag = 0;
  10405. }
  10406. }
  10407. if (flag == 0) {
  10408. flag = wc_Sha384Copy(&sha384, NULL);
  10409. if (flag == BAD_FUNC_ARG) {
  10410. flag = 0;
  10411. }
  10412. }
  10413. wc_Sha384Free(&sha384);
  10414. wc_Sha384Free(&temp);
  10415. printf(resultFmt, flag == 0 ? passed : failed);
  10416. #endif
  10417. return flag;
  10418. } /* END test_wc_Sha384Copy */
  10419. /*
  10420. * Testing wc_InitSha224();
  10421. */
  10422. static int test_wc_InitSha224(void)
  10423. {
  10424. int flag = 0;
  10425. #ifdef WOLFSSL_SHA224
  10426. wc_Sha224 sha224;
  10427. int ret;
  10428. printf(testingFmt, "wc_InitSha224()");
  10429. /* Test good arg. */
  10430. ret = wc_InitSha224(&sha224);
  10431. if (ret != 0) {
  10432. flag = WOLFSSL_FATAL_ERROR;
  10433. }
  10434. /* Test bad arg. */
  10435. if (!flag) {
  10436. ret = wc_InitSha224(NULL);
  10437. if (ret != BAD_FUNC_ARG) {
  10438. flag = WOLFSSL_FATAL_ERROR;
  10439. }
  10440. }
  10441. wc_Sha224Free(&sha224);
  10442. printf(resultFmt, flag == 0 ? passed : failed);
  10443. #endif
  10444. return flag;
  10445. } /* END test_wc_InitSha224 */
  10446. /*
  10447. * Unit test on wc_Sha224Update
  10448. */
  10449. static int test_wc_Sha224Update(void)
  10450. {
  10451. int flag = 0;
  10452. #ifdef WOLFSSL_SHA224
  10453. wc_Sha224 sha224;
  10454. byte hash[WC_SHA224_DIGEST_SIZE];
  10455. testVector a, b, c;
  10456. int ret;
  10457. ret = wc_InitSha224(&sha224);
  10458. if (ret != 0) {
  10459. flag = ret;
  10460. }
  10461. printf(testingFmt, "wc_Sha224Update()");
  10462. /* Input. */
  10463. if (!flag) {
  10464. a.input = "a";
  10465. a.inLen = XSTRLEN(a.input);
  10466. ret = wc_Sha224Update(&sha224, NULL, 0);
  10467. if (ret != 0) {
  10468. flag = ret;
  10469. }
  10470. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  10471. if (ret != 0) {
  10472. flag = ret;
  10473. }
  10474. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  10475. if (ret != 0) {
  10476. flag = ret;
  10477. }
  10478. }
  10479. if (!flag) {
  10480. ret = wc_Sha224Final(&sha224, hash);
  10481. if (ret != 0) {
  10482. flag = ret;
  10483. }
  10484. }
  10485. /* Update input. */
  10486. if (!flag) {
  10487. a.input = "abc";
  10488. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  10489. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  10490. a.inLen = XSTRLEN(a.input);
  10491. a.outLen = XSTRLEN(a.output);
  10492. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  10493. if (ret != 0) {
  10494. flag = ret;
  10495. }
  10496. }
  10497. if (!flag) {
  10498. ret = wc_Sha224Final(&sha224, hash);
  10499. if (ret != 0) {
  10500. flag = ret;
  10501. }
  10502. }
  10503. if (!flag) {
  10504. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  10505. flag = WOLFSSL_FATAL_ERROR;
  10506. }
  10507. }
  10508. /* Pass in bad values. */
  10509. if (!flag) {
  10510. b.input = NULL;
  10511. b.inLen = 0;
  10512. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  10513. if (ret != 0) {
  10514. flag = ret;
  10515. }
  10516. }
  10517. if (!flag) {
  10518. c.input = NULL;
  10519. c.inLen = WC_SHA224_DIGEST_SIZE;
  10520. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  10521. if (ret != BAD_FUNC_ARG) {
  10522. flag = WOLFSSL_FATAL_ERROR;
  10523. }
  10524. }
  10525. if (!flag) {
  10526. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  10527. if (ret != BAD_FUNC_ARG) {
  10528. flag = WOLFSSL_FATAL_ERROR;
  10529. }
  10530. }
  10531. wc_Sha224Free(&sha224);
  10532. /* If not returned then the unit test passed test vectors. */
  10533. printf(resultFmt, flag == 0 ? passed : failed);
  10534. #endif
  10535. return flag;
  10536. } /* END test_wc_Sha224Update */
  10537. /*
  10538. * Unit test for wc_Sha224Final();
  10539. */
  10540. static int test_wc_Sha224Final(void)
  10541. {
  10542. int flag = 0;
  10543. #ifdef WOLFSSL_SHA224
  10544. wc_Sha224 sha224;
  10545. byte* hash_test[3];
  10546. byte hash1[WC_SHA224_DIGEST_SIZE];
  10547. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  10548. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  10549. int times, i, ret;
  10550. /* Initialize */
  10551. ret = wc_InitSha224(&sha224);
  10552. if (ret) {
  10553. flag = ret;
  10554. }
  10555. if (!flag) {
  10556. hash_test[0] = hash1;
  10557. hash_test[1] = hash2;
  10558. hash_test[2] = hash3;
  10559. }
  10560. times = sizeof(hash_test) / sizeof(byte*);
  10561. /* Good test args. */
  10562. printf(testingFmt, "wc_sha224Final()");
  10563. /* Testing oversized buffers. */
  10564. for (i = 0; i < times; i++) {
  10565. if (!flag) {
  10566. ret = wc_Sha224Final(&sha224, hash_test[i]);
  10567. if (ret != 0) {
  10568. flag = WOLFSSL_FATAL_ERROR;
  10569. }
  10570. }
  10571. }
  10572. /* Test bad args. */
  10573. if (!flag) {
  10574. ret = wc_Sha224Final(NULL, NULL);
  10575. if (ret != BAD_FUNC_ARG) {
  10576. flag = WOLFSSL_FATAL_ERROR;
  10577. }
  10578. }
  10579. if (!flag) {
  10580. ret = wc_Sha224Final(NULL, hash1);
  10581. if (ret != BAD_FUNC_ARG) {
  10582. flag = WOLFSSL_FATAL_ERROR;
  10583. }
  10584. }
  10585. if (!flag) {
  10586. ret = wc_Sha224Final(&sha224, NULL);
  10587. if (ret != BAD_FUNC_ARG) {
  10588. flag = WOLFSSL_FATAL_ERROR;
  10589. }
  10590. }
  10591. wc_Sha224Free(&sha224);
  10592. printf(resultFmt, flag == 0 ? passed : failed);
  10593. #endif
  10594. return flag;
  10595. } /* END test_wc_Sha224Final */
  10596. /*
  10597. * Unit test function for wc_Sha224SetFlags()
  10598. */
  10599. static int test_wc_Sha224SetFlags(void)
  10600. {
  10601. int flag = 0;
  10602. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  10603. wc_Sha224 sha224;
  10604. word32 flags = 0;
  10605. printf(testingFmt, "wc_Sha224SetFlags()");
  10606. /* Initialize */
  10607. flag = wc_InitSha224(&sha224);
  10608. if (flag == 0) {
  10609. flag = wc_Sha224SetFlags(&sha224, flags);
  10610. }
  10611. if (flag == 0) {
  10612. if (flags & WC_HASH_FLAG_ISCOPY) {
  10613. flag = 0;
  10614. }
  10615. }
  10616. wc_Sha224Free(&sha224);
  10617. printf(resultFmt, flag == 0 ? passed : failed);
  10618. #endif
  10619. return flag;
  10620. } /* END test_wc_Sha224SetFlags */
  10621. /*
  10622. * Unit test function for wc_Sha224GetFlags()
  10623. */
  10624. static int test_wc_Sha224GetFlags(void)
  10625. {
  10626. int flag = 0;
  10627. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  10628. wc_Sha224 sha224;
  10629. word32 flags = 0;
  10630. printf(testingFmt, "wc_Sha224GetFlags()");
  10631. /* Initialize */
  10632. flag = wc_InitSha224(&sha224);
  10633. if (flag == 0) {
  10634. flag = wc_Sha224GetFlags(&sha224, &flags);
  10635. }
  10636. if (flag == 0) {
  10637. if (flags & WC_HASH_FLAG_ISCOPY) {
  10638. flag = 0;
  10639. }
  10640. }
  10641. wc_Sha224Free(&sha224);
  10642. printf(resultFmt, flag == 0 ? passed : failed);
  10643. #endif
  10644. return flag;
  10645. } /* END test_wc_Sha224GetFlags */
  10646. /*
  10647. * Unit test function for wc_Sha224Free()
  10648. */
  10649. static int test_wc_Sha224Free(void)
  10650. {
  10651. int flag = 0;
  10652. #ifdef WOLFSSL_SHA224
  10653. printf(testingFmt, "wc_Sha224Free()");
  10654. wc_Sha224Free(NULL);
  10655. printf(resultFmt, flag == 0 ? passed : failed);
  10656. #endif
  10657. return flag;
  10658. } /* END test_wc_Sha224Free */
  10659. /*
  10660. * Unit test function for wc_Sha224GetHash()
  10661. */
  10662. static int test_wc_Sha224GetHash(void)
  10663. {
  10664. int flag = 0;
  10665. #ifdef WOLFSSL_SHA224
  10666. wc_Sha224 sha224;
  10667. byte hash1[WC_SHA224_DIGEST_SIZE];
  10668. printf(testingFmt, "wc_Sha224GetHash()");
  10669. /* Initialize */
  10670. flag = wc_InitSha224(&sha224);
  10671. if (flag == 0) {
  10672. flag = wc_Sha224GetHash(&sha224, hash1);
  10673. }
  10674. /*test bad arguments*/
  10675. if (flag == 0) {
  10676. flag = wc_Sha224GetHash(NULL, NULL);
  10677. if (flag == BAD_FUNC_ARG) {
  10678. flag = 0;
  10679. }
  10680. }
  10681. if (flag == 0) {
  10682. flag = wc_Sha224GetHash(NULL, hash1);
  10683. if (flag == BAD_FUNC_ARG) {
  10684. flag = 0;
  10685. }
  10686. }
  10687. if (flag == 0) {
  10688. flag = wc_Sha224GetHash(&sha224, NULL);
  10689. if (flag == BAD_FUNC_ARG) {
  10690. flag = 0;
  10691. }
  10692. }
  10693. wc_Sha224Free(&sha224);
  10694. printf(resultFmt, flag == 0 ? passed : failed);
  10695. #endif
  10696. return flag;
  10697. } /* END test_wc_Sha224GetHash */
  10698. /*
  10699. * Unit test function for wc_Sha224Copy()
  10700. */
  10701. static int test_wc_Sha224Copy(void)
  10702. {
  10703. int flag = 0;
  10704. #ifdef WOLFSSL_SHA224
  10705. wc_Sha224 sha224;
  10706. wc_Sha224 temp;
  10707. printf(testingFmt, "wc_Sha224Copy()");
  10708. /* Initialize */
  10709. flag = wc_InitSha224(&sha224);
  10710. if (flag == 0) {
  10711. flag = wc_InitSha224(&temp);
  10712. }
  10713. if (flag == 0) {
  10714. flag = wc_Sha224Copy(&sha224, &temp);
  10715. }
  10716. /*test bad arguments*/
  10717. if (flag == 0) {
  10718. flag = wc_Sha224Copy(NULL, NULL);
  10719. if (flag == BAD_FUNC_ARG) {
  10720. flag = 0;
  10721. }
  10722. }
  10723. if (flag == 0) {
  10724. flag = wc_Sha224Copy(NULL, &temp);
  10725. if (flag == BAD_FUNC_ARG) {
  10726. flag = 0;
  10727. }
  10728. }
  10729. if (flag == 0) {
  10730. flag = wc_Sha224Copy(&sha224, NULL);
  10731. if (flag == BAD_FUNC_ARG) {
  10732. flag = 0;
  10733. }
  10734. }
  10735. wc_Sha224Free(&sha224);
  10736. wc_Sha224Free(&temp);
  10737. printf(resultFmt, flag == 0 ? passed : failed);
  10738. #endif
  10739. return flag;
  10740. } /* END test_wc_Sha224Copy */
  10741. /*
  10742. * Testing wc_InitRipeMd()
  10743. */
  10744. static int test_wc_InitRipeMd(void)
  10745. {
  10746. int flag = 0;
  10747. #ifdef WOLFSSL_RIPEMD
  10748. RipeMd ripemd;
  10749. int ret;
  10750. printf(testingFmt, "wc_InitRipeMd()");
  10751. /* Test good arg. */
  10752. ret = wc_InitRipeMd(&ripemd);
  10753. if (ret != 0) {
  10754. flag = WOLFSSL_FATAL_ERROR;
  10755. }
  10756. /* Test bad arg. */
  10757. if (!flag) {
  10758. ret = wc_InitRipeMd(NULL);
  10759. if (ret != BAD_FUNC_ARG) {
  10760. flag = WOLFSSL_FATAL_ERROR;
  10761. }
  10762. }
  10763. printf(resultFmt, flag == 0 ? passed : failed);
  10764. #endif
  10765. return flag;
  10766. } /* END test_wc_InitRipeMd */
  10767. /*
  10768. * Testing wc_RipeMdUpdate()
  10769. */
  10770. static int test_wc_RipeMdUpdate(void)
  10771. {
  10772. int flag = 0;
  10773. #ifdef WOLFSSL_RIPEMD
  10774. RipeMd ripemd;
  10775. byte hash[RIPEMD_DIGEST_SIZE];
  10776. testVector a, b, c;
  10777. int ret;
  10778. ret = wc_InitRipeMd(&ripemd);
  10779. if (ret != 0) {
  10780. flag = ret;
  10781. }
  10782. printf(testingFmt, "wc_RipeMdUpdate()");
  10783. /* Input */
  10784. if (!flag) {
  10785. a.input = "a";
  10786. a.inLen = XSTRLEN(a.input);
  10787. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  10788. if (ret != 0) {
  10789. flag = ret;
  10790. }
  10791. }
  10792. if (!flag) {
  10793. ret = wc_RipeMdFinal(&ripemd, hash);
  10794. if (ret != 0) {
  10795. flag = ret;
  10796. }
  10797. }
  10798. /* Update input. */
  10799. if (!flag) {
  10800. a.input = "abc";
  10801. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  10802. "\xb0\x87\xf1\x5a\x0b\xfc";
  10803. a.inLen = XSTRLEN(a.input);
  10804. a.outLen = XSTRLEN(a.output);
  10805. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  10806. if (ret != 0) {
  10807. flag = ret;
  10808. }
  10809. }
  10810. if (!flag) {
  10811. ret = wc_RipeMdFinal(&ripemd, hash);
  10812. if (ret != 0) {
  10813. flag = ret;
  10814. }
  10815. }
  10816. if (!flag) {
  10817. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  10818. flag = WOLFSSL_FATAL_ERROR;
  10819. }
  10820. }
  10821. /* Pass in bad values. */
  10822. if (!flag) {
  10823. b.input = NULL;
  10824. b.inLen = 0;
  10825. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  10826. if (ret != 0) {
  10827. flag = ret;
  10828. }
  10829. }
  10830. if (!flag) {
  10831. c.input = NULL;
  10832. c.inLen = RIPEMD_DIGEST_SIZE;
  10833. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  10834. if (ret != BAD_FUNC_ARG) {
  10835. flag = WOLFSSL_FATAL_ERROR;
  10836. }
  10837. }
  10838. if (!flag) {
  10839. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10840. if (ret != BAD_FUNC_ARG) {
  10841. flag = WOLFSSL_FATAL_ERROR;
  10842. }
  10843. }
  10844. printf(resultFmt, flag == 0 ? passed : failed);
  10845. #endif
  10846. return flag;
  10847. } /* END test_wc_RipeMdUdpate */
  10848. /*
  10849. * Unit test function for wc_RipeMdFinal()
  10850. */
  10851. static int test_wc_RipeMdFinal(void)
  10852. {
  10853. int flag = 0;
  10854. #ifdef WOLFSSL_RIPEMD
  10855. RipeMd ripemd;
  10856. byte* hash_test[3];
  10857. byte hash1[RIPEMD_DIGEST_SIZE];
  10858. byte hash2[2*RIPEMD_DIGEST_SIZE];
  10859. byte hash3[5*RIPEMD_DIGEST_SIZE];
  10860. int times, i, ret;
  10861. /* Initialize */
  10862. ret = wc_InitRipeMd(&ripemd);
  10863. if (ret != 0) {
  10864. flag = ret;
  10865. }
  10866. if (!flag) {
  10867. hash_test[0] = hash1;
  10868. hash_test[1] = hash2;
  10869. hash_test[2] = hash3;
  10870. }
  10871. times = sizeof(hash_test) / sizeof(byte*);
  10872. /* Good test args. */
  10873. printf(testingFmt, "wc_RipeMdFinal()");
  10874. /* Testing oversized buffers. */
  10875. for (i = 0; i < times; i++) {
  10876. if (!flag) {
  10877. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  10878. if (ret != 0) {
  10879. flag = WOLFSSL_FATAL_ERROR;
  10880. }
  10881. }
  10882. }
  10883. /* Test bad args. */
  10884. if (!flag) {
  10885. ret = wc_RipeMdFinal(NULL, NULL);
  10886. if (ret != BAD_FUNC_ARG) {
  10887. flag = WOLFSSL_FATAL_ERROR;
  10888. }
  10889. }
  10890. if (!flag) {
  10891. ret = wc_RipeMdFinal(NULL, hash1);
  10892. if (ret != BAD_FUNC_ARG) {
  10893. flag = WOLFSSL_FATAL_ERROR;
  10894. }
  10895. }
  10896. if (!flag) {
  10897. ret = wc_RipeMdFinal(&ripemd, NULL);
  10898. if (ret != BAD_FUNC_ARG) {
  10899. flag = WOLFSSL_FATAL_ERROR;
  10900. }
  10901. }
  10902. printf(resultFmt, flag == 0 ? passed : failed);
  10903. #endif
  10904. return flag;
  10905. } /* END test_wc_RipeMdFinal */
  10906. /*
  10907. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  10908. * wc_InitSha3_512
  10909. */
  10910. static int test_wc_InitSha3(void)
  10911. {
  10912. int ret = 0;
  10913. #if defined(WOLFSSL_SHA3)
  10914. wc_Sha3 sha3;
  10915. (void)sha3;
  10916. #if !defined(WOLFSSL_NOSHA3_224)
  10917. printf(testingFmt, "wc_InitSha3_224()");
  10918. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  10919. /* Test bad args. */
  10920. if (ret == 0) {
  10921. ret = wc_InitSha3_224(NULL, HEAP_HINT, devId);
  10922. if (ret == BAD_FUNC_ARG) {
  10923. ret = 0;
  10924. } else if (ret == 0) {
  10925. ret = WOLFSSL_FATAL_ERROR;
  10926. }
  10927. }
  10928. wc_Sha3_224_Free(&sha3);
  10929. printf(resultFmt, ret == 0 ? passed : failed);
  10930. #endif /* NOSHA3_224 */
  10931. #if !defined(WOLFSSL_NOSHA3_256)
  10932. if (ret == 0) {
  10933. printf(testingFmt, "wc_InitSha3_256()");
  10934. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  10935. /* Test bad args. */
  10936. if (ret == 0) {
  10937. ret = wc_InitSha3_256(NULL, HEAP_HINT, devId);
  10938. if (ret == BAD_FUNC_ARG) {
  10939. ret = 0;
  10940. } else if (ret == 0) {
  10941. ret = WOLFSSL_FATAL_ERROR;
  10942. }
  10943. }
  10944. wc_Sha3_256_Free(&sha3);
  10945. printf(resultFmt, ret == 0 ? passed : failed);
  10946. } /* END sha3_256 */
  10947. #endif /* NOSHA3_256 */
  10948. #if !defined(WOLFSSL_NOSHA3_384)
  10949. if (ret == 0) {
  10950. printf(testingFmt, "wc_InitSha3_384()");
  10951. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  10952. /* Test bad args. */
  10953. if (ret == 0) {
  10954. ret = wc_InitSha3_384(NULL, HEAP_HINT, devId);
  10955. if (ret == BAD_FUNC_ARG) {
  10956. ret = 0;
  10957. } else if (ret == 0) {
  10958. ret = WOLFSSL_FATAL_ERROR;
  10959. }
  10960. }
  10961. wc_Sha3_384_Free(&sha3);
  10962. printf(resultFmt, ret == 0 ? passed : failed);
  10963. } /* END sha3_384 */
  10964. #endif /* NOSHA3_384 */
  10965. #if !defined(WOLFSSL_NOSHA3_512)
  10966. if (ret == 0) {
  10967. printf(testingFmt, "wc_InitSha3_512()");
  10968. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  10969. /* Test bad args. */
  10970. if (ret == 0) {
  10971. ret = wc_InitSha3_512(NULL, HEAP_HINT, devId);
  10972. if (ret == BAD_FUNC_ARG) {
  10973. ret = 0;
  10974. } else if (ret == 0) {
  10975. ret = WOLFSSL_FATAL_ERROR;
  10976. }
  10977. }
  10978. wc_Sha3_512_Free(&sha3);
  10979. printf(resultFmt, ret == 0 ? passed : failed);
  10980. } /* END sha3_512 */
  10981. #endif /* NOSHA3_512 */
  10982. #endif
  10983. return ret;
  10984. } /* END test_wc_InitSha3 */
  10985. /*
  10986. * Testing wc_Sha3_Update()
  10987. */
  10988. static int testing_wc_Sha3_Update(void)
  10989. {
  10990. int ret = 0;
  10991. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  10992. !defined(WOLFSSL_AFALG_XILINX)
  10993. wc_Sha3 sha3;
  10994. byte msg[] = "Everybody's working for the weekend.";
  10995. byte msg2[] = "Everybody gets Friday off.";
  10996. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  10997. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  10998. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  10999. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11000. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11001. word32 msglen = sizeof(msg) - 1;
  11002. word32 msg2len = sizeof(msg2);
  11003. word32 msgCmplen = sizeof(msgCmp);
  11004. #if !defined(WOLFSSL_NOSHA3_224)
  11005. printf(testingFmt, "wc_Sha3_224_Update()");
  11006. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  11007. if (ret != 0) {
  11008. return ret;
  11009. }
  11010. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  11011. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11012. ret = WOLFSSL_FATAL_ERROR;
  11013. }
  11014. if (ret == 0) {
  11015. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11016. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11017. ret = WOLFSSL_FATAL_ERROR;
  11018. }
  11019. }
  11020. /* Pass bad args. */
  11021. if (ret == 0) {
  11022. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  11023. if (ret == BAD_FUNC_ARG) {
  11024. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  11025. }
  11026. if (ret == BAD_FUNC_ARG) {
  11027. wc_Sha3_224_Free(&sha3);
  11028. if (wc_InitSha3_224(&sha3, HEAP_HINT, devId)) {
  11029. return ret;
  11030. }
  11031. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  11032. if (ret == 0) {
  11033. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11034. }
  11035. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11036. ret = WOLFSSL_FATAL_ERROR;
  11037. }
  11038. }
  11039. }
  11040. wc_Sha3_224_Free(&sha3);
  11041. printf(resultFmt, ret == 0 ? passed : failed);
  11042. #endif /* SHA3_224 */
  11043. #if !defined(WOLFSSL_NOSHA3_256)
  11044. if (ret == 0) {
  11045. printf(testingFmt, "wc_Sha3_256_Update()");
  11046. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  11047. if (ret != 0) {
  11048. return ret;
  11049. }
  11050. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  11051. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11052. ret = WOLFSSL_FATAL_ERROR;
  11053. }
  11054. if (ret == 0) {
  11055. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11056. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11057. ret = WOLFSSL_FATAL_ERROR;
  11058. }
  11059. }
  11060. /* Pass bad args. */
  11061. if (ret == 0) {
  11062. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  11063. if (ret == BAD_FUNC_ARG) {
  11064. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  11065. }
  11066. if (ret == BAD_FUNC_ARG) {
  11067. wc_Sha3_256_Free(&sha3);
  11068. if (wc_InitSha3_256(&sha3, HEAP_HINT, devId)) {
  11069. return ret;
  11070. }
  11071. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  11072. if (ret == 0) {
  11073. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11074. }
  11075. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11076. ret = WOLFSSL_FATAL_ERROR;
  11077. }
  11078. }
  11079. }
  11080. wc_Sha3_256_Free(&sha3);
  11081. printf(resultFmt, ret == 0 ? passed : failed);
  11082. }
  11083. #endif /* SHA3_256 */
  11084. #if !defined(WOLFSSL_NOSHA3_384)
  11085. if (ret == 0) {
  11086. printf(testingFmt, "wc_Sha3_384_Update()");
  11087. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  11088. if (ret != 0) {
  11089. return ret;
  11090. }
  11091. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  11092. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11093. ret = WOLFSSL_FATAL_ERROR;
  11094. }
  11095. if (ret == 0) {
  11096. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11097. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11098. ret = WOLFSSL_FATAL_ERROR;
  11099. }
  11100. }
  11101. /* Pass bad args. */
  11102. if (ret == 0) {
  11103. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  11104. if (ret == BAD_FUNC_ARG) {
  11105. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  11106. }
  11107. if (ret == BAD_FUNC_ARG) {
  11108. wc_Sha3_384_Free(&sha3);
  11109. if (wc_InitSha3_384(&sha3, HEAP_HINT, devId)) {
  11110. return ret;
  11111. }
  11112. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  11113. if (ret == 0) {
  11114. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11115. }
  11116. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11117. ret = WOLFSSL_FATAL_ERROR;
  11118. }
  11119. }
  11120. }
  11121. wc_Sha3_384_Free(&sha3);
  11122. printf(resultFmt, ret == 0 ? passed : failed);
  11123. }
  11124. #endif /* SHA3_384 */
  11125. #if !defined(WOLFSSL_NOSHA3_512)
  11126. if (ret == 0) {
  11127. printf(testingFmt, "wc_Sha3_512_Update()");
  11128. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  11129. if (ret != 0) {
  11130. return ret;
  11131. }
  11132. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  11133. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11134. ret = WOLFSSL_FATAL_ERROR;
  11135. }
  11136. if (ret == 0) {
  11137. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11138. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11139. ret = WOLFSSL_FATAL_ERROR;
  11140. }
  11141. }
  11142. /* Pass bad args. */
  11143. if (ret == 0) {
  11144. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  11145. if (ret == BAD_FUNC_ARG) {
  11146. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  11147. }
  11148. if (ret == BAD_FUNC_ARG) {
  11149. wc_Sha3_512_Free(&sha3);
  11150. if (wc_InitSha3_512(&sha3, HEAP_HINT, devId)) {
  11151. return ret;
  11152. }
  11153. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  11154. if (ret == 0) {
  11155. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11156. }
  11157. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11158. ret = WOLFSSL_FATAL_ERROR;
  11159. }
  11160. }
  11161. }
  11162. wc_Sha3_512_Free(&sha3);
  11163. printf(resultFmt, ret == 0 ? passed : failed);
  11164. }
  11165. #endif /* SHA3_512 */
  11166. #endif /* WOLFSSL_SHA3 */
  11167. return ret;
  11168. } /* END testing_wc_Sha3_Update */
  11169. /*
  11170. * Testing wc_Sha3_224_Final()
  11171. */
  11172. static int test_wc_Sha3_224_Final(void)
  11173. {
  11174. int ret = 0;
  11175. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11176. wc_Sha3 sha3;
  11177. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11178. "nopnopq";
  11179. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  11180. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  11181. "\x64\xea\xd0\xfc\xce\x33";
  11182. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11183. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  11184. /* Init stack variables. */
  11185. XMEMSET(hash, 0, sizeof(hash));
  11186. printf(testingFmt, "wc_Sha3_224_Final()");
  11187. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  11188. if (ret != 0) {
  11189. return ret;
  11190. }
  11191. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11192. if (ret == 0) {
  11193. ret = wc_Sha3_224_Final(&sha3, hash);
  11194. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11195. ret = WOLFSSL_FATAL_ERROR;
  11196. }
  11197. }
  11198. /* Test bad args. */
  11199. if (ret == 0) {
  11200. ret = wc_Sha3_224_Final(NULL, hash);
  11201. if (ret == 0) {
  11202. ret = wc_Sha3_224_Final(&sha3, NULL);
  11203. }
  11204. if (ret == BAD_FUNC_ARG) {
  11205. ret = 0;
  11206. } else if (ret == 0) {
  11207. ret = WOLFSSL_FATAL_ERROR;
  11208. }
  11209. }
  11210. wc_Sha3_224_Free(&sha3);
  11211. printf(resultFmt, ret == 0 ? passed : failed);
  11212. if (ret == 0) {
  11213. printf(testingFmt, "wc_Sha3_224_GetHash()");
  11214. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  11215. if (ret != 0) {
  11216. return ret;
  11217. }
  11218. /* Init stack variables. */
  11219. XMEMSET(hash, 0, sizeof(hash));
  11220. XMEMSET(hashRet, 0, sizeof(hashRet));
  11221. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11222. if (ret == 0) {
  11223. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  11224. }
  11225. if (ret == 0) {
  11226. ret = wc_Sha3_224_Final(&sha3, hash);
  11227. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11228. ret = WOLFSSL_FATAL_ERROR;
  11229. }
  11230. }
  11231. if (ret == 0) {
  11232. /* Test bad args. */
  11233. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  11234. if (ret == BAD_FUNC_ARG) {
  11235. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  11236. }
  11237. if (ret == BAD_FUNC_ARG) {
  11238. ret = 0;
  11239. } else if (ret == 0) {
  11240. ret = WOLFSSL_FATAL_ERROR;
  11241. }
  11242. }
  11243. printf(resultFmt, ret == 0 ? passed : failed);
  11244. }
  11245. wc_Sha3_224_Free(&sha3);
  11246. #endif
  11247. return ret;
  11248. } /* END test_wc_Sha3_224_Final */
  11249. /*
  11250. * Testing wc_Sha3_256_Final()
  11251. */
  11252. static int test_wc_Sha3_256_Final(void)
  11253. {
  11254. int ret = 0;
  11255. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  11256. wc_Sha3 sha3;
  11257. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11258. "nopnopq";
  11259. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  11260. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  11261. "\xdd\x97\x49\x6d\x33\x76";
  11262. byte hash[WC_SHA3_256_DIGEST_SIZE];
  11263. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  11264. /* Init stack variables. */
  11265. XMEMSET(hash, 0, sizeof(hash));
  11266. printf(testingFmt, "wc_Sha3_256_Final()");
  11267. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  11268. if (ret != 0) {
  11269. return ret;
  11270. }
  11271. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11272. if (ret == 0) {
  11273. ret = wc_Sha3_256_Final(&sha3, hash);
  11274. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11275. ret = WOLFSSL_FATAL_ERROR;
  11276. }
  11277. }
  11278. /* Test bad args. */
  11279. if (ret == 0) {
  11280. ret = wc_Sha3_256_Final(NULL, hash);
  11281. if (ret == 0) {
  11282. ret = wc_Sha3_256_Final(&sha3, NULL);
  11283. }
  11284. if (ret == BAD_FUNC_ARG) {
  11285. ret = 0;
  11286. } else if (ret == 0) {
  11287. ret = WOLFSSL_FATAL_ERROR;
  11288. }
  11289. }
  11290. wc_Sha3_256_Free(&sha3);
  11291. printf(resultFmt, ret == 0 ? passed : failed);
  11292. if (ret == 0) {
  11293. printf(testingFmt, "wc_Sha3_256_GetHash()");
  11294. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  11295. if (ret != 0) {
  11296. return ret;
  11297. }
  11298. /* Init stack variables. */
  11299. XMEMSET(hash, 0, sizeof(hash));
  11300. XMEMSET(hashRet, 0, sizeof(hashRet));
  11301. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11302. if (ret == 0) {
  11303. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  11304. }
  11305. if (ret == 0) {
  11306. ret = wc_Sha3_256_Final(&sha3, hash);
  11307. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11308. ret = WOLFSSL_FATAL_ERROR;
  11309. }
  11310. }
  11311. if (ret == 0) {
  11312. /* Test bad args. */
  11313. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  11314. if (ret == BAD_FUNC_ARG) {
  11315. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  11316. }
  11317. if (ret == BAD_FUNC_ARG) {
  11318. ret = 0;
  11319. } else if (ret == 0) {
  11320. ret = WOLFSSL_FATAL_ERROR;
  11321. }
  11322. }
  11323. printf(resultFmt, ret == 0 ? passed : failed);
  11324. }
  11325. wc_Sha3_256_Free(&sha3);
  11326. #endif
  11327. return ret;
  11328. } /* END test_wc_Sha3_256_Final */
  11329. /*
  11330. * Testing wc_Sha3_384_Final()
  11331. */
  11332. static int test_wc_Sha3_384_Final(void)
  11333. {
  11334. int ret = 0;
  11335. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  11336. wc_Sha3 sha3;
  11337. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11338. "nopnopq";
  11339. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  11340. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  11341. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  11342. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  11343. byte hash[WC_SHA3_384_DIGEST_SIZE];
  11344. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  11345. /* Init stack variables. */
  11346. XMEMSET(hash, 0, sizeof(hash));
  11347. printf(testingFmt, "wc_Sha3_384_Final()");
  11348. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  11349. if (ret != 0) {
  11350. return ret;
  11351. }
  11352. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11353. if (ret == 0) {
  11354. ret = wc_Sha3_384_Final(&sha3, hash);
  11355. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  11356. ret = WOLFSSL_FATAL_ERROR;
  11357. }
  11358. }
  11359. /* Test bad args. */
  11360. if (ret == 0) {
  11361. ret = wc_Sha3_384_Final(NULL, hash);
  11362. if (ret == 0) {
  11363. ret = wc_Sha3_384_Final(&sha3, NULL);
  11364. }
  11365. if (ret == BAD_FUNC_ARG) {
  11366. ret = 0;
  11367. } else if (ret == 0) {
  11368. ret = WOLFSSL_FATAL_ERROR;
  11369. }
  11370. }
  11371. wc_Sha3_384_Free(&sha3);
  11372. printf(resultFmt, ret == 0 ? passed : failed);
  11373. if (ret == 0) {
  11374. printf(testingFmt, "wc_Sha3_384_GetHash()");
  11375. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  11376. if (ret != 0) {
  11377. return ret;
  11378. }
  11379. /* Init stack variables. */
  11380. XMEMSET(hash, 0, sizeof(hash));
  11381. XMEMSET(hashRet, 0, sizeof(hashRet));
  11382. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11383. if (ret == 0) {
  11384. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  11385. }
  11386. if (ret == 0) {
  11387. ret = wc_Sha3_384_Final(&sha3, hash);
  11388. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  11389. ret = WOLFSSL_FATAL_ERROR;
  11390. }
  11391. }
  11392. if (ret == 0) {
  11393. /* Test bad args. */
  11394. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  11395. if (ret == BAD_FUNC_ARG) {
  11396. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  11397. }
  11398. if (ret == BAD_FUNC_ARG) {
  11399. ret = 0;
  11400. } else if (ret == 0) {
  11401. ret = WOLFSSL_FATAL_ERROR;
  11402. }
  11403. }
  11404. printf(resultFmt, ret == 0 ? passed : failed);
  11405. }
  11406. wc_Sha3_384_Free(&sha3);
  11407. #endif
  11408. return ret;
  11409. } /* END test_wc_Sha3_384_Final */
  11410. /*
  11411. * Testing wc_Sha3_512_Final()
  11412. */
  11413. static int test_wc_Sha3_512_Final(void)
  11414. {
  11415. int ret = 0;
  11416. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  11417. !defined(WOLFSSL_NOSHA3_384)
  11418. wc_Sha3 sha3;
  11419. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11420. "nopnopq";
  11421. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  11422. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  11423. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  11424. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  11425. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  11426. byte hash[WC_SHA3_512_DIGEST_SIZE];
  11427. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  11428. /* Init stack variables. */
  11429. XMEMSET(hash, 0, sizeof(hash));
  11430. printf(testingFmt, "wc_Sha3_512_Final()");
  11431. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  11432. if (ret != 0) {
  11433. return ret;
  11434. }
  11435. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11436. if (ret == 0) {
  11437. ret = wc_Sha3_512_Final(&sha3, hash);
  11438. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  11439. ret = WOLFSSL_FATAL_ERROR;
  11440. }
  11441. }
  11442. /* Test bad args. */
  11443. if (ret == 0) {
  11444. ret = wc_Sha3_512_Final(NULL, hash);
  11445. if (ret == 0) {
  11446. ret = wc_Sha3_384_Final(&sha3, NULL);
  11447. }
  11448. if (ret == BAD_FUNC_ARG) {
  11449. ret = 0;
  11450. } else if (ret == 0) {
  11451. ret = WOLFSSL_FATAL_ERROR;
  11452. }
  11453. }
  11454. wc_Sha3_512_Free(&sha3);
  11455. printf(resultFmt, ret == 0 ? passed : failed);
  11456. if (ret == 0) {
  11457. printf(testingFmt, "wc_Sha3_512_GetHash()");
  11458. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  11459. if (ret != 0) {
  11460. return ret;
  11461. }
  11462. /* Init stack variables. */
  11463. XMEMSET(hash, 0, sizeof(hash));
  11464. XMEMSET(hashRet, 0, sizeof(hashRet));
  11465. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11466. if (ret == 0) {
  11467. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  11468. }
  11469. if (ret == 0) {
  11470. ret = wc_Sha3_512_Final(&sha3, hash);
  11471. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  11472. ret = WOLFSSL_FATAL_ERROR;
  11473. }
  11474. }
  11475. if (ret == 0) {
  11476. /* Test bad args. */
  11477. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  11478. if (ret == BAD_FUNC_ARG) {
  11479. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  11480. }
  11481. if (ret == BAD_FUNC_ARG) {
  11482. ret = 0;
  11483. } else if (ret == 0) {
  11484. ret = WOLFSSL_FATAL_ERROR;
  11485. }
  11486. }
  11487. printf(resultFmt, ret == 0 ? passed : failed);
  11488. }
  11489. wc_Sha3_512_Free(&sha3);
  11490. #endif
  11491. return ret;
  11492. } /* END test_wc_Sha3_512_Final */
  11493. /*
  11494. * Testing wc_Sha3_224_Copy()
  11495. */
  11496. static int test_wc_Sha3_224_Copy(void)
  11497. {
  11498. int ret = 0;
  11499. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11500. wc_Sha3 sha3, sha3Cpy;
  11501. const char* msg = TEST_STRING;
  11502. word32 msglen = (word32)TEST_STRING_SZ;
  11503. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11504. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  11505. XMEMSET(hash, 0, sizeof(hash));
  11506. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11507. printf(testingFmt, "wc_Sha3_224_Copy()");
  11508. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  11509. if (ret != 0) {
  11510. return ret;
  11511. }
  11512. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, devId);
  11513. if (ret != 0) {
  11514. wc_Sha3_224_Free(&sha3);
  11515. return ret;
  11516. }
  11517. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  11518. if (ret == 0) {
  11519. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  11520. if (ret == 0) {
  11521. ret = wc_Sha3_224_Final(&sha3, hash);
  11522. if (ret == 0) {
  11523. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  11524. }
  11525. }
  11526. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11527. ret = WOLFSSL_FATAL_ERROR;
  11528. }
  11529. }
  11530. /* Test bad args. */
  11531. if (ret == 0) {
  11532. ret = wc_Sha3_224_Copy(NULL, &sha3);
  11533. if (ret == BAD_FUNC_ARG) {
  11534. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  11535. }
  11536. if (ret == BAD_FUNC_ARG) {
  11537. ret = 0;
  11538. } else if (ret == 0) {
  11539. ret = WOLFSSL_FATAL_ERROR;
  11540. }
  11541. }
  11542. printf(resultFmt, ret == 0 ? passed : failed);
  11543. #endif
  11544. return ret;
  11545. } /* END test_wc_Sha3_224_Copy */
  11546. /*
  11547. * Testing wc_Sha3_256_Copy()
  11548. */
  11549. static int test_wc_Sha3_256_Copy(void)
  11550. {
  11551. int ret = 0;
  11552. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  11553. wc_Sha3 sha3, sha3Cpy;
  11554. const char* msg = TEST_STRING;
  11555. word32 msglen = (word32)TEST_STRING_SZ;
  11556. byte hash[WC_SHA3_256_DIGEST_SIZE];
  11557. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  11558. XMEMSET(hash, 0, sizeof(hash));
  11559. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11560. printf(testingFmt, "wc_Sha3_256_Copy()");
  11561. ret = wc_InitSha3_256(&sha3, HEAP_HINT, devId);
  11562. if (ret != 0) {
  11563. return ret;
  11564. }
  11565. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, devId);
  11566. if (ret != 0) {
  11567. wc_Sha3_256_Free(&sha3);
  11568. return ret;
  11569. }
  11570. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  11571. if (ret == 0) {
  11572. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  11573. if (ret == 0) {
  11574. ret = wc_Sha3_256_Final(&sha3, hash);
  11575. if (ret == 0) {
  11576. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  11577. }
  11578. }
  11579. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11580. ret = WOLFSSL_FATAL_ERROR;
  11581. }
  11582. }
  11583. /* Test bad args. */
  11584. if (ret == 0) {
  11585. ret = wc_Sha3_256_Copy(NULL, &sha3);
  11586. if (ret == BAD_FUNC_ARG) {
  11587. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  11588. }
  11589. if (ret == BAD_FUNC_ARG) {
  11590. ret = 0;
  11591. } else if (ret == 0) {
  11592. ret = WOLFSSL_FATAL_ERROR;
  11593. }
  11594. }
  11595. printf(resultFmt, ret == 0 ? passed : failed);
  11596. #endif
  11597. return ret;
  11598. } /* END test_wc_Sha3_256_Copy */
  11599. /*
  11600. * Testing wc_Sha3_384_Copy()
  11601. */
  11602. static int test_wc_Sha3_384_Copy(void)
  11603. {
  11604. int ret = 0;
  11605. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  11606. wc_Sha3 sha3, sha3Cpy;
  11607. const char* msg = TEST_STRING;
  11608. word32 msglen = (word32)TEST_STRING_SZ;
  11609. byte hash[WC_SHA3_384_DIGEST_SIZE];
  11610. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  11611. XMEMSET(hash, 0, sizeof(hash));
  11612. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11613. printf(testingFmt, "wc_Sha3_384_Copy()");
  11614. ret = wc_InitSha3_384(&sha3, HEAP_HINT, devId);
  11615. if (ret != 0) {
  11616. return ret;
  11617. }
  11618. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, devId);
  11619. if (ret != 0) {
  11620. wc_Sha3_384_Free(&sha3);
  11621. return ret;
  11622. }
  11623. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  11624. if (ret == 0) {
  11625. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  11626. if (ret == 0) {
  11627. ret = wc_Sha3_384_Final(&sha3, hash);
  11628. if (ret == 0) {
  11629. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  11630. }
  11631. }
  11632. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11633. ret = WOLFSSL_FATAL_ERROR;
  11634. }
  11635. }
  11636. /* Test bad args. */
  11637. if (ret == 0) {
  11638. ret = wc_Sha3_384_Copy(NULL, &sha3);
  11639. if (ret == BAD_FUNC_ARG) {
  11640. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  11641. }
  11642. if (ret == BAD_FUNC_ARG) {
  11643. ret = 0;
  11644. } else if (ret == 0) {
  11645. ret = WOLFSSL_FATAL_ERROR;
  11646. }
  11647. }
  11648. printf(resultFmt, ret == 0 ? passed : failed);
  11649. #endif
  11650. return ret;
  11651. } /* END test_wc_Sha3_384_Copy */
  11652. /*
  11653. * Testing wc_Sha3_512_Copy()
  11654. */
  11655. static int test_wc_Sha3_512_Copy(void)
  11656. {
  11657. int ret = 0;
  11658. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  11659. wc_Sha3 sha3, sha3Cpy;
  11660. const char* msg = TEST_STRING;
  11661. word32 msglen = (word32)TEST_STRING_SZ;
  11662. byte hash[WC_SHA3_512_DIGEST_SIZE];
  11663. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  11664. XMEMSET(hash, 0, sizeof(hash));
  11665. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11666. printf(testingFmt, "wc_Sha3_512_Copy()");
  11667. ret = wc_InitSha3_512(&sha3, HEAP_HINT, devId);
  11668. if (ret != 0) {
  11669. return ret;
  11670. }
  11671. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, devId);
  11672. if (ret != 0) {
  11673. wc_Sha3_512_Free(&sha3);
  11674. return ret;
  11675. }
  11676. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  11677. if (ret == 0) {
  11678. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  11679. if (ret == 0) {
  11680. ret = wc_Sha3_512_Final(&sha3, hash);
  11681. if (ret == 0) {
  11682. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  11683. }
  11684. }
  11685. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11686. ret = WOLFSSL_FATAL_ERROR;
  11687. }
  11688. }
  11689. /* Test bad args. */
  11690. if (ret == 0) {
  11691. ret = wc_Sha3_512_Copy(NULL, &sha3);
  11692. if (ret == BAD_FUNC_ARG) {
  11693. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  11694. }
  11695. if (ret == BAD_FUNC_ARG) {
  11696. ret = 0;
  11697. } else if (ret == 0) {
  11698. ret = WOLFSSL_FATAL_ERROR;
  11699. }
  11700. }
  11701. printf(resultFmt, ret == 0 ? passed : failed);
  11702. #endif
  11703. return ret;
  11704. } /* END test_wc_Sha3_512_Copy */
  11705. /*
  11706. * Unit test function for wc_Sha3_GetFlags()
  11707. */
  11708. static int test_wc_Sha3_GetFlags(void)
  11709. {
  11710. int ret = 0;
  11711. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  11712. wc_Sha3 sha3;
  11713. word32 flags = 0;
  11714. printf(testingFmt, "wc_Sha3_GetFlags()");
  11715. /* Initialize */
  11716. ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
  11717. if (ret != 0) {
  11718. return ret;
  11719. }
  11720. if (ret == 0) {
  11721. ret = wc_Sha3_GetFlags(&sha3, &flags);
  11722. }
  11723. if (ret == 0) {
  11724. if (flags & WC_HASH_FLAG_ISCOPY) {
  11725. ret = 0;
  11726. }
  11727. }
  11728. wc_Sha3_224_Free(&sha3);
  11729. printf(resultFmt, ret == 0 ? passed : failed);
  11730. #endif
  11731. return ret;
  11732. } /* END test_wc_Sha3_GetFlags */
  11733. static int test_wc_InitShake256(void)
  11734. {
  11735. int ret = 0;
  11736. #ifdef WOLFSSL_SHAKE256
  11737. wc_Shake shake;
  11738. printf(testingFmt, "wc_InitShake256()");
  11739. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11740. /* Test bad args. */
  11741. if (ret == 0) {
  11742. ret = wc_InitShake256(NULL, HEAP_HINT, devId);
  11743. if (ret == BAD_FUNC_ARG) {
  11744. ret = 0;
  11745. } else if (ret == 0) {
  11746. ret = WOLFSSL_FATAL_ERROR;
  11747. }
  11748. }
  11749. wc_Shake256_Free(&shake);
  11750. printf(resultFmt, ret == 0 ? passed : failed);
  11751. #endif
  11752. return ret;
  11753. } /* END test_wc_InitSha3 */
  11754. static int testing_wc_Shake256_Update(void)
  11755. {
  11756. int ret = 0;
  11757. #ifdef WOLFSSL_SHAKE256
  11758. wc_Shake shake;
  11759. byte msg[] = "Everybody's working for the weekend.";
  11760. byte msg2[] = "Everybody gets Friday off.";
  11761. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  11762. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  11763. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  11764. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11765. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11766. word32 msglen = sizeof(msg) - 1;
  11767. word32 msg2len = sizeof(msg2);
  11768. word32 msgCmplen = sizeof(msgCmp);
  11769. printf(testingFmt, "wc_Shake256_Update()");
  11770. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11771. if (ret != 0) {
  11772. return ret;
  11773. }
  11774. ret = wc_Shake256_Update(&shake, msg, msglen);
  11775. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  11776. ret = WOLFSSL_FATAL_ERROR;
  11777. }
  11778. if (ret == 0) {
  11779. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  11780. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  11781. ret = WOLFSSL_FATAL_ERROR;
  11782. }
  11783. }
  11784. /* Pass bad args. */
  11785. if (ret == 0) {
  11786. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  11787. if (ret == BAD_FUNC_ARG) {
  11788. ret = wc_Shake256_Update(&shake, NULL, 5);
  11789. }
  11790. if (ret == BAD_FUNC_ARG) {
  11791. wc_Shake256_Free(&shake);
  11792. if (wc_InitShake256(&shake, HEAP_HINT, devId)) {
  11793. return ret;
  11794. }
  11795. ret = wc_Shake256_Update(&shake, NULL, 0);
  11796. if (ret == 0) {
  11797. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  11798. }
  11799. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  11800. ret = WOLFSSL_FATAL_ERROR;
  11801. }
  11802. }
  11803. }
  11804. wc_Shake256_Free(&shake);
  11805. printf(resultFmt, ret == 0 ? passed : failed);
  11806. #endif /* WOLFSSL_SHAKE256 */
  11807. return ret;
  11808. }
  11809. static int test_wc_Shake256_Final(void)
  11810. {
  11811. int ret = 0;
  11812. #ifdef WOLFSSL_SHAKE256
  11813. wc_Shake shake;
  11814. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11815. "nopnopq";
  11816. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  11817. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  11818. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  11819. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  11820. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  11821. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  11822. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  11823. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  11824. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  11825. byte hash[114];
  11826. /* Init stack variables. */
  11827. XMEMSET(hash, 0, sizeof(hash));
  11828. printf(testingFmt, "wc_Shake256_Final()");
  11829. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11830. if (ret != 0) {
  11831. return ret;
  11832. }
  11833. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  11834. if (ret == 0) {
  11835. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  11836. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  11837. ret = WOLFSSL_FATAL_ERROR;
  11838. }
  11839. }
  11840. /* Test bad args. */
  11841. if (ret == 0) {
  11842. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  11843. if (ret == 0) {
  11844. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  11845. }
  11846. if (ret == BAD_FUNC_ARG) {
  11847. ret = 0;
  11848. } else if (ret == 0) {
  11849. ret = WOLFSSL_FATAL_ERROR;
  11850. }
  11851. }
  11852. wc_Shake256_Free(&shake);
  11853. printf(resultFmt, ret == 0 ? passed : failed);
  11854. #endif
  11855. return ret;
  11856. }
  11857. /*
  11858. * Testing wc_Shake256_Copy()
  11859. */
  11860. static int test_wc_Shake256_Copy(void)
  11861. {
  11862. int ret = 0;
  11863. #ifdef WOLFSSL_SHAKE256
  11864. wc_Shake shake, shakeCpy;
  11865. const char* msg = TEST_STRING;
  11866. word32 msglen = (word32)TEST_STRING_SZ;
  11867. byte hash[144];
  11868. byte hashCpy[144];
  11869. word32 hashLen = sizeof(hash);
  11870. word32 hashLenCpy = sizeof(hashCpy);
  11871. XMEMSET(hash, 0, sizeof(hash));
  11872. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11873. printf(testingFmt, "wc_Shake256_Copy()");
  11874. ret = wc_InitShake256(&shake, HEAP_HINT, devId);
  11875. if (ret != 0) {
  11876. return ret;
  11877. }
  11878. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, devId);
  11879. if (ret != 0) {
  11880. wc_Shake256_Free(&shake);
  11881. return ret;
  11882. }
  11883. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  11884. if (ret == 0) {
  11885. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  11886. if (ret == 0) {
  11887. ret = wc_Shake256_Final(&shake, hash, hashLen);
  11888. if (ret == 0) {
  11889. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  11890. }
  11891. }
  11892. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11893. ret = WOLFSSL_FATAL_ERROR;
  11894. }
  11895. }
  11896. /* Test bad args. */
  11897. if (ret == 0) {
  11898. ret = wc_Shake256_Copy(NULL, &shake);
  11899. if (ret == BAD_FUNC_ARG) {
  11900. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  11901. }
  11902. if (ret == BAD_FUNC_ARG) {
  11903. ret = 0;
  11904. } else if (ret == 0) {
  11905. ret = WOLFSSL_FATAL_ERROR;
  11906. }
  11907. }
  11908. wc_Shake256_Free(&shake);
  11909. printf(resultFmt, ret == 0 ? passed : failed);
  11910. #endif
  11911. return ret;
  11912. } /* END test_wc_Shake256_Copy */
  11913. /*
  11914. * Unit test function for wc_Shake256Hash()
  11915. */
  11916. static int test_wc_Shake256Hash(void)
  11917. {
  11918. int ret = 0;
  11919. #ifdef WOLFSSL_SHAKE256
  11920. const byte data[] = { /* Hello World */
  11921. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  11922. 0x72,0x6c,0x64
  11923. };
  11924. word32 len = sizeof(data);
  11925. byte hash[144];
  11926. word32 hashLen = sizeof(hash);
  11927. printf(testingFmt, "wc_Shake256Hash()");
  11928. ret = wc_Shake256Hash(data, len, hash, hashLen);
  11929. printf(resultFmt, ret == 0 ? passed : failed);
  11930. #endif
  11931. return ret;
  11932. } /* END test_wc_Shake256Hash */
  11933. /*
  11934. * Test function for wc_HmacSetKey
  11935. */
  11936. static int test_wc_Md5HmacSetKey(void)
  11937. {
  11938. int flag = 0;
  11939. #if !defined(NO_HMAC) && !defined(NO_MD5)
  11940. Hmac hmac;
  11941. int ret, times, itr;
  11942. const char* keys[]=
  11943. {
  11944. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  11945. #ifndef HAVE_FIPS
  11946. "Jefe", /* smaller than minimum FIPS key size */
  11947. #endif
  11948. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  11949. };
  11950. times = sizeof(keys) / sizeof(char*);
  11951. flag = 0;
  11952. printf(testingFmt, "wc_HmacSetKey() with MD5");
  11953. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  11954. if (ret != 0)
  11955. return ret;
  11956. for (itr = 0; itr < times; itr++) {
  11957. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  11958. (word32)XSTRLEN(keys[itr]));
  11959. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  11960. wc_HmacFree(&hmac);
  11961. if (ret == BAD_FUNC_ARG)
  11962. return 0;
  11963. else {
  11964. return WOLFSSL_FATAL_ERROR;
  11965. }
  11966. #else
  11967. if (ret != 0) {
  11968. flag = ret;
  11969. }
  11970. #endif
  11971. }
  11972. /* Bad args. */
  11973. if (!flag) {
  11974. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  11975. (word32)XSTRLEN(keys[0]));
  11976. if (ret != BAD_FUNC_ARG) {
  11977. flag = WOLFSSL_FATAL_ERROR;
  11978. }
  11979. }
  11980. if (!flag) {
  11981. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  11982. if (ret != BAD_FUNC_ARG) {
  11983. flag = WOLFSSL_FATAL_ERROR;
  11984. }
  11985. }
  11986. if (!flag) {
  11987. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  11988. (word32)XSTRLEN(keys[0]));
  11989. if (ret != BAD_FUNC_ARG) {
  11990. flag = WOLFSSL_FATAL_ERROR;
  11991. }
  11992. }
  11993. if (!flag) {
  11994. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  11995. #ifdef HAVE_FIPS
  11996. if (ret != HMAC_MIN_KEYLEN_E) {
  11997. flag = WOLFSSL_FATAL_ERROR;
  11998. }
  11999. #else
  12000. if (ret != 0) {
  12001. flag = WOLFSSL_FATAL_ERROR;
  12002. }
  12003. #endif
  12004. }
  12005. wc_HmacFree(&hmac);
  12006. printf(resultFmt, flag == 0 ? passed : failed);
  12007. #endif
  12008. return flag;
  12009. } /* END test_wc_Md5HmacSetKey */
  12010. /*
  12011. * testing wc_HmacSetKey() on wc_Sha hash.
  12012. */
  12013. static int test_wc_ShaHmacSetKey(void)
  12014. {
  12015. int flag = 0;
  12016. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12017. Hmac hmac;
  12018. int ret, times, itr;
  12019. const char* keys[]=
  12020. {
  12021. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12022. "\x0b\x0b\x0b",
  12023. #ifndef HAVE_FIPS
  12024. "Jefe", /* smaller than minimum FIPS key size */
  12025. #endif
  12026. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12027. "\xAA\xAA\xAA"
  12028. };
  12029. times = sizeof(keys) / sizeof(char*);
  12030. flag = 0;
  12031. printf(testingFmt, "wc_HmacSetKey() with SHA");
  12032. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12033. if (ret != 0)
  12034. return ret;
  12035. for (itr = 0; itr < times; itr++) {
  12036. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12037. (word32)XSTRLEN(keys[itr]));
  12038. if (ret != 0) {
  12039. flag = ret;
  12040. }
  12041. }
  12042. /* Bad args. */
  12043. if (!flag) {
  12044. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  12045. (word32)XSTRLEN(keys[0]));
  12046. if (ret != BAD_FUNC_ARG) {
  12047. flag = WOLFSSL_FATAL_ERROR;
  12048. }
  12049. }
  12050. if (!flag) {
  12051. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  12052. if (ret != BAD_FUNC_ARG) {
  12053. flag = WOLFSSL_FATAL_ERROR;
  12054. }
  12055. }
  12056. if (!flag) {
  12057. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12058. (word32)XSTRLEN(keys[0]));
  12059. if (ret != BAD_FUNC_ARG) {
  12060. flag = WOLFSSL_FATAL_ERROR;
  12061. }
  12062. }
  12063. if (!flag) {
  12064. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  12065. #ifdef HAVE_FIPS
  12066. if (ret != HMAC_MIN_KEYLEN_E) {
  12067. flag = WOLFSSL_FATAL_ERROR;
  12068. }
  12069. #else
  12070. if (ret != 0) {
  12071. flag = WOLFSSL_FATAL_ERROR;
  12072. }
  12073. #endif
  12074. }
  12075. wc_HmacFree(&hmac);
  12076. printf(resultFmt, flag == 0 ? passed : failed);
  12077. #endif
  12078. return flag;
  12079. } /* END test_wc_ShaHmacSetKey() */
  12080. /*
  12081. * testing wc_HmacSetKey() on Sha224 hash.
  12082. */
  12083. static int test_wc_Sha224HmacSetKey(void)
  12084. {
  12085. int flag = 0;
  12086. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12087. Hmac hmac;
  12088. int ret, times, itr;
  12089. const char* keys[]=
  12090. {
  12091. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12092. "\x0b\x0b\x0b",
  12093. #ifndef HAVE_FIPS
  12094. "Jefe", /* smaller than minimum FIPS key size */
  12095. #endif
  12096. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12097. "\xAA\xAA\xAA"
  12098. };
  12099. times = sizeof(keys) / sizeof(char*);
  12100. flag = 0;
  12101. printf(testingFmt, "wc_HmacSetKey() with SHA 224");
  12102. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12103. if (ret != 0)
  12104. return ret;
  12105. for (itr = 0; itr < times; itr++) {
  12106. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  12107. (word32)XSTRLEN(keys[itr]));
  12108. if (ret != 0) {
  12109. flag = ret;
  12110. }
  12111. }
  12112. /* Bad args. */
  12113. if (!flag) {
  12114. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  12115. (word32)XSTRLEN(keys[0]));
  12116. if (ret != BAD_FUNC_ARG) {
  12117. flag = WOLFSSL_FATAL_ERROR;
  12118. }
  12119. }
  12120. if (!flag) {
  12121. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  12122. if (ret != BAD_FUNC_ARG) {
  12123. flag = WOLFSSL_FATAL_ERROR;
  12124. }
  12125. }
  12126. if (!flag) {
  12127. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12128. (word32)XSTRLEN(keys[0]));
  12129. if (ret != BAD_FUNC_ARG) {
  12130. flag = WOLFSSL_FATAL_ERROR;
  12131. }
  12132. }
  12133. if (!flag) {
  12134. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  12135. #ifdef HAVE_FIPS
  12136. if (ret != HMAC_MIN_KEYLEN_E) {
  12137. flag = WOLFSSL_FATAL_ERROR;
  12138. }
  12139. #else
  12140. if (ret != 0) {
  12141. flag = WOLFSSL_FATAL_ERROR;
  12142. }
  12143. #endif
  12144. }
  12145. wc_HmacFree(&hmac);
  12146. printf(resultFmt, flag == 0 ? passed : failed);
  12147. #endif
  12148. return flag;
  12149. } /* END test_wc_Sha224HmacSetKey() */
  12150. /*
  12151. * testing wc_HmacSetKey() on Sha256 hash
  12152. */
  12153. static int test_wc_Sha256HmacSetKey(void)
  12154. {
  12155. int flag = 0;
  12156. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12157. Hmac hmac;
  12158. int ret, times, itr;
  12159. const char* keys[]=
  12160. {
  12161. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12162. "\x0b\x0b\x0b",
  12163. #ifndef HAVE_FIPS
  12164. "Jefe", /* smaller than minimum FIPS key size */
  12165. #endif
  12166. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12167. "\xAA\xAA\xAA"
  12168. };
  12169. times = sizeof(keys) / sizeof(char*);
  12170. flag = 0;
  12171. printf(testingFmt, "wc_HmacSetKey() with SHA256");
  12172. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12173. if (ret != 0)
  12174. return ret;
  12175. for (itr = 0; itr < times; itr++) {
  12176. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  12177. (word32)XSTRLEN(keys[itr]));
  12178. if (ret != 0) {
  12179. flag = ret;
  12180. }
  12181. }
  12182. /* Bad args. */
  12183. if (!flag) {
  12184. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  12185. (word32)XSTRLEN(keys[0]));
  12186. if (ret != BAD_FUNC_ARG) {
  12187. flag = WOLFSSL_FATAL_ERROR;
  12188. }
  12189. }
  12190. if (!flag) {
  12191. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  12192. if (ret != BAD_FUNC_ARG) {
  12193. flag = WOLFSSL_FATAL_ERROR;
  12194. }
  12195. }
  12196. if (!flag) {
  12197. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12198. (word32)XSTRLEN(keys[0]));
  12199. if (ret != BAD_FUNC_ARG) {
  12200. flag = WOLFSSL_FATAL_ERROR;
  12201. }
  12202. }
  12203. if (!flag) {
  12204. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  12205. #ifdef HAVE_FIPS
  12206. if (ret != HMAC_MIN_KEYLEN_E) {
  12207. flag = WOLFSSL_FATAL_ERROR;
  12208. }
  12209. #else
  12210. if (ret != 0) {
  12211. flag = WOLFSSL_FATAL_ERROR;
  12212. }
  12213. #endif
  12214. }
  12215. wc_HmacFree(&hmac);
  12216. printf(resultFmt, flag == 0 ? passed : failed);
  12217. #endif
  12218. return flag;
  12219. } /* END test_wc_Sha256HmacSetKey() */
  12220. /*
  12221. * testing wc_HmacSetKey on Sha384 hash.
  12222. */
  12223. static int test_wc_Sha384HmacSetKey(void)
  12224. {
  12225. int flag = 0;
  12226. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12227. Hmac hmac;
  12228. int ret, times, itr;
  12229. const char* keys[]=
  12230. {
  12231. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12232. "\x0b\x0b\x0b",
  12233. #ifndef HAVE_FIPS
  12234. "Jefe", /* smaller than minimum FIPS key size */
  12235. #endif
  12236. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12237. "\xAA\xAA\xAA"
  12238. };
  12239. times = sizeof(keys) / sizeof(char*);
  12240. flag = 0;
  12241. printf(testingFmt, "wc_HmacSetKey() with SHA384");
  12242. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12243. if (ret != 0)
  12244. return ret;
  12245. for (itr = 0; itr < times; itr++) {
  12246. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  12247. (word32)XSTRLEN(keys[itr]));
  12248. if (ret != 0) {
  12249. flag = ret;
  12250. }
  12251. }
  12252. /* Bad args. */
  12253. if (!flag) {
  12254. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  12255. (word32)XSTRLEN(keys[0]));
  12256. if (ret != BAD_FUNC_ARG) {
  12257. flag = WOLFSSL_FATAL_ERROR;
  12258. }
  12259. }
  12260. if (!flag) {
  12261. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  12262. if (ret != BAD_FUNC_ARG) {
  12263. flag = WOLFSSL_FATAL_ERROR;
  12264. }
  12265. }
  12266. if (!flag) {
  12267. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12268. (word32)XSTRLEN(keys[0]));
  12269. if (ret != BAD_FUNC_ARG) {
  12270. flag = WOLFSSL_FATAL_ERROR;
  12271. }
  12272. }
  12273. if (!flag) {
  12274. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  12275. #ifdef HAVE_FIPS
  12276. if (ret != HMAC_MIN_KEYLEN_E) {
  12277. flag = WOLFSSL_FATAL_ERROR;
  12278. }
  12279. #else
  12280. if (ret != 0) {
  12281. flag = WOLFSSL_FATAL_ERROR;
  12282. }
  12283. #endif
  12284. }
  12285. wc_HmacFree(&hmac);
  12286. printf(resultFmt, flag == 0 ? passed : failed);
  12287. #endif
  12288. return flag;
  12289. } /* END test_wc_Sha384HmacSetKey() */
  12290. /*
  12291. * testing wc_HmacUpdate on wc_Md5 hash.
  12292. */
  12293. static int test_wc_Md5HmacUpdate(void)
  12294. {
  12295. int flag = 0;
  12296. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  12297. Hmac hmac;
  12298. testVector a, b;
  12299. int ret;
  12300. #ifdef HAVE_FIPS
  12301. const char* keys =
  12302. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12303. #else
  12304. const char* keys = "Jefe";
  12305. #endif
  12306. a.input = "what do ya want for nothing?";
  12307. a.inLen = XSTRLEN(a.input);
  12308. b.input = "Hi There";
  12309. b.inLen = XSTRLEN(b.input);
  12310. flag = 0;
  12311. printf(testingFmt, "wc_HmacUpdate() with MD5");
  12312. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12313. if (ret != 0)
  12314. return ret;
  12315. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  12316. if (ret != 0) {
  12317. flag = ret;
  12318. }
  12319. if (!flag) {
  12320. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12321. if (ret != 0) {
  12322. flag = ret;
  12323. }
  12324. }
  12325. /* Update Hmac. */
  12326. if (!flag) {
  12327. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12328. if (ret != 0) {
  12329. flag = ret;
  12330. }
  12331. }
  12332. /* Test bad args. */
  12333. if (!flag) {
  12334. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12335. if (ret != BAD_FUNC_ARG) {
  12336. flag = WOLFSSL_FATAL_ERROR;
  12337. }
  12338. }
  12339. if (!flag) {
  12340. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12341. if (ret != BAD_FUNC_ARG) {
  12342. flag = WOLFSSL_FATAL_ERROR;
  12343. }
  12344. }
  12345. if (!flag) {
  12346. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12347. if (ret != 0) {
  12348. flag = ret;
  12349. }
  12350. }
  12351. wc_HmacFree(&hmac);
  12352. printf(resultFmt, flag == 0 ? passed : failed);
  12353. #endif
  12354. return flag;
  12355. } /* END test_wc_Md5HmacUpdate */
  12356. /*
  12357. * testing wc_HmacUpdate on SHA hash.
  12358. */
  12359. static int test_wc_ShaHmacUpdate(void)
  12360. {
  12361. int flag = 0;
  12362. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12363. Hmac hmac;
  12364. testVector a, b;
  12365. int ret;
  12366. #ifdef HAVE_FIPS
  12367. const char* keys =
  12368. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12369. #else
  12370. const char* keys = "Jefe";
  12371. #endif
  12372. a.input = "what do ya want for nothing?";
  12373. a.inLen = XSTRLEN(a.input);
  12374. b.input = "Hi There";
  12375. b.inLen = XSTRLEN(b.input);
  12376. flag = 0;
  12377. printf(testingFmt, "wc_HmacUpdate() with SHA");
  12378. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12379. if (ret != 0)
  12380. return ret;
  12381. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  12382. if (ret != 0) {
  12383. flag = ret;
  12384. }
  12385. if (!flag) {
  12386. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12387. if (ret != 0) {
  12388. flag = ret;
  12389. }
  12390. }
  12391. /* Update Hmac. */
  12392. if (!flag) {
  12393. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12394. if (ret != 0) {
  12395. flag = ret;
  12396. }
  12397. }
  12398. /* Test bad args. */
  12399. if (!flag) {
  12400. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12401. if (ret != BAD_FUNC_ARG) {
  12402. flag = WOLFSSL_FATAL_ERROR;
  12403. }
  12404. }
  12405. if (!flag) {
  12406. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12407. if (ret != BAD_FUNC_ARG) {
  12408. flag = WOLFSSL_FATAL_ERROR;
  12409. }
  12410. }
  12411. if (!flag) {
  12412. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12413. if (ret != 0) {
  12414. flag = ret;
  12415. }
  12416. }
  12417. wc_HmacFree(&hmac);
  12418. printf(resultFmt, flag == 0 ? passed : failed);
  12419. #endif
  12420. return flag;
  12421. } /* END test_wc_ShaHmacUpdate */
  12422. /*
  12423. * testing wc_HmacUpdate on SHA224 hash.
  12424. */
  12425. static int test_wc_Sha224HmacUpdate(void)
  12426. {
  12427. int flag = 0;
  12428. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12429. Hmac hmac;
  12430. testVector a, b;
  12431. int ret;
  12432. #ifdef HAVE_FIPS
  12433. const char* keys =
  12434. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12435. #else
  12436. const char* keys = "Jefe";
  12437. #endif
  12438. a.input = "what do ya want for nothing?";
  12439. a.inLen = XSTRLEN(a.input);
  12440. b.input = "Hi There";
  12441. b.inLen = XSTRLEN(b.input);
  12442. flag = 0;
  12443. printf(testingFmt, "wc_HmacUpdate() with SHA224");
  12444. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12445. if (ret != 0)
  12446. return ret;
  12447. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  12448. if (ret != 0) {
  12449. flag = ret;
  12450. }
  12451. if (!flag) {
  12452. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12453. if (ret != 0) {
  12454. flag = ret;
  12455. }
  12456. }
  12457. /* Update Hmac. */
  12458. if (!flag) {
  12459. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12460. if (ret != 0) {
  12461. flag = ret;
  12462. }
  12463. }
  12464. /* Test bad args. */
  12465. if (!flag) {
  12466. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12467. if (ret != BAD_FUNC_ARG) {
  12468. flag = WOLFSSL_FATAL_ERROR;
  12469. }
  12470. }
  12471. if (!flag) {
  12472. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12473. if (ret != BAD_FUNC_ARG) {
  12474. flag = WOLFSSL_FATAL_ERROR;
  12475. }
  12476. }
  12477. if (!flag) {
  12478. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12479. if (ret != 0) {
  12480. flag = ret;
  12481. }
  12482. }
  12483. wc_HmacFree(&hmac);
  12484. printf(resultFmt, flag == 0 ? passed : failed);
  12485. #endif
  12486. return flag;
  12487. } /* END test_wc_Sha224HmacUpdate */
  12488. /*
  12489. * testing wc_HmacUpdate on SHA256 hash.
  12490. */
  12491. static int test_wc_Sha256HmacUpdate(void)
  12492. {
  12493. int flag = 0;
  12494. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12495. Hmac hmac;
  12496. testVector a, b;
  12497. int ret;
  12498. #ifdef HAVE_FIPS
  12499. const char* keys =
  12500. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12501. #else
  12502. const char* keys = "Jefe";
  12503. #endif
  12504. a.input = "what do ya want for nothing?";
  12505. a.inLen = XSTRLEN(a.input);
  12506. b.input = "Hi There";
  12507. b.inLen = XSTRLEN(b.input);
  12508. flag = 0;
  12509. printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
  12510. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12511. if (ret != 0)
  12512. return ret;
  12513. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  12514. if (ret != 0) {
  12515. flag = ret;
  12516. }
  12517. if (!flag) {
  12518. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12519. if (ret != 0) {
  12520. flag = ret;
  12521. }
  12522. }
  12523. /* Update Hmac. */
  12524. if (!flag) {
  12525. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12526. if (ret != 0) {
  12527. flag = ret;
  12528. }
  12529. }
  12530. /* Test bad args. */
  12531. if (!flag) {
  12532. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12533. if (ret != BAD_FUNC_ARG) {
  12534. flag = WOLFSSL_FATAL_ERROR;
  12535. }
  12536. }
  12537. if (!flag) {
  12538. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12539. if (ret != BAD_FUNC_ARG) {
  12540. flag = WOLFSSL_FATAL_ERROR;
  12541. }
  12542. }
  12543. if (!flag) {
  12544. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12545. if (ret != 0) {
  12546. flag = ret;
  12547. }
  12548. }
  12549. wc_HmacFree(&hmac);
  12550. printf(resultFmt, flag == 0 ? passed : failed);
  12551. #endif
  12552. return flag;
  12553. } /* END test_wc_Sha256HmacUpdate */
  12554. /*
  12555. * testing wc_HmacUpdate on SHA384 hash.
  12556. */
  12557. static int test_wc_Sha384HmacUpdate(void)
  12558. {
  12559. int flag = 0;
  12560. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12561. Hmac hmac;
  12562. testVector a, b;
  12563. int ret;
  12564. #ifdef HAVE_FIPS
  12565. const char* keys =
  12566. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12567. #else
  12568. const char* keys = "Jefe";
  12569. #endif
  12570. a.input = "what do ya want for nothing?";
  12571. a.inLen = XSTRLEN(a.input);
  12572. b.input = "Hi There";
  12573. b.inLen = XSTRLEN(b.input);
  12574. flag = 0;
  12575. printf(testingFmt, "wc_HmacUpdate() with SHA384");
  12576. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12577. if (ret != 0)
  12578. return ret;
  12579. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  12580. if (ret != 0) {
  12581. flag = ret;
  12582. }
  12583. if (!flag) {
  12584. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12585. if (ret != 0) {
  12586. flag = ret;
  12587. }
  12588. }
  12589. /* Update Hmac. */
  12590. if (!flag) {
  12591. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12592. if (ret != 0) {
  12593. flag = ret;
  12594. }
  12595. }
  12596. /* Test bad args. */
  12597. if (!flag) {
  12598. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12599. if (ret != BAD_FUNC_ARG) {
  12600. flag = WOLFSSL_FATAL_ERROR;
  12601. }
  12602. }
  12603. if (!flag) {
  12604. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12605. if (ret != BAD_FUNC_ARG) {
  12606. flag = WOLFSSL_FATAL_ERROR;
  12607. }
  12608. }
  12609. if (!flag) {
  12610. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12611. if (ret != 0) {
  12612. flag = ret;
  12613. }
  12614. }
  12615. wc_HmacFree(&hmac);
  12616. printf(resultFmt, flag == 0 ? passed : failed);
  12617. #endif
  12618. return flag;
  12619. } /* END test_wc_Sha384HmacUpdate */
  12620. /*
  12621. * Testing wc_HmacFinal() with MD5
  12622. */
  12623. static int test_wc_Md5HmacFinal(void)
  12624. {
  12625. int flag = 0;
  12626. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  12627. Hmac hmac;
  12628. byte hash[WC_MD5_DIGEST_SIZE];
  12629. testVector a;
  12630. int ret;
  12631. const char* key;
  12632. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12633. a.input = "Hi There";
  12634. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  12635. "\x9d";
  12636. a.inLen = XSTRLEN(a.input);
  12637. a.outLen = XSTRLEN(a.output);
  12638. flag = 0;
  12639. printf(testingFmt, "wc_HmacFinal() with MD5");
  12640. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12641. if (ret != 0)
  12642. return ret;
  12643. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  12644. if (ret != 0) {
  12645. flag = ret;
  12646. }
  12647. if (!flag) {
  12648. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12649. if (ret != 0) {
  12650. flag = ret;
  12651. }
  12652. }
  12653. if (!flag) {
  12654. ret = wc_HmacFinal(&hmac, hash);
  12655. if (ret != 0) {
  12656. flag = ret;
  12657. }
  12658. }
  12659. if (!flag) {
  12660. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  12661. flag = WOLFSSL_FATAL_ERROR;
  12662. }
  12663. }
  12664. /* Try bad parameters. */
  12665. if (!flag) {
  12666. ret = wc_HmacFinal(NULL, hash);
  12667. if (ret != BAD_FUNC_ARG) {
  12668. flag = WOLFSSL_FATAL_ERROR;
  12669. }
  12670. }
  12671. #ifndef HAVE_FIPS
  12672. if (!flag) {
  12673. ret = wc_HmacFinal(&hmac, NULL);
  12674. if (ret != BAD_FUNC_ARG) {
  12675. flag = WOLFSSL_FATAL_ERROR;
  12676. }
  12677. }
  12678. #endif
  12679. wc_HmacFree(&hmac);
  12680. printf(resultFmt, flag == 0 ? passed : failed);
  12681. #endif
  12682. return flag;
  12683. } /* END test_wc_Md5HmacFinal */
  12684. /*
  12685. * Testing wc_HmacFinal() with SHA
  12686. */
  12687. static int test_wc_ShaHmacFinal(void)
  12688. {
  12689. int flag = 0;
  12690. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12691. Hmac hmac;
  12692. byte hash[WC_SHA_DIGEST_SIZE];
  12693. testVector a;
  12694. int ret;
  12695. const char* key;
  12696. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12697. "\x0b\x0b\x0b";
  12698. a.input = "Hi There";
  12699. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  12700. "\x8e\xf1\x46\xbe\x00";
  12701. a.inLen = XSTRLEN(a.input);
  12702. a.outLen = XSTRLEN(a.output);
  12703. flag = 0;
  12704. printf(testingFmt, "wc_HmacFinal() with SHA");
  12705. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12706. if (ret != 0)
  12707. return ret;
  12708. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  12709. if (ret != 0) {
  12710. flag = ret;
  12711. }
  12712. if (!flag) {
  12713. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12714. if (ret != 0) {
  12715. flag = ret;
  12716. }
  12717. }
  12718. if (!flag) {
  12719. ret = wc_HmacFinal(&hmac, hash);
  12720. if (ret != 0) {
  12721. flag = ret;
  12722. }
  12723. }
  12724. if (!flag) {
  12725. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  12726. flag = WOLFSSL_FATAL_ERROR;
  12727. }
  12728. }
  12729. /* Try bad parameters. */
  12730. if (!flag) {
  12731. ret = wc_HmacFinal(NULL, hash);
  12732. if (ret != BAD_FUNC_ARG) {
  12733. flag = WOLFSSL_FATAL_ERROR;
  12734. }
  12735. }
  12736. #ifndef HAVE_FIPS
  12737. if (!flag) {
  12738. ret = wc_HmacFinal(&hmac, NULL);
  12739. if (ret != BAD_FUNC_ARG) {
  12740. flag = WOLFSSL_FATAL_ERROR;
  12741. }
  12742. }
  12743. #endif
  12744. wc_HmacFree(&hmac);
  12745. printf(resultFmt, flag == 0 ? passed : failed);
  12746. #endif
  12747. return flag;
  12748. } /* END test_wc_ShaHmacFinal */
  12749. /*
  12750. * Testing wc_HmacFinal() with SHA224
  12751. */
  12752. static int test_wc_Sha224HmacFinal(void)
  12753. {
  12754. int flag = 0;
  12755. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12756. Hmac hmac;
  12757. byte hash[WC_SHA224_DIGEST_SIZE];
  12758. testVector a;
  12759. int ret;
  12760. const char* key;
  12761. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12762. "\x0b\x0b\x0b";
  12763. a.input = "Hi There";
  12764. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  12765. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  12766. a.inLen = XSTRLEN(a.input);
  12767. a.outLen = XSTRLEN(a.output);
  12768. flag = 0;
  12769. printf(testingFmt, "wc_HmacFinal() with SHA224");
  12770. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12771. if (ret != 0)
  12772. return ret;
  12773. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  12774. if (ret != 0) {
  12775. flag = ret;
  12776. }
  12777. if (!flag) {
  12778. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12779. if (ret != 0) {
  12780. flag = ret;
  12781. }
  12782. }
  12783. if (!flag) {
  12784. ret = wc_HmacFinal(&hmac, hash);
  12785. if (ret != 0) {
  12786. flag = ret;
  12787. }
  12788. }
  12789. if (!flag) {
  12790. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  12791. flag = WOLFSSL_FATAL_ERROR;
  12792. }
  12793. }
  12794. /* Try bad parameters. */
  12795. if (!flag) {
  12796. ret = wc_HmacFinal(NULL, hash);
  12797. if (ret != BAD_FUNC_ARG) {
  12798. flag = WOLFSSL_FATAL_ERROR;
  12799. }
  12800. }
  12801. #ifndef HAVE_FIPS
  12802. if (!flag) {
  12803. ret = wc_HmacFinal(&hmac, NULL);
  12804. if (ret != BAD_FUNC_ARG) {
  12805. flag = WOLFSSL_FATAL_ERROR;
  12806. }
  12807. }
  12808. #endif
  12809. wc_HmacFree(&hmac);
  12810. printf(resultFmt, flag == 0 ? passed : failed);
  12811. #endif
  12812. return flag;
  12813. } /* END test_wc_Sha224HmacFinal */
  12814. /*
  12815. * Testing wc_HmacFinal() with SHA256
  12816. */
  12817. static int test_wc_Sha256HmacFinal(void)
  12818. {
  12819. int flag = 0;
  12820. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12821. Hmac hmac;
  12822. byte hash[WC_SHA256_DIGEST_SIZE];
  12823. testVector a;
  12824. int ret;
  12825. const char* key;
  12826. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12827. "\x0b\x0b\x0b";
  12828. a.input = "Hi There";
  12829. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  12830. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  12831. "\xcf\xf7";
  12832. a.inLen = XSTRLEN(a.input);
  12833. a.outLen = XSTRLEN(a.output);
  12834. flag = 0;
  12835. printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
  12836. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12837. if (ret != 0)
  12838. return ret;
  12839. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  12840. if (ret != 0) {
  12841. flag = ret;
  12842. }
  12843. if (!flag) {
  12844. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12845. if (ret != 0) {
  12846. flag = ret;
  12847. }
  12848. }
  12849. if (!flag) {
  12850. ret = wc_HmacFinal(&hmac, hash);
  12851. if (ret != 0) {
  12852. flag = ret;
  12853. }
  12854. }
  12855. if (!flag) {
  12856. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  12857. flag = WOLFSSL_FATAL_ERROR;
  12858. }
  12859. }
  12860. /* Try bad parameters. */
  12861. if (!flag) {
  12862. ret = wc_HmacFinal(NULL, hash);
  12863. if (ret != BAD_FUNC_ARG) {
  12864. flag = WOLFSSL_FATAL_ERROR;
  12865. }
  12866. }
  12867. #ifndef HAVE_FIPS
  12868. if (!flag) {
  12869. ret = wc_HmacFinal(&hmac, NULL);
  12870. if (ret != BAD_FUNC_ARG) {
  12871. flag = WOLFSSL_FATAL_ERROR;
  12872. }
  12873. }
  12874. #endif
  12875. wc_HmacFree(&hmac);
  12876. printf(resultFmt, flag == 0 ? passed : failed);
  12877. #endif
  12878. return flag;
  12879. } /* END test_wc_Sha256HmacFinal */
  12880. /*
  12881. * Testing wc_HmacFinal() with SHA384
  12882. */
  12883. static int test_wc_Sha384HmacFinal(void)
  12884. {
  12885. int flag = 0;
  12886. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12887. Hmac hmac;
  12888. byte hash[WC_SHA384_DIGEST_SIZE];
  12889. testVector a;
  12890. int ret;
  12891. const char* key;
  12892. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12893. "\x0b\x0b\x0b";
  12894. a.input = "Hi There";
  12895. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  12896. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  12897. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  12898. "\xfa\x9c\xb6";
  12899. a.inLen = XSTRLEN(a.input);
  12900. a.outLen = XSTRLEN(a.output);
  12901. flag = 0;
  12902. printf(testingFmt, "wc_HmacFinal() with SHA384");
  12903. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12904. if (ret != 0)
  12905. return ret;
  12906. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  12907. if (ret != 0) {
  12908. flag = ret;
  12909. }
  12910. if (!flag) {
  12911. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12912. if (ret != 0) {
  12913. flag = ret;
  12914. }
  12915. }
  12916. if (!flag) {
  12917. ret = wc_HmacFinal(&hmac, hash);
  12918. if (ret != 0) {
  12919. flag = ret;
  12920. }
  12921. }
  12922. if (!flag) {
  12923. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  12924. flag = WOLFSSL_FATAL_ERROR;
  12925. }
  12926. }
  12927. /* Try bad parameters. */
  12928. if (!flag) {
  12929. ret = wc_HmacFinal(NULL, hash);
  12930. if (ret != BAD_FUNC_ARG) {
  12931. flag = WOLFSSL_FATAL_ERROR;
  12932. }
  12933. }
  12934. #ifndef HAVE_FIPS
  12935. if (!flag) {
  12936. ret = wc_HmacFinal(&hmac, NULL);
  12937. if (ret != BAD_FUNC_ARG) {
  12938. flag = WOLFSSL_FATAL_ERROR;
  12939. }
  12940. }
  12941. #endif
  12942. wc_HmacFree(&hmac);
  12943. printf(resultFmt, flag == 0 ? passed : failed);
  12944. #endif
  12945. return flag;
  12946. } /* END test_wc_Sha384HmacFinal */
  12947. /*
  12948. * Testing wc_InitCmac()
  12949. */
  12950. static int test_wc_InitCmac(void)
  12951. {
  12952. int ret = 0;
  12953. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  12954. Cmac cmac1, cmac2, cmac3;
  12955. /* AES 128 key. */
  12956. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  12957. "\x09\x10\x11\x12\x13\x14\x15\x16";
  12958. /* AES 192 key. */
  12959. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  12960. "\x09\x01\x11\x12\x13\x14\x15\x16"
  12961. "\x01\x02\x03\x04\x05\x06\x07\x08";
  12962. /* AES 256 key. */
  12963. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  12964. "\x09\x01\x11\x12\x13\x14\x15\x16"
  12965. "\x01\x02\x03\x04\x05\x06\x07\x08"
  12966. "\x09\x01\x11\x12\x13\x14\x15\x16";
  12967. word32 key1Sz = (word32)sizeof(key1) - 1;
  12968. word32 key2Sz = (word32)sizeof(key2) - 1;
  12969. word32 key3Sz = (word32)sizeof(key3) - 1;
  12970. int type = WC_CMAC_AES;
  12971. printf(testingFmt, "wc_InitCmac()");
  12972. #ifdef WOLFSSL_AES_128
  12973. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  12974. #endif
  12975. #ifdef WOLFSSL_AES_192
  12976. if (ret == 0) {
  12977. wc_AesFree(&cmac1.aes);
  12978. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  12979. }
  12980. #endif
  12981. #ifdef WOLFSSL_AES_256
  12982. if (ret == 0) {
  12983. wc_AesFree(&cmac2.aes);
  12984. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  12985. }
  12986. #endif
  12987. /* Test bad args. */
  12988. if (ret == 0) {
  12989. wc_AesFree(&cmac3.aes);
  12990. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  12991. if (ret == BAD_FUNC_ARG) {
  12992. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  12993. }
  12994. if (ret == BAD_FUNC_ARG) {
  12995. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  12996. }
  12997. if (ret == BAD_FUNC_ARG) {
  12998. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  12999. }
  13000. if (ret == BAD_FUNC_ARG) {
  13001. ret = 0;
  13002. } else {
  13003. ret = WOLFSSL_FATAL_ERROR;
  13004. }
  13005. }
  13006. (void)key1;
  13007. (void)key1Sz;
  13008. (void)key2;
  13009. (void)key2Sz;
  13010. (void)cmac1;
  13011. (void)cmac2;
  13012. printf(resultFmt, ret == 0 ? passed : failed);
  13013. #endif
  13014. return ret;
  13015. } /* END test_wc_InitCmac */
  13016. /*
  13017. * Testing wc_CmacUpdate()
  13018. */
  13019. static int test_wc_CmacUpdate(void)
  13020. {
  13021. int ret = 0;
  13022. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13023. Cmac cmac;
  13024. byte key[] =
  13025. {
  13026. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13027. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13028. };
  13029. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13030. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13031. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13032. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13033. "\xf4";
  13034. word32 inSz = (word32)sizeof(in) - 1;
  13035. word32 keySz = (word32)sizeof(key);
  13036. int type = WC_CMAC_AES;
  13037. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13038. if (ret != 0) {
  13039. return ret;
  13040. }
  13041. printf(testingFmt, "wc_CmacUpdate()");
  13042. ret = wc_CmacUpdate(&cmac, in, inSz);
  13043. /* Test bad args. */
  13044. if (ret == 0) {
  13045. ret = wc_CmacUpdate(NULL, in, inSz);
  13046. if (ret == BAD_FUNC_ARG) {
  13047. ret = wc_CmacUpdate(&cmac, NULL, 30);
  13048. }
  13049. if (ret == BAD_FUNC_ARG) {
  13050. ret = 0;
  13051. } else if (ret == 0) {
  13052. ret = WOLFSSL_FATAL_ERROR;
  13053. }
  13054. wc_AesFree(&cmac.aes);
  13055. }
  13056. printf(resultFmt, ret == 0 ? passed : failed);
  13057. #endif
  13058. return ret;
  13059. } /* END test_wc_CmacUpdate */
  13060. /*
  13061. * Testing wc_CmacFinal()
  13062. */
  13063. static int test_wc_CmacFinal(void)
  13064. {
  13065. int ret = 0;
  13066. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13067. Cmac cmac;
  13068. byte key[] =
  13069. {
  13070. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13071. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13072. };
  13073. byte msg[] =
  13074. {
  13075. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13076. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13077. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13078. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13079. 0xf4
  13080. };
  13081. /* Test vectors from CMACGenAES128.rsp from
  13082. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13083. * Per RFC4493 truncation of lsb is possible.
  13084. */
  13085. byte expMac[] =
  13086. {
  13087. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13088. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13089. };
  13090. byte mac[AES_BLOCK_SIZE];
  13091. word32 msgSz = (word32)sizeof(msg);
  13092. word32 keySz = (word32)sizeof(key);
  13093. word32 macSz = sizeof(mac);
  13094. word32 badMacSz = 17;
  13095. int expMacSz = sizeof(expMac);
  13096. int type = WC_CMAC_AES;
  13097. XMEMSET(mac, 0, macSz);
  13098. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13099. if (ret != 0) {
  13100. return ret;
  13101. }
  13102. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13103. printf(testingFmt, "wc_CmacFinal()");
  13104. if (ret == 0) {
  13105. ret = wc_CmacFinal(&cmac, mac, &macSz);
  13106. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13107. ret = WOLFSSL_FATAL_ERROR;
  13108. }
  13109. /* Pass in bad args. */
  13110. if (ret == 0) {
  13111. ret = wc_CmacFinal(NULL, mac, &macSz);
  13112. if (ret == BAD_FUNC_ARG) {
  13113. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  13114. }
  13115. if (ret == BAD_FUNC_ARG) {
  13116. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  13117. if (ret == BUFFER_E) {
  13118. ret = 0;
  13119. }
  13120. } else if (ret == 0) {
  13121. ret = WOLFSSL_FATAL_ERROR;
  13122. }
  13123. }
  13124. }
  13125. printf(resultFmt, ret == 0 ? passed : failed);
  13126. #endif
  13127. return ret;
  13128. } /* END test_wc_CmacFinal */
  13129. /*
  13130. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13131. */
  13132. static int test_wc_AesCmacGenerate(void)
  13133. {
  13134. int ret = 0;
  13135. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13136. Cmac cmac;
  13137. byte key[] =
  13138. {
  13139. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13140. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13141. };
  13142. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13143. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13144. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13145. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13146. byte mac[AES_BLOCK_SIZE];
  13147. word32 keySz = sizeof(key);
  13148. word32 macSz = sizeof(mac);
  13149. word32 msgSz = sizeof(msg) - 1;
  13150. word32 expMacSz = sizeof(expMac) - 1;
  13151. int type = WC_CMAC_AES;
  13152. XMEMSET(mac, 0, macSz);
  13153. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13154. if (ret != 0) {
  13155. return ret;
  13156. }
  13157. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13158. if (ret != 0) {
  13159. return ret;
  13160. } else {
  13161. wc_AesFree(&cmac.aes);
  13162. }
  13163. printf(testingFmt, "wc_AesCmacGenerate()");
  13164. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  13165. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13166. ret = WOLFSSL_FATAL_ERROR;
  13167. }
  13168. /* Pass in bad args. */
  13169. if (ret == 0) {
  13170. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  13171. if (ret == BAD_FUNC_ARG) {
  13172. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  13173. }
  13174. if (ret == BAD_FUNC_ARG) {
  13175. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  13176. }
  13177. if (ret == BAD_FUNC_ARG) {
  13178. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  13179. }
  13180. if (ret == BAD_FUNC_ARG) {
  13181. ret = 0;
  13182. } else if (ret == 0) {
  13183. ret = WOLFSSL_FATAL_ERROR;
  13184. }
  13185. }
  13186. printf(resultFmt, ret == 0 ? passed : failed);
  13187. if (ret == 0) {
  13188. printf(testingFmt, "wc_AesCmacVerify()");
  13189. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  13190. /* Test bad args. */
  13191. if (ret == 0) {
  13192. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  13193. if (ret == BAD_FUNC_ARG) {
  13194. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  13195. }
  13196. if (ret == BAD_FUNC_ARG) {
  13197. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  13198. }
  13199. if (ret == BAD_FUNC_ARG) {
  13200. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  13201. }
  13202. if (ret == BAD_FUNC_ARG) {
  13203. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  13204. }
  13205. if (ret == BAD_FUNC_ARG) {
  13206. ret = 0;
  13207. } else if (ret == 0) {
  13208. ret = WOLFSSL_FATAL_ERROR;
  13209. }
  13210. }
  13211. printf(resultFmt, ret == 0 ? passed : failed);
  13212. }
  13213. #endif
  13214. return ret;
  13215. } /* END test_wc_AesCmacGenerate */
  13216. /*
  13217. * Testing streaming AES-GCM API.
  13218. */
  13219. static int test_wc_AesGcmStream(void)
  13220. {
  13221. int ret = 0;
  13222. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13223. defined(WOLFSSL_AESGCM_STREAM)
  13224. int i;
  13225. WC_RNG rng[1];
  13226. Aes aesEnc[1];
  13227. Aes aesDec[1];
  13228. byte tag[AES_BLOCK_SIZE];
  13229. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13230. byte out[AES_BLOCK_SIZE * 3 + 2];
  13231. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13232. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13233. byte key[AES_128_KEY_SIZE] = { 0, };
  13234. byte iv[AES_IV_SIZE] = { 1, };
  13235. byte ivOut[AES_IV_SIZE];
  13236. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13237. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13238. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13239. };
  13240. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13241. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13242. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13243. };
  13244. static const byte expTag[AES_BLOCK_SIZE] = {
  13245. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13246. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13247. };
  13248. /* Create a random for generating IV/nonce. */
  13249. AssertIntEQ(wc_InitRng(rng), 0);
  13250. /* Initialize data structures. */
  13251. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13252. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13253. /* BadParameters to streaming init. */
  13254. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13255. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13256. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13257. BAD_FUNC_ARG);
  13258. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13259. BAD_FUNC_ARG);
  13260. /* Bad parameters to encrypt update. */
  13261. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13262. BAD_FUNC_ARG);
  13263. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13264. BAD_FUNC_ARG);
  13265. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13266. BAD_FUNC_ARG);
  13267. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13268. BAD_FUNC_ARG);
  13269. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13270. BAD_FUNC_ARG);
  13271. /* Bad parameters to decrypt update. */
  13272. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13273. BAD_FUNC_ARG);
  13274. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13275. BAD_FUNC_ARG);
  13276. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13277. BAD_FUNC_ARG);
  13278. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13279. BAD_FUNC_ARG);
  13280. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13281. BAD_FUNC_ARG);
  13282. /* Bad parameters to encrypt final. */
  13283. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13284. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13285. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13286. BAD_FUNC_ARG);
  13287. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13288. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13289. BAD_FUNC_ARG);
  13290. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13291. BAD_FUNC_ARG);
  13292. /* Bad parameters to decrypt final. */
  13293. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13294. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13295. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13296. BAD_FUNC_ARG);
  13297. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13298. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13299. BAD_FUNC_ARG);
  13300. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13301. BAD_FUNC_ARG);
  13302. /* Check calling final before setting key fails. */
  13303. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13304. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13305. /* Check calling update before setting key else fails. */
  13306. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13307. MISSING_KEY);
  13308. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13309. MISSING_KEY);
  13310. /* Set key but not IV. */
  13311. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13312. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13313. /* Check calling final before setting IV fails. */
  13314. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13315. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13316. /* Check calling update before setting IV else fails. */
  13317. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13318. MISSING_IV);
  13319. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13320. MISSING_IV);
  13321. /* Set IV using fixed part IV and external IV APIs. */
  13322. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13323. rng), 0);
  13324. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13325. GCM_NONCE_MID_SZ), 0);
  13326. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13327. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13328. /* Encrypt and decrypt data. */
  13329. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13330. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13331. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  13332. /* Finalize and check tag matches. */
  13333. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13334. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13335. /* Set key and IV through streaming init API. */
  13336. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13337. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13338. /* Encrypt/decrypt one block and AAD of one block. */
  13339. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13340. AES_BLOCK_SIZE), 0);
  13341. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13342. AES_BLOCK_SIZE), 0);
  13343. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13344. /* Finalize and check tag matches. */
  13345. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13346. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13347. /* Set key and IV through streaming init API. */
  13348. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13349. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13350. /* No data to encrypt/decrypt one byte of AAD. */
  13351. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13352. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13353. /* Finalize and check tag matches. */
  13354. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13355. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13356. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13357. /* Set key and IV through streaming init API. */
  13358. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13359. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13360. /* Encrypt/decrypt one byte and no AAD. */
  13361. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  13362. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  13363. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  13364. /* Finalize and check tag matches. */
  13365. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13366. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  13367. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13368. /* Set key and IV through streaming init API. */
  13369. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13370. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13371. /* Encryption AES is one byte at a time */
  13372. for (i = 0; i < (int)sizeof(aad); i++) {
  13373. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13374. 0);
  13375. }
  13376. for (i = 0; i < (int)sizeof(in); i++) {
  13377. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13378. 0);
  13379. }
  13380. /* Decryption AES is two bytes at a time */
  13381. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13382. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13383. 0);
  13384. }
  13385. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13386. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13387. 0), 0);
  13388. }
  13389. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13390. /* Finalize and check tag matches. */
  13391. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13392. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13393. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13394. /* Check streaming encryption can be decrypted with one shot. */
  13395. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13396. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13397. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13398. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13399. wc_AesFree(aesEnc);
  13400. wc_AesFree(aesDec);
  13401. wc_FreeRng(rng);
  13402. #endif
  13403. return ret;
  13404. } /* END test_wc_AesGcmStream */
  13405. /*
  13406. * unit test for wc_Des3_SetIV()
  13407. */
  13408. static int test_wc_Des3_SetIV(void)
  13409. {
  13410. int ret = 0;
  13411. #ifndef NO_DES3
  13412. Des3 des;
  13413. const byte key[] =
  13414. {
  13415. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13416. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13417. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13418. };
  13419. const byte iv[] =
  13420. {
  13421. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13422. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13423. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13424. };
  13425. printf(testingFmt, "wc_Des3_SetIV()");
  13426. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13427. if (ret != 0)
  13428. return ret;
  13429. /* DES_ENCRYPTION or DES_DECRYPTION */
  13430. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13431. if (ret == 0) {
  13432. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13433. ret = WOLFSSL_FATAL_ERROR;
  13434. }
  13435. }
  13436. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  13437. /* Test explicitly wc_Des3_SetIV() */
  13438. if (ret == 0) {
  13439. ret = wc_Des3_SetIV(NULL, iv);
  13440. if (ret == BAD_FUNC_ARG) {
  13441. ret = wc_Des3_SetIV(&des, NULL);
  13442. } else if (ret == 0) {
  13443. ret = WOLFSSL_FATAL_ERROR;
  13444. }
  13445. }
  13446. #endif
  13447. wc_Des3Free(&des);
  13448. printf(resultFmt, ret == 0 ? passed : failed);
  13449. #endif
  13450. return ret;
  13451. } /* END test_wc_Des3_SetIV */
  13452. /*
  13453. * unit test for wc_Des3_SetKey()
  13454. */
  13455. static int test_wc_Des3_SetKey(void)
  13456. {
  13457. int ret = 0;
  13458. #ifndef NO_DES3
  13459. Des3 des;
  13460. const byte key[] =
  13461. {
  13462. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13463. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13464. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13465. };
  13466. const byte iv[] =
  13467. {
  13468. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13469. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13470. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13471. };
  13472. printf(testingFmt, "wc_Des3_SetKey()");
  13473. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13474. if (ret != 0)
  13475. return ret;
  13476. /* DES_ENCRYPTION or DES_DECRYPTION */
  13477. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13478. if (ret == 0) {
  13479. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13480. ret = WOLFSSL_FATAL_ERROR;
  13481. }
  13482. }
  13483. /* Test bad args. */
  13484. if (ret == 0) {
  13485. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  13486. if (ret == BAD_FUNC_ARG) {
  13487. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  13488. }
  13489. if (ret == BAD_FUNC_ARG) {
  13490. ret = wc_Des3_SetKey(&des, key, iv, -1);
  13491. }
  13492. if (ret == BAD_FUNC_ARG) {
  13493. /* Default case. Should return 0. */
  13494. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  13495. }
  13496. } /* END if ret != 0 */
  13497. wc_Des3Free(&des);
  13498. printf(resultFmt, ret == 0 ? passed : failed);
  13499. #endif
  13500. return ret;
  13501. } /* END test_wc_Des3_SetKey */
  13502. /*
  13503. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  13504. */
  13505. static int test_wc_Des3_CbcEncryptDecrypt(void)
  13506. {
  13507. int ret = 0;
  13508. #ifndef NO_DES3
  13509. Des3 des;
  13510. byte cipher[24];
  13511. byte plain[24];
  13512. const byte key[] =
  13513. {
  13514. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13515. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13516. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13517. };
  13518. const byte iv[] =
  13519. {
  13520. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13521. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13522. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13523. };
  13524. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  13525. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13526. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13527. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13528. };
  13529. printf(testingFmt, "wc_Des3_CbcEncrypt()");
  13530. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13531. if (ret != 0)
  13532. return ret;
  13533. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13534. if (ret == 0) {
  13535. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  13536. if (ret == 0) {
  13537. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  13538. }
  13539. if (ret == 0) {
  13540. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  13541. }
  13542. }
  13543. if (ret == 0) {
  13544. if (XMEMCMP(plain, vector, 24) != 0) {
  13545. ret = WOLFSSL_FATAL_ERROR;
  13546. }
  13547. }
  13548. /* Pass in bad args. */
  13549. if (ret == 0) {
  13550. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  13551. if (ret == BAD_FUNC_ARG) {
  13552. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  13553. }
  13554. if (ret == BAD_FUNC_ARG) {
  13555. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  13556. }
  13557. if (ret != BAD_FUNC_ARG) {
  13558. ret = WOLFSSL_FATAL_ERROR;
  13559. } else {
  13560. ret = 0;
  13561. }
  13562. }
  13563. if (ret == 0) {
  13564. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  13565. if (ret == BAD_FUNC_ARG) {
  13566. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  13567. }
  13568. if (ret == BAD_FUNC_ARG) {
  13569. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  13570. }
  13571. if (ret != BAD_FUNC_ARG) {
  13572. ret = WOLFSSL_FATAL_ERROR;
  13573. } else {
  13574. ret = 0;
  13575. }
  13576. }
  13577. wc_Des3Free(&des);
  13578. printf(resultFmt, ret == 0 ? passed : failed);
  13579. #endif
  13580. return ret;
  13581. } /* END wc_Des3_CbcEncrypt */
  13582. /*
  13583. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  13584. */
  13585. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  13586. {
  13587. int ret = 0;
  13588. #ifndef NO_DES3
  13589. word32 vectorSz, cipherSz;
  13590. byte cipher[24];
  13591. byte plain[24];
  13592. byte vector[] = /* Now is the time for all w/o trailing 0 */
  13593. {
  13594. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13595. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13596. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13597. };
  13598. byte key[] =
  13599. {
  13600. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13601. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13602. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13603. };
  13604. byte iv[] =
  13605. {
  13606. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13607. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13608. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13609. };
  13610. vectorSz = sizeof(byte) * 24;
  13611. cipherSz = sizeof(byte) * 24;
  13612. printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
  13613. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  13614. if (ret == 0) {
  13615. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  13616. if (ret == 0) {
  13617. if (XMEMCMP(plain, vector, 24) != 0) {
  13618. ret = WOLFSSL_FATAL_ERROR;
  13619. }
  13620. }
  13621. }
  13622. /* pass in bad args. */
  13623. if (ret == 0) {
  13624. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  13625. if (ret == BAD_FUNC_ARG) {
  13626. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  13627. }
  13628. if (ret == BAD_FUNC_ARG) {
  13629. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  13630. }
  13631. if (ret == BAD_FUNC_ARG) {
  13632. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  13633. key, NULL);
  13634. } else {
  13635. /* Return code catch. */
  13636. ret = WOLFSSL_FAILURE;
  13637. }
  13638. }
  13639. if (ret == 0) {
  13640. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  13641. if (ret == BAD_FUNC_ARG) {
  13642. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  13643. }
  13644. if (ret == BAD_FUNC_ARG) {
  13645. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  13646. }
  13647. if (ret == BAD_FUNC_ARG) {
  13648. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  13649. } else {
  13650. ret = WOLFSSL_FAILURE;
  13651. }
  13652. }
  13653. printf(resultFmt, ret == 0 ? passed : failed);
  13654. #endif
  13655. return ret;
  13656. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  13657. /*
  13658. * Unit test for wc_Des3_EcbEncrypt
  13659. */
  13660. static int test_wc_Des3_EcbEncrypt(void)
  13661. {
  13662. int ret = 0;
  13663. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  13664. Des3 des;
  13665. byte cipher[24];
  13666. word32 cipherSz = sizeof(cipher);
  13667. const byte key[] =
  13668. {
  13669. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13670. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13671. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13672. };
  13673. const byte iv[] =
  13674. {
  13675. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13676. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13677. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13678. };
  13679. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  13680. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13681. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13682. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13683. };
  13684. printf(testingFmt, "wc_Des3_EcbEncrypt()");
  13685. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13686. if (ret != 0) {
  13687. return ret;
  13688. }
  13689. if (ret == 0 ) {
  13690. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13691. }
  13692. /* Bad Cases */
  13693. if (ret == 0) {
  13694. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  13695. if (ret == BAD_FUNC_ARG) {
  13696. ret = 0;
  13697. }
  13698. }
  13699. if (ret == 0) {
  13700. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  13701. if (ret == BAD_FUNC_ARG) {
  13702. ret = 0;
  13703. }
  13704. }
  13705. if (ret == 0) {
  13706. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  13707. if (ret == BAD_FUNC_ARG) {
  13708. ret = 0;
  13709. }
  13710. }
  13711. if (ret == 0) {
  13712. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  13713. if (ret == BAD_FUNC_ARG) {
  13714. ret = 0;
  13715. }
  13716. }
  13717. if (ret == 0) {
  13718. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  13719. if (ret == BAD_FUNC_ARG) {
  13720. ret = 0;
  13721. }
  13722. }
  13723. /* Good Cases */
  13724. if (ret == 0) {
  13725. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  13726. }
  13727. wc_Des3Free(&des);
  13728. printf(resultFmt, ret == 0 ? passed : failed);
  13729. #endif
  13730. return ret;
  13731. } /* END test_wc_Des3_EcbEncrypt */
  13732. /*
  13733. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  13734. */
  13735. static int test_wc_Chacha_SetKey(void)
  13736. {
  13737. int ret = 0;
  13738. #ifdef HAVE_CHACHA
  13739. ChaCha ctx;
  13740. const byte key[] =
  13741. {
  13742. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13743. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13744. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13745. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13746. };
  13747. byte cipher[128];
  13748. printf(testingFmt, "wc_Chacha_SetKey()");
  13749. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  13750. /* Test bad args. */
  13751. if (ret == 0) {
  13752. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  13753. if (ret == BAD_FUNC_ARG) {
  13754. ret = wc_Chacha_SetKey(&ctx, key, 18);
  13755. }
  13756. if (ret == BAD_FUNC_ARG) {
  13757. ret = 0;
  13758. } else {
  13759. ret = WOLFSSL_FATAL_ERROR;
  13760. }
  13761. }
  13762. printf(resultFmt, ret == 0 ? passed : failed);
  13763. if (ret != 0) {
  13764. return ret;
  13765. }
  13766. printf(testingFmt, "wc_Chacha_SetIV");
  13767. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  13768. if (ret == 0) {
  13769. /* Test bad args. */
  13770. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  13771. if (ret == BAD_FUNC_ARG) {
  13772. ret = 0;
  13773. } else {
  13774. ret = WOLFSSL_FAILURE;
  13775. }
  13776. }
  13777. printf(resultFmt, ret == 0 ? passed : failed);
  13778. #endif
  13779. return ret;
  13780. } /* END test_wc_Chacha_SetKey */
  13781. /*
  13782. * unit test for wc_Poly1305SetKey()
  13783. */
  13784. static int test_wc_Poly1305SetKey(void)
  13785. {
  13786. int ret = 0;
  13787. #ifdef HAVE_POLY1305
  13788. Poly1305 ctx;
  13789. const byte key[] =
  13790. {
  13791. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13792. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13793. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13794. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13795. };
  13796. printf(testingFmt, "wc_Poly1305_SetKey()");
  13797. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  13798. /* Test bad args. */
  13799. if (ret == 0) {
  13800. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  13801. if(ret == BAD_FUNC_ARG) {
  13802. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  13803. }
  13804. if (ret == BAD_FUNC_ARG) {
  13805. ret = wc_Poly1305SetKey(&ctx, key, 18);
  13806. }
  13807. if (ret == BAD_FUNC_ARG) {
  13808. ret = 0;
  13809. } else {
  13810. ret = WOLFSSL_FATAL_ERROR;
  13811. }
  13812. }
  13813. printf(resultFmt, ret == 0 ? passed : failed);
  13814. #endif
  13815. return ret;
  13816. } /* END test_wc_Poly1305_SetKey() */
  13817. /*
  13818. * Testing wc_Chacha_Process()
  13819. */
  13820. static int test_wc_Chacha_Process(void)
  13821. {
  13822. int ret = 0;
  13823. #ifdef HAVE_CHACHA
  13824. ChaCha enc, dec;
  13825. byte cipher[128];
  13826. byte plain[128];
  13827. const byte key[] =
  13828. {
  13829. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13830. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13831. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13832. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13833. };
  13834. const char* input = "Everybody gets Friday off.";
  13835. word32 keySz = sizeof(key)/sizeof(byte);
  13836. unsigned long int inlen = XSTRLEN(input);
  13837. /*Initialize stack varialbes.*/
  13838. XMEMSET(cipher, 0, 128);
  13839. XMEMSET(plain, 0, 128);
  13840. printf(testingFmt, "wc_Chacha_Process()");
  13841. ret = wc_Chacha_SetKey(&enc, key, keySz);
  13842. AssertIntEQ(ret, 0);
  13843. ret = wc_Chacha_SetKey(&dec, key, keySz);
  13844. AssertIntEQ(ret, 0);
  13845. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  13846. AssertIntEQ(ret, 0);
  13847. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  13848. AssertIntEQ(ret, 0);
  13849. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  13850. AssertIntEQ(ret, 0);
  13851. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  13852. AssertIntEQ(ret, 0);
  13853. ret = XMEMCMP(input, plain, (int)inlen);
  13854. AssertIntEQ(ret, 0);
  13855. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  13856. /* test checking and using leftovers, currently just in C code */
  13857. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  13858. AssertIntEQ(ret, 0);
  13859. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  13860. AssertIntEQ(ret, 0);
  13861. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  13862. AssertIntEQ(ret, 0);
  13863. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  13864. (byte*)input + (inlen - 2), 2);
  13865. AssertIntEQ(ret, 0);
  13866. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  13867. AssertIntEQ(ret, 0);
  13868. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  13869. (byte*)input + (inlen - 2), 2);
  13870. AssertIntEQ(ret, 0);
  13871. ret = XMEMCMP(input, plain, (int)inlen);
  13872. AssertIntEQ(ret, 0);
  13873. /* check edge cases with counter increment */
  13874. {
  13875. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  13876. const byte expected[] = {
  13877. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  13878. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  13879. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  13880. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  13881. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  13882. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  13883. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  13884. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  13885. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  13886. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  13887. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  13888. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  13889. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  13890. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  13891. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  13892. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  13893. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  13894. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  13895. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  13896. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  13897. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  13898. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  13899. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  13900. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  13901. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  13902. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  13903. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  13904. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  13905. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  13906. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  13907. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  13908. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  13909. };
  13910. const byte iv2[] = {
  13911. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  13912. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  13913. };
  13914. byte input2[256];
  13915. int i;
  13916. for (i = 0; i < 256; i++)
  13917. input2[i] = i;
  13918. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  13919. AssertIntEQ(ret, 0);
  13920. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  13921. AssertIntEQ(ret, 0);
  13922. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  13923. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  13924. AssertIntEQ(ret, 0);
  13925. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  13926. /* partial */
  13927. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  13928. AssertIntEQ(ret, 0);
  13929. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  13930. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  13931. AssertIntEQ(ret, 0);
  13932. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  13933. }
  13934. #endif
  13935. /* Test bad args. */
  13936. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  13937. AssertIntEQ(ret, BAD_FUNC_ARG);
  13938. if (ret == BAD_FUNC_ARG) {
  13939. ret = 0;
  13940. }
  13941. printf(resultFmt, ret == 0 ? passed : failed);
  13942. #endif
  13943. return ret;
  13944. } /* END test_wc_Chacha_Process */
  13945. /*
  13946. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  13947. */
  13948. static int test_wc_ChaCha20Poly1305_aead(void)
  13949. {
  13950. int ret = 0;
  13951. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  13952. const byte key[] = {
  13953. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  13954. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  13955. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  13956. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  13957. };
  13958. const byte plaintext[] = {
  13959. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  13960. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  13961. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  13962. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  13963. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  13964. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  13965. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  13966. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  13967. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  13968. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  13969. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  13970. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  13971. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  13972. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  13973. 0x74, 0x2e
  13974. };
  13975. const byte iv[] = {
  13976. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  13977. 0x44, 0x45, 0x46, 0x47
  13978. };
  13979. const byte aad[] = { /* additional data */
  13980. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  13981. 0xc4, 0xc5, 0xc6, 0xc7
  13982. };
  13983. const byte cipher[] = { /* expected output from operation */
  13984. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  13985. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  13986. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  13987. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  13988. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  13989. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  13990. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  13991. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  13992. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  13993. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  13994. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  13995. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  13996. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  13997. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  13998. 0x61, 0x16
  13999. };
  14000. const byte authTag[] = { /* expected output from operation */
  14001. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14002. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14003. };
  14004. byte generatedCiphertext[272];
  14005. byte generatedPlaintext[272];
  14006. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14007. /* Initialize stack variables. */
  14008. XMEMSET(generatedCiphertext, 0, 272);
  14009. XMEMSET(generatedPlaintext, 0, 272);
  14010. /* Test Encrypt */
  14011. printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
  14012. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  14013. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14014. AssertIntEQ(ret, 0);
  14015. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  14016. AssertIntEQ(ret, 0);
  14017. /* Test bad args. */
  14018. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  14019. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14020. AssertIntEQ(ret, BAD_FUNC_ARG);
  14021. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14022. plaintext, sizeof(plaintext),
  14023. generatedCiphertext, generatedAuthTag);
  14024. AssertIntEQ(ret, BAD_FUNC_ARG);
  14025. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14026. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14027. AssertIntEQ(ret, BAD_FUNC_ARG);
  14028. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14029. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14030. AssertIntEQ(ret, BAD_FUNC_ARG);
  14031. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14032. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  14033. AssertIntEQ(ret, BAD_FUNC_ARG);
  14034. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14035. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  14036. if (ret == BAD_FUNC_ARG) {
  14037. ret = 0;
  14038. }
  14039. printf(resultFmt, ret == 0 ? passed : failed);
  14040. if (ret != 0) {
  14041. return ret;
  14042. }
  14043. printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
  14044. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14045. sizeof(cipher), authTag, generatedPlaintext);
  14046. AssertIntEQ(ret, 0);
  14047. ret = XMEMCMP(generatedPlaintext, plaintext,
  14048. sizeof(plaintext)/sizeof(byte));
  14049. AssertIntEQ(ret, 0);
  14050. /* Test bad args. */
  14051. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14052. sizeof(cipher), authTag, generatedPlaintext);
  14053. AssertIntEQ(ret, BAD_FUNC_ARG);
  14054. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14055. cipher, sizeof(cipher), authTag, generatedPlaintext);
  14056. AssertIntEQ(ret, BAD_FUNC_ARG);
  14057. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14058. sizeof(cipher), authTag, generatedPlaintext);
  14059. AssertIntEQ(ret, BAD_FUNC_ARG);
  14060. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14061. sizeof(cipher), NULL, generatedPlaintext);
  14062. AssertIntEQ(ret, BAD_FUNC_ARG);
  14063. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14064. sizeof(cipher), authTag, NULL);
  14065. AssertIntEQ(ret, BAD_FUNC_ARG);
  14066. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14067. sizeof(cipher), authTag, generatedPlaintext);
  14068. AssertIntEQ(ret, BAD_FUNC_ARG);
  14069. if (ret == BAD_FUNC_ARG) {
  14070. ret = 0;
  14071. }
  14072. printf(resultFmt, ret == 0 ? passed : failed);
  14073. #endif
  14074. return ret;
  14075. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  14076. /*
  14077. * Testing function for wc_Rc2SetKey().
  14078. */
  14079. static int test_wc_Rc2SetKey(void)
  14080. {
  14081. int ret = 0;
  14082. #ifdef WC_RC2
  14083. Rc2 rc2;
  14084. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14085. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14086. printf(testingFmt, "wc_Rc2SetKey()");
  14087. /* valid key and IV */
  14088. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14089. iv, 40);
  14090. if (ret == 0) {
  14091. /* valid key, no IV */
  14092. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14093. NULL, 40);
  14094. }
  14095. /* bad arguments */
  14096. if (ret == 0) {
  14097. /* null Rc2 struct */
  14098. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14099. iv, 40);
  14100. if (ret == BAD_FUNC_ARG) {
  14101. ret = 0;
  14102. }
  14103. }
  14104. if (ret == 0) {
  14105. /* null key */
  14106. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14107. iv, 40);
  14108. if (ret == BAD_FUNC_ARG) {
  14109. ret = 0;
  14110. }
  14111. }
  14112. if (ret == 0) {
  14113. /* key size == 0 */
  14114. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  14115. if (ret == WC_KEY_SIZE_E) {
  14116. ret = 0;
  14117. }
  14118. }
  14119. if (ret == 0) {
  14120. /* key size > 128 */
  14121. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  14122. if (ret == WC_KEY_SIZE_E) {
  14123. ret = 0;
  14124. }
  14125. }
  14126. if (ret == 0) {
  14127. /* effective bits == 0 */
  14128. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14129. iv, 0);
  14130. if (ret == WC_KEY_SIZE_E) {
  14131. ret = 0;
  14132. }
  14133. }
  14134. if (ret == 0) {
  14135. /* effective bits > 1024 */
  14136. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14137. iv, 1025);
  14138. if (ret == WC_KEY_SIZE_E) {
  14139. ret = 0;
  14140. }
  14141. }
  14142. printf(resultFmt, ret == 0 ? passed : failed);
  14143. #endif
  14144. return ret;
  14145. } /* END test_wc_Rc2SetKey */
  14146. /*
  14147. * Testing function for wc_Rc2SetIV().
  14148. */
  14149. static int test_wc_Rc2SetIV(void)
  14150. {
  14151. int ret = 0;
  14152. #ifdef WC_RC2
  14153. Rc2 rc2;
  14154. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14155. printf(testingFmt, "wc_Rc2SetIV()");
  14156. /* valid IV */
  14157. ret = wc_Rc2SetIV(&rc2, iv);
  14158. if (ret == 0) {
  14159. /* valid NULL IV */
  14160. ret = wc_Rc2SetIV(&rc2, NULL);
  14161. }
  14162. /* bad arguments */
  14163. if (ret == 0) {
  14164. ret = wc_Rc2SetIV(NULL, iv);
  14165. if (ret == BAD_FUNC_ARG) {
  14166. ret = 0;
  14167. }
  14168. }
  14169. printf(resultFmt, ret == 0 ? passed : failed);
  14170. #endif
  14171. return ret;
  14172. } /* END test_wc_Rc2SetKey */
  14173. /*
  14174. * Testing function for wc_Rc2EcbEncrypt().
  14175. */
  14176. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14177. {
  14178. int ret = 0;
  14179. #ifdef WC_RC2
  14180. Rc2 rc2;
  14181. int effectiveKeyBits = 63;
  14182. byte cipher[RC2_BLOCK_SIZE];
  14183. byte plain[RC2_BLOCK_SIZE];
  14184. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14185. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14186. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  14187. printf(testingFmt, "wc_Rc2EcbEncryptDecrypt()");
  14188. XMEMSET(cipher, 0, sizeof(cipher));
  14189. XMEMSET(plain, 0, sizeof(plain));
  14190. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14191. NULL, effectiveKeyBits);
  14192. if (ret == 0) {
  14193. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  14194. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  14195. ret = WOLFSSL_FATAL_ERROR;
  14196. }
  14197. if (ret == 0) {
  14198. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  14199. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  14200. ret = WOLFSSL_FATAL_ERROR;
  14201. }
  14202. }
  14203. }
  14204. /* Rc2EcbEncrypt bad arguments */
  14205. if (ret == 0) {
  14206. /* null Rc2 struct */
  14207. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  14208. if (ret == BAD_FUNC_ARG) {
  14209. ret = 0;
  14210. }
  14211. }
  14212. if (ret == 0) {
  14213. /* null out buffer */
  14214. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  14215. if (ret == BAD_FUNC_ARG) {
  14216. ret = 0;
  14217. }
  14218. }
  14219. if (ret == 0) {
  14220. /* null input buffer */
  14221. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  14222. if (ret == BAD_FUNC_ARG) {
  14223. ret = 0;
  14224. }
  14225. }
  14226. if (ret == 0) {
  14227. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14228. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  14229. if (ret == BUFFER_E) {
  14230. ret = 0;
  14231. }
  14232. }
  14233. /* Rc2EcbDecrypt bad arguments */
  14234. if (ret == 0) {
  14235. /* null Rc2 struct */
  14236. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  14237. if (ret == BAD_FUNC_ARG) {
  14238. ret = 0;
  14239. }
  14240. }
  14241. if (ret == 0) {
  14242. /* null out buffer */
  14243. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  14244. if (ret == BAD_FUNC_ARG) {
  14245. ret = 0;
  14246. }
  14247. }
  14248. if (ret == 0) {
  14249. /* null input buffer */
  14250. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  14251. if (ret == BAD_FUNC_ARG) {
  14252. ret = 0;
  14253. }
  14254. }
  14255. if (ret == 0) {
  14256. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14257. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  14258. if (ret == BUFFER_E) {
  14259. ret = 0;
  14260. }
  14261. }
  14262. printf(resultFmt, ret == 0 ? passed : failed);
  14263. #endif
  14264. return ret;
  14265. } /* END test_wc_Rc2SetKey */
  14266. /*
  14267. * Testing function for wc_Rc2CbcEncrypt().
  14268. */
  14269. static int test_wc_Rc2CbcEncryptDecrypt(void)
  14270. {
  14271. int ret = 0;
  14272. #ifdef WC_RC2
  14273. Rc2 rc2;
  14274. int effectiveKeyBits = 63;
  14275. byte cipher[RC2_BLOCK_SIZE*2];
  14276. byte plain[RC2_BLOCK_SIZE*2];
  14277. /* vector taken from test.c */
  14278. byte key[] = {
  14279. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14280. };
  14281. byte iv[] = {
  14282. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14283. };
  14284. byte input[] = {
  14285. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14286. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14287. };
  14288. byte output[] = {
  14289. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  14290. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  14291. };
  14292. printf(testingFmt, "wc_Rc2CbcEncryptDecrypt()");
  14293. XMEMSET(cipher, 0, sizeof(cipher));
  14294. XMEMSET(plain, 0, sizeof(plain));
  14295. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14296. iv, effectiveKeyBits);
  14297. if (ret == 0) {
  14298. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  14299. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  14300. ret = WOLFSSL_FATAL_ERROR;
  14301. } else {
  14302. /* reset IV for decrypt */
  14303. ret = wc_Rc2SetIV(&rc2, iv);
  14304. }
  14305. if (ret == 0) {
  14306. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  14307. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  14308. ret = WOLFSSL_FATAL_ERROR;
  14309. }
  14310. }
  14311. }
  14312. /* Rc2CbcEncrypt bad arguments */
  14313. if (ret == 0) {
  14314. /* null Rc2 struct */
  14315. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  14316. if (ret == BAD_FUNC_ARG) {
  14317. ret = 0;
  14318. }
  14319. }
  14320. if (ret == 0) {
  14321. /* null out buffer */
  14322. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  14323. if (ret == BAD_FUNC_ARG) {
  14324. ret = 0;
  14325. }
  14326. }
  14327. if (ret == 0) {
  14328. /* null input buffer */
  14329. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  14330. if (ret == BAD_FUNC_ARG) {
  14331. ret = 0;
  14332. }
  14333. }
  14334. /* Rc2CbcDecrypt bad arguments */
  14335. if (ret == 0) {
  14336. /* in size is 0 */
  14337. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  14338. if (ret != 0) {
  14339. ret = WOLFSSL_FATAL_ERROR;
  14340. }
  14341. }
  14342. if (ret == 0) {
  14343. /* null Rc2 struct */
  14344. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  14345. if (ret == BAD_FUNC_ARG) {
  14346. ret = 0;
  14347. }
  14348. }
  14349. if (ret == 0) {
  14350. /* null out buffer */
  14351. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  14352. if (ret == BAD_FUNC_ARG) {
  14353. ret = 0;
  14354. }
  14355. }
  14356. if (ret == 0) {
  14357. /* null input buffer */
  14358. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  14359. if (ret == BAD_FUNC_ARG) {
  14360. ret = 0;
  14361. }
  14362. }
  14363. printf(resultFmt, ret == 0 ? passed : failed);
  14364. #endif
  14365. return ret;
  14366. } /* END test_wc_Rc2SetKey */
  14367. /*
  14368. * Testing function for wc_AesSetIV
  14369. */
  14370. static int test_wc_AesSetIV(void)
  14371. {
  14372. int ret = 0;
  14373. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14374. Aes aes;
  14375. byte key16[] =
  14376. {
  14377. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14378. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14379. };
  14380. byte iv1[] = "1234567890abcdef";
  14381. byte iv2[] = "0987654321fedcba";
  14382. printf(testingFmt, "wc_AesSetIV()");
  14383. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14384. if (ret != 0)
  14385. return ret;
  14386. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14387. iv1, AES_ENCRYPTION);
  14388. if(ret == 0) {
  14389. ret = wc_AesSetIV(&aes, iv2);
  14390. }
  14391. /* Test bad args. */
  14392. if(ret == 0) {
  14393. ret = wc_AesSetIV(NULL, iv1);
  14394. if(ret == BAD_FUNC_ARG) {
  14395. /* NULL iv should return 0. */
  14396. ret = wc_AesSetIV(&aes, NULL);
  14397. } else {
  14398. ret = WOLFSSL_FATAL_ERROR;
  14399. }
  14400. }
  14401. wc_AesFree(&aes);
  14402. printf(resultFmt, ret == 0 ? passed : failed);
  14403. #endif
  14404. return ret;
  14405. } /* test_wc_AesSetIV */
  14406. /*
  14407. * Testing function for wc_AesSetKey().
  14408. */
  14409. static int test_wc_AesSetKey(void)
  14410. {
  14411. int ret = 0;
  14412. #ifndef NO_AES
  14413. Aes aes;
  14414. byte key16[] =
  14415. {
  14416. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14417. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14418. };
  14419. #ifdef WOLFSSL_AES_192
  14420. byte key24[] =
  14421. {
  14422. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14423. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14424. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14425. };
  14426. #endif
  14427. #ifdef WOLFSSL_AES_256
  14428. byte key32[] =
  14429. {
  14430. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14431. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14432. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14433. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14434. };
  14435. #endif
  14436. byte badKey16[] =
  14437. {
  14438. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14439. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14440. };
  14441. byte iv[] = "1234567890abcdef";
  14442. printf(testingFmt, "wc_AesSetKey()");
  14443. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14444. if (ret != 0)
  14445. return ret;
  14446. #ifdef WOLFSSL_AES_128
  14447. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14448. iv, AES_ENCRYPTION);
  14449. #endif
  14450. #ifdef WOLFSSL_AES_192
  14451. if (ret == 0) {
  14452. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  14453. iv, AES_ENCRYPTION);
  14454. }
  14455. #endif
  14456. #ifdef WOLFSSL_AES_256
  14457. if (ret == 0) {
  14458. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  14459. iv, AES_ENCRYPTION);
  14460. }
  14461. #endif
  14462. /* Pass in bad args. */
  14463. if (ret == 0) {
  14464. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  14465. iv, AES_ENCRYPTION);
  14466. if (ret == BAD_FUNC_ARG) {
  14467. ret = wc_AesSetKey(&aes, badKey16,
  14468. (word32) sizeof(badKey16) / sizeof(byte),
  14469. iv, AES_ENCRYPTION);
  14470. }
  14471. if (ret == BAD_FUNC_ARG) {
  14472. ret = 0;
  14473. } else {
  14474. ret = WOLFSSL_FATAL_ERROR;
  14475. }
  14476. }
  14477. wc_AesFree(&aes);
  14478. printf(resultFmt, ret == 0 ? passed : failed);
  14479. #endif
  14480. return ret;
  14481. } /* END test_wc_AesSetKey */
  14482. /*
  14483. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  14484. * and wc_AesCbcDecryptWithKey()
  14485. */
  14486. static int test_wc_AesCbcEncryptDecrypt(void)
  14487. {
  14488. int ret = 0;
  14489. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  14490. defined(WOLFSSL_AES_256)
  14491. Aes aes;
  14492. byte key32[] =
  14493. {
  14494. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14495. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14496. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14497. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14498. };
  14499. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  14500. {
  14501. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14502. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14503. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  14504. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  14505. };
  14506. byte iv[] = "1234567890abcdef";
  14507. byte enc[sizeof(vector)];
  14508. byte dec[sizeof(vector)];
  14509. int cbcE = WOLFSSL_FATAL_ERROR;
  14510. int cbcD = WOLFSSL_FATAL_ERROR;
  14511. int cbcDWK = WOLFSSL_FATAL_ERROR;
  14512. byte dec2[sizeof(vector)];
  14513. /* Init stack variables. */
  14514. XMEMSET(enc, 0, sizeof(enc));
  14515. XMEMSET(dec, 0, sizeof(vector));
  14516. XMEMSET(dec2, 0, sizeof(vector));
  14517. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14518. if (ret != 0)
  14519. return ret;
  14520. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  14521. if (ret == 0) {
  14522. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  14523. if (ret == 0) {
  14524. /* Re init for decrypt and set flag. */
  14525. cbcE = 0;
  14526. wc_AesFree(&aes);
  14527. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  14528. iv, AES_DECRYPTION);
  14529. }
  14530. if (ret == 0) {
  14531. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  14532. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  14533. ret = WOLFSSL_FATAL_ERROR;
  14534. } else {
  14535. /* Set flag. */
  14536. cbcD = 0;
  14537. }
  14538. }
  14539. }
  14540. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  14541. if (ret == 0 || cbcE == 0) {
  14542. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14543. key32, sizeof(key32)/sizeof(byte), iv);
  14544. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  14545. cbcDWK = 0;
  14546. }
  14547. }
  14548. printf(testingFmt, "wc_AesCbcEncrypt()");
  14549. /* Pass in bad args */
  14550. if (cbcE == 0) {
  14551. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  14552. if (cbcE == BAD_FUNC_ARG) {
  14553. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  14554. }
  14555. if (cbcE == BAD_FUNC_ARG) {
  14556. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  14557. }
  14558. if (cbcE == BAD_FUNC_ARG) {
  14559. cbcE = 0;
  14560. } else {
  14561. cbcE = WOLFSSL_FATAL_ERROR;
  14562. }
  14563. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  14564. if (cbcE == 0) {
  14565. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  14566. }
  14567. if (cbcE == BAD_LENGTH_E) {
  14568. cbcE = 0;
  14569. } else {
  14570. cbcE = WOLFSSL_FATAL_ERROR;
  14571. }
  14572. #endif
  14573. }
  14574. if (cbcE == 0) {
  14575. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14576. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  14577. printf("Zero length inputs not supported with AESNI in FIPS mode (v2),"
  14578. " skip test");
  14579. #else
  14580. /* Test passing in size of 0 */
  14581. XMEMSET(enc, 0, sizeof(enc));
  14582. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  14583. if (cbcE == 0) {
  14584. /* Check enc was not modified */
  14585. int i;
  14586. for (i = 0; i < (int)sizeof(enc); i++)
  14587. cbcE |= enc[i];
  14588. }
  14589. #endif
  14590. }
  14591. printf(resultFmt, cbcE == 0 ? passed : failed);
  14592. if (cbcE != 0) {
  14593. wc_AesFree(&aes);
  14594. return cbcE;
  14595. }
  14596. printf(testingFmt, "wc_AesCbcDecrypt()");
  14597. if (cbcD == 0) {
  14598. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  14599. if (cbcD == BAD_FUNC_ARG) {
  14600. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  14601. }
  14602. if (cbcD == BAD_FUNC_ARG) {
  14603. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  14604. }
  14605. if (cbcD == BAD_FUNC_ARG) {
  14606. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  14607. }
  14608. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  14609. if (cbcD == BAD_LENGTH_E) {
  14610. cbcD = 0;
  14611. } else {
  14612. cbcD = WOLFSSL_FATAL_ERROR;
  14613. }
  14614. #else
  14615. if (cbcD == BAD_FUNC_ARG) {
  14616. cbcD = 0;
  14617. } else {
  14618. cbcD = WOLFSSL_FATAL_ERROR;
  14619. }
  14620. #endif
  14621. }
  14622. if (cbcD == 0) {
  14623. /* Test passing in size of 0 */
  14624. XMEMSET(dec, 0, sizeof(dec));
  14625. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  14626. if (cbcD == 0) {
  14627. /* Check dec was not modified */
  14628. int i;
  14629. for (i = 0; i < (int)sizeof(dec); i++)
  14630. cbcD |= dec[i];
  14631. }
  14632. }
  14633. printf(resultFmt, cbcD == 0 ? passed : failed);
  14634. if (cbcD != 0) {
  14635. wc_AesFree(&aes);
  14636. return cbcD;
  14637. }
  14638. printf(testingFmt, "wc_AesCbcDecryptWithKey()");
  14639. if (cbcDWK == 0) {
  14640. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  14641. key32, sizeof(key32)/sizeof(byte), iv);
  14642. if (cbcDWK == BAD_FUNC_ARG) {
  14643. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  14644. key32, sizeof(key32)/sizeof(byte), iv);
  14645. }
  14646. if (cbcDWK == BAD_FUNC_ARG) {
  14647. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14648. NULL, sizeof(key32)/sizeof(byte), iv);
  14649. }
  14650. if (cbcDWK == BAD_FUNC_ARG) {
  14651. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14652. key32, sizeof(key32)/sizeof(byte), NULL);
  14653. }
  14654. if (cbcDWK == BAD_FUNC_ARG) {
  14655. cbcDWK = 0;
  14656. } else {
  14657. cbcDWK = WOLFSSL_FATAL_ERROR;
  14658. }
  14659. }
  14660. wc_AesFree(&aes);
  14661. printf(resultFmt, cbcDWK == 0 ? passed : failed);
  14662. if (cbcDWK != 0) {
  14663. return cbcDWK;
  14664. }
  14665. #endif
  14666. return ret;
  14667. } /* END test_wc_AesCbcEncryptDecrypt */
  14668. /*
  14669. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  14670. */
  14671. static int test_wc_AesCtrEncryptDecrypt(void)
  14672. {
  14673. int ret = 0;
  14674. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  14675. Aes aesEnc, aesDec;
  14676. byte key32[] =
  14677. {
  14678. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14679. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14680. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14681. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14682. };
  14683. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14684. {
  14685. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14686. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14687. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14688. };
  14689. byte iv[] = "1234567890abcdef";
  14690. byte enc[AES_BLOCK_SIZE * 2];
  14691. byte dec[AES_BLOCK_SIZE * 2];
  14692. /* Init stack variables. */
  14693. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  14694. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  14695. printf(testingFmt, "wc_AesCtrEncrypt()");
  14696. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  14697. if (ret != 0)
  14698. return ret;
  14699. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  14700. if (ret != 0) {
  14701. wc_AesFree(&aesEnc);
  14702. return ret;
  14703. }
  14704. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  14705. iv, AES_ENCRYPTION);
  14706. if (ret == 0) {
  14707. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  14708. sizeof(vector)/sizeof(byte));
  14709. if (ret == 0) {
  14710. /* Decrypt with wc_AesCtrEncrypt() */
  14711. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  14712. iv, AES_ENCRYPTION);
  14713. }
  14714. if (ret == 0) {
  14715. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  14716. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  14717. ret = WOLFSSL_FATAL_ERROR;
  14718. }
  14719. }
  14720. }
  14721. /* Test bad args. */
  14722. if (ret == 0) {
  14723. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  14724. if (ret == BAD_FUNC_ARG) {
  14725. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  14726. }
  14727. if (ret == BAD_FUNC_ARG) {
  14728. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  14729. }
  14730. if (ret == BAD_FUNC_ARG) {
  14731. ret = 0;
  14732. } else {
  14733. ret = WOLFSSL_FATAL_ERROR;
  14734. }
  14735. }
  14736. wc_AesFree(&aesEnc);
  14737. wc_AesFree(&aesDec);
  14738. printf(resultFmt, ret == 0 ? passed : failed);
  14739. #endif
  14740. return ret;
  14741. } /* END test_wc_AesCtrEncryptDecrypt */
  14742. /*
  14743. * test function for wc_AesGcmSetKey()
  14744. */
  14745. static int test_wc_AesGcmSetKey(void)
  14746. {
  14747. int ret = 0;
  14748. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  14749. Aes aes;
  14750. #ifdef WOLFSSL_AES_128
  14751. byte key16[] =
  14752. {
  14753. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14754. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14755. };
  14756. #endif
  14757. #ifdef WOLFSSL_AES_192
  14758. byte key24[] =
  14759. {
  14760. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14761. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14762. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14763. };
  14764. #endif
  14765. #ifdef WOLFSSL_AES_256
  14766. byte key32[] =
  14767. {
  14768. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14769. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14770. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14771. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14772. };
  14773. #endif
  14774. byte badKey16[] =
  14775. {
  14776. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14777. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14778. };
  14779. byte badKey24[] =
  14780. {
  14781. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14782. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14783. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  14784. };
  14785. byte badKey32[] =
  14786. {
  14787. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  14788. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14789. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14790. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14791. };
  14792. printf(testingFmt, "wc_AesGcmSetKey()");
  14793. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14794. if (ret != 0)
  14795. return ret;
  14796. #ifdef WOLFSSL_AES_128
  14797. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  14798. #endif
  14799. #ifdef WOLFSSL_AES_192
  14800. if (ret == 0) {
  14801. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  14802. }
  14803. #endif
  14804. #ifdef WOLFSSL_AES_256
  14805. if (ret == 0) {
  14806. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  14807. }
  14808. #endif
  14809. /* Pass in bad args. */
  14810. if (ret == 0) {
  14811. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  14812. if (ret == BAD_FUNC_ARG) {
  14813. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  14814. }
  14815. if (ret == BAD_FUNC_ARG) {
  14816. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  14817. }
  14818. if (ret == BAD_FUNC_ARG) {
  14819. ret = 0;
  14820. } else {
  14821. ret = WOLFSSL_FATAL_ERROR;
  14822. }
  14823. }
  14824. wc_AesFree(&aes);
  14825. printf(resultFmt, ret == 0 ? passed : failed);
  14826. #endif
  14827. return ret;
  14828. } /* END test_wc_AesGcmSetKey */
  14829. /*
  14830. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  14831. */
  14832. static int test_wc_AesGcmEncryptDecrypt(void)
  14833. {
  14834. int ret = 0;
  14835. /* WOLFSSL_AFALG requires 12 byte IV */
  14836. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  14837. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  14838. Aes aes;
  14839. byte key32[] =
  14840. {
  14841. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14842. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14843. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14844. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14845. };
  14846. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14847. {
  14848. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14849. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14850. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14851. };
  14852. const byte a[] =
  14853. {
  14854. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  14855. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  14856. 0xab, 0xad, 0xda, 0xd2
  14857. };
  14858. byte iv[] = "1234567890a";
  14859. byte longIV[] = "1234567890abcdefghij";
  14860. byte enc[sizeof(vector)];
  14861. byte resultT[AES_BLOCK_SIZE];
  14862. byte dec[sizeof(vector)];
  14863. int gcmD = WOLFSSL_FATAL_ERROR;
  14864. int gcmE = WOLFSSL_FATAL_ERROR;
  14865. /* Init stack variables. */
  14866. XMEMSET(enc, 0, sizeof(vector));
  14867. XMEMSET(dec, 0, sizeof(vector));
  14868. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  14869. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14870. if (ret != 0)
  14871. return ret;
  14872. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  14873. if (ret == 0) {
  14874. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  14875. iv, sizeof(iv)/sizeof(byte), resultT,
  14876. sizeof(resultT), a, sizeof(a));
  14877. }
  14878. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  14879. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  14880. iv, sizeof(iv)/sizeof(byte), resultT,
  14881. sizeof(resultT), a, sizeof(a));
  14882. if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  14883. gcmD = WOLFSSL_FATAL_ERROR;
  14884. }
  14885. }
  14886. printf(testingFmt, "wc_AesGcmEncrypt()");
  14887. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  14888. if (gcmE == 0) {
  14889. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  14890. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  14891. a, sizeof(a));
  14892. if (gcmE == BAD_FUNC_ARG) {
  14893. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14894. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  14895. resultT, sizeof(resultT) + 1, a, sizeof(a));
  14896. }
  14897. if (gcmE == BAD_FUNC_ARG) {
  14898. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14899. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  14900. resultT, sizeof(resultT) - 5, a, sizeof(a));
  14901. }
  14902. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14903. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  14904. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14905. /* FIPS does not check the lower bound of ivSz */
  14906. #else
  14907. if (gcmE == BAD_FUNC_ARG) {
  14908. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14909. sizeof(vector), iv, 0,
  14910. resultT, sizeof(resultT), a, sizeof(a));
  14911. }
  14912. #endif
  14913. if (gcmE == BAD_FUNC_ARG) {
  14914. gcmE = 0;
  14915. } else {
  14916. gcmE = WOLFSSL_FATAL_ERROR;
  14917. }
  14918. }
  14919. /* This case is now considered good. Long IVs are now allowed.
  14920. * Except for the original FIPS release, it still has an upper
  14921. * bound on the IV length. */
  14922. #if (!defined(HAVE_FIPS) || \
  14923. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  14924. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14925. if (gcmE == 0) {
  14926. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  14927. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  14928. a, sizeof(a));
  14929. }
  14930. #else
  14931. (void)longIV;
  14932. #endif /* Old FIPS */
  14933. /* END wc_AesGcmEncrypt */
  14934. printf(resultFmt, gcmE == 0 ? passed : failed);
  14935. if (gcmE != 0) {
  14936. wc_AesFree(&aes);
  14937. return gcmE;
  14938. }
  14939. #ifdef HAVE_AES_DECRYPT
  14940. printf(testingFmt, "wc_AesGcmDecrypt()");
  14941. if (gcmD == 0) {
  14942. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  14943. iv, sizeof(iv)/sizeof(byte), resultT,
  14944. sizeof(resultT), a, sizeof(a));
  14945. if (gcmD == BAD_FUNC_ARG) {
  14946. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  14947. iv, sizeof(iv)/sizeof(byte), resultT,
  14948. sizeof(resultT), a, sizeof(a));
  14949. }
  14950. if (gcmD == BAD_FUNC_ARG) {
  14951. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  14952. iv, sizeof(iv)/sizeof(byte), resultT,
  14953. sizeof(resultT), a, sizeof(a));
  14954. }
  14955. if (gcmD == BAD_FUNC_ARG) {
  14956. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14957. NULL, sizeof(iv)/sizeof(byte), resultT,
  14958. sizeof(resultT), a, sizeof(a));
  14959. }
  14960. if (gcmD == BAD_FUNC_ARG) {
  14961. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14962. iv, sizeof(iv)/sizeof(byte), NULL,
  14963. sizeof(resultT), a, sizeof(a));
  14964. }
  14965. if (gcmD == BAD_FUNC_ARG) {
  14966. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14967. iv, sizeof(iv)/sizeof(byte), resultT,
  14968. sizeof(resultT) + 1, a, sizeof(a));
  14969. }
  14970. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14971. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  14972. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14973. /* FIPS does not check the lower bound of ivSz */
  14974. #else
  14975. if (gcmD == BAD_FUNC_ARG) {
  14976. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  14977. iv, 0, resultT,
  14978. sizeof(resultT), a, sizeof(a));
  14979. }
  14980. #endif
  14981. if (gcmD == BAD_FUNC_ARG) {
  14982. gcmD = 0;
  14983. } else {
  14984. gcmD = WOLFSSL_FATAL_ERROR;
  14985. }
  14986. } /* END wc_AesGcmDecrypt */
  14987. printf(resultFmt, gcmD == 0 ? passed : failed);
  14988. #endif /* HAVE_AES_DECRYPT */
  14989. wc_AesFree(&aes);
  14990. #endif
  14991. return ret;
  14992. } /* END test_wc_AesGcmEncryptDecrypt */
  14993. /*
  14994. * unit test for wc_GmacSetKey()
  14995. */
  14996. static int test_wc_GmacSetKey(void)
  14997. {
  14998. int ret = 0;
  14999. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15000. Gmac gmac;
  15001. byte key16[] =
  15002. {
  15003. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15004. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15005. };
  15006. #ifdef WOLFSSL_AES_192
  15007. byte key24[] =
  15008. {
  15009. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15010. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15011. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15012. };
  15013. #endif
  15014. #ifdef WOLFSSL_AES_256
  15015. byte key32[] =
  15016. {
  15017. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15018. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15019. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15020. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15021. };
  15022. #endif
  15023. byte badKey16[] =
  15024. {
  15025. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15026. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15027. };
  15028. byte badKey24[] =
  15029. {
  15030. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15031. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15032. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15033. };
  15034. byte badKey32[] =
  15035. {
  15036. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15037. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15038. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15039. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15040. };
  15041. printf(testingFmt, "wc_GmacSetKey()");
  15042. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15043. if (ret != 0)
  15044. return ret;
  15045. #ifdef WOLFSSL_AES_128
  15046. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  15047. #endif
  15048. #ifdef WOLFSSL_AES_192
  15049. if (ret == 0) {
  15050. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15051. }
  15052. #endif
  15053. #ifdef WOLFSSL_AES_256
  15054. if (ret == 0) {
  15055. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15056. }
  15057. #endif
  15058. /* Pass in bad args. */
  15059. if (ret == 0) {
  15060. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  15061. if (ret == BAD_FUNC_ARG) {
  15062. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  15063. }
  15064. if (ret == BAD_FUNC_ARG) {
  15065. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  15066. }
  15067. if (ret == BAD_FUNC_ARG) {
  15068. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  15069. }
  15070. if (ret == BAD_FUNC_ARG) {
  15071. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  15072. }
  15073. if (ret == BAD_FUNC_ARG) {
  15074. ret = 0;
  15075. } else {
  15076. ret = WOLFSSL_FATAL_ERROR;
  15077. }
  15078. }
  15079. wc_AesFree(&gmac.aes);
  15080. printf(resultFmt, ret == 0 ? passed : failed);
  15081. #endif
  15082. return ret;
  15083. } /* END test_wc_GmacSetKey */
  15084. /*
  15085. * unit test for wc_GmacUpdate
  15086. */
  15087. static int test_wc_GmacUpdate(void)
  15088. {
  15089. int ret = 0;
  15090. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15091. Gmac gmac;
  15092. #ifdef WOLFSSL_AES_128
  15093. const byte key16[] =
  15094. {
  15095. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15096. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15097. };
  15098. #endif
  15099. #ifdef WOLFSSL_AES_192
  15100. byte key24[] =
  15101. {
  15102. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15103. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15104. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15105. };
  15106. #endif
  15107. #ifdef WOLFSSL_AES_256
  15108. byte key32[] =
  15109. {
  15110. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15111. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15112. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15113. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15114. };
  15115. #endif
  15116. #ifdef WOLFSSL_AES_128
  15117. const byte authIn[] =
  15118. {
  15119. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15120. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15121. };
  15122. #endif
  15123. #ifdef WOLFSSL_AES_192
  15124. const byte authIn2[] =
  15125. {
  15126. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15127. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15128. };
  15129. #endif
  15130. const byte authIn3[] =
  15131. {
  15132. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15133. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15134. };
  15135. #ifdef WOLFSSL_AES_128
  15136. const byte tag1[] = /* Known. */
  15137. {
  15138. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15139. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15140. };
  15141. #endif
  15142. #ifdef WOLFSSL_AES_192
  15143. const byte tag2[] = /* Known */
  15144. {
  15145. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15146. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15147. };
  15148. #endif
  15149. const byte tag3[] = /* Known */
  15150. {
  15151. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15152. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15153. };
  15154. #ifdef WOLFSSL_AES_128
  15155. const byte iv[] =
  15156. {
  15157. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15158. 0xe2, 0x8c, 0x8f, 0x16
  15159. };
  15160. #endif
  15161. #ifdef WOLFSSL_AES_192
  15162. const byte iv2[] =
  15163. {
  15164. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15165. 0x7e, 0x1a, 0x6f, 0xbc
  15166. };
  15167. #endif
  15168. const byte iv3[] =
  15169. {
  15170. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15171. 0xc3, 0xfb, 0x6c, 0x8a
  15172. };
  15173. byte tagOut[16];
  15174. byte tagOut2[24];
  15175. byte tagOut3[32];
  15176. /* Init stack variables. */
  15177. XMEMSET(tagOut, 0, sizeof(tagOut));
  15178. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15179. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15180. printf(testingFmt, "wc_GmacUpdate()");
  15181. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15182. if (ret != 0)
  15183. return ret;
  15184. #ifdef WOLFSSL_AES_128
  15185. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  15186. if (ret == 0) {
  15187. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15188. tagOut, sizeof(tag1));
  15189. if (ret == 0) {
  15190. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  15191. }
  15192. wc_AesFree(&gmac.aes);
  15193. }
  15194. #endif
  15195. #ifdef WOLFSSL_AES_192
  15196. if (ret == 0) {
  15197. XMEMSET(&gmac, 0, sizeof(Gmac));
  15198. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15199. }
  15200. if (ret == 0) {
  15201. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  15202. sizeof(authIn2), tagOut2, sizeof(tag2));
  15203. }
  15204. if (ret == 0) {
  15205. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  15206. wc_AesFree(&gmac.aes);
  15207. }
  15208. #endif
  15209. #ifdef WOLFSSL_AES_256
  15210. if (ret == 0) {
  15211. XMEMSET(&gmac, 0, sizeof(Gmac));
  15212. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15213. }
  15214. if (ret == 0) {
  15215. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15216. sizeof(authIn3), tagOut3, sizeof(tag3));
  15217. }
  15218. if (ret == 0) {
  15219. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  15220. }
  15221. #endif
  15222. /*Pass bad args. */
  15223. if (ret == 0) {
  15224. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  15225. sizeof(authIn3), tagOut3, sizeof(tag3));
  15226. if (ret == BAD_FUNC_ARG) {
  15227. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15228. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  15229. }
  15230. if (ret == BAD_FUNC_ARG) {
  15231. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15232. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  15233. }
  15234. if (ret == BAD_FUNC_ARG) {
  15235. ret = 0;
  15236. } else {
  15237. ret = WOLFSSL_FATAL_ERROR;
  15238. }
  15239. }
  15240. wc_AesFree(&gmac.aes);
  15241. printf(resultFmt, ret == 0 ? passed : failed);
  15242. #endif
  15243. return ret;
  15244. } /* END test_wc_GmacUpdate */
  15245. /*
  15246. * testing wc_CamelliaSetKey
  15247. */
  15248. static int test_wc_CamelliaSetKey(void)
  15249. {
  15250. int ret = 0;
  15251. #ifdef HAVE_CAMELLIA
  15252. Camellia camellia;
  15253. /*128-bit key*/
  15254. static const byte key16[] =
  15255. {
  15256. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15257. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15258. };
  15259. /* 192-bit key */
  15260. static const byte key24[] =
  15261. {
  15262. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15263. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15264. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15265. };
  15266. /* 256-bit key */
  15267. static const byte key32[] =
  15268. {
  15269. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15270. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15271. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  15272. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15273. };
  15274. static const byte iv[] =
  15275. {
  15276. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15277. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15278. };
  15279. printf(testingFmt, "wc_CamelliaSetKey()");
  15280. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  15281. if (ret == 0) {
  15282. ret = wc_CamelliaSetKey(&camellia, key16,
  15283. (word32)sizeof(key16), NULL);
  15284. if (ret == 0) {
  15285. ret = wc_CamelliaSetKey(&camellia, key24,
  15286. (word32)sizeof(key24), iv);
  15287. }
  15288. if (ret == 0) {
  15289. ret = wc_CamelliaSetKey(&camellia, key24,
  15290. (word32)sizeof(key24), NULL);
  15291. }
  15292. if (ret == 0) {
  15293. ret = wc_CamelliaSetKey(&camellia, key32,
  15294. (word32)sizeof(key32), iv);
  15295. }
  15296. if (ret == 0) {
  15297. ret = wc_CamelliaSetKey(&camellia, key32,
  15298. (word32)sizeof(key32), NULL);
  15299. }
  15300. }
  15301. /* Bad args. */
  15302. if (ret == 0) {
  15303. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  15304. if (ret != BAD_FUNC_ARG) {
  15305. ret = WOLFSSL_FATAL_ERROR;
  15306. } else {
  15307. ret = 0;
  15308. }
  15309. } /* END bad args. */
  15310. #endif
  15311. return ret;
  15312. } /* END test_wc_CammeliaSetKey */
  15313. /*
  15314. * Testing wc_CamelliaSetIV()
  15315. */
  15316. static int test_wc_CamelliaSetIV(void)
  15317. {
  15318. int ret = 0;
  15319. #ifdef HAVE_CAMELLIA
  15320. Camellia camellia;
  15321. static const byte iv[] =
  15322. {
  15323. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15324. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15325. };
  15326. printf(testingFmt, "wc_CamelliaSetIV()");
  15327. ret = wc_CamelliaSetIV(&camellia, iv);
  15328. if (ret == 0) {
  15329. ret = wc_CamelliaSetIV(&camellia, NULL);
  15330. }
  15331. /* Bad args. */
  15332. if (ret == 0) {
  15333. ret = wc_CamelliaSetIV(NULL, NULL);
  15334. if (ret != BAD_FUNC_ARG) {
  15335. ret = WOLFSSL_FATAL_ERROR;
  15336. } else {
  15337. ret = 0;
  15338. }
  15339. }
  15340. printf(resultFmt, ret == 0 ? passed : failed);
  15341. #endif
  15342. return ret;
  15343. } /*END test_wc_CamelliaSetIV*/
  15344. /*
  15345. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  15346. */
  15347. static int test_wc_CamelliaEncryptDecryptDirect(void)
  15348. {
  15349. int ret = 0;
  15350. #ifdef HAVE_CAMELLIA
  15351. Camellia camellia;
  15352. static const byte key24[] =
  15353. {
  15354. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15355. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15356. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15357. };
  15358. static const byte iv[] =
  15359. {
  15360. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15361. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15362. };
  15363. static const byte plainT[] =
  15364. {
  15365. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15366. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15367. };
  15368. byte enc[sizeof(plainT)];
  15369. byte dec[sizeof(enc)];
  15370. int camE = WOLFSSL_FATAL_ERROR;
  15371. int camD = WOLFSSL_FATAL_ERROR;
  15372. /*Init stack variables.*/
  15373. XMEMSET(enc, 0, 16);
  15374. XMEMSET(enc, 0, 16);
  15375. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  15376. if (ret == 0) {
  15377. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  15378. if (ret == 0) {
  15379. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  15380. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15381. ret = WOLFSSL_FATAL_ERROR;
  15382. }
  15383. }
  15384. }
  15385. printf(testingFmt, "wc_CamelliaEncryptDirect()");
  15386. /* Pass bad args. */
  15387. if (ret == 0) {
  15388. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  15389. if (camE == BAD_FUNC_ARG) {
  15390. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  15391. }
  15392. if (camE == BAD_FUNC_ARG) {
  15393. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  15394. }
  15395. if (camE == BAD_FUNC_ARG) {
  15396. camE = 0;
  15397. } else {
  15398. camE = WOLFSSL_FATAL_ERROR;
  15399. }
  15400. }
  15401. printf(resultFmt, camE == 0 ? passed : failed);
  15402. if (camE != 0) {
  15403. return camE;
  15404. }
  15405. printf(testingFmt, "wc_CamelliaDecryptDirect()");
  15406. if (ret == 0) {
  15407. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  15408. if (camD == BAD_FUNC_ARG) {
  15409. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  15410. }
  15411. if (camD == BAD_FUNC_ARG) {
  15412. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  15413. }
  15414. if (camD == BAD_FUNC_ARG) {
  15415. camD = 0;
  15416. } else {
  15417. camD = WOLFSSL_FATAL_ERROR;
  15418. }
  15419. }
  15420. printf(resultFmt, camD == 0 ? passed : failed);
  15421. if (camD != 0) {
  15422. return camD;
  15423. }
  15424. #endif
  15425. return ret;
  15426. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15427. /*
  15428. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15429. */
  15430. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  15431. {
  15432. int ret = 0;
  15433. #ifdef HAVE_CAMELLIA
  15434. Camellia camellia;
  15435. static const byte key24[] =
  15436. {
  15437. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15438. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15439. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15440. };
  15441. static const byte plainT[] =
  15442. {
  15443. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15444. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15445. };
  15446. byte enc[CAMELLIA_BLOCK_SIZE];
  15447. byte dec[CAMELLIA_BLOCK_SIZE];
  15448. int camCbcE = WOLFSSL_FATAL_ERROR;
  15449. int camCbcD = WOLFSSL_FATAL_ERROR;
  15450. /* Init stack variables. */
  15451. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15452. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15453. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15454. if (ret == 0) {
  15455. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15456. if (ret != 0) {
  15457. ret = WOLFSSL_FATAL_ERROR;
  15458. }
  15459. }
  15460. if (ret == 0) {
  15461. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15462. if (ret == 0) {
  15463. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  15464. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15465. ret = WOLFSSL_FATAL_ERROR;
  15466. }
  15467. }
  15468. }
  15469. printf(testingFmt, "wc_CamelliaCbcEncrypt");
  15470. /* Pass in bad args. */
  15471. if (ret == 0) {
  15472. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15473. if (camCbcE == BAD_FUNC_ARG) {
  15474. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15475. CAMELLIA_BLOCK_SIZE);
  15476. }
  15477. if (camCbcE == BAD_FUNC_ARG) {
  15478. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15479. CAMELLIA_BLOCK_SIZE);
  15480. }
  15481. if (camCbcE == BAD_FUNC_ARG) {
  15482. camCbcE = 0;
  15483. } else {
  15484. camCbcE = WOLFSSL_FATAL_ERROR;
  15485. }
  15486. }
  15487. printf(resultFmt, camCbcE == 0 ? passed : failed);
  15488. if (camCbcE != 0) {
  15489. return camCbcE;
  15490. }
  15491. printf(testingFmt, "wc_CamelliaCbcDecrypt()");
  15492. if (ret == 0) {
  15493. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  15494. if (camCbcD == BAD_FUNC_ARG) {
  15495. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15496. CAMELLIA_BLOCK_SIZE);
  15497. }
  15498. if (camCbcD == BAD_FUNC_ARG) {
  15499. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15500. CAMELLIA_BLOCK_SIZE);
  15501. }
  15502. if (camCbcD == BAD_FUNC_ARG) {
  15503. camCbcD = 0;
  15504. } else {
  15505. camCbcD = WOLFSSL_FATAL_ERROR;
  15506. }
  15507. } /* END bad args. */
  15508. printf(resultFmt, camCbcD == 0 ? passed : failed);
  15509. if (camCbcD != 0) {
  15510. return camCbcD;
  15511. }
  15512. #endif
  15513. return ret;
  15514. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  15515. /*
  15516. * Testing wc_Arc4SetKey()
  15517. */
  15518. static int test_wc_Arc4SetKey(void)
  15519. {
  15520. int ret = 0;
  15521. #ifndef NO_RC4
  15522. Arc4 arc;
  15523. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15524. int keyLen = 8;
  15525. printf(testingFmt, "wc_Arch4SetKey()");
  15526. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  15527. /* Test bad args. */
  15528. if (ret == 0) {
  15529. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  15530. if (ret == BAD_FUNC_ARG)
  15531. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  15532. if (ret == BAD_FUNC_ARG)
  15533. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  15534. if (ret == BAD_FUNC_ARG)
  15535. ret = WOLFSSL_ERROR_NONE;
  15536. else
  15537. ret = WOLFSSL_FATAL_ERROR;
  15538. } /* END test bad args. */
  15539. printf(resultFmt, ret == 0 ? passed : failed);
  15540. #endif
  15541. return ret;
  15542. } /* END test_wc_Arc4SetKey */
  15543. /*
  15544. * Testing wc_Arc4Process for ENC/DEC.
  15545. */
  15546. static int test_wc_Arc4Process(void)
  15547. {
  15548. int ret = 0;
  15549. #ifndef NO_RC4
  15550. Arc4 enc, dec;
  15551. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15552. int keyLen = 8;
  15553. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15554. byte cipher[8];
  15555. byte plain[8];
  15556. /* Init stack variables */
  15557. XMEMSET(cipher, 0, sizeof(cipher));
  15558. XMEMSET(plain, 0, sizeof(plain));
  15559. /* Use for async. */
  15560. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  15561. if (ret == 0) {
  15562. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  15563. }
  15564. printf(testingFmt, "wc_Arc4Process()");
  15565. if (ret == 0) {
  15566. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  15567. }
  15568. if (ret == 0) {
  15569. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  15570. }
  15571. if (ret == 0) {
  15572. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  15573. }
  15574. if (ret == 0) {
  15575. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  15576. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  15577. ret = WOLFSSL_FATAL_ERROR;
  15578. } else {
  15579. ret = 0;
  15580. }
  15581. }
  15582. /* Bad args. */
  15583. if (ret == 0) {
  15584. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  15585. if (ret == BAD_FUNC_ARG) {
  15586. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  15587. }
  15588. if (ret == BAD_FUNC_ARG) {
  15589. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  15590. }
  15591. if (ret == BAD_FUNC_ARG) {
  15592. ret = 0;
  15593. } else {
  15594. ret = WOLFSSL_FATAL_ERROR;
  15595. }
  15596. }
  15597. printf(resultFmt, ret == 0 ? passed : failed);
  15598. wc_Arc4Free(&enc);
  15599. wc_Arc4Free(&dec);
  15600. #endif
  15601. return ret;
  15602. }/* END test_wc_Arc4Process */
  15603. /*
  15604. * Testing wc_Init RsaKey()
  15605. */
  15606. static int test_wc_InitRsaKey(void)
  15607. {
  15608. int ret = 0;
  15609. #ifndef NO_RSA
  15610. RsaKey key;
  15611. printf(testingFmt, "wc_InitRsaKey()");
  15612. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15613. /* Test bad args. */
  15614. if (ret == 0) {
  15615. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  15616. #ifndef HAVE_USER_RSA
  15617. if (ret == BAD_FUNC_ARG) {
  15618. ret = 0;
  15619. } else {
  15620. #else
  15621. if (ret == USER_CRYPTO_ERROR) {
  15622. ret = 0;
  15623. } else {
  15624. #endif
  15625. ret = WOLFSSL_FATAL_ERROR;
  15626. }
  15627. } /* end if */
  15628. if (wc_FreeRsaKey(&key) || ret != 0) {
  15629. ret = WOLFSSL_FATAL_ERROR;
  15630. }
  15631. printf(resultFmt, ret == 0 ? passed : failed);
  15632. #endif
  15633. return ret;
  15634. } /* END test_wc_InitRsaKey */
  15635. /*
  15636. * Testing wc_RsaPrivateKeyDecode()
  15637. */
  15638. static int test_wc_RsaPrivateKeyDecode(void)
  15639. {
  15640. int ret = 0;
  15641. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15642. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15643. RsaKey key;
  15644. byte* tmp;
  15645. word32 idx = 0;
  15646. int bytes = 0;
  15647. printf(testingFmt, "wc_RsaPrivateKeyDecode()");
  15648. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15649. if (tmp == NULL) {
  15650. ret = WOLFSSL_FATAL_ERROR;
  15651. }
  15652. if (ret == 0) {
  15653. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15654. }
  15655. if (ret == 0) {
  15656. #ifdef USE_CERT_BUFFERS_1024
  15657. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  15658. bytes = sizeof_client_key_der_1024;
  15659. #else
  15660. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  15661. bytes = sizeof_client_key_der_2048;
  15662. #endif /* Use cert buffers. */
  15663. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  15664. }
  15665. #ifndef HAVE_USER_RSA
  15666. /* Test bad args. */
  15667. if (ret == 0) {
  15668. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  15669. if (ret == BAD_FUNC_ARG) {
  15670. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  15671. }
  15672. if (ret == BAD_FUNC_ARG) {
  15673. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15674. }
  15675. if (ret == BAD_FUNC_ARG) {
  15676. ret = 0;
  15677. } else {
  15678. ret = WOLFSSL_FATAL_ERROR;
  15679. }
  15680. }
  15681. #else
  15682. /* Test bad args. User RSA. */
  15683. if (ret == 0) {
  15684. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  15685. if (ret == USER_CRYPTO_ERROR) {
  15686. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  15687. }
  15688. if (ret == USER_CRYPTO_ERROR) {
  15689. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15690. }
  15691. if (ret == USER_CRYPTO_ERROR) {
  15692. ret = 0;
  15693. } else {
  15694. ret = WOLFSSL_FATAL_ERROR;
  15695. }
  15696. }
  15697. #endif
  15698. if (tmp != NULL) {
  15699. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15700. }
  15701. if (wc_FreeRsaKey(&key) || ret != 0) {
  15702. ret = WOLFSSL_FATAL_ERROR;
  15703. }
  15704. printf(resultFmt, ret == 0 ? passed : failed);
  15705. #endif
  15706. return ret;
  15707. } /* END test_wc_RsaPrivateKeyDecode */
  15708. /*
  15709. * Testing wc_RsaPublicKeyDecode()
  15710. */
  15711. static int test_wc_RsaPublicKeyDecode(void)
  15712. {
  15713. int ret = 0;
  15714. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15715. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15716. RsaKey keyPub;
  15717. byte* tmp;
  15718. word32 idx = 0;
  15719. int bytes = 0;
  15720. word32 keySz = 0;
  15721. word32 tstKeySz = 0;
  15722. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  15723. XFILE f;
  15724. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  15725. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  15726. byte buf[4096];
  15727. #endif
  15728. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15729. if (tmp == NULL) {
  15730. ret = WOLFSSL_FATAL_ERROR;
  15731. }
  15732. if (ret == 0) {
  15733. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  15734. }
  15735. if (ret == 0) {
  15736. #ifdef USE_CERT_BUFFERS_1024
  15737. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  15738. bytes = sizeof_client_keypub_der_1024;
  15739. keySz = 1024;
  15740. #else
  15741. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  15742. bytes = sizeof_client_keypub_der_2048;
  15743. keySz = 2048;
  15744. #endif
  15745. printf(testingFmt, "wc_RsaPublicKeyDecode()");
  15746. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  15747. }
  15748. #ifndef HAVE_USER_RSA
  15749. /* Pass in bad args. */
  15750. if (ret == 0) {
  15751. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  15752. if (ret == BAD_FUNC_ARG) {
  15753. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  15754. }
  15755. if (ret == BAD_FUNC_ARG) {
  15756. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15757. }
  15758. if (ret == BAD_FUNC_ARG) {
  15759. ret = 0;
  15760. } else {
  15761. ret = WOLFSSL_FATAL_ERROR;
  15762. }
  15763. }
  15764. #else
  15765. /* Pass in bad args. */
  15766. if (ret == 0) {
  15767. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  15768. if (ret == USER_CRYPTO_ERROR) {
  15769. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  15770. }
  15771. if (ret == USER_CRYPTO_ERROR) {
  15772. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15773. }
  15774. if (ret == USER_CRYPTO_ERROR) {
  15775. ret = 0;
  15776. } else {
  15777. ret = WOLFSSL_FATAL_ERROR;
  15778. }
  15779. }
  15780. #endif
  15781. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  15782. ret = WOLFSSL_FATAL_ERROR;
  15783. }
  15784. if (ret == 0) {
  15785. /* Test for getting modulus key size */
  15786. idx = 0;
  15787. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  15788. &tstKeySz, NULL, NULL);
  15789. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  15790. }
  15791. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  15792. f = XFOPEN(rsaPssPubKey, "rb");
  15793. AssertTrue((f != XBADFILE));
  15794. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  15795. XFCLOSE(f);
  15796. idx = 0;
  15797. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  15798. NULL), 0);
  15799. f = XFOPEN(rsaPssPubKeyNoParams, "rb");
  15800. AssertTrue((f != XBADFILE));
  15801. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  15802. XFCLOSE(f);
  15803. idx = 0;
  15804. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  15805. NULL), 0);
  15806. #endif
  15807. if (tmp != NULL) {
  15808. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15809. }
  15810. printf(resultFmt, ret == 0 ? passed : failed);
  15811. #endif
  15812. return ret;
  15813. } /* END test_wc_RsaPublicKeyDecode */
  15814. /*
  15815. * Testing wc_RsaPublicKeyDecodeRaw()
  15816. */
  15817. static int test_wc_RsaPublicKeyDecodeRaw(void)
  15818. {
  15819. int ret = 0;
  15820. #if !defined(NO_RSA)
  15821. RsaKey key;
  15822. const byte n = 0x23;
  15823. const byte e = 0x03;
  15824. int nSz = sizeof(n);
  15825. int eSz = sizeof(e);
  15826. printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
  15827. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15828. if (ret == 0) {
  15829. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  15830. }
  15831. #ifndef HAVE_USER_RSA
  15832. /* Pass in bad args. */
  15833. if (ret == 0) {
  15834. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  15835. if (ret == BAD_FUNC_ARG) {
  15836. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  15837. }
  15838. if (ret == BAD_FUNC_ARG) {
  15839. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  15840. }
  15841. if (ret == BAD_FUNC_ARG) {
  15842. ret = 0;
  15843. } else {
  15844. ret = WOLFSSL_FATAL_ERROR;
  15845. }
  15846. }
  15847. #else
  15848. /* Pass in bad args. User RSA. */
  15849. if (ret == 0) {
  15850. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  15851. if (ret == USER_CRYPTO_ERROR) {
  15852. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  15853. }
  15854. if (ret == USER_CRYPTO_ERROR) {
  15855. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  15856. }
  15857. if (ret == USER_CRYPTO_ERROR) {
  15858. ret = 0;
  15859. } else {
  15860. ret = WOLFSSL_FATAL_ERROR;
  15861. }
  15862. }
  15863. #endif
  15864. if (wc_FreeRsaKey(&key) || ret != 0) {
  15865. ret = WOLFSSL_FATAL_ERROR;
  15866. }
  15867. printf(resultFmt, ret == 0 ? passed : failed);
  15868. #endif
  15869. return ret;
  15870. } /* END test_wc_RsaPublicKeyDecodeRaw */
  15871. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  15872. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  15873. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  15874. * trying until it gets a probable prime. */
  15875. #ifdef HAVE_FIPS
  15876. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  15877. {
  15878. int ret;
  15879. for (;;) {
  15880. ret = wc_MakeRsaKey(key, size, e, rng);
  15881. if (ret != PRIME_GEN_E) break;
  15882. printf("MakeRsaKey couldn't find prime; trying again.\n");
  15883. }
  15884. return ret;
  15885. }
  15886. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  15887. #else
  15888. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  15889. #endif
  15890. #endif
  15891. /*
  15892. * Testing wc_MakeRsaKey()
  15893. */
  15894. static int test_wc_MakeRsaKey(void)
  15895. {
  15896. int ret = 0;
  15897. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15898. RsaKey genKey;
  15899. WC_RNG rng;
  15900. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  15901. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  15902. int bits = 1024;
  15903. #else
  15904. int bits = 2048;
  15905. #endif
  15906. printf(testingFmt, "wc_MakeRsaKey()");
  15907. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  15908. if (ret == 0) {
  15909. ret = wc_InitRng(&rng);
  15910. if (ret == 0) {
  15911. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  15912. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  15913. ret = WOLFSSL_FATAL_ERROR;
  15914. }
  15915. }
  15916. }
  15917. #ifndef HAVE_USER_RSA
  15918. /* Test bad args. */
  15919. if (ret == 0) {
  15920. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  15921. if (ret == BAD_FUNC_ARG) {
  15922. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  15923. }
  15924. if (ret == BAD_FUNC_ARG) {
  15925. /* e < 3 */
  15926. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  15927. }
  15928. if (ret == BAD_FUNC_ARG) {
  15929. /* e & 1 == 0 */
  15930. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  15931. }
  15932. if (ret == BAD_FUNC_ARG) {
  15933. ret = 0;
  15934. } else {
  15935. ret = WOLFSSL_FATAL_ERROR;
  15936. }
  15937. }
  15938. #else
  15939. /* Test bad args. */
  15940. if (ret == 0) {
  15941. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  15942. if (ret == USER_CRYPTO_ERROR) {
  15943. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  15944. }
  15945. if (ret == USER_CRYPTO_ERROR) {
  15946. /* e < 3 */
  15947. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  15948. }
  15949. if (ret == USER_CRYPTO_ERROR) {
  15950. /* e & 1 == 0 */
  15951. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  15952. }
  15953. if (ret == USER_CRYPTO_ERROR) {
  15954. ret = 0;
  15955. } else {
  15956. ret = WOLFSSL_FATAL_ERROR;
  15957. }
  15958. }
  15959. #endif
  15960. if (wc_FreeRng(&rng) || ret != 0) {
  15961. ret = WOLFSSL_FATAL_ERROR;
  15962. }
  15963. printf(resultFmt, ret == 0 ? passed : failed);
  15964. #endif
  15965. return ret;
  15966. } /* END test_wc_MakeRsaKey */
  15967. /*
  15968. * Test the bounds checking on the cipher text versus the key modulus.
  15969. * 1. Make a new RSA key.
  15970. * 2. Set c to 1.
  15971. * 3. Decrypt c into k. (error)
  15972. * 4. Copy the key modulus to c and sub 1 from the copy.
  15973. * 5. Decrypt c into k. (error)
  15974. * Valid bounds test cases are covered by all the other RSA tests.
  15975. */
  15976. static int test_RsaDecryptBoundsCheck(void)
  15977. {
  15978. int ret = 0;
  15979. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  15980. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  15981. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  15982. RsaKey key;
  15983. byte flatC[256];
  15984. word32 flatCSz;
  15985. byte out[256];
  15986. word32 outSz = sizeof(out);
  15987. WC_RNG rng;
  15988. printf(testingFmt, "RSA decrypt bounds check");
  15989. XMEMSET(&rng, 0, sizeof(rng));
  15990. ret = wc_InitRng(&rng);
  15991. if (ret == 0)
  15992. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15993. if (ret == 0) {
  15994. const byte* derKey;
  15995. word32 derKeySz;
  15996. word32 idx = 0;
  15997. #ifdef USE_CERT_BUFFERS_1024
  15998. derKey = server_key_der_1024;
  15999. derKeySz = (word32)sizeof_server_key_der_1024;
  16000. flatCSz = 128;
  16001. #else
  16002. derKey = server_key_der_2048;
  16003. derKeySz = (word32)sizeof_server_key_der_2048;
  16004. flatCSz = 256;
  16005. #endif
  16006. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  16007. }
  16008. if (ret == 0) {
  16009. XMEMSET(flatC, 0, flatCSz);
  16010. flatC[flatCSz-1] = 1;
  16011. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16012. RSA_PRIVATE_DECRYPT, &rng);
  16013. if (ret == RSA_OUT_OF_RANGE_E) {
  16014. mp_int c;
  16015. mp_init_copy(&c, &key.n);
  16016. mp_sub_d(&c, 1, &c);
  16017. mp_to_unsigned_bin(&c, flatC);
  16018. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16019. RSA_PRIVATE_DECRYPT, NULL);
  16020. mp_clear(&c);
  16021. }
  16022. if (ret == RSA_OUT_OF_RANGE_E)
  16023. ret = 0;
  16024. else
  16025. ret = WOLFSSL_FATAL_ERROR;
  16026. }
  16027. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  16028. ret = WOLFSSL_FATAL_ERROR;
  16029. printf(resultFmt, ret == 0 ? passed : failed);
  16030. #endif
  16031. return ret;
  16032. } /* END test_wc_RsaDecryptBoundsCheck */
  16033. /*
  16034. * Testing wc_SetKeyUsage()
  16035. */
  16036. static int test_wc_SetKeyUsage(void)
  16037. {
  16038. int ret = 0;
  16039. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16040. Cert myCert;
  16041. ret = wc_InitCert(&myCert);
  16042. printf(testingFmt, "wc_SetKeyUsage()");
  16043. if (ret == 0) {
  16044. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  16045. if (ret == 0) {
  16046. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  16047. }
  16048. if (ret == 0) {
  16049. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  16050. }
  16051. if (ret == 0) {
  16052. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  16053. }
  16054. if (ret == 0) {
  16055. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  16056. }
  16057. }
  16058. /* Test bad args. */
  16059. if (ret == 0) {
  16060. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  16061. if (ret == BAD_FUNC_ARG) {
  16062. ret = wc_SetKeyUsage(&myCert, NULL);
  16063. }
  16064. if (ret == BAD_FUNC_ARG) {
  16065. ret = wc_SetKeyUsage(&myCert, "");
  16066. }
  16067. if (ret == KEYUSAGE_E) {
  16068. ret = wc_SetKeyUsage(&myCert, ",");
  16069. }
  16070. if (ret == KEYUSAGE_E) {
  16071. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  16072. }
  16073. if (ret == KEYUSAGE_E) {
  16074. ret = 0;
  16075. } else {
  16076. ret = WOLFSSL_FATAL_ERROR;
  16077. }
  16078. }
  16079. printf(resultFmt, ret == 0 ? passed : failed);
  16080. #endif
  16081. return ret;
  16082. } /* END test_wc_SetKeyUsage */
  16083. /*
  16084. * Testing wc_CheckProbablePrime()
  16085. */
  16086. static int test_wc_CheckProbablePrime(void)
  16087. {
  16088. int ret = 0;
  16089. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16090. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16091. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16092. RsaKey key;
  16093. WC_RNG rng;
  16094. byte e[3];
  16095. word32 eSz = (word32)sizeof(e);
  16096. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16097. word32 nSz = (word32)sizeof(n);
  16098. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16099. word32 dSz = (word32)sizeof(d);
  16100. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16101. word32 pSz = (word32)sizeof(p);
  16102. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16103. word32 qSz = (word32)sizeof(q);
  16104. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16105. int* isPrime;
  16106. int test[5];
  16107. isPrime = test;
  16108. printf(testingFmt, "wc_CheckProbablePrime()");
  16109. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16110. if (ret == 0) {
  16111. ret = wc_InitRng(&rng);
  16112. }
  16113. if (ret == 0) {
  16114. ret = wc_RsaSetRNG(&key, &rng);
  16115. }
  16116. if (ret == 0) {
  16117. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  16118. }
  16119. if (ret == 0) {
  16120. PRIVATE_KEY_UNLOCK();
  16121. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  16122. p, &pSz, q, &qSz);
  16123. PRIVATE_KEY_LOCK();
  16124. }
  16125. /* Bad cases */
  16126. if (ret == 0) {
  16127. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  16128. nlen, isPrime);
  16129. if (ret == BAD_FUNC_ARG) {
  16130. ret = 0;
  16131. }
  16132. }
  16133. if (ret == 0) {
  16134. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  16135. nlen, isPrime);
  16136. if (ret == BAD_FUNC_ARG) {
  16137. ret = 0;
  16138. }
  16139. }
  16140. if (ret == 0) {
  16141. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  16142. nlen, isPrime);
  16143. if (ret == BAD_FUNC_ARG) {
  16144. ret = 0;
  16145. }
  16146. }
  16147. if (ret == 0) {
  16148. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  16149. nlen, isPrime);
  16150. if (ret == BAD_FUNC_ARG) {
  16151. ret = 0;
  16152. }
  16153. }
  16154. if (ret == 0) {
  16155. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  16156. nlen, isPrime);
  16157. if (ret == BAD_FUNC_ARG) {
  16158. ret = 0;
  16159. }
  16160. }
  16161. if (ret == 0) {
  16162. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  16163. nlen, isPrime);
  16164. if (ret == BAD_FUNC_ARG) {
  16165. ret = 0;
  16166. }
  16167. }
  16168. if (ret == 0) {
  16169. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  16170. nlen, isPrime);
  16171. if (ret == BAD_FUNC_ARG) {
  16172. ret = 0;
  16173. }
  16174. }
  16175. /* Good case */
  16176. if (ret == 0) {
  16177. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  16178. nlen, isPrime);
  16179. }
  16180. wc_FreeRsaKey(&key);
  16181. wc_FreeRng(&rng);
  16182. printf(resultFmt, ret == 0 ? passed : failed);
  16183. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16184. #endif
  16185. return ret;
  16186. } /* END test_wc_CheckProbablePrime */
  16187. /*
  16188. * Testing wc_RsaPSS_Verify()
  16189. */
  16190. static int test_wc_RsaPSS_Verify(void)
  16191. {
  16192. int ret = 0;
  16193. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16194. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16195. RsaKey key;
  16196. WC_RNG rng;
  16197. int sz = 256;
  16198. byte* pt;
  16199. const char* szMessage = "This is the string to be signed";
  16200. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16201. unsigned char pDecrypted[2048/8];
  16202. word32 outLen = sizeof(pDecrypted);
  16203. pt = pDecrypted;
  16204. printf(testingFmt, "wc_RsaPSS_Verify()");
  16205. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16206. if (ret == 0) {
  16207. ret = wc_InitRng(&rng);
  16208. }
  16209. if (ret == 0) {
  16210. ret = wc_RsaSetRNG(&key, &rng);
  16211. }
  16212. if (ret == 0) {
  16213. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16214. }
  16215. if (ret == 0) {
  16216. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  16217. pSignature, sizeof(pSignature),
  16218. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16219. if (ret > 0 ){
  16220. sz = ret;
  16221. ret = 0;
  16222. }
  16223. }
  16224. /* Bad cases */
  16225. if (ret == 0) {
  16226. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16227. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16228. if (ret == BAD_FUNC_ARG) {
  16229. ret = 0;
  16230. }
  16231. }
  16232. if (ret == 0) {
  16233. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16234. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16235. if (ret == BAD_FUNC_ARG) {
  16236. ret = 0;
  16237. }
  16238. }
  16239. if (ret == 0) {
  16240. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16241. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16242. if (ret == BAD_FUNC_ARG) {
  16243. ret = 0;
  16244. }
  16245. }
  16246. if (ret == 0) {
  16247. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16248. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16249. if (ret == BAD_FUNC_ARG) {
  16250. ret = 0;
  16251. }
  16252. }
  16253. /* Good case */
  16254. if (ret == 0) {
  16255. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16256. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16257. if (ret > 0) {
  16258. ret = 0;
  16259. }
  16260. }
  16261. wc_FreeRsaKey(&key);
  16262. wc_FreeRng(&rng);
  16263. printf(resultFmt, ret == 0 ? passed : failed);
  16264. #endif
  16265. return ret;
  16266. } /* END test_wc_RsaPSS_Verify */
  16267. /*
  16268. * Testing wc_RsaPSS_VerifyCheck()
  16269. */
  16270. static int test_wc_RsaPSS_VerifyCheck(void)
  16271. {
  16272. int ret = 0;
  16273. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16274. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16275. RsaKey key;
  16276. WC_RNG rng;
  16277. int sz = 256; /* 2048/8 */
  16278. byte* pt;
  16279. byte digest[32];
  16280. word32 digestSz = sizeof(digest);
  16281. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16282. word32 pSignatureSz = sizeof(pSignature);
  16283. unsigned char pDecrypted[2048/8];
  16284. word32 outLen = sizeof(pDecrypted);
  16285. pt = pDecrypted;
  16286. printf(testingFmt, "wc_RsaPSS_VerifyCheck()");
  16287. XMEMSET(digest, 0, sizeof(digest));
  16288. XMEMSET(pSignature, 0, sizeof(pSignature));
  16289. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16290. if (ret == 0) {
  16291. ret = wc_InitRng(&rng);
  16292. }
  16293. if (ret == 0) {
  16294. ret = wc_RsaSetRNG(&key, &rng);
  16295. }
  16296. if (ret == 0) {
  16297. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16298. }
  16299. if (ret == 0) {
  16300. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  16301. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  16302. }
  16303. if (ret == 0) {
  16304. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16305. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16306. if (ret > 0 ){
  16307. sz = ret;
  16308. ret = 0;
  16309. }
  16310. }
  16311. /* Bad cases */
  16312. if (ret == 0) {
  16313. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  16314. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16315. if (ret == BAD_FUNC_ARG) {
  16316. ret = 0;
  16317. }
  16318. }
  16319. if (ret == 0) {
  16320. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  16321. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16322. if (ret == BAD_FUNC_ARG) {
  16323. ret = 0;
  16324. }
  16325. }
  16326. if (ret == 0) {
  16327. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  16328. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16329. if (ret == BAD_FUNC_ARG) {
  16330. ret = 0;
  16331. }
  16332. }
  16333. if (ret == 0) {
  16334. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  16335. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16336. if (ret == BAD_FUNC_ARG) {
  16337. ret = 0;
  16338. }
  16339. }
  16340. /* Good case */
  16341. if (ret == 0) {
  16342. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  16343. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16344. if (ret > 0) {
  16345. ret = 0;
  16346. }
  16347. }
  16348. wc_FreeRsaKey(&key);
  16349. wc_FreeRng(&rng);
  16350. printf(resultFmt, ret == 0 ? passed : failed);
  16351. #endif
  16352. return ret;
  16353. } /* END test_wc_RsaPSS_VerifyCheck */
  16354. /*
  16355. * Testing wc_RsaPSS_VerifyCheckInline()
  16356. */
  16357. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16358. {
  16359. int ret = 0;
  16360. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16361. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16362. RsaKey key;
  16363. WC_RNG rng;
  16364. int sz = 256;
  16365. byte* pt;
  16366. byte digest[32];
  16367. word32 digestSz = sizeof(digest);
  16368. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16369. unsigned char pDecrypted[2048/8];
  16370. pt = pDecrypted;
  16371. printf(testingFmt, "wc_RsaPSS_VerifyCheckInline()");
  16372. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16373. XMEMSET(digest, 0, sizeof(digest));
  16374. XMEMSET(pSignature, 0, sizeof(pSignature));
  16375. if (ret == 0) {
  16376. ret = wc_InitRng(&rng);
  16377. }
  16378. if (ret == 0) {
  16379. ret = wc_RsaSetRNG(&key, &rng);
  16380. }
  16381. if (ret == 0) {
  16382. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16383. }
  16384. if (ret == 0) {
  16385. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  16386. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  16387. }
  16388. if (ret == 0) {
  16389. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  16390. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16391. if (ret > 0 ){
  16392. sz = ret;
  16393. ret = 0;
  16394. }
  16395. }
  16396. /* Bad Cases */
  16397. if (ret == 0) {
  16398. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  16399. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16400. if (ret == BAD_FUNC_ARG) {
  16401. ret = 0;
  16402. }
  16403. }
  16404. if (ret == 0) {
  16405. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  16406. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16407. if (ret == BAD_FUNC_ARG) {
  16408. ret = 0;
  16409. }
  16410. }
  16411. if (ret == 0) {
  16412. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  16413. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16414. if (ret == BAD_FUNC_ARG) {
  16415. ret = 0;
  16416. }
  16417. }
  16418. if (ret == 0) {
  16419. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16420. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  16421. if (ret == BAD_FUNC_ARG) {
  16422. ret = 0;
  16423. }
  16424. }
  16425. /* Good case */
  16426. if (ret == 0) {
  16427. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16428. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16429. if (ret > 0) {
  16430. ret = 0;
  16431. }
  16432. }
  16433. wc_FreeRsaKey(&key);
  16434. wc_FreeRng(&rng);
  16435. printf(resultFmt, ret == 0 ? passed : failed);
  16436. #endif
  16437. return ret;
  16438. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16439. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16440. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16441. {
  16442. (void)flag;
  16443. (void)type;
  16444. (void)file;
  16445. (void)line;
  16446. }
  16447. #endif
  16448. /*
  16449. * Testing wc_LockMutex_ex
  16450. */
  16451. static int test_wc_LockMutex_ex(void)
  16452. {
  16453. int ret = 0;
  16454. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16455. int flag = CRYPTO_LOCK;
  16456. int type = 0;
  16457. const char* file = "./test-LockMutex_ex.txt";
  16458. int line = 0;
  16459. printf(testingFmt, "wc_LockMutex_ex()");
  16460. /*without SetMutexCb*/
  16461. ret = wc_LockMutex_ex(flag, type, file, line);
  16462. if (ret == BAD_STATE_E) {
  16463. ret = 0;
  16464. }
  16465. /*with SetMutexCb*/
  16466. if (ret == 0) {
  16467. ret = wc_SetMutexCb(sample_mutex_cb);
  16468. if (ret == 0) {
  16469. ret = wc_LockMutex_ex(flag, type, file, line);
  16470. }
  16471. }
  16472. printf(resultFmt, ret == 0 ? passed : failed);
  16473. #endif
  16474. return ret;
  16475. }/*End test_wc_LockMutex_ex*/
  16476. /*
  16477. * Testing wc_SetMutexCb
  16478. */
  16479. static int test_wc_SetMutexCb(void)
  16480. {
  16481. int ret = 0;
  16482. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16483. printf(testingFmt, "wc_SetMutexCb()");
  16484. ret = wc_SetMutexCb(sample_mutex_cb);
  16485. printf(resultFmt, ret == 0 ? passed : failed);
  16486. #endif
  16487. return ret;
  16488. }/*End test_wc_SetMutexCb*/
  16489. /*
  16490. * Testing wc_RsaKeyToDer()
  16491. */
  16492. static int test_wc_RsaKeyToDer(void)
  16493. {
  16494. int ret = 0;
  16495. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16496. RsaKey genKey;
  16497. WC_RNG rng;
  16498. byte* der;
  16499. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16500. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16501. int bits = 1024;
  16502. word32 derSz = 611;
  16503. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16504. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16505. #else
  16506. int bits = 2048;
  16507. word32 derSz = 1196;
  16508. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16509. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16510. #endif
  16511. XMEMSET(&rng, 0, sizeof(rng));
  16512. XMEMSET(&genKey, 0, sizeof(genKey));
  16513. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16514. if (der == NULL) {
  16515. ret = WOLFSSL_FATAL_ERROR;
  16516. }
  16517. /* Init structures. */
  16518. if (ret == 0) {
  16519. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  16520. }
  16521. if (ret == 0) {
  16522. ret = wc_InitRng(&rng);
  16523. }
  16524. /* Make key. */
  16525. if (ret == 0) {
  16526. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  16527. if (ret != 0) {
  16528. ret = WOLFSSL_FATAL_ERROR;
  16529. }
  16530. }
  16531. printf(testingFmt, "wc_RsaKeyToDer()");
  16532. if (ret == 0) {
  16533. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  16534. if (ret > 0) {
  16535. ret = 0;
  16536. } else {
  16537. ret = WOLFSSL_FATAL_ERROR;
  16538. }
  16539. }
  16540. #ifndef HAVE_USER_RSA
  16541. /* Pass good/bad args. */
  16542. if (ret == 0) {
  16543. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  16544. if (ret == BAD_FUNC_ARG) {
  16545. /* Get just the output length */
  16546. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  16547. }
  16548. if (ret > 0) {
  16549. /* Try Public Key. */
  16550. genKey.type = 0;
  16551. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  16552. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16553. /* Put back to Private Key */
  16554. genKey.type = 1;
  16555. #endif
  16556. }
  16557. if (ret == BAD_FUNC_ARG) {
  16558. ret = 0;
  16559. } else {
  16560. ret = WOLFSSL_FATAL_ERROR;
  16561. }
  16562. }
  16563. #else
  16564. /* Pass good/bad args. */
  16565. if (ret == 0) {
  16566. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  16567. if (ret == USER_CRYPTO_ERROR) {
  16568. /* Get just the output length */
  16569. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  16570. }
  16571. if (ret > 0) {
  16572. /* Try Public Key. */
  16573. genKey.type = 0;
  16574. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  16575. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16576. /* Put back to Private Key */
  16577. genKey.type = 1;
  16578. #endif
  16579. }
  16580. if (ret == USER_CRYPTO_ERROR) {
  16581. ret = 0;
  16582. } else {
  16583. ret = WOLFSSL_FATAL_ERROR;
  16584. }
  16585. }
  16586. #endif
  16587. if (der != NULL) {
  16588. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16589. }
  16590. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  16591. ret = WOLFSSL_FATAL_ERROR;
  16592. }
  16593. if (wc_FreeRng(&rng) || ret != 0) {
  16594. ret = WOLFSSL_FATAL_ERROR;
  16595. }
  16596. printf(resultFmt, ret == 0 ? passed : failed);
  16597. #endif
  16598. return ret;
  16599. } /* END test_wc_RsaKeyToDer */
  16600. /*
  16601. * Testing wc_RsaKeyToPublicDer()
  16602. */
  16603. static int test_wc_RsaKeyToPublicDer(void)
  16604. {
  16605. int ret = 0;
  16606. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16607. RsaKey key;
  16608. WC_RNG rng;
  16609. byte* der;
  16610. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16611. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16612. int bits = 1024;
  16613. word32 derLen = 162;
  16614. #else
  16615. int bits = 2048;
  16616. word32 derLen = 294;
  16617. #endif
  16618. XMEMSET(&rng, 0, sizeof(rng));
  16619. XMEMSET(&key, 0, sizeof(key));
  16620. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16621. if (der == NULL) {
  16622. ret = WOLFSSL_FATAL_ERROR;
  16623. }
  16624. if (ret == 0) {
  16625. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16626. }
  16627. if (ret == 0) {
  16628. ret = wc_InitRng(&rng);
  16629. }
  16630. if (ret == 0) {
  16631. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16632. }
  16633. printf(testingFmt, "wc_RsaKeyToPublicDer()");
  16634. if (ret == 0) {
  16635. /* test getting size only */
  16636. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  16637. if (ret >= 0)
  16638. ret = 0;
  16639. }
  16640. if (ret == 0) {
  16641. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  16642. if (ret >= 0) {
  16643. ret = 0;
  16644. } else {
  16645. ret = WOLFSSL_FATAL_ERROR;
  16646. }
  16647. }
  16648. if (ret == 0) {
  16649. /* test getting size only */
  16650. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  16651. if (ret >= 0)
  16652. ret = 0;
  16653. }
  16654. if (ret == 0) {
  16655. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  16656. if (ret >= 0) {
  16657. ret = 0;
  16658. } else {
  16659. ret = WOLFSSL_FATAL_ERROR;
  16660. }
  16661. }
  16662. #ifndef HAVE_USER_RSA
  16663. /* Pass in bad args. */
  16664. if (ret == 0) {
  16665. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  16666. if (ret == BAD_FUNC_ARG) {
  16667. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  16668. }
  16669. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  16670. ret = 0;
  16671. } else {
  16672. ret = WOLFSSL_FATAL_ERROR;
  16673. }
  16674. }
  16675. #else
  16676. /* Pass in bad args. */
  16677. if (ret == 0) {
  16678. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  16679. if (ret == USER_CRYPTO_ERROR) {
  16680. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  16681. }
  16682. if (ret == USER_CRYPTO_ERROR) {
  16683. ret = 0;
  16684. } else {
  16685. ret = WOLFSSL_FATAL_ERROR;
  16686. }
  16687. }
  16688. #endif
  16689. if (der != NULL) {
  16690. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16691. }
  16692. if (wc_FreeRsaKey(&key) || ret != 0) {
  16693. ret = WOLFSSL_FATAL_ERROR;
  16694. }
  16695. if (wc_FreeRng(&rng) || ret != 0) {
  16696. ret = WOLFSSL_FATAL_ERROR;
  16697. }
  16698. printf(resultFmt, ret == 0 ? passed : failed);
  16699. #endif
  16700. return ret;
  16701. } /* END test_wc_RsaKeyToPublicDer */
  16702. /*
  16703. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16704. */
  16705. static int test_wc_RsaPublicEncryptDecrypt(void)
  16706. {
  16707. int ret = 0;
  16708. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16709. RsaKey key;
  16710. WC_RNG rng;
  16711. const char inStr[] = TEST_STRING;
  16712. const word32 plainLen = (word32)TEST_STRING_SZ;
  16713. const word32 inLen = (word32)TEST_STRING_SZ;
  16714. int bits = TEST_RSA_BITS;
  16715. const word32 cipherLen = TEST_RSA_BYTES;
  16716. word32 cipherLenResult = cipherLen;
  16717. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16718. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16719. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16720. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16721. if (in == NULL || plain == NULL || cipher == NULL) {
  16722. printf("test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  16723. return MEMORY_E;
  16724. }
  16725. #endif
  16726. XMEMCPY(in, inStr, inLen);
  16727. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16728. if (ret == 0) {
  16729. ret = wc_InitRng(&rng);
  16730. }
  16731. if (ret == 0) {
  16732. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16733. }
  16734. /* Encrypt. */
  16735. printf(testingFmt, "wc_RsaPublicEncrypt()");
  16736. if (ret == 0) {
  16737. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  16738. if (ret >= 0) {
  16739. cipherLenResult = ret;
  16740. ret = 0;
  16741. } else {
  16742. ret = WOLFSSL_FATAL_ERROR;
  16743. }
  16744. }
  16745. /* Pass bad args. */
  16746. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  16747. printf(resultFmt, ret == 0 ? passed : failed);
  16748. if (ret != 0) {
  16749. return ret;
  16750. }
  16751. /* Decrypt */
  16752. printf(testingFmt, "wc_RsaPrivateDecrypt()");
  16753. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16754. /* Bind rng */
  16755. if (ret == 0) {
  16756. ret = wc_RsaSetRNG(&key, &rng);
  16757. }
  16758. #endif
  16759. if (ret == 0) {
  16760. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  16761. }
  16762. if (ret >= 0) {
  16763. ret = XMEMCMP(plain, inStr, plainLen);
  16764. }
  16765. /* Pass in bad args. */
  16766. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  16767. WC_FREE_VAR(in, NULL);
  16768. WC_FREE_VAR(plain, NULL);
  16769. WC_FREE_VAR(cipher, NULL);
  16770. if (wc_FreeRsaKey(&key) || ret != 0) {
  16771. ret = WOLFSSL_FATAL_ERROR;
  16772. }
  16773. if (wc_FreeRng(&rng) || ret != 0) {
  16774. ret = WOLFSSL_FATAL_ERROR;
  16775. }
  16776. printf(resultFmt, ret == 0 ? passed : failed);
  16777. #endif
  16778. return ret;
  16779. } /* END test_wc_RsaPublicEncryptDecrypt */
  16780. /*
  16781. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16782. */
  16783. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  16784. {
  16785. int ret = 0;
  16786. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16787. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16788. && !defined(NO_SHA)
  16789. RsaKey key;
  16790. WC_RNG rng;
  16791. const char inStr[] = TEST_STRING;
  16792. const word32 inLen = (word32)TEST_STRING_SZ;
  16793. const word32 plainSz = (word32)TEST_STRING_SZ;
  16794. byte* res = NULL;
  16795. int idx = 0;
  16796. int bits = TEST_RSA_BITS;
  16797. const word32 cipherSz = TEST_RSA_BYTES;
  16798. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16799. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16800. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16801. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16802. if (in == NULL || plain == NULL || cipher == NULL) {
  16803. printf("test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  16804. return MEMORY_E;
  16805. }
  16806. #endif
  16807. XMEMCPY(in, inStr, inLen);
  16808. /* Initialize stack structures. */
  16809. XMEMSET(&rng, 0, sizeof(rng));
  16810. XMEMSET(&key, 0, sizeof(key));
  16811. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  16812. if (ret == 0) {
  16813. ret = wc_InitRng(&rng);
  16814. }
  16815. if (ret == 0) {
  16816. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16817. }
  16818. /* Encrypt */
  16819. printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
  16820. if (ret == 0) {
  16821. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  16822. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  16823. if (ret >= 0) {
  16824. idx = ret;
  16825. ret = 0;
  16826. } else {
  16827. ret = WOLFSSL_FATAL_ERROR;
  16828. }
  16829. }
  16830. /*Pass bad args.*/
  16831. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  16832. printf(resultFmt, ret == 0 ? passed : failed);
  16833. if (ret != 0) {
  16834. return ret;
  16835. }
  16836. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16837. /* Decrypt */
  16838. printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
  16839. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16840. if (ret == 0) {
  16841. ret = wc_RsaSetRNG(&key, &rng);
  16842. }
  16843. #endif
  16844. if (ret == 0) {
  16845. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  16846. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  16847. WC_MGF1SHA1, NULL, 0);
  16848. }
  16849. if (ret >= 0) {
  16850. if (!XMEMCMP(plain, inStr, plainSz)) {
  16851. ret = 0;
  16852. } else {
  16853. ret = WOLFSSL_FATAL_ERROR;
  16854. }
  16855. }
  16856. /*Pass bad args.*/
  16857. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  16858. printf(resultFmt, ret == 0 ? passed : failed);
  16859. if (ret != 0) {
  16860. return ret;
  16861. }
  16862. printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
  16863. if (ret == 0) {
  16864. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  16865. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  16866. WC_MGF1SHA1, NULL, 0);
  16867. if (ret >= 0) {
  16868. if (!XMEMCMP(inStr, res, plainSz)) {
  16869. ret = 0;
  16870. } else {
  16871. ret = WOLFSSL_FATAL_ERROR;
  16872. }
  16873. }
  16874. }
  16875. #endif
  16876. WC_FREE_VAR(in, NULL);
  16877. WC_FREE_VAR(plain, NULL);
  16878. WC_FREE_VAR(cipher, NULL);
  16879. if (wc_FreeRsaKey(&key) || ret != 0) {
  16880. ret = WOLFSSL_FATAL_ERROR;
  16881. }
  16882. if (wc_FreeRng(&rng) || ret != 0) {
  16883. ret = WOLFSSL_FATAL_ERROR;
  16884. }
  16885. printf(resultFmt, ret == 0 ? passed : failed);
  16886. #endif
  16887. return ret;
  16888. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16889. /*
  16890. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16891. */
  16892. static int test_wc_RsaSSL_SignVerify(void)
  16893. {
  16894. int ret = 0;
  16895. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16896. RsaKey key;
  16897. WC_RNG rng;
  16898. const char inStr[] = TEST_STRING;
  16899. const word32 plainSz = (word32)TEST_STRING_SZ;
  16900. const word32 inLen = (word32)TEST_STRING_SZ;
  16901. word32 idx = 0;
  16902. int bits = TEST_RSA_BITS;
  16903. const word32 outSz = TEST_RSA_BYTES;
  16904. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16905. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16906. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16907. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16908. if (in == NULL || out == NULL || plain == NULL) {
  16909. printf("test_wc_RsaSSL_SignVerify failed\n");
  16910. return MEMORY_E;
  16911. }
  16912. #endif
  16913. XMEMCPY(in, inStr, inLen);
  16914. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16915. if (ret == 0) {
  16916. ret = wc_InitRng(&rng);
  16917. }
  16918. if (ret == 0) {
  16919. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16920. }
  16921. /* Sign. */
  16922. printf(testingFmt, "wc_RsaSSL_Sign()");
  16923. if (ret == 0) {
  16924. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  16925. if (ret == (int)outSz) {
  16926. idx = ret;
  16927. ret = 0;
  16928. } else {
  16929. ret = WOLFSSL_FATAL_ERROR;
  16930. }
  16931. }
  16932. #ifndef HAVE_USER_RSA
  16933. /* Test bad args. */
  16934. if (ret == 0) {
  16935. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  16936. if (ret == BAD_FUNC_ARG) {
  16937. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  16938. }
  16939. if (ret == BAD_FUNC_ARG) {
  16940. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  16941. }
  16942. if (ret == BAD_FUNC_ARG) {
  16943. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  16944. }
  16945. if (ret == BAD_FUNC_ARG) {
  16946. ret = 0;
  16947. } else {
  16948. ret = WOLFSSL_FATAL_ERROR;
  16949. }
  16950. }
  16951. #else
  16952. /* Test bad args. */
  16953. if (ret == 0) {
  16954. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  16955. if (ret == USER_CRYPTO_ERROR) {
  16956. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  16957. }
  16958. if (ret == USER_CRYPTO_ERROR) {
  16959. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  16960. }
  16961. if (ret == USER_CRYPTO_ERROR) {
  16962. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  16963. }
  16964. if (ret == USER_CRYPTO_ERROR) {
  16965. ret = 0;
  16966. } else {
  16967. ret = WOLFSSL_FATAL_ERROR;
  16968. }
  16969. }
  16970. #endif
  16971. printf(resultFmt, ret == 0 ? passed : failed);
  16972. if (ret != 0) {
  16973. return ret;
  16974. }
  16975. /* Verify. */
  16976. printf(testingFmt, "wc_RsaSSL_Verify()");
  16977. if (ret == 0) {
  16978. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  16979. if (ret == (int)inLen) {
  16980. ret = 0;
  16981. } else {
  16982. ret = WOLFSSL_FATAL_ERROR;
  16983. }
  16984. }
  16985. #ifndef HAVE_USER_RSA
  16986. /* Pass bad args. */
  16987. if (ret == 0) {
  16988. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  16989. if (ret == BAD_FUNC_ARG) {
  16990. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  16991. }
  16992. if (ret == BAD_FUNC_ARG) {
  16993. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  16994. }
  16995. if (ret == BAD_FUNC_ARG) {
  16996. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  16997. }
  16998. if (ret == BAD_FUNC_ARG) {
  16999. ret = 0;
  17000. } else {
  17001. ret = WOLFSSL_FATAL_ERROR;
  17002. }
  17003. }
  17004. #else
  17005. /* Pass bad args. */
  17006. if (ret == 0) {
  17007. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17008. if (ret == USER_CRYPTO_ERROR) {
  17009. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17010. }
  17011. if (ret == USER_CRYPTO_ERROR) {
  17012. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17013. }
  17014. if (ret == USER_CRYPTO_ERROR) {
  17015. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17016. }
  17017. if (ret == USER_CRYPTO_ERROR) {
  17018. ret = 0;
  17019. } else {
  17020. ret = WOLFSSL_FATAL_ERROR;
  17021. }
  17022. }
  17023. #endif
  17024. WC_FREE_VAR(in, NULL);
  17025. WC_FREE_VAR(out, NULL);
  17026. WC_FREE_VAR(plain, NULL);
  17027. if (wc_FreeRsaKey(&key) || ret != 0) {
  17028. ret = WOLFSSL_FATAL_ERROR;
  17029. }
  17030. if (wc_FreeRng(&rng) || ret != 0) {
  17031. ret = WOLFSSL_FATAL_ERROR;
  17032. }
  17033. printf(resultFmt, ret == 0 ? passed : failed);
  17034. #endif
  17035. return ret;
  17036. } /* END test_wc_RsaSSL_SignVerify */
  17037. /*
  17038. * Testing wc_RsaEncryptSize()
  17039. */
  17040. static int test_wc_RsaEncryptSize(void)
  17041. {
  17042. int ret = 0;
  17043. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17044. RsaKey key;
  17045. WC_RNG rng;
  17046. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17047. if (ret == 0) {
  17048. ret = wc_InitRng(&rng);
  17049. }
  17050. printf(testingFmt, "wc_RsaEncryptSize()");
  17051. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17052. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17053. if (ret == 0) {
  17054. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  17055. if (ret == 0) {
  17056. ret = wc_RsaEncryptSize(&key);
  17057. }
  17058. if (ret == 128) {
  17059. ret = 0;
  17060. } else {
  17061. ret = WOLFSSL_FATAL_ERROR;
  17062. }
  17063. }
  17064. if (wc_FreeRsaKey(&key) || ret != 0) {
  17065. ret = WOLFSSL_FATAL_ERROR;
  17066. } else {
  17067. ret = 0;
  17068. }
  17069. #endif
  17070. if (ret == 0) {
  17071. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  17072. if (ret == 0) {
  17073. ret = wc_RsaEncryptSize(&key);
  17074. }
  17075. if (ret == 256) {
  17076. ret = 0;
  17077. } else {
  17078. ret = WOLFSSL_FATAL_ERROR;
  17079. }
  17080. }
  17081. /* Pass in bad arg. */
  17082. if (ret == 0) {
  17083. ret = wc_RsaEncryptSize(NULL);
  17084. #ifndef HAVE_USER_RSA
  17085. if (ret == BAD_FUNC_ARG) {
  17086. ret = 0;
  17087. } else {
  17088. ret = WOLFSSL_FATAL_ERROR;
  17089. }
  17090. #endif
  17091. }
  17092. if (wc_FreeRsaKey(&key) || ret != 0) {
  17093. ret = WOLFSSL_FATAL_ERROR;
  17094. }
  17095. if (wc_FreeRng(&rng) || ret != 0) {
  17096. ret = WOLFSSL_FATAL_ERROR;
  17097. }
  17098. printf(resultFmt, ret == 0 ? passed : failed);
  17099. #endif
  17100. return ret;
  17101. } /* END test_wc_RsaEncryptSize*/
  17102. /*
  17103. * Testing wc_RsaFlattenPublicKey()
  17104. */
  17105. static int test_wc_RsaFlattenPublicKey(void)
  17106. {
  17107. int ret = 0;
  17108. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17109. RsaKey key;
  17110. WC_RNG rng;
  17111. byte e[256];
  17112. byte n[256];
  17113. word32 eSz = sizeof(e);
  17114. word32 nSz = sizeof(n);
  17115. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17116. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17117. int bits = 1024;
  17118. #else
  17119. int bits = 2048;
  17120. #endif
  17121. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17122. if (ret == 0) {
  17123. ret = wc_InitRng(&rng);
  17124. }
  17125. if (ret == 0) {
  17126. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17127. if (ret >= 0) {
  17128. ret = 0;
  17129. } else {
  17130. ret = WOLFSSL_FATAL_ERROR;
  17131. }
  17132. }
  17133. printf(testingFmt, "wc_RsaFlattenPublicKey()");
  17134. if (ret == 0) {
  17135. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  17136. }
  17137. #ifndef HAVE_USER_RSA
  17138. /* Pass bad args. */
  17139. if (ret == 0) {
  17140. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17141. if (ret == BAD_FUNC_ARG) {
  17142. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17143. }
  17144. if (ret == BAD_FUNC_ARG) {
  17145. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17146. }
  17147. if (ret == BAD_FUNC_ARG) {
  17148. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17149. }
  17150. if (ret == BAD_FUNC_ARG) {
  17151. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17152. }
  17153. if (ret == BAD_FUNC_ARG) {
  17154. ret = 0;
  17155. } else {
  17156. ret = WOLFSSL_FATAL_ERROR;
  17157. }
  17158. }
  17159. #else
  17160. /* Pass bad args. */
  17161. if (ret == 0) {
  17162. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17163. if (ret == USER_CRYPTO_ERROR) {
  17164. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17165. }
  17166. if (ret == USER_CRYPTO_ERROR) {
  17167. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17168. }
  17169. if (ret == USER_CRYPTO_ERROR) {
  17170. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17171. }
  17172. if (ret == USER_CRYPTO_ERROR) {
  17173. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17174. }
  17175. if (ret == USER_CRYPTO_ERROR) {
  17176. ret = 0;
  17177. } else {
  17178. ret = WOLFSSL_FATAL_ERROR;
  17179. }
  17180. }
  17181. #endif
  17182. if (wc_FreeRsaKey(&key) || ret != 0) {
  17183. ret = WOLFSSL_FATAL_ERROR;
  17184. }
  17185. if (wc_FreeRng(&rng) || ret != 0) {
  17186. ret = WOLFSSL_FATAL_ERROR;
  17187. }
  17188. printf(resultFmt, ret == 0 ? passed : failed);
  17189. #endif
  17190. return ret;
  17191. } /* END test_wc_RsaFlattenPublicKey */
  17192. /*
  17193. * unit test for wc_AesCcmSetKey
  17194. */
  17195. static int test_wc_AesCcmSetKey(void)
  17196. {
  17197. int ret = 0;
  17198. #ifdef HAVE_AESCCM
  17199. Aes aes;
  17200. const byte key16[] =
  17201. {
  17202. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17203. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17204. };
  17205. const byte key24[] =
  17206. {
  17207. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17208. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17209. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17210. };
  17211. const byte key32[] =
  17212. {
  17213. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17214. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17215. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17216. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17217. };
  17218. printf(testingFmt, "wc_AesCcmSetKey()");
  17219. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  17220. if (ret != 0)
  17221. return ret;
  17222. #ifdef WOLFSSL_AES_128
  17223. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  17224. #endif
  17225. #ifdef WOLFSSL_AES_192
  17226. if (ret == 0) {
  17227. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  17228. }
  17229. #endif
  17230. #ifdef WOLFSSL_AES_256
  17231. if (ret == 0) {
  17232. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  17233. }
  17234. #endif
  17235. /* Test bad args. */
  17236. if (ret == 0) {
  17237. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  17238. if (ret == BAD_FUNC_ARG) {
  17239. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  17240. }
  17241. if (ret == BAD_FUNC_ARG) {
  17242. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  17243. }
  17244. if (ret != BAD_FUNC_ARG) {
  17245. ret = WOLFSSL_FATAL_ERROR;
  17246. } else {
  17247. ret = 0;
  17248. }
  17249. }
  17250. wc_AesFree(&aes);
  17251. printf(resultFmt, ret == 0 ? passed : failed);
  17252. #endif
  17253. return ret;
  17254. } /* END test_wc_AesCcmSetKey */
  17255. /*
  17256. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17257. */
  17258. static int test_wc_AesCcmEncryptDecrypt(void)
  17259. {
  17260. int ret = 0;
  17261. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17262. Aes aes;
  17263. const byte key16[] =
  17264. {
  17265. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17266. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17267. };
  17268. /* plaintext */
  17269. const byte plainT[] =
  17270. {
  17271. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17272. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17273. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17274. };
  17275. /* nonce */
  17276. const byte iv[] =
  17277. {
  17278. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17279. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17280. };
  17281. const byte c[] = /* cipher text. */
  17282. {
  17283. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17284. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17285. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17286. };
  17287. const byte t[] = /* Auth tag */
  17288. {
  17289. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17290. };
  17291. const byte authIn[] =
  17292. {
  17293. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17294. };
  17295. byte cipherOut[sizeof(plainT)];
  17296. byte authTag[sizeof(t)];
  17297. int ccmE = WOLFSSL_FATAL_ERROR;
  17298. #ifdef HAVE_AES_DECRYPT
  17299. int ccmD = WOLFSSL_FATAL_ERROR;
  17300. byte plainOut[sizeof(cipherOut)];
  17301. #endif
  17302. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  17303. if (ret != 0)
  17304. return ret;
  17305. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  17306. if (ret == 0) {
  17307. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17308. iv, sizeof(iv), authTag, sizeof(authTag),
  17309. authIn , sizeof(authIn));
  17310. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  17311. XMEMCMP(t, authTag, sizeof(t))) {
  17312. ccmE = WOLFSSL_FATAL_ERROR;
  17313. ret = WOLFSSL_FATAL_ERROR;
  17314. }
  17315. #ifdef HAVE_AES_DECRYPT
  17316. if (ret == 0) {
  17317. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17318. sizeof(plainOut), iv, sizeof(iv),
  17319. authTag, sizeof(authTag),
  17320. authIn, sizeof(authIn));
  17321. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  17322. ccmD = WOLFSSL_FATAL_ERROR;
  17323. }
  17324. }
  17325. #endif
  17326. }
  17327. printf(testingFmt, "wc_AesCcmEncrypt()");
  17328. /* Pass in bad args. Encrypt*/
  17329. if (ret == 0 && ccmE == 0) {
  17330. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17331. iv, sizeof(iv), authTag, sizeof(authTag),
  17332. authIn , sizeof(authIn));
  17333. if (ccmE == BAD_FUNC_ARG) {
  17334. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17335. iv, sizeof(iv), authTag, sizeof(authTag),
  17336. authIn , sizeof(authIn));
  17337. }
  17338. if (ccmE == BAD_FUNC_ARG) {
  17339. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17340. iv, sizeof(iv), authTag, sizeof(authTag),
  17341. authIn , sizeof(authIn));
  17342. }
  17343. if (ccmE == BAD_FUNC_ARG) {
  17344. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17345. NULL, sizeof(iv), authTag, sizeof(authTag),
  17346. authIn , sizeof(authIn));
  17347. }
  17348. if (ccmE == BAD_FUNC_ARG) {
  17349. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17350. iv, sizeof(iv), NULL, sizeof(authTag),
  17351. authIn , sizeof(authIn));
  17352. }
  17353. if (ccmE == BAD_FUNC_ARG) {
  17354. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17355. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  17356. authIn , sizeof(authIn));
  17357. }
  17358. if (ccmE == BAD_FUNC_ARG) {
  17359. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17360. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  17361. authIn , sizeof(authIn));
  17362. }
  17363. if (ccmE != BAD_FUNC_ARG) {
  17364. ccmE = WOLFSSL_FATAL_ERROR;
  17365. } else {
  17366. ccmE = 0;
  17367. }
  17368. } /* End Encrypt */
  17369. printf(resultFmt, ccmE == 0 ? passed : failed);
  17370. if (ccmE != 0) {
  17371. wc_AesFree(&aes);
  17372. return ccmE;
  17373. }
  17374. #ifdef HAVE_AES_DECRYPT
  17375. printf(testingFmt, "wc_AesCcmDecrypt()");
  17376. /* Pass in bad args. Decrypt*/
  17377. if (ret == 0 && ccmD == 0) {
  17378. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17379. iv, sizeof(iv), authTag, sizeof(authTag),
  17380. authIn, sizeof(authIn));
  17381. if (ccmD == BAD_FUNC_ARG) {
  17382. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17383. iv, sizeof(iv), authTag, sizeof(authTag),
  17384. authIn, sizeof(authIn));
  17385. }
  17386. if (ccmD == BAD_FUNC_ARG) {
  17387. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17388. iv, sizeof(iv), authTag, sizeof(authTag),
  17389. authIn, sizeof(authIn));
  17390. }
  17391. if (ccmD == BAD_FUNC_ARG) {
  17392. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17393. sizeof(plainOut), NULL, sizeof(iv),
  17394. authTag, sizeof(authTag),
  17395. authIn, sizeof(authIn));
  17396. }
  17397. if (ccmD == BAD_FUNC_ARG) {
  17398. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17399. sizeof(plainOut), iv, sizeof(iv), NULL,
  17400. sizeof(authTag), authIn, sizeof(authIn));
  17401. }
  17402. if (ccmD == BAD_FUNC_ARG) {
  17403. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17404. sizeof(plainOut), iv, sizeof(iv) + 1,
  17405. authTag, sizeof(authTag),
  17406. authIn, sizeof(authIn));
  17407. }
  17408. if (ccmD == BAD_FUNC_ARG) {
  17409. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17410. sizeof(plainOut), iv, sizeof(iv) - 7,
  17411. authTag, sizeof(authTag),
  17412. authIn, sizeof(authIn));
  17413. }
  17414. if (ccmD != BAD_FUNC_ARG) {
  17415. ccmD = WOLFSSL_FATAL_ERROR;
  17416. } else {
  17417. ccmD = 0;
  17418. }
  17419. } /* END Decrypt */
  17420. printf(resultFmt, ccmD == 0 ? passed : failed);
  17421. if (ccmD != 0) {
  17422. return ccmD;
  17423. }
  17424. #endif
  17425. wc_AesFree(&aes);
  17426. #endif /* HAVE_AESCCM */
  17427. return ret;
  17428. } /* END test_wc_AesCcmEncryptDecrypt */
  17429. /*
  17430. * Testing wc_InitDsaKey()
  17431. */
  17432. static int test_wc_InitDsaKey(void)
  17433. {
  17434. int ret = 0;
  17435. #ifndef NO_DSA
  17436. DsaKey key;
  17437. printf(testingFmt, "wc_InitDsaKey()");
  17438. ret = wc_InitDsaKey(&key);
  17439. /* Pass in bad args. */
  17440. if (ret == 0) {
  17441. ret = wc_InitDsaKey(NULL);
  17442. if (ret == BAD_FUNC_ARG) {
  17443. ret = 0;
  17444. } else {
  17445. ret = WOLFSSL_FATAL_ERROR;
  17446. }
  17447. }
  17448. printf(resultFmt, ret == 0 ? passed : failed);
  17449. wc_FreeDsaKey(&key);
  17450. #endif
  17451. return ret;
  17452. } /* END test_wc_InitDsaKey */
  17453. /*
  17454. * Testing wc_DsaSign() and wc_DsaVerify()
  17455. */
  17456. static int test_wc_DsaSignVerify(void)
  17457. {
  17458. int ret = 0;
  17459. #if !defined(NO_DSA)
  17460. DsaKey key;
  17461. WC_RNG rng;
  17462. wc_Sha sha;
  17463. byte signature[DSA_SIG_SIZE];
  17464. byte hash[WC_SHA_DIGEST_SIZE];
  17465. word32 idx = 0;
  17466. word32 bytes;
  17467. int answer;
  17468. #ifdef USE_CERT_BUFFERS_1024
  17469. byte tmp[ONEK_BUF];
  17470. XMEMSET(tmp, 0, sizeof(tmp));
  17471. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17472. bytes = sizeof_dsa_key_der_1024;
  17473. #elif defined(USE_CERT_BUFFERS_2048)
  17474. byte tmp[TWOK_BUF];
  17475. XMEMSET(tmp, 0, sizeof(tmp));
  17476. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17477. bytes = sizeof_dsa_key_der_2048;
  17478. #else
  17479. byte tmp[TWOK_BUF];
  17480. XMEMSET(tmp, 0, sizeof(tmp));
  17481. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17482. if (fp == XBADFILE) {
  17483. return WOLFSSL_BAD_FILE;
  17484. }
  17485. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17486. XFCLOSE(fp);
  17487. #endif /* END USE_CERT_BUFFERS_1024 */
  17488. ret = wc_InitSha(&sha);
  17489. if (ret == 0) {
  17490. ret = wc_ShaUpdate(&sha, tmp, bytes);
  17491. if (ret == 0) {
  17492. ret = wc_ShaFinal(&sha, hash);
  17493. }
  17494. if (ret == 0) {
  17495. ret = wc_InitDsaKey(&key);
  17496. }
  17497. if (ret == 0) {
  17498. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17499. }
  17500. if (ret == 0) {
  17501. ret = wc_InitRng(&rng);
  17502. }
  17503. }
  17504. printf(testingFmt, "wc_DsaSign()");
  17505. /* Sign. */
  17506. if (ret == 0) {
  17507. ret = wc_DsaSign(hash, signature, &key, &rng);
  17508. }
  17509. /* Test bad args. */
  17510. if (ret == 0) {
  17511. ret = wc_DsaSign(NULL, signature, &key, &rng);
  17512. if (ret == BAD_FUNC_ARG) {
  17513. ret = wc_DsaSign(hash, NULL, &key, &rng);
  17514. }
  17515. if (ret == BAD_FUNC_ARG) {
  17516. ret = wc_DsaSign(hash, signature, NULL, &rng);
  17517. }
  17518. if (ret == BAD_FUNC_ARG) {
  17519. ret = wc_DsaSign(hash, signature, &key, NULL);
  17520. }
  17521. if (ret == BAD_FUNC_ARG) {
  17522. ret = 0;
  17523. } else {
  17524. ret = WOLFSSL_FATAL_ERROR;
  17525. }
  17526. }
  17527. printf(resultFmt, ret == 0 ? passed : failed);
  17528. if (ret != 0) {
  17529. return ret;
  17530. }
  17531. /* Verify. */
  17532. printf(testingFmt, "wc_DsaVerify()");
  17533. ret = wc_DsaVerify(hash, signature, &key, &answer);
  17534. if (ret != 0 || answer != 1) {
  17535. ret = WOLFSSL_FATAL_ERROR;
  17536. } else {
  17537. ret = 0;
  17538. }
  17539. /* Pass in bad args. */
  17540. if (ret == 0) {
  17541. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  17542. if (ret == BAD_FUNC_ARG) {
  17543. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  17544. }
  17545. if (ret == BAD_FUNC_ARG) {
  17546. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  17547. }
  17548. if (ret == BAD_FUNC_ARG) {
  17549. ret = wc_DsaVerify(hash, signature, &key, NULL);
  17550. }
  17551. if (ret == BAD_FUNC_ARG) {
  17552. ret = 0;
  17553. } else {
  17554. ret = WOLFSSL_FATAL_ERROR;
  17555. }
  17556. }
  17557. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  17558. /* hard set q to 0 and test fail case */
  17559. mp_free(&key.q);
  17560. mp_init(&key.q);
  17561. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17562. mp_set(&key.q, 1);
  17563. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17564. #endif
  17565. if (wc_FreeRng(&rng) && ret == 0) {
  17566. ret = WOLFSSL_FATAL_ERROR;
  17567. }
  17568. printf(resultFmt, ret == 0 ? passed : failed);
  17569. wc_FreeDsaKey(&key);
  17570. wc_ShaFree(&sha);
  17571. #endif
  17572. return ret;
  17573. } /* END test_wc_DsaSign */
  17574. /*
  17575. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  17576. */
  17577. static int test_wc_DsaPublicPrivateKeyDecode(void)
  17578. {
  17579. int ret = 0;
  17580. #if !defined(NO_DSA)
  17581. DsaKey key;
  17582. word32 bytes;
  17583. word32 idx = 0;
  17584. int priv = WOLFSSL_FATAL_ERROR;
  17585. int pub = WOLFSSL_FATAL_ERROR;
  17586. #ifdef USE_CERT_BUFFERS_1024
  17587. byte tmp[ONEK_BUF];
  17588. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17589. bytes = sizeof_dsa_key_der_1024;
  17590. #elif defined(USE_CERT_BUFFERS_2048)
  17591. byte tmp[TWOK_BUF];
  17592. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17593. bytes = sizeof_dsa_key_der_2048;
  17594. #else
  17595. byte tmp[TWOK_BUF];
  17596. XMEMSET(tmp, 0, sizeof(tmp));
  17597. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17598. if (fp == XBADFILE)
  17599. {
  17600. return WOLFSSL_BAD_FILE;
  17601. }
  17602. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17603. XFCLOSE(fp);
  17604. #endif /* END USE_CERT_BUFFERS_1024 */
  17605. ret = wc_InitDsaKey(&key);
  17606. printf(testingFmt, "wc_DsaPrivateKeyDecode()");
  17607. if (ret == 0) {
  17608. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17609. /* Test bad args. */
  17610. if (priv == 0) {
  17611. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  17612. if (priv == BAD_FUNC_ARG) {
  17613. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  17614. }
  17615. if (priv == BAD_FUNC_ARG) {
  17616. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  17617. }
  17618. if (priv == BAD_FUNC_ARG) {
  17619. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17620. }
  17621. if (priv == ASN_PARSE_E) {
  17622. priv = 0;
  17623. } else {
  17624. priv = WOLFSSL_FATAL_ERROR;
  17625. }
  17626. }
  17627. wc_FreeDsaKey(&key);
  17628. ret = wc_InitDsaKey(&key);
  17629. }
  17630. printf(resultFmt, priv == 0 ? passed : failed);
  17631. printf(testingFmt, "wc_DsaPublicKeyDecode()");
  17632. if (ret == 0) {
  17633. idx = 0; /* Reset */
  17634. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  17635. /* Test bad args. */
  17636. if (pub == 0) {
  17637. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  17638. if (pub == BAD_FUNC_ARG) {
  17639. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  17640. }
  17641. if (pub == BAD_FUNC_ARG) {
  17642. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  17643. }
  17644. if (pub == BAD_FUNC_ARG) {
  17645. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  17646. }
  17647. if (pub == ASN_PARSE_E) {
  17648. pub = 0;
  17649. } else {
  17650. pub = WOLFSSL_FATAL_ERROR;
  17651. }
  17652. }
  17653. } /* END Public Key */
  17654. printf(resultFmt, pub == 0 ? passed : failed);
  17655. wc_FreeDsaKey(&key);
  17656. #endif
  17657. return ret;
  17658. } /* END test_wc_DsaPublicPrivateKeyDecode */
  17659. /*
  17660. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  17661. */
  17662. static int test_wc_MakeDsaKey(void)
  17663. {
  17664. int ret = 0;
  17665. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17666. DsaKey genKey;
  17667. WC_RNG rng;
  17668. XMEMSET(&rng, 0, sizeof(rng));
  17669. XMEMSET(&genKey, 0, sizeof(genKey));
  17670. ret = wc_InitRng(&rng);
  17671. if (ret == 0) {
  17672. ret = wc_InitDsaKey(&genKey);
  17673. }
  17674. printf(testingFmt, "wc_MakeDsaParameters()");
  17675. if (ret == 0) {
  17676. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  17677. }
  17678. /* Test bad args. */
  17679. if (ret == 0) {
  17680. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  17681. if (ret == BAD_FUNC_ARG) {
  17682. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  17683. }
  17684. if (ret == BAD_FUNC_ARG) {
  17685. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  17686. }
  17687. if (ret == BAD_FUNC_ARG) {
  17688. ret = 0;
  17689. } else {
  17690. ret = WOLFSSL_FATAL_ERROR;
  17691. }
  17692. }
  17693. printf(resultFmt, ret == 0 ? passed : failed);
  17694. printf(testingFmt, "wc_MakeDsaKey()");
  17695. if (ret == 0) {
  17696. ret = wc_MakeDsaKey(&rng, &genKey);
  17697. }
  17698. /* Test bad args. */
  17699. if (ret == 0) {
  17700. ret = wc_MakeDsaKey(NULL, &genKey);
  17701. if (ret == BAD_FUNC_ARG) {
  17702. ret = wc_MakeDsaKey(&rng, NULL);
  17703. }
  17704. if (ret == BAD_FUNC_ARG) {
  17705. ret = 0;
  17706. } else {
  17707. ret = WOLFSSL_FATAL_ERROR;
  17708. }
  17709. }
  17710. if (wc_FreeRng(&rng) && ret == 0) {
  17711. ret = WOLFSSL_FAILURE;
  17712. }
  17713. printf(resultFmt, ret == 0 ? passed : failed);
  17714. wc_FreeDsaKey(&genKey);
  17715. #endif
  17716. return ret;
  17717. } /* END test_wc_MakeDsaKey */
  17718. /*
  17719. * Testing wc_DsaKeyToDer()
  17720. */
  17721. static int test_wc_DsaKeyToDer(void)
  17722. {
  17723. int ret = 0;
  17724. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17725. DsaKey genKey;
  17726. WC_RNG rng;
  17727. word32 bytes;
  17728. word32 idx = 0;
  17729. #ifdef USE_CERT_BUFFERS_1024
  17730. byte tmp[ONEK_BUF];
  17731. byte der[ONEK_BUF];
  17732. XMEMSET(tmp, 0, sizeof(tmp));
  17733. XMEMSET(der, 0, sizeof(der));
  17734. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17735. bytes = sizeof_dsa_key_der_1024;
  17736. #elif defined(USE_CERT_BUFFERS_2048)
  17737. byte tmp[TWOK_BUF];
  17738. byte der[TWOK_BUF];
  17739. XMEMSET(tmp, 0, sizeof(tmp));
  17740. XMEMSET(der, 0, sizeof(der));
  17741. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17742. bytes = sizeof_dsa_key_der_2048;
  17743. #else
  17744. byte tmp[TWOK_BUF];
  17745. byte der[TWOK_BUF];
  17746. XMEMSET(tmp, 0, sizeof(tmp));
  17747. XMEMSET(der, 0, sizeof(der));
  17748. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17749. if (fp == XBADFILE) {
  17750. return WOLFSSL_BAD_FILE;
  17751. }
  17752. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17753. XFCLOSE(fp);
  17754. #endif /* END USE_CERT_BUFFERS_1024 */
  17755. XMEMSET(&rng, 0, sizeof(rng));
  17756. XMEMSET(&genKey, 0, sizeof(genKey));
  17757. ret = wc_InitRng(&rng);
  17758. if (ret == 0) {
  17759. ret = wc_InitDsaKey(&genKey);
  17760. }
  17761. if (ret == 0) {
  17762. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  17763. if (ret == 0) {
  17764. wc_FreeDsaKey(&genKey);
  17765. ret = wc_InitDsaKey(&genKey);
  17766. }
  17767. }
  17768. if (ret == 0) {
  17769. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  17770. }
  17771. printf(testingFmt, "wc_DsaKeyToDer()");
  17772. if (ret == 0) {
  17773. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  17774. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  17775. ret = 0;
  17776. }
  17777. }
  17778. /* Test bad args. */
  17779. if (ret == 0) {
  17780. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  17781. if (ret == BAD_FUNC_ARG) {
  17782. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  17783. }
  17784. if (ret == BAD_FUNC_ARG) {
  17785. ret = 0;
  17786. } else {
  17787. ret = WOLFSSL_FATAL_ERROR;
  17788. }
  17789. }
  17790. if (wc_FreeRng(&rng) && ret == 0) {
  17791. ret = WOLFSSL_FATAL_ERROR;
  17792. }
  17793. printf(resultFmt, ret == 0 ? passed : failed);
  17794. wc_FreeDsaKey(&genKey);
  17795. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  17796. return ret;
  17797. } /* END test_wc_DsaKeyToDer */
  17798. /*
  17799. * Testing wc_DsaKeyToPublicDer()
  17800. * (indirectly testing setDsaPublicKey())
  17801. */
  17802. static int test_wc_DsaKeyToPublicDer(void)
  17803. {
  17804. int ret = 0;
  17805. #ifndef HAVE_SELFTEST
  17806. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17807. DsaKey genKey;
  17808. WC_RNG rng;
  17809. byte* der;
  17810. word32 sz;
  17811. printf(testingFmt, "wc_DsaKeyToPublicDer()");
  17812. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17813. if (der == NULL) {
  17814. ret = WOLFSSL_FATAL_ERROR;
  17815. }
  17816. if (ret == 0) {
  17817. ret = wc_InitDsaKey(&genKey);
  17818. }
  17819. if (ret == 0) {
  17820. ret = wc_InitRng(&rng);
  17821. }
  17822. if (ret == 0) {
  17823. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  17824. }
  17825. if (ret == 0) {
  17826. ret = wc_MakeDsaKey(&rng, &genKey);
  17827. }
  17828. if (ret == 0) {
  17829. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  17830. if (ret >= 0) {
  17831. sz = ret;
  17832. ret = 0;
  17833. } else {
  17834. ret = WOLFSSL_FATAL_ERROR;
  17835. }
  17836. }
  17837. if (ret == 0) {
  17838. word32 idx = 0;
  17839. wc_FreeDsaKey(&genKey);
  17840. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  17841. }
  17842. /* Test without the SubjectPublicKeyInfo header */
  17843. if (ret == 0) {
  17844. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  17845. if (ret >= 0) {
  17846. sz = ret;
  17847. ret = 0;
  17848. } else {
  17849. ret = WOLFSSL_FATAL_ERROR;
  17850. }
  17851. }
  17852. if (ret == 0) {
  17853. word32 idx = 0;
  17854. wc_FreeDsaKey(&genKey);
  17855. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  17856. }
  17857. /* Test bad args. */
  17858. if (ret == 0) {
  17859. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  17860. if (ret == BAD_FUNC_ARG) {
  17861. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  17862. }
  17863. if (ret == BAD_FUNC_ARG) {
  17864. ret = 0;
  17865. } else {
  17866. ret = WOLFSSL_FATAL_ERROR;
  17867. }
  17868. }
  17869. if (wc_FreeRng(&rng) && ret == 0) {
  17870. ret = WOLFSSL_FATAL_ERROR;
  17871. }
  17872. printf(resultFmt, ret == 0 ? passed : failed);
  17873. XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
  17874. wc_FreeDsaKey(&genKey);
  17875. #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
  17876. #endif /* HAVE_SELFTEST */
  17877. return ret;
  17878. } /* END test_wc_DsaKeyToPublicDer */
  17879. /*
  17880. * Testing wc_DsaImportParamsRaw()
  17881. */
  17882. static int test_wc_DsaImportParamsRaw(void)
  17883. {
  17884. int ret = 0;
  17885. #if !defined(NO_DSA)
  17886. DsaKey key;
  17887. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17888. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17889. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17890. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17891. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17892. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17893. "80a0093113a8bd73";
  17894. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17895. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17896. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17897. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17898. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17899. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17900. "76341a7e7d9";
  17901. /* invalid p and q parameters */
  17902. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17903. const char* invalidQ = "96c5390a";
  17904. printf(testingFmt, "wc_DsaImportParamsRaw()");
  17905. ret = wc_InitDsaKey(&key);
  17906. if (ret == 0) {
  17907. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  17908. }
  17909. /* test bad args */
  17910. if (ret == 0) {
  17911. /* null key struct */
  17912. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  17913. if (ret == BAD_FUNC_ARG) {
  17914. /* null param pointers */
  17915. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  17916. }
  17917. if (ret == BAD_FUNC_ARG) {
  17918. /* illegal p length */
  17919. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  17920. }
  17921. if (ret == BAD_FUNC_ARG) {
  17922. /* illegal q length */
  17923. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  17924. if (ret == BAD_FUNC_ARG)
  17925. ret = 0;
  17926. }
  17927. }
  17928. printf(resultFmt, ret == 0 ? passed : failed);
  17929. wc_FreeDsaKey(&key);
  17930. #endif
  17931. return ret;
  17932. } /* END test_wc_DsaImportParamsRaw */
  17933. /*
  17934. * Testing wc_DsaImportParamsRawCheck()
  17935. */
  17936. static int test_wc_DsaImportParamsRawCheck(void)
  17937. {
  17938. int ret = 0;
  17939. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17940. DsaKey key;
  17941. int trusted = 0;
  17942. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17943. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17944. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17945. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17946. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17947. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17948. "80a0093113a8bd73";
  17949. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17950. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17951. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17952. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17953. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17954. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17955. "76341a7e7d9";
  17956. /* invalid p and q parameters */
  17957. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17958. const char* invalidQ = "96c5390a";
  17959. printf(testingFmt, "wc_DsaImportParamsRawCheck()");
  17960. ret = wc_InitDsaKey(&key);
  17961. if (ret == 0) {
  17962. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  17963. }
  17964. /* test bad args */
  17965. if (ret == 0) {
  17966. /* null key struct */
  17967. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  17968. if (ret == BAD_FUNC_ARG) {
  17969. /* null param pointers */
  17970. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  17971. }
  17972. if (ret == BAD_FUNC_ARG) {
  17973. /* illegal p length */
  17974. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  17975. }
  17976. if (ret == BAD_FUNC_ARG) {
  17977. /* illegal q length */
  17978. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  17979. if (ret == BAD_FUNC_ARG)
  17980. ret = 0;
  17981. }
  17982. }
  17983. printf(resultFmt, ret == 0 ? passed : failed);
  17984. wc_FreeDsaKey(&key);
  17985. #endif
  17986. return ret;
  17987. } /* END test_wc_DsaImportParamsRawCheck */
  17988. /*
  17989. * Testing wc_DsaExportParamsRaw()
  17990. */
  17991. static int test_wc_DsaExportParamsRaw(void)
  17992. {
  17993. int ret = 0;
  17994. #if !defined(NO_DSA)
  17995. DsaKey key;
  17996. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17997. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17998. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17999. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18000. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18001. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18002. "80a0093113a8bd73";
  18003. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18004. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18005. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18006. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18007. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18008. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18009. "76341a7e7d9";
  18010. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  18011. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  18012. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  18013. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  18014. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  18015. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  18016. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  18017. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  18018. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  18019. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  18020. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  18021. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  18022. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  18023. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  18024. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  18025. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  18026. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  18027. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  18028. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  18029. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  18030. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  18031. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  18032. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  18033. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  18034. byte pOut[MAX_DSA_PARAM_SIZE];
  18035. byte qOut[MAX_DSA_PARAM_SIZE];
  18036. byte gOut[MAX_DSA_PARAM_SIZE];
  18037. word32 pOutSz, qOutSz, gOutSz;
  18038. printf(testingFmt, "wc_DsaExportParamsRaw()");
  18039. ret = wc_InitDsaKey(&key);
  18040. if (ret == 0) {
  18041. /* first test using imported raw parameters, for expected */
  18042. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18043. }
  18044. if (ret == 0) {
  18045. pOutSz = sizeof(pOut);
  18046. qOutSz = sizeof(qOut);
  18047. gOutSz = sizeof(gOut);
  18048. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18049. gOut, &gOutSz);
  18050. }
  18051. if (ret == 0) {
  18052. /* validate exported parameters are correct */
  18053. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  18054. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  18055. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  18056. ret = -1;
  18057. }
  18058. }
  18059. /* test bad args */
  18060. if (ret == 0) {
  18061. /* null key struct */
  18062. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  18063. gOut, &gOutSz);
  18064. if (ret == BAD_FUNC_ARG) {
  18065. /* null output pointers */
  18066. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  18067. NULL, &gOutSz);
  18068. }
  18069. if (ret == LENGTH_ONLY_E) {
  18070. /* null output size pointers */
  18071. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  18072. gOut, NULL);
  18073. }
  18074. if (ret == BAD_FUNC_ARG) {
  18075. /* p output buffer size too small */
  18076. pOutSz = 1;
  18077. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18078. gOut, &gOutSz);
  18079. pOutSz = sizeof(pOut);
  18080. }
  18081. if (ret == BUFFER_E) {
  18082. /* q output buffer size too small */
  18083. qOutSz = 1;
  18084. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18085. gOut, &gOutSz);
  18086. qOutSz = sizeof(qOut);
  18087. }
  18088. if (ret == BUFFER_E) {
  18089. /* g output buffer size too small */
  18090. gOutSz = 1;
  18091. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18092. gOut, &gOutSz);
  18093. if (ret == BUFFER_E)
  18094. ret = 0;
  18095. }
  18096. }
  18097. printf(resultFmt, ret == 0 ? passed : failed);
  18098. wc_FreeDsaKey(&key);
  18099. #endif
  18100. return ret;
  18101. } /* END test_wc_DsaExportParamsRaw */
  18102. /*
  18103. * Testing wc_DsaExportKeyRaw()
  18104. */
  18105. static int test_wc_DsaExportKeyRaw(void)
  18106. {
  18107. int ret = 0;
  18108. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18109. DsaKey key;
  18110. WC_RNG rng;
  18111. byte xOut[MAX_DSA_PARAM_SIZE];
  18112. byte yOut[MAX_DSA_PARAM_SIZE];
  18113. word32 xOutSz, yOutSz;
  18114. printf(testingFmt, "wc_DsaExportKeyRaw()");
  18115. XMEMSET(&rng, 0, sizeof(rng));
  18116. XMEMSET(&key, 0, sizeof(key));
  18117. ret = wc_InitRng(&rng);
  18118. if (ret == 0) {
  18119. ret = wc_InitDsaKey(&key);
  18120. }
  18121. if (ret == 0) {
  18122. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  18123. if (ret == 0) {
  18124. ret = wc_MakeDsaKey(&rng, &key);
  18125. }
  18126. }
  18127. /* try successful export */
  18128. if (ret == 0) {
  18129. xOutSz = sizeof(xOut);
  18130. yOutSz = sizeof(yOut);
  18131. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18132. }
  18133. /* test bad args */
  18134. if (ret == 0) {
  18135. /* null key struct */
  18136. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  18137. if (ret == BAD_FUNC_ARG) {
  18138. /* null output pointers */
  18139. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  18140. }
  18141. if (ret == LENGTH_ONLY_E) {
  18142. /* null output size pointers */
  18143. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  18144. }
  18145. if (ret == BAD_FUNC_ARG) {
  18146. /* x output buffer size too small */
  18147. xOutSz = 1;
  18148. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18149. xOutSz = sizeof(xOut);
  18150. }
  18151. if (ret == BUFFER_E) {
  18152. /* y output buffer size too small */
  18153. yOutSz = 1;
  18154. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18155. if (ret == BUFFER_E)
  18156. ret = 0;
  18157. }
  18158. }
  18159. printf(resultFmt, ret == 0 ? passed : failed);
  18160. wc_FreeDsaKey(&key);
  18161. wc_FreeRng(&rng);
  18162. #endif
  18163. return ret;
  18164. } /* END test_wc_DsaExportParamsRaw */
  18165. /*
  18166. * Testing wc_ed25519_make_key().
  18167. */
  18168. static int test_wc_ed25519_make_key(void)
  18169. {
  18170. int ret = 0;
  18171. #if defined(HAVE_ED25519)
  18172. ed25519_key key;
  18173. WC_RNG rng;
  18174. ret = wc_InitRng(&rng);
  18175. if (ret == 0) {
  18176. ret = wc_ed25519_init(&key);
  18177. }
  18178. printf(testingFmt, "wc_ed25519_make_key()");
  18179. if (ret == 0) {
  18180. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18181. }
  18182. /* Test bad args. */
  18183. if (ret == 0) {
  18184. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  18185. if (ret == BAD_FUNC_ARG) {
  18186. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  18187. }
  18188. if (ret == BAD_FUNC_ARG) {
  18189. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  18190. }
  18191. if (ret == BAD_FUNC_ARG) {
  18192. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  18193. }
  18194. if (ret == BAD_FUNC_ARG) {
  18195. ret = 0;
  18196. } else if (ret == 0) {
  18197. ret = WOLFSSL_FATAL_ERROR;
  18198. }
  18199. }
  18200. printf(resultFmt, ret == 0 ? passed : failed);
  18201. if (wc_FreeRng(&rng) && ret == 0) {
  18202. ret = WOLFSSL_FATAL_ERROR;
  18203. }
  18204. wc_ed25519_free(&key);
  18205. #endif
  18206. return ret;
  18207. } /* END test_wc_ed25519_make_key */
  18208. /*
  18209. * Testing wc_ed25519_init()
  18210. */
  18211. static int test_wc_ed25519_init(void)
  18212. {
  18213. int ret = 0;
  18214. #if defined(HAVE_ED25519)
  18215. ed25519_key key;
  18216. printf(testingFmt, "wc_ed25519_init()");
  18217. ret = wc_ed25519_init(&key);
  18218. /* Test bad args. */
  18219. if (ret == 0) {
  18220. ret = wc_ed25519_init(NULL);
  18221. if (ret == BAD_FUNC_ARG) {
  18222. ret = 0;
  18223. } else if (ret == 0) {
  18224. ret = WOLFSSL_FATAL_ERROR;
  18225. }
  18226. }
  18227. printf(resultFmt, ret == 0 ? passed : failed);
  18228. wc_ed25519_free(&key);
  18229. #endif
  18230. return ret;
  18231. } /* END test_wc_ed25519_init */
  18232. /*
  18233. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  18234. */
  18235. static int test_wc_ed25519_sign_msg(void)
  18236. {
  18237. int ret = 0;
  18238. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  18239. WC_RNG rng;
  18240. ed25519_key key;
  18241. byte msg[] = "Everybody gets Friday off.\n";
  18242. byte sig[ED25519_SIG_SIZE];
  18243. word32 msglen = sizeof(msg);
  18244. word32 siglen = sizeof(sig);
  18245. word32 badSigLen = sizeof(sig) - 1;
  18246. #ifdef HAVE_ED25519_VERIFY
  18247. int verify_ok = 0; /*1 = Verify success.*/
  18248. #endif
  18249. /* Initialize stack variables. */
  18250. XMEMSET(sig, 0, siglen);
  18251. /* Initialize key. */
  18252. ret = wc_InitRng(&rng);
  18253. if (ret == 0) {
  18254. ret = wc_ed25519_init(&key);
  18255. if (ret == 0) {
  18256. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18257. }
  18258. }
  18259. printf(testingFmt, "wc_ed25519_sign_msg()");
  18260. if (ret == 0) {
  18261. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  18262. }
  18263. /* Test bad args. */
  18264. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  18265. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  18266. if (ret == BAD_FUNC_ARG) {
  18267. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  18268. }
  18269. if (ret == BAD_FUNC_ARG) {
  18270. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  18271. }
  18272. if (ret == BAD_FUNC_ARG) {
  18273. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  18274. }
  18275. if (ret == BAD_FUNC_ARG) {
  18276. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  18277. }
  18278. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  18279. badSigLen -= 1;
  18280. ret = 0;
  18281. } else if (ret == 0) {
  18282. ret = WOLFSSL_FATAL_ERROR;
  18283. }
  18284. } /* END sign */
  18285. printf(resultFmt, ret == 0 ? passed : failed);
  18286. #ifdef HAVE_ED25519_VERIFY
  18287. printf(testingFmt, "wc_ed25519_verify_msg()");
  18288. if (ret == 0) {
  18289. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  18290. if (ret == 0 && verify_ok == 1) {
  18291. ret = 0;
  18292. } else if (ret == 0) {
  18293. ret = WOLFSSL_FATAL_ERROR;
  18294. }
  18295. /* Test bad args. */
  18296. if (ret == 0) {
  18297. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  18298. msglen, &verify_ok, &key),
  18299. BAD_FUNC_ARG);
  18300. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  18301. msglen, &verify_ok, &key),
  18302. BAD_FUNC_ARG);
  18303. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  18304. &key);
  18305. if (ret == BAD_FUNC_ARG) {
  18306. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  18307. &verify_ok, &key);
  18308. }
  18309. if (ret == BAD_FUNC_ARG) {
  18310. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18311. NULL, &key);
  18312. }
  18313. if (ret == BAD_FUNC_ARG) {
  18314. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18315. &verify_ok, NULL);
  18316. }
  18317. if (ret == BAD_FUNC_ARG) {
  18318. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  18319. &verify_ok, &key);
  18320. }
  18321. if (ret == BAD_FUNC_ARG) {
  18322. ret = 0;
  18323. } else if (ret == 0) {
  18324. ret = WOLFSSL_FATAL_ERROR;
  18325. }
  18326. }
  18327. } /* END verify. */
  18328. printf(resultFmt, ret == 0 ? passed : failed);
  18329. #endif /* Verify. */
  18330. if (wc_FreeRng(&rng) && ret == 0) {
  18331. ret = WOLFSSL_FATAL_ERROR;
  18332. }
  18333. wc_ed25519_free(&key);
  18334. #endif
  18335. return ret;
  18336. } /* END test_wc_ed25519_sign_msg */
  18337. /*
  18338. * Testing wc_ed25519_import_public()
  18339. */
  18340. static int test_wc_ed25519_import_public(void)
  18341. {
  18342. int ret = 0;
  18343. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18344. WC_RNG rng;
  18345. ed25519_key pubKey;
  18346. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  18347. word32 inlen = sizeof(in);
  18348. ret = wc_InitRng(&rng);
  18349. if (ret == 0) {
  18350. ret = wc_ed25519_init(&pubKey);
  18351. if (ret == 0) {
  18352. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  18353. }
  18354. }
  18355. printf(testingFmt, "wc_ed25519_import_public()");
  18356. if (ret == 0) {
  18357. ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
  18358. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  18359. ret = 0;
  18360. } else {
  18361. ret = WOLFSSL_FATAL_ERROR;
  18362. }
  18363. /* Test bad args. */
  18364. if (ret == 0) {
  18365. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  18366. if (ret == BAD_FUNC_ARG) {
  18367. ret = wc_ed25519_import_public(in, inlen, NULL);
  18368. }
  18369. if (ret == BAD_FUNC_ARG) {
  18370. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  18371. }
  18372. if (ret == BAD_FUNC_ARG) {
  18373. ret = 0;
  18374. } else if (ret == 0) {
  18375. ret = WOLFSSL_FATAL_ERROR;
  18376. }
  18377. }
  18378. }
  18379. printf(resultFmt, ret == 0 ? passed : failed);
  18380. if (wc_FreeRng(&rng) && ret == 0) {
  18381. ret = WOLFSSL_FATAL_ERROR;
  18382. }
  18383. wc_ed25519_free(&pubKey);
  18384. #endif
  18385. return ret;
  18386. } /* END wc_ed25519_import_public */
  18387. /*
  18388. * Testing wc_ed25519_import_private_key()
  18389. */
  18390. static int test_wc_ed25519_import_private_key(void)
  18391. {
  18392. int ret = 0;
  18393. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18394. WC_RNG rng;
  18395. ed25519_key key;
  18396. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  18397. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  18398. word32 privKeySz = sizeof(privKey);
  18399. word32 pubKeySz = sizeof(pubKey);
  18400. #ifdef HAVE_ED25519_KEY_EXPORT
  18401. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  18402. word32 bothKeysSz = sizeof(bothKeys);
  18403. #endif
  18404. ret = wc_InitRng(&rng);
  18405. if (ret != 0) {
  18406. return ret;
  18407. }
  18408. ret = wc_ed25519_init(&key);
  18409. if (ret != 0) {
  18410. wc_FreeRng(&rng);
  18411. return ret;
  18412. }
  18413. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18414. printf(testingFmt, "wc_ed25519_import_private_key()");
  18415. if (ret == 0) {
  18416. ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  18417. pubKeySz, &key, 1);
  18418. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18419. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18420. ret = WOLFSSL_FATAL_ERROR;
  18421. }
  18422. }
  18423. #ifdef HAVE_ED25519_KEY_EXPORT
  18424. if (ret == 0)
  18425. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  18426. if (ret == 0) {
  18427. ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  18428. &key, 1);
  18429. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18430. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18431. ret = WOLFSSL_FATAL_ERROR;
  18432. }
  18433. }
  18434. #endif
  18435. /* Test bad args. */
  18436. if (ret == 0) {
  18437. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  18438. &key);
  18439. if (ret == BAD_FUNC_ARG) {
  18440. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18441. pubKeySz, &key);
  18442. }
  18443. if (ret == BAD_FUNC_ARG) {
  18444. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18445. pubKeySz, NULL);
  18446. }
  18447. if (ret == BAD_FUNC_ARG) {
  18448. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  18449. pubKeySz, &key);
  18450. }
  18451. if (ret == BAD_FUNC_ARG) {
  18452. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18453. pubKeySz - 1, &key);
  18454. }
  18455. if (ret == BAD_FUNC_ARG) {
  18456. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18457. 0, &key);
  18458. }
  18459. if (ret == BAD_FUNC_ARG) {
  18460. ret = 0;
  18461. } else if (ret == 0) {
  18462. ret = WOLFSSL_FATAL_ERROR;
  18463. }
  18464. }
  18465. printf(resultFmt, ret == 0 ? passed : failed);
  18466. if (wc_FreeRng(&rng) && ret == 0) {
  18467. ret = WOLFSSL_FATAL_ERROR;
  18468. }
  18469. wc_ed25519_free(&key);
  18470. #endif
  18471. return ret;
  18472. } /* END test_wc_ed25519_import_private_key */
  18473. /*
  18474. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  18475. */
  18476. static int test_wc_ed25519_export(void)
  18477. {
  18478. int ret = 0;
  18479. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  18480. WC_RNG rng;
  18481. ed25519_key key;
  18482. byte priv[ED25519_PRV_KEY_SIZE];
  18483. byte pub[ED25519_PUB_KEY_SIZE];
  18484. word32 privSz = sizeof(priv);
  18485. word32 pubSz = sizeof(pub);
  18486. ret = wc_InitRng(&rng);
  18487. if (ret != 0) {
  18488. return ret;
  18489. }
  18490. ret = wc_ed25519_init(&key);
  18491. if (ret != 0) {
  18492. wc_FreeRng(&rng);
  18493. return ret;
  18494. }
  18495. if (ret == 0) {
  18496. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18497. }
  18498. printf(testingFmt, "wc_ed25519_export_public()");
  18499. if (ret == 0) {
  18500. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  18501. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  18502. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  18503. ret = WOLFSSL_FATAL_ERROR;
  18504. }
  18505. if (ret == 0) {
  18506. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  18507. if (ret == BAD_FUNC_ARG) {
  18508. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  18509. }
  18510. if (ret == BAD_FUNC_ARG) {
  18511. ret = wc_ed25519_export_public(&key, pub, NULL);
  18512. }
  18513. if (ret == BAD_FUNC_ARG) {
  18514. ret = 0;
  18515. } else if (ret == 0) {
  18516. ret = WOLFSSL_FATAL_ERROR;
  18517. }
  18518. }
  18519. }
  18520. printf(resultFmt, ret == 0 ? passed : failed);
  18521. printf(testingFmt, "wc_ed25519_export_private_only()");
  18522. if (ret == 0) {
  18523. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  18524. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  18525. || XMEMCMP(key.k, priv, privSz) != 0)) {
  18526. ret = WOLFSSL_FATAL_ERROR;
  18527. }
  18528. if (ret == 0) {
  18529. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  18530. if (ret == BAD_FUNC_ARG) {
  18531. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  18532. }
  18533. if (ret == BAD_FUNC_ARG) {
  18534. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  18535. }
  18536. if (ret == BAD_FUNC_ARG) {
  18537. ret = 0;
  18538. } else if (ret == 0) {
  18539. ret = WOLFSSL_FATAL_ERROR;
  18540. }
  18541. }
  18542. }
  18543. printf(resultFmt, ret == 0 ? passed : failed);
  18544. if (wc_FreeRng(&rng) && ret == 0) {
  18545. ret = WOLFSSL_FATAL_ERROR;
  18546. }
  18547. wc_ed25519_free(&key);
  18548. #endif
  18549. return ret;
  18550. } /* END test_wc_ed25519_export */
  18551. /*
  18552. * Testing wc_ed25519_size()
  18553. */
  18554. static int test_wc_ed25519_size(void)
  18555. {
  18556. int ret = 0;
  18557. #if defined(HAVE_ED25519)
  18558. WC_RNG rng;
  18559. ed25519_key key;
  18560. ret = wc_InitRng(&rng);
  18561. if (ret != 0) {
  18562. return ret;
  18563. }
  18564. ret = wc_ed25519_init(&key);
  18565. if (ret != 0) {
  18566. wc_FreeRng(&rng);
  18567. return ret;
  18568. }
  18569. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18570. if (ret != 0) {
  18571. wc_FreeRng(&rng);
  18572. wc_ed25519_free(&key);
  18573. return ret;
  18574. }
  18575. printf(testingFmt, "wc_ed25519_size()");
  18576. ret = wc_ed25519_size(&key);
  18577. /* Test bad args. */
  18578. if (ret == ED25519_KEY_SIZE) {
  18579. ret = wc_ed25519_size(NULL);
  18580. if (ret == BAD_FUNC_ARG) {
  18581. ret = 0;
  18582. }
  18583. }
  18584. printf(resultFmt, ret == 0 ? passed : failed);
  18585. if (ret == 0) {
  18586. printf(testingFmt, "wc_ed25519_sig_size()");
  18587. ret = wc_ed25519_sig_size(&key);
  18588. if (ret == ED25519_SIG_SIZE) {
  18589. ret = 0;
  18590. }
  18591. /* Test bad args. */
  18592. if (ret == 0) {
  18593. ret = wc_ed25519_sig_size(NULL);
  18594. if (ret == BAD_FUNC_ARG) {
  18595. ret = 0;
  18596. }
  18597. }
  18598. printf(resultFmt, ret == 0 ? passed : failed);
  18599. } /* END wc_ed25519_sig_size() */
  18600. if (ret == 0) {
  18601. printf(testingFmt, "wc_ed25519_pub_size");
  18602. ret = wc_ed25519_pub_size(&key);
  18603. if (ret == ED25519_PUB_KEY_SIZE) {
  18604. ret = 0;
  18605. }
  18606. if (ret == 0) {
  18607. ret = wc_ed25519_pub_size(NULL);
  18608. if (ret == BAD_FUNC_ARG) {
  18609. ret = 0;
  18610. }
  18611. }
  18612. printf(resultFmt, ret == 0 ? passed : failed);
  18613. } /* END wc_ed25519_pub_size */
  18614. if (ret == 0) {
  18615. printf(testingFmt, "wc_ed25519_priv_size");
  18616. ret = wc_ed25519_priv_size(&key);
  18617. if (ret == ED25519_PRV_KEY_SIZE) {
  18618. ret = 0;
  18619. }
  18620. if (ret == 0) {
  18621. ret = wc_ed25519_priv_size(NULL);
  18622. if (ret == BAD_FUNC_ARG) {
  18623. ret = 0;
  18624. }
  18625. }
  18626. printf(resultFmt, ret == 0 ? passed : failed);
  18627. } /* END wc_ed25519_pub_size */
  18628. if (wc_FreeRng(&rng) && ret == 0) {
  18629. ret = WOLFSSL_FATAL_ERROR;
  18630. }
  18631. wc_ed25519_free(&key);
  18632. #endif
  18633. return ret;
  18634. } /* END test_wc_ed25519_size */
  18635. /*
  18636. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  18637. */
  18638. static int test_wc_ed25519_exportKey(void)
  18639. {
  18640. int ret = 0;
  18641. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  18642. WC_RNG rng;
  18643. ed25519_key key;
  18644. byte priv[ED25519_PRV_KEY_SIZE];
  18645. byte pub[ED25519_PUB_KEY_SIZE];
  18646. byte privOnly[ED25519_PRV_KEY_SIZE];
  18647. word32 privSz = sizeof(priv);
  18648. word32 pubSz = sizeof(pub);
  18649. word32 privOnlySz = sizeof(privOnly);
  18650. ret = wc_InitRng(&rng);
  18651. if (ret != 0) {
  18652. return ret;
  18653. }
  18654. ret = wc_ed25519_init(&key);
  18655. if (ret != 0) {
  18656. wc_FreeRng(&rng);
  18657. return ret;
  18658. }
  18659. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18660. if (ret != 0) {
  18661. wc_FreeRng(&rng);
  18662. wc_ed25519_free(&key);
  18663. return ret;
  18664. }
  18665. printf(testingFmt, "wc_ed25519_export_private()");
  18666. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  18667. if (ret == 0) {
  18668. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  18669. if (ret == BAD_FUNC_ARG) {
  18670. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  18671. }
  18672. if (ret == BAD_FUNC_ARG) {
  18673. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  18674. }
  18675. if (ret == BAD_FUNC_ARG) {
  18676. ret = 0;
  18677. } else if (ret == 0) {
  18678. ret = WOLFSSL_FATAL_ERROR;
  18679. }
  18680. }
  18681. printf(resultFmt, ret == 0 ? passed : failed);
  18682. if (ret == 0) {
  18683. printf(testingFmt, "wc_ed25519_export_key()");
  18684. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  18685. if (ret == 0) {
  18686. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  18687. if (ret == BAD_FUNC_ARG) {
  18688. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  18689. }
  18690. if (ret == BAD_FUNC_ARG) {
  18691. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  18692. }
  18693. if (ret == BAD_FUNC_ARG) {
  18694. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  18695. }
  18696. if (ret == BAD_FUNC_ARG) {
  18697. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  18698. }
  18699. if (ret == BAD_FUNC_ARG) {
  18700. ret = 0;
  18701. } else if (ret == 0) {
  18702. ret = WOLFSSL_FATAL_ERROR;
  18703. }
  18704. }
  18705. printf(resultFmt, ret == 0 ? passed : failed);
  18706. } /* END wc_ed25519_export_key() */
  18707. /* Cross check output. */
  18708. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  18709. ret = WOLFSSL_FATAL_ERROR;
  18710. }
  18711. if (wc_FreeRng(&rng) && ret == 0) {
  18712. ret = WOLFSSL_FATAL_ERROR;
  18713. }
  18714. wc_ed25519_free(&key);
  18715. #endif
  18716. return ret;
  18717. } /* END test_wc_ed25519_exportKey */
  18718. /*
  18719. * Testing wc_Ed25519PublicKeyToDer
  18720. */
  18721. static int test_wc_Ed25519PublicKeyToDer(void)
  18722. {
  18723. int ret = 0;
  18724. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  18725. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  18726. int tmp;
  18727. ed25519_key key;
  18728. byte derBuf[1024];
  18729. printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
  18730. /* Test bad args */
  18731. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  18732. if (tmp != BAD_FUNC_ARG) {
  18733. ret = WOLFSSL_FATAL_ERROR;
  18734. }
  18735. if (ret == 0) {
  18736. wc_ed25519_init(&key);
  18737. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  18738. if (tmp != BUFFER_E) {
  18739. ret = WOLFSSL_FATAL_ERROR;
  18740. }
  18741. wc_ed25519_free(&key);
  18742. }
  18743. /* Test good args */
  18744. if (ret == 0) {
  18745. WC_RNG rng;
  18746. ret = wc_InitRng(&rng);
  18747. if (ret != 0) {
  18748. return ret;
  18749. }
  18750. ret = wc_ed25519_init(&key);
  18751. if (ret != 0) {
  18752. wc_FreeRng(&rng);
  18753. return ret;
  18754. }
  18755. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18756. if (ret != 0) {
  18757. wc_FreeRng(&rng);
  18758. wc_ed25519_free(&key);
  18759. return ret;
  18760. }
  18761. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  18762. if (tmp <= 0) {
  18763. ret = WOLFSSL_FATAL_ERROR;
  18764. }
  18765. wc_FreeRng(&rng);
  18766. wc_ed25519_free(&key);
  18767. }
  18768. printf(resultFmt, ret == 0 ? passed : failed);
  18769. #endif
  18770. return ret;
  18771. } /* END testing wc_Ed25519PublicKeyToDer */
  18772. /*
  18773. * Testing wc_curve25519_init and wc_curve25519_free.
  18774. */
  18775. static int test_wc_curve25519_init(void)
  18776. {
  18777. int ret = 0;
  18778. #if defined(HAVE_CURVE25519)
  18779. curve25519_key key;
  18780. printf(testingFmt, "wc_curve25519_init()");
  18781. ret = wc_curve25519_init(&key);
  18782. /* Test bad args for wc_curve25519_init */
  18783. if (ret == 0) {
  18784. ret = wc_curve25519_init(NULL);
  18785. if (ret == BAD_FUNC_ARG) {
  18786. ret = 0;
  18787. } else if (ret == 0) {
  18788. ret = WOLFSSL_FATAL_ERROR;
  18789. }
  18790. }
  18791. printf(resultFmt, ret == 0 ? passed : failed);
  18792. /* Test good args for wc_curve_25519_free */
  18793. wc_curve25519_free(&key);
  18794. wc_curve25519_free(NULL);
  18795. #endif
  18796. return ret;
  18797. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  18798. /*
  18799. * Testing test_wc_curve25519_size.
  18800. */
  18801. static int test_wc_curve25519_size(void)
  18802. {
  18803. int ret = 0;
  18804. #if defined(HAVE_CURVE25519)
  18805. curve25519_key key;
  18806. printf(testingFmt, "wc_curve25519_size()");
  18807. ret = wc_curve25519_init(&key);
  18808. /* Test good args for wc_curve25519_size */
  18809. if (ret == 0) {
  18810. ret = wc_curve25519_size(&key);
  18811. }
  18812. /* Test bad args for wc_curve25519_size */
  18813. if (ret != 0) {
  18814. ret = wc_curve25519_size(NULL);
  18815. }
  18816. printf(resultFmt, ret == 0 ? passed : failed);
  18817. wc_curve25519_free(&key);
  18818. #endif
  18819. return ret;
  18820. } /* END test_wc_curve25519_size*/
  18821. /*
  18822. * Testing test_wc_curve25519_export_key_raw().
  18823. */
  18824. static int test_wc_curve25519_export_key_raw(void)
  18825. {
  18826. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  18827. curve25519_key key;
  18828. WC_RNG rng;
  18829. byte privateKey[CURVE25519_KEYSIZE];
  18830. byte publicKey[CURVE25519_KEYSIZE];
  18831. word32 prvkSz;
  18832. word32 pubkSz;
  18833. byte prik[CURVE25519_KEYSIZE];
  18834. byte pubk[CURVE25519_KEYSIZE];
  18835. word32 prksz;
  18836. word32 pbksz;
  18837. printf(testingFmt, "wc_curve25519_export_key_raw()");
  18838. if(0 != wc_InitRng(&rng)){
  18839. printf(testingFmt, "failed due to wc_InitRng");
  18840. fflush(stdout);
  18841. return 1;
  18842. }
  18843. if(0 != wc_curve25519_init(&key)){
  18844. printf(testingFmt, "failed due to wc_curve25519_init");
  18845. fflush(stdout);
  18846. wc_FreeRng(&rng);
  18847. return 1;
  18848. }
  18849. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  18850. printf(testingFmt, "failed due to wc_curve25519_make_key");
  18851. fflush(stdout);
  18852. wc_curve25519_free(&key);
  18853. wc_FreeRng(&rng);
  18854. return 1;
  18855. }
  18856. /*
  18857. bad-argument-test cases
  18858. target function sould return BAD_FUNC_ARG
  18859. */
  18860. prvkSz = CURVE25519_KEYSIZE;
  18861. pubkSz = CURVE25519_KEYSIZE;
  18862. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18863. NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
  18864. printf(testingFmt,"failed at bad-arg-case-1.");
  18865. fflush(stdout);
  18866. wc_curve25519_free(&key);
  18867. wc_FreeRng(&rng);
  18868. return 1;
  18869. }
  18870. prvkSz = CURVE25519_KEYSIZE;
  18871. pubkSz = CURVE25519_KEYSIZE;
  18872. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18873. &key , NULL, &prvkSz, publicKey, &pubkSz)){
  18874. printf(testingFmt,"failed at bad-arg-case-2.");
  18875. fflush(stdout);
  18876. wc_curve25519_free(&key);
  18877. wc_FreeRng(&rng);
  18878. return 1;
  18879. }
  18880. prvkSz = CURVE25519_KEYSIZE;
  18881. pubkSz = CURVE25519_KEYSIZE;
  18882. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18883. &key , privateKey, NULL, publicKey, &pubkSz)){
  18884. printf(testingFmt,"failed at bad-arg-case-3.");
  18885. fflush(stdout);
  18886. wc_curve25519_free(&key);
  18887. wc_FreeRng(&rng);
  18888. return 1;
  18889. }
  18890. /* prvkSz = CURVE25519_KEYSIZE; */
  18891. pubkSz = CURVE25519_KEYSIZE;
  18892. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18893. &key , privateKey, &prvkSz, NULL, &pubkSz)){
  18894. printf(testingFmt,"failed at bad-arg-case-4.");
  18895. fflush(stdout);
  18896. wc_curve25519_free(&key);
  18897. wc_FreeRng(&rng);
  18898. return 1;
  18899. }
  18900. prvkSz = CURVE25519_KEYSIZE;
  18901. pubkSz = CURVE25519_KEYSIZE;
  18902. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18903. &key , privateKey, &prvkSz, publicKey, NULL )){
  18904. printf(testingFmt,"failed at bad-arg-case-5.");
  18905. fflush(stdout);
  18906. wc_curve25519_free(&key);
  18907. wc_FreeRng(&rng);
  18908. return 1;
  18909. }
  18910. /*
  18911. cross-testing
  18912. */
  18913. prksz = CURVE25519_KEYSIZE;
  18914. if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
  18915. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  18916. fflush(stdout);
  18917. wc_curve25519_free(&key);
  18918. wc_FreeRng(&rng);
  18919. return 1;
  18920. }
  18921. pbksz = CURVE25519_KEYSIZE;
  18922. if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
  18923. printf(testingFmt,"failed due to wc_curve25519_export_public");
  18924. fflush(stdout);
  18925. wc_curve25519_free(&key);
  18926. wc_FreeRng(&rng);
  18927. return 1;
  18928. }
  18929. prvkSz = CURVE25519_KEYSIZE;
  18930. /* pubkSz = CURVE25519_KEYSIZE; */
  18931. if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  18932. publicKey, &pubkSz)){
  18933. printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
  18934. fflush(stdout);
  18935. wc_curve25519_free(&key);
  18936. wc_FreeRng(&rng);
  18937. return 1;
  18938. }
  18939. if((prksz == CURVE25519_KEYSIZE) &&
  18940. (pbksz == CURVE25519_KEYSIZE) &&
  18941. (prvkSz == CURVE25519_KEYSIZE) &&
  18942. (pubkSz == CURVE25519_KEYSIZE)){
  18943. if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) &&
  18944. 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){
  18945. printf(resultFmt,passed);
  18946. fflush(stdout);
  18947. wc_curve25519_free(&key);
  18948. wc_FreeRng(&rng);
  18949. return 0;
  18950. }
  18951. else{
  18952. printf(testingFmt,"failed due to key-contents-inconsistency.");
  18953. fflush(stdout);
  18954. wc_curve25519_free(&key);
  18955. wc_FreeRng(&rng);
  18956. return 1;
  18957. }
  18958. }
  18959. else{
  18960. printf(testingFmt,"failed due to bad-key-size.");
  18961. fflush(stdout);
  18962. wc_curve25519_free(&key);
  18963. wc_FreeRng(&rng);
  18964. return 1;
  18965. }
  18966. #endif
  18967. fflush(stdout);
  18968. return 0;
  18969. } /* end of test_wc_curve25519_export_key_raw */
  18970. /*
  18971. * Testing test_wc_curve25519_export_key_raw_ex().
  18972. */
  18973. static int test_wc_curve25519_export_key_raw_ex(void)
  18974. {
  18975. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  18976. curve25519_key key;
  18977. WC_RNG rng;
  18978. byte privateKey[CURVE25519_KEYSIZE];
  18979. byte publicKey[CURVE25519_KEYSIZE];
  18980. word32 prvkSz;
  18981. word32 pubkSz;
  18982. byte prik[CURVE25519_KEYSIZE];
  18983. byte pubk[CURVE25519_KEYSIZE];
  18984. word32 prksz;
  18985. word32 pbksz;
  18986. printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
  18987. if(0 != wc_InitRng(&rng)){
  18988. printf(testingFmt, "failed due to wc_InitRng");
  18989. fflush(stdout);
  18990. return 1;
  18991. }
  18992. if(0 != wc_curve25519_init(&key)){
  18993. printf(testingFmt, "failed due to wc_curve25519_init");
  18994. fflush(stdout);
  18995. wc_FreeRng(&rng);
  18996. return 1;
  18997. }
  18998. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  18999. printf(testingFmt, "failed due to wc_curve25519_make_key");
  19000. fflush(stdout);
  19001. wc_curve25519_free(&key);
  19002. wc_FreeRng(&rng);
  19003. return 1;
  19004. }
  19005. /*
  19006. bad-argument-test cases
  19007. target function sould return BAD_FUNC_ARG
  19008. */
  19009. prvkSz = CURVE25519_KEYSIZE;
  19010. pubkSz = CURVE25519_KEYSIZE;
  19011. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  19012. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19013. printf(testingFmt,"failed at bad-arg-case-1.");
  19014. fflush(stdout);
  19015. wc_curve25519_free(&key);
  19016. wc_FreeRng(&rng);
  19017. return 1;
  19018. }
  19019. prvkSz = CURVE25519_KEYSIZE;
  19020. pubkSz = CURVE25519_KEYSIZE;
  19021. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  19022. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19023. printf(testingFmt,"failed at bad-arg-case-2.");
  19024. fflush(stdout);
  19025. wc_curve25519_free(&key);
  19026. wc_FreeRng(&rng);
  19027. return 1;
  19028. }
  19029. prvkSz = CURVE25519_KEYSIZE;
  19030. pubkSz = CURVE25519_KEYSIZE;
  19031. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  19032. NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
  19033. printf(testingFmt,"failed at bad-arg-case-3.");
  19034. fflush(stdout);
  19035. wc_curve25519_free(&key);
  19036. wc_FreeRng(&rng);
  19037. return 1;
  19038. }
  19039. /* prvkSz = CURVE25519_KEYSIZE; */
  19040. pubkSz = CURVE25519_KEYSIZE;
  19041. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19042. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19043. printf(testingFmt,"failed at bad-arg-case-4.");
  19044. fflush(stdout);
  19045. wc_curve25519_free(&key);
  19046. wc_FreeRng(&rng);
  19047. return 1;
  19048. }
  19049. prvkSz = CURVE25519_KEYSIZE;
  19050. pubkSz = CURVE25519_KEYSIZE;
  19051. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19052. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
  19053. printf(testingFmt,"failed at bad-arg-case-5.");
  19054. fflush(stdout);
  19055. wc_curve25519_free(&key);
  19056. wc_FreeRng(&rng);
  19057. return 1;
  19058. }
  19059. prvkSz = CURVE25519_KEYSIZE;
  19060. /* pubkSz = CURVE25519_KEYSIZE; */
  19061. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  19062. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19063. printf(testingFmt,"failed at bad-arg-case-6.");
  19064. fflush(stdout);
  19065. wc_curve25519_free(&key);
  19066. wc_FreeRng(&rng);
  19067. return 1;
  19068. }
  19069. prvkSz = CURVE25519_KEYSIZE;
  19070. pubkSz = CURVE25519_KEYSIZE;
  19071. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
  19072. publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19073. printf(testingFmt,"failed at bad-arg-case-7.");
  19074. fflush(stdout);
  19075. wc_curve25519_free(&key);
  19076. wc_FreeRng(&rng);
  19077. return 1;
  19078. }
  19079. prvkSz = CURVE25519_KEYSIZE;
  19080. pubkSz = CURVE25519_KEYSIZE;
  19081. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19082. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19083. printf(testingFmt,"failed at bad-arg-case-8.");
  19084. fflush(stdout);
  19085. wc_curve25519_free(&key);
  19086. wc_FreeRng(&rng);
  19087. return 1;
  19088. }
  19089. /* prvkSz = CURVE25519_KEYSIZE; */
  19090. pubkSz = CURVE25519_KEYSIZE;
  19091. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19092. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
  19093. printf(testingFmt,"failed at bad-arg-case-9.");
  19094. fflush(stdout);
  19095. wc_curve25519_free(&key);
  19096. wc_FreeRng(&rng);
  19097. return 1;
  19098. }
  19099. prvkSz = CURVE25519_KEYSIZE;
  19100. pubkSz = CURVE25519_KEYSIZE;
  19101. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19102. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
  19103. printf(testingFmt,"failed at bad-arg-case-10.");
  19104. fflush(stdout);
  19105. wc_curve25519_free(&key);
  19106. wc_FreeRng(&rng);
  19107. return 1;
  19108. }
  19109. /* illegal value for endien */
  19110. prvkSz = CURVE25519_KEYSIZE;
  19111. /* pubkSz = CURVE25519_KEYSIZE; */
  19112. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19113. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){
  19114. printf(testingFmt,"failed at bad-arg-case-11.");
  19115. fflush(stdout);
  19116. wc_curve25519_free(&key);
  19117. wc_FreeRng(&rng);
  19118. return 1;
  19119. }
  19120. /*
  19121. cross-testing
  19122. */
  19123. prksz = CURVE25519_KEYSIZE;
  19124. if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
  19125. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  19126. fflush(stdout);
  19127. wc_curve25519_free(&key);
  19128. wc_FreeRng(&rng);
  19129. return 1;
  19130. }
  19131. pbksz = CURVE25519_KEYSIZE;
  19132. if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
  19133. printf(testingFmt,"failed due to wc_curve25519_export_public");
  19134. fflush(stdout);
  19135. wc_curve25519_free(&key);
  19136. wc_FreeRng(&rng);
  19137. return 1;
  19138. }
  19139. prvkSz = CURVE25519_KEYSIZE;
  19140. /* pubkSz = CURVE25519_KEYSIZE; */
  19141. if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19142. publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19143. printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
  19144. fflush(stdout);
  19145. wc_curve25519_free(&key);
  19146. wc_FreeRng(&rng);
  19147. return 1;
  19148. }
  19149. if( prksz == CURVE25519_KEYSIZE &&
  19150. pbksz == CURVE25519_KEYSIZE &&
  19151. prvkSz == CURVE25519_KEYSIZE &&
  19152. pubkSz == CURVE25519_KEYSIZE ){
  19153. if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) &&
  19154. 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){
  19155. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  19156. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19157. if( prvkSz == CURVE25519_KEYSIZE &&
  19158. pubkSz == CURVE25519_KEYSIZE ){
  19159. ; /* proceed to the next test */
  19160. }
  19161. else{
  19162. printf(testingFmt,"failed due to key-size-inconsistency");
  19163. fflush(stdout);
  19164. wc_curve25519_free(&key);
  19165. wc_FreeRng(&rng);
  19166. return 1;
  19167. }
  19168. }
  19169. else{
  19170. printf(testingFmt,
  19171. "failed due to wc_curve25519_export_key_raw_ex");
  19172. fflush(stdout);
  19173. wc_curve25519_free(&key);
  19174. wc_FreeRng(&rng);
  19175. return 1;
  19176. }
  19177. }
  19178. else{
  19179. printf(testingFmt,"failed due to key-contents-inconsistency");
  19180. fflush(stdout);
  19181. wc_curve25519_free(&key);
  19182. wc_FreeRng(&rng);
  19183. return 1;
  19184. }
  19185. }
  19186. else{
  19187. printf(testingFmt,"failed due to bad-key-size");
  19188. fflush(stdout);
  19189. wc_curve25519_free(&key);
  19190. wc_FreeRng(&rng);
  19191. return 1;
  19192. }
  19193. /*
  19194. try once with another endian
  19195. */
  19196. prvkSz = CURVE25519_KEYSIZE;
  19197. pubkSz = CURVE25519_KEYSIZE;
  19198. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  19199. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19200. if( prvkSz == CURVE25519_KEYSIZE &&
  19201. pubkSz == CURVE25519_KEYSIZE ){
  19202. /* no more test*/
  19203. printf(resultFmt, passed );
  19204. fflush(stdout);
  19205. wc_curve25519_free(&key);
  19206. wc_FreeRng(&rng);
  19207. return 0;
  19208. }
  19209. else{
  19210. printf(testingFmt,"failed due to key-size-inconsistency");
  19211. fflush(stdout);
  19212. wc_curve25519_free(&key);
  19213. wc_FreeRng(&rng);
  19214. return 1;
  19215. }
  19216. }
  19217. else{
  19218. printf(testingFmt,
  19219. "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
  19220. fflush(stdout);
  19221. wc_curve25519_free(&key);
  19222. wc_FreeRng(&rng);
  19223. return 1;
  19224. }
  19225. #else
  19226. return 0;
  19227. #endif
  19228. } /* end of test_wc_curve25519_export_key_raw_ex */
  19229. /*
  19230. * Testing wc_curve25519_make_key
  19231. */
  19232. static int test_wc_curve25519_make_key(void)
  19233. {
  19234. int ret = 0;
  19235. #if defined(HAVE_CURVE25519)
  19236. WC_RNG rng;
  19237. curve25519_key key;
  19238. int keysize;
  19239. printf(testingFmt, "wc_curve25519_make_key()");
  19240. ret = wc_curve25519_init(&key);
  19241. if (ret == 0) {
  19242. ret = wc_InitRng(&rng);
  19243. }
  19244. if (ret == 0) {
  19245. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19246. if (ret == 0) {
  19247. keysize = wc_curve25519_size(&key);
  19248. if (keysize != CURVE25519_KEYSIZE) {
  19249. ret = WOLFSSL_FATAL_ERROR;
  19250. }
  19251. }
  19252. if (ret == 0) {
  19253. ret = wc_curve25519_make_key(&rng, keysize, &key);
  19254. }
  19255. }
  19256. /*test bad cases*/
  19257. if (ret == 0) {
  19258. ret = wc_curve25519_make_key(NULL, 0, NULL);
  19259. if (ret == BAD_FUNC_ARG) {
  19260. ret = 0;
  19261. }
  19262. }
  19263. if (ret == 0) {
  19264. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  19265. if (ret == BAD_FUNC_ARG) {
  19266. ret = 0;
  19267. }
  19268. }
  19269. if (ret == 0) {
  19270. ret = wc_curve25519_make_key(NULL, keysize, &key);
  19271. if (ret == BAD_FUNC_ARG) {
  19272. ret = 0;
  19273. }
  19274. }
  19275. if (ret == 0) {
  19276. ret = wc_curve25519_make_key(&rng, 0, &key);
  19277. if (ret == ECC_BAD_ARG_E) {
  19278. ret = 0;
  19279. }
  19280. }
  19281. printf(resultFmt, ret == 0 ? passed : failed);
  19282. wc_curve25519_free(&key);
  19283. wc_FreeRng(&rng);
  19284. #endif
  19285. return ret;
  19286. } /*END test_wc_curve25519_make_key*/
  19287. /*
  19288. * Testing wc_curve25519_shared_secret_ex
  19289. */
  19290. static int test_wc_curve25519_shared_secret_ex(void)
  19291. {
  19292. int ret = 0;
  19293. #if defined(HAVE_CURVE25519)
  19294. WC_RNG rng;
  19295. curve25519_key private_key, public_key;
  19296. byte out[CURVE25519_KEYSIZE];
  19297. word32 outLen = sizeof(out);
  19298. int endian = EC25519_BIG_ENDIAN;
  19299. printf(testingFmt, "wc_curve25519_shared_secret_ex()");
  19300. ret = wc_curve25519_init(&private_key);
  19301. if (ret == 0) {
  19302. ret = wc_curve25519_init(&public_key);
  19303. }
  19304. if (ret == 0) {
  19305. ret = wc_InitRng(&rng);
  19306. }
  19307. if (ret == 0) {
  19308. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  19309. }
  19310. if (ret == 0) {
  19311. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  19312. }
  19313. if (ret == 0) {
  19314. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19315. &outLen, endian);
  19316. }
  19317. /*test bad cases*/
  19318. if (ret == 0) {
  19319. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  19320. 0, endian);
  19321. if (ret == 0) {
  19322. ret = -1;
  19323. }
  19324. if (ret == BAD_FUNC_ARG) {
  19325. ret = 0;
  19326. }
  19327. }
  19328. if (ret == 0) {
  19329. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  19330. &outLen, endian);
  19331. if (ret == 0) {
  19332. ret = -1;
  19333. }
  19334. else if (ret == BAD_FUNC_ARG) {
  19335. ret = 0;
  19336. }
  19337. }
  19338. if (ret == 0) {
  19339. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  19340. &outLen, endian);
  19341. if (ret == 0) {
  19342. ret = -1;
  19343. }
  19344. else if (ret == BAD_FUNC_ARG) {
  19345. ret = 0;
  19346. }
  19347. }
  19348. if (ret == 0) {
  19349. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19350. &outLen, endian);
  19351. if (ret == 0) {
  19352. ret = -1;
  19353. }
  19354. else if (ret == BAD_FUNC_ARG) {
  19355. ret = 0;
  19356. }
  19357. }
  19358. if (ret == 0) {
  19359. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19360. NULL, endian);
  19361. if (ret == 0) {
  19362. ret = -1;
  19363. }
  19364. else if (ret == BAD_FUNC_ARG) {
  19365. ret = 0;
  19366. }
  19367. }
  19368. if (ret == 0) {
  19369. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  19370. *increasing to 0x8f checks for error being returned*/
  19371. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  19372. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19373. &outLen, endian);
  19374. if (ret == 0) {
  19375. ret = -1;
  19376. }
  19377. else if (ret == ECC_BAD_ARG_E) {
  19378. ret = 0;
  19379. }
  19380. }
  19381. outLen = outLen - 2;
  19382. if (ret == 0) {
  19383. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19384. &outLen, endian);
  19385. if (ret == 0) {
  19386. ret = -1;
  19387. }
  19388. else if (ret == BAD_FUNC_ARG) {
  19389. ret = 0;
  19390. }
  19391. }
  19392. printf(resultFmt, ret == 0 ? passed : failed);
  19393. fflush(stdout);
  19394. wc_curve25519_free(&private_key);
  19395. wc_curve25519_free(&public_key);
  19396. wc_FreeRng(&rng);
  19397. #endif
  19398. return ret;
  19399. } /*END test_wc_curve25519_shared_secret_ex*/
  19400. /*
  19401. * Testing wc_curve25519_make_pub
  19402. */
  19403. static int test_wc_curve25519_make_pub(void)
  19404. {
  19405. int ret = 0;
  19406. #ifdef HAVE_CURVE25519
  19407. WC_RNG rng;
  19408. curve25519_key key;
  19409. byte out[CURVE25519_KEYSIZE];
  19410. printf(testingFmt, "wc_curve25519_make_pub()");
  19411. ret = wc_curve25519_init(&key);
  19412. if (ret == 0) {
  19413. ret = wc_InitRng(&rng);
  19414. if (ret == 0) {
  19415. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19416. }
  19417. }
  19418. if (ret == 0) {
  19419. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  19420. }
  19421. /*test bad cases*/
  19422. if (ret == 0) {
  19423. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  19424. if (ret == ECC_BAD_ARG_E) {
  19425. ret = 0;
  19426. }
  19427. }
  19428. if (ret == 0) {
  19429. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  19430. if (ret == ECC_BAD_ARG_E) {
  19431. ret = 0;
  19432. }
  19433. }
  19434. if (ret == 0) {
  19435. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  19436. if (ret == ECC_BAD_ARG_E) {
  19437. ret = 0;
  19438. }
  19439. }
  19440. if (ret == 0) {
  19441. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  19442. if (ret == ECC_BAD_ARG_E) {
  19443. ret = 0;
  19444. }
  19445. }
  19446. if (ret == 0) {
  19447. /* verify clamping test */
  19448. key.k[0] |= ~248;
  19449. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19450. if (ret == ECC_BAD_ARG_E) {
  19451. ret = 0;
  19452. }
  19453. key.k[0] &= 248;
  19454. }
  19455. /* repeat the expected-to-succeed test. */
  19456. if (ret == 0) {
  19457. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19458. }
  19459. printf(resultFmt, ret == 0 ? passed : failed);
  19460. fflush(stdout);
  19461. wc_curve25519_free(&key);
  19462. wc_FreeRng(&rng);
  19463. #endif
  19464. return ret;
  19465. } /*END test_wc_curve25519_make_pub */
  19466. /*
  19467. * Testing test_wc_curve25519_export_public_ex
  19468. */
  19469. static int test_wc_curve25519_export_public_ex(void)
  19470. {
  19471. int ret = 0;
  19472. #if defined(HAVE_CURVE25519)
  19473. WC_RNG rng;
  19474. curve25519_key key;
  19475. byte out[CURVE25519_KEYSIZE];
  19476. word32 outLen = sizeof(out);
  19477. int endian = EC25519_BIG_ENDIAN;
  19478. printf(testingFmt, "wc_curve25519_export_public_ex()");
  19479. ret = wc_curve25519_init(&key);
  19480. if (ret == 0) {
  19481. ret = wc_InitRng(&rng);
  19482. }
  19483. if (ret == 0) {
  19484. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19485. if (ret == 0) {
  19486. ret = wc_curve25519_export_public(&key, out, &outLen);
  19487. }
  19488. if (ret == 0) {
  19489. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  19490. }
  19491. }
  19492. /*test bad cases*/
  19493. if (ret == 0) {
  19494. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  19495. if (ret == BAD_FUNC_ARG) {
  19496. ret = 0;
  19497. }
  19498. }
  19499. if (ret == 0) {
  19500. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  19501. if (ret == BAD_FUNC_ARG) {
  19502. ret = 0;
  19503. }
  19504. }
  19505. if (ret == 0) {
  19506. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  19507. if (ret == BAD_FUNC_ARG) {
  19508. ret = 0;
  19509. }
  19510. }
  19511. if (ret == 0) {
  19512. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  19513. if (ret == BAD_FUNC_ARG) {
  19514. ret = 0;
  19515. }
  19516. }
  19517. outLen = outLen - 2;
  19518. if (ret == 0) {
  19519. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  19520. if (ret == ECC_BAD_ARG_E) {
  19521. ret = 0;
  19522. }
  19523. }
  19524. printf(resultFmt, ret == 0 ? passed : failed);
  19525. fflush(stdout);
  19526. wc_curve25519_free(&key);
  19527. wc_FreeRng(&rng);
  19528. #endif
  19529. return ret;
  19530. } /*END test_wc_curve25519_export_public_ex*/
  19531. /*
  19532. * Testing test_wc_curve25519_import_private_raw_ex
  19533. */
  19534. static int test_wc_curve25519_import_private_raw_ex(void)
  19535. {
  19536. int ret = 0;
  19537. #if defined(HAVE_CURVE25519)
  19538. WC_RNG rng;
  19539. curve25519_key key;
  19540. byte priv[CURVE25519_KEYSIZE];
  19541. byte pub[CURVE25519_KEYSIZE];
  19542. word32 privSz = sizeof(priv);
  19543. word32 pubSz = sizeof(pub);
  19544. int endian = EC25519_BIG_ENDIAN;
  19545. printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
  19546. ret = wc_curve25519_init(&key);
  19547. if (ret == 0) {
  19548. ret = wc_InitRng(&rng);
  19549. }
  19550. if (ret == 0) {
  19551. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19552. if (ret == 0) {
  19553. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  19554. }
  19555. if (ret == 0) {
  19556. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  19557. }
  19558. if (ret == 0) {
  19559. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19560. &key, endian);
  19561. }
  19562. }
  19563. /*test bad cases*/
  19564. if (ret == 0) {
  19565. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  19566. endian);
  19567. if (ret == BAD_FUNC_ARG) {
  19568. ret = 0;
  19569. }
  19570. }
  19571. if (ret == 0) {
  19572. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  19573. &key, endian);
  19574. if (ret == BAD_FUNC_ARG) {
  19575. ret = 0;
  19576. }
  19577. }
  19578. if (ret == 0) {
  19579. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  19580. &key, endian);
  19581. if (ret == BAD_FUNC_ARG) {
  19582. ret = 0;
  19583. }
  19584. }
  19585. if (ret == 0) {
  19586. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19587. NULL, endian);
  19588. if (ret == BAD_FUNC_ARG) {
  19589. ret = 0;
  19590. }
  19591. }
  19592. if (ret == 0) {
  19593. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  19594. &key, endian);
  19595. if (ret == ECC_BAD_ARG_E) {
  19596. ret = 0;
  19597. }
  19598. }
  19599. if (ret == 0) {
  19600. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  19601. &key, endian);
  19602. if (ret == ECC_BAD_ARG_E) {
  19603. ret = 0;
  19604. }
  19605. }
  19606. if (ret == 0) {
  19607. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19608. &key, EC25519_LITTLE_ENDIAN);
  19609. }
  19610. printf(resultFmt, ret == 0 ? passed : failed);
  19611. fflush(stdout);
  19612. wc_curve25519_free(&key);
  19613. wc_FreeRng(&rng);
  19614. #endif
  19615. return ret;
  19616. } /*END test_wc_curve25519_import_private_raw_ex*/
  19617. /*
  19618. * Testing test_wc_curve25519_import_private
  19619. */
  19620. static int test_wc_curve25519_import_private(void)
  19621. {
  19622. int ret = 0;
  19623. #if defined(HAVE_CURVE25519)
  19624. curve25519_key key;
  19625. WC_RNG rng;
  19626. byte priv[CURVE25519_KEYSIZE];
  19627. word32 privSz = sizeof(priv);
  19628. printf(testingFmt, "wc_curve25519_import_private()");
  19629. ret = wc_curve25519_init(&key);
  19630. if (ret == 0) {
  19631. ret = wc_InitRng(&rng);
  19632. }
  19633. if (ret == 0) {
  19634. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19635. if (ret == 0) {
  19636. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  19637. }
  19638. }
  19639. if (ret == 0) {
  19640. ret = wc_curve25519_import_private(priv, privSz, &key);
  19641. }
  19642. printf(resultFmt, ret == 0 ? passed : failed);
  19643. fflush(stdout);
  19644. wc_curve25519_free(&key);
  19645. wc_FreeRng(&rng);
  19646. #endif
  19647. return ret;
  19648. } /*END test_wc_curve25519_import*/
  19649. /*
  19650. * Testing test_wc_curve25519_export_private_raw_ex
  19651. */
  19652. static int test_wc_curve25519_export_private_raw_ex(void)
  19653. {
  19654. int ret = 0;
  19655. #if defined(HAVE_CURVE25519)
  19656. curve25519_key key;
  19657. byte out[CURVE25519_KEYSIZE];
  19658. word32 outLen = sizeof(out);
  19659. int endian = EC25519_BIG_ENDIAN;
  19660. printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
  19661. ret = wc_curve25519_init(&key);
  19662. if (ret == 0) {
  19663. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  19664. }
  19665. /*test bad cases*/
  19666. if (ret == 0) {
  19667. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  19668. if (ret == BAD_FUNC_ARG) {
  19669. ret = 0;
  19670. }
  19671. }
  19672. if (ret == 0) {
  19673. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  19674. if (ret == BAD_FUNC_ARG) {
  19675. ret = 0;
  19676. }
  19677. }
  19678. if (ret == 0) {
  19679. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  19680. if (ret == BAD_FUNC_ARG) {
  19681. ret = 0;
  19682. }
  19683. }
  19684. if (ret == 0) {
  19685. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  19686. if (ret == BAD_FUNC_ARG) {
  19687. ret = 0;
  19688. }
  19689. }
  19690. if (ret == 0) {
  19691. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  19692. EC25519_LITTLE_ENDIAN);
  19693. }
  19694. outLen = outLen - 2;
  19695. if (ret == 0) {
  19696. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  19697. if (ret == ECC_BAD_ARG_E) {
  19698. ret = 0;
  19699. }
  19700. }
  19701. printf(resultFmt, ret == 0 ? passed : failed);
  19702. fflush(stdout);
  19703. wc_curve25519_free(&key);
  19704. #endif
  19705. return ret;
  19706. }/*END test_wc_curve25519_export_private_raw_ex*/
  19707. /*
  19708. * Testing wc_ed448_make_key().
  19709. */
  19710. static int test_wc_ed448_make_key(void)
  19711. {
  19712. int ret = 0;
  19713. #if defined(HAVE_ED448)
  19714. ed448_key key;
  19715. WC_RNG rng;
  19716. ret = wc_InitRng(&rng);
  19717. if (ret == 0) {
  19718. ret = wc_ed448_init(&key);
  19719. }
  19720. printf(testingFmt, "wc_ed448_make_key()");
  19721. if (ret == 0) {
  19722. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19723. }
  19724. /* Test bad args. */
  19725. if (ret == 0) {
  19726. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  19727. if (ret == BAD_FUNC_ARG) {
  19728. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  19729. }
  19730. if (ret == BAD_FUNC_ARG) {
  19731. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  19732. }
  19733. if (ret == BAD_FUNC_ARG) {
  19734. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  19735. }
  19736. if (ret == BAD_FUNC_ARG) {
  19737. ret = 0;
  19738. } else if (ret == 0) {
  19739. ret = WOLFSSL_FATAL_ERROR;
  19740. }
  19741. }
  19742. printf(resultFmt, ret == 0 ? passed : failed);
  19743. fflush(stdout);
  19744. if (wc_FreeRng(&rng) && ret == 0) {
  19745. ret = WOLFSSL_FATAL_ERROR;
  19746. }
  19747. wc_ed448_free(&key);
  19748. #endif
  19749. return ret;
  19750. } /* END test_wc_ed448_make_key */
  19751. /*
  19752. * Testing wc_ed448_init()
  19753. */
  19754. static int test_wc_ed448_init(void)
  19755. {
  19756. int ret = 0;
  19757. #if defined(HAVE_ED448)
  19758. ed448_key key;
  19759. printf(testingFmt, "wc_ed448_init()");
  19760. ret = wc_ed448_init(&key);
  19761. /* Test bad args. */
  19762. if (ret == 0) {
  19763. ret = wc_ed448_init(NULL);
  19764. if (ret == BAD_FUNC_ARG) {
  19765. ret = 0;
  19766. } else if (ret == 0) {
  19767. ret = WOLFSSL_FATAL_ERROR;
  19768. }
  19769. }
  19770. printf(resultFmt, ret == 0 ? passed : failed);
  19771. fflush(stdout);
  19772. wc_ed448_free(&key);
  19773. #endif
  19774. return ret;
  19775. } /* END test_wc_ed448_init */
  19776. /*
  19777. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  19778. */
  19779. static int test_wc_ed448_sign_msg(void)
  19780. {
  19781. int ret = 0;
  19782. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  19783. WC_RNG rng;
  19784. ed448_key key;
  19785. byte msg[] = "Everybody gets Friday off.\n";
  19786. byte sig[ED448_SIG_SIZE];
  19787. word32 msglen = sizeof(msg);
  19788. word32 siglen = sizeof(sig);
  19789. word32 badSigLen = sizeof(sig) - 1;
  19790. #ifdef HAVE_ED448_VERIFY
  19791. int verify_ok = 0; /*1 = Verify success.*/
  19792. #endif
  19793. /* Initialize stack variables. */
  19794. XMEMSET(sig, 0, siglen);
  19795. /* Initialize key. */
  19796. ret = wc_InitRng(&rng);
  19797. if (ret == 0) {
  19798. ret = wc_ed448_init(&key);
  19799. if (ret == 0) {
  19800. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19801. }
  19802. }
  19803. printf(testingFmt, "wc_ed448_sign_msg()");
  19804. if (ret == 0) {
  19805. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  19806. }
  19807. /* Test bad args. */
  19808. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  19809. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  19810. if (ret == BAD_FUNC_ARG) {
  19811. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  19812. }
  19813. if (ret == BAD_FUNC_ARG) {
  19814. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  19815. }
  19816. if (ret == BAD_FUNC_ARG) {
  19817. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  19818. }
  19819. if (ret == BAD_FUNC_ARG) {
  19820. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  19821. NULL, 0);
  19822. }
  19823. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  19824. badSigLen -= 1;
  19825. ret = 0;
  19826. } else if (ret == 0) {
  19827. ret = WOLFSSL_FATAL_ERROR;
  19828. }
  19829. } /* END sign */
  19830. printf(resultFmt, ret == 0 ? passed : failed);
  19831. fflush(stdout);
  19832. #ifdef HAVE_ED448_VERIFY
  19833. printf(testingFmt, "wc_ed448_verify_msg()");
  19834. if (ret == 0) {
  19835. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  19836. &key, NULL, 0);
  19837. if (ret == 0 && verify_ok == 1) {
  19838. ret = 0;
  19839. } else if (ret == 0) {
  19840. ret = WOLFSSL_FATAL_ERROR;
  19841. }
  19842. /* Test bad args. */
  19843. if (ret == 0) {
  19844. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  19845. msglen, &verify_ok, &key,
  19846. NULL, 0),
  19847. BAD_FUNC_ARG);
  19848. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  19849. msglen, &verify_ok, &key,
  19850. NULL, 0),
  19851. BAD_FUNC_ARG);
  19852. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19853. &key, NULL, 0);
  19854. if (ret == BAD_FUNC_ARG) {
  19855. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  19856. &verify_ok, &key, NULL, 0);
  19857. }
  19858. if (ret == BAD_FUNC_ARG) {
  19859. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  19860. NULL, &key, NULL, 0);
  19861. }
  19862. if (ret == BAD_FUNC_ARG) {
  19863. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  19864. &verify_ok, NULL, NULL, 0);
  19865. }
  19866. if (ret == BAD_FUNC_ARG) {
  19867. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  19868. &verify_ok, &key, NULL, 0);
  19869. }
  19870. if (ret == BAD_FUNC_ARG) {
  19871. ret = 0;
  19872. } else if (ret == 0) {
  19873. ret = WOLFSSL_FATAL_ERROR;
  19874. }
  19875. }
  19876. } /* END verify. */
  19877. printf(resultFmt, ret == 0 ? passed : failed);
  19878. fflush(stdout);
  19879. #endif /* Verify. */
  19880. if (wc_FreeRng(&rng) && ret == 0) {
  19881. ret = WOLFSSL_FATAL_ERROR;
  19882. }
  19883. wc_ed448_free(&key);
  19884. #endif
  19885. return ret;
  19886. } /* END test_wc_ed448_sign_msg */
  19887. /*
  19888. * Testing wc_ed448_import_public()
  19889. */
  19890. static int test_wc_ed448_import_public(void)
  19891. {
  19892. int ret = 0;
  19893. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19894. WC_RNG rng;
  19895. ed448_key pubKey;
  19896. const byte in[] =
  19897. "Ed448PublicKeyUnitTest.................................\n";
  19898. word32 inlen = sizeof(in);
  19899. ret = wc_InitRng(&rng);
  19900. if (ret == 0) {
  19901. ret = wc_ed448_init(&pubKey);
  19902. if (ret == 0) {
  19903. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  19904. }
  19905. }
  19906. printf(testingFmt, "wc_ed448_import_public()");
  19907. if (ret == 0) {
  19908. ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
  19909. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  19910. ret = 0;
  19911. } else {
  19912. ret = WOLFSSL_FATAL_ERROR;
  19913. }
  19914. /* Test bad args. */
  19915. if (ret == 0) {
  19916. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  19917. if (ret == BAD_FUNC_ARG) {
  19918. ret = wc_ed448_import_public(in, inlen, NULL);
  19919. }
  19920. if (ret == BAD_FUNC_ARG) {
  19921. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  19922. }
  19923. if (ret == BAD_FUNC_ARG) {
  19924. ret = 0;
  19925. } else if (ret == 0) {
  19926. ret = WOLFSSL_FATAL_ERROR;
  19927. }
  19928. }
  19929. }
  19930. printf(resultFmt, ret == 0 ? passed : failed);
  19931. fflush(stdout);
  19932. if (wc_FreeRng(&rng) && ret == 0) {
  19933. ret = WOLFSSL_FATAL_ERROR;
  19934. }
  19935. wc_ed448_free(&pubKey);
  19936. #endif
  19937. return ret;
  19938. } /* END wc_ed448_import_public */
  19939. /*
  19940. * Testing wc_ed448_import_private_key()
  19941. */
  19942. static int test_wc_ed448_import_private_key(void)
  19943. {
  19944. int ret = 0;
  19945. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19946. WC_RNG rng;
  19947. ed448_key key;
  19948. const byte privKey[] =
  19949. "Ed448PrivateKeyUnitTest................................\n";
  19950. const byte pubKey[] =
  19951. "Ed448PublicKeyUnitTest.................................\n";
  19952. word32 privKeySz = sizeof(privKey);
  19953. word32 pubKeySz = sizeof(pubKey);
  19954. #ifdef HAVE_ED448_KEY_EXPORT
  19955. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19956. word32 bothKeysSz = sizeof(bothKeys);
  19957. #endif
  19958. ret = wc_InitRng(&rng);
  19959. if (ret != 0) {
  19960. return ret;
  19961. }
  19962. ret = wc_ed448_init(&key);
  19963. if (ret != 0) {
  19964. wc_FreeRng(&rng);
  19965. return ret;
  19966. }
  19967. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19968. printf(testingFmt, "wc_ed448_import_private_key()");
  19969. if (ret == 0) {
  19970. ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  19971. pubKeySz, &key, 1);
  19972. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  19973. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19974. ret = WOLFSSL_FATAL_ERROR;
  19975. }
  19976. }
  19977. #ifdef HAVE_ED448_KEY_EXPORT
  19978. if (ret == 0)
  19979. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  19980. if (ret == 0) {
  19981. ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19982. &key, 1);
  19983. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  19984. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  19985. ret = WOLFSSL_FATAL_ERROR;
  19986. }
  19987. }
  19988. #endif
  19989. /* Test bad args. */
  19990. if (ret == 0) {
  19991. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19992. &key);
  19993. if (ret == BAD_FUNC_ARG) {
  19994. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  19995. pubKeySz, &key);
  19996. }
  19997. if (ret == BAD_FUNC_ARG) {
  19998. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  19999. pubKeySz, NULL);
  20000. }
  20001. if (ret == BAD_FUNC_ARG) {
  20002. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20003. pubKeySz, &key);
  20004. }
  20005. if (ret == BAD_FUNC_ARG) {
  20006. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20007. pubKeySz - 1, &key);
  20008. }
  20009. if (ret == BAD_FUNC_ARG) {
  20010. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20011. 0, &key);
  20012. }
  20013. if (ret == BAD_FUNC_ARG) {
  20014. ret = 0;
  20015. } else if (ret == 0) {
  20016. ret = WOLFSSL_FATAL_ERROR;
  20017. }
  20018. }
  20019. printf(resultFmt, ret == 0 ? passed : failed);
  20020. fflush(stdout);
  20021. if (wc_FreeRng(&rng) && ret == 0) {
  20022. ret = WOLFSSL_FATAL_ERROR;
  20023. }
  20024. wc_ed448_free(&key);
  20025. #endif
  20026. return ret;
  20027. } /* END test_wc_ed448_import_private_key */
  20028. /*
  20029. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20030. */
  20031. static int test_wc_ed448_export(void)
  20032. {
  20033. int ret = 0;
  20034. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20035. WC_RNG rng;
  20036. ed448_key key;
  20037. byte priv[ED448_PRV_KEY_SIZE];
  20038. byte pub[ED448_PUB_KEY_SIZE];
  20039. word32 privSz = sizeof(priv);
  20040. word32 pubSz = sizeof(pub);
  20041. ret = wc_InitRng(&rng);
  20042. if (ret != 0) {
  20043. return ret;
  20044. }
  20045. ret = wc_ed448_init(&key);
  20046. if (ret != 0) {
  20047. wc_FreeRng(&rng);
  20048. return ret;
  20049. }
  20050. if (ret == 0) {
  20051. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20052. }
  20053. printf(testingFmt, "wc_ed448_export_public()");
  20054. if (ret == 0) {
  20055. ret = wc_ed448_export_public(&key, pub, &pubSz);
  20056. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  20057. XMEMCMP(key.p, pub, pubSz) != 0)) {
  20058. ret = WOLFSSL_FATAL_ERROR;
  20059. }
  20060. if (ret == 0) {
  20061. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  20062. if (ret == BAD_FUNC_ARG) {
  20063. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  20064. }
  20065. if (ret == BAD_FUNC_ARG) {
  20066. ret = wc_ed448_export_public(&key, pub, NULL);
  20067. }
  20068. if (ret == BAD_FUNC_ARG) {
  20069. ret = 0;
  20070. } else if (ret == 0) {
  20071. ret = WOLFSSL_FATAL_ERROR;
  20072. }
  20073. }
  20074. }
  20075. printf(resultFmt, ret == 0 ? passed : failed);
  20076. fflush(stdout);
  20077. printf(testingFmt, "wc_ed448_export_private_only()");
  20078. if (ret == 0) {
  20079. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  20080. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  20081. XMEMCMP(key.k, priv, privSz) != 0)) {
  20082. ret = WOLFSSL_FATAL_ERROR;
  20083. }
  20084. if (ret == 0) {
  20085. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  20086. if (ret == BAD_FUNC_ARG) {
  20087. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  20088. }
  20089. if (ret == BAD_FUNC_ARG) {
  20090. ret = wc_ed448_export_private_only(&key, priv, NULL);
  20091. }
  20092. if (ret == BAD_FUNC_ARG) {
  20093. ret = 0;
  20094. } else if (ret == 0) {
  20095. ret = WOLFSSL_FATAL_ERROR;
  20096. }
  20097. }
  20098. }
  20099. printf(resultFmt, ret == 0 ? passed : failed);
  20100. fflush(stdout);
  20101. if (wc_FreeRng(&rng) && ret == 0) {
  20102. ret = WOLFSSL_FATAL_ERROR;
  20103. }
  20104. wc_ed448_free(&key);
  20105. #endif
  20106. return ret;
  20107. } /* END test_wc_ed448_export */
  20108. /*
  20109. * Testing wc_ed448_size()
  20110. */
  20111. static int test_wc_ed448_size(void)
  20112. {
  20113. int ret = 0;
  20114. #if defined(HAVE_ED448)
  20115. WC_RNG rng;
  20116. ed448_key key;
  20117. ret = wc_InitRng(&rng);
  20118. if (ret != 0) {
  20119. return ret;
  20120. }
  20121. ret = wc_ed448_init(&key);
  20122. if (ret != 0) {
  20123. wc_FreeRng(&rng);
  20124. return ret;
  20125. }
  20126. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20127. if (ret != 0) {
  20128. wc_FreeRng(&rng);
  20129. wc_ed448_free(&key);
  20130. return ret;
  20131. }
  20132. printf(testingFmt, "wc_ed448_size()");
  20133. ret = wc_ed448_size(&key);
  20134. /* Test bad args. */
  20135. if (ret == ED448_KEY_SIZE) {
  20136. ret = wc_ed448_size(NULL);
  20137. if (ret == BAD_FUNC_ARG) {
  20138. ret = 0;
  20139. }
  20140. }
  20141. printf(resultFmt, ret == 0 ? passed : failed);
  20142. fflush(stdout);
  20143. if (ret == 0) {
  20144. printf(testingFmt, "wc_ed448_sig_size()");
  20145. ret = wc_ed448_sig_size(&key);
  20146. if (ret == ED448_SIG_SIZE) {
  20147. ret = 0;
  20148. }
  20149. /* Test bad args. */
  20150. if (ret == 0) {
  20151. ret = wc_ed448_sig_size(NULL);
  20152. if (ret == BAD_FUNC_ARG) {
  20153. ret = 0;
  20154. }
  20155. }
  20156. printf(resultFmt, ret == 0 ? passed : failed);
  20157. fflush(stdout);
  20158. } /* END wc_ed448_sig_size() */
  20159. if (ret == 0) {
  20160. printf(testingFmt, "wc_ed448_pub_size");
  20161. ret = wc_ed448_pub_size(&key);
  20162. if (ret == ED448_PUB_KEY_SIZE) {
  20163. ret = 0;
  20164. }
  20165. if (ret == 0) {
  20166. ret = wc_ed448_pub_size(NULL);
  20167. if (ret == BAD_FUNC_ARG) {
  20168. ret = 0;
  20169. }
  20170. }
  20171. printf(resultFmt, ret == 0 ? passed : failed);
  20172. fflush(stdout);
  20173. } /* END wc_ed448_pub_size */
  20174. if (ret == 0) {
  20175. printf(testingFmt, "wc_ed448_priv_size");
  20176. ret = wc_ed448_priv_size(&key);
  20177. if (ret == ED448_PRV_KEY_SIZE) {
  20178. ret = 0;
  20179. }
  20180. if (ret == 0) {
  20181. ret = wc_ed448_priv_size(NULL);
  20182. if (ret == BAD_FUNC_ARG) {
  20183. ret = 0;
  20184. }
  20185. }
  20186. printf(resultFmt, ret == 0 ? passed : failed);
  20187. fflush(stdout);
  20188. } /* END wc_ed448_pub_size */
  20189. if (wc_FreeRng(&rng) && ret == 0) {
  20190. ret = WOLFSSL_FATAL_ERROR;
  20191. }
  20192. wc_ed448_free(&key);
  20193. #endif
  20194. return ret;
  20195. } /* END test_wc_ed448_size */
  20196. /*
  20197. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20198. */
  20199. static int test_wc_ed448_exportKey(void)
  20200. {
  20201. int ret = 0;
  20202. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20203. WC_RNG rng;
  20204. ed448_key key;
  20205. byte priv[ED448_PRV_KEY_SIZE];
  20206. byte pub[ED448_PUB_KEY_SIZE];
  20207. byte privOnly[ED448_PRV_KEY_SIZE];
  20208. word32 privSz = sizeof(priv);
  20209. word32 pubSz = sizeof(pub);
  20210. word32 privOnlySz = sizeof(privOnly);
  20211. ret = wc_InitRng(&rng);
  20212. if (ret != 0) {
  20213. return ret;
  20214. }
  20215. ret = wc_ed448_init(&key);
  20216. if (ret != 0) {
  20217. wc_FreeRng(&rng);
  20218. return ret;
  20219. }
  20220. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20221. if (ret != 0) {
  20222. wc_FreeRng(&rng);
  20223. wc_ed448_free(&key);
  20224. return ret;
  20225. }
  20226. printf(testingFmt, "wc_ed448_export_private()");
  20227. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  20228. if (ret == 0) {
  20229. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  20230. if (ret == BAD_FUNC_ARG) {
  20231. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  20232. }
  20233. if (ret == BAD_FUNC_ARG) {
  20234. ret = wc_ed448_export_private(&key, privOnly, NULL);
  20235. }
  20236. if (ret == BAD_FUNC_ARG) {
  20237. ret = 0;
  20238. } else if (ret == 0) {
  20239. ret = WOLFSSL_FATAL_ERROR;
  20240. }
  20241. }
  20242. printf(resultFmt, ret == 0 ? passed : failed);
  20243. fflush(stdout);
  20244. if (ret == 0) {
  20245. printf(testingFmt, "wc_ed448_export_key()");
  20246. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  20247. if (ret == 0) {
  20248. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  20249. if (ret == BAD_FUNC_ARG) {
  20250. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  20251. }
  20252. if (ret == BAD_FUNC_ARG) {
  20253. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  20254. }
  20255. if (ret == BAD_FUNC_ARG) {
  20256. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  20257. }
  20258. if (ret == BAD_FUNC_ARG) {
  20259. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  20260. }
  20261. if (ret == BAD_FUNC_ARG) {
  20262. ret = 0;
  20263. } else if (ret == 0) {
  20264. ret = WOLFSSL_FATAL_ERROR;
  20265. }
  20266. }
  20267. printf(resultFmt, ret == 0 ? passed : failed);
  20268. fflush(stdout);
  20269. } /* END wc_ed448_export_key() */
  20270. /* Cross check output. */
  20271. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  20272. ret = WOLFSSL_FATAL_ERROR;
  20273. }
  20274. if (wc_FreeRng(&rng) && ret == 0) {
  20275. ret = WOLFSSL_FATAL_ERROR;
  20276. }
  20277. wc_ed448_free(&key);
  20278. #endif
  20279. return ret;
  20280. } /* END test_wc_ed448_exportKey */
  20281. /*
  20282. * Testing wc_Ed448PublicKeyToDer
  20283. */
  20284. static int test_wc_Ed448PublicKeyToDer(void)
  20285. {
  20286. int ret = 0;
  20287. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20288. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20289. int tmp;
  20290. ed448_key key;
  20291. byte derBuf[1024];
  20292. printf(testingFmt, "wc_Ed448PublicKeyToDer()");
  20293. /* Test bad args */
  20294. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  20295. if (tmp != BAD_FUNC_ARG) {
  20296. ret = WOLFSSL_FATAL_ERROR;
  20297. }
  20298. if (ret == 0) {
  20299. wc_ed448_init(&key);
  20300. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  20301. if (tmp != BUFFER_E) {
  20302. ret = WOLFSSL_FATAL_ERROR;
  20303. }
  20304. wc_ed448_free(&key);
  20305. }
  20306. /* Test good args */
  20307. if (ret == 0) {
  20308. WC_RNG rng;
  20309. ret = wc_InitRng(&rng);
  20310. if (ret != 0) {
  20311. return ret;
  20312. }
  20313. ret = wc_ed448_init(&key);
  20314. if (ret != 0) {
  20315. wc_FreeRng(&rng);
  20316. return ret;
  20317. }
  20318. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20319. if (ret != 0) {
  20320. wc_FreeRng(&rng);
  20321. wc_ed448_free(&key);
  20322. return ret;
  20323. }
  20324. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  20325. if (tmp <= 0) {
  20326. ret = WOLFSSL_FATAL_ERROR;
  20327. }
  20328. wc_FreeRng(&rng);
  20329. wc_ed448_free(&key);
  20330. }
  20331. printf(resultFmt, ret == 0 ? passed : failed);
  20332. fflush(stdout);
  20333. #endif
  20334. return ret;
  20335. } /* END testing wc_Ed448PublicKeyToDer */
  20336. /*
  20337. * Testing wc_curve448_init and wc_curve448_free.
  20338. */
  20339. static int test_wc_curve448_init(void)
  20340. {
  20341. int ret = 0;
  20342. #if defined(HAVE_CURVE448)
  20343. curve448_key key;
  20344. printf(testingFmt, "wc_curve448_init()");
  20345. ret = wc_curve448_init(&key);
  20346. /* Test bad args for wc_curve448_init */
  20347. if (ret == 0) {
  20348. ret = wc_curve448_init(NULL);
  20349. if (ret == BAD_FUNC_ARG) {
  20350. ret = 0;
  20351. } else if (ret == 0) {
  20352. ret = WOLFSSL_FATAL_ERROR;
  20353. }
  20354. }
  20355. printf(resultFmt, ret == 0 ? passed : failed);
  20356. fflush(stdout);
  20357. /* Test good args for wc_curve_448_free */
  20358. wc_curve448_free(&key);
  20359. wc_curve448_free(NULL);
  20360. #endif
  20361. return ret;
  20362. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20363. /*
  20364. * Testing wc_curve448_make_key
  20365. */
  20366. static int test_wc_curve448_make_key(void)
  20367. {
  20368. int ret = 0;
  20369. #if defined(HAVE_CURVE448)
  20370. WC_RNG rng;
  20371. curve448_key key;
  20372. int keysize;
  20373. printf(testingFmt, "wc_curve448_make_key()");
  20374. ret = wc_curve448_init(&key);
  20375. if (ret == 0) {
  20376. ret = wc_InitRng(&rng);
  20377. }
  20378. if (ret == 0) {
  20379. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20380. if (ret == 0) {
  20381. keysize = wc_curve448_size(&key);
  20382. if (keysize != CURVE448_KEY_SIZE) {
  20383. ret = WOLFSSL_FATAL_ERROR;
  20384. }
  20385. }
  20386. if (ret == 0) {
  20387. ret = wc_curve448_make_key(&rng, keysize, &key);
  20388. }
  20389. }
  20390. /*test bad cases*/
  20391. if (ret == 0) {
  20392. ret = wc_curve448_make_key(NULL, 0, NULL);
  20393. if (ret == BAD_FUNC_ARG) {
  20394. ret = 0;
  20395. }
  20396. }
  20397. if (ret == 0) {
  20398. ret = wc_curve448_make_key(&rng, keysize, NULL);
  20399. if (ret == BAD_FUNC_ARG) {
  20400. ret = 0;
  20401. }
  20402. }
  20403. if (ret == 0) {
  20404. ret = wc_curve448_make_key(NULL, keysize, &key);
  20405. if (ret == BAD_FUNC_ARG) {
  20406. ret = 0;
  20407. }
  20408. }
  20409. if (ret == 0) {
  20410. ret = wc_curve448_make_key(&rng, 0, &key);
  20411. if (ret == ECC_BAD_ARG_E) {
  20412. ret = 0;
  20413. }
  20414. }
  20415. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20416. ret = WOLFSSL_FATAL_ERROR;
  20417. }
  20418. printf(resultFmt, ret == 0 ? passed : failed);
  20419. fflush(stdout);
  20420. wc_curve448_free(&key);
  20421. #endif
  20422. return ret;
  20423. } /*END test_wc_curve448_make_key*/
  20424. /*
  20425. * Testing test_wc_curve448_shared_secret_ex
  20426. */
  20427. static int test_wc_curve448_shared_secret_ex(void)
  20428. {
  20429. int ret = 0;
  20430. #if defined(HAVE_CURVE448)
  20431. WC_RNG rng;
  20432. curve448_key private_key, public_key;
  20433. byte out[CURVE448_KEY_SIZE];
  20434. word32 outLen = sizeof(out);
  20435. int endian = EC448_BIG_ENDIAN;
  20436. printf(testingFmt, "wc_curve448_shared_secret_ex()");
  20437. ret = wc_curve448_init(&private_key);
  20438. if (ret == 0) {
  20439. ret = wc_InitRng(&rng);
  20440. if (ret == 0) {
  20441. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  20442. }
  20443. }
  20444. if (ret == 0) {
  20445. ret = wc_curve448_init(&public_key);
  20446. }
  20447. if (ret == 0) {
  20448. if (ret == 0) {
  20449. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  20450. }
  20451. }
  20452. if (ret == 0) {
  20453. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20454. &outLen, endian);
  20455. }
  20456. /*test bad cases*/
  20457. if (ret == 0) {
  20458. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL,
  20459. 0, endian);
  20460. if (ret == BAD_FUNC_ARG) {
  20461. ret = 0;
  20462. }
  20463. }
  20464. if (ret == 0) {
  20465. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  20466. &outLen, endian);
  20467. if (ret == BAD_FUNC_ARG) {
  20468. ret = 0;
  20469. }
  20470. }
  20471. if (ret == 0) {
  20472. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  20473. &outLen, endian);
  20474. if (ret == BAD_FUNC_ARG) {
  20475. ret = 0;
  20476. }
  20477. }
  20478. if (ret == 0) {
  20479. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20480. &outLen, endian);
  20481. if (ret == BAD_FUNC_ARG) {
  20482. ret = 0;
  20483. }
  20484. }
  20485. if (ret == 0) {
  20486. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20487. NULL, endian);
  20488. if (ret == BAD_FUNC_ARG) {
  20489. ret = 0;
  20490. }
  20491. }
  20492. outLen = outLen - 2;
  20493. if (ret == 0) {
  20494. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20495. &outLen, endian);
  20496. if (ret == BAD_FUNC_ARG) {
  20497. ret = 0;
  20498. }
  20499. }
  20500. printf(resultFmt, ret == 0 ? passed : failed);
  20501. fflush(stdout);
  20502. wc_curve448_free(&private_key);
  20503. wc_curve448_free(&public_key);
  20504. wc_FreeRng(&rng);
  20505. #endif
  20506. return ret;
  20507. } /*END test_wc_curve448_shared_secret_ex*/
  20508. /*
  20509. * Testing test_wc_curve448_export_public_ex
  20510. */
  20511. static int test_wc_curve448_export_public_ex(void)
  20512. {
  20513. int ret = 0;
  20514. #if defined(HAVE_CURVE448)
  20515. WC_RNG rng;
  20516. curve448_key key;
  20517. byte out[CURVE448_KEY_SIZE];
  20518. word32 outLen = sizeof(out);
  20519. int endian = EC448_BIG_ENDIAN;
  20520. printf(testingFmt, "wc_curve448_export_public_ex()");
  20521. ret = wc_curve448_init(&key);
  20522. if (ret == 0) {
  20523. ret = wc_InitRng(&rng);
  20524. }
  20525. if (ret == 0) {
  20526. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20527. if (ret == 0){
  20528. ret = wc_curve448_export_public(&key, out, &outLen);
  20529. }
  20530. if (ret == 0) {
  20531. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  20532. }
  20533. }
  20534. /*test bad cases*/
  20535. if (ret == 0) {
  20536. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  20537. if (ret == BAD_FUNC_ARG) {
  20538. ret = 0;
  20539. }
  20540. }
  20541. if (ret == 0) {
  20542. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  20543. if (ret == BAD_FUNC_ARG) {
  20544. ret = 0;
  20545. }
  20546. }
  20547. if (ret == 0) {
  20548. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  20549. if (ret == BAD_FUNC_ARG) {
  20550. ret = 0;
  20551. }
  20552. }
  20553. if (ret == 0) {
  20554. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  20555. if (ret == BAD_FUNC_ARG) {
  20556. ret = 0;
  20557. }
  20558. }
  20559. outLen = outLen - 2;
  20560. if (ret == 0) {
  20561. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  20562. if (ret == ECC_BAD_ARG_E) {
  20563. ret = 0;
  20564. }
  20565. }
  20566. printf(resultFmt, ret == 0 ? passed : failed);
  20567. fflush(stdout);
  20568. wc_curve448_free(&key);
  20569. wc_FreeRng(&rng);
  20570. #endif
  20571. return ret;
  20572. } /*END test_wc_curve448_export_public_ex*/
  20573. /*
  20574. * Testing test_wc_curve448_export_private_raw_ex
  20575. */
  20576. static int test_wc_curve448_export_private_raw_ex(void)
  20577. {
  20578. int ret = 0;
  20579. #if defined(HAVE_CURVE448)
  20580. curve448_key key;
  20581. byte out[CURVE448_KEY_SIZE];
  20582. word32 outLen = sizeof(out);
  20583. int endian = EC448_BIG_ENDIAN;
  20584. printf(testingFmt, "wc_curve448_export_private_raw_ex()");
  20585. ret = wc_curve448_init(&key);
  20586. if (ret == 0) {
  20587. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  20588. }
  20589. /*test bad cases*/
  20590. if (ret == 0) {
  20591. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  20592. if (ret == BAD_FUNC_ARG) {
  20593. ret = 0;
  20594. }
  20595. }
  20596. if (ret == 0) {
  20597. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  20598. if (ret == BAD_FUNC_ARG) {
  20599. ret = 0;
  20600. }
  20601. }
  20602. if (ret == 0) {
  20603. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  20604. if (ret == BAD_FUNC_ARG) {
  20605. ret = 0;
  20606. }
  20607. }
  20608. if (ret == 0) {
  20609. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  20610. if (ret == BAD_FUNC_ARG) {
  20611. ret = 0;
  20612. }
  20613. }
  20614. if (ret == 0) {
  20615. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20616. EC448_LITTLE_ENDIAN);
  20617. }
  20618. outLen = outLen - 2;
  20619. if (ret == 0) {
  20620. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  20621. if (ret == ECC_BAD_ARG_E) {
  20622. ret = 0;
  20623. }
  20624. }
  20625. printf(resultFmt, ret == 0 ? passed : failed);
  20626. fflush(stdout);
  20627. wc_curve448_free(&key);
  20628. #endif
  20629. return ret;
  20630. }/*END test_wc_curve448_export_private_raw_ex*/
  20631. /*
  20632. * Testing test_wc_curve448_import_private_raw_ex
  20633. */
  20634. static int test_wc_curve448_import_private_raw_ex(void)
  20635. {
  20636. int ret = 0;
  20637. #if defined(HAVE_CURVE448)
  20638. WC_RNG rng;
  20639. curve448_key key;
  20640. byte priv[CURVE448_KEY_SIZE];
  20641. byte pub[CURVE448_KEY_SIZE];
  20642. word32 privSz = sizeof(priv);
  20643. word32 pubSz = sizeof(pub);
  20644. int endian = EC448_BIG_ENDIAN;
  20645. printf(testingFmt, "wc_curve448_import_private_raw_ex()");
  20646. ret = wc_curve448_init(&key);
  20647. if (ret == 0) {
  20648. ret = wc_InitRng(&rng);
  20649. }
  20650. if (ret == 0) {
  20651. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20652. if (ret == 0){
  20653. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20654. }
  20655. if (ret == 0){
  20656. ret = wc_curve448_export_public(&key, pub, &pubSz);
  20657. }
  20658. if (ret == 0) {
  20659. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20660. &key, endian);
  20661. }
  20662. }
  20663. /*test bad cases*/
  20664. if (ret == 0) {
  20665. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  20666. if (ret == BAD_FUNC_ARG) {
  20667. ret = 0;
  20668. }
  20669. }
  20670. if (ret == 0) {
  20671. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20672. &key, endian);
  20673. if (ret == BAD_FUNC_ARG) {
  20674. ret = 0;
  20675. }
  20676. }
  20677. if (ret == 0) {
  20678. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20679. &key, endian);
  20680. if (ret == BAD_FUNC_ARG) {
  20681. ret = 0;
  20682. }
  20683. }
  20684. if (ret == 0) {
  20685. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20686. NULL, endian);
  20687. if (ret == BAD_FUNC_ARG) {
  20688. ret = 0;
  20689. }
  20690. }
  20691. if (ret == 0) {
  20692. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20693. &key, endian);
  20694. if (ret == ECC_BAD_ARG_E) {
  20695. ret = 0;
  20696. }
  20697. }
  20698. if (ret == 0) {
  20699. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20700. &key, endian);
  20701. if (ret == ECC_BAD_ARG_E) {
  20702. ret = 0;
  20703. }
  20704. }
  20705. if (ret == 0) {
  20706. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20707. &key, EC448_LITTLE_ENDIAN);
  20708. }
  20709. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20710. ret = WOLFSSL_FATAL_ERROR;
  20711. }
  20712. printf(resultFmt, ret == 0 ? passed : failed);
  20713. fflush(stdout);
  20714. wc_curve448_free(&key);
  20715. #endif
  20716. return ret;
  20717. } /*END test_wc_curve448_import_private_raw_ex*/
  20718. /*
  20719. * Testing test_curve448_export_key_raw
  20720. */
  20721. static int test_wc_curve448_export_key_raw(void)
  20722. {
  20723. int ret = 0;
  20724. #if defined(HAVE_CURVE448)
  20725. WC_RNG rng;
  20726. curve448_key key;
  20727. byte priv[CURVE448_KEY_SIZE];
  20728. byte pub[CURVE448_KEY_SIZE];
  20729. word32 privSz = sizeof(priv);
  20730. word32 pubSz = sizeof(pub);
  20731. printf(testingFmt, "wc_curve448_export_key_raw()");
  20732. ret = wc_curve448_init(&key);
  20733. if (ret == 0) {
  20734. ret = wc_InitRng(&rng);
  20735. }
  20736. if (ret == 0) {
  20737. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20738. if (ret == 0) {
  20739. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20740. }
  20741. if (ret == 0) {
  20742. ret = wc_curve448_export_public(&key, pub, &pubSz);
  20743. }
  20744. if (ret == 0) {
  20745. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  20746. }
  20747. }
  20748. printf(resultFmt, ret == 0 ? passed : failed);
  20749. fflush(stdout);
  20750. wc_curve448_free(&key);
  20751. wc_FreeRng(&rng);
  20752. #endif
  20753. return ret;
  20754. }/*END test_wc_curve448_import_private_raw_ex*/
  20755. /*
  20756. * Testing test_wc_curve448_import_private
  20757. */
  20758. static int test_wc_curve448_import_private(void)
  20759. {
  20760. int ret = 0;
  20761. #if defined(HAVE_CURVE448)
  20762. curve448_key key;
  20763. WC_RNG rng;
  20764. byte priv[CURVE448_KEY_SIZE];
  20765. word32 privSz = sizeof(priv);
  20766. printf(testingFmt, "wc_curve448_import_private()");
  20767. ret = wc_curve448_init(&key);
  20768. if (ret == 0) {
  20769. ret = wc_InitRng(&rng);
  20770. }
  20771. if (ret == 0) {
  20772. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20773. if (ret == 0) {
  20774. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20775. }
  20776. }
  20777. if (ret == 0) {
  20778. ret = wc_curve448_import_private(priv, privSz, &key);
  20779. }
  20780. printf(resultFmt, ret == 0 ? passed : failed);
  20781. fflush(stdout);
  20782. wc_curve448_free(&key);
  20783. wc_FreeRng(&rng);
  20784. #endif
  20785. return ret;
  20786. } /*END test_wc_curve448_import*/
  20787. /*
  20788. * Testing test_wc_curve448_size.
  20789. */
  20790. static int test_wc_curve448_size(void)
  20791. {
  20792. int ret = 0;
  20793. #if defined(HAVE_CURVE448)
  20794. curve448_key key;
  20795. printf(testingFmt, "wc_curve448_size()");
  20796. ret = wc_curve448_init(&key);
  20797. /* Test good args for wc_curve448_size */
  20798. if (ret == 0) {
  20799. ret = wc_curve448_size(&key);
  20800. }
  20801. /* Test bad args for wc_curve448_size */
  20802. if (ret != 0) {
  20803. ret = wc_curve448_size(NULL);
  20804. }
  20805. printf(resultFmt, ret == 0 ? passed : failed);
  20806. fflush(stdout);
  20807. wc_curve448_free(&key);
  20808. #endif
  20809. return ret;
  20810. } /* END test_wc_curve448_size*/
  20811. /*
  20812. * Testing wc_ecc_make_key.
  20813. */
  20814. static int test_wc_ecc_make_key(void)
  20815. {
  20816. int ret = 0;
  20817. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20818. WC_RNG rng;
  20819. ecc_key key;
  20820. printf(testingFmt, "wc_ecc_make_key()");
  20821. ret = wc_InitRng(&rng);
  20822. if (ret != 0)
  20823. return ret;
  20824. ret = wc_ecc_init(&key);
  20825. if (ret == 0) {
  20826. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20827. #if defined(WOLFSSL_ASYNC_CRYPT)
  20828. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20829. #endif
  20830. /* Pass in bad args. */
  20831. if (ret == 0) {
  20832. ret = wc_ecc_make_key(NULL, KEY14, &key);
  20833. if (ret == BAD_FUNC_ARG) {
  20834. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  20835. }
  20836. if (ret == BAD_FUNC_ARG) {
  20837. ret = 0;
  20838. } else if (ret == 0) {
  20839. ret = WOLFSSL_FATAL_ERROR;
  20840. }
  20841. }
  20842. wc_ecc_free(&key);
  20843. }
  20844. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20845. ret = WOLFSSL_FATAL_ERROR;
  20846. }
  20847. #ifdef FP_ECC
  20848. wc_ecc_fp_free();
  20849. #endif
  20850. printf(resultFmt, ret == 0 ? passed : failed);
  20851. fflush(stdout);
  20852. #endif
  20853. return ret;
  20854. } /* END test_wc_ecc_make_key */
  20855. /*
  20856. * Testing wc_ecc_init()
  20857. */
  20858. static int test_wc_ecc_init(void)
  20859. {
  20860. int ret = 0;
  20861. #ifdef HAVE_ECC
  20862. ecc_key key;
  20863. printf(testingFmt, "wc_ecc_init()");
  20864. ret = wc_ecc_init(&key);
  20865. /* Pass in bad args. */
  20866. if (ret == 0) {
  20867. ret = wc_ecc_init(NULL);
  20868. if (ret == BAD_FUNC_ARG) {
  20869. ret = 0;
  20870. } else if (ret == 0) {
  20871. ret = WOLFSSL_FATAL_ERROR;
  20872. }
  20873. }
  20874. printf(resultFmt, ret == 0 ? passed : failed);
  20875. fflush(stdout);
  20876. wc_ecc_free(&key);
  20877. #endif
  20878. return ret;
  20879. } /* END test_wc_ecc_init */
  20880. /*
  20881. * Testing wc_ecc_check_key()
  20882. */
  20883. static int test_wc_ecc_check_key(void)
  20884. {
  20885. int ret = 0;
  20886. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20887. WC_RNG rng;
  20888. ecc_key key;
  20889. XMEMSET(&rng, 0, sizeof(rng));
  20890. XMEMSET(&key, 0, sizeof(key));
  20891. ret = wc_InitRng(&rng);
  20892. if (ret == 0) {
  20893. ret = wc_ecc_init(&key);
  20894. if (ret == 0) {
  20895. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20896. #if defined(WOLFSSL_ASYNC_CRYPT)
  20897. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20898. #endif
  20899. }
  20900. }
  20901. printf(testingFmt, "wc_ecc_check_key()");
  20902. if (ret == 0) {
  20903. ret = wc_ecc_check_key(&key);
  20904. }
  20905. /* Pass in bad args. */
  20906. if (ret == 0) {
  20907. ret = wc_ecc_check_key(NULL);
  20908. if (ret == BAD_FUNC_ARG) {
  20909. ret = 0;
  20910. } else if (ret == 0) {
  20911. ret = WOLFSSL_FATAL_ERROR;
  20912. }
  20913. }
  20914. printf(resultFmt, ret == 0 ? passed : failed);
  20915. fflush(stdout);
  20916. if (wc_FreeRng(&rng) && ret == 0) {
  20917. ret = WOLFSSL_FATAL_ERROR;
  20918. }
  20919. wc_ecc_free(&key);
  20920. #ifdef FP_ECC
  20921. wc_ecc_fp_free();
  20922. #endif
  20923. #endif
  20924. return ret;
  20925. } /* END test_wc_ecc_check_key */
  20926. /*
  20927. * Testing wc_ecc_get_generator()
  20928. */
  20929. static int test_wc_ecc_get_generator(void)
  20930. {
  20931. int ret = 0;
  20932. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  20933. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  20934. ecc_point* pt;
  20935. printf(testingFmt, "wc_ecc_new_point()");
  20936. pt = wc_ecc_new_point();
  20937. if (!pt) {
  20938. ret = WOLFSSL_FATAL_ERROR;
  20939. }
  20940. printf(testingFmt, "wc_ecc_get_generator()");
  20941. if (ret == 0) {
  20942. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  20943. }
  20944. /* Test bad args. */
  20945. if (ret == MP_OKAY) {
  20946. /* Returns Zero for bad arg. */
  20947. ret = wc_ecc_get_generator(pt, -1);
  20948. if (ret != MP_OKAY)
  20949. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  20950. if (ret != MP_OKAY)
  20951. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  20952. * increase this number */
  20953. if (ret != MP_OKAY)
  20954. wc_ecc_get_generator(NULL, -1);
  20955. ret = ret == MP_OKAY ? WOLFSSL_FATAL_ERROR : 0;
  20956. }
  20957. printf(resultFmt, ret == 0 ? passed : failed);
  20958. fflush(stdout);
  20959. wc_ecc_del_point(pt);
  20960. #endif
  20961. return ret;
  20962. } /* END test_wc_ecc_get_generator */
  20963. /*
  20964. * Testing wc_ecc_size()
  20965. */
  20966. static int test_wc_ecc_size(void)
  20967. {
  20968. int ret = 0;
  20969. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20970. WC_RNG rng;
  20971. ecc_key key;
  20972. XMEMSET(&rng, 0, sizeof(rng));
  20973. XMEMSET(&key, 0, sizeof(key));
  20974. ret = wc_InitRng(&rng);
  20975. if (ret == 0) {
  20976. ret = wc_ecc_init(&key);
  20977. if (ret == 0) {
  20978. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20979. #if defined(WOLFSSL_ASYNC_CRYPT)
  20980. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20981. #endif
  20982. }
  20983. }
  20984. printf(testingFmt, "wc_ecc_size()");
  20985. if (ret == 0) {
  20986. ret = wc_ecc_size(&key);
  20987. if (ret == KEY14) {
  20988. ret = 0;
  20989. } else if (ret == 0){
  20990. ret = WOLFSSL_FATAL_ERROR;
  20991. }
  20992. }
  20993. /* Test bad args. */
  20994. if (ret == 0) {
  20995. /* Returns Zero for bad arg. */
  20996. ret = wc_ecc_size(NULL);
  20997. }
  20998. printf(resultFmt, ret == 0 ? passed : failed);
  20999. fflush(stdout);
  21000. if (wc_FreeRng(&rng) && ret == 0) {
  21001. ret = WOLFSSL_FATAL_ERROR;
  21002. }
  21003. wc_ecc_free(&key);
  21004. #endif
  21005. return ret;
  21006. } /* END test_wc_ecc_size */
  21007. static int test_wc_ecc_params(void)
  21008. {
  21009. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21010. It was added after certifications */
  21011. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21012. const ecc_set_type* ecc_set;
  21013. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21014. /* Test for SECP256R1 curve */
  21015. int curve_id = ECC_SECP256R1;
  21016. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  21017. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  21018. ecc_set = wc_ecc_get_curve_params(curve_idx);
  21019. AssertNotNull(ecc_set);
  21020. AssertIntEQ(ecc_set->id, curve_id);
  21021. #endif
  21022. /* Test case when SECP256R1 is not enabled */
  21023. /* Test that we get curve params for index 0 */
  21024. ecc_set = wc_ecc_get_curve_params(0);
  21025. AssertNotNull(ecc_set);
  21026. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21027. return 0;
  21028. }
  21029. /*
  21030. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21031. */
  21032. static int test_wc_ecc_signVerify_hash(void)
  21033. {
  21034. int ret = 0;
  21035. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21036. WC_RNG rng;
  21037. ecc_key key;
  21038. int signH = WOLFSSL_FATAL_ERROR;
  21039. #ifdef HAVE_ECC_VERIFY
  21040. int verifyH = WOLFSSL_FATAL_ERROR;
  21041. int verify = 0;
  21042. #endif
  21043. word32 siglen = ECC_BUFSIZE;
  21044. byte sig[ECC_BUFSIZE];
  21045. byte adjustedSig[ECC_BUFSIZE+1];
  21046. byte digest[] = TEST_STRING;
  21047. word32 digestlen = (word32)TEST_STRING_SZ;
  21048. /* Init stack var */
  21049. XMEMSET(sig, 0, siglen);
  21050. XMEMSET(&key, 0, sizeof(key));
  21051. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21052. /* Init structs. */
  21053. ret = wc_InitRng(&rng);
  21054. if (ret == 0) {
  21055. ret = wc_ecc_init(&key);
  21056. if (ret == 0) {
  21057. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21058. #if defined(WOLFSSL_ASYNC_CRYPT)
  21059. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21060. #endif
  21061. }
  21062. }
  21063. printf(testingFmt, "wc_ecc_sign_hash()");
  21064. if (ret == 0) {
  21065. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  21066. }
  21067. /* Check bad args. */
  21068. if (ret == 0) {
  21069. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  21070. if (signH == ECC_BAD_ARG_E) {
  21071. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  21072. &rng, &key);
  21073. }
  21074. if (signH == ECC_BAD_ARG_E) {
  21075. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  21076. &rng, &key);
  21077. }
  21078. if (signH == ECC_BAD_ARG_E) {
  21079. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21080. NULL, &key);
  21081. }
  21082. if (signH == ECC_BAD_ARG_E) {
  21083. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21084. &rng, NULL);
  21085. }
  21086. if (signH == ECC_BAD_ARG_E) {
  21087. signH = 0;
  21088. } else if (ret == 0) {
  21089. signH = WOLFSSL_FATAL_ERROR;
  21090. }
  21091. }
  21092. printf(resultFmt, signH == 0 ? passed : failed);
  21093. #ifdef HAVE_ECC_VERIFY
  21094. printf(testingFmt, "wc_ecc_verify_hash()");
  21095. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  21096. if (verify != 1 && ret == 0) {
  21097. ret = WOLFSSL_FATAL_ERROR;
  21098. }
  21099. /* test check on length of signature passed in */
  21100. XMEMCPY(adjustedSig, sig, siglen);
  21101. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21102. #ifndef NO_STRICT_ECDSA_LEN
  21103. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21104. &verify, &key), 0);
  21105. #else
  21106. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21107. * is allowed */
  21108. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21109. &verify, &key), 0);
  21110. #endif
  21111. /* Test bad args. */
  21112. if (ret == 0) {
  21113. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  21114. &verify, &key);
  21115. if (verifyH == ECC_BAD_ARG_E) {
  21116. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  21117. &verify, &key);
  21118. }
  21119. if (verifyH == ECC_BAD_ARG_E) {
  21120. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21121. NULL, &key);
  21122. }
  21123. if (verifyH == ECC_BAD_ARG_E) {
  21124. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21125. &verify, NULL);
  21126. }
  21127. if (verifyH == ECC_BAD_ARG_E) {
  21128. verifyH = 0;
  21129. } else if (ret == 0) {
  21130. verifyH = WOLFSSL_FATAL_ERROR;
  21131. }
  21132. }
  21133. printf(resultFmt, verifyH == 0 ? passed : failed);
  21134. #endif /* HAVE_ECC_VERIFY */
  21135. if (wc_FreeRng(&rng) && ret == 0) {
  21136. ret = WOLFSSL_FATAL_ERROR;
  21137. }
  21138. wc_ecc_free(&key);
  21139. #ifdef FP_ECC
  21140. wc_ecc_fp_free();
  21141. #endif
  21142. #endif
  21143. return ret;
  21144. } /* END test_wc_ecc_sign_hash */
  21145. /*
  21146. * Testing wc_ecc_shared_secret()
  21147. */
  21148. static int test_wc_ecc_shared_secret(void)
  21149. {
  21150. int ret = 0;
  21151. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21152. ecc_key key, pubKey;
  21153. WC_RNG rng;
  21154. byte out[KEY32];
  21155. int keySz = sizeof(out);
  21156. word32 outlen = (word32)sizeof(out);
  21157. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21158. const char* qx =
  21159. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21160. const char* qy =
  21161. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21162. const char* d =
  21163. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21164. const char* curveName = "SECP256R1";
  21165. const byte expected_shared_secret[] =
  21166. {
  21167. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21168. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21169. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21170. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21171. };
  21172. #endif
  21173. PRIVATE_KEY_UNLOCK();
  21174. /* Initialize variables. */
  21175. XMEMSET(out, 0, keySz);
  21176. XMEMSET(&rng, 0, sizeof(rng));
  21177. XMEMSET(&key, 0, sizeof(key));
  21178. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21179. ret = wc_InitRng(&rng);
  21180. if (ret == 0) {
  21181. ret = wc_ecc_init(&key);
  21182. if (ret == 0) {
  21183. ret = wc_ecc_init(&pubKey);
  21184. }
  21185. }
  21186. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21187. if (ret == 0) {
  21188. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21189. }
  21190. if (ret == 0) {
  21191. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  21192. }
  21193. #else
  21194. if (ret == 0) {
  21195. ret = wc_ecc_make_key(&rng, keySz, &key);
  21196. #if defined(WOLFSSL_ASYNC_CRYPT)
  21197. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21198. #endif
  21199. }
  21200. if (ret == 0) {
  21201. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  21202. #if defined(WOLFSSL_ASYNC_CRYPT)
  21203. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21204. #endif
  21205. }
  21206. #endif
  21207. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21208. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21209. !defined(HAVE_SELFTEST)
  21210. if (ret == 0) {
  21211. ret = wc_ecc_set_rng(&key, &rng);
  21212. }
  21213. #endif
  21214. printf(testingFmt, "wc_ecc_shared_secret()");
  21215. if (ret == 0) {
  21216. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21217. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21218. if (ret == 0) {
  21219. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  21220. ret = WOLFSSL_FATAL_ERROR;
  21221. }
  21222. }
  21223. #endif
  21224. /* Test bad args. */
  21225. if (ret == 0) {
  21226. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  21227. if (ret == BAD_FUNC_ARG) {
  21228. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  21229. }
  21230. if (ret == BAD_FUNC_ARG) {
  21231. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  21232. }
  21233. if (ret == BAD_FUNC_ARG) {
  21234. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  21235. }
  21236. if (ret == BAD_FUNC_ARG) {
  21237. /* Invalid length */
  21238. outlen = 1;
  21239. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21240. }
  21241. if (ret == BUFFER_E) {
  21242. ret = 0;
  21243. } else if (ret == 0) {
  21244. ret = WOLFSSL_FATAL_ERROR;
  21245. }
  21246. }
  21247. }
  21248. printf(resultFmt, ret == 0 ? passed : failed);
  21249. fflush(stdout);
  21250. if (wc_FreeRng(&rng) && ret == 0) {
  21251. ret = WOLFSSL_FATAL_ERROR;
  21252. }
  21253. wc_ecc_free(&key);
  21254. wc_ecc_free(&pubKey);
  21255. #ifdef FP_ECC
  21256. wc_ecc_fp_free();
  21257. #endif
  21258. #endif
  21259. PRIVATE_KEY_LOCK();
  21260. return ret;
  21261. } /* END tests_wc_ecc_shared_secret */
  21262. /*
  21263. * testint wc_ecc_export_x963()
  21264. */
  21265. static int test_wc_ecc_export_x963(void)
  21266. {
  21267. int ret = 0;
  21268. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21269. ecc_key key;
  21270. WC_RNG rng;
  21271. byte out[ECC_ASN963_MAX_BUF_SZ];
  21272. word32 outlen = sizeof(out);
  21273. PRIVATE_KEY_UNLOCK();
  21274. /* Initialize variables. */
  21275. XMEMSET(out, 0, outlen);
  21276. XMEMSET(&rng, 0, sizeof(rng));
  21277. XMEMSET(&key, 0, sizeof(key));
  21278. ret = wc_InitRng(&rng);
  21279. if (ret == 0) {
  21280. ret = wc_ecc_init(&key);
  21281. if (ret == 0) {
  21282. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21283. #if defined(WOLFSSL_ASYNC_CRYPT)
  21284. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21285. #endif
  21286. }
  21287. }
  21288. printf(testingFmt, "wc_ecc_export_x963()");
  21289. if (ret == 0) {
  21290. ret = wc_ecc_export_x963(&key, out, &outlen);
  21291. }
  21292. /* Test bad args. */
  21293. if (ret == 0) {
  21294. ret = wc_ecc_export_x963(NULL, out, &outlen);
  21295. if (ret == ECC_BAD_ARG_E) {
  21296. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  21297. }
  21298. if (ret == LENGTH_ONLY_E) {
  21299. ret = wc_ecc_export_x963(&key, out, NULL);
  21300. }
  21301. if (ret == ECC_BAD_ARG_E) {
  21302. key.idx = -4;
  21303. ret = wc_ecc_export_x963(&key, out, &outlen);
  21304. }
  21305. if (ret == ECC_BAD_ARG_E) {
  21306. ret = 0;
  21307. } else {
  21308. ret = WOLFSSL_FATAL_ERROR;
  21309. }
  21310. }
  21311. printf(resultFmt, ret == 0 ? passed : failed);
  21312. fflush(stdout);
  21313. if (wc_FreeRng(&rng) && ret == 0) {
  21314. ret = WOLFSSL_FATAL_ERROR;
  21315. }
  21316. wc_ecc_free(&key);
  21317. #ifdef FP_ECC
  21318. wc_ecc_fp_free();
  21319. #endif
  21320. #endif
  21321. PRIVATE_KEY_LOCK();
  21322. return ret;
  21323. } /* END test_wc_ecc_export_x963 */
  21324. /*
  21325. * Testing wc_ecc_export_x963_ex()
  21326. * compile with --enable-compkey will use compression.
  21327. */
  21328. static int test_wc_ecc_export_x963_ex(void)
  21329. {
  21330. int ret = 0;
  21331. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21332. ecc_key key;
  21333. WC_RNG rng;
  21334. byte out[ECC_ASN963_MAX_BUF_SZ];
  21335. word32 outlen = sizeof(out);
  21336. #ifdef HAVE_COMP_KEY
  21337. word32 badOutLen = 5;
  21338. #endif
  21339. /* Init stack variables. */
  21340. XMEMSET(out, 0, outlen);
  21341. XMEMSET(&rng, 0, sizeof(rng));
  21342. XMEMSET(&key, 0, sizeof(key));
  21343. ret = wc_InitRng(&rng);
  21344. if (ret == 0) {
  21345. ret = wc_ecc_init(&key);
  21346. if (ret == 0) {
  21347. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21348. #if defined(WOLFSSL_ASYNC_CRYPT)
  21349. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21350. #endif
  21351. }
  21352. }
  21353. printf(testingFmt, "wc_ecc_export_x963_ex()");
  21354. #ifdef HAVE_COMP_KEY
  21355. if (ret == 0) {
  21356. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21357. }
  21358. #else
  21359. if (ret == 0) {
  21360. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21361. }
  21362. #endif
  21363. /* Test bad args. */
  21364. #ifdef HAVE_COMP_KEY
  21365. if (ret == 0) {
  21366. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  21367. if (ret == BAD_FUNC_ARG) {
  21368. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  21369. }
  21370. if (ret == BAD_FUNC_ARG) {
  21371. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  21372. }
  21373. if (ret == BAD_FUNC_ARG) {
  21374. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  21375. }
  21376. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21377. if (ret == BUFFER_E)
  21378. #else
  21379. if (ret == LENGTH_ONLY_E)
  21380. #endif
  21381. {
  21382. key.idx = -4;
  21383. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21384. }
  21385. if (ret == ECC_BAD_ARG_E) {
  21386. ret = 0;
  21387. } else {
  21388. ret = WOLFSSL_FATAL_ERROR;
  21389. }
  21390. }
  21391. #else
  21392. if (ret == 0) {
  21393. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  21394. if (ret == BAD_FUNC_ARG) {
  21395. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  21396. }
  21397. if (ret == BAD_FUNC_ARG) {
  21398. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  21399. }
  21400. if (ret == NOT_COMPILED_IN) {
  21401. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  21402. }
  21403. if (ret == BAD_FUNC_ARG) {
  21404. key.idx = -4;
  21405. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21406. }
  21407. if (ret == ECC_BAD_ARG_E) {
  21408. ret = 0;
  21409. } else if (ret == 0) {
  21410. ret = WOLFSSL_FATAL_ERROR;
  21411. }
  21412. }
  21413. #endif
  21414. printf(resultFmt, ret == 0 ? passed : failed);
  21415. fflush(stdout);
  21416. if (wc_FreeRng(&rng) && ret == 0) {
  21417. ret = WOLFSSL_FATAL_ERROR;
  21418. }
  21419. wc_ecc_free(&key);
  21420. #ifdef FP_ECC
  21421. wc_ecc_fp_free();
  21422. #endif
  21423. #endif
  21424. return ret;
  21425. } /* END test_wc_ecc_export_x963_ex */
  21426. /*
  21427. * testing wc_ecc_import_x963()
  21428. */
  21429. static int test_wc_ecc_import_x963(void)
  21430. {
  21431. int ret = 0;
  21432. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21433. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21434. ecc_key pubKey, key;
  21435. WC_RNG rng;
  21436. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21437. word32 x963Len = (word32)sizeof(x963);
  21438. /* Init stack variables. */
  21439. XMEMSET(x963, 0, x963Len);
  21440. XMEMSET(&rng, 0, sizeof(rng));
  21441. XMEMSET(&key, 0, sizeof(key));
  21442. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21443. ret = wc_InitRng(&rng);
  21444. if (ret == 0) {
  21445. ret = wc_ecc_init(&pubKey);
  21446. if (ret == 0) {
  21447. ret = wc_ecc_init(&key);
  21448. }
  21449. if (ret == 0) {
  21450. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21451. #if defined(WOLFSSL_ASYNC_CRYPT)
  21452. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21453. #endif
  21454. }
  21455. if (ret == 0) {
  21456. PRIVATE_KEY_UNLOCK();
  21457. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  21458. PRIVATE_KEY_LOCK();
  21459. }
  21460. }
  21461. printf(testingFmt, "wc_ecc_import_x963()");
  21462. if (ret == 0) {
  21463. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  21464. }
  21465. /* Test bad args. */
  21466. if (ret == 0) {
  21467. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  21468. if (ret == BAD_FUNC_ARG) {
  21469. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  21470. }
  21471. if (ret == BAD_FUNC_ARG) {
  21472. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  21473. }
  21474. if (ret == ECC_BAD_ARG_E) {
  21475. ret = 0;
  21476. } else if (ret == 0) {
  21477. ret = WOLFSSL_FATAL_ERROR;
  21478. }
  21479. }
  21480. printf(resultFmt, ret == 0 ? passed : failed);
  21481. fflush(stdout);
  21482. if (wc_FreeRng(&rng) && ret == 0) {
  21483. ret = WOLFSSL_FATAL_ERROR;
  21484. }
  21485. wc_ecc_free(&key);
  21486. wc_ecc_free(&pubKey);
  21487. #ifdef FP_ECC
  21488. wc_ecc_fp_free();
  21489. #endif
  21490. #endif
  21491. return ret;
  21492. } /* END wc_ecc_import_x963 */
  21493. /*
  21494. * testing wc_ecc_import_private_key()
  21495. */
  21496. static int ecc_import_private_key (void)
  21497. {
  21498. int ret = 0;
  21499. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21500. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21501. ecc_key key, keyImp;
  21502. WC_RNG rng;
  21503. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21504. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21505. word32 privKeySz = (word32)sizeof(privKey);
  21506. word32 x963KeySz = (word32)sizeof(x963Key);
  21507. /* Init stack variables. */
  21508. XMEMSET(privKey, 0, privKeySz);
  21509. XMEMSET(x963Key, 0, x963KeySz);
  21510. XMEMSET(&rng, 0, sizeof(rng));
  21511. XMEMSET(&key, 0, sizeof(key));
  21512. XMEMSET(&keyImp, 0, sizeof(keyImp));
  21513. ret = wc_InitRng(&rng);
  21514. if (ret == 0) {
  21515. ret = wc_ecc_init(&key);
  21516. if (ret == 0) {
  21517. ret = wc_ecc_init(&keyImp);
  21518. }
  21519. if (ret == 0) {
  21520. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21521. #if defined(WOLFSSL_ASYNC_CRYPT)
  21522. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21523. #endif
  21524. }
  21525. if (ret == 0) {
  21526. PRIVATE_KEY_UNLOCK();
  21527. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  21528. PRIVATE_KEY_LOCK();
  21529. }
  21530. if (ret == 0) {
  21531. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  21532. }
  21533. }
  21534. printf(testingFmt, "wc_ecc_import_private_key()");
  21535. if (ret == 0) {
  21536. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21537. x963KeySz, &keyImp);
  21538. }
  21539. /* Pass in bad args. */
  21540. if (ret == 0) {
  21541. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21542. x963KeySz, NULL);
  21543. if (ret == BAD_FUNC_ARG) {
  21544. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  21545. x963KeySz, &keyImp);
  21546. }
  21547. if (ret == BAD_FUNC_ARG) {
  21548. ret = 0;
  21549. } else if (ret == 0) {
  21550. ret = WOLFSSL_FATAL_ERROR;
  21551. }
  21552. }
  21553. printf(resultFmt, ret == 0 ? passed : failed);
  21554. fflush(stdout);
  21555. if (wc_FreeRng(&rng) && ret == 0) {
  21556. ret = WOLFSSL_FATAL_ERROR;
  21557. }
  21558. wc_ecc_free(&key);
  21559. wc_ecc_free(&keyImp);
  21560. #ifdef FP_ECC
  21561. wc_ecc_fp_free();
  21562. #endif
  21563. #endif
  21564. return ret;
  21565. } /* END wc_ecc_import_private_key */
  21566. /*
  21567. * Testing wc_ecc_export_private_only()
  21568. */
  21569. static int test_wc_ecc_export_private_only(void)
  21570. {
  21571. int ret = 0;
  21572. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21573. ecc_key key;
  21574. WC_RNG rng;
  21575. byte out[ECC_PRIV_KEY_BUF];
  21576. word32 outlen = sizeof(out);
  21577. /* Init stack variables. */
  21578. XMEMSET(out, 0, outlen);
  21579. XMEMSET(&rng, 0, sizeof(rng));
  21580. XMEMSET(&key, 0, sizeof(key));
  21581. ret = wc_InitRng(&rng);
  21582. if (ret == 0) {
  21583. ret = wc_ecc_init(&key);
  21584. if (ret == 0) {
  21585. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21586. #if defined(WOLFSSL_ASYNC_CRYPT)
  21587. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21588. #endif
  21589. }
  21590. }
  21591. printf(testingFmt, "wc_ecc_export_private_only()");
  21592. if (ret == 0) {
  21593. ret = wc_ecc_export_private_only(&key, out, &outlen);
  21594. }
  21595. /* Pass in bad args. */
  21596. if (ret == 0) {
  21597. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  21598. if (ret == BAD_FUNC_ARG) {
  21599. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  21600. }
  21601. if (ret == BAD_FUNC_ARG) {
  21602. ret = wc_ecc_export_private_only(&key, out, NULL);
  21603. }
  21604. if (ret == BAD_FUNC_ARG) {
  21605. ret = 0;
  21606. } else if (ret == 0) {
  21607. ret = WOLFSSL_FATAL_ERROR;
  21608. }
  21609. }
  21610. printf(resultFmt, ret == 0 ? passed : failed);
  21611. fflush(stdout);
  21612. if (wc_FreeRng(&rng) && ret == 0) {
  21613. ret = WOLFSSL_FATAL_ERROR;
  21614. }
  21615. wc_ecc_free(&key);
  21616. #ifdef FP_ECC
  21617. wc_ecc_fp_free();
  21618. #endif
  21619. #endif
  21620. return ret;
  21621. } /* END test_wc_ecc_export_private_only */
  21622. /*
  21623. * Testing wc_ecc_rs_to_sig()
  21624. */
  21625. static int test_wc_ecc_rs_to_sig(void)
  21626. {
  21627. int ret = 0;
  21628. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21629. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21630. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21631. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21632. const char* zeroStr = "0";
  21633. byte sig[ECC_MAX_SIG_SIZE];
  21634. word32 siglen = (word32)sizeof(sig);
  21635. /*R and S max size is the order of curve. 2^192.*/
  21636. int keySz = KEY24;
  21637. byte r[KEY24];
  21638. byte s[KEY24];
  21639. word32 rlen = (word32)sizeof(r);
  21640. word32 slen = (word32)sizeof(s);
  21641. /* Init stack variables. */
  21642. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21643. XMEMSET(r, 0, keySz);
  21644. XMEMSET(s, 0, keySz);
  21645. printf(testingFmt, "wc_ecc_rs_to_sig()");
  21646. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  21647. /* Test bad args. */
  21648. if (ret == 0) {
  21649. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  21650. if (ret == ECC_BAD_ARG_E) {
  21651. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  21652. }
  21653. if (ret == ECC_BAD_ARG_E) {
  21654. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  21655. }
  21656. if (ret == ECC_BAD_ARG_E) {
  21657. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  21658. }
  21659. if (ret == ECC_BAD_ARG_E) {
  21660. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  21661. }
  21662. if (ret == MP_ZERO_E) {
  21663. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  21664. }
  21665. if (ret == MP_ZERO_E) {
  21666. ret = 0;
  21667. } else {
  21668. ret = WOLFSSL_FATAL_ERROR;
  21669. }
  21670. }
  21671. printf(resultFmt, ret == 0 ? passed : failed);
  21672. fflush(stdout);
  21673. printf(testingFmt, "wc_ecc_sig_to_rs()");
  21674. if (ret == 0) {
  21675. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  21676. }
  21677. /* Test bad args. */
  21678. if (ret == 0) {
  21679. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  21680. if (ret == ECC_BAD_ARG_E) {
  21681. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  21682. }
  21683. if (ret == ECC_BAD_ARG_E) {
  21684. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  21685. }
  21686. if (ret == ECC_BAD_ARG_E) {
  21687. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  21688. }
  21689. if (ret == ECC_BAD_ARG_E) {
  21690. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  21691. }
  21692. if (ret == ECC_BAD_ARG_E) {
  21693. ret = 0;
  21694. } else if (ret == 0) {
  21695. ret = WOLFSSL_FATAL_ERROR;
  21696. }
  21697. }
  21698. printf(resultFmt, ret == 0 ? passed : failed);
  21699. fflush(stdout);
  21700. #endif
  21701. return ret;
  21702. } /* END test_wc_ecc_rs_to_sig */
  21703. static int test_wc_ecc_import_raw(void)
  21704. {
  21705. int ret = 0;
  21706. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21707. ecc_key key;
  21708. const char* qx =
  21709. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21710. const char* qy =
  21711. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21712. const char* d =
  21713. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21714. const char* curveName = "SECP256R1";
  21715. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21716. const char* kNullStr = "";
  21717. #endif
  21718. ret = wc_ecc_init(&key);
  21719. printf(testingFmt, "wc_ecc_import_raw()");
  21720. if (ret == 0) {
  21721. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21722. }
  21723. /* Test bad args. */
  21724. if (ret == 0) {
  21725. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  21726. if (ret == BAD_FUNC_ARG) {
  21727. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  21728. }
  21729. if (ret == BAD_FUNC_ARG) {
  21730. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  21731. }
  21732. if (ret == BAD_FUNC_ARG) {
  21733. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  21734. }
  21735. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21736. if (ret == BAD_FUNC_ARG) {
  21737. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21738. wc_ecc_free(&key);
  21739. #endif
  21740. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  21741. if (ret == ECC_INF_E)
  21742. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  21743. }
  21744. #endif
  21745. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21746. if (ret == BAD_FUNC_ARG) {
  21747. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21748. wc_ecc_free(&key);
  21749. #endif
  21750. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  21751. }
  21752. if (ret == BAD_FUNC_ARG) {
  21753. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21754. wc_ecc_free(&key);
  21755. #endif
  21756. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  21757. }
  21758. #endif
  21759. if (ret == BAD_FUNC_ARG) {
  21760. ret = 0;
  21761. }
  21762. }
  21763. printf(resultFmt, ret == 0 ? passed : failed);
  21764. fflush(stdout);
  21765. wc_ecc_free(&key);
  21766. #endif
  21767. return ret;
  21768. } /* END test_wc_ecc_import_raw */
  21769. static int test_wc_ecc_import_unsigned(void)
  21770. {
  21771. int ret = 0;
  21772. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21773. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  21774. ecc_key key;
  21775. const byte qx[] = {
  21776. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21777. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21778. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21779. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21780. };
  21781. const byte qy[] = {
  21782. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21783. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21784. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21785. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21786. };
  21787. const byte d[] = {
  21788. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21789. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21790. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21791. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21792. };
  21793. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21794. const byte nullBytes[32] = {0};
  21795. #endif
  21796. int curveId = ECC_SECP256R1;
  21797. ret = wc_ecc_init(&key);
  21798. printf(testingFmt, "wc_ecc_import_unsigned()");
  21799. if (ret == 0) {
  21800. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21801. curveId);
  21802. }
  21803. /* Test bad args. */
  21804. if (ret == 0) {
  21805. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21806. curveId);
  21807. if (ret == BAD_FUNC_ARG) {
  21808. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21809. curveId);
  21810. }
  21811. if (ret == BAD_FUNC_ARG) {
  21812. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21813. curveId);
  21814. }
  21815. if (ret == BAD_FUNC_ARG) {
  21816. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21817. ECC_CURVE_INVALID);
  21818. }
  21819. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21820. if (ret == BAD_FUNC_ARG) {
  21821. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21822. (byte*)nullBytes, (byte*)nullBytes, curveId);
  21823. }
  21824. #endif
  21825. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  21826. ret = 0;
  21827. }
  21828. }
  21829. printf(resultFmt, ret == 0 ? passed : failed);
  21830. fflush(stdout);
  21831. wc_ecc_free(&key);
  21832. #endif
  21833. return ret;
  21834. } /* END test_wc_ecc_import_unsigned */
  21835. /*
  21836. * Testing wc_ecc_sig_size()
  21837. */
  21838. static int test_wc_ecc_sig_size(void)
  21839. {
  21840. int ret = 0;
  21841. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21842. ecc_key key;
  21843. WC_RNG rng;
  21844. int keySz = KEY16;
  21845. XMEMSET(&rng, 0, sizeof(rng));
  21846. XMEMSET(&key, 0, sizeof(key));
  21847. ret = wc_InitRng(&rng);
  21848. if (ret == 0) {
  21849. ret = wc_ecc_init(&key);
  21850. if (ret == 0) {
  21851. ret = wc_ecc_make_key(&rng, keySz, &key);
  21852. #if defined(WOLFSSL_ASYNC_CRYPT)
  21853. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21854. #endif
  21855. }
  21856. }
  21857. printf(testingFmt, "wc_ecc_sig_size()");
  21858. if (ret == 0) {
  21859. ret = wc_ecc_sig_size(&key);
  21860. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  21861. ret = 0;
  21862. }
  21863. }
  21864. printf(resultFmt, ret == 0 ? passed : failed);
  21865. fflush(stdout);
  21866. if (wc_FreeRng(&rng) && ret == 0) {
  21867. ret = WOLFSSL_FATAL_ERROR;
  21868. }
  21869. wc_ecc_free(&key);
  21870. #endif
  21871. return ret;
  21872. } /* END test_wc_ecc_sig_size */
  21873. /*
  21874. * Testing wc_ecc_ctx_new()
  21875. */
  21876. static int test_wc_ecc_ctx_new(void)
  21877. {
  21878. int ret = 0;
  21879. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21880. WC_RNG rng;
  21881. ecEncCtx* cli = NULL;
  21882. ecEncCtx* srv = NULL;
  21883. ret = wc_InitRng(&rng);
  21884. printf(testingFmt, "wc_ecc_ctx_new()");
  21885. if (ret == 0) {
  21886. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  21887. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  21888. }
  21889. if (ret == 0 && (cli == NULL || srv == NULL)) {
  21890. ret = WOLFSSL_FATAL_ERROR;
  21891. }
  21892. wc_ecc_ctx_free(cli);
  21893. wc_ecc_ctx_free(srv);
  21894. /* Test bad args. */
  21895. if (ret == 0) {
  21896. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21897. cli = wc_ecc_ctx_new(0, &rng);
  21898. if (cli != NULL) {
  21899. ret = WOLFSSL_FATAL_ERROR;
  21900. }
  21901. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  21902. if (cli != NULL) {
  21903. ret = WOLFSSL_FATAL_ERROR;
  21904. }
  21905. }
  21906. printf(resultFmt, ret == 0 ? passed : failed);
  21907. fflush(stdout);
  21908. if (wc_FreeRng(&rng) && ret == 0) {
  21909. ret = WOLFSSL_FATAL_ERROR;
  21910. }
  21911. wc_ecc_ctx_free(cli);
  21912. #endif
  21913. return ret;
  21914. } /* END test_wc_ecc_ctx_new */
  21915. /*
  21916. * Tesing wc_ecc_reset()
  21917. */
  21918. static int test_wc_ecc_ctx_reset(void)
  21919. {
  21920. int ret = 0;
  21921. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21922. ecEncCtx* ctx = NULL;
  21923. WC_RNG rng;
  21924. ret = wc_InitRng(&rng);
  21925. if (ret == 0) {
  21926. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  21927. ret = WOLFSSL_FATAL_ERROR;
  21928. }
  21929. }
  21930. printf(testingFmt, "wc_ecc_ctx_reset()");
  21931. if (ret == 0) {
  21932. ret = wc_ecc_ctx_reset(ctx, &rng);
  21933. }
  21934. /* Pass in bad args. */
  21935. if (ret == 0) {
  21936. ret = wc_ecc_ctx_reset(NULL, &rng);
  21937. if (ret == BAD_FUNC_ARG) {
  21938. ret = wc_ecc_ctx_reset(ctx, NULL);
  21939. }
  21940. if (ret == BAD_FUNC_ARG) {
  21941. ret = 0;
  21942. } else if (ret == 0) {
  21943. ret = WOLFSSL_FATAL_ERROR;
  21944. }
  21945. }
  21946. printf(resultFmt, ret == 0 ? passed : failed);
  21947. fflush(stdout);
  21948. if (wc_FreeRng(&rng) && ret == 0) {
  21949. ret = WOLFSSL_FATAL_ERROR;
  21950. }
  21951. wc_ecc_ctx_free(ctx);
  21952. #endif
  21953. return ret;
  21954. } /* END test_wc_ecc_ctx_reset */
  21955. /*
  21956. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  21957. */
  21958. static int test_wc_ecc_ctx_set_peer_salt(void)
  21959. {
  21960. int ret = 0;
  21961. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21962. WC_RNG rng;
  21963. ecEncCtx* cliCtx = NULL;
  21964. ecEncCtx* servCtx = NULL;
  21965. const byte* cliSalt = NULL;
  21966. const byte* servSalt = NULL;
  21967. ret = wc_InitRng(&rng);
  21968. if (ret == 0) {
  21969. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  21970. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  21971. ret = WOLFSSL_FATAL_ERROR;
  21972. }
  21973. }
  21974. printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
  21975. /* Test bad args. */
  21976. if (ret == 0) {
  21977. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  21978. if (cliSalt != NULL) {
  21979. ret = WOLFSSL_FATAL_ERROR;
  21980. }
  21981. }
  21982. if (ret == 0) {
  21983. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  21984. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  21985. if (cliSalt == NULL || servSalt == NULL) {
  21986. ret = WOLFSSL_FATAL_ERROR;
  21987. }
  21988. }
  21989. printf(resultFmt, ret == 0 ? passed : failed);
  21990. fflush(stdout);
  21991. printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
  21992. if (ret == 0) {
  21993. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  21994. }
  21995. /* Test bad args. */
  21996. if (ret == 0) {
  21997. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  21998. if (ret == BAD_FUNC_ARG) {
  21999. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  22000. }
  22001. if (ret == BAD_FUNC_ARG) {
  22002. ret = 0;
  22003. } else if (ret == 0) {
  22004. ret = WOLFSSL_FATAL_ERROR;
  22005. }
  22006. }
  22007. printf(resultFmt, ret == 0 ? passed : failed);
  22008. fflush(stdout);
  22009. if (wc_FreeRng(&rng) && ret == 0) {
  22010. ret = WOLFSSL_FATAL_ERROR;
  22011. }
  22012. wc_ecc_ctx_free(cliCtx);
  22013. wc_ecc_ctx_free(servCtx);
  22014. #endif
  22015. return ret;
  22016. } /* END test_wc_ecc_ctx_set_peer_salt */
  22017. /*
  22018. * Testing wc_ecc_ctx_set_info()
  22019. */
  22020. static int test_wc_ecc_ctx_set_info(void)
  22021. {
  22022. int ret = 0;
  22023. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22024. ecEncCtx* ctx = NULL;
  22025. WC_RNG rng;
  22026. const char* optInfo = "Optional Test Info.";
  22027. int optInfoSz = (int)XSTRLEN(optInfo);
  22028. const char* badOptInfo = NULL;
  22029. ret = wc_InitRng(&rng);
  22030. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  22031. ret = WOLFSSL_FATAL_ERROR;
  22032. }
  22033. printf(testingFmt, "wc_ecc_ctx_set_info()");
  22034. if (ret == 0) {
  22035. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  22036. }
  22037. /* Test bad args. */
  22038. if (ret == 0) {
  22039. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  22040. if (ret == BAD_FUNC_ARG) {
  22041. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  22042. }
  22043. if (ret == BAD_FUNC_ARG) {
  22044. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  22045. }
  22046. if (ret == BAD_FUNC_ARG) {
  22047. ret = 0;
  22048. } else if (ret == 0) {
  22049. ret = WOLFSSL_FATAL_ERROR;
  22050. }
  22051. }
  22052. printf(resultFmt, ret == 0 ? passed : failed);
  22053. fflush(stdout);
  22054. if (wc_FreeRng(&rng) && ret == 0) {
  22055. ret = WOLFSSL_FATAL_ERROR;
  22056. }
  22057. wc_ecc_ctx_free(ctx);
  22058. #endif
  22059. return ret;
  22060. } /* END test_wc_ecc_ctx_set_info */
  22061. /*
  22062. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22063. */
  22064. static int test_wc_ecc_encryptDecrypt(void)
  22065. {
  22066. int ret = 0;
  22067. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22068. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22069. ecc_key srvKey, cliKey, tmpKey;
  22070. WC_RNG rng;
  22071. const char* msg = "EccBlock Size 16";
  22072. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22073. #ifdef WOLFSSL_ECIES_OLD
  22074. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22075. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22076. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22077. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22078. #else
  22079. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22080. #endif
  22081. word32 outSz = (word32)sizeof(out);
  22082. byte plain[sizeof("EccBlock Size 16")];
  22083. word32 plainSz = (word32)sizeof(plain);
  22084. int keySz = KEY20;
  22085. /* Init stack variables. */
  22086. XMEMSET(out, 0, outSz);
  22087. XMEMSET(plain, 0, plainSz);
  22088. XMEMSET(&rng, 0, sizeof(rng));
  22089. XMEMSET(&srvKey, 0, sizeof(srvKey));
  22090. XMEMSET(&cliKey, 0, sizeof(cliKey));
  22091. ret = wc_InitRng(&rng);
  22092. if (ret == 0) {
  22093. ret = wc_ecc_init(&cliKey);
  22094. if (ret == 0) {
  22095. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22096. #if defined(WOLFSSL_ASYNC_CRYPT)
  22097. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22098. #endif
  22099. }
  22100. if (ret == 0) {
  22101. ret = wc_ecc_init(&srvKey);
  22102. }
  22103. if (ret == 0) {
  22104. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22105. #if defined(WOLFSSL_ASYNC_CRYPT)
  22106. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22107. #endif
  22108. }
  22109. if (ret == 0) {
  22110. ret = wc_ecc_init(&tmpKey);
  22111. }
  22112. }
  22113. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22114. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22115. !defined(HAVE_SELFTEST)
  22116. if (ret == 0) {
  22117. ret = wc_ecc_set_rng(&srvKey, &rng);
  22118. }
  22119. if (ret == 0) {
  22120. ret = wc_ecc_set_rng(&cliKey, &rng);
  22121. }
  22122. #endif
  22123. printf(testingFmt, "wc_ecc_encrypt()");
  22124. if (ret == 0) {
  22125. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22126. &outSz, NULL);
  22127. }
  22128. if (ret == 0) {
  22129. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  22130. &outSz, NULL);
  22131. if (ret == BAD_FUNC_ARG) {
  22132. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  22133. &outSz, NULL);
  22134. }
  22135. if (ret == BAD_FUNC_ARG) {
  22136. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  22137. &outSz, NULL);
  22138. }
  22139. if (ret == BAD_FUNC_ARG) {
  22140. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22141. &outSz, NULL);
  22142. }
  22143. if (ret == BAD_FUNC_ARG) {
  22144. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22145. NULL, NULL);
  22146. }
  22147. if (ret == BAD_FUNC_ARG) {
  22148. ret = 0;
  22149. } else if (ret == 0) {
  22150. ret = WOLFSSL_FATAL_ERROR;
  22151. }
  22152. }
  22153. printf(resultFmt, ret == 0 ? passed : failed);
  22154. fflush(stdout);
  22155. printf(testingFmt, "wc_ecc_decrypt()");
  22156. #ifdef WOLFSSL_ECIES_OLD
  22157. if (ret == 0) {
  22158. tmpKey.dp = cliKey.dp;
  22159. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  22160. }
  22161. #endif
  22162. if (ret == 0) {
  22163. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  22164. &plainSz, NULL);
  22165. }
  22166. if (ret == 0) {
  22167. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  22168. &plainSz, NULL);
  22169. #ifdef WOLFSSL_ECIES_OLD
  22170. /* NULL parameter allowed in new implementations - public key comes from
  22171. * the message. */
  22172. if (ret == BAD_FUNC_ARG) {
  22173. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  22174. &plainSz, NULL);
  22175. }
  22176. #endif
  22177. if (ret == BAD_FUNC_ARG) {
  22178. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  22179. &plainSz, NULL);
  22180. }
  22181. if (ret == BAD_FUNC_ARG) {
  22182. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  22183. &plainSz, NULL);
  22184. }
  22185. if (ret == BAD_FUNC_ARG) {
  22186. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  22187. plain, NULL, NULL);
  22188. }
  22189. if (ret == BAD_FUNC_ARG) {
  22190. ret = 0;
  22191. } else if (ret == 0) {
  22192. ret = WOLFSSL_FATAL_ERROR;
  22193. }
  22194. }
  22195. if (XMEMCMP(msg, plain, msgSz) != 0) {
  22196. ret = WOLFSSL_FATAL_ERROR;
  22197. }
  22198. printf(resultFmt, ret == 0 ? passed : failed);
  22199. fflush(stdout);
  22200. if (wc_FreeRng(&rng) && ret == 0) {
  22201. ret = WOLFSSL_FATAL_ERROR;
  22202. }
  22203. wc_ecc_free(&tmpKey);
  22204. wc_ecc_free(&cliKey);
  22205. wc_ecc_free(&srvKey);
  22206. #endif
  22207. return ret;
  22208. } /* END test_wc_ecc_encryptDecrypt */
  22209. /*
  22210. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22211. */
  22212. static int test_wc_ecc_del_point(void)
  22213. {
  22214. int ret = 0;
  22215. #if defined(HAVE_ECC)
  22216. ecc_point* pt;
  22217. printf(testingFmt, "wc_ecc_new_point()");
  22218. pt = wc_ecc_new_point();
  22219. if (!pt) {
  22220. ret = WOLFSSL_FATAL_ERROR;
  22221. }
  22222. printf(resultFmt, ret == 0 ? passed : failed);
  22223. fflush(stdout);
  22224. wc_ecc_del_point(pt);
  22225. #endif
  22226. return ret;
  22227. } /* END test_wc_ecc_del_point */
  22228. /*
  22229. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22230. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22231. * and wc_ecc_cmp_point()
  22232. */
  22233. static int test_wc_ecc_pointFns(void)
  22234. {
  22235. int ret = 0;
  22236. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22237. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22238. !defined(WOLFSSL_ATECC608A)
  22239. ecc_key key;
  22240. WC_RNG rng;
  22241. ecc_point* point = NULL;
  22242. ecc_point* cpypt = NULL;
  22243. int idx = 0;
  22244. int keySz = KEY32;
  22245. byte der[DER_SZ(KEY32)];
  22246. word32 derlenChk = 0;
  22247. word32 derSz = DER_SZ(KEY32);
  22248. /* Init stack variables. */
  22249. XMEMSET(der, 0, derSz);
  22250. XMEMSET(&rng, 0, sizeof(rng));
  22251. XMEMSET(&key, 0, sizeof(key));
  22252. ret = wc_InitRng(&rng);
  22253. if (ret == 0) {
  22254. ret = wc_ecc_init(&key);
  22255. if (ret == 0) {
  22256. ret = wc_ecc_make_key(&rng, keySz, &key);
  22257. #if defined(WOLFSSL_ASYNC_CRYPT)
  22258. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22259. #endif
  22260. }
  22261. }
  22262. if (ret == 0) {
  22263. point = wc_ecc_new_point();
  22264. if (!point) {
  22265. ret = WOLFSSL_FATAL_ERROR;
  22266. }
  22267. }
  22268. if (ret == 0) {
  22269. cpypt = wc_ecc_new_point();
  22270. if (!cpypt) {
  22271. ret = WOLFSSL_FATAL_ERROR;
  22272. }
  22273. }
  22274. /* Export */
  22275. printf(testingFmt, "wc_ecc_export_point_der()");
  22276. if (ret == 0) {
  22277. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22278. NULL, &derlenChk);
  22279. /* Check length value. */
  22280. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  22281. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22282. der, &derSz);
  22283. }
  22284. }
  22285. /* Test bad args. */
  22286. if (ret == 0) {
  22287. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  22288. if (ret == ECC_BAD_ARG_E) {
  22289. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  22290. }
  22291. if (ret == ECC_BAD_ARG_E) {
  22292. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22293. der, NULL);
  22294. }
  22295. if (ret == ECC_BAD_ARG_E) {
  22296. ret = 0;
  22297. } else if (ret == 0) {
  22298. ret = WOLFSSL_FATAL_ERROR;
  22299. }
  22300. }
  22301. printf(resultFmt, ret == 0 ? passed : failed);
  22302. fflush(stdout);
  22303. /* Import */
  22304. printf(testingFmt, "wc_ecc_import_point_der()");
  22305. if (ret == 0) {
  22306. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  22307. /* Condition double checks wc_ecc_cmp_point(). */
  22308. if (ret == 0 &&
  22309. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  22310. ret = wc_ecc_cmp_point(&key.pubkey, point);
  22311. }
  22312. }
  22313. /* Test bad args. */
  22314. if (ret == 0) {
  22315. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  22316. if (ret == ECC_BAD_ARG_E) {
  22317. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  22318. }
  22319. if (ret == ECC_BAD_ARG_E) {
  22320. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  22321. }
  22322. if (ret == ECC_BAD_ARG_E) {
  22323. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  22324. }
  22325. if (ret == ECC_BAD_ARG_E) {
  22326. ret = 0;
  22327. } else if (ret == 0) {
  22328. ret = WOLFSSL_FATAL_ERROR;
  22329. }
  22330. }
  22331. printf(resultFmt, ret == 0 ? passed : failed);
  22332. fflush(stdout);
  22333. /* Copy */
  22334. printf(testingFmt, "wc_ecc_copy_point()");
  22335. if (ret == 0) {
  22336. ret = wc_ecc_copy_point(point, cpypt);
  22337. }
  22338. /* Test bad args. */
  22339. if (ret == 0) {
  22340. ret = wc_ecc_copy_point(NULL, cpypt);
  22341. if (ret == ECC_BAD_ARG_E) {
  22342. ret = wc_ecc_copy_point(point, NULL);
  22343. }
  22344. if (ret == ECC_BAD_ARG_E) {
  22345. ret = 0;
  22346. } else if (ret == 0) {
  22347. ret = WOLFSSL_FATAL_ERROR;
  22348. }
  22349. }
  22350. printf(resultFmt, ret == 0 ? passed : failed);
  22351. fflush(stdout);
  22352. printf(testingFmt, "wc_ecc_cmp_point()");
  22353. /* Compare point */
  22354. if (ret == 0) {
  22355. ret = wc_ecc_cmp_point(point, cpypt);
  22356. }
  22357. /* Test bad args. */
  22358. if (ret == 0) {
  22359. ret = wc_ecc_cmp_point(NULL, cpypt);
  22360. if (ret == BAD_FUNC_ARG) {
  22361. ret = wc_ecc_cmp_point(point, NULL);
  22362. }
  22363. if (ret == BAD_FUNC_ARG) {
  22364. ret = 0;
  22365. } else if (ret == 0) {
  22366. ret = WOLFSSL_FATAL_ERROR;
  22367. }
  22368. }
  22369. printf(resultFmt, ret == 0 ? passed : failed);
  22370. fflush(stdout);
  22371. printf(testingFmt, "wc_ecc_point_is_at_infinity()");
  22372. /* At infinity if return == 1, otherwise return == 0. */
  22373. if (ret == 0) {
  22374. ret = wc_ecc_point_is_at_infinity(point);
  22375. }
  22376. /* Test bad args. */
  22377. if (ret == 0) {
  22378. ret = wc_ecc_point_is_at_infinity(NULL);
  22379. if (ret == BAD_FUNC_ARG) {
  22380. ret = 0;
  22381. } else if (ret == 0) {
  22382. ret = WOLFSSL_FATAL_ERROR;
  22383. }
  22384. }
  22385. printf(resultFmt, ret == 0 ? passed : failed);
  22386. fflush(stdout);
  22387. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  22388. #ifdef USE_ECC_B_PARAM
  22389. printf(testingFmt, "wc_ecc_point_is_on_curve()");
  22390. /* On curve if ret == 0 */
  22391. if (ret == 0) {
  22392. ret = wc_ecc_point_is_on_curve(point, idx);
  22393. }
  22394. /* Test bad args. */
  22395. if (ret == 0) {
  22396. ret = wc_ecc_point_is_on_curve(NULL, idx);
  22397. if (ret == BAD_FUNC_ARG) {
  22398. ret = wc_ecc_point_is_on_curve(point, 1000);
  22399. }
  22400. if (ret == ECC_BAD_ARG_E) {
  22401. ret = 0;
  22402. } else if (ret == 0) {
  22403. ret = WOLFSSL_FATAL_ERROR;
  22404. }
  22405. }
  22406. printf(resultFmt, ret == 0 ? passed : failed);
  22407. fflush(stdout);
  22408. #endif /* USE_ECC_B_PARAM */
  22409. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  22410. /* Free */
  22411. wc_ecc_del_point(point);
  22412. wc_ecc_del_point(cpypt);
  22413. wc_ecc_free(&key);
  22414. if (wc_FreeRng(&rng) && ret == 0) {
  22415. ret = WOLFSSL_FATAL_ERROR;
  22416. }
  22417. #endif
  22418. return ret;
  22419. } /* END test_wc_ecc_pointFns */
  22420. /*
  22421. * Testing wc_ecc_sahred_secret_ssh()
  22422. */
  22423. static int test_wc_ecc_shared_secret_ssh(void)
  22424. {
  22425. int ret = 0;
  22426. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22427. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22428. !defined(WOLFSSL_ATECC608A)
  22429. ecc_key key, key2;
  22430. WC_RNG rng;
  22431. int keySz = KEY32;
  22432. int key2Sz = KEY24;
  22433. byte secret[KEY32];
  22434. word32 secretLen = keySz;
  22435. /* Init stack variables. */
  22436. XMEMSET(secret, 0, secretLen);
  22437. XMEMSET(&rng, 0, sizeof(rng));
  22438. XMEMSET(&key, 0, sizeof(key));
  22439. XMEMSET(&key2, 0, sizeof(key2));
  22440. /* Make keys */
  22441. ret = wc_InitRng(&rng);
  22442. if (ret == 0) {
  22443. ret = wc_ecc_init(&key);
  22444. if (ret == 0) {
  22445. ret = wc_ecc_make_key(&rng, keySz, &key);
  22446. #if defined(WOLFSSL_ASYNC_CRYPT)
  22447. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22448. #endif
  22449. }
  22450. if (wc_FreeRng(&rng) && ret == 0) {
  22451. ret = WOLFSSL_FATAL_ERROR;
  22452. }
  22453. }
  22454. if (ret == 0) {
  22455. ret = wc_InitRng(&rng);
  22456. if (ret == 0) {
  22457. ret = wc_ecc_init(&key2);
  22458. }
  22459. if (ret == 0) {
  22460. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22461. #if defined(WOLFSSL_ASYNC_CRYPT)
  22462. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22463. #endif
  22464. }
  22465. }
  22466. printf(testingFmt, "ecc_shared_secret_ssh()");
  22467. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22468. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22469. !defined(HAVE_SELFTEST)
  22470. if (ret == 0) {
  22471. ret = wc_ecc_set_rng(&key, &rng);
  22472. }
  22473. #endif
  22474. if (ret == 0) {
  22475. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  22476. }
  22477. /* Pass in bad args. */
  22478. if (ret == 0) {
  22479. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  22480. if (ret == BAD_FUNC_ARG) {
  22481. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  22482. }
  22483. if (ret == BAD_FUNC_ARG) {
  22484. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  22485. }
  22486. if (ret == BAD_FUNC_ARG) {
  22487. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  22488. }
  22489. if (ret == BAD_FUNC_ARG) {
  22490. key.type = ECC_PUBLICKEY;
  22491. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  22492. if (ret == ECC_BAD_ARG_E) {
  22493. ret = 0;
  22494. } else if (ret == 0) {
  22495. ret = WOLFSSL_FATAL_ERROR;
  22496. }
  22497. } else if (ret == 0) {
  22498. ret = WOLFSSL_FATAL_ERROR;
  22499. }
  22500. }
  22501. printf(resultFmt, ret == 0 ? passed : failed);
  22502. fflush(stdout);
  22503. if (wc_FreeRng(&rng) && ret == 0) {
  22504. ret = WOLFSSL_FATAL_ERROR;
  22505. }
  22506. wc_ecc_free(&key);
  22507. wc_ecc_free(&key2);
  22508. #ifdef FP_ECC
  22509. wc_ecc_fp_free();
  22510. #endif
  22511. #endif
  22512. return ret;
  22513. } /* END test_wc_ecc_shared_secret_ssh */
  22514. /*
  22515. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22516. */
  22517. static int test_wc_ecc_verify_hash_ex(void)
  22518. {
  22519. int ret = 0;
  22520. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22521. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22522. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22523. ecc_key key;
  22524. WC_RNG rng;
  22525. mp_int r;
  22526. mp_int s;
  22527. mp_int z;
  22528. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22529. unsigned char iHash[] = "Everyone gets Friday off.......";
  22530. unsigned char shortHash[] = TEST_STRING;
  22531. word32 hashlen = sizeof(hash);
  22532. word32 iHashLen = sizeof(iHash);
  22533. word32 shortHashLen = sizeof(shortHash);
  22534. int keySz = KEY32;
  22535. int sig = WOLFSSL_FATAL_ERROR;
  22536. int ver = WOLFSSL_FATAL_ERROR;
  22537. int verify_ok = 0;
  22538. /* Initialize r and s. */
  22539. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  22540. if (ret != MP_OKAY) {
  22541. return MP_INIT_E;
  22542. }
  22543. ret = wc_InitRng(&rng);
  22544. if (ret == 0) {
  22545. ret = wc_ecc_init(&key);
  22546. if (ret == 0) {
  22547. ret = wc_ecc_make_key(&rng, keySz, &key);
  22548. #if defined(WOLFSSL_ASYNC_CRYPT)
  22549. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22550. #endif
  22551. }
  22552. }
  22553. if (ret == 0) {
  22554. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  22555. if (ret == 0) {
  22556. /* verify_ok should be 1. */
  22557. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  22558. if (verify_ok != 1 && ret == 0) {
  22559. ret = WOLFSSL_FATAL_ERROR;
  22560. }
  22561. }
  22562. if (ret == 0) {
  22563. /* verify_ok should be 0 */
  22564. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  22565. &verify_ok, &key);
  22566. if (verify_ok != 0 && ret == 0) {
  22567. ret = WOLFSSL_FATAL_ERROR;
  22568. }
  22569. }
  22570. if (ret == 0) {
  22571. /* verify_ok should be 0. */
  22572. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22573. &verify_ok, &key);
  22574. if (verify_ok != 0 && ret == 0) {
  22575. ret = WOLFSSL_FATAL_ERROR;
  22576. }
  22577. }
  22578. }
  22579. printf(testingFmt, "wc_ecc_sign_hash_ex()");
  22580. /* Test bad args. */
  22581. if (ret == 0) {
  22582. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  22583. == ECC_BAD_ARG_E) {
  22584. sig = 0;
  22585. }
  22586. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  22587. != ECC_BAD_ARG_E) {
  22588. sig = WOLFSSL_FATAL_ERROR;
  22589. }
  22590. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  22591. != ECC_BAD_ARG_E) {
  22592. sig = WOLFSSL_FATAL_ERROR;
  22593. }
  22594. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  22595. != ECC_BAD_ARG_E) {
  22596. sig = WOLFSSL_FATAL_ERROR;
  22597. }
  22598. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  22599. != ECC_BAD_ARG_E) {
  22600. sig = WOLFSSL_FATAL_ERROR;
  22601. }
  22602. }
  22603. printf(resultFmt, sig == 0 ? passed : failed);
  22604. printf(testingFmt, "wc_ecc_verify_hash_ex()");
  22605. /* Test bad args. */
  22606. if (ret == 0) {
  22607. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  22608. == ECC_BAD_ARG_E) {
  22609. ver = 0;
  22610. }
  22611. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22612. &verify_ok, &key) != ECC_BAD_ARG_E) {
  22613. ver = WOLFSSL_FATAL_ERROR;
  22614. }
  22615. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  22616. != MP_ZERO_E) {
  22617. ver = WOLFSSL_FATAL_ERROR;
  22618. }
  22619. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  22620. != MP_ZERO_E) {
  22621. ver = WOLFSSL_FATAL_ERROR;
  22622. }
  22623. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  22624. != MP_ZERO_E) {
  22625. ver = WOLFSSL_FATAL_ERROR;
  22626. }
  22627. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22628. &key) != ECC_BAD_ARG_E) {
  22629. ver = WOLFSSL_FATAL_ERROR;
  22630. }
  22631. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22632. NULL, &key) != ECC_BAD_ARG_E) {
  22633. ver = WOLFSSL_FATAL_ERROR;
  22634. }
  22635. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22636. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  22637. ver = WOLFSSL_FATAL_ERROR;
  22638. }
  22639. }
  22640. printf(resultFmt, ver == 0 ? passed : failed);
  22641. wc_ecc_free(&key);
  22642. mp_free(&r);
  22643. mp_free(&s);
  22644. if (wc_FreeRng(&rng)) {
  22645. return WOLFSSL_FATAL_ERROR;
  22646. }
  22647. if (ret == 0 && (sig != 0 || ver != 0)) {
  22648. ret = WOLFSSL_FATAL_ERROR;
  22649. }
  22650. #endif
  22651. return ret;
  22652. } /* END test_wc_ecc_verify_hash_ex */
  22653. /*
  22654. * Testing wc_ecc_mulmod()
  22655. */
  22656. static int test_wc_ecc_mulmod(void)
  22657. {
  22658. int ret = 0;
  22659. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22660. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22661. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22662. ecc_key key1, key2, key3;
  22663. WC_RNG rng;
  22664. ret = wc_InitRng(&rng);
  22665. if (ret == 0) {
  22666. ret = wc_ecc_init(&key1);
  22667. if (ret == 0) {
  22668. ret = wc_ecc_init(&key2);
  22669. }
  22670. if (ret == 0) {
  22671. ret = wc_ecc_init(&key3);
  22672. }
  22673. if (ret == 0) {
  22674. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22675. #if defined(WOLFSSL_ASYNC_CRYPT)
  22676. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22677. #endif
  22678. }
  22679. wc_FreeRng(&rng);
  22680. }
  22681. if (ret == 0) {
  22682. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  22683. ECC_SECP256R1);
  22684. if (ret == 0) {
  22685. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22686. key1.dp->prime, ECC_SECP256R1);
  22687. }
  22688. }
  22689. printf(testingFmt, "wc_ecc_mulmod()");
  22690. if (ret == 0) {
  22691. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  22692. &key3.k, 1);
  22693. }
  22694. /* Test bad args. */
  22695. if (ret == 0) {
  22696. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  22697. &key3.k, 1);
  22698. if (ret == ECC_BAD_ARG_E) {
  22699. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  22700. &key3.k, 1);
  22701. }
  22702. if (ret == ECC_BAD_ARG_E) {
  22703. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  22704. &key3.k, 1);
  22705. }
  22706. if (ret == ECC_BAD_ARG_E) {
  22707. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  22708. &key2.k, NULL, 1);
  22709. }
  22710. if (ret == ECC_BAD_ARG_E) {
  22711. ret = 0;
  22712. } else if (ret == 0) {
  22713. ret = WOLFSSL_FATAL_ERROR;
  22714. }
  22715. }
  22716. printf(resultFmt, ret == 0 ? passed : failed);
  22717. fflush(stdout);
  22718. wc_ecc_free(&key1);
  22719. wc_ecc_free(&key2);
  22720. wc_ecc_free(&key3);
  22721. #ifdef FP_ECC
  22722. wc_ecc_fp_free();
  22723. #endif
  22724. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22725. return ret;
  22726. } /* END test_wc_ecc_mulmod */
  22727. /*
  22728. * Testing wc_ecc_is_valid_idx()
  22729. */
  22730. static int test_wc_ecc_is_valid_idx(void)
  22731. {
  22732. int ret = 0;
  22733. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22734. ecc_key key;
  22735. WC_RNG rng;
  22736. int iVal = -2;
  22737. int iVal2 = 3000;
  22738. XMEMSET(&rng, 0, sizeof(rng));
  22739. XMEMSET(&key, 0, sizeof(key));
  22740. ret = wc_InitRng(&rng);
  22741. if (ret == 0) {
  22742. ret = wc_ecc_init(&key);
  22743. if (ret == 0) {
  22744. ret = wc_ecc_make_key(&rng, 32, &key);
  22745. #if defined(WOLFSSL_ASYNC_CRYPT)
  22746. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22747. #endif
  22748. }
  22749. }
  22750. printf(testingFmt, "wc_ecc_is_valid_idx()");
  22751. if (ret == 0) {
  22752. ret = wc_ecc_is_valid_idx(key.idx);
  22753. if (ret == 1) {
  22754. ret = 0;
  22755. } else {
  22756. ret = WOLFSSL_FATAL_ERROR;
  22757. }
  22758. }
  22759. /* Test bad args. */
  22760. if (ret == 0) {
  22761. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  22762. if (ret == 0) {
  22763. ret = wc_ecc_is_valid_idx(iVal2);
  22764. }
  22765. if (ret != 0) {
  22766. ret = WOLFSSL_FATAL_ERROR;
  22767. }
  22768. }
  22769. printf(resultFmt, ret == 0 ? passed : failed);
  22770. fflush(stdout);
  22771. if (wc_FreeRng(&rng) && ret == 0) {
  22772. ret = WOLFSSL_FATAL_ERROR;
  22773. }
  22774. wc_ecc_free(&key);
  22775. #ifdef FP_ECC
  22776. wc_ecc_fp_free();
  22777. #endif
  22778. #endif
  22779. return ret;
  22780. } /* END test_wc_ecc_is_valid_idx */
  22781. /*
  22782. * Testing wc_ecc_get_curve_id_from_oid()
  22783. */
  22784. static int test_wc_ecc_get_curve_id_from_oid(void)
  22785. {
  22786. int ret = 0;
  22787. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22788. !defined(HAVE_FIPS)
  22789. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22790. word32 len = sizeof(oid);
  22791. printf(testingFmt, "wc_ecc_get_curve_id_from_oid()");
  22792. /* Bad Cases */
  22793. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  22794. if (ret == BAD_FUNC_ARG) {
  22795. ret = 0;
  22796. }
  22797. if (ret == 0) {
  22798. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  22799. if (ret == ECC_CURVE_INVALID) {
  22800. ret = 0;
  22801. }
  22802. }
  22803. /* Good Case */
  22804. if (ret == 0) {
  22805. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  22806. if (ret == ECC_SECP256R1) {
  22807. ret = 0;
  22808. }
  22809. }
  22810. printf(resultFmt, ret == 0 ? passed : failed);
  22811. fflush(stdout);
  22812. #endif
  22813. return ret;
  22814. }/* END test_wc_ecc_get_curve_id_from_oid */
  22815. /*
  22816. * Testing wc_ecc_sig_size_calc()
  22817. */
  22818. static int test_wc_ecc_sig_size_calc(void)
  22819. {
  22820. int ret = 0;
  22821. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22822. ecc_key key;
  22823. WC_RNG rng;
  22824. int sz = 0;
  22825. printf(testingFmt, "wc_ecc_sig_size_calc()");
  22826. ret = wc_InitRng(&rng);
  22827. if (ret == 0) {
  22828. ret = wc_ecc_init(&key);
  22829. if (ret == 0) {
  22830. ret = wc_ecc_make_key(&rng, 16, &key);
  22831. #if defined(WOLFSSL_ASYNC_CRYPT)
  22832. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22833. #endif
  22834. }
  22835. sz = key.dp->size;
  22836. }
  22837. if (ret == 0) {
  22838. ret = wc_ecc_sig_size_calc(sz);
  22839. if (ret > 0) {
  22840. ret = 0;
  22841. }
  22842. }
  22843. printf(resultFmt, ret == 0 ? passed : failed);
  22844. fflush(stdout);
  22845. wc_ecc_free(&key);
  22846. wc_FreeRng(&rng);
  22847. #endif
  22848. return ret;
  22849. } /* END test_wc_ecc_sig_size_calc */
  22850. /*
  22851. * Testing ToTraditional
  22852. */
  22853. static int test_ToTraditional(void)
  22854. {
  22855. int ret = 0;
  22856. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22857. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22858. defined(OPENSSL_EXTRA_X509_SMALL))
  22859. XFILE f;
  22860. byte input[TWOK_BUF];
  22861. word32 sz;
  22862. printf(testingFmt, "ToTraditional()");
  22863. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  22864. AssertTrue((f != XBADFILE));
  22865. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  22866. XFCLOSE(f);
  22867. /* Good case */
  22868. ret = ToTraditional(input, sz);
  22869. if (ret > 0) {
  22870. ret = 0;
  22871. }
  22872. /* Bad cases */
  22873. if (ret == 0) {
  22874. ret = ToTraditional(NULL, 0);
  22875. if (ret == BAD_FUNC_ARG) {
  22876. ret = 0;
  22877. }
  22878. }
  22879. if (ret == 0) {
  22880. ret = ToTraditional(NULL, sz);
  22881. if (ret == BAD_FUNC_ARG) {
  22882. ret = 0;
  22883. }
  22884. }
  22885. if (ret == 0) {
  22886. ret = ToTraditional(input, 0);
  22887. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  22888. ret = 0;
  22889. }
  22890. }
  22891. printf(resultFmt, ret == 0 ? passed : failed);
  22892. fflush(stdout);
  22893. #endif
  22894. return ret;
  22895. }/* End test_ToTraditional*/
  22896. /*
  22897. * Testing wc_EccPrivateKeyToDer
  22898. */
  22899. static int test_wc_EccPrivateKeyToDer(void)
  22900. {
  22901. int ret = 0;
  22902. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22903. byte output[ONEK_BUF];
  22904. ecc_key eccKey;
  22905. WC_RNG rng;
  22906. word32 inLen;
  22907. printf(testingFmt, "wc_EccPrivateKeyToDer()");
  22908. ret = wc_InitRng(&rng);
  22909. if (ret == 0) {
  22910. ret = wc_ecc_init(&eccKey);
  22911. if (ret == 0) {
  22912. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22913. #if defined(WOLFSSL_ASYNC_CRYPT)
  22914. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22915. #endif
  22916. }
  22917. inLen = (word32)sizeof(output);
  22918. /* Bad Cases */
  22919. if (ret == 0) {
  22920. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  22921. if (ret == BAD_FUNC_ARG) {
  22922. ret = 0;
  22923. }
  22924. }
  22925. if (ret == 0) {
  22926. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  22927. if (ret == BAD_FUNC_ARG) {
  22928. ret = 0;
  22929. }
  22930. }
  22931. if (ret == 0) {
  22932. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  22933. if (ret == LENGTH_ONLY_E) {
  22934. ret = 0;
  22935. }
  22936. }
  22937. if (ret == 0) {
  22938. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  22939. if (ret == BAD_FUNC_ARG) {
  22940. ret = 0;
  22941. }
  22942. }
  22943. /*Good Case */
  22944. if (ret == 0) {
  22945. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  22946. if (ret > 0) {
  22947. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  22948. /* test importing private only into a PKEY struct */
  22949. EC_KEY* ec;
  22950. EVP_PKEY* pkey;
  22951. const unsigned char* der = output;
  22952. pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
  22953. AssertNotNull(pkey);
  22954. der = output;
  22955. ec = d2i_ECPrivateKey(NULL, &der, ret);
  22956. AssertNotNull(ec);
  22957. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  22958. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  22959. #endif
  22960. ret = 0;
  22961. }
  22962. }
  22963. wc_ecc_free(&eccKey);
  22964. }
  22965. wc_FreeRng(&rng);
  22966. printf(resultFmt, ret == 0 ? passed : failed);
  22967. fflush(stdout);
  22968. #endif
  22969. return ret;
  22970. }/* End test_wc_EccPrivateKeyToDer*/
  22971. /*
  22972. * Testing wc_DhPublicKeyDecode
  22973. */
  22974. static int test_wc_DhPublicKeyDecode(void)
  22975. {
  22976. int ret = 0;
  22977. #ifndef NO_DH
  22978. word32 inOutIdx;
  22979. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  22980. DhKey key;
  22981. AssertIntEQ(wc_InitDhKey(&key), 0);
  22982. printf(testingFmt, "wc_DhPublicKeyDecode()");
  22983. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  22984. BAD_FUNC_ARG);
  22985. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22986. BAD_FUNC_ARG);
  22987. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22988. BAD_FUNC_ARG);
  22989. inOutIdx = 0;
  22990. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  22991. BAD_FUNC_ARG);
  22992. inOutIdx = 0;
  22993. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  22994. BAD_FUNC_ARG);
  22995. inOutIdx = 0;
  22996. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  22997. sizeof_dh_pub_key_der_2048), 0);
  22998. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  22999. key.pub.used != 0 && key.priv.used == 0);
  23000. wc_FreeDhKey(&key);
  23001. printf(resultFmt, passed);
  23002. #endif
  23003. (void)inOutIdx;
  23004. #endif /* !NO_DH */
  23005. return ret;
  23006. }
  23007. /*
  23008. * Testing wc_Ed25519KeyToDer
  23009. */
  23010. static int test_wc_Ed25519KeyToDer(void)
  23011. {
  23012. int ret = 0;
  23013. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23014. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23015. byte output[ONEK_BUF];
  23016. ed25519_key ed25519Key;
  23017. WC_RNG rng;
  23018. word32 inLen;
  23019. printf(testingFmt, "wc_Ed25519KeyToDer()");
  23020. ret = wc_InitRng(&rng);
  23021. if (ret == 0) {
  23022. ret = wc_ed25519_init(&ed25519Key);
  23023. if (ret == 0) {
  23024. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23025. }
  23026. inLen = (word32)sizeof(output);
  23027. /* Bad Cases */
  23028. if (ret == 0) {
  23029. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  23030. if (ret == BAD_FUNC_ARG) {
  23031. ret = 0;
  23032. }
  23033. }
  23034. if (ret == 0) {
  23035. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  23036. if (ret == BAD_FUNC_ARG) {
  23037. ret = 0;
  23038. }
  23039. }
  23040. if (ret == 0) {
  23041. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  23042. if (ret == BAD_FUNC_ARG) {
  23043. ret = 0;
  23044. }
  23045. }
  23046. /* Good Cases */
  23047. if (ret == 0) {
  23048. /* length only */
  23049. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  23050. if (ret > 0) {
  23051. ret = 0;
  23052. }
  23053. }
  23054. if (ret == 0) {
  23055. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  23056. if (ret > 0) {
  23057. ret = 0;
  23058. }
  23059. }
  23060. wc_ed25519_free(&ed25519Key);
  23061. }
  23062. wc_FreeRng(&rng);
  23063. printf(resultFmt, ret == 0 ? passed : failed);
  23064. fflush(stdout);
  23065. #endif
  23066. return ret;
  23067. }/* End test_wc_Ed25519KeyToDer*/
  23068. /*
  23069. * Testing wc_Ed25519PrivateKeyToDer
  23070. */
  23071. static int test_wc_Ed25519PrivateKeyToDer(void)
  23072. {
  23073. int ret = 0;
  23074. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23075. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23076. byte output[ONEK_BUF];
  23077. ed25519_key ed25519PrivKey;
  23078. WC_RNG rng;
  23079. word32 inLen;
  23080. printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
  23081. ret = wc_InitRng(&rng);
  23082. if (ret == 0) {
  23083. ret = wc_ed25519_init(&ed25519PrivKey);
  23084. if (ret == 0) {
  23085. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  23086. }
  23087. inLen = (word32)sizeof(output);
  23088. /* Bad Cases */
  23089. if (ret == 0) {
  23090. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  23091. if (ret == BAD_FUNC_ARG) {
  23092. ret = 0;
  23093. }
  23094. }
  23095. if (ret == 0) {
  23096. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  23097. if (ret == BAD_FUNC_ARG) {
  23098. ret = 0;
  23099. }
  23100. }
  23101. if (ret == 0) {
  23102. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  23103. if (ret == BAD_FUNC_ARG) {
  23104. ret = 0;
  23105. }
  23106. }
  23107. /* Good Cases */
  23108. if (ret == 0) {
  23109. /* length only */
  23110. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  23111. if (ret > 0) {
  23112. ret = 0;
  23113. }
  23114. }
  23115. if (ret == 0) {
  23116. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  23117. if (ret > 0) {
  23118. ret = 0;
  23119. }
  23120. }
  23121. wc_ed25519_free(&ed25519PrivKey);
  23122. }
  23123. wc_FreeRng(&rng);
  23124. printf(resultFmt, ret == 0 ? passed : failed);
  23125. fflush(stdout);
  23126. #endif
  23127. return ret;
  23128. }/* End test_wc_Ed25519PrivateKeyToDer*/
  23129. /*
  23130. * Testing wc_Ed448KeyToDer
  23131. */
  23132. static int test_wc_Ed448KeyToDer(void)
  23133. {
  23134. int ret = 0;
  23135. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23136. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23137. byte output[ONEK_BUF];
  23138. ed448_key ed448Key;
  23139. WC_RNG rng;
  23140. word32 inLen;
  23141. printf(testingFmt, "wc_Ed448KeyToDer()");
  23142. ret = wc_InitRng(&rng);
  23143. if (ret == 0) {
  23144. ret = wc_ed448_init(&ed448Key);
  23145. if (ret == 0) {
  23146. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23147. }
  23148. inLen = sizeof(output);
  23149. /* Bad Cases */
  23150. if (ret == 0) {
  23151. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  23152. if (ret == BAD_FUNC_ARG) {
  23153. ret = 0;
  23154. }
  23155. }
  23156. if (ret == 0) {
  23157. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  23158. if (ret == BAD_FUNC_ARG) {
  23159. ret = 0;
  23160. }
  23161. }
  23162. if (ret == 0) {
  23163. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  23164. if (ret == BAD_FUNC_ARG) {
  23165. ret = 0;
  23166. }
  23167. }
  23168. /* Good Cases */
  23169. if (ret == 0) {
  23170. /* length only */
  23171. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  23172. if (ret > 0) {
  23173. ret = 0;
  23174. }
  23175. }
  23176. if (ret == 0) {
  23177. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  23178. if (ret > 0) {
  23179. ret = 0;
  23180. }
  23181. }
  23182. wc_ed448_free(&ed448Key);
  23183. }
  23184. wc_FreeRng(&rng);
  23185. printf(resultFmt, ret == 0 ? passed : failed);
  23186. fflush(stdout);
  23187. #endif
  23188. return ret;
  23189. }/* End test_wc_Ed448KeyToDer*/
  23190. /*
  23191. * Testing wc_Ed448PrivateKeyToDer
  23192. */
  23193. static int test_wc_Ed448PrivateKeyToDer(void)
  23194. {
  23195. int ret = 0;
  23196. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23197. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23198. byte output[ONEK_BUF];
  23199. ed448_key ed448PrivKey;
  23200. WC_RNG rng;
  23201. word32 inLen;
  23202. printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
  23203. ret = wc_InitRng(&rng);
  23204. if (ret == 0) {
  23205. ret = wc_ed448_init(&ed448PrivKey);
  23206. if (ret == 0) {
  23207. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  23208. }
  23209. inLen = sizeof(output);
  23210. /* Bad Cases */
  23211. if (ret == 0) {
  23212. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  23213. if (ret == BAD_FUNC_ARG) {
  23214. ret = 0;
  23215. }
  23216. }
  23217. if (ret == 0) {
  23218. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  23219. if (ret == BAD_FUNC_ARG) {
  23220. ret = 0;
  23221. }
  23222. }
  23223. if (ret == 0) {
  23224. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  23225. if (ret == BAD_FUNC_ARG) {
  23226. ret = 0;
  23227. }
  23228. }
  23229. /* Good cases */
  23230. if (ret == 0) {
  23231. /* length only */
  23232. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  23233. if (ret > 0) {
  23234. ret = 0;
  23235. }
  23236. }
  23237. if (ret == 0) {
  23238. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  23239. if (ret > 0) {
  23240. ret = 0;
  23241. }
  23242. }
  23243. wc_ed448_free(&ed448PrivKey);
  23244. }
  23245. wc_FreeRng(&rng);
  23246. printf(resultFmt, ret == 0 ? passed : failed);
  23247. fflush(stdout);
  23248. #endif
  23249. return ret;
  23250. }/* End test_wc_Ed448PrivateKeyToDer*/
  23251. /*
  23252. * Testing wc_SetSubjectBuffer
  23253. */
  23254. static int test_wc_SetSubjectBuffer(void)
  23255. {
  23256. int ret = 0;
  23257. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  23258. Cert cert;
  23259. FILE* file;
  23260. byte* der;
  23261. word32 derSz;
  23262. printf(testingFmt, "wc_SetSubjectBuffer()");
  23263. derSz = FOURK_BUF;
  23264. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23265. if (der == NULL) {
  23266. ret = -1;
  23267. }
  23268. if (ret == 0) {
  23269. file = XFOPEN("./certs/ca-cert.der", "rb");
  23270. if (file != NULL) {
  23271. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  23272. XFCLOSE(file);
  23273. }
  23274. else {
  23275. ret = -1;
  23276. }
  23277. }
  23278. if (ret == 0) {
  23279. ret = wc_InitCert(&cert);
  23280. }
  23281. if (ret == 0) {
  23282. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  23283. }
  23284. if (ret == 0) {
  23285. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  23286. if (ret == BAD_FUNC_ARG) {
  23287. ret = 0;
  23288. }
  23289. }
  23290. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23291. printf(resultFmt, ret == 0 ? passed : failed);
  23292. fflush(stdout);
  23293. #endif
  23294. return ret;
  23295. }/* End test_wc_SetSubjectBuffer*/
  23296. /*
  23297. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23298. */
  23299. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23300. {
  23301. int ret = 0;
  23302. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23303. WC_RNG rng;
  23304. Cert cert;
  23305. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23306. ed25519_key ed25519Key;
  23307. #endif
  23308. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23309. RsaKey rsaKey;
  23310. int bits = 2048;
  23311. #endif
  23312. #if defined(HAVE_ECC)
  23313. ecc_key eccKey;
  23314. #endif
  23315. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23316. ed448_key ed448Key;
  23317. #endif
  23318. printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
  23319. #ifndef HAVE_FIPS
  23320. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23321. #else
  23322. ret = wc_InitRng(&rng);
  23323. #endif
  23324. wc_InitCert(&cert);
  23325. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23326. if (ret == 0) { /*ED25519*/
  23327. ret = wc_ed25519_init(&ed25519Key);
  23328. if (ret == 0) {
  23329. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23330. }
  23331. if (ret == 0) {
  23332. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23333. &ed25519Key);
  23334. }
  23335. wc_ed25519_free(&ed25519Key);
  23336. }
  23337. #endif
  23338. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23339. if (ret == 0) { /*RSA*/
  23340. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23341. if (ret == 0) {
  23342. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23343. }
  23344. if (ret == 0) {
  23345. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23346. }
  23347. wc_FreeRsaKey(&rsaKey);
  23348. }
  23349. #endif
  23350. #if defined(HAVE_ECC)
  23351. if (ret == 0) { /*ECC*/
  23352. ret = wc_ecc_init(&eccKey);
  23353. if (ret == 0) {
  23354. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23355. #if defined(WOLFSSL_ASYNC_CRYPT)
  23356. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23357. #endif
  23358. }
  23359. if (ret == 0) {
  23360. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23361. }
  23362. wc_ecc_free(&eccKey);
  23363. }
  23364. #endif
  23365. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23366. if (ret == 0) { /*ED448*/
  23367. ret = wc_ed448_init(&ed448Key);
  23368. if (ret == 0) {
  23369. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23370. }
  23371. if (ret == 0) {
  23372. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23373. &ed448Key);
  23374. }
  23375. wc_ed448_free(&ed448Key);
  23376. }
  23377. #endif
  23378. printf(resultFmt, ret == 0 ? passed : failed);
  23379. fflush(stdout);
  23380. wc_FreeRng(&rng);
  23381. #endif
  23382. return ret;
  23383. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23384. /*
  23385. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23386. */
  23387. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23388. {
  23389. int ret = 0;
  23390. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23391. WC_RNG rng;
  23392. Cert cert;
  23393. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23394. ed25519_key ed25519Key;
  23395. #endif
  23396. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23397. RsaKey rsaKey;
  23398. int bits = 2048;
  23399. #endif
  23400. #if defined(HAVE_ECC)
  23401. ecc_key eccKey;
  23402. #endif
  23403. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23404. ed448_key ed448Key;
  23405. #endif
  23406. printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
  23407. #ifndef HAVE_FIPS
  23408. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23409. #else
  23410. ret = wc_InitRng(&rng);
  23411. #endif
  23412. wc_InitCert(&cert);
  23413. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23414. if (ret == 0) { /*ED25519*/
  23415. ret = wc_ed25519_init(&ed25519Key);
  23416. if (ret == 0) {
  23417. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23418. }
  23419. if (ret == 0) {
  23420. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23421. &ed25519Key);
  23422. }
  23423. wc_ed25519_free(&ed25519Key);
  23424. }
  23425. #endif
  23426. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23427. if (ret == 0) { /*RSA*/
  23428. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23429. if (ret == 0) {
  23430. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23431. }
  23432. if (ret == 0) {
  23433. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23434. }
  23435. wc_FreeRsaKey(&rsaKey);
  23436. }
  23437. #endif
  23438. #if defined(HAVE_ECC)
  23439. if (ret == 0) { /*ECC*/
  23440. ret = wc_ecc_init(&eccKey);
  23441. if (ret == 0) {
  23442. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23443. #if defined(WOLFSSL_ASYNC_CRYPT)
  23444. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23445. #endif
  23446. }
  23447. if (ret == 0) {
  23448. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23449. }
  23450. wc_ecc_free(&eccKey);
  23451. }
  23452. #endif
  23453. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23454. if (ret == 0) { /*ED448*/
  23455. ret = wc_ed448_init(&ed448Key);
  23456. if (ret == 0) {
  23457. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23458. }
  23459. if (ret == 0) {
  23460. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23461. &ed448Key);
  23462. }
  23463. wc_ed448_free(&ed448Key);
  23464. }
  23465. #endif
  23466. printf(resultFmt, ret == 0 ? passed : failed);
  23467. fflush(stdout);
  23468. wc_FreeRng(&rng);
  23469. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23470. return ret;
  23471. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23472. /*
  23473. * Testing wc_PKCS7_New()
  23474. */
  23475. static int test_wc_PKCS7_New (void)
  23476. {
  23477. #if defined(HAVE_PKCS7)
  23478. PKCS7* pkcs7;
  23479. void* heap = NULL;
  23480. printf(testingFmt, "wc_PKCS7_New()");
  23481. pkcs7 = wc_PKCS7_New(heap, devId);
  23482. AssertNotNull(pkcs7);
  23483. printf(resultFmt, passed);
  23484. wc_PKCS7_Free(pkcs7);
  23485. #endif
  23486. return 0;
  23487. } /* END test-wc_PKCS7_New */
  23488. /*
  23489. * Testing wc_PKCS7_Init()
  23490. */
  23491. static int test_wc_PKCS7_Init (void)
  23492. {
  23493. #if defined(HAVE_PKCS7)
  23494. PKCS7* pkcs7;
  23495. void* heap = NULL;
  23496. printf(testingFmt, "wc_PKCS7_Init()");
  23497. pkcs7 = wc_PKCS7_New(heap, devId);
  23498. AssertNotNull(pkcs7);
  23499. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  23500. /* Pass in bad args. */
  23501. AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
  23502. printf(resultFmt, passed);
  23503. wc_PKCS7_Free(pkcs7);
  23504. #endif
  23505. return 0;
  23506. } /* END test-wc_PKCS7_Init */
  23507. /*
  23508. * Testing wc_PKCS7_InitWithCert()
  23509. */
  23510. static int test_wc_PKCS7_InitWithCert (void)
  23511. {
  23512. #if defined(HAVE_PKCS7)
  23513. PKCS7* pkcs7;
  23514. #ifndef NO_RSA
  23515. #if defined(USE_CERT_BUFFERS_2048)
  23516. unsigned char cert[sizeof(client_cert_der_2048)];
  23517. int certSz = (int)sizeof(cert);
  23518. XMEMSET(cert, 0, certSz);
  23519. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23520. #elif defined(USE_CERT_BUFFERS_1024)
  23521. unsigned char cert[sizeof(client_cert_der_1024)];
  23522. int certSz = (int)sizeof(cert);
  23523. XMEMSET(cert, 0, certSz);
  23524. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23525. #else
  23526. unsigned char cert[ONEK_BUF];
  23527. XFILE fp;
  23528. int certSz;
  23529. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23530. AssertTrue(fp != XBADFILE);
  23531. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23532. XFCLOSE(fp);
  23533. #endif
  23534. #elif defined(HAVE_ECC)
  23535. #if defined(USE_CERT_BUFFERS_256)
  23536. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23537. int certSz = (int)sizeof(cert);
  23538. XMEMSET(cert, 0, certSz);
  23539. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23540. #else
  23541. unsigned char cert[ONEK_BUF];
  23542. XFILE fp;
  23543. int certSz;
  23544. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23545. AssertTrue(fp != XBADFILE);
  23546. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  23547. XFCLOSE(fp);
  23548. #endif
  23549. #else
  23550. #error PKCS7 requires ECC or RSA
  23551. #endif
  23552. #ifdef HAVE_ECC
  23553. {
  23554. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23555. static unsigned char certWithInvalidEccKey[] = {
  23556. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23557. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23558. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23559. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23560. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23561. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23562. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23563. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23564. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23565. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23566. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23567. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23568. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23569. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23570. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23571. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23572. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23573. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23574. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23575. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23576. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23577. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23578. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23579. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23580. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23581. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23582. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23583. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23584. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23585. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23586. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23587. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23588. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23589. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23590. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23591. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23592. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23593. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23594. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23595. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23596. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23597. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23598. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23599. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23600. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23601. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23602. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23603. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23604. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23605. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23606. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23607. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23608. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23609. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23610. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23611. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23612. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23613. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23614. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23615. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23616. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23617. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23618. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23619. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23620. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23621. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23622. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23623. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23624. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23625. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23626. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23627. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23628. 0x08, 0xA8, 0xB4};
  23629. #endif
  23630. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  23631. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23632. /* If initialization is not successful, it's free'd in init func. */
  23633. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  23634. wc_PKCS7_Free(pkcs7);
  23635. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23636. /* Valid initialization usage. */
  23637. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23638. /* Pass in bad args. No need free for null checks, free at end.*/
  23639. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23640. BAD_FUNC_ARG);
  23641. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23642. BAD_FUNC_ARG);
  23643. #ifdef HAVE_ECC
  23644. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23645. sizeof(certWithInvalidEccKey)), 0);
  23646. }
  23647. #endif
  23648. printf(resultFmt, passed);
  23649. wc_PKCS7_Free(pkcs7);
  23650. #endif
  23651. return 0;
  23652. } /* END test_wc_PKCS7_InitWithCert */
  23653. /*
  23654. * Testing wc_PKCS7_EncodeData()
  23655. */
  23656. static int test_wc_PKCS7_EncodeData (void)
  23657. {
  23658. #if defined(HAVE_PKCS7)
  23659. PKCS7* pkcs7;
  23660. byte output[FOURK_BUF];
  23661. byte data[] = "My encoded DER cert.";
  23662. #ifndef NO_RSA
  23663. #if defined(USE_CERT_BUFFERS_2048)
  23664. unsigned char cert[sizeof(client_cert_der_2048)];
  23665. unsigned char key[sizeof(client_key_der_2048)];
  23666. int certSz = (int)sizeof(cert);
  23667. int keySz = (int)sizeof(key);
  23668. XMEMSET(cert, 0, certSz);
  23669. XMEMSET(key, 0, keySz);
  23670. XMEMCPY(cert, client_cert_der_2048, certSz);
  23671. XMEMCPY(key, client_key_der_2048, keySz);
  23672. #elif defined(USE_CERT_BUFFERS_1024)
  23673. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23674. unsigned char key[sizeof_client_key_der_1024];
  23675. int certSz = (int)sizeof(cert);
  23676. int keySz = (int)sizeof(key);
  23677. XMEMSET(cert, 0, certSz);
  23678. XMEMSET(key, 0, keySz);
  23679. XMEMCPY(cert, client_cert_der_1024, certSz);
  23680. XMEMCPY(key, client_key_der_1024, keySz);
  23681. #else
  23682. unsigned char cert[ONEK_BUF];
  23683. unsigned char key[ONEK_BUF];
  23684. XFILE fp;
  23685. int certSz;
  23686. int keySz;
  23687. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23688. AssertTrue(fp != XBADFILE);
  23689. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23690. XFCLOSE(fp);
  23691. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23692. AssertTrue(fp != XBADFILE);
  23693. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23694. XFCLOSE(fp);
  23695. #endif
  23696. #elif defined(HAVE_ECC)
  23697. #if defined(USE_CERT_BUFFERS_256)
  23698. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23699. unsigned char key[sizeof(ecc_clikey_der_256)];
  23700. int certSz = (int)sizeof(cert);
  23701. int keySz = (int)sizeof(key);
  23702. XMEMSET(cert, 0, certSz);
  23703. XMEMSET(key, 0, keySz);
  23704. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23705. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23706. #else
  23707. unsigned char cert[ONEK_BUF];
  23708. unsigned char key[ONEK_BUF];
  23709. XFILE fp;
  23710. int certSz, keySz;
  23711. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23712. AssertTrue(fp != XBADFILE);
  23713. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  23714. XFCLOSE(fp);
  23715. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23716. AssertTrue(fp != XBADFILE);
  23717. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  23718. XFCLOSE(fp);
  23719. #endif
  23720. #endif
  23721. XMEMSET(output, 0, sizeof(output));
  23722. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23723. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23724. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23725. printf(testingFmt, "wc_PKCS7_EncodeData()");
  23726. pkcs7->content = data;
  23727. pkcs7->contentSz = sizeof(data);
  23728. pkcs7->privateKey = key;
  23729. pkcs7->privateKeySz = keySz;
  23730. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23731. /* Test bad args. */
  23732. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23733. BAD_FUNC_ARG);
  23734. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23735. BAD_FUNC_ARG);
  23736. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23737. printf(resultFmt, passed);
  23738. wc_PKCS7_Free(pkcs7);
  23739. #endif
  23740. return 0;
  23741. } /* END test_wc_PKCS7_EncodeData */
  23742. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23743. !defined(NO_RSA) && !defined(NO_SHA256)
  23744. /* RSA sign raw digest callback */
  23745. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23746. byte* out, word32 outSz, byte* privateKey,
  23747. word32 privateKeySz, int devid, int hashOID)
  23748. {
  23749. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23750. byte digInfoEncoding[] = {
  23751. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23752. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23753. 0x00, 0x04, 0x20
  23754. };
  23755. int ret;
  23756. byte digestInfo[ONEK_BUF];
  23757. byte sig[FOURK_BUF];
  23758. word32 digestInfoSz = 0;
  23759. word32 idx = 0;
  23760. RsaKey rsa;
  23761. /* SHA-256 required only for this example callback due to above
  23762. * digInfoEncoding[] */
  23763. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23764. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23765. (hashOID != SHA256h)) {
  23766. return -1;
  23767. }
  23768. /* build DigestInfo */
  23769. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23770. digestInfoSz += sizeof(digInfoEncoding);
  23771. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23772. digestInfoSz += digestSz;
  23773. /* set up RSA key */
  23774. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23775. if (ret != 0) {
  23776. return ret;
  23777. }
  23778. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23779. /* sign DigestInfo */
  23780. if (ret == 0) {
  23781. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23782. &rsa, pkcs7->rng);
  23783. if (ret > 0) {
  23784. if (ret > (int)outSz) {
  23785. /* output buffer too small */
  23786. ret = -1;
  23787. } else {
  23788. /* success, ret holds sig size */
  23789. XMEMCPY(out, sig, ret);
  23790. }
  23791. }
  23792. }
  23793. wc_FreeRsaKey(&rsa);
  23794. return ret;
  23795. }
  23796. #endif
  23797. /*
  23798. * Testing wc_PKCS7_EncodeSignedData()
  23799. */
  23800. static int test_wc_PKCS7_EncodeSignedData(void)
  23801. {
  23802. #if defined(HAVE_PKCS7)
  23803. PKCS7* pkcs7;
  23804. WC_RNG rng;
  23805. byte output[FOURK_BUF];
  23806. byte badOut[1];
  23807. word32 outputSz = (word32)sizeof(output);
  23808. word32 badOutSz = 0;
  23809. byte data[] = "Test data to encode.";
  23810. #ifndef NO_RSA
  23811. #if defined(USE_CERT_BUFFERS_2048)
  23812. byte key[sizeof(client_key_der_2048)];
  23813. byte cert[sizeof(client_cert_der_2048)];
  23814. word32 keySz = (word32)sizeof(key);
  23815. word32 certSz = (word32)sizeof(cert);
  23816. XMEMSET(key, 0, keySz);
  23817. XMEMSET(cert, 0, certSz);
  23818. XMEMCPY(key, client_key_der_2048, keySz);
  23819. XMEMCPY(cert, client_cert_der_2048, certSz);
  23820. #elif defined(USE_CERT_BUFFERS_1024)
  23821. byte key[sizeof_client_key_der_1024];
  23822. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23823. word32 keySz = (word32)sizeof(key);
  23824. word32 certSz = (word32)sizeof(cert);
  23825. XMEMSET(key, 0, keySz);
  23826. XMEMSET(cert, 0, certSz);
  23827. XMEMCPY(key, client_key_der_1024, keySz);
  23828. XMEMCPY(cert, client_cert_der_1024, certSz);
  23829. #else
  23830. unsigned char cert[ONEK_BUF];
  23831. unsigned char key[ONEK_BUF];
  23832. XFILE fp;
  23833. int certSz;
  23834. int keySz;
  23835. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23836. AssertTrue(fp != XBADFILE);
  23837. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23838. XFCLOSE(fp);
  23839. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23840. AssertTrue(fp != XBADFILE);
  23841. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23842. XFCLOSE(fp);
  23843. #endif
  23844. #elif defined(HAVE_ECC)
  23845. #if defined(USE_CERT_BUFFERS_256)
  23846. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23847. unsigned char key[sizeof(ecc_clikey_der_256)];
  23848. int certSz = (int)sizeof(cert);
  23849. int keySz = (int)sizeof(key);
  23850. XMEMSET(cert, 0, certSz);
  23851. XMEMSET(key, 0, keySz);
  23852. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23853. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23854. #else
  23855. unsigned char cert[ONEK_BUF];
  23856. unsigned char key[ONEK_BUF];
  23857. XFILE fp;
  23858. int certSz, keySz;
  23859. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  23860. AssertTrue(fp != XBADFILE);
  23861. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  23862. XFCLOSE(fp);
  23863. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23864. AssertTrue(fp != XBADFILE);
  23865. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  23866. XFCLOSE(fp);
  23867. #endif
  23868. #endif
  23869. XMEMSET(output, 0, outputSz);
  23870. AssertIntEQ(wc_InitRng(&rng), 0);
  23871. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23872. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23873. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23874. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  23875. pkcs7->content = data;
  23876. pkcs7->contentSz = (word32)sizeof(data);
  23877. pkcs7->privateKey = key;
  23878. pkcs7->privateKeySz = (word32)sizeof(key);
  23879. pkcs7->encryptOID = RSAk;
  23880. pkcs7->hashOID = SHAh;
  23881. pkcs7->rng = &rng;
  23882. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23883. wc_PKCS7_Free(pkcs7);
  23884. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23885. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23886. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23887. /* Pass in bad args. */
  23888. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  23889. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23890. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23891. badOutSz), BAD_FUNC_ARG);
  23892. pkcs7->hashOID = 0; /* bad hashOID */
  23893. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  23894. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23895. !defined(NO_RSA) && !defined(NO_SHA256)
  23896. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23897. * Example callback assumes SHA-256, so only run test if compiled in. */
  23898. wc_PKCS7_Free(pkcs7);
  23899. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  23900. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23901. pkcs7->content = data;
  23902. pkcs7->contentSz = (word32)sizeof(data);
  23903. pkcs7->privateKey = key;
  23904. pkcs7->privateKeySz = (word32)sizeof(key);
  23905. pkcs7->encryptOID = RSAk;
  23906. pkcs7->hashOID = SHA256h;
  23907. pkcs7->rng = &rng;
  23908. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23909. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23910. #endif
  23911. printf(resultFmt, passed);
  23912. wc_PKCS7_Free(pkcs7);
  23913. wc_FreeRng(&rng);
  23914. #endif
  23915. return 0;
  23916. } /* END test_wc_PKCS7_EncodeSignedData */
  23917. /*
  23918. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23919. */
  23920. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  23921. {
  23922. #if defined(HAVE_PKCS7)
  23923. int ret, i;
  23924. PKCS7* pkcs7;
  23925. WC_RNG rng;
  23926. byte outputHead[FOURK_BUF/2];
  23927. byte outputFoot[FOURK_BUF/2];
  23928. word32 outputHeadSz = (word32)sizeof(outputHead);
  23929. word32 outputFootSz = (word32)sizeof(outputFoot);
  23930. byte data[FOURK_BUF];
  23931. wc_HashAlg hash;
  23932. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23933. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23934. word32 hashSz = wc_HashGetDigestSize(hashType);
  23935. #ifndef NO_RSA
  23936. #if defined(USE_CERT_BUFFERS_2048)
  23937. byte key[sizeof(client_key_der_2048)];
  23938. byte cert[sizeof(client_cert_der_2048)];
  23939. word32 keySz = (word32)sizeof(key);
  23940. word32 certSz = (word32)sizeof(cert);
  23941. XMEMSET(key, 0, keySz);
  23942. XMEMSET(cert, 0, certSz);
  23943. XMEMCPY(key, client_key_der_2048, keySz);
  23944. XMEMCPY(cert, client_cert_der_2048, certSz);
  23945. #elif defined(USE_CERT_BUFFERS_1024)
  23946. byte key[sizeof_client_key_der_1024];
  23947. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23948. word32 keySz = (word32)sizeof(key);
  23949. word32 certSz = (word32)sizeof(cert);
  23950. XMEMSET(key, 0, keySz);
  23951. XMEMSET(cert, 0, certSz);
  23952. XMEMCPY(key, client_key_der_1024, keySz);
  23953. XMEMCPY(cert, client_cert_der_1024, certSz);
  23954. #else
  23955. unsigned char cert[ONEK_BUF];
  23956. unsigned char key[ONEK_BUF];
  23957. XFILE fp;
  23958. int certSz;
  23959. int keySz;
  23960. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23961. AssertTrue((fp != XBADFILE));
  23962. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23963. XFCLOSE(fp);
  23964. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23965. AssertTrue(fp != XBADFILE);
  23966. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23967. XFCLOSE(fp);
  23968. #endif
  23969. #elif defined(HAVE_ECC)
  23970. #if defined(USE_CERT_BUFFERS_256)
  23971. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23972. unsigned char key[sizeof(ecc_clikey_der_256)];
  23973. int certSz = (int)sizeof(cert);
  23974. int keySz = (int)sizeof(key);
  23975. XMEMSET(cert, 0, certSz);
  23976. XMEMSET(key, 0, keySz);
  23977. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23978. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23979. #else
  23980. unsigned char cert[ONEK_BUF];
  23981. unsigned char key[ONEK_BUF];
  23982. XFILE fp;
  23983. int certSz, keySz;
  23984. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23985. AssertTrue(fp != XBADFILE);
  23986. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  23987. XFCLOSE(fp);
  23988. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23989. AssertTrue(fp != XBADFILE);
  23990. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  23991. XFCLOSE(fp);
  23992. #endif
  23993. #endif
  23994. /* initialize large data with sequence */
  23995. for (i=0; i<(int)sizeof(data); i++)
  23996. data[i] = i & 0xff;
  23997. XMEMSET(outputHead, 0, outputHeadSz);
  23998. XMEMSET(outputFoot, 0, outputFootSz);
  23999. AssertIntEQ(wc_InitRng(&rng), 0);
  24000. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24001. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24002. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24003. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  24004. pkcs7->content = NULL; /* not used for ex */
  24005. pkcs7->contentSz = (word32)sizeof(data);
  24006. pkcs7->privateKey = key;
  24007. pkcs7->privateKeySz = (word32)sizeof(key);
  24008. pkcs7->encryptOID = RSAk;
  24009. pkcs7->hashOID = SHAh;
  24010. pkcs7->rng = &rng;
  24011. /* calculate hash for content */
  24012. ret = wc_HashInit(&hash, hashType);
  24013. if (ret == 0) {
  24014. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  24015. if (ret == 0) {
  24016. ret = wc_HashFinal(&hash, hashType, hashBuf);
  24017. }
  24018. wc_HashFree(&hash, hashType);
  24019. }
  24020. AssertIntEQ(ret, 0);
  24021. /* Perform PKCS7 sign using hash directly */
  24022. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24023. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24024. AssertIntGT(outputHeadSz, 0);
  24025. AssertIntGT(outputFootSz, 0);
  24026. wc_PKCS7_Free(pkcs7);
  24027. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24028. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24029. /* required parameter even on verify when using _ex, if using outputHead
  24030. * and outputFoot */
  24031. pkcs7->contentSz = (word32)sizeof(data);
  24032. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24033. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24034. wc_PKCS7_Free(pkcs7);
  24035. /* assembly complete PKCS7 sign and use normal verify */
  24036. {
  24037. byte* output = (byte*)XMALLOC(outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24038. word32 outputSz = 0;
  24039. AssertNotNull(output);
  24040. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24041. outputSz += outputHeadSz;
  24042. XMEMCPY(&output[outputSz], data, sizeof(data));
  24043. outputSz += sizeof(data);
  24044. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24045. outputSz += outputFootSz;
  24046. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24047. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24048. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24049. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24050. }
  24051. /* Pass in bad args. */
  24052. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24053. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24054. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24055. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24056. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24057. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24058. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24059. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24060. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24061. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24062. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24063. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24064. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24065. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24066. pkcs7->hashOID = 0; /* bad hashOID */
  24067. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24068. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24069. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24070. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24071. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24072. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24073. #ifndef NO_PKCS7_STREAM
  24074. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24075. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24076. #else
  24077. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24078. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24079. #endif
  24080. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24081. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24082. #ifndef NO_PKCS7_STREAM
  24083. /* can pass in 0 buffer length with streaming API */
  24084. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24085. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24086. #else
  24087. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24088. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24089. #endif
  24090. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24091. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24092. #ifndef NO_PKCS7_STREAM
  24093. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24094. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24095. #else
  24096. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24097. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  24098. #endif
  24099. printf(resultFmt, passed);
  24100. wc_PKCS7_Free(pkcs7);
  24101. wc_FreeRng(&rng);
  24102. #endif
  24103. return 0;
  24104. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24105. #if defined(HAVE_PKCS7)
  24106. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24107. byte* data, word32 dataSz,
  24108. int withAttribs, int detachedSig)
  24109. {
  24110. PKCS7* pkcs7;
  24111. WC_RNG rng;
  24112. static byte messageTypeOid[] =
  24113. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24114. 0x09, 0x02 };
  24115. static byte messageType[] = { 0x13, 2, '1', '9' };
  24116. PKCS7Attrib attribs[] =
  24117. {
  24118. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24119. sizeof(messageType) }
  24120. };
  24121. #ifndef NO_RSA
  24122. #if defined(USE_CERT_BUFFERS_2048)
  24123. byte key[sizeof(client_key_der_2048)];
  24124. byte cert[sizeof(client_cert_der_2048)];
  24125. word32 keySz = (word32)sizeof(key);
  24126. word32 certSz = (word32)sizeof(cert);
  24127. XMEMSET(key, 0, keySz);
  24128. XMEMSET(cert, 0, certSz);
  24129. XMEMCPY(key, client_key_der_2048, keySz);
  24130. XMEMCPY(cert, client_cert_der_2048, certSz);
  24131. #elif defined(USE_CERT_BUFFERS_1024)
  24132. byte key[sizeof_client_key_der_1024];
  24133. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24134. word32 keySz = (word32)sizeof(key);
  24135. word32 certSz = (word32)sizeof(cert);
  24136. XMEMSET(key, 0, keySz);
  24137. XMEMSET(cert, 0, certSz);
  24138. XMEMCPY(key, client_key_der_1024, keySz);
  24139. XMEMCPY(cert, client_cert_der_1024, certSz);
  24140. #else
  24141. unsigned char cert[ONEK_BUF];
  24142. unsigned char key[ONEK_BUF];
  24143. FILE* fp;
  24144. int certSz;
  24145. int keySz;
  24146. fp = fopen("./certs/1024/client-cert.der", "rb");
  24147. AssertNotNull(fp);
  24148. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  24149. fclose(fp);
  24150. fp = fopen("./certs/1024/client-key.der", "rb");
  24151. AssertNotNull(fp);
  24152. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  24153. fclose(fp);
  24154. #endif
  24155. #elif defined(HAVE_ECC)
  24156. #if defined(USE_CERT_BUFFERS_256)
  24157. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24158. unsigned char key[sizeof(ecc_clikey_der_256)];
  24159. int certSz = (int)sizeof(cert);
  24160. int keySz = (int)sizeof(key);
  24161. XMEMSET(cert, 0, certSz);
  24162. XMEMSET(key, 0, keySz);
  24163. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24164. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24165. #else
  24166. unsigned char cert[ONEK_BUF];
  24167. unsigned char key[ONEK_BUF];
  24168. FILE* fp;
  24169. int certSz, keySz;
  24170. fp = fopen("./certs/client-ecc-cert.der", "rb");
  24171. AssertNotNull(fp);
  24172. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24173. fclose(fp);
  24174. fp = fopen("./certs/client-ecc-key.der", "rb");
  24175. AssertNotNull(fp);
  24176. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  24177. fclose(fp);
  24178. #endif
  24179. #endif
  24180. XMEMSET(output, 0, outputSz);
  24181. AssertIntEQ(wc_InitRng(&rng), 0);
  24182. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24183. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24184. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24185. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  24186. pkcs7->content = data;
  24187. pkcs7->contentSz = dataSz;
  24188. pkcs7->privateKey = key;
  24189. pkcs7->privateKeySz = (word32)sizeof(key);
  24190. pkcs7->encryptOID = RSAk;
  24191. pkcs7->hashOID = SHAh;
  24192. pkcs7->rng = &rng;
  24193. if (withAttribs) {
  24194. /* include a signed attribute */
  24195. pkcs7->signedAttribs = attribs;
  24196. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24197. }
  24198. if (detachedSig) {
  24199. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24200. }
  24201. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24202. wc_PKCS7_Free(pkcs7);
  24203. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24204. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24205. if (detachedSig) {
  24206. pkcs7->content = data;
  24207. pkcs7->contentSz = dataSz;
  24208. }
  24209. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24210. wc_PKCS7_Free(pkcs7);
  24211. wc_FreeRng(&rng);
  24212. return outputSz;
  24213. }
  24214. #endif
  24215. /*
  24216. * Testing wc_PKCS_VerifySignedData()
  24217. */
  24218. static int test_wc_PKCS7_VerifySignedData(void)
  24219. {
  24220. #if defined(HAVE_PKCS7)
  24221. PKCS7* pkcs7;
  24222. byte output[FOURK_BUF];
  24223. word32 outputSz = sizeof(output);
  24224. byte data[] = "Test data to encode.";
  24225. byte badOut[1];
  24226. word32 badOutSz = 0;
  24227. byte badContent[] = "This is different content than was signed";
  24228. int ret;
  24229. wc_HashAlg hash;
  24230. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24231. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24232. word32 hashSz = wc_HashGetDigestSize(hashType);
  24233. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24234. (word32)sizeof(data),
  24235. 0, 0)), 0);
  24236. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24237. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24238. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24239. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24240. /* Test bad args. */
  24241. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  24242. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24243. #ifndef NO_PKCS7_STREAM
  24244. /* can pass in 0 buffer length with streaming API */
  24245. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24246. badOutSz), WC_PKCS7_WANT_READ_E);
  24247. #else
  24248. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24249. badOutSz), BAD_FUNC_ARG);
  24250. #endif
  24251. wc_PKCS7_Free(pkcs7);
  24252. /* Invalid content should error, use detached signature so we can
  24253. * easily change content */
  24254. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24255. (word32)sizeof(data),
  24256. 1, 1)), 0);
  24257. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24258. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24259. pkcs7->content = badContent;
  24260. pkcs7->contentSz = sizeof(badContent);
  24261. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), SIG_VERIFY_E);
  24262. wc_PKCS7_Free(pkcs7);
  24263. /* Test success case with detached signature and valid content */
  24264. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24265. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24266. pkcs7->content = data;
  24267. pkcs7->contentSz = sizeof(data);
  24268. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24269. wc_PKCS7_Free(pkcs7);
  24270. /* verify using pre-computed content digest only (no content) */
  24271. {
  24272. /* calculate hash for content */
  24273. ret = wc_HashInit(&hash, hashType);
  24274. if (ret == 0) {
  24275. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  24276. if (ret == 0) {
  24277. ret = wc_HashFinal(&hash, hashType, hashBuf);
  24278. }
  24279. wc_HashFree(&hash, hashType);
  24280. }
  24281. AssertIntEQ(ret, 0);
  24282. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24283. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24284. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24285. output, outputSz,
  24286. NULL, 0), 0);
  24287. wc_PKCS7_Free(pkcs7);
  24288. }
  24289. printf(resultFmt, passed);
  24290. #endif
  24291. return 0;
  24292. } /* END test_wc_PKCS7_VerifySignedData() */
  24293. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  24294. !defined(NO_AES_256)
  24295. static const byte defKey[] = {
  24296. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24297. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24298. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24299. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24300. };
  24301. static byte aesHandle[32]; /* simulated hardware key handle */
  24302. /* return 0 on success */
  24303. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  24304. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  24305. byte* in, int inSz, byte* out, void* usrCtx)
  24306. {
  24307. int ret;
  24308. Aes aes;
  24309. if (usrCtx == NULL) {
  24310. /* no simulated handle passed in */
  24311. return -1;
  24312. }
  24313. switch (encryptOID) {
  24314. case AES256CBCb:
  24315. if (ivSz != AES_BLOCK_SIZE)
  24316. return BAD_FUNC_ARG;
  24317. break;
  24318. default:
  24319. WOLFSSL_MSG("Unsupported content cipher type for test");
  24320. return ALGO_ID_E;
  24321. };
  24322. /* simulate using handle to get key */
  24323. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  24324. if (ret == 0) {
  24325. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  24326. if (ret == 0)
  24327. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  24328. wc_AesFree(&aes);
  24329. }
  24330. (void)aad;
  24331. (void)aadSz;
  24332. (void)authTag;
  24333. (void)authTagSz;
  24334. (void)pkcs7;
  24335. return ret;
  24336. }
  24337. /* returns key size on success */
  24338. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  24339. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  24340. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  24341. {
  24342. int ret = -1;
  24343. if (out == NULL)
  24344. return BAD_FUNC_ARG;
  24345. if (keyId[0] != 0x00) {
  24346. return -1;
  24347. }
  24348. if (type != (int)PKCS7_KEKRI) {
  24349. return -1;
  24350. }
  24351. switch (keyWrapAlgo) {
  24352. case AES256_WRAP:
  24353. /* simulate setting a handle for later decryption but use key
  24354. * as handle in the test case here */
  24355. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  24356. aesHandle, sizeof(aesHandle), NULL);
  24357. if (ret < 0)
  24358. return ret;
  24359. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  24360. if (ret < 0)
  24361. return ret;
  24362. /* return key size on success */
  24363. return sizeof(defKey);
  24364. default:
  24365. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  24366. return BAD_KEYWRAP_ALG_E;
  24367. };
  24368. (void)cekSz;
  24369. (void)cek;
  24370. (void)outSz;
  24371. (void)keyIdSz;
  24372. (void)direction;
  24373. (void)orginKey; /* used with KAKRI */
  24374. (void)orginKeySz;
  24375. return ret;
  24376. }
  24377. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  24378. /*
  24379. * Testing wc_PKCS7_EncodeEnvelopedData()
  24380. */
  24381. static int test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  24382. {
  24383. #if defined(HAVE_PKCS7)
  24384. PKCS7* pkcs7;
  24385. #ifdef ECC_TIMING_RESISTANT
  24386. WC_RNG rng;
  24387. #endif
  24388. word32 tempWrd32 = 0;
  24389. byte* tmpBytePtr = NULL;
  24390. const char input[] = "Test data to encode.";
  24391. int i;
  24392. int testSz = 0;
  24393. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  24394. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24395. byte* rsaCert = NULL;
  24396. byte* rsaPrivKey = NULL;
  24397. word32 rsaCertSz;
  24398. word32 rsaPrivKeySz;
  24399. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  24400. !defined(USE_CERT_BUFFERS_2048) )
  24401. static const char* rsaClientCert = "./certs/client-cert.der";
  24402. static const char* rsaClientKey = "./certs/client-key.der";
  24403. rsaCertSz = (word32)sizeof(rsaClientCert);
  24404. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  24405. #endif
  24406. #endif
  24407. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24408. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24409. byte* eccCert = NULL;
  24410. byte* eccPrivKey = NULL;
  24411. word32 eccCertSz;
  24412. word32 eccPrivKeySz;
  24413. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24414. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24415. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24416. #endif
  24417. #endif
  24418. /* Generic buffer size. */
  24419. byte output[ONEK_BUF];
  24420. byte decoded[sizeof(input)/sizeof(char)];
  24421. int decodedSz = 0;
  24422. #ifndef NO_FILESYSTEM
  24423. XFILE certFile;
  24424. XFILE keyFile;
  24425. #endif
  24426. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24427. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24428. /* RSA certs and keys. */
  24429. #if defined(USE_CERT_BUFFERS_1024)
  24430. /* Allocate buffer space. */
  24431. AssertNotNull(rsaCert =
  24432. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24433. /* Init buffer. */
  24434. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24435. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  24436. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  24437. DYNAMIC_TYPE_TMP_BUFFER));
  24438. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  24439. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  24440. #elif defined(USE_CERT_BUFFERS_2048)
  24441. /* Allocate buffer */
  24442. AssertNotNull(rsaCert =
  24443. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24444. /* Init buffer. */
  24445. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  24446. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  24447. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24448. DYNAMIC_TYPE_TMP_BUFFER));
  24449. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  24450. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  24451. #else
  24452. /* File system. */
  24453. certFile = XFOPEN(rsaClientCert, "rb");
  24454. AssertTrue(certFile != XBADFILE);
  24455. rsaCertSz = (word32)FOURK_BUF;
  24456. AssertNotNull(rsaCert =
  24457. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24458. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  24459. XFCLOSE(certFile);
  24460. keyFile = XFOPEN(rsaClientKey, "rb");
  24461. AssertTrue(keyFile != XBADFILE);
  24462. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24463. DYNAMIC_TYPE_TMP_BUFFER));
  24464. rsaPrivKeySz = (word32)FOURK_BUF;
  24465. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  24466. XFCLOSE(keyFile);
  24467. #endif /* USE_CERT_BUFFERS */
  24468. #endif /* NO_RSA */
  24469. /* ECC */
  24470. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24471. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24472. #ifdef USE_CERT_BUFFERS_256
  24473. AssertNotNull(eccCert =
  24474. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24475. /* Init buffer. */
  24476. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  24477. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  24478. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24479. DYNAMIC_TYPE_TMP_BUFFER));
  24480. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  24481. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  24482. #else /* File system. */
  24483. certFile = XFOPEN(eccClientCert, "rb");
  24484. AssertTrue(certFile != XBADFILE);
  24485. eccCertSz = (word32)FOURK_BUF;
  24486. AssertNotNull(eccCert =
  24487. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24488. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  24489. XFCLOSE(certFile);
  24490. keyFile = XFOPEN(eccClientKey, "rb");
  24491. AssertTrue(keyFile != XBADFILE);
  24492. eccPrivKeySz = (word32)FOURK_BUF;
  24493. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24494. DYNAMIC_TYPE_TMP_BUFFER));
  24495. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  24496. XFCLOSE(keyFile);
  24497. #endif /* USE_CERT_BUFFERS_256 */
  24498. #endif /* END HAVE_ECC */
  24499. /* Silence. */
  24500. (void)keyFile;
  24501. (void)certFile;
  24502. {
  24503. const pkcs7EnvelopedVector testVectors[] = {
  24504. /* DATA is a global variable defined in the makefile. */
  24505. #if !defined(NO_RSA)
  24506. #ifndef NO_DES3
  24507. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  24508. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24509. #endif /* NO_DES3 */
  24510. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24511. #ifndef NO_AES_128
  24512. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24513. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24514. #endif
  24515. #ifndef NO_AES_192
  24516. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  24517. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24518. #endif
  24519. #ifndef NO_AES_256
  24520. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24521. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24522. #endif
  24523. #endif /* NO_AES && HAVE_AES_CBC */
  24524. #endif /* NO_RSA */
  24525. #if defined(HAVE_ECC)
  24526. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24527. #if !defined(NO_SHA) && !defined(NO_AES_128)
  24528. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24529. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  24530. eccCertSz, eccPrivKey, eccPrivKeySz},
  24531. #endif
  24532. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  24533. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24534. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  24535. eccCertSz, eccPrivKey, eccPrivKeySz},
  24536. #endif
  24537. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  24538. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24539. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  24540. eccCertSz, eccPrivKey, eccPrivKeySz},
  24541. #endif
  24542. #endif /* NO_AES && HAVE_AES_CBC*/
  24543. #endif /* END HAVE_ECC */
  24544. }; /* END pkcs7EnvelopedVector */
  24545. #ifdef ECC_TIMING_RESISTANT
  24546. AssertIntEQ(wc_InitRng(&rng), 0);
  24547. #endif
  24548. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  24549. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24550. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24551. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  24552. for (i = 0; i < testSz; i++) {
  24553. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24554. (word32)(testVectors + i)->certSz), 0);
  24555. #ifdef ECC_TIMING_RESISTANT
  24556. pkcs7->rng = &rng;
  24557. #endif
  24558. pkcs7->content = (byte*)(testVectors + i)->content;
  24559. pkcs7->contentSz = (testVectors + i)->contentSz;
  24560. pkcs7->contentOID = (testVectors + i)->contentOID;
  24561. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24562. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24563. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24564. pkcs7->privateKey = (testVectors + i)->privateKey;
  24565. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24566. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24567. (word32)sizeof(output)), 0);
  24568. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24569. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  24570. AssertIntGE(decodedSz, 0);
  24571. /* Verify the size of each buffer. */
  24572. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24573. /* Don't free the last time through the loop. */
  24574. if (i < testSz - 1 ){
  24575. wc_PKCS7_Free(pkcs7);
  24576. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24577. }
  24578. } /* END test loop. */
  24579. }
  24580. /* Test bad args. */
  24581. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  24582. (word32)sizeof(output)), BAD_FUNC_ARG);
  24583. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  24584. (word32)sizeof(output)), BAD_FUNC_ARG);
  24585. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  24586. printf(resultFmt, passed);
  24587. /* Decode. */
  24588. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  24589. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  24590. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24591. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24592. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24593. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24594. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  24595. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  24596. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24597. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  24598. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24599. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  24600. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  24601. /* only a failure for KARI test cases */
  24602. tempWrd32 = pkcs7->singleCertSz;
  24603. pkcs7->singleCertSz = 0;
  24604. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24605. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24606. pkcs7->singleCertSz = tempWrd32;
  24607. tmpBytePtr = pkcs7->singleCert;
  24608. pkcs7->singleCert = NULL;
  24609. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24610. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24611. pkcs7->singleCert = tmpBytePtr;
  24612. #endif
  24613. tempWrd32 = pkcs7->privateKeySz;
  24614. pkcs7->privateKeySz = 0;
  24615. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24616. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24617. pkcs7->privateKeySz = tempWrd32;
  24618. tmpBytePtr = pkcs7->privateKey;
  24619. pkcs7->privateKey = NULL;
  24620. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24621. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24622. pkcs7->privateKey = tmpBytePtr;
  24623. wc_PKCS7_Free(pkcs7);
  24624. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  24625. /* test of decrypt callback with KEKRI enveloped data */
  24626. {
  24627. int envelopedSz;
  24628. const byte keyId[] = { 0x00 };
  24629. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24630. pkcs7->content = (byte*)input;
  24631. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  24632. pkcs7->contentOID = DATA;
  24633. pkcs7->encryptOID = AES256CBCb;
  24634. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  24635. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  24636. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  24637. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  24638. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24639. (word32)sizeof(output))), 0);
  24640. wc_PKCS7_Free(pkcs7);
  24641. /* decode envelopedData */
  24642. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24643. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  24644. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  24645. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24646. envelopedSz, decoded, sizeof(decoded))), 0);
  24647. wc_PKCS7_Free(pkcs7);
  24648. }
  24649. #endif /* !NO_AES && !NO_AES_256 */
  24650. printf(resultFmt, passed);
  24651. #ifndef NO_RSA
  24652. if (rsaCert) {
  24653. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24654. }
  24655. if (rsaPrivKey) {
  24656. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24657. }
  24658. #endif /*NO_RSA */
  24659. #ifdef HAVE_ECC
  24660. if (eccCert) {
  24661. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24662. }
  24663. if (eccPrivKey) {
  24664. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24665. }
  24666. #endif /* HAVE_ECC */
  24667. #ifdef ECC_TIMING_RESISTANT
  24668. wc_FreeRng(&rng);
  24669. #endif
  24670. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && !defined(NO_RSA)
  24671. {
  24672. byte out[7];
  24673. byte *cms;
  24674. word32 cmsSz;
  24675. XFILE cmsFile;
  24676. XMEMSET(out, 0, sizeof(out));
  24677. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24678. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  24679. AssertTrue(cmsFile != XBADFILE);
  24680. cmsSz = (word32)FOURK_BUF;
  24681. AssertNotNull(cms =
  24682. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24683. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  24684. XFCLOSE(cmsFile);
  24685. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  24686. sizeof_client_cert_der_2048), 0);
  24687. pkcs7->privateKey = (byte*)client_key_der_2048;
  24688. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  24689. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24690. 2), 0);
  24691. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24692. sizeof(out)), 0);
  24693. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24694. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  24695. wc_PKCS7_Free(pkcs7);
  24696. }
  24697. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 */
  24698. #endif /* HAVE_PKCS7 */
  24699. return 0;
  24700. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  24701. /*
  24702. * Testing wc_PKCS7_EncodeEncryptedData()
  24703. */
  24704. static int test_wc_PKCS7_EncodeEncryptedData (void)
  24705. {
  24706. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  24707. PKCS7* pkcs7 = NULL;
  24708. byte* tmpBytePtr = NULL;
  24709. byte encrypted[TWOK_BUF];
  24710. byte decoded[TWOK_BUF];
  24711. word32 tmpWrd32 = 0;
  24712. int tmpInt = 0;
  24713. int decodedSz;
  24714. int encryptedSz;
  24715. int testSz;
  24716. int i;
  24717. const byte data[] = { /* Hello World */
  24718. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24719. 0x72,0x6c,0x64
  24720. };
  24721. #ifndef NO_DES3
  24722. byte desKey[] = {
  24723. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  24724. };
  24725. byte des3Key[] = {
  24726. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  24727. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  24728. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  24729. };
  24730. #endif
  24731. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24732. #ifndef NO_AES_128
  24733. byte aes128Key[] = {
  24734. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24735. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24736. };
  24737. #endif
  24738. #ifndef NO_AES_192
  24739. byte aes192Key[] = {
  24740. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24741. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24742. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24743. };
  24744. #endif
  24745. #ifndef NO_AES_256
  24746. byte aes256Key[] = {
  24747. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24748. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24749. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24750. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24751. };
  24752. #endif
  24753. #endif /* !NO_AES && HAVE_AES_CBC */
  24754. const pkcs7EncryptedVector testVectors[] =
  24755. {
  24756. #ifndef NO_DES3
  24757. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  24758. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  24759. #endif /* !NO_DES3 */
  24760. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24761. #ifndef NO_AES_128
  24762. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  24763. sizeof(aes128Key)},
  24764. #endif
  24765. #ifndef NO_AES_192
  24766. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  24767. sizeof(aes192Key)},
  24768. #endif
  24769. #ifndef NO_AES_256
  24770. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  24771. sizeof(aes256Key)},
  24772. #endif
  24773. #endif /* !NO_AES && HAVE_AES_CBC */
  24774. };
  24775. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  24776. for (i = 0; i < testSz; i++) {
  24777. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24778. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24779. pkcs7->content = (byte*)testVectors[i].content;
  24780. pkcs7->contentSz = testVectors[i].contentSz;
  24781. pkcs7->contentOID = testVectors[i].contentOID;
  24782. pkcs7->encryptOID = testVectors[i].encryptOID;
  24783. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  24784. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  24785. pkcs7->heap = HEAP_HINT;
  24786. /* encode encryptedData */
  24787. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24788. sizeof(encrypted));
  24789. AssertIntGT(encryptedSz, 0);
  24790. /* Decode encryptedData */
  24791. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24792. decoded, sizeof(decoded));
  24793. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  24794. /* Keep values for last itr. */
  24795. if (i < testSz - 1) {
  24796. wc_PKCS7_Free(pkcs7);
  24797. }
  24798. }
  24799. if (pkcs7 == NULL || testSz == 0) {
  24800. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24801. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  24802. }
  24803. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  24804. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  24805. sizeof(encrypted)),BAD_FUNC_ARG);
  24806. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  24807. sizeof(encrypted)), BAD_FUNC_ARG);
  24808. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24809. 0), BAD_FUNC_ARG);
  24810. /* Testing the struct. */
  24811. tmpBytePtr = pkcs7->content;
  24812. pkcs7->content = NULL;
  24813. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24814. sizeof(encrypted)), BAD_FUNC_ARG);
  24815. pkcs7->content = tmpBytePtr;
  24816. tmpWrd32 = pkcs7->contentSz;
  24817. pkcs7->contentSz = 0;
  24818. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24819. sizeof(encrypted)), BAD_FUNC_ARG);
  24820. pkcs7->contentSz = tmpWrd32;
  24821. tmpInt = pkcs7->encryptOID;
  24822. pkcs7->encryptOID = 0;
  24823. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24824. sizeof(encrypted)), BAD_FUNC_ARG);
  24825. pkcs7->encryptOID = tmpInt;
  24826. tmpBytePtr = pkcs7->encryptionKey;
  24827. pkcs7->encryptionKey = NULL;
  24828. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24829. sizeof(encrypted)), BAD_FUNC_ARG);
  24830. pkcs7->encryptionKey = tmpBytePtr;
  24831. tmpWrd32 = pkcs7->encryptionKeySz;
  24832. pkcs7->encryptionKeySz = 0;
  24833. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24834. sizeof(encrypted)), BAD_FUNC_ARG);
  24835. pkcs7->encryptionKeySz = tmpWrd32;
  24836. printf(resultFmt, passed);
  24837. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  24838. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  24839. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24840. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  24841. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24842. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  24843. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24844. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24845. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  24846. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24847. decoded, 0), BAD_FUNC_ARG);
  24848. /* Test struct fields */
  24849. tmpBytePtr = pkcs7->encryptionKey;
  24850. pkcs7->encryptionKey = NULL;
  24851. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24852. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24853. pkcs7->encryptionKey = tmpBytePtr;
  24854. pkcs7->encryptionKeySz = 0;
  24855. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24856. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24857. printf(resultFmt, passed);
  24858. wc_PKCS7_Free(pkcs7);
  24859. #endif
  24860. return 0;
  24861. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  24862. /*
  24863. * Testing wc_PKCS7_Degenerate()
  24864. */
  24865. static int test_wc_PKCS7_Degenerate(void)
  24866. {
  24867. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24868. PKCS7* pkcs7;
  24869. char fName[] = "./certs/test-degenerate.p7b";
  24870. XFILE f;
  24871. byte der[4096];
  24872. word32 derSz;
  24873. int ret;
  24874. printf(testingFmt, "wc_PKCS7_Degenerate()");
  24875. AssertNotNull(f = XFOPEN(fName, "rb"));
  24876. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24877. derSz = (word32)ret;
  24878. XFCLOSE(f);
  24879. /* test degenerate success */
  24880. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24881. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24882. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24883. #ifndef NO_RSA
  24884. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24885. #else
  24886. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24887. #endif
  24888. wc_PKCS7_Free(pkcs7);
  24889. /* test with turning off degenerate cases */
  24890. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  24891. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24892. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24893. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  24894. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  24895. wc_PKCS7_Free(pkcs7);
  24896. printf(resultFmt, passed);
  24897. #endif
  24898. return 0;
  24899. } /* END test_wc_PKCS7_Degenerate() */
  24900. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24901. defined(ASN_BER_TO_DER) && !defined(NO_DES3)
  24902. static byte berContent[] = {
  24903. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24904. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  24905. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  24906. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  24907. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  24908. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  24909. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  24910. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  24911. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  24912. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  24913. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  24914. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  24915. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  24916. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  24917. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  24918. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  24919. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  24920. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  24921. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24922. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24923. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  24924. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24925. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24926. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  24927. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  24928. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  24929. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24930. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  24931. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  24932. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  24933. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  24934. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  24935. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  24936. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  24937. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  24938. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  24939. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  24940. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  24941. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  24942. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  24943. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  24944. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  24945. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  24946. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  24947. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24948. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  24949. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  24950. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  24951. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  24952. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  24953. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  24954. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  24955. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  24956. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  24957. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  24958. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  24959. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  24960. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  24961. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  24962. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  24963. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  24964. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  24965. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  24966. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  24967. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  24968. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  24969. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  24970. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  24971. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  24972. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  24973. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  24974. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  24975. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  24976. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  24977. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  24978. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  24979. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  24980. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  24981. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  24982. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  24983. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  24984. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  24985. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  24986. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  24987. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  24988. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  24989. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  24990. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  24991. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  24992. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  24993. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  24994. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  24995. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  24996. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  24997. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  24998. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  24999. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  25000. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  25001. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  25002. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  25003. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  25004. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  25005. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  25006. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  25007. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  25008. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  25009. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  25010. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  25011. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  25012. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  25013. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  25014. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  25015. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  25016. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  25017. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  25018. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  25019. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  25020. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  25021. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  25022. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  25023. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  25024. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  25025. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  25026. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  25027. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  25028. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  25029. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  25030. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  25031. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  25032. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  25033. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  25034. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25035. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25036. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25037. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25038. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25039. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25040. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25041. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25042. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25043. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25044. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25045. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25046. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25047. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25048. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25049. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25050. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25051. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25052. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25053. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25054. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25055. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25056. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25057. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25058. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25059. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25060. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25061. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25062. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25063. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25064. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25065. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25066. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25067. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25068. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25069. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25070. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25071. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25072. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25073. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25074. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25075. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25076. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25077. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25078. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25079. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25080. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25081. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25082. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25083. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25084. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25085. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25086. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25087. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25088. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25089. 0x00, 0x00, 0x00, 0x00, 0x00
  25090. };
  25091. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
  25092. /*
  25093. * Testing wc_PKCS7_BER()
  25094. */
  25095. static int test_wc_PKCS7_BER(void)
  25096. {
  25097. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25098. defined(ASN_BER_TO_DER)
  25099. PKCS7* pkcs7;
  25100. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25101. XFILE f;
  25102. byte der[4096];
  25103. #ifndef NO_DES3
  25104. byte decoded[2048];
  25105. #endif
  25106. word32 derSz;
  25107. int ret;
  25108. printf(testingFmt, "wc_PKCS7_BER()");
  25109. AssertNotNull(f = XFOPEN(fName, "rb"));
  25110. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25111. derSz = (word32)ret;
  25112. XFCLOSE(f);
  25113. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  25114. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25115. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25116. #ifndef NO_RSA
  25117. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25118. #else
  25119. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25120. #endif
  25121. wc_PKCS7_Free(pkcs7);
  25122. #ifndef NO_DES3
  25123. /* decode BER content */
  25124. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  25125. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25126. derSz = (word32)ret;
  25127. XFCLOSE(f);
  25128. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  25129. #ifndef NO_RSA
  25130. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25131. #else
  25132. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25133. #endif
  25134. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  25135. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25136. derSz = (word32)ret;
  25137. XFCLOSE(f);
  25138. pkcs7->privateKey = der;
  25139. pkcs7->privateKeySz = derSz;
  25140. #ifndef NO_RSA
  25141. #ifdef WOLFSSL_SP_MATH
  25142. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25143. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  25144. #else
  25145. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25146. sizeof(berContent), decoded, sizeof(decoded)), 0);
  25147. #endif
  25148. #else
  25149. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25150. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  25151. #endif
  25152. wc_PKCS7_Free(pkcs7);
  25153. #endif /* !NO_DES3 */
  25154. printf(resultFmt, passed);
  25155. #endif
  25156. return 0;
  25157. } /* END test_wc_PKCS7_BER() */
  25158. static int test_PKCS7_signed_enveloped(void)
  25159. {
  25160. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  25161. !defined(NO_FILESYSTEM)
  25162. XFILE f;
  25163. PKCS7* pkcs7;
  25164. #ifdef HAVE_AES_CBC
  25165. PKCS7* inner;
  25166. #endif
  25167. void* pt;
  25168. WC_RNG rng;
  25169. unsigned char key[FOURK_BUF/2];
  25170. unsigned char cert[FOURK_BUF/2];
  25171. unsigned char env[FOURK_BUF];
  25172. int envSz = FOURK_BUF;
  25173. int keySz;
  25174. int certSz;
  25175. unsigned char sig[FOURK_BUF * 2];
  25176. int sigSz = FOURK_BUF * 2;
  25177. #ifdef HAVE_AES_CBC
  25178. unsigned char decoded[FOURK_BUF];
  25179. int decodedSz = FOURK_BUF;
  25180. #endif
  25181. printf(testingFmt, "PKCS7_signed_enveloped");
  25182. /* load cert */
  25183. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  25184. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  25185. XFCLOSE(f);
  25186. /* load key */
  25187. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  25188. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  25189. XFCLOSE(f);
  25190. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  25191. /* sign cert for envelope */
  25192. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25193. AssertIntEQ(wc_InitRng(&rng), 0);
  25194. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25195. pkcs7->content = cert;
  25196. pkcs7->contentSz = certSz;
  25197. pkcs7->contentOID = DATA;
  25198. pkcs7->privateKey = key;
  25199. pkcs7->privateKeySz = keySz;
  25200. pkcs7->encryptOID = RSAk;
  25201. pkcs7->hashOID = SHA256h;
  25202. pkcs7->rng = &rng;
  25203. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25204. wc_PKCS7_Free(pkcs7);
  25205. wc_FreeRng(&rng);
  25206. #ifdef HAVE_AES_CBC
  25207. /* create envelope */
  25208. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25209. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25210. pkcs7->content = sig;
  25211. pkcs7->contentSz = sigSz;
  25212. pkcs7->contentOID = DATA;
  25213. pkcs7->encryptOID = AES256CBCb;
  25214. pkcs7->privateKey = key;
  25215. pkcs7->privateKeySz = keySz;
  25216. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  25217. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  25218. wc_PKCS7_Free(pkcs7);
  25219. #endif
  25220. /* create bad signed enveloped data */
  25221. sigSz = FOURK_BUF * 2;
  25222. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25223. AssertIntEQ(wc_InitRng(&rng), 0);
  25224. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25225. pkcs7->content = env;
  25226. pkcs7->contentSz = envSz;
  25227. pkcs7->contentOID = DATA;
  25228. pkcs7->privateKey = key;
  25229. pkcs7->privateKeySz = keySz;
  25230. pkcs7->encryptOID = RSAk;
  25231. pkcs7->hashOID = SHA256h;
  25232. pkcs7->rng = &rng;
  25233. /* Set no certs in bundle for this test. Hang on to the pointer though to
  25234. * free it later. */
  25235. pt = (void*)pkcs7->certList;
  25236. pkcs7->certList = NULL; /* no certs in bundle */
  25237. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25238. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  25239. wc_PKCS7_Free(pkcs7);
  25240. /* check verify fails */
  25241. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25242. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25243. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  25244. PKCS7_SIGNEEDS_CHECK);
  25245. /* try verifying the signature manually */
  25246. {
  25247. RsaKey rKey;
  25248. word32 idx = 0;
  25249. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  25250. WC_MAX_DIGEST_SIZE];
  25251. int digestSz;
  25252. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  25253. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  25254. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  25255. digest, sizeof(digest), &rKey);
  25256. AssertIntGT(digestSz, 0);
  25257. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  25258. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  25259. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  25260. /* verify was success */
  25261. }
  25262. wc_PKCS7_Free(pkcs7);
  25263. /* initializing the PKCS7 struct with the signing certificate should pass */
  25264. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25265. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25266. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25267. wc_PKCS7_Free(pkcs7);
  25268. /* create valid degenerate bundle */
  25269. sigSz = FOURK_BUF * 2;
  25270. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25271. pkcs7->content = env;
  25272. pkcs7->contentSz = envSz;
  25273. pkcs7->contentOID = DATA;
  25274. pkcs7->privateKey = key;
  25275. pkcs7->privateKeySz = keySz;
  25276. pkcs7->encryptOID = RSAk;
  25277. pkcs7->hashOID = SHA256h;
  25278. pkcs7->rng = &rng;
  25279. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  25280. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25281. wc_PKCS7_Free(pkcs7);
  25282. wc_FreeRng(&rng);
  25283. /* check verify */
  25284. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25285. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 0);
  25286. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25287. AssertNotNull(pkcs7->content);
  25288. #ifdef HAVE_AES_CBC
  25289. /* check decode */
  25290. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  25291. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  25292. inner->privateKey = key;
  25293. inner->privateKeySz = keySz;
  25294. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  25295. pkcs7->contentSz, decoded, decodedSz)), 0);
  25296. wc_PKCS7_Free(inner);
  25297. #endif
  25298. wc_PKCS7_Free(pkcs7);
  25299. #ifdef HAVE_AES_CBC
  25300. /* check cert set */
  25301. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25302. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25303. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  25304. AssertNotNull(pkcs7->singleCert);
  25305. AssertIntNE(pkcs7->singleCertSz, 0);
  25306. wc_PKCS7_Free(pkcs7);
  25307. #endif
  25308. printf(resultFmt, passed);
  25309. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  25310. return 0;
  25311. }
  25312. static int test_wc_PKCS7_NoDefaultSignedAttribs (void)
  25313. {
  25314. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25315. && !defined(NO_AES)
  25316. PKCS7* pkcs7;
  25317. void* heap = NULL;
  25318. printf(testingFmt, "wc_PKCS7_NoDefaultSignedAttribs()");
  25319. pkcs7 = wc_PKCS7_New(heap, devId);
  25320. AssertNotNull(pkcs7);
  25321. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  25322. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  25323. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  25324. wc_PKCS7_Free(pkcs7);
  25325. printf(resultFmt, passed);
  25326. #endif
  25327. return 0;
  25328. }
  25329. static int test_wc_PKCS7_SetOriEncryptCtx (void)
  25330. {
  25331. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25332. && !defined(NO_AES)
  25333. PKCS7* pkcs7;
  25334. void* heap = NULL;
  25335. WOLFSSL_CTX* ctx;
  25336. ctx = NULL;
  25337. printf(testingFmt, "wc_PKCS7_SetOriEncryptCtx()");
  25338. pkcs7 = wc_PKCS7_New(heap, devId);
  25339. AssertNotNull(pkcs7);
  25340. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  25341. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25342. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  25343. wc_PKCS7_Free(pkcs7);
  25344. printf(resultFmt, passed);
  25345. #endif
  25346. return 0;
  25347. }
  25348. static int test_wc_PKCS7_SetOriDecryptCtx (void)
  25349. {
  25350. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25351. && !defined(NO_AES)
  25352. PKCS7* pkcs7;
  25353. void* heap = NULL;
  25354. WOLFSSL_CTX* ctx;
  25355. ctx = NULL;
  25356. printf(testingFmt, "wc_PKCS7_SetOriDecryptCtx()");
  25357. pkcs7 = wc_PKCS7_New(heap, devId);
  25358. AssertNotNull(pkcs7);
  25359. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
  25360. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25361. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  25362. wc_PKCS7_Free(pkcs7);
  25363. printf(resultFmt, passed);
  25364. #endif
  25365. return 0;
  25366. }
  25367. static int test_wc_PKCS7_DecodeCompressedData(void)
  25368. {
  25369. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25370. && !defined(NO_AES) && defined(HAVE_LIBZ)
  25371. PKCS7* pkcs7;
  25372. void* heap = NULL;
  25373. byte out[4096];
  25374. byte *decompressed;
  25375. int outSz, decompressedSz;
  25376. const char* cert = "./certs/client-cert.pem";
  25377. byte* cert_buf = NULL;
  25378. size_t cert_sz = 0;
  25379. printf(testingFmt, "wc_PKCS7_DecodeCompressedData()");
  25380. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  25381. AssertNotNull((decompressed =
  25382. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  25383. decompressedSz = (int)cert_sz;
  25384. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
  25385. pkcs7->content = (byte*)cert_buf;
  25386. pkcs7->contentSz = (word32)cert_sz;
  25387. pkcs7->contentOID = DATA;
  25388. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  25389. sizeof(out))), 0);
  25390. wc_PKCS7_Free(pkcs7);
  25391. /* compressed key should be smaller than when started */
  25392. AssertIntLT(outSz, cert_sz);
  25393. /* test decompression */
  25394. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, devId)));
  25395. AssertIntEQ(pkcs7->contentOID, 0);
  25396. /* fail case with out buffer too small */
  25397. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25398. decompressed, outSz), 0);
  25399. /* success case */
  25400. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25401. decompressed, decompressedSz), cert_sz);
  25402. AssertIntEQ(pkcs7->contentOID, DATA);
  25403. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25404. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25405. decompressed = NULL;
  25406. /* test decompression function with different 'max' inputs */
  25407. outSz = sizeof(out);
  25408. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  25409. 0);
  25410. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  25411. out, outSz, 0, heap), 0);
  25412. AssertNull(decompressed);
  25413. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  25414. out, outSz, 0, heap), 0);
  25415. AssertNotNull(decompressed);
  25416. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25417. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25418. decompressed = NULL;
  25419. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  25420. out, outSz, 0, heap), 0);
  25421. AssertNotNull(decompressed);
  25422. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25423. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25424. if (cert_buf)
  25425. free(cert_buf);
  25426. wc_PKCS7_Free(pkcs7);
  25427. printf(resultFmt, passed);
  25428. #endif
  25429. return 0;
  25430. }
  25431. static int test_wc_i2d_PKCS12(void)
  25432. {
  25433. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  25434. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25435. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  25436. WC_PKCS12* pkcs12 = NULL;
  25437. unsigned char der[FOURK_BUF * 2];
  25438. unsigned char* pt;
  25439. int derSz;
  25440. unsigned char out[FOURK_BUF * 2];
  25441. int outSz = FOURK_BUF * 2;
  25442. const char p12_f[] = "./certs/test-servercert.p12";
  25443. XFILE f;
  25444. printf(testingFmt, "wc_i2d_PKCS12");
  25445. f = XFOPEN(p12_f, "rb");
  25446. AssertNotNull(f);
  25447. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  25448. AssertIntGT(derSz, 0);
  25449. XFCLOSE(f);
  25450. AssertNotNull(pkcs12 = wc_PKCS12_new());
  25451. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  25452. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25453. AssertIntEQ(outSz, derSz);
  25454. outSz = derSz - 1;
  25455. pt = out;
  25456. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  25457. outSz = derSz;
  25458. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  25459. AssertIntEQ((pt == out), 0);
  25460. pt = NULL;
  25461. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  25462. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  25463. wc_PKCS12_free(pkcs12);
  25464. /* Run the same test but use wc_d2i_PKCS12_fp. */
  25465. AssertNotNull(pkcs12 = wc_PKCS12_new());
  25466. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25467. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25468. AssertIntEQ(outSz, derSz);
  25469. wc_PKCS12_free(pkcs12);
  25470. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  25471. pkcs12 = NULL;
  25472. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25473. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25474. AssertIntEQ(outSz, derSz);
  25475. wc_PKCS12_free(pkcs12);
  25476. printf(resultFmt, passed);
  25477. #endif
  25478. return 0;
  25479. }
  25480. /* Testing wc_SignatureGetSize() for signature type ECC */
  25481. static int test_wc_SignatureGetSize_ecc(void)
  25482. {
  25483. int ret = 0;
  25484. #ifndef NO_SIG_WRAPPER
  25485. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  25486. enum wc_SignatureType sig_type;
  25487. word32 key_len;
  25488. /* Initialize ECC Key */
  25489. ecc_key ecc;
  25490. const char* qx =
  25491. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25492. const char* qy =
  25493. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25494. const char* d =
  25495. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25496. ret = wc_ecc_init(&ecc);
  25497. if (ret == 0) {
  25498. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  25499. }
  25500. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  25501. if (ret == 0) {
  25502. /* Input for signature type ECC */
  25503. sig_type = WC_SIGNATURE_TYPE_ECC;
  25504. key_len = sizeof(ecc_key);
  25505. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  25506. /* Test bad args */
  25507. if (ret > 0) {
  25508. sig_type = (enum wc_SignatureType) 100;
  25509. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  25510. if (ret == BAD_FUNC_ARG) {
  25511. sig_type = WC_SIGNATURE_TYPE_ECC;
  25512. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  25513. }
  25514. if (ret >= 0) {
  25515. key_len = (word32) 0;
  25516. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  25517. }
  25518. if (ret == BAD_FUNC_ARG) {
  25519. ret = SIG_TYPE_E;
  25520. }
  25521. }
  25522. } else {
  25523. ret = WOLFSSL_FATAL_ERROR;
  25524. }
  25525. wc_ecc_free(&ecc);
  25526. #else
  25527. ret = SIG_TYPE_E;
  25528. #endif
  25529. if (ret == SIG_TYPE_E) {
  25530. ret = 0;
  25531. }
  25532. else {
  25533. ret = WOLFSSL_FATAL_ERROR;
  25534. }
  25535. printf(resultFmt, ret == 0 ? passed : failed);
  25536. fflush(stdout);
  25537. #endif /* NO_SIG_WRAPPER */
  25538. return ret;
  25539. }/* END test_wc_SignatureGetSize_ecc() */
  25540. /* Testing wc_SignatureGetSize() for signature type rsa */
  25541. static int test_wc_SignatureGetSize_rsa(void)
  25542. {
  25543. int ret = 0;
  25544. #ifndef NO_SIG_WRAPPER
  25545. #ifndef NO_RSA
  25546. enum wc_SignatureType sig_type;
  25547. word32 key_len;
  25548. word32 idx = 0;
  25549. /* Initialize RSA Key */
  25550. RsaKey rsa_key;
  25551. byte* tmp = NULL;
  25552. size_t bytes;
  25553. #ifdef USE_CERT_BUFFERS_1024
  25554. bytes = (size_t)sizeof_client_key_der_1024;
  25555. if (bytes < (size_t)sizeof_client_key_der_1024)
  25556. bytes = (size_t)sizeof_client_cert_der_1024;
  25557. #elif defined(USE_CERT_BUFFERS_2048)
  25558. bytes = (size_t)sizeof_client_key_der_2048;
  25559. if (bytes < (size_t)sizeof_client_cert_der_2048)
  25560. bytes = (size_t)sizeof_client_cert_der_2048;
  25561. #else
  25562. bytes = FOURK_BUF;
  25563. #endif
  25564. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25565. if (tmp != NULL) {
  25566. #ifdef USE_CERT_BUFFERS_1024
  25567. XMEMCPY(tmp, client_key_der_1024,
  25568. (size_t)sizeof_client_key_der_1024);
  25569. #elif defined(USE_CERT_BUFFERS_2048)
  25570. XMEMCPY(tmp, client_key_der_2048,
  25571. (size_t)sizeof_client_key_der_2048);
  25572. #elif !defined(NO_FILESYSTEM)
  25573. file = XFOPEN(clientKey, "rb");
  25574. if (file != XBADFILE) {
  25575. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  25576. XFCLOSE(file);
  25577. }
  25578. else {
  25579. ret = WOLFSSL_FATAL_ERROR;
  25580. }
  25581. #else
  25582. ret = WOLFSSL_FATAL_ERROR;
  25583. #endif
  25584. } else {
  25585. ret = WOLFSSL_FATAL_ERROR;
  25586. }
  25587. if (ret == 0) {
  25588. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, devId);
  25589. }
  25590. if (ret == 0) {
  25591. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  25592. }
  25593. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  25594. if (ret == 0) {
  25595. /* Input for signature type RSA */
  25596. sig_type = WC_SIGNATURE_TYPE_RSA;
  25597. key_len = sizeof(RsaKey);
  25598. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25599. /* Test bad args */
  25600. if (ret > 0) {
  25601. sig_type = (enum wc_SignatureType) 100;
  25602. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25603. if (ret == BAD_FUNC_ARG) {
  25604. sig_type = WC_SIGNATURE_TYPE_RSA;
  25605. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  25606. }
  25607. #ifndef HAVE_USER_RSA
  25608. if (ret == BAD_FUNC_ARG) {
  25609. #else
  25610. if (ret == 0) {
  25611. #endif
  25612. key_len = (word32)0;
  25613. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25614. }
  25615. if (ret == BAD_FUNC_ARG) {
  25616. ret = SIG_TYPE_E;
  25617. }
  25618. }
  25619. } else {
  25620. ret = WOLFSSL_FATAL_ERROR;
  25621. }
  25622. wc_FreeRsaKey(&rsa_key);
  25623. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25624. #else
  25625. ret = SIG_TYPE_E;
  25626. #endif
  25627. if (ret == SIG_TYPE_E) {
  25628. ret = 0;
  25629. }else {
  25630. ret = WOLFSSL_FATAL_ERROR;
  25631. }
  25632. printf(resultFmt, ret == 0 ? passed : failed);
  25633. #endif /* NO_SIG_WRAPPER */
  25634. return ret;
  25635. }/* END test_wc_SignatureGetSize_rsa(void) */
  25636. /*----------------------------------------------------------------------------*
  25637. | hash.h Tests
  25638. *----------------------------------------------------------------------------*/
  25639. static int test_wc_HashInit(void)
  25640. {
  25641. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  25642. wc_HashAlg hash;
  25643. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25644. enum wc_HashType enumArray[] = {
  25645. #ifndef NO_MD5
  25646. WC_HASH_TYPE_MD5,
  25647. #endif
  25648. #ifndef NO_SHA
  25649. WC_HASH_TYPE_SHA,
  25650. #endif
  25651. #ifndef WOLFSSL_SHA224
  25652. WC_HASH_TYPE_SHA224,
  25653. #endif
  25654. #ifndef NO_SHA256
  25655. WC_HASH_TYPE_SHA256,
  25656. #endif
  25657. #ifndef WOLFSSL_SHA384
  25658. WC_HASH_TYPE_SHA384,
  25659. #endif
  25660. #ifndef WOLFSSL_SHA512
  25661. WC_HASH_TYPE_SHA512,
  25662. #endif
  25663. };
  25664. /* dynamically finds the length */
  25665. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25666. /* For loop to test various arguments... */
  25667. for (i = 0; i < enumlen; i++) {
  25668. /* check for bad args */
  25669. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  25670. ret = 1;
  25671. break;
  25672. }
  25673. wc_HashFree(&hash, enumArray[i]);
  25674. /* check for null ptr */
  25675. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  25676. ret = 1;
  25677. break;
  25678. }
  25679. } /* end of for loop */
  25680. printf(testingFmt, "wc_HashInit()");
  25681. if (ret==0) { /* all tests have passed */
  25682. printf(resultFmt, passed);
  25683. }
  25684. else { /* a test has failed */
  25685. printf(resultFmt, failed);
  25686. }
  25687. return ret;
  25688. } /* end of test_wc_HashInit */
  25689. /*
  25690. * Unit test function for wc_HashSetFlags()
  25691. */
  25692. static int test_wc_HashSetFlags(void)
  25693. {
  25694. int ret = 0;
  25695. #ifdef WOLFSSL_HASH_FLAGS
  25696. wc_HashAlg hash;
  25697. word32 flags = 0;
  25698. int i, j;
  25699. int notSupportedLen;
  25700. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25701. enum wc_HashType enumArray[] = {
  25702. #ifndef NO_MD5
  25703. WC_HASH_TYPE_MD5,
  25704. #endif
  25705. #ifndef NO_SHA
  25706. WC_HASH_TYPE_SHA,
  25707. #endif
  25708. #ifdef WOLFSSL_SHA224
  25709. WC_HASH_TYPE_SHA224,
  25710. #endif
  25711. #ifndef NO_SHA256
  25712. WC_HASH_TYPE_SHA256,
  25713. #endif
  25714. #ifdef WOLFSSL_SHA384
  25715. WC_HASH_TYPE_SHA384,
  25716. #endif
  25717. #ifdef WOLFSSL_SHA512
  25718. WC_HASH_TYPE_SHA512,
  25719. #endif
  25720. #ifdef WOLFSSL_SHA3
  25721. WC_HASH_TYPE_SHA3_224,
  25722. #endif
  25723. };
  25724. enum wc_HashType notSupported[] = {
  25725. WC_HASH_TYPE_MD5_SHA,
  25726. WC_HASH_TYPE_MD2,
  25727. WC_HASH_TYPE_MD4,
  25728. WC_HASH_TYPE_BLAKE2B,
  25729. WC_HASH_TYPE_BLAKE2S,
  25730. WC_HASH_TYPE_NONE,
  25731. };
  25732. /* dynamically finds the length */
  25733. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25734. printf(testingFmt, "wc_HashSetFlags()");
  25735. /* For loop to test various arguments... */
  25736. for (i = 0; i < enumlen; i++) {
  25737. ret = wc_HashInit(&hash, enumArray[i]);
  25738. if (ret == 0) {
  25739. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  25740. }
  25741. if (ret == 0) {
  25742. if (flags & WC_HASH_FLAG_ISCOPY) {
  25743. ret = 0;
  25744. }
  25745. }
  25746. if (ret == 0) {
  25747. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  25748. if (ret == BAD_FUNC_ARG) {
  25749. ret = 0;
  25750. }
  25751. }
  25752. wc_HashFree(&hash, enumArray[i]);
  25753. }
  25754. /* For loop to test not supported cases */
  25755. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25756. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  25757. ret = wc_HashInit(&hash, notSupported[j]);
  25758. if (ret == 0) {
  25759. ret = -1;
  25760. }
  25761. else if (ret == BAD_FUNC_ARG){
  25762. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  25763. if (ret == 0) {
  25764. ret = -1;
  25765. }
  25766. else if (ret == BAD_FUNC_ARG) {
  25767. ret = 0;
  25768. }
  25769. }
  25770. if (ret == 0) {
  25771. ret = wc_HashFree(&hash, notSupported[j]);
  25772. if (ret == 0) {
  25773. ret = -1;
  25774. }
  25775. else if (ret == BAD_FUNC_ARG) {
  25776. ret = 0;
  25777. }
  25778. }
  25779. }
  25780. printf(resultFmt, ret == 0 ? passed : failed);
  25781. fflush(stdout);
  25782. #endif
  25783. return ret;
  25784. } /* END test_wc_HashSetFlags */
  25785. /*
  25786. * Unit test function for wc_HashGetFlags()
  25787. */
  25788. static int test_wc_HashGetFlags(void)
  25789. {
  25790. int ret = 0;
  25791. #ifdef WOLFSSL_HASH_FLAGS
  25792. wc_HashAlg hash;
  25793. word32 flags = 0;
  25794. int i, j;
  25795. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25796. enum wc_HashType enumArray[] = {
  25797. #ifndef NO_MD5
  25798. WC_HASH_TYPE_MD5,
  25799. #endif
  25800. #ifndef NO_SHA
  25801. WC_HASH_TYPE_SHA,
  25802. #endif
  25803. #ifdef WOLFSSL_SHA224
  25804. WC_HASH_TYPE_SHA224,
  25805. #endif
  25806. #ifndef NO_SHA256
  25807. WC_HASH_TYPE_SHA256,
  25808. #endif
  25809. #ifdef WOLFSSL_SHA384
  25810. WC_HASH_TYPE_SHA384,
  25811. #endif
  25812. #ifdef WOLFSSL_SHA512
  25813. WC_HASH_TYPE_SHA512,
  25814. #endif
  25815. #ifdef WOLFSSL_SHA3
  25816. WC_HASH_TYPE_SHA3_224,
  25817. #endif
  25818. };
  25819. enum wc_HashType notSupported[] = {
  25820. WC_HASH_TYPE_MD5_SHA,
  25821. WC_HASH_TYPE_MD2,
  25822. WC_HASH_TYPE_MD4,
  25823. WC_HASH_TYPE_BLAKE2B,
  25824. WC_HASH_TYPE_BLAKE2S,
  25825. WC_HASH_TYPE_NONE,
  25826. };
  25827. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25828. int notSupportedLen;
  25829. printf(testingFmt, "wc_HashGetFlags()");
  25830. /* For loop to test various arguments... */
  25831. for (i = 0; i < enumlen; i++) {
  25832. ret = wc_HashInit(&hash, enumArray[i]);
  25833. if (ret == 0) {
  25834. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  25835. }
  25836. if (ret == 0) {
  25837. if (flags & WC_HASH_FLAG_ISCOPY) {
  25838. ret = 0;
  25839. }
  25840. }
  25841. if (ret == 0) {
  25842. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  25843. if (ret == BAD_FUNC_ARG) {
  25844. ret = 0;
  25845. }
  25846. }
  25847. wc_HashFree(&hash, enumArray[i]);
  25848. if (ret != 0) {
  25849. break;
  25850. }
  25851. }
  25852. /* For loop to test not supported cases */
  25853. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25854. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  25855. ret = wc_HashInit(&hash, notSupported[j]);
  25856. if (ret == 0) {
  25857. ret = -1;
  25858. }
  25859. else if (ret == BAD_FUNC_ARG){
  25860. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  25861. if (ret == 0) {
  25862. ret = -1;
  25863. }
  25864. else if (ret == BAD_FUNC_ARG) {
  25865. ret = 0;
  25866. }
  25867. }
  25868. if (ret == 0) {
  25869. ret = wc_HashFree(&hash, notSupported[j]);
  25870. if (ret == 0) {
  25871. ret = -1;
  25872. }
  25873. if (ret == BAD_FUNC_ARG) {
  25874. ret = 0;
  25875. }
  25876. }
  25877. }
  25878. printf(resultFmt, ret == 0 ? passed : failed);
  25879. fflush(stdout);
  25880. #endif
  25881. return ret;
  25882. } /* END test_wc_HashGetFlags */
  25883. /*----------------------------------------------------------------------------*
  25884. | Compatibility Tests
  25885. *----------------------------------------------------------------------------*/
  25886. static int test_wolfSSL_lhash(void)
  25887. {
  25888. #ifdef OPENSSL_ALL
  25889. const char testStr[] = "Like a true nature's child\n"
  25890. "We were born\n"
  25891. "Born to be wild";
  25892. printf(testingFmt, "wolfSSL_LH_strhash()");
  25893. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  25894. printf(resultFmt, passed);
  25895. #endif
  25896. return 0;
  25897. }
  25898. static int test_wolfSSL_X509_NAME(void)
  25899. {
  25900. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  25901. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25902. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  25903. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  25904. defined(OPENSSL_EXTRA))
  25905. X509* x509;
  25906. const unsigned char* c;
  25907. unsigned char buf[4096];
  25908. int bytes;
  25909. XFILE f;
  25910. const X509_NAME* a;
  25911. const X509_NAME* b;
  25912. X509_NAME* d2i_name = NULL;
  25913. int sz;
  25914. unsigned char* tmp;
  25915. char file[] = "./certs/ca-cert.der";
  25916. #ifndef OPENSSL_EXTRA_X509_SMALL
  25917. byte empty[] = { /* CN=empty emailAddress= */
  25918. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  25919. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  25920. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  25921. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25922. 0x01, 0x16, 0x00
  25923. };
  25924. #endif
  25925. printf(testingFmt, "wolfSSL_X509_NAME()");
  25926. #ifndef OPENSSL_EXTRA_X509_SMALL
  25927. /* test compile of deprecated function, returns 0 */
  25928. AssertIntEQ(CRYPTO_thread_id(), 0);
  25929. #endif
  25930. AssertNotNull(a = X509_NAME_new());
  25931. X509_NAME_free((X509_NAME*)a);
  25932. f = XFOPEN(file, "rb");
  25933. AssertTrue(f != XBADFILE);
  25934. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  25935. XFCLOSE(f);
  25936. c = buf;
  25937. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  25938. /* test cmp function */
  25939. AssertNotNull(a = X509_get_issuer_name(x509));
  25940. AssertNotNull(b = X509_get_subject_name(x509));
  25941. #ifndef OPENSSL_EXTRA_X509_SMALL
  25942. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  25943. #endif
  25944. tmp = buf;
  25945. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  25946. if (sz > 0 && tmp == buf) {
  25947. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__);
  25948. printf(" Expected pointer to be incremented\n");
  25949. abort();
  25950. }
  25951. #ifndef OPENSSL_EXTRA_X509_SMALL
  25952. tmp = buf;
  25953. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  25954. #endif
  25955. /* if output parameter is NULL, should still return required size. */
  25956. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  25957. /* retry but with the function creating a buffer */
  25958. tmp = NULL;
  25959. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  25960. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  25961. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  25962. #ifndef OPENSSL_EXTRA_X509_SMALL
  25963. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  25964. #endif
  25965. X509_NAME_free((X509_NAME*)b);
  25966. X509_NAME_free(d2i_name);
  25967. X509_free(x509);
  25968. #ifndef OPENSSL_EXTRA_X509_SMALL
  25969. /* test with an empty domain component */
  25970. tmp = empty;
  25971. sz = sizeof(empty);
  25972. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  25973. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  25974. /* size of empty emailAddress will be 0 */
  25975. tmp = buf;
  25976. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  25977. (char*)tmp, sizeof(buf)), 0);
  25978. /* should contain no organization name */
  25979. tmp = buf;
  25980. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  25981. (char*)tmp, sizeof(buf)), -1);
  25982. X509_NAME_free(d2i_name);
  25983. #endif
  25984. printf(resultFmt, passed);
  25985. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  25986. return 0;
  25987. }
  25988. static int test_wolfSSL_X509_NAME_hash(void)
  25989. {
  25990. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  25991. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  25992. BIO* bio;
  25993. X509* x509 = NULL;
  25994. printf(testingFmt, "wolfSSL_X509_NAME_hash");
  25995. AssertNotNull(bio = BIO_new(BIO_s_file()));
  25996. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  25997. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  25998. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  25999. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  26000. X509_free(x509);
  26001. BIO_free(bio);
  26002. printf(resultFmt, passed);
  26003. #endif
  26004. return 0;
  26005. }
  26006. static int test_wolfSSL_X509_NAME_print_ex(void)
  26007. {
  26008. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  26009. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  26010. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  26011. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  26012. !defined(NO_BIO) && !defined(NO_RSA)
  26013. int memSz;
  26014. byte* mem = NULL;
  26015. BIO* bio = NULL;
  26016. BIO* membio = NULL;
  26017. X509* x509 = NULL;
  26018. X509_NAME* name = NULL;
  26019. const char* expNormal = "C=US,CN=wolfssl.com";
  26020. const char* expReverse = "CN=wolfssl.com,C=US";
  26021. const char* expNotEscaped = "C= US,+\"\\ ,CN=#wolfssl.com<>;";
  26022. const char* expNotEscapedRev = "CN=#wolfssl.com<>;,C= US,+\"\\ ";
  26023. const char* expRFC5523 =
  26024. "CN=\\#wolfssl.com\\<\\>\\;,C=\\ US\\,\\+\\\"\\\\\\ ";
  26025. printf(testingFmt, "wolfSSL_X509_NAME_print_ex");
  26026. /* Test with real cert (svrCertFile) first */
  26027. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26028. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26029. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26030. AssertNotNull(name = X509_get_subject_name(x509));
  26031. /* Test without flags */
  26032. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26033. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26034. BIO_free(membio);
  26035. /* Test flag: XN_FLAG_RFC2253 */
  26036. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26037. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26038. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26039. BIO_free(membio);
  26040. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  26041. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26042. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26043. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26044. BIO_free(membio);
  26045. X509_free(x509);
  26046. BIO_free(bio);
  26047. /* Test normal case without escaped characters */
  26048. {
  26049. /* Create name: "/C=US/CN=wolfssl.com" */
  26050. AssertNotNull(name = X509_NAME_new());
  26051. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26052. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  26053. WOLFSSL_SUCCESS);
  26054. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26055. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  26056. WOLFSSL_SUCCESS);
  26057. /* Test without flags */
  26058. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26059. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26060. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26061. AssertIntEQ(memSz, XSTRLEN(expNormal)+1);
  26062. AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  26063. BIO_free(membio);
  26064. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  26065. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26066. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26067. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26068. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26069. AssertIntEQ(memSz, XSTRLEN(expReverse)+1);
  26070. BIO_free(membio);
  26071. /* Test flags: XN_FLAG_DN_REV - reversed */
  26072. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26073. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26074. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26075. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26076. AssertIntEQ(memSz, XSTRLEN(expReverse)+1);
  26077. AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  26078. BIO_free(membio);
  26079. X509_NAME_free(name);
  26080. }
  26081. /* Test RFC2253 characters are escaped with backslashes */
  26082. {
  26083. AssertNotNull(name = X509_NAME_new());
  26084. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26085. /* space at beginning and end, and: ,+"\ */
  26086. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  26087. WOLFSSL_SUCCESS);
  26088. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26089. /* # at beginning, and: <>;*/
  26090. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  26091. WOLFSSL_SUCCESS);
  26092. /* Test without flags */
  26093. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26094. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26095. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26096. AssertIntEQ(memSz, XSTRLEN(expNotEscaped)+1);
  26097. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  26098. XSTRLEN(expNotEscaped)), 0);
  26099. BIO_free(membio);
  26100. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  26101. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26102. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26103. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26104. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26105. AssertIntEQ(memSz, XSTRLEN(expRFC5523)+1);
  26106. AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  26107. BIO_free(membio);
  26108. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  26109. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26110. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26111. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26112. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26113. AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev)+1);
  26114. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  26115. XSTRLEN(expNotEscapedRev)), 0);
  26116. BIO_free(membio);
  26117. X509_NAME_free(name);
  26118. }
  26119. printf(resultFmt, passed);
  26120. #endif
  26121. return 0;
  26122. }
  26123. #ifndef NO_BIO
  26124. static int test_wolfSSL_X509_INFO_multiple_info(void)
  26125. {
  26126. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26127. STACK_OF(X509_INFO) *info_stack;
  26128. X509_INFO *info;
  26129. int len;
  26130. int i;
  26131. const char* files[] = {
  26132. cliCertFile,
  26133. cliKeyFile,
  26134. /* This needs to be the order as svrCertFile contains the
  26135. * intermediate cert as well. */
  26136. svrKeyFile,
  26137. svrCertFile,
  26138. NULL,
  26139. };
  26140. const char** curFile;
  26141. BIO *fileBIO;
  26142. BIO *concatBIO = NULL;
  26143. byte tmp[FOURK_BUF];
  26144. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  26145. * to group objects together. */
  26146. AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
  26147. for (curFile = files; *curFile != NULL; curFile++) {
  26148. int fileLen;
  26149. AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  26150. fileLen = wolfSSL_BIO_get_len(fileBIO);
  26151. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  26152. AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
  26153. fileLen -= len;
  26154. }
  26155. /* Make sure we read the entire file */
  26156. AssertIntEQ(fileLen, 0);
  26157. BIO_free(fileBIO);
  26158. }
  26159. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  26160. NULL));
  26161. AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
  26162. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26163. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  26164. AssertNotNull(info->x509);
  26165. AssertNull(info->crl);
  26166. if (i != 0) {
  26167. AssertNotNull(info->x_pkey);
  26168. AssertIntEQ(X509_check_private_key(info->x509,
  26169. info->x_pkey->dec_pkey), 1);
  26170. }
  26171. else {
  26172. AssertNull(info->x_pkey);
  26173. }
  26174. }
  26175. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26176. BIO_free(concatBIO);
  26177. #endif
  26178. return 0;
  26179. }
  26180. #endif
  26181. #ifndef NO_BIO
  26182. static int test_wolfSSL_X509_INFO(void)
  26183. {
  26184. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26185. STACK_OF(X509_INFO) *info_stack;
  26186. X509_INFO *info;
  26187. BIO *cert;
  26188. int i;
  26189. /* PEM in hex format to avoid null terminator */
  26190. byte data[] = {
  26191. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  26192. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  26193. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  26194. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  26195. 0x2d, 0x2d, 0x2d
  26196. };
  26197. /* PEM in hex format to avoid null terminator */
  26198. byte data2[] = {
  26199. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  26200. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  26201. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  26202. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  26203. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  26204. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  26205. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  26206. };
  26207. printf(testingFmt, "wolfSSL_X509_INFO");
  26208. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26209. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26210. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26211. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  26212. AssertNotNull(info->x509);
  26213. AssertNull(info->crl);
  26214. AssertNull(info->x_pkey);
  26215. }
  26216. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26217. BIO_free(cert);
  26218. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26219. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26220. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26221. BIO_free(cert);
  26222. /* This case should fail due to invalid input. */
  26223. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  26224. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  26225. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26226. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26227. BIO_free(cert);
  26228. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  26229. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  26230. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26231. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26232. BIO_free(cert);
  26233. printf(resultFmt, passed);
  26234. #endif
  26235. return 0;
  26236. }
  26237. #endif
  26238. static int test_wolfSSL_X509_subject_name_hash(void)
  26239. {
  26240. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26241. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  26242. X509* x509;
  26243. X509_NAME* subjectName = NULL;
  26244. unsigned long ret = 0;
  26245. printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
  26246. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26247. SSL_FILETYPE_PEM));
  26248. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  26249. ret = X509_subject_name_hash(x509);
  26250. AssertIntNE(ret, 0);
  26251. X509_free(x509);
  26252. printf(resultFmt, passed);
  26253. #endif
  26254. return 0;
  26255. }
  26256. static int test_wolfSSL_X509_issuer_name_hash(void)
  26257. {
  26258. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26259. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  26260. X509* x509;
  26261. X509_NAME* issuertName = NULL;
  26262. unsigned long ret = 0;
  26263. printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
  26264. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26265. SSL_FILETYPE_PEM));
  26266. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  26267. ret = X509_issuer_name_hash(x509);
  26268. AssertIntNE(ret, 0);
  26269. X509_free(x509);
  26270. printf(resultFmt, passed);
  26271. #endif
  26272. return 0;
  26273. }
  26274. static int test_wolfSSL_X509_check_host(void)
  26275. {
  26276. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26277. && !defined(NO_SHA) && !defined(NO_RSA)
  26278. X509* x509;
  26279. const char altName[] = "example.com";
  26280. printf(testingFmt, "wolfSSL_X509_check_host()");
  26281. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26282. SSL_FILETYPE_PEM));
  26283. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  26284. WOLFSSL_SUCCESS);
  26285. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  26286. WOLFSSL_FAILURE);
  26287. X509_free(x509);
  26288. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  26289. WOLFSSL_FAILURE);
  26290. printf(resultFmt, passed);
  26291. #endif
  26292. return 0;
  26293. }
  26294. static int test_wolfSSL_X509_check_email(void)
  26295. {
  26296. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  26297. X509* x509;
  26298. const char goodEmail[] = "info@wolfssl.com";
  26299. const char badEmail[] = "disinfo@wolfssl.com";
  26300. printf(testingFmt, "wolfSSL_X509_check_email()");
  26301. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26302. SSL_FILETYPE_PEM));
  26303. /* Should fail on non-matching email address */
  26304. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  26305. WOLFSSL_FAILURE);
  26306. /* Should succeed on matching email address */
  26307. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  26308. WOLFSSL_SUCCESS);
  26309. /* Should compute length internally when not provided */
  26310. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  26311. WOLFSSL_SUCCESS);
  26312. /* Should fail when email address is NULL */
  26313. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  26314. WOLFSSL_FAILURE);
  26315. X509_free(x509);
  26316. /* Should fail when x509 is NULL */
  26317. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  26318. WOLFSSL_FAILURE);
  26319. printf(resultFmt, passed);
  26320. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  26321. return 0;
  26322. }
  26323. static int test_wolfSSL_DES(void)
  26324. {
  26325. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  26326. const_DES_cblock myDes;
  26327. DES_cblock iv;
  26328. DES_key_schedule key;
  26329. word32 i;
  26330. DES_LONG dl;
  26331. unsigned char msg[] = "hello wolfssl";
  26332. printf(testingFmt, "wolfSSL_DES()");
  26333. DES_check_key(1);
  26334. DES_set_key(&myDes, &key);
  26335. /* check, check of odd parity */
  26336. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  26337. XMEMSET(key, 5, sizeof(DES_key_schedule));
  26338. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  26339. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  26340. /* set odd parity for success case */
  26341. DES_set_odd_parity(&myDes);
  26342. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  26343. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  26344. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  26345. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26346. AssertIntEQ(key[i], myDes[i]);
  26347. }
  26348. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  26349. /* check weak key */
  26350. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  26351. XMEMSET(key, 5, sizeof(DES_key_schedule));
  26352. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  26353. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  26354. /* now do unchecked copy of a weak key over */
  26355. DES_set_key_unchecked(&myDes, &key);
  26356. /* compare arrays, should be the same */
  26357. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26358. AssertIntEQ(key[i], myDes[i]);
  26359. }
  26360. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  26361. /* check DES_key_sched API */
  26362. XMEMSET(key, 1, sizeof(DES_key_schedule));
  26363. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  26364. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  26365. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  26366. /* compare arrays, should be the same */
  26367. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26368. AssertIntEQ(key[i], myDes[i]);
  26369. }
  26370. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  26371. * DES_cbc_encrypt on the input */
  26372. XMEMSET(iv, 0, sizeof(DES_cblock));
  26373. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  26374. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  26375. AssertIntEQ(dl, 480052723);
  26376. printf(resultFmt, passed);
  26377. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  26378. return 0;
  26379. }
  26380. static int test_wc_PemToDer(void)
  26381. {
  26382. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  26383. int ret;
  26384. DerBuffer* pDer = NULL;
  26385. const char* ca_cert = "./certs/server-cert.pem";
  26386. byte* cert_buf = NULL;
  26387. size_t cert_sz = 0;
  26388. int eccKey = 0;
  26389. EncryptedInfo info;
  26390. printf(testingFmt, "wc_PemToDer()");
  26391. XMEMSET(&info, 0, sizeof(info));
  26392. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  26393. if (ret == 0) {
  26394. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  26395. &pDer, NULL, &info, &eccKey);
  26396. AssertIntEQ(ret, 0);
  26397. wc_FreeDer(&pDer);
  26398. }
  26399. if (cert_buf)
  26400. free(cert_buf);
  26401. #ifdef HAVE_ECC
  26402. {
  26403. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  26404. byte key_buf[256] = {0};
  26405. /* Test fail of loading a key with cert type */
  26406. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  26407. key_buf[0] = '\n';
  26408. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  26409. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  26410. &pDer, NULL, &info, &eccKey)), 0);
  26411. #ifdef OPENSSL_EXTRA
  26412. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  26413. &pDer, NULL, &info, &eccKey)), 0);
  26414. #endif
  26415. wc_FreeDer(&pDer);
  26416. if (cert_buf)
  26417. free(cert_buf);
  26418. }
  26419. #endif
  26420. printf(resultFmt, passed);
  26421. #endif
  26422. return 0;
  26423. }
  26424. static int test_wc_AllocDer(void)
  26425. {
  26426. #if !defined(NO_CERTS)
  26427. int ret;
  26428. DerBuffer* pDer = NULL;
  26429. word32 testSize = 1024;
  26430. printf(testingFmt, "wc_AllocDer()");
  26431. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  26432. AssertIntEQ(ret, 0);
  26433. AssertNotNull(pDer);
  26434. wc_FreeDer(&pDer);
  26435. printf(resultFmt, passed);
  26436. #endif
  26437. return 0;
  26438. }
  26439. static int test_wc_CertPemToDer(void)
  26440. {
  26441. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  26442. int ret;
  26443. const char* ca_cert = "./certs/ca-cert.pem";
  26444. byte* cert_buf = NULL;
  26445. size_t cert_sz = 0, cert_dersz = 0;
  26446. byte* cert_der = NULL;
  26447. printf(testingFmt, "wc_CertPemToDer()");
  26448. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  26449. if (ret == 0) {
  26450. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26451. cert_der = (byte*)malloc(cert_dersz);
  26452. if (cert_der) {
  26453. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  26454. cert_der, (int)cert_dersz, CERT_TYPE);
  26455. AssertIntGE(ret, 0);
  26456. }
  26457. }
  26458. if (cert_der)
  26459. free(cert_der);
  26460. if (cert_buf)
  26461. free(cert_buf);
  26462. printf(resultFmt, passed);
  26463. #endif
  26464. return 0;
  26465. }
  26466. static int test_wc_PubKeyPemToDer(void)
  26467. {
  26468. #ifdef WOLFSSL_PEM_TO_DER
  26469. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  26470. int ret;
  26471. const char* key = "./certs/ecc-client-keyPub.pem";
  26472. byte* cert_buf = NULL;
  26473. size_t cert_sz = 0, cert_dersz = 0;
  26474. byte* cert_der = NULL;
  26475. printf(testingFmt, "wc_PubKeyPemToDer()");
  26476. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  26477. cert_der, (int)cert_dersz);
  26478. AssertIntGE(ret, BAD_FUNC_ARG);
  26479. ret = load_file(key, &cert_buf, &cert_sz);
  26480. if (ret == 0) {
  26481. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26482. cert_der = (byte*)malloc(cert_dersz);
  26483. if (cert_der) {
  26484. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  26485. cert_der, (int)cert_dersz);
  26486. AssertIntGE(ret, 0);
  26487. }
  26488. }
  26489. if (cert_der)
  26490. free(cert_der);
  26491. if (cert_buf)
  26492. free(cert_buf);
  26493. printf(resultFmt, passed);
  26494. #endif
  26495. #endif
  26496. return 0;
  26497. }
  26498. static int test_wc_PemPubKeyToDer(void)
  26499. {
  26500. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  26501. int ret;
  26502. const char* key = "./certs/ecc-client-keyPub.pem";
  26503. size_t cert_dersz = 1024;
  26504. byte* cert_der = (byte*)malloc(cert_dersz);
  26505. printf(testingFmt, "wc_PemPubKeyToDer()");
  26506. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  26507. AssertIntGE(ret, BAD_FUNC_ARG);
  26508. if (cert_der) {
  26509. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  26510. AssertIntGE(ret, 0);
  26511. free(cert_der);
  26512. }
  26513. printf(resultFmt, passed);
  26514. #endif
  26515. return 0;
  26516. }
  26517. static int test_wc_GetPubKeyDerFromCert(void)
  26518. {
  26519. #if !defined(NO_RSA) || defined(HAVE_ECC)
  26520. int ret;
  26521. word32 idx = 0;
  26522. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  26523. word32 keyDerSz = (word32)sizeof(keyDer);
  26524. DecodedCert decoded;
  26525. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ)
  26526. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  26527. word32 certBufSz = sizeof(certBuf);
  26528. #endif
  26529. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  26530. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA)
  26531. XFILE fp;
  26532. #endif
  26533. #ifndef NO_RSA
  26534. RsaKey rsaKey;
  26535. #if defined(USE_CERT_BUFFERS_2048)
  26536. byte* rsaCertDer = (byte*)client_cert_der_2048;
  26537. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  26538. #elif defined(USE_CERT_BUFFERS_1024)
  26539. byte* rsaCertDer = (byte*)client_cert_der_1024;
  26540. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  26541. #else
  26542. unsigned char rsaCertDer[TWOK_BUF];
  26543. word32 rsaCertDerSz;
  26544. #endif
  26545. #endif
  26546. #ifdef HAVE_ECC
  26547. ecc_key eccKey;
  26548. #if defined(USE_CERT_BUFFERS_256)
  26549. byte* eccCert = (byte*)cliecc_cert_der_256;
  26550. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  26551. #else
  26552. unsigned char eccCert[ONEK_BUF];
  26553. word32 eccCertSz;
  26554. XFILE fp2;
  26555. #endif
  26556. #endif
  26557. printf(testingFmt, "wc_GetPubKeyDerFromCert()");
  26558. #ifndef NO_RSA
  26559. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  26560. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  26561. AssertTrue((fp != XBADFILE));
  26562. rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
  26563. XFCLOSE(fp);
  26564. #endif
  26565. /* good test case - RSA DER cert */
  26566. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  26567. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  26568. AssertIntEQ(ret, 0);
  26569. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26570. AssertIntEQ(ret, 0);
  26571. AssertIntGT(keyDerSz, 0);
  26572. /* sanity check, verify we can import DER public key */
  26573. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26574. AssertIntEQ(ret, 0);
  26575. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  26576. AssertIntEQ(ret, 0);
  26577. wc_FreeRsaKey(&rsaKey);
  26578. /* test LENGTH_ONLY_E case */
  26579. keyDerSz = 0;
  26580. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  26581. AssertIntEQ(ret, LENGTH_ONLY_E);
  26582. AssertIntGT(keyDerSz, 0);
  26583. /* bad args: DecodedCert NULL */
  26584. ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
  26585. AssertIntEQ(ret, BAD_FUNC_ARG);
  26586. /* bad args: output key buff size */
  26587. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
  26588. AssertIntEQ(ret, BAD_FUNC_ARG);
  26589. /* bad args: zero size output key buffer */
  26590. keyDerSz = 0;
  26591. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26592. AssertIntEQ(ret, BAD_FUNC_ARG);
  26593. wc_FreeDecodedCert(&decoded);
  26594. /* Certificate Request Tests */
  26595. #ifdef WOLFSSL_CERT_REQ
  26596. {
  26597. XMEMSET(certBuf, 0, sizeof(certBuf));
  26598. fp = XFOPEN("./certs/csr.signed.der", "rb");
  26599. AssertTrue((fp != XBADFILE));
  26600. certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
  26601. XFCLOSE(fp);
  26602. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  26603. ret = wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL);
  26604. AssertIntEQ(ret, 0);
  26605. /* good test case - RSA DER certificate request */
  26606. keyDerSz = sizeof(keyDer);
  26607. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26608. AssertIntEQ(ret, 0);
  26609. AssertIntGT(keyDerSz, 0);
  26610. /* sanity check, verify we can import DER public key */
  26611. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26612. AssertIntEQ(ret, 0);
  26613. idx = 0;
  26614. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  26615. AssertIntEQ(ret, 0);
  26616. wc_FreeRsaKey(&rsaKey);
  26617. wc_FreeDecodedCert(&decoded);
  26618. }
  26619. #endif /* WOLFSSL_CERT_REQ */
  26620. #endif /* NO_RSA */
  26621. #ifdef HAVE_ECC
  26622. #ifndef USE_CERT_BUFFERS_256
  26623. fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
  26624. AssertTrue((fp2 != XBADFILE));
  26625. eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
  26626. XFCLOSE(fp2);
  26627. #endif
  26628. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  26629. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  26630. AssertIntEQ(ret, 0);
  26631. /* good test case - ECC */
  26632. XMEMSET(keyDer, 0, sizeof(keyDer));
  26633. keyDerSz = sizeof(keyDer);
  26634. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26635. AssertIntEQ(ret, 0);
  26636. AssertIntGT(keyDerSz, 0);
  26637. /* sanity check, verify we can import DER public key */
  26638. ret = wc_ecc_init(&eccKey);
  26639. AssertIntEQ(ret, 0);
  26640. idx = 0; /* reset idx to 0, used above in RSA case */
  26641. ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
  26642. AssertIntEQ(ret, 0);
  26643. wc_ecc_free(&eccKey);
  26644. /* test LENGTH_ONLY_E case */
  26645. keyDerSz = 0;
  26646. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  26647. AssertIntEQ(ret, LENGTH_ONLY_E);
  26648. AssertIntGT(keyDerSz, 0);
  26649. wc_FreeDecodedCert(&decoded);
  26650. #endif
  26651. printf(resultFmt, passed);
  26652. #endif /* !NO_RSA || HAVE_ECC */
  26653. return 0;
  26654. }
  26655. static int test_wc_CheckCertSigPubKey(void)
  26656. {
  26657. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26658. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  26659. int ret;
  26660. const char* ca_cert = "./certs/ca-cert.pem";
  26661. byte* cert_buf = NULL;
  26662. size_t cert_sz = 0;
  26663. byte* cert_der = NULL;
  26664. word32 cert_dersz = 0;
  26665. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  26666. word32 keyDerSz = (word32)sizeof(keyDer);
  26667. DecodedCert decoded;
  26668. printf(testingFmt, "wc_CheckCertSigPubKey()");
  26669. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  26670. if (ret == 0) {
  26671. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  26672. cert_der = (byte*)malloc(cert_dersz);
  26673. if (cert_der) {
  26674. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  26675. cert_der, (int)cert_dersz, CERT_TYPE);
  26676. AssertIntGE(ret, 0);
  26677. }
  26678. }
  26679. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  26680. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  26681. AssertIntEQ(ret, 0);
  26682. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26683. AssertIntEQ(ret, 0);
  26684. AssertIntGT(keyDerSz, 0);
  26685. /* Good test case. */
  26686. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  26687. RSAk);
  26688. AssertIntEQ(ret, 0);
  26689. /* No certificate. */
  26690. ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  26691. ECDSAk);
  26692. AssertIntEQ(ret, BAD_FUNC_ARG);
  26693. /* Bad cert size. */
  26694. ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  26695. RSAk);
  26696. AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  26697. /* No public key. */
  26698. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
  26699. RSAk);
  26700. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  26701. /* Bad public key size. */
  26702. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  26703. RSAk);
  26704. AssertIntEQ(ret, BAD_FUNC_ARG);
  26705. /* Wrong aglo. */
  26706. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  26707. ECDSAk);
  26708. AssertIntEQ(ret, ASN_PARSE_E);
  26709. wc_FreeDecodedCert(&decoded);
  26710. if (cert_der)
  26711. free(cert_der);
  26712. if (cert_buf)
  26713. free(cert_buf);
  26714. printf(resultFmt, passed);
  26715. #endif
  26716. return 0;
  26717. }
  26718. static int test_wolfSSL_certs(void)
  26719. {
  26720. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26721. !defined(NO_RSA)
  26722. X509* x509ext;
  26723. #ifdef OPENSSL_ALL
  26724. X509* x509;
  26725. WOLFSSL_X509_EXTENSION* ext;
  26726. ASN1_OBJECT* obj;
  26727. #endif
  26728. WOLFSSL* ssl;
  26729. WOLFSSL_CTX* ctx;
  26730. STACK_OF(ASN1_OBJECT)* sk;
  26731. ASN1_STRING* asn1_str;
  26732. AUTHORITY_KEYID* akey;
  26733. BASIC_CONSTRAINTS* bc;
  26734. int crit;
  26735. printf(testingFmt, "wolfSSL_certs()");
  26736. #ifndef NO_WOLFSSL_SERVER
  26737. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  26738. #else
  26739. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  26740. #endif
  26741. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  26742. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  26743. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  26744. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26745. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  26746. #endif
  26747. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  26748. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26749. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  26750. #endif
  26751. AssertNotNull(ssl = SSL_new(ctx));
  26752. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26753. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26754. #endif
  26755. #ifdef HAVE_PK_CALLBACKS
  26756. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  26757. #endif /* HAVE_PK_CALLBACKS */
  26758. /* create and use x509 */
  26759. #ifdef OPENSSL_ALL
  26760. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  26761. AssertNotNull(x509);
  26762. #endif
  26763. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  26764. AssertNotNull(x509ext);
  26765. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  26766. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26767. /* with loading in a new cert the check on private key should now fail */
  26768. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26769. #endif
  26770. #if defined(USE_CERT_BUFFERS_2048)
  26771. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  26772. (unsigned char*)server_cert_der_2048,
  26773. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  26774. #endif
  26775. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  26776. /************* Get Digest of Certificate ******************/
  26777. {
  26778. byte digest[64]; /* max digest size */
  26779. word32 digestSz;
  26780. XMEMSET(digest, 0, sizeof(digest));
  26781. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  26782. WOLFSSL_SUCCESS);
  26783. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  26784. WOLFSSL_SUCCESS);
  26785. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  26786. WOLFSSL_FAILURE);
  26787. }
  26788. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  26789. /* test and checkout X509 extensions */
  26790. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  26791. &crit, NULL);
  26792. AssertNotNull(bc);
  26793. AssertIntEQ(crit, 0);
  26794. #ifdef OPENSSL_ALL
  26795. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  26796. AssertNotNull(ext);
  26797. X509_EXTENSION_free(ext);
  26798. AssertNotNull(ext = X509_EXTENSION_new());
  26799. X509_EXTENSION_set_critical(ext, 1);
  26800. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  26801. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  26802. ASN1_OBJECT_free(obj);
  26803. X509_EXTENSION_free(ext);
  26804. AssertNotNull(ext = X509_EXTENSION_new());
  26805. X509_EXTENSION_set_critical(ext, 0);
  26806. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  26807. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  26808. NULL);
  26809. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  26810. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  26811. * and X509_get_ext_d2i has created new */
  26812. X509_EXTENSION_free(ext);
  26813. #endif
  26814. BASIC_CONSTRAINTS_free(bc);
  26815. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  26816. AssertNotNull(asn1_str);
  26817. AssertIntEQ(crit, 1);
  26818. AssertIntEQ(asn1_str->type, NID_key_usage);
  26819. #ifdef OPENSSL_ALL
  26820. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  26821. AssertNotNull(ext);
  26822. X509_EXTENSION_free(ext);
  26823. #endif
  26824. ASN1_STRING_free(asn1_str);
  26825. #ifdef OPENSSL_ALL
  26826. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  26827. &crit, NULL);
  26828. AssertNotNull(sk);
  26829. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  26830. AssertNotNull(ext);
  26831. X509_EXTENSION_free(ext);
  26832. sk_ASN1_OBJECT_pop_free(sk, NULL);
  26833. #else
  26834. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  26835. &crit, NULL);
  26836. AssertNull(sk);
  26837. #endif
  26838. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  26839. NID_authority_key_identifier, &crit, NULL);
  26840. AssertNotNull(akey);
  26841. #ifdef OPENSSL_ALL
  26842. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  26843. AssertNotNull(ext);
  26844. X509_EXTENSION_free(ext);
  26845. #endif
  26846. wolfSSL_AUTHORITY_KEYID_free(akey);
  26847. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  26848. NID_private_key_usage_period, &crit, NULL);
  26849. /* AssertNotNull(sk); NID not yet supported */
  26850. AssertIntEQ(crit, -1);
  26851. sk_ASN1_OBJECT_free(sk);
  26852. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  26853. &crit, NULL);
  26854. /* AssertNotNull(sk); no alt names set */
  26855. sk_GENERAL_NAME_free(sk);
  26856. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  26857. &crit, NULL);
  26858. /* AssertNotNull(sk); NID not yet supported */
  26859. AssertIntEQ(crit, -1);
  26860. sk_ASN1_OBJECT_free(sk);
  26861. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  26862. NULL);
  26863. /* AssertNotNull(sk); no auth info set */
  26864. sk_ASN1_OBJECT_free(sk);
  26865. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  26866. &crit, NULL);
  26867. /* AssertNotNull(sk); NID not yet supported */
  26868. AssertIntEQ(crit, -1);
  26869. sk_ASN1_OBJECT_free(sk);
  26870. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  26871. &crit, NULL);
  26872. /* AssertNotNull(sk); NID not yet supported */
  26873. AssertIntEQ(crit, -1);
  26874. sk_ASN1_OBJECT_free(sk);
  26875. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  26876. NID_certificate_policies, &crit, NULL);
  26877. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  26878. AssertNull(sk);
  26879. #else
  26880. /* AssertNotNull(sk); no cert policy set */
  26881. #endif
  26882. sk_ASN1_OBJECT_free(sk);
  26883. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  26884. &crit, NULL);
  26885. /* AssertNotNull(sk); NID not yet supported */
  26886. AssertIntEQ(crit, -1);
  26887. sk_ASN1_OBJECT_free(sk);
  26888. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  26889. &crit, NULL);
  26890. /* AssertNotNull(sk); NID not yet supported */
  26891. AssertIntEQ(crit, -1);
  26892. sk_ASN1_OBJECT_free(sk);
  26893. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  26894. &crit, NULL);
  26895. /* AssertNotNull(sk); NID not yet supported */
  26896. AssertIntEQ(crit, -1);
  26897. sk_ASN1_OBJECT_free(sk);
  26898. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  26899. NULL);
  26900. /* AssertNotNull(sk); NID not yet supported */
  26901. AssertIntEQ(crit, -1);
  26902. sk_ASN1_OBJECT_free(sk);
  26903. /* test invalid cases */
  26904. crit = 0;
  26905. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  26906. AssertNull(sk);
  26907. AssertIntEQ(crit, -1);
  26908. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  26909. NULL, NULL);
  26910. AssertNull(sk);
  26911. AssertIntEQ(SSL_get_hit(ssl), 0);
  26912. #ifdef OPENSSL_ALL
  26913. X509_free(x509);
  26914. #endif
  26915. X509_free(x509ext);
  26916. SSL_free(ssl);
  26917. SSL_CTX_free(ctx);
  26918. printf(resultFmt, passed);
  26919. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  26920. return 0;
  26921. }
  26922. static int test_wolfSSL_X509_check_private_key(void)
  26923. {
  26924. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  26925. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  26926. X509* x509;
  26927. EVP_PKEY* pkey = NULL;
  26928. const byte* key;
  26929. printf(testingFmt, "wolfSSL_X509_check_private_key()");
  26930. /* Check with correct key */
  26931. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  26932. SSL_FILETYPE_PEM)));
  26933. key = client_key_der_2048;
  26934. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26935. &key, (long)sizeof_client_key_der_2048));
  26936. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  26937. EVP_PKEY_free(pkey);
  26938. pkey = NULL;
  26939. /* Check with wrong key */
  26940. key = server_key_der_2048;
  26941. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26942. &key, (long)sizeof_server_key_der_2048));
  26943. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  26944. /* test for incorrect parameter */
  26945. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  26946. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  26947. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  26948. EVP_PKEY_free(pkey);
  26949. X509_free(x509);
  26950. printf(resultFmt, passed);
  26951. #endif
  26952. return 0;
  26953. }
  26954. static int test_wolfSSL_ASN1_TIME_print(void)
  26955. {
  26956. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  26957. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  26958. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048) && \
  26959. !defined(NO_BIO)
  26960. BIO* bio;
  26961. X509* x509;
  26962. const unsigned char* der = client_cert_der_2048;
  26963. ASN1_TIME* t;
  26964. unsigned char buf[25];
  26965. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  26966. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26967. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  26968. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  26969. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  26970. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  26971. AssertIntEQ(XMEMCMP(buf, "Feb 15 12:50:24 2022 GMT", sizeof(buf) - 1), 0);
  26972. /* create a bad time and test results */
  26973. AssertNotNull(t = X509_get_notAfter(x509));
  26974. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  26975. t->data[8] = 0;
  26976. t->data[3] = 0;
  26977. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  26978. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  26979. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  26980. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  26981. BIO_free(bio);
  26982. X509_free(x509);
  26983. printf(resultFmt, passed);
  26984. #endif
  26985. return 0;
  26986. }
  26987. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  26988. {
  26989. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  26990. BIO* bio;
  26991. ASN1_UTCTIME* utc = NULL;
  26992. unsigned char buf[25];
  26993. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  26994. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  26995. printf(testingFmt, "ASN1_UTCTIME_print()");
  26996. /* NULL parameter check */
  26997. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  26998. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  26999. BIO_free(bio);
  27000. /* Valid date */
  27001. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27002. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27003. DYNAMIC_TYPE_ASN1));
  27004. utc->type = ASN_UTC_TIME;
  27005. utc->length = ASN_UTC_TIME_SIZE;
  27006. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27007. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27008. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27009. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27010. XMEMSET(buf, 0, sizeof(buf));
  27011. BIO_free(bio);
  27012. /* Invalid format */
  27013. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27014. utc->type = ASN_UTC_TIME;
  27015. utc->length = ASN_UTC_TIME_SIZE;
  27016. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27017. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27018. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27019. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27020. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27021. BIO_free(bio);
  27022. printf(resultFmt, passed);
  27023. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27024. return 0;
  27025. }
  27026. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27027. {
  27028. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27029. ASN1_TIME* fromTime;
  27030. ASN1_TIME* toTime;
  27031. int daysDiff;
  27032. int secsDiff;
  27033. printf(testingFmt, "test_wolfSSL_ASN1_TIME_diff");
  27034. AssertNotNull((fromTime = ASN1_TIME_new()));
  27035. /* Feb 22, 2003, 21:15:15 */
  27036. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), WOLFSSL_SUCCESS);
  27037. AssertNotNull((toTime = ASN1_TIME_new()));
  27038. /* Dec 19, 2010, 18:10:11 */
  27039. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), WOLFSSL_SUCCESS);
  27040. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), WOLFSSL_SUCCESS);
  27041. /* Error conditions. */
  27042. AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime),
  27043. WOLFSSL_FAILURE);
  27044. AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime),
  27045. WOLFSSL_FAILURE);
  27046. /* If both times are NULL, difference is 0. */
  27047. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL),
  27048. WOLFSSL_SUCCESS);
  27049. AssertIntEQ(daysDiff, 0);
  27050. AssertIntEQ(secsDiff, 0);
  27051. /* If one time is NULL, it defaults to the current time. */
  27052. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime),
  27053. WOLFSSL_SUCCESS);
  27054. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL),
  27055. WOLFSSL_SUCCESS);
  27056. /* Normal operation. Both times non-NULL. */
  27057. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27058. WOLFSSL_SUCCESS);
  27059. AssertIntEQ(daysDiff, 2856);
  27060. AssertIntEQ(secsDiff, 75296);
  27061. /* Swapping the times should return negative values. */
  27062. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime),
  27063. WOLFSSL_SUCCESS);
  27064. AssertIntEQ(daysDiff, -2856);
  27065. AssertIntEQ(secsDiff, -75296);
  27066. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27067. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27068. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27069. /* Compare regression test: No seconds difference, just difference in days.
  27070. */
  27071. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27072. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27073. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27074. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27075. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27076. /* Edge case with Unix epoch. */
  27077. AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27078. AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27079. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27080. WOLFSSL_SUCCESS);
  27081. AssertIntEQ(daysDiff, 3652);
  27082. AssertIntEQ(secsDiff, 0);
  27083. /* Edge case with year > 2038 (year 2038 problem). */
  27084. AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27085. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27086. WOLFSSL_SUCCESS);
  27087. AssertIntEQ(daysDiff, 2932896);
  27088. AssertIntEQ(secsDiff, 86399);
  27089. ASN1_TIME_free(fromTime);
  27090. ASN1_TIME_free(toTime);
  27091. printf(resultFmt, passed);
  27092. #endif
  27093. return 0;
  27094. }
  27095. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27096. {
  27097. #if defined(OPENSSL_EXTRA)
  27098. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  27099. unsigned char nullstr[32];
  27100. printf(testingFmt, "test_wolfSSL_ASN1_GENERALIZEDTIME_free");
  27101. XMEMSET(nullstr, 0, 32);
  27102. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27103. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  27104. DYNAMIC_TYPE_TMP_BUFFER);
  27105. if (asn1_gtime) {
  27106. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  27107. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  27108. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  27109. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27110. }
  27111. printf(resultFmt, passed);
  27112. #endif /* OPENSSL_EXTRA */
  27113. return 0;
  27114. }
  27115. static int test_wolfSSL_private_keys(void)
  27116. {
  27117. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27118. !defined(NO_FILESYSTEM)
  27119. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  27120. WOLFSSL* ssl;
  27121. WOLFSSL_CTX* ctx;
  27122. EVP_PKEY* pkey = NULL;
  27123. printf(testingFmt, "wolfSSL_private_keys()");
  27124. OpenSSL_add_all_digests();
  27125. OpenSSL_add_all_algorithms();
  27126. #ifndef NO_RSA
  27127. #ifndef NO_WOLFSSL_SERVER
  27128. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27129. #else
  27130. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27131. #endif
  27132. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  27133. /* Have to load a cert before you can check the private key against that
  27134. * certificates public key! */
  27135. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27136. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  27137. #endif
  27138. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  27139. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27140. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27141. #endif
  27142. AssertNotNull(ssl = SSL_new(ctx));
  27143. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27144. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27145. #endif
  27146. #ifdef USE_CERT_BUFFERS_2048
  27147. {
  27148. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27149. unsigned char buf[FOURK_BUF];
  27150. word32 bufSz;
  27151. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  27152. (unsigned char*)client_key_der_2048,
  27153. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27154. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27155. /* Should mismatch now that a different private key loaded */
  27156. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27157. #endif
  27158. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  27159. (unsigned char*)server_key,
  27160. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27161. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27162. /* After loading back in DER format of original key, should match */
  27163. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27164. #endif
  27165. /* test loading private key to the WOLFSSL_CTX */
  27166. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27167. (unsigned char*)client_key_der_2048,
  27168. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27169. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27170. /* Should mismatch now that a different private key loaded */
  27171. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27172. #endif
  27173. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27174. (unsigned char*)server_key,
  27175. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27176. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27177. /* After loading back in DER format of original key, should match */
  27178. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27179. #endif
  27180. /* pkey not set yet, expecting to fail */
  27181. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  27182. /* set PKEY and test again */
  27183. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27184. &server_key, (long)sizeof_server_key_der_2048));
  27185. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27186. /* reuse PKEY structure and test
  27187. * this should be checked with a memory management sanity checker */
  27188. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  27189. server_key = (const unsigned char*)server_key_der_2048;
  27190. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27191. &server_key, (long)sizeof_server_key_der_2048));
  27192. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27193. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  27194. bufSz = FOURK_BUF;
  27195. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  27196. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  27197. RSAk, NULL, 0)), 0);
  27198. server_key = (const unsigned char*)buf;
  27199. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  27200. (long)bufSz));
  27201. }
  27202. #endif
  27203. EVP_PKEY_free(pkey);
  27204. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  27205. SSL_CTX_free(ctx);
  27206. #endif /* end of RSA private key match tests */
  27207. #ifdef HAVE_ECC
  27208. #ifndef NO_WOLFSSL_SERVER
  27209. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27210. #else
  27211. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27212. #endif
  27213. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  27214. WOLFSSL_FILETYPE_PEM));
  27215. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  27216. WOLFSSL_FILETYPE_PEM));
  27217. AssertNotNull(ssl = SSL_new(ctx));
  27218. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27219. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27220. #endif
  27221. SSL_free(ssl);
  27222. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  27223. WOLFSSL_FILETYPE_PEM));
  27224. AssertNotNull(ssl = SSL_new(ctx));
  27225. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  27226. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27227. #endif
  27228. SSL_free(ssl);
  27229. SSL_CTX_free(ctx);
  27230. #endif /* end of ECC private key match tests */
  27231. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  27232. #ifndef NO_WOLFSSL_SERVER
  27233. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27234. #else
  27235. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27236. #endif
  27237. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  27238. WOLFSSL_FILETYPE_PEM));
  27239. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  27240. WOLFSSL_FILETYPE_PEM));
  27241. AssertNotNull(ssl = SSL_new(ctx));
  27242. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27243. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27244. #endif
  27245. SSL_free(ssl);
  27246. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  27247. WOLFSSL_FILETYPE_PEM));
  27248. AssertNotNull(ssl = SSL_new(ctx));
  27249. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27250. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27251. #endif
  27252. SSL_free(ssl);
  27253. SSL_CTX_free(ctx);
  27254. #endif /* end of Ed25519 private key match tests */
  27255. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  27256. #ifndef NO_WOLFSSL_SERVER
  27257. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27258. #else
  27259. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27260. #endif
  27261. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  27262. WOLFSSL_FILETYPE_PEM));
  27263. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  27264. WOLFSSL_FILETYPE_PEM));
  27265. AssertNotNull(ssl = SSL_new(ctx));
  27266. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27267. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27268. #endif
  27269. SSL_free(ssl);
  27270. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  27271. WOLFSSL_FILETYPE_PEM));
  27272. AssertNotNull(ssl = SSL_new(ctx));
  27273. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27274. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27275. #endif
  27276. SSL_free(ssl);
  27277. SSL_CTX_free(ctx);
  27278. #endif /* end of Ed448 private key match tests */
  27279. EVP_cleanup();
  27280. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  27281. CONF_modules_free();
  27282. ENGINE_cleanup();
  27283. CONF_modules_unload();
  27284. (void)ssl;
  27285. (void)ctx;
  27286. (void)pkey;
  27287. printf(resultFmt, passed);
  27288. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  27289. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27290. return 0;
  27291. }
  27292. static int test_wolfSSL_PEM_read_PrivateKey(void)
  27293. {
  27294. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  27295. && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  27296. XFILE file;
  27297. const char* fname = "./certs/server-key.pem";
  27298. EVP_PKEY* pkey;
  27299. RSA* rsa;
  27300. WOLFSSL_EVP_PKEY_CTX* ctx;
  27301. unsigned char* sig;
  27302. size_t sigLen;
  27303. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  27304. size_t tbsLen = sizeof(tbs);
  27305. printf(testingFmt, "test_wolfSSL_PEM_read_PrivateKey()");
  27306. /* Check error case. */
  27307. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  27308. /* Read in an RSA key. */
  27309. file = XFOPEN(fname, "rb");
  27310. AssertTrue(file != XBADFILE);
  27311. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  27312. XFCLOSE(file);
  27313. /* Make sure the key is usable by signing some data with it. */
  27314. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  27315. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  27316. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  27317. DYNAMIC_TYPE_TMP_BUFFER));
  27318. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  27319. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  27320. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  27321. WOLFSSL_SUCCESS);
  27322. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27323. EVP_PKEY_CTX_free(ctx);
  27324. EVP_PKEY_free(pkey);
  27325. printf(resultFmt, passed);
  27326. #endif
  27327. return 0;
  27328. }
  27329. static int test_wolfSSL_PEM_PrivateKey(void)
  27330. {
  27331. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27332. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  27333. #ifndef NO_BIO
  27334. BIO* bio = NULL;
  27335. #endif
  27336. EVP_PKEY* pkey = NULL;
  27337. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27338. #ifndef NO_BIO
  27339. /* test creating new EVP_PKEY with bad arg */
  27340. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  27341. /* test loading RSA key using BIO */
  27342. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  27343. {
  27344. XFILE file;
  27345. const char* fname = "./certs/server-key.pem";
  27346. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  27347. size_t sz;
  27348. byte* buf;
  27349. EVP_PKEY* pkey2;
  27350. EVP_PKEY* pkey3;
  27351. RSA* rsa_key = NULL;
  27352. file = XFOPEN(fname, "rb");
  27353. AssertTrue((file != XBADFILE));
  27354. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27355. sz = XFTELL(file);
  27356. XREWIND(file);
  27357. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27358. if (buf) {
  27359. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27360. }
  27361. XFCLOSE(file);
  27362. /* Test using BIO new mem and loading PEM private key */
  27363. bio = BIO_new_mem_buf(buf, (int)sz);
  27364. AssertNotNull(bio);
  27365. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27366. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27367. BIO_free(bio);
  27368. bio = NULL;
  27369. AssertNotNull(pkey2 = EVP_PKEY_new());
  27370. pkey2->type = EVP_PKEY_RSA;
  27371. /* Test parameter copy */
  27372. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  27373. EVP_PKEY_free(pkey2);
  27374. EVP_PKEY_free(pkey);
  27375. pkey = NULL;
  27376. /* Qt unit test case : rsa pkcs8 key */
  27377. file = XFOPEN(fname_rsa_p8, "rb");
  27378. AssertTrue((file != XBADFILE));
  27379. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27380. sz = XFTELL(file);
  27381. XREWIND(file);
  27382. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27383. if (buf)
  27384. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27385. XFCLOSE(file);
  27386. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27387. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27388. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27389. BIO_free(bio);
  27390. bio = NULL;
  27391. AssertNotNull(pkey3 = EVP_PKEY_new());
  27392. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  27393. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  27394. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27395. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27396. #else
  27397. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27398. #endif
  27399. RSA_free(rsa_key);
  27400. EVP_PKEY_free(pkey3);
  27401. EVP_PKEY_free(pkey);
  27402. pkey = NULL;
  27403. }
  27404. #endif
  27405. /* test loading ECC key using BIO */
  27406. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27407. {
  27408. XFILE file;
  27409. const char* fname = "./certs/ecc-key.pem";
  27410. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  27411. size_t sz;
  27412. byte* buf;
  27413. EVP_PKEY* pkey2;
  27414. EVP_PKEY* pkey3;
  27415. EC_KEY* ec_key;
  27416. int nid = 0;
  27417. file = XFOPEN(fname, "rb");
  27418. AssertTrue((file != XBADFILE));
  27419. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27420. sz = XFTELL(file);
  27421. XREWIND(file);
  27422. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27423. if (buf)
  27424. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27425. XFCLOSE(file);
  27426. /* Test using BIO new mem and loading PEM private key */
  27427. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27428. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27429. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27430. BIO_free(bio);
  27431. bio = NULL;
  27432. AssertNotNull(pkey2 = EVP_PKEY_new());
  27433. AssertNotNull(pkey3 = EVP_PKEY_new());
  27434. pkey2->type = EVP_PKEY_EC;
  27435. /* Test parameter copy */
  27436. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  27437. /* Qt unit test case 1*/
  27438. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27439. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  27440. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27441. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27442. #else
  27443. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27444. #endif
  27445. /* Test default digest */
  27446. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  27447. AssertIntEQ(nid, NID_sha256);
  27448. EC_KEY_free(ec_key);
  27449. EVP_PKEY_free(pkey3);
  27450. EVP_PKEY_free(pkey2);
  27451. EVP_PKEY_free(pkey);
  27452. pkey = NULL;
  27453. /* Qt unit test case ec pkcs8 key */
  27454. file = XFOPEN(fname_ecc_p8, "rb");
  27455. AssertTrue((file != XBADFILE));
  27456. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27457. sz = XFTELL(file);
  27458. XREWIND(file);
  27459. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27460. if (buf)
  27461. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27462. XFCLOSE(file);
  27463. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27464. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27465. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27466. BIO_free(bio);
  27467. bio = NULL;
  27468. AssertNotNull(pkey3 = EVP_PKEY_new());
  27469. /* Qt unit test case */
  27470. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27471. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  27472. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27473. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27474. #else
  27475. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27476. #endif
  27477. EC_KEY_free(ec_key);
  27478. EVP_PKEY_free(pkey3);
  27479. EVP_PKEY_free(pkey);
  27480. pkey = NULL;
  27481. }
  27482. #endif
  27483. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  27484. defined(WOLFSSL_CERT_GEN))
  27485. {
  27486. #define BIO_PEM_TEST_CHAR 'a'
  27487. EVP_PKEY* pkey2 = NULL;
  27488. unsigned char extra[10];
  27489. int i;
  27490. BIO* pub_bio = NULL;
  27491. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  27492. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  27493. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27494. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  27495. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27496. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  27497. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  27498. &server_key, (long)sizeof_server_key_der_2048));
  27499. AssertNull(pkey);
  27500. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27501. &server_key, (long)sizeof_server_key_der_2048));
  27502. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  27503. WOLFSSL_SUCCESS);
  27504. AssertIntGT(BIO_pending(bio), 0);
  27505. AssertIntEQ(BIO_pending(bio), 1679);
  27506. /* Check if the pubkey API writes only the public key */
  27507. #ifdef WOLFSSL_KEY_GEN
  27508. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  27509. AssertIntGT(BIO_pending(pub_bio), 0);
  27510. /* Previously both the private key and the pubkey calls would write
  27511. * out the private key and the PEM header was the only difference.
  27512. * The public PEM should be significantly shorter than the
  27513. * private key versison. */
  27514. AssertIntEQ(BIO_pending(pub_bio), 451);
  27515. #endif
  27516. /* test creating new EVP_PKEY with good args */
  27517. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27518. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  27519. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  27520. /* test of reuse of EVP_PKEY */
  27521. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27522. AssertIntEQ(BIO_pending(bio), 0);
  27523. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  27524. SSL_SUCCESS);
  27525. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  27526. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27527. AssertNotNull(pkey);
  27528. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  27529. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  27530. }
  27531. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  27532. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  27533. for (i = 0; i < 10; i++) {
  27534. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  27535. }
  27536. BIO_free(pub_bio);
  27537. BIO_free(bio);
  27538. bio = NULL;
  27539. EVP_PKEY_free(pkey);
  27540. pkey = NULL;
  27541. EVP_PKEY_free(pkey2);
  27542. }
  27543. #endif
  27544. /* key is DES encrypted */
  27545. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  27546. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  27547. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  27548. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  27549. {
  27550. XFILE f;
  27551. wc_pem_password_cb* passwd_cb;
  27552. void* passwd_cb_userdata;
  27553. SSL_CTX* ctx;
  27554. char passwd[] = "bad password";
  27555. #ifndef WOLFSSL_NO_TLS12
  27556. #ifndef NO_WOLFSSL_SERVER
  27557. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27558. #else
  27559. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27560. #endif
  27561. #else
  27562. #ifndef NO_WOLFSSL_SERVER
  27563. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27564. #else
  27565. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27566. #endif
  27567. #endif
  27568. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27569. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  27570. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  27571. AssertNull(passwd_cb_userdata =
  27572. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  27573. /* fail case with password call back */
  27574. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  27575. (void*)passwd));
  27576. BIO_free(bio);
  27577. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27578. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27579. (void*)passwd));
  27580. BIO_free(bio);
  27581. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  27582. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  27583. /* use callback that works */
  27584. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27585. (void*)"yassl123"));
  27586. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27587. EVP_PKEY_free(pkey);
  27588. pkey = NULL;
  27589. BIO_free(bio);
  27590. bio = NULL;
  27591. SSL_CTX_free(ctx);
  27592. }
  27593. #endif /* !defined(NO_DES3) */
  27594. #endif /* !NO_BIO */
  27595. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27596. {
  27597. unsigned char buf[2048];
  27598. size_t bytes;
  27599. XFILE f;
  27600. SSL_CTX* ctx;
  27601. #ifndef WOLFSSL_NO_TLS12
  27602. #ifndef NO_WOLFSSL_SERVER
  27603. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27604. #else
  27605. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27606. #endif
  27607. #else
  27608. #ifndef NO_WOLFSSL_SERVER
  27609. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27610. #else
  27611. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27612. #endif
  27613. #endif
  27614. f = XFOPEN("./certs/ecc-key.der", "rb");
  27615. AssertTrue((f != XBADFILE));
  27616. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  27617. XFCLOSE(f);
  27618. server_key = buf;
  27619. pkey = NULL;
  27620. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  27621. AssertNull(pkey);
  27622. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  27623. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27624. EVP_PKEY_free(pkey);
  27625. pkey = NULL;
  27626. SSL_CTX_free(ctx);
  27627. }
  27628. #endif
  27629. printf(resultFmt, passed);
  27630. #ifndef NO_BIO
  27631. (void)bio;
  27632. #endif
  27633. (void)pkey;
  27634. (void)server_key;
  27635. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  27636. return 0;
  27637. }
  27638. #ifndef NO_BIO
  27639. static int test_wolfSSL_PEM_bio_RSAKey(void)
  27640. {
  27641. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27642. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  27643. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  27644. RSA* rsa = NULL;
  27645. BIO* bio = NULL;
  27646. printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
  27647. /* PrivateKey */
  27648. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  27649. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  27650. AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  27651. AssertNotNull(rsa);
  27652. AssertIntEQ(RSA_size(rsa), 256);
  27653. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  27654. NULL), WOLFSSL_FAILURE);
  27655. BIO_free(bio);
  27656. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27657. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  27658. NULL), WOLFSSL_SUCCESS);
  27659. BIO_free(bio);
  27660. RSA_free(rsa);
  27661. /* PUBKEY */
  27662. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  27663. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  27664. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27665. AssertIntEQ(RSA_size(rsa), 256);
  27666. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27667. BIO_free(bio);
  27668. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27669. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  27670. BIO_free(bio);
  27671. /* Same test as above, but with a file pointer rather than a BIO. */
  27672. AssertIntEQ(PEM_write_RSAPublicKey(NULL, rsa), WOLFSSL_FAILURE);
  27673. AssertIntEQ(PEM_write_RSAPublicKey(stdout, NULL), WOLFSSL_FAILURE);
  27674. AssertIntEQ(PEM_write_RSAPublicKey(stdout, rsa), WOLFSSL_SUCCESS);
  27675. RSA_free(rsa);
  27676. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  27677. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  27678. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27679. BIO_free(bio);
  27680. RSA_free(rsa);
  27681. #ifdef HAVE_ECC
  27682. /* ensure that non-rsa keys do not work */
  27683. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  27684. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27685. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27686. BIO_free(bio);
  27687. RSA_free(rsa);
  27688. #endif /* HAVE_ECC */
  27689. printf(resultFmt, passed);
  27690. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27691. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  27692. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  27693. return 0;
  27694. }
  27695. static int test_wolfSSL_PEM_RSAPrivateKey(void)
  27696. {
  27697. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27698. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  27699. RSA* rsa = NULL;
  27700. RSA* rsa_dup = NULL;
  27701. BIO* bio = NULL;
  27702. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  27703. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  27704. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27705. AssertIntEQ(RSA_size(rsa), 256);
  27706. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  27707. AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
  27708. /* Test duplicating empty key. */
  27709. rsa_dup = RSA_new();
  27710. AssertNull(RSAPublicKey_dup(rsa_dup));
  27711. RSA_free(rsa_dup);
  27712. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  27713. AssertPtrNE(rsa_dup, rsa);
  27714. #endif
  27715. /* test if valgrind complains about unreleased memory */
  27716. RSA_up_ref(rsa);
  27717. RSA_free(rsa);
  27718. BIO_free(bio);
  27719. RSA_free(rsa);
  27720. RSA_free(rsa_dup);
  27721. #ifdef HAVE_ECC
  27722. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  27723. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27724. BIO_free(bio);
  27725. #endif /* HAVE_ECC */
  27726. printf(resultFmt, passed);
  27727. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27728. return 0;
  27729. }
  27730. static int test_wolfSSL_PEM_bio_DSAKey(void)
  27731. {
  27732. #ifndef HAVE_SELFTEST
  27733. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  27734. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  27735. DSA* dsa = NULL;
  27736. BIO* bio = NULL;
  27737. printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
  27738. /* PrivateKey */
  27739. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  27740. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  27741. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  27742. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  27743. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  27744. WOLFSSL_FAILURE);
  27745. BIO_free(bio);
  27746. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27747. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  27748. WOLFSSL_SUCCESS);
  27749. BIO_free(bio);
  27750. DSA_free(dsa);
  27751. /* PUBKEY */
  27752. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  27753. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  27754. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  27755. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  27756. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27757. BIO_free(bio);
  27758. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27759. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  27760. BIO_free(bio);
  27761. DSA_free(dsa);
  27762. #ifdef HAVE_ECC
  27763. /* ensure that non-dsa keys do not work */
  27764. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  27765. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  27766. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  27767. BIO_free(bio);
  27768. DSA_free(dsa);
  27769. #endif /* HAVE_ECC */
  27770. printf(resultFmt, passed);
  27771. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  27772. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  27773. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  27774. #endif /* HAVE_SELFTEST */
  27775. return 0;
  27776. }
  27777. static int test_wolfSSL_PEM_bio_ECKey(void)
  27778. {
  27779. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27780. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  27781. EC_KEY* ec = NULL;
  27782. BIO* bio = NULL;
  27783. printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
  27784. /* PrivateKey */
  27785. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  27786. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  27787. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  27788. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  27789. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  27790. NULL),WOLFSSL_FAILURE);
  27791. BIO_free(bio);
  27792. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27793. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  27794. NULL), WOLFSSL_SUCCESS);
  27795. BIO_free(bio);
  27796. EC_KEY_free(ec);
  27797. /* PUBKEY */
  27798. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  27799. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  27800. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  27801. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  27802. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27803. BIO_free(bio);
  27804. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27805. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  27806. BIO_free(bio);
  27807. /* Same test as above, but with a file pointer rather than a BIO. */
  27808. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  27809. AssertIntEQ(PEM_write_EC_PUBKEY(stdout, NULL), WOLFSSL_FAILURE);
  27810. AssertIntEQ(PEM_write_EC_PUBKEY(stdout, ec), WOLFSSL_SUCCESS);
  27811. EC_KEY_free(ec);
  27812. #ifndef NO_RSA
  27813. /* ensure that non-ec keys do not work */
  27814. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  27815. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  27816. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  27817. BIO_free(bio);
  27818. EC_KEY_free(ec);
  27819. #endif /* HAVE_ECC */
  27820. printf(resultFmt, passed);
  27821. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27822. return 0;
  27823. }
  27824. static int test_wolfSSL_PEM_PUBKEY(void)
  27825. {
  27826. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  27827. BIO* bio = NULL;
  27828. EVP_PKEY* pkey = NULL;
  27829. /* test creating new EVP_PKEY with bad arg */
  27830. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  27831. /* test loading ECC key using BIO */
  27832. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27833. {
  27834. XFILE file;
  27835. const char* fname = "./certs/ecc-client-keyPub.pem";
  27836. size_t sz;
  27837. byte* buf;
  27838. EVP_PKEY* pkey2;
  27839. EC_KEY* ec_key;
  27840. file = XFOPEN(fname, "rb");
  27841. AssertTrue((file != XBADFILE));
  27842. AssertIntGE(XFSEEK(file, 0, XSEEK_END), 0);
  27843. sz = XFTELL(file);
  27844. XREWIND(file);
  27845. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27846. if (buf)
  27847. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27848. XFCLOSE(file);
  27849. /* Test using BIO new mem and loading PEM private key */
  27850. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27851. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  27852. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27853. BIO_free(bio);
  27854. bio = NULL;
  27855. /* Qt unit test case*/
  27856. AssertNotNull(pkey2 = EVP_PKEY_new());
  27857. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27858. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  27859. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27860. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  27861. #else
  27862. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  27863. #endif
  27864. EC_KEY_free(ec_key);
  27865. EVP_PKEY_free(pkey2);
  27866. EVP_PKEY_free(pkey);
  27867. pkey = NULL;
  27868. }
  27869. #endif
  27870. (void)bio;
  27871. (void)pkey;
  27872. #endif
  27873. return 0;
  27874. }
  27875. #endif /* !NO_BIO */
  27876. static int test_DSA_do_sign_verify(void)
  27877. {
  27878. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  27879. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  27880. !defined(NO_DSA)
  27881. unsigned char digest[WC_SHA_DIGEST_SIZE];
  27882. DSA_SIG* sig;
  27883. DSA* dsa;
  27884. word32 bytes;
  27885. byte sigBin[DSA_SIG_SIZE];
  27886. int dsacheck;
  27887. #ifdef USE_CERT_BUFFERS_1024
  27888. byte tmp[ONEK_BUF];
  27889. XMEMSET(tmp, 0, sizeof(tmp));
  27890. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  27891. bytes = sizeof_dsa_key_der_1024;
  27892. #elif defined(USE_CERT_BUFFERS_2048)
  27893. byte tmp[TWOK_BUF];
  27894. XMEMSET(tmp, 0, sizeof(tmp));
  27895. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  27896. bytes = sizeof_dsa_key_der_2048;
  27897. #else
  27898. byte tmp[TWOK_BUF];
  27899. XMEMSET(tmp, 0, sizeof(tmp));
  27900. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  27901. if (fp == XBADFILE) {
  27902. return WOLFSSL_BAD_FILE;
  27903. }
  27904. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  27905. XFCLOSE(fp);
  27906. #endif /* END USE_CERT_BUFFERS_1024 */
  27907. printf(testingFmt, "DSA_do_sign_verify()");
  27908. XMEMSET(digest, 202, sizeof(digest));
  27909. AssertNotNull(dsa = DSA_new());
  27910. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  27911. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  27912. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  27913. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  27914. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  27915. DSA_SIG_free(sig);
  27916. DSA_free(dsa);
  27917. #endif
  27918. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  27919. return 0;
  27920. }
  27921. static int test_wolfSSL_tmp_dh(void)
  27922. {
  27923. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27924. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  27925. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  27926. byte buff[6000];
  27927. char file[] = "./certs/dsaparams.pem";
  27928. XFILE f;
  27929. int bytes;
  27930. DSA* dsa;
  27931. DH* dh;
  27932. #if defined(WOLFSSL_DH_EXTRA) && \
  27933. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  27934. DH* dh2;
  27935. #endif
  27936. BIO* bio;
  27937. SSL* ssl;
  27938. SSL_CTX* ctx;
  27939. printf(testingFmt, "wolfSSL_tmp_dh()");
  27940. #ifndef NO_WOLFSSL_SERVER
  27941. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27942. #else
  27943. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27944. #endif
  27945. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  27946. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  27947. AssertNotNull(ssl = SSL_new(ctx));
  27948. f = XFOPEN(file, "rb");
  27949. AssertTrue((f != XBADFILE));
  27950. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  27951. XFCLOSE(f);
  27952. bio = BIO_new_mem_buf((void*)buff, bytes);
  27953. AssertNotNull(bio);
  27954. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  27955. AssertNotNull(dsa);
  27956. dh = wolfSSL_DSA_dup_DH(dsa);
  27957. AssertNotNull(dh);
  27958. #if defined(WOLFSSL_DH_EXTRA) && \
  27959. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  27960. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  27961. #endif
  27962. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  27963. #ifndef NO_WOLFSSL_SERVER
  27964. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  27965. #else
  27966. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  27967. #endif
  27968. BIO_free(bio);
  27969. DSA_free(dsa);
  27970. DH_free(dh);
  27971. #if defined(WOLFSSL_DH_EXTRA) && \
  27972. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  27973. DH_free(dh2);
  27974. #endif
  27975. SSL_free(ssl);
  27976. SSL_CTX_free(ctx);
  27977. printf(resultFmt, passed);
  27978. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  27979. #endif
  27980. return 0;
  27981. }
  27982. static int test_wolfSSL_ctrl(void)
  27983. {
  27984. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  27985. byte buff[6000];
  27986. BIO* bio;
  27987. int bytes;
  27988. BUF_MEM* ptr = NULL;
  27989. printf(testingFmt, "wolfSSL_crtl()");
  27990. bytes = sizeof(buff);
  27991. bio = BIO_new_mem_buf((void*)buff, bytes);
  27992. AssertNotNull(bio);
  27993. AssertNotNull(BIO_s_socket());
  27994. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  27995. /* needs tested after stubs filled out @TODO
  27996. SSL_ctrl
  27997. SSL_CTX_ctrl
  27998. */
  27999. BIO_free(bio);
  28000. printf(resultFmt, passed);
  28001. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  28002. return 0;
  28003. }
  28004. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  28005. {
  28006. #ifdef OPENSSL_EXTRA
  28007. static const unsigned char pw[] = "password";
  28008. static const int pwSz = sizeof(pw) - 1;
  28009. size_t checkPwSz = 0;
  28010. const unsigned char* checkPw = NULL;
  28011. WOLFSSL_EVP_PKEY* key = NULL;
  28012. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  28013. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  28014. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  28015. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  28016. if (key) {
  28017. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  28018. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  28019. AssertIntEQ(key->pkey_sz, pwSz);
  28020. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  28021. }
  28022. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  28023. AssertIntEQ((int)checkPwSz, pwSz);
  28024. if (checkPw) {
  28025. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  28026. }
  28027. wolfSSL_EVP_PKEY_free(key);
  28028. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  28029. if (key) {
  28030. AssertIntEQ(key->pkey_sz, 0);
  28031. }
  28032. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28033. (void)checkPw;
  28034. AssertIntEQ((int)checkPwSz, 0);
  28035. wolfSSL_EVP_PKEY_free(key);
  28036. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  28037. if (key) {
  28038. AssertIntEQ(key->pkey_sz, 0);
  28039. }
  28040. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28041. (void)checkPw;
  28042. AssertIntEQ((int)checkPwSz, 0);
  28043. wolfSSL_EVP_PKEY_free(key);
  28044. printf(resultFmt, passed);
  28045. #endif /* OPENSSL_EXTRA */
  28046. return 0;
  28047. }
  28048. static int test_wolfSSL_EVP_Digest(void)
  28049. {
  28050. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  28051. const char* in = "abc";
  28052. int inLen = (int)XSTRLEN(in);
  28053. byte out[WC_SHA256_DIGEST_SIZE];
  28054. unsigned int outLen;
  28055. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  28056. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  28057. "\x15\xAD";
  28058. printf(testingFmt, "wolfSSL_EVP_Digest()");
  28059. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  28060. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  28061. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  28062. printf(resultFmt, passed);
  28063. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  28064. return 0;
  28065. }
  28066. static int test_wolfSSL_EVP_Digest_all(void)
  28067. {
  28068. #ifdef OPENSSL_EXTRA
  28069. const char* digests[] = {
  28070. #ifndef NO_MD5
  28071. "MD5",
  28072. #endif
  28073. #ifndef NO_SHA
  28074. "SHA",
  28075. #endif
  28076. #ifdef WOLFSSL_SHA224
  28077. "SHA224",
  28078. #endif
  28079. #ifndef NO_SHA256
  28080. "SHA256",
  28081. #endif
  28082. #ifdef WOLFSSL_SHA384
  28083. "SHA384",
  28084. #endif
  28085. #ifdef WOLFSSL_SHA512
  28086. "SHA512",
  28087. #endif
  28088. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  28089. "SHA512_224",
  28090. #endif
  28091. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  28092. "SHA512_256",
  28093. #endif
  28094. #ifdef WOLFSSL_SHA3
  28095. #ifndef WOLFSSL_NOSHA3_224
  28096. "SHA3_224",
  28097. #endif
  28098. #ifndef WOLFSSL_NOSHA3_256
  28099. "SHA3_256",
  28100. #endif
  28101. "SHA3_384",
  28102. #ifndef WOLFSSL_NOSHA3_512
  28103. "SHA3_512",
  28104. #endif
  28105. #endif /* WOLFSSL_SHA3 */
  28106. NULL
  28107. };
  28108. const char** d;
  28109. const unsigned char in[] = "abc";
  28110. int inLen = XSTR_SIZEOF(in);
  28111. byte out[WC_MAX_DIGEST_SIZE];
  28112. unsigned int outLen;
  28113. printf(testingFmt, "wolfSSL_EVP_Digest_all");
  28114. for (d = digests; *d != NULL; d++) {
  28115. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  28116. AssertIntGT(outLen, 0);
  28117. AssertIntEQ(EVP_MD_size(*d), outLen);
  28118. }
  28119. printf(resultFmt, passed);
  28120. #endif
  28121. return 0;
  28122. }
  28123. static int test_wolfSSL_EVP_MD_size(void)
  28124. {
  28125. #ifdef OPENSSL_EXTRA
  28126. WOLFSSL_EVP_MD_CTX mdCtx;
  28127. printf(testingFmt, "wolfSSL_EVP_MD_size()");
  28128. #ifdef WOLFSSL_SHA3
  28129. #ifndef WOLFSSL_NOSHA3_224
  28130. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28131. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  28132. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  28133. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  28134. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28135. #endif
  28136. #ifndef WOLFSSL_NOSHA3_256
  28137. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28138. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  28139. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  28140. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  28141. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28142. #endif
  28143. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28144. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  28145. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  28146. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  28147. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28148. #ifndef WOLFSSL_NOSHA3_512
  28149. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28150. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  28151. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  28152. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  28153. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28154. #endif
  28155. #endif /* WOLFSSL_SHA3 */
  28156. #ifndef NO_SHA256
  28157. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28158. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  28159. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  28160. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  28161. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  28162. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  28163. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28164. #endif
  28165. #ifndef NO_MD5
  28166. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28167. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  28168. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  28169. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  28170. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  28171. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  28172. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28173. #endif
  28174. #ifdef WOLFSSL_SHA224
  28175. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28176. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  28177. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  28178. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  28179. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  28180. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  28181. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28182. #endif
  28183. #ifdef WOLFSSL_SHA384
  28184. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28185. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  28186. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  28187. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  28188. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  28189. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  28190. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28191. #endif
  28192. #ifdef WOLFSSL_SHA512
  28193. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28194. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  28195. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  28196. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  28197. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  28198. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  28199. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28200. #endif
  28201. #ifndef NO_SHA
  28202. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28203. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  28204. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  28205. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  28206. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  28207. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  28208. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28209. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28210. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  28211. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  28212. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  28213. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  28214. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  28215. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28216. #endif
  28217. /* error case */
  28218. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28219. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  28220. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  28221. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  28222. /* Cleanup is valid on uninit'ed struct */
  28223. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28224. printf(resultFmt, passed);
  28225. #endif /* OPENSSL_EXTRA */
  28226. return 0;
  28227. }
  28228. static int test_wolfSSL_EVP_MD_pkey_type(void)
  28229. {
  28230. #ifdef OPENSSL_EXTRA
  28231. const WOLFSSL_EVP_MD* md;
  28232. printf(testingFmt, "test_wolfSSL_EVP_MD_pkey_type()");
  28233. #ifndef NO_MD5
  28234. AssertNotNull(md = EVP_md5());
  28235. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  28236. #endif
  28237. #ifndef NO_SHA
  28238. AssertNotNull(md = EVP_sha1());
  28239. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  28240. #endif
  28241. #ifdef WOLFSSL_SHA224
  28242. AssertNotNull(md = EVP_sha224());
  28243. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  28244. #endif
  28245. AssertNotNull(md = EVP_sha256());
  28246. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  28247. #ifdef WOLFSSL_SHA384
  28248. AssertNotNull(md = EVP_sha384());
  28249. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  28250. #endif
  28251. #ifdef WOLFSSL_SHA512
  28252. AssertNotNull(md = EVP_sha512());
  28253. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  28254. #endif
  28255. printf(resultFmt, passed);
  28256. #endif
  28257. return 0;
  28258. }
  28259. #ifdef OPENSSL_EXTRA
  28260. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  28261. size_t testKeySz, const char* testData, size_t testDataSz,
  28262. const byte* testResult, size_t testResultSz)
  28263. {
  28264. unsigned char check[WC_MAX_DIGEST_SIZE];
  28265. size_t checkSz = -1;
  28266. WOLFSSL_EVP_PKEY* key;
  28267. WOLFSSL_EVP_MD_CTX mdCtx;
  28268. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  28269. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  28270. testKey, (int)testKeySz));
  28271. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28272. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  28273. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28274. (unsigned int)testDataSz), 1);
  28275. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28276. AssertIntEQ((int)checkSz, (int)testResultSz);
  28277. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28278. AssertIntEQ((int)checkSz,(int)testResultSz);
  28279. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  28280. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28281. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  28282. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28283. (unsigned int)testDataSz), 1);
  28284. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  28285. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28286. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28287. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  28288. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28289. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28290. AssertIntEQ((int)checkSz, (int)testResultSz);
  28291. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28292. AssertIntEQ((int)checkSz,(int)testResultSz);
  28293. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28294. (unsigned int)testDataSz - 4), 1);
  28295. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28296. AssertIntEQ((int)checkSz,(int)testResultSz);
  28297. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  28298. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28299. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  28300. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28301. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28302. (unsigned int)testDataSz - 4), 1);
  28303. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  28304. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28305. wolfSSL_EVP_PKEY_free(key);
  28306. }
  28307. #endif
  28308. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  28309. {
  28310. #ifdef OPENSSL_EXTRA
  28311. static const unsigned char testKey[] =
  28312. {
  28313. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  28314. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  28315. 0x0b, 0x0b, 0x0b, 0x0b
  28316. };
  28317. static const char testData[] = "Hi There";
  28318. #ifdef WOLFSSL_SHA224
  28319. static const unsigned char testResultSha224[] =
  28320. {
  28321. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  28322. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  28323. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  28324. 0x53, 0x68, 0x4b, 0x22
  28325. };
  28326. #endif
  28327. #ifndef NO_SHA256
  28328. static const unsigned char testResultSha256[] =
  28329. {
  28330. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  28331. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  28332. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  28333. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  28334. };
  28335. #endif
  28336. #ifdef WOLFSSL_SHA384
  28337. static const unsigned char testResultSha384[] =
  28338. {
  28339. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  28340. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  28341. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  28342. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  28343. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  28344. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  28345. };
  28346. #endif
  28347. #ifdef WOLFSSL_SHA512
  28348. static const unsigned char testResultSha512[] =
  28349. {
  28350. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  28351. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  28352. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  28353. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  28354. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  28355. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  28356. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  28357. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  28358. };
  28359. #endif
  28360. #ifdef WOLFSSL_SHA3
  28361. #ifndef WOLFSSL_NOSHA3_224
  28362. static const unsigned char testResultSha3_224[] =
  28363. {
  28364. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  28365. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  28366. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  28367. 0xf3, 0xc8, 0x60, 0xf7
  28368. };
  28369. #endif
  28370. #ifndef WOLFSSL_NOSHA3_256
  28371. static const unsigned char testResultSha3_256[] =
  28372. {
  28373. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  28374. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  28375. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  28376. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  28377. };
  28378. #endif
  28379. #ifndef WOLFSSL_NOSHA3_384
  28380. static const unsigned char testResultSha3_384[] =
  28381. {
  28382. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  28383. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  28384. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  28385. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  28386. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  28387. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  28388. };
  28389. #endif
  28390. #ifndef WOLFSSL_NOSHA3_512
  28391. static const unsigned char testResultSha3_512[] =
  28392. {
  28393. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  28394. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  28395. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  28396. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  28397. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  28398. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  28399. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  28400. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  28401. };
  28402. #endif
  28403. #endif
  28404. #ifndef NO_SHA256
  28405. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  28406. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  28407. #endif
  28408. #ifdef WOLFSSL_SHA224
  28409. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  28410. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  28411. #endif
  28412. #ifdef WOLFSSL_SHA384
  28413. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  28414. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  28415. #endif
  28416. #ifdef WOLFSSL_SHA512
  28417. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  28418. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  28419. #endif
  28420. #ifdef WOLFSSL_SHA3
  28421. #ifndef WOLFSSL_NOSHA3_224
  28422. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  28423. testData, XSTRLEN(testData), testResultSha3_224,
  28424. sizeof(testResultSha3_224));
  28425. #endif
  28426. #ifndef WOLFSSL_NOSHA3_256
  28427. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  28428. testData, XSTRLEN(testData), testResultSha3_256,
  28429. sizeof(testResultSha3_256));
  28430. #endif
  28431. #ifndef WOLFSSL_NOSHA3_384
  28432. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  28433. testData, XSTRLEN(testData), testResultSha3_384,
  28434. sizeof(testResultSha3_384));
  28435. #endif
  28436. #ifndef WOLFSSL_NOSHA3_512
  28437. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  28438. testData, XSTRLEN(testData), testResultSha3_512,
  28439. sizeof(testResultSha3_512));
  28440. #endif
  28441. #endif
  28442. printf(resultFmt, passed);
  28443. #endif /* OPENSSL_EXTRA */
  28444. return 0;
  28445. }
  28446. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  28447. {
  28448. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  28449. defined(USE_CERT_BUFFERS_2048)
  28450. WOLFSSL_EVP_PKEY* privKey;
  28451. WOLFSSL_EVP_PKEY* pubKey;
  28452. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  28453. const char testData[] = "Hi There";
  28454. WOLFSSL_EVP_MD_CTX mdCtx;
  28455. size_t checkSz = -1;
  28456. int sz = 2048 / 8;
  28457. const unsigned char* cp;
  28458. const unsigned char* p;
  28459. unsigned char check[2048/8];
  28460. size_t i;
  28461. int paddings[] = {
  28462. RSA_PKCS1_PADDING,
  28463. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  28464. RSA_PKCS1_PSS_PADDING,
  28465. #endif
  28466. };
  28467. printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
  28468. cp = client_key_der_2048;
  28469. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  28470. sizeof_client_key_der_2048)));
  28471. p = client_keypub_der_2048;
  28472. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  28473. sizeof_client_keypub_der_2048)));
  28474. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28475. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28476. NULL, privKey), 1);
  28477. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28478. (unsigned int)XSTRLEN(testData)), 1);
  28479. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28480. AssertIntEQ((int)checkSz, sz);
  28481. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28482. AssertIntEQ((int)checkSz,sz);
  28483. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28484. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28485. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28486. NULL, pubKey), 1);
  28487. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28488. (unsigned int)XSTRLEN(testData)),
  28489. 1);
  28490. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28491. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28492. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28493. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28494. NULL, privKey), 1);
  28495. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28496. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28497. AssertIntEQ((int)checkSz, sz);
  28498. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28499. AssertIntEQ((int)checkSz, sz);
  28500. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28501. (unsigned int)XSTRLEN(testData) - 4), 1);
  28502. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28503. AssertIntEQ((int)checkSz, sz);
  28504. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28505. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28506. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28507. NULL, pubKey), 1);
  28508. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28509. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28510. (unsigned int)XSTRLEN(testData) - 4),
  28511. 1);
  28512. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28513. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28514. /* Check all signing padding types */
  28515. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  28516. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28517. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  28518. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  28519. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  28520. paddings[i]), 1);
  28521. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28522. (unsigned int)XSTRLEN(testData)), 1);
  28523. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28524. AssertIntEQ((int)checkSz, sz);
  28525. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28526. AssertIntEQ((int)checkSz,sz);
  28527. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28528. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28529. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  28530. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  28531. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  28532. paddings[i]), 1);
  28533. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28534. (unsigned int)XSTRLEN(testData)), 1);
  28535. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28536. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28537. }
  28538. wolfSSL_EVP_PKEY_free(pubKey);
  28539. wolfSSL_EVP_PKEY_free(privKey);
  28540. printf(resultFmt, passed);
  28541. #endif
  28542. return 0;
  28543. }
  28544. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  28545. {
  28546. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28547. WOLFSSL_EVP_PKEY* privKey;
  28548. WOLFSSL_EVP_PKEY* pubKey;
  28549. const char testData[] = "Hi There";
  28550. WOLFSSL_EVP_MD_CTX mdCtx;
  28551. size_t checkSz = -1;
  28552. const unsigned char* cp;
  28553. const unsigned char* p;
  28554. unsigned char check[2048/8];
  28555. printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
  28556. cp = ecc_clikey_der_256;
  28557. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  28558. sizeof_ecc_clikey_der_256);
  28559. AssertNotNull(privKey);
  28560. p = ecc_clikeypub_der_256;
  28561. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  28562. sizeof_ecc_clikeypub_der_256)));
  28563. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28564. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28565. NULL, privKey), 1);
  28566. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28567. (unsigned int)XSTRLEN(testData)), 1);
  28568. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28569. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28570. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28571. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28572. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28573. NULL, pubKey), 1);
  28574. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28575. (unsigned int)XSTRLEN(testData)),
  28576. 1);
  28577. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28578. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28579. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28580. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28581. NULL, privKey), 1);
  28582. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28583. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28584. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28585. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28586. (unsigned int)XSTRLEN(testData) - 4), 1);
  28587. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28588. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28589. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28590. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28591. NULL, pubKey), 1);
  28592. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28593. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28594. (unsigned int)XSTRLEN(testData) - 4),
  28595. 1);
  28596. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28597. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28598. wolfSSL_EVP_PKEY_free(pubKey);
  28599. wolfSSL_EVP_PKEY_free(privKey);
  28600. printf(resultFmt, passed);
  28601. #endif
  28602. return 0;
  28603. }
  28604. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  28605. {
  28606. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28607. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  28608. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28609. char caFile[] = "./certs/client-ca.pem";
  28610. char clientFile[] = "./certs/client-cert.pem";
  28611. SSL_CTX* ctx;
  28612. X509* x509;
  28613. BIO *bio = NULL;
  28614. X509 *cert = NULL;
  28615. X509 *ca;
  28616. STACK_OF(X509) *chain = NULL;
  28617. STACK_OF(X509) *chain2 = NULL;
  28618. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  28619. #ifndef NO_WOLFSSL_SERVER
  28620. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28621. #else
  28622. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28623. #endif
  28624. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  28625. AssertNotNull(x509);
  28626. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  28627. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  28628. AssertNotNull(x509);
  28629. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  28630. /* additional test of getting EVP_PKEY key size from X509
  28631. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  28632. * allowed with user RSA */
  28633. {
  28634. EVP_PKEY* pkey;
  28635. #if defined(HAVE_ECC)
  28636. X509* ecX509;
  28637. #endif /* HAVE_ECC */
  28638. AssertNotNull(pkey = X509_get_pubkey(x509));
  28639. /* current RSA key is 2048 bit (256 bytes) */
  28640. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  28641. EVP_PKEY_free(pkey);
  28642. #if defined(HAVE_ECC)
  28643. #if defined(USE_CERT_BUFFERS_256)
  28644. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  28645. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  28646. SSL_FILETYPE_ASN1));
  28647. #else
  28648. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  28649. SSL_FILETYPE_PEM));
  28650. #endif
  28651. pkey = X509_get_pubkey(ecX509);
  28652. AssertNotNull(pkey);
  28653. /* current ECC key is 256 bit (32 bytes) */
  28654. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  28655. X509_free(ecX509);
  28656. EVP_PKEY_free(pkey);
  28657. #endif /* HAVE_ECC */
  28658. }
  28659. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  28660. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  28661. #ifdef WOLFSSL_ENCRYPTED_KEYS
  28662. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  28663. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  28664. #endif
  28665. SSL_CTX_free(ctx);
  28666. #ifndef NO_WOLFSSL_SERVER
  28667. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28668. #else
  28669. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28670. #endif
  28671. /* Test haproxy use case */
  28672. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  28673. /* Read Certificate */
  28674. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  28675. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  28676. AssertNotNull(chain = sk_X509_new_null());
  28677. AssertIntEQ(sk_X509_push(chain, ca), 1);
  28678. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  28679. AssertNotNull(ca = sk_X509_shift(chain2));
  28680. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  28681. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  28682. BIO_free(bio);
  28683. X509_free(cert);
  28684. sk_X509_pop_free(chain, X509_free);
  28685. sk_X509_pop_free(chain2, X509_free);
  28686. SSL_CTX_free(ctx);
  28687. printf(resultFmt, passed);
  28688. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28689. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28690. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  28691. return 0;
  28692. }
  28693. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  28694. static int test_wolfSSL_ERR_peek_last_error_line(void)
  28695. {
  28696. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28697. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  28698. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  28699. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  28700. tcp_ready ready;
  28701. func_args client_args;
  28702. func_args server_args;
  28703. #ifndef SINGLE_THREADED
  28704. THREAD_TYPE serverThread;
  28705. #endif
  28706. callback_functions client_cb;
  28707. callback_functions server_cb;
  28708. int line = 0;
  28709. int flag = ERR_TXT_STRING;
  28710. const char* file = NULL;
  28711. const char* data = NULL;
  28712. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  28713. /* create a failed connection and inspect the error */
  28714. #ifdef WOLFSSL_TIRTOS
  28715. fdOpenSession(Task_self());
  28716. #endif
  28717. XMEMSET(&client_args, 0, sizeof(func_args));
  28718. XMEMSET(&server_args, 0, sizeof(func_args));
  28719. StartTCP();
  28720. InitTcpReady(&ready);
  28721. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  28722. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  28723. client_cb.method = wolfTLSv1_1_client_method;
  28724. server_cb.method = wolfTLSv1_2_server_method;
  28725. server_args.signal = &ready;
  28726. server_args.callbacks = &server_cb;
  28727. client_args.signal = &ready;
  28728. client_args.callbacks = &client_cb;
  28729. #ifndef SINGLE_THREADED
  28730. start_thread(test_server_nofail, &server_args, &serverThread);
  28731. wait_tcp_ready(&server_args);
  28732. test_client_nofail(&client_args, NULL);
  28733. join_thread(serverThread);
  28734. #endif
  28735. FreeTcpReady(&ready);
  28736. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  28737. AssertNotNull(data);
  28738. /* check clearing error state */
  28739. ERR_remove_state(0);
  28740. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  28741. ERR_peek_last_error_line(NULL, &line);
  28742. AssertIntEQ(line, 0);
  28743. ERR_peek_last_error_line(&file, NULL);
  28744. AssertNull(file);
  28745. /* retry connection to fill error queue */
  28746. XMEMSET(&client_args, 0, sizeof(func_args));
  28747. XMEMSET(&server_args, 0, sizeof(func_args));
  28748. StartTCP();
  28749. InitTcpReady(&ready);
  28750. client_cb.method = wolfTLSv1_1_client_method;
  28751. server_cb.method = wolfTLSv1_2_server_method;
  28752. server_args.signal = &ready;
  28753. server_args.callbacks = &server_cb;
  28754. client_args.signal = &ready;
  28755. client_args.callbacks = &client_cb;
  28756. start_thread(test_server_nofail, &server_args, &serverThread);
  28757. wait_tcp_ready(&server_args);
  28758. test_client_nofail(&client_args, NULL);
  28759. join_thread(serverThread);
  28760. FreeTcpReady(&ready);
  28761. /* check that error code was stored */
  28762. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  28763. ERR_peek_last_error_line(NULL, &line);
  28764. AssertIntNE(line, 0);
  28765. ERR_peek_last_error_line(&file, NULL);
  28766. AssertNotNull(file);
  28767. #ifdef WOLFSSL_TIRTOS
  28768. fdOpenSession(Task_self());
  28769. #endif
  28770. printf(resultFmt, passed);
  28771. printf("\nTesting error print out\n");
  28772. ERR_print_errors_fp(stdout);
  28773. printf("Done testing print out\n\n");
  28774. fflush(stdout);
  28775. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28776. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  28777. return 0;
  28778. }
  28779. #endif
  28780. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28781. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28782. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  28783. {
  28784. (void) ok;
  28785. (void) ctx;
  28786. printf("ENTER verify_cb\n");
  28787. return SSL_SUCCESS;
  28788. }
  28789. #endif
  28790. static int test_wolfSSL_X509_Name_canon(void)
  28791. {
  28792. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  28793. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  28794. defined(WOLFSSL_CERT_GEN) && \
  28795. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  28796. const long ex_hash1 = 0x0fdb2da4;
  28797. const long ex_hash2 = 0x9f3e8c9e;
  28798. X509_NAME *name = NULL;
  28799. X509 *x509 = NULL;
  28800. FILE* file = NULL;
  28801. unsigned long hash = 0;
  28802. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  28803. byte *pbuf = NULL;
  28804. word32 len = 0;
  28805. (void) ex_hash2;
  28806. printf(testingFmt, "test_wolfSSL_X509_Name_canon()");
  28807. file = XFOPEN(caCertFile, "rb");
  28808. AssertNotNull(file);
  28809. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  28810. AssertNotNull(name = X509_get_issuer_name(x509));
  28811. /* When output buffer is NULL, should return necessary output buffer
  28812. * length.*/
  28813. AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  28814. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  28815. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  28816. hash = (((unsigned long)digest[3] << 24) |
  28817. ((unsigned long)digest[2] << 16) |
  28818. ((unsigned long)digest[1] << 8) |
  28819. ((unsigned long)digest[0]));
  28820. AssertIntEQ(hash, ex_hash1);
  28821. XFCLOSE(file);
  28822. X509_free(x509);
  28823. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  28824. pbuf = NULL;
  28825. file = XFOPEN(cliCertFile, "rb");
  28826. AssertNotNull(file);
  28827. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  28828. AssertNotNull(name = X509_get_issuer_name(x509));
  28829. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  28830. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  28831. hash = (((unsigned long)digest[3] << 24) |
  28832. ((unsigned long)digest[2] << 16) |
  28833. ((unsigned long)digest[1] << 8) |
  28834. ((unsigned long)digest[0]));
  28835. AssertIntEQ(hash, ex_hash2);
  28836. XFCLOSE(file);
  28837. X509_free(x509);
  28838. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  28839. printf(resultFmt, passed);
  28840. #endif
  28841. return 0;
  28842. }
  28843. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  28844. {
  28845. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  28846. const int MAX_DIR = 4;
  28847. const char paths[][32] = {
  28848. "./certs/ed25519",
  28849. "./certs/ecc",
  28850. "./certs/crl",
  28851. "./certs/",
  28852. };
  28853. char CertCrl_path[MAX_FILENAME_SZ];
  28854. char *p;
  28855. X509_STORE* str;
  28856. X509_LOOKUP* lookup;
  28857. WOLFSSL_STACK* sk = NULL;
  28858. int len, total_len, i;
  28859. (void) sk;
  28860. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_hash_dir()");
  28861. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  28862. /* illegal string */
  28863. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28864. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28865. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  28866. SSL_FILETYPE_PEM,NULL), 0);
  28867. /* free store */
  28868. X509_STORE_free(str);
  28869. /* short folder string */
  28870. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28871. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28872. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  28873. SSL_FILETYPE_PEM,NULL), 1);
  28874. #if defined(WOLFSSL_INT_H)
  28875. /* only available when including internal.h */
  28876. AssertNotNull(sk = lookup->dirs->dir_entry);
  28877. #endif
  28878. /* free store */
  28879. X509_STORE_free(str);
  28880. /* typical function check */
  28881. p = &CertCrl_path[0];
  28882. total_len = 0;
  28883. for(i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  28884. len = (int)XSTRLEN((const char*)&paths[i]);
  28885. total_len += len;
  28886. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  28887. p += len;
  28888. if (i != 0) *(p++) = SEPARATOR_CHAR;
  28889. }
  28890. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28891. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28892. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  28893. SSL_FILETYPE_PEM,NULL), 1);
  28894. #if defined(WOLFSSL_INT_H)
  28895. /* only available when including internal.h */
  28896. AssertNotNull(sk = lookup->dirs->dir_entry);
  28897. #endif
  28898. X509_STORE_free(str);
  28899. printf(resultFmt, passed);
  28900. #endif
  28901. return 0;
  28902. }
  28903. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  28904. {
  28905. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  28906. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  28907. defined(WOLFSSL_SIGNER_DER_CERT)
  28908. X509_STORE_CTX* ctx;
  28909. X509_STORE* str;
  28910. X509_LOOKUP* lookup;
  28911. X509* cert1;
  28912. X509* x509Ca;
  28913. X509* x509Svr;
  28914. X509* issuer;
  28915. WOLFSSL_STACK* sk = NULL;
  28916. X509_NAME* caName;
  28917. X509_NAME* issuerName;
  28918. FILE* file1 = NULL;
  28919. int i, cert_count, cmp;
  28920. char der[] = "certs/ca-cert.der";
  28921. #ifdef HAVE_CRL
  28922. char pem[][100] = {
  28923. "./certs/crl/crl.pem",
  28924. "./certs/crl/crl2.pem",
  28925. "./certs/crl/caEccCrl.pem",
  28926. "./certs/crl/eccCliCRL.pem",
  28927. "./certs/crl/eccSrvCRL.pem",
  28928. ""
  28929. };
  28930. #endif
  28931. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_file()");
  28932. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  28933. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  28934. fclose(file1);
  28935. AssertNotNull(ctx = X509_STORE_CTX_new());
  28936. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28937. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28938. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  28939. SSL_FILETYPE_PEM,NULL), 1);
  28940. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  28941. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  28942. /* check if CA cert is loaded into the store */
  28943. for (i = 0; i < cert_count; i++) {
  28944. x509Ca = sk_X509_value(sk, i);
  28945. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  28946. }
  28947. AssertNotNull((x509Svr =
  28948. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  28949. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  28950. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  28951. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  28952. AssertNotNull(issuer);
  28953. caName = X509_get_subject_name(x509Ca);
  28954. AssertNotNull(caName);
  28955. issuerName = X509_get_subject_name(issuer);
  28956. AssertNotNull(issuerName);
  28957. cmp = X509_NAME_cmp(caName, issuerName);
  28958. AssertIntEQ(cmp, 0);
  28959. /* load der format */
  28960. X509_free(issuer);
  28961. X509_STORE_CTX_free(ctx);
  28962. X509_STORE_free(str);
  28963. sk_X509_pop_free(sk, NULL);
  28964. X509_free(x509Svr);
  28965. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28966. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28967. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  28968. SSL_FILETYPE_ASN1,NULL), 1);
  28969. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  28970. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  28971. /* check if CA cert is loaded into the store */
  28972. for (i = 0; i < cert_count; i++) {
  28973. x509Ca = sk_X509_value(sk, i);
  28974. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  28975. }
  28976. X509_STORE_free(str);
  28977. sk_X509_pop_free(sk, NULL);
  28978. X509_free(cert1);
  28979. #ifdef HAVE_CRL
  28980. AssertNotNull(str = wolfSSL_X509_STORE_new());
  28981. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28982. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  28983. SSL_FILETYPE_PEM,NULL), 1);
  28984. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  28985. "certs/server-revoked-cert.pem",
  28986. SSL_FILETYPE_PEM,NULL), 1);
  28987. if (str) {
  28988. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  28989. WOLFSSL_FILETYPE_PEM), 1);
  28990. /* since store hasn't yet known the revoked cert*/
  28991. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  28992. "certs/server-revoked-cert.pem",
  28993. WOLFSSL_FILETYPE_PEM), 1);
  28994. }
  28995. for (i = 0; pem[i][0] != '\0'; i++)
  28996. {
  28997. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  28998. SSL_FILETYPE_PEM, NULL), 1);
  28999. }
  29000. if (str) {
  29001. /* since store knows crl list */
  29002. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29003. "certs/server-revoked-cert.pem",
  29004. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  29005. }
  29006. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  29007. X509_STORE_free(str);
  29008. #endif
  29009. printf(resultFmt, passed);
  29010. #endif
  29011. return 0;
  29012. }
  29013. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  29014. {
  29015. #if defined(OPENSSL_EXTRA)
  29016. printf(testingFmt, "test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup()");
  29017. X509_STORE_CTX_cleanup(NULL);
  29018. X509_STORE_CTX_trusted_stack(NULL, NULL);
  29019. AssertTrue(1); /* to confirm previous call gives no harm */
  29020. printf(resultFmt, passed);
  29021. #endif
  29022. return 0;
  29023. }
  29024. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  29025. {
  29026. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29027. #ifdef WOLFSSL_SIGNER_DER_CERT
  29028. int cmp;
  29029. #endif
  29030. X509_STORE_CTX* ctx;
  29031. X509_STORE* str;
  29032. X509* x509Ca;
  29033. X509* x509Svr;
  29034. X509* issuer;
  29035. X509_NAME* caName;
  29036. X509_NAME* issuerName;
  29037. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
  29038. AssertNotNull(ctx = X509_STORE_CTX_new());
  29039. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29040. AssertNotNull((x509Ca =
  29041. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  29042. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  29043. AssertNotNull((x509Svr =
  29044. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29045. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29046. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29047. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  29048. AssertNotNull(issuer);
  29049. caName = X509_get_subject_name(x509Ca);
  29050. AssertNotNull(caName);
  29051. issuerName = X509_get_subject_name(issuer);
  29052. AssertNotNull(issuerName);
  29053. #ifdef WOLFSSL_SIGNER_DER_CERT
  29054. cmp = X509_NAME_cmp(caName, issuerName);
  29055. AssertIntEQ(cmp, 0);
  29056. #endif
  29057. X509_free(issuer);
  29058. X509_STORE_CTX_free(ctx);
  29059. X509_free(x509Svr);
  29060. X509_STORE_free(str);
  29061. X509_free(x509Ca);
  29062. printf(resultFmt, passed);
  29063. #endif
  29064. return 0;
  29065. }
  29066. static int test_wolfSSL_PKCS7_certs(void)
  29067. {
  29068. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  29069. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  29070. STACK_OF(X509)* sk = NULL;
  29071. STACK_OF(X509_INFO)* info_sk = NULL;
  29072. PKCS7 *p7 = NULL;
  29073. BIO* bio;
  29074. const byte* p = NULL;
  29075. int buflen = 0;
  29076. int i;
  29077. printf(testingFmt, "wolfSSL_PKCS7_certs()");
  29078. /* Test twice. Once with d2i and once without to test
  29079. * that everything is free'd correctly. */
  29080. for (i = 0; i < 2; i++) {
  29081. AssertNotNull(p7 = PKCS7_new());
  29082. p7->version = 1;
  29083. p7->hashOID = SHAh;
  29084. AssertNotNull(bio = BIO_new(BIO_s_file()));
  29085. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29086. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  29087. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  29088. AssertNotNull(sk = sk_X509_new_null());
  29089. while (sk_X509_INFO_num(info_sk)) {
  29090. X509_INFO* info;
  29091. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  29092. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  29093. info->x509 = NULL;
  29094. X509_INFO_free(info);
  29095. }
  29096. sk_X509_INFO_free(info_sk);
  29097. BIO_free(bio);
  29098. bio = BIO_new(BIO_s_mem());
  29099. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  29100. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  29101. if (i == 0) {
  29102. PKCS7_free(p7);
  29103. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  29104. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  29105. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  29106. /* PKCS7_free free's the certs */
  29107. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  29108. }
  29109. BIO_free(bio);
  29110. PKCS7_free(p7);
  29111. }
  29112. printf(resultFmt, passed);
  29113. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29114. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  29115. return 0;
  29116. }
  29117. static int test_wolfSSL_X509_STORE_CTX(void)
  29118. {
  29119. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29120. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29121. X509_STORE_CTX* ctx;
  29122. X509_STORE* str;
  29123. X509* x509;
  29124. #ifdef OPENSSL_ALL
  29125. X509* x5092;
  29126. STACK_OF(X509) *sk, *sk2, *sk3;
  29127. #endif
  29128. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  29129. AssertNotNull(ctx = X509_STORE_CTX_new());
  29130. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29131. AssertNotNull((x509 =
  29132. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29133. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  29134. #ifdef OPENSSL_ALL
  29135. /* sk_X509_new only in OPENSSL_ALL */
  29136. sk = sk_X509_new();
  29137. AssertNotNull(sk);
  29138. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  29139. #else
  29140. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  29141. #endif
  29142. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  29143. X509_STORE_CTX_set_error(ctx, -5);
  29144. X509_STORE_CTX_set_error(NULL, -5);
  29145. X509_STORE_CTX_free(ctx);
  29146. #ifdef OPENSSL_ALL
  29147. sk_X509_pop_free(sk, NULL);
  29148. #endif
  29149. X509_STORE_free(str);
  29150. X509_free(x509);
  29151. AssertNotNull(ctx = X509_STORE_CTX_new());
  29152. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  29153. X509_STORE_CTX_free(ctx);
  29154. #ifdef OPENSSL_ALL
  29155. /* test X509_STORE_CTX_get(1)_chain */
  29156. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  29157. SSL_FILETYPE_PEM)));
  29158. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  29159. SSL_FILETYPE_PEM)));
  29160. AssertNotNull((sk = sk_X509_new()));
  29161. AssertIntEQ(sk_X509_push(sk, x509), 1);
  29162. AssertNotNull((str = X509_STORE_new()));
  29163. AssertNotNull((ctx = X509_STORE_CTX_new()));
  29164. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  29165. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  29166. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  29167. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  29168. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  29169. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  29170. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  29171. X509_STORE_CTX_free(ctx);
  29172. X509_STORE_free(str);
  29173. /* CTX certs not freed yet */
  29174. X509_free(x5092);
  29175. sk_X509_pop_free(sk, NULL);
  29176. /* sk3 is dup so free here */
  29177. sk_X509_pop_free(sk3, NULL);
  29178. #endif
  29179. /* test X509_STORE_CTX_get/set_ex_data */
  29180. {
  29181. int i = 0, tmpData = 5;
  29182. void* tmpDataRet;
  29183. AssertNotNull(ctx = X509_STORE_CTX_new());
  29184. #ifdef HAVE_EX_DATA
  29185. for (i = 0; i < MAX_EX_DATA; i++) {
  29186. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  29187. WOLFSSL_SUCCESS);
  29188. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  29189. AssertNotNull(tmpDataRet);
  29190. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  29191. }
  29192. #else
  29193. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  29194. WOLFSSL_FAILURE);
  29195. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  29196. AssertNull(tmpDataRet);
  29197. #endif
  29198. X509_STORE_CTX_free(ctx);
  29199. }
  29200. /* test X509_STORE_get/set_ex_data */
  29201. {
  29202. int i = 0, tmpData = 99;
  29203. void* tmpDataRet;
  29204. AssertNotNull(str = X509_STORE_new());
  29205. #ifdef HAVE_EX_DATA
  29206. for (i = 0; i < MAX_EX_DATA; i++) {
  29207. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  29208. WOLFSSL_SUCCESS);
  29209. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  29210. AssertNotNull(tmpDataRet);
  29211. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  29212. }
  29213. #else
  29214. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  29215. WOLFSSL_FAILURE);
  29216. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  29217. AssertNull(tmpDataRet);
  29218. #endif
  29219. X509_STORE_free(str);
  29220. }
  29221. printf(resultFmt, passed);
  29222. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29223. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29224. return 0;
  29225. }
  29226. static int test_wolfSSL_X509_STORE_set_flags(void)
  29227. {
  29228. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29229. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29230. X509_STORE* store;
  29231. X509* x509;
  29232. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  29233. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  29234. AssertNotNull((x509 =
  29235. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  29236. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  29237. #ifdef HAVE_CRL
  29238. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  29239. #else
  29240. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  29241. NOT_COMPILED_IN);
  29242. #endif
  29243. wolfSSL_X509_free(x509);
  29244. wolfSSL_X509_STORE_free(store);
  29245. printf(resultFmt, passed);
  29246. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29247. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29248. return 0;
  29249. }
  29250. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  29251. {
  29252. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  29253. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29254. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  29255. WOLFSSL_X509_STORE* store;
  29256. WOLFSSL_X509_LOOKUP* lookup;
  29257. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  29258. AssertNotNull(store = wolfSSL_X509_STORE_new());
  29259. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  29260. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  29261. X509_FILETYPE_PEM), 1);
  29262. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  29263. X509_FILETYPE_PEM), 1);
  29264. if (store) {
  29265. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  29266. WOLFSSL_FILETYPE_PEM), 1);
  29267. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  29268. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  29269. }
  29270. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  29271. X509_FILETYPE_PEM), 1);
  29272. if (store) {
  29273. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  29274. WOLFSSL_FILETYPE_PEM), 1);
  29275. }
  29276. wolfSSL_X509_STORE_free(store);
  29277. printf(resultFmt, passed);
  29278. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  29279. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29280. return 0;
  29281. }
  29282. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  29283. {
  29284. #if defined(OPENSSL_EXTRA)
  29285. WOLFSSL_X509_STORE_CTX* ctx;
  29286. time_t c_time;
  29287. printf(testingFmt, "wolfSSL_X509_set_time()");
  29288. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  29289. c_time = 365*24*60*60;
  29290. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  29291. AssertTrue(
  29292. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  29293. AssertTrue(ctx->param->check_time == c_time);
  29294. wolfSSL_X509_STORE_CTX_free(ctx);
  29295. printf(resultFmt, passed);
  29296. #endif /* OPENSSL_EXTRA */
  29297. return 0;
  29298. }
  29299. static int test_wolfSSL_CTX_get0_set1_param(void)
  29300. {
  29301. #if defined(OPENSSL_EXTRA)
  29302. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29303. int ret;
  29304. SSL_CTX* ctx;
  29305. WOLFSSL_X509_VERIFY_PARAM* pParam;
  29306. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  29307. char testIPv4[] = "127.0.0.1";
  29308. char testhostName[] = "foo.hoge.com";
  29309. printf(testingFmt, "wolfSSL_CTX_get0_set1_param()");
  29310. #ifndef NO_WOLFSSL_SERVER
  29311. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29312. #else
  29313. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29314. #endif
  29315. AssertNull(SSL_CTX_get0_param(NULL));
  29316. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  29317. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  29318. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  29319. AssertNotNull(pvpm);
  29320. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29321. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  29322. (int)XSTRLEN(testhostName));
  29323. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  29324. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  29325. ret = SSL_CTX_set1_param(ctx, pvpm);
  29326. AssertIntEQ(1, ret);
  29327. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  29328. (int)XSTRLEN(testhostName)));
  29329. AssertIntEQ(0x01, pParam->hostFlags);
  29330. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  29331. /* test for incorrect patameter */
  29332. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  29333. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  29334. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  29335. SSL_CTX_free(ctx);
  29336. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  29337. printf(resultFmt, passed);
  29338. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29339. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  29340. return 0;
  29341. }
  29342. static int test_wolfSSL_get0_param(void)
  29343. {
  29344. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29345. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29346. SSL_CTX* ctx;
  29347. SSL* ssl;
  29348. WOLFSSL_X509_VERIFY_PARAM* pParam;
  29349. printf(testingFmt, "wolfSSL_get0_param()");
  29350. #ifndef NO_WOLFSSL_SERVER
  29351. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29352. #else
  29353. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29354. #endif
  29355. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29356. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29357. AssertNotNull(ssl = SSL_new(ctx));
  29358. pParam = SSL_get0_param(ssl);
  29359. (void)pParam;
  29360. SSL_free(ssl);
  29361. SSL_CTX_free(ctx);
  29362. printf(resultFmt, passed);
  29363. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29364. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  29365. return 0;
  29366. }
  29367. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  29368. {
  29369. #if defined(OPENSSL_EXTRA)
  29370. const char host[] = "www.example.com";
  29371. WOLFSSL_X509_VERIFY_PARAM* pParam;
  29372. printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
  29373. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  29374. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  29375. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  29376. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29377. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  29378. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29379. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29380. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29381. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29382. printf(resultFmt, passed);
  29383. #endif /* OPENSSL_EXTRA */
  29384. return 0;
  29385. }
  29386. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  29387. {
  29388. #if defined(OPENSSL_EXTRA)
  29389. unsigned char buf[16] = {0};
  29390. WOLFSSL_X509_VERIFY_PARAM* param;
  29391. printf(testingFmt, "test_wolfSSL_X509_VERIFY_PARAM_set1_ip()");
  29392. AssertNotNull(param = X509_VERIFY_PARAM_new());
  29393. /* test 127.0.0.1 */
  29394. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  29395. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  29396. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  29397. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  29398. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29399. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  29400. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  29401. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  29402. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29403. AssertIntEQ(XSTRNCMP(param->ipasc,
  29404. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  29405. /* test 2001:db8:: */
  29406. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29407. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29408. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29409. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  29410. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29411. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  29412. /* test ::1234:5678 */
  29413. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  29414. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29415. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29416. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  29417. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29418. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  29419. /* test 2001:db8::1234:5678 */
  29420. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29421. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29422. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29423. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  29424. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29425. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  29426. sizeof(param->ipasc)), 0);
  29427. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  29428. /* 2001:db8:1::ab9:c0a8:102 */
  29429. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29430. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  29431. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  29432. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  29433. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29434. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  29435. sizeof(param->ipasc)), 0);
  29436. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29437. printf(resultFmt, passed);
  29438. #endif /* OPENSSL_EXTRA */
  29439. return 0;
  29440. }
  29441. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  29442. {
  29443. #if defined(OPENSSL_EXTRA)
  29444. X509_STORE* store;
  29445. X509_STORE_CTX* ctx;
  29446. X509_STORE_CTX* ctx_no_init;
  29447. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
  29448. AssertNotNull((store = X509_STORE_new()));
  29449. AssertNotNull(ctx = X509_STORE_CTX_new());
  29450. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  29451. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  29452. AssertNull(X509_STORE_CTX_get0_store(NULL));
  29453. /* should return NULL if ctx has not bee initialized */
  29454. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  29455. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  29456. wolfSSL_X509_STORE_CTX_free(ctx);
  29457. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  29458. X509_STORE_free(store);
  29459. printf(resultFmt, passed);
  29460. #endif /* OPENSSL_EXTRA */
  29461. return 0;
  29462. }
  29463. static int test_wolfSSL_CTX_set_client_CA_list(void)
  29464. {
  29465. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  29466. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  29467. WOLFSSL_CTX* ctx;
  29468. WOLFSSL* ssl;
  29469. X509_NAME* name = NULL;
  29470. STACK_OF(X509_NAME)* names = NULL;
  29471. STACK_OF(X509_NAME)* ca_list = NULL;
  29472. int i, names_len;
  29473. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  29474. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  29475. /* Send two X501 names in cert request */
  29476. names = SSL_load_client_CA_file(cliCertFile);
  29477. AssertNotNull(names);
  29478. ca_list = SSL_load_client_CA_file(caCertFile);
  29479. AssertNotNull(ca_list);
  29480. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  29481. SSL_CTX_set_client_CA_list(ctx, names);
  29482. /* This should only free the stack structure */
  29483. sk_X509_NAME_free(ca_list);
  29484. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  29485. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  29486. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  29487. for (i=0; i<names_len; i++) {
  29488. AssertNotNull(name = sk_X509_NAME_value(names, i));
  29489. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  29490. }
  29491. /* Needed to be able to create ssl object */
  29492. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29493. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29494. AssertNotNull(ssl = wolfSSL_new(ctx));
  29495. /* load again as old names are responsibility of ctx to free*/
  29496. names = SSL_load_client_CA_file(cliCertFile);
  29497. AssertNotNull(names);
  29498. SSL_set_client_CA_list(ssl, names);
  29499. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  29500. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  29501. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  29502. for (i=0; i<names_len; i++) {
  29503. AssertNotNull(name = sk_X509_NAME_value(names, i));
  29504. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  29505. }
  29506. printf(resultFmt, passed);
  29507. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  29508. {
  29509. tcp_ready ready;
  29510. func_args server_args;
  29511. callback_functions server_cb;
  29512. THREAD_TYPE serverThread;
  29513. WOLFSSL* ssl_client;
  29514. WOLFSSL_CTX* ctx_client;
  29515. SOCKET_T sockfd = 0;
  29516. printf(testingFmt, "wolfSSL_get_client_CA_list() with handshake");
  29517. StartTCP();
  29518. InitTcpReady(&ready);
  29519. XMEMSET(&server_args, 0, sizeof(func_args));
  29520. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29521. server_args.signal = &ready;
  29522. server_args.callbacks = &server_cb;
  29523. /* we are responsible for free'ing WOLFSSL_CTX */
  29524. server_cb.ctx = ctx;
  29525. server_cb.isSharedCtx = 1;
  29526. AssertIntEQ(WOLFSSL_SUCCESS,
  29527. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  29528. start_thread(test_server_nofail, &server_args, &serverThread);
  29529. wait_tcp_ready(&server_args);
  29530. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  29531. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  29532. AssertIntEQ(WOLFSSL_SUCCESS,
  29533. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  29534. AssertIntEQ(WOLFSSL_SUCCESS,
  29535. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  29536. AssertIntEQ(WOLFSSL_SUCCESS,
  29537. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  29538. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  29539. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  29540. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  29541. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  29542. /* We are expecting two cert names to be sent */
  29543. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  29544. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  29545. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  29546. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  29547. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  29548. }
  29549. wolfSSL_shutdown(ssl_client);
  29550. wolfSSL_free(ssl_client);
  29551. wolfSSL_CTX_free(ctx_client);
  29552. join_thread(serverThread);
  29553. FreeTcpReady(&ready);
  29554. printf(resultFmt, passed);
  29555. }
  29556. #endif
  29557. wolfSSL_free(ssl);
  29558. wolfSSL_CTX_free(ctx);
  29559. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT && !NO_BIO */
  29560. return 0;
  29561. }
  29562. static int test_wolfSSL_CTX_add_client_CA(void)
  29563. {
  29564. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  29565. !defined(NO_WOLFSSL_CLIENT)
  29566. WOLFSSL_CTX* ctx;
  29567. WOLFSSL_X509* x509;
  29568. WOLFSSL_X509* x509_a;
  29569. STACK_OF(X509_NAME)* ca_list;
  29570. int ret = 0;
  29571. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  29572. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29573. /* Add client cert */
  29574. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  29575. AssertNotNull(x509);
  29576. ret = SSL_CTX_add_client_CA(ctx, x509);
  29577. AssertIntEQ(ret, SSL_SUCCESS);
  29578. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  29579. /* Add another client cert */
  29580. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  29581. SSL_FILETYPE_PEM));
  29582. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  29583. /* test for incorrect parameter */
  29584. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  29585. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  29586. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  29587. X509_free(x509);
  29588. X509_free(x509_a);
  29589. SSL_CTX_free(ctx);
  29590. printf(resultFmt, passed);
  29591. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  29592. return 0;
  29593. }
  29594. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  29595. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  29596. {
  29597. callback_functions* callbacks = ((func_args*)args)->callbacks;
  29598. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  29599. WOLFSSL* ssl = NULL;
  29600. SOCKET_T sfd = 0;
  29601. SOCKET_T cfd = 0;
  29602. word16 port;
  29603. char msg[] = "I hear you fa shizzle!";
  29604. int len = (int) XSTRLEN(msg);
  29605. char input[1024];
  29606. int idx;
  29607. int ret, err = 0;
  29608. #ifdef WOLFSSL_TIRTOS
  29609. fdOpenSession(Task_self());
  29610. #endif
  29611. ((func_args*)args)->return_code = TEST_FAIL;
  29612. port = ((func_args*)args)->signal->port;
  29613. AssertIntEQ(WOLFSSL_SUCCESS,
  29614. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  29615. AssertIntEQ(WOLFSSL_SUCCESS,
  29616. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  29617. WOLFSSL_FILETYPE_PEM));
  29618. AssertIntEQ(WOLFSSL_SUCCESS,
  29619. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  29620. WOLFSSL_FILETYPE_PEM));
  29621. if (callbacks->ctx_ready)
  29622. callbacks->ctx_ready(ctx);
  29623. ssl = wolfSSL_new(ctx);
  29624. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  29625. CloseSocket(sfd);
  29626. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  29627. if (callbacks->ssl_ready)
  29628. callbacks->ssl_ready(ssl);
  29629. do {
  29630. err = 0; /* Reset error */
  29631. ret = wolfSSL_accept(ssl);
  29632. if (ret != WOLFSSL_SUCCESS) {
  29633. err = wolfSSL_get_error(ssl, 0);
  29634. }
  29635. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  29636. if (ret != WOLFSSL_SUCCESS) {
  29637. char buff[WOLFSSL_MAX_ERROR_SZ];
  29638. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  29639. }
  29640. else {
  29641. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  29642. input[idx] = 0;
  29643. printf("Client message: %s\n", input);
  29644. }
  29645. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  29646. #ifdef WOLFSSL_TIRTOS
  29647. Task_yield();
  29648. #endif
  29649. ((func_args*)args)->return_code = TEST_SUCCESS;
  29650. }
  29651. if (callbacks->on_result)
  29652. callbacks->on_result(ssl);
  29653. wolfSSL_shutdown(ssl);
  29654. wolfSSL_free(ssl);
  29655. wolfSSL_CTX_free(ctx);
  29656. CloseSocket(cfd);
  29657. #ifdef WOLFSSL_TIRTOS
  29658. fdCloseSession(Task_self());
  29659. #endif
  29660. #ifndef WOLFSSL_TIRTOS
  29661. return 0;
  29662. #endif
  29663. }
  29664. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  29665. {
  29666. AssertNotNull(ssl);
  29667. AssertNotNull(line);
  29668. XFILE fp;
  29669. const byte lf = '\n';
  29670. fp = XFOPEN("./MyKeyLog.txt", "a");
  29671. XFWRITE( line, 1, strlen(line),fp);
  29672. XFWRITE( (void*)&lf,1,1,fp);
  29673. XFCLOSE(fp);
  29674. }
  29675. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  29676. static int test_wolfSSL_CTX_set_keylog_callback(void)
  29677. {
  29678. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  29679. !defined(NO_WOLFSSL_CLIENT)
  29680. SSL_CTX* ctx;
  29681. printf( testingFmt, "wolfSSL_CTX_set_keylog_callback()");
  29682. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29683. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  29684. SSL_CTX_free(ctx);
  29685. SSL_CTX_set_keylog_callback(NULL, NULL);
  29686. printf(resultFmt, passed);
  29687. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  29688. return 0;
  29689. }
  29690. static int test_wolfSSL_CTX_get_keylog_callback(void)
  29691. {
  29692. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  29693. !defined(NO_WOLFSSL_CLIENT)
  29694. SSL_CTX* ctx;
  29695. printf( testingFmt, "wolfSSL_CTX_get_keylog_callback()");
  29696. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29697. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  29698. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  29699. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  29700. SSL_CTX_set_keylog_callback(ctx, NULL );
  29701. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  29702. SSL_CTX_free(ctx);
  29703. printf(resultFmt, passed);
  29704. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  29705. return 0;
  29706. }
  29707. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  29708. {
  29709. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  29710. /* This test is intended for checking whether keylog callback is called
  29711. * in client during TLS handshake between the client and a server.
  29712. */
  29713. tcp_ready ready;
  29714. func_args client_args;
  29715. func_args server_args;
  29716. THREAD_TYPE serverThread;
  29717. callback_functions server_cbf;
  29718. callback_functions client_cbf;
  29719. SOCKET_T sockfd = 0;
  29720. WOLFSSL_CTX* ctx;
  29721. WOLFSSL* ssl;
  29722. XFILE fp;
  29723. char msg[64] = "hello wolfssl!";
  29724. char reply[1024];
  29725. int msgSz = (int)XSTRLEN(msg);
  29726. printf(testingFmt, "wolfSSL_Tls12_Key_Logging_test()");
  29727. #ifdef WOLFSSL_TIRTOS
  29728. fdOpenSession(Task_self());
  29729. #endif
  29730. InitTcpReady(&ready);
  29731. ready.port = 22222;
  29732. XMEMSET(&client_args, 0, sizeof(func_args));
  29733. XMEMSET(&server_args, 0, sizeof(func_args));
  29734. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29735. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29736. server_cbf.method = wolfTLSv1_2_server_method;
  29737. server_args.callbacks = &server_cbf;
  29738. server_args.signal = &ready;
  29739. /* clean up keylog file */
  29740. fp = XFOPEN("./MyKeyLog.txt", "w");
  29741. XFCLOSE(fp);
  29742. /* start server task */
  29743. start_thread(server_task, &server_args, &serverThread);
  29744. wait_tcp_ready(&server_args);
  29745. /* run as a TLS1.2 client */
  29746. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  29747. AssertIntEQ(WOLFSSL_SUCCESS,
  29748. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  29749. AssertIntEQ(WOLFSSL_SUCCESS,
  29750. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  29751. AssertIntEQ(WOLFSSL_SUCCESS,
  29752. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  29753. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  29754. /* set keylog callback */
  29755. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  29756. /* get connected the server task */
  29757. AssertNotNull(ssl = wolfSSL_new(ctx));
  29758. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  29759. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  29760. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  29761. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  29762. wolfSSL_shutdown(ssl);
  29763. wolfSSL_free(ssl);
  29764. wolfSSL_CTX_free(ctx);
  29765. CloseSocket(sockfd);
  29766. join_thread(serverThread);
  29767. FreeTcpReady(&ready);
  29768. #ifdef WOLFSSL_TIRTOS
  29769. fdOpenSession(Task_self());
  29770. #endif
  29771. /* check if the keylog file exists */
  29772. char buff[300] = {0};
  29773. int found = 0;
  29774. fp = XFOPEN("./MyKeyLog.txt", "r");
  29775. AssertNotNull(fp);
  29776. while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  29777. if(0 == strncmp(buff,"CLIENT_RANDOM ",
  29778. sizeof("CLIENT_RANDOM ")-1)) {
  29779. found = 1;
  29780. break;
  29781. }
  29782. }
  29783. XFCLOSE(fp);
  29784. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  29785. AssertNotNull( found );
  29786. printf(resultFmt, passed);
  29787. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  29788. return 0;
  29789. }
  29790. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  29791. {
  29792. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  29793. defined(HAVE_SECRET_CALLBACK)
  29794. /* This test is intended for checking whether keylog callback is called
  29795. * in client during TLS handshake between the client and a server.
  29796. */
  29797. tcp_ready ready;
  29798. func_args client_args;
  29799. func_args server_args;
  29800. THREAD_TYPE serverThread;
  29801. callback_functions server_cbf;
  29802. callback_functions client_cbf;
  29803. SOCKET_T sockfd = 0;
  29804. WOLFSSL_CTX* ctx;
  29805. WOLFSSL* ssl;
  29806. XFILE fp;
  29807. char msg[64] = "hello wolfssl!";
  29808. char reply[1024];
  29809. int msgSz = (int)XSTRLEN(msg);
  29810. printf(testingFmt, "wolfSSL_Tls13_Key_Logging_test()");
  29811. #ifdef WOLFSSL_TIRTOS
  29812. fdOpenSession(Task_self());
  29813. #endif
  29814. InitTcpReady(&ready);
  29815. ready.port = 22222;
  29816. XMEMSET(&client_args, 0, sizeof(func_args));
  29817. XMEMSET(&server_args, 0, sizeof(func_args));
  29818. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29819. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29820. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  29821. server_args.callbacks = &server_cbf;
  29822. server_args.signal = &ready;
  29823. /* clean up keylog file */
  29824. fp = XFOPEN("./MyKeyLog.txt", "w");
  29825. XFCLOSE(fp);
  29826. /* start server task */
  29827. start_thread(server_task, &server_args, &serverThread);
  29828. wait_tcp_ready(&server_args);
  29829. /* run as a TLS1.3 client */
  29830. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  29831. AssertIntEQ(WOLFSSL_SUCCESS,
  29832. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  29833. AssertIntEQ(WOLFSSL_SUCCESS,
  29834. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  29835. AssertIntEQ(WOLFSSL_SUCCESS,
  29836. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  29837. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  29838. /* set keylog callback */
  29839. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  29840. /* get connected the server task */
  29841. AssertNotNull(ssl = wolfSSL_new(ctx));
  29842. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  29843. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  29844. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  29845. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  29846. wolfSSL_free(ssl);
  29847. wolfSSL_CTX_free(ctx);
  29848. join_thread(serverThread);
  29849. FreeTcpReady(&ready);
  29850. #ifdef WOLFSSL_TIRTOS
  29851. fdOpenSession(Task_self());
  29852. #endif
  29853. /* check if the keylog file exists */
  29854. {
  29855. char buff[300] = {0};
  29856. int found[4] = {0};
  29857. int numfnd = 0;
  29858. int i;
  29859. fp = XFOPEN("./MyKeyLog.txt", "r");
  29860. AssertNotNull(fp);
  29861. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  29862. if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  29863. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  29864. found[0] = 1;
  29865. continue;
  29866. }
  29867. else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  29868. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  29869. found[1] = 1;
  29870. continue;
  29871. }
  29872. else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  29873. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  29874. found[2] = 1;
  29875. continue;
  29876. }
  29877. else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  29878. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  29879. found[3] = 1;
  29880. continue;
  29881. }
  29882. }
  29883. XFCLOSE(fp);
  29884. for (i = 0; i < 4; i++) {
  29885. if( found[i] != 0)
  29886. numfnd++;
  29887. }
  29888. AssertIntEQ(numfnd, 4);
  29889. }
  29890. printf(resultFmt, passed);
  29891. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  29892. return 0;
  29893. }
  29894. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  29895. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29896. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  29897. static void post_auth_version_cb(WOLFSSL* ssl)
  29898. {
  29899. /* do handshake and then test version error */
  29900. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  29901. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  29902. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  29903. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  29904. /* check was added to error queue */
  29905. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  29906. /* check the string matches expected string */
  29907. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  29908. "WRONG_SSL_VERSION");
  29909. #endif
  29910. }
  29911. static void post_auth_cb(WOLFSSL* ssl)
  29912. {
  29913. WOLFSSL_X509* x509;
  29914. /* do handshake and then test version error */
  29915. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  29916. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  29917. AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
  29918. wolfSSL_X509_free(x509);
  29919. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  29920. }
  29921. static void set_post_auth_cb(WOLFSSL* ssl)
  29922. {
  29923. if (!wolfSSL_is_server(ssl)) {
  29924. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  29925. }
  29926. else {
  29927. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  29928. }
  29929. }
  29930. #endif
  29931. static int test_wolfSSL_Tls13_postauth(void)
  29932. {
  29933. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  29934. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29935. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  29936. tcp_ready ready;
  29937. func_args client_args;
  29938. func_args server_args;
  29939. callback_functions server_cbf;
  29940. callback_functions client_cbf;
  29941. THREAD_TYPE serverThread;
  29942. printf(testingFmt, "wolfSSL_Tls13_postauth()");
  29943. XMEMSET(&client_args, 0, sizeof(func_args));
  29944. XMEMSET(&server_args, 0, sizeof(func_args));
  29945. StartTCP();
  29946. InitTcpReady(&ready);
  29947. #if defined(USE_WINDOWS_API)
  29948. /* use RNG to get random port if using windows */
  29949. ready.port = GetRandomPort();
  29950. #endif
  29951. server_args.signal = &ready;
  29952. client_args.signal = &ready;
  29953. /* test version failure doing post auth with TLS 1.2 connection */
  29954. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29955. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29956. server_cbf.method = wolfTLSv1_2_server_method;
  29957. server_cbf.ssl_ready = set_post_auth_cb;
  29958. client_cbf.ssl_ready = set_post_auth_cb;
  29959. server_cbf.on_result = post_auth_version_cb;
  29960. server_args.callbacks = &server_cbf;
  29961. client_args.callbacks = &client_cbf;
  29962. start_thread(test_server_nofail, &server_args, &serverThread);
  29963. wait_tcp_ready(&server_args);
  29964. test_client_nofail(&client_args, NULL);
  29965. join_thread(serverThread);
  29966. /* tests on post auth with TLS 1.3 */
  29967. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29968. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29969. server_cbf.method = wolfTLSv1_3_server_method;
  29970. server_cbf.ssl_ready = set_post_auth_cb;
  29971. client_cbf.ssl_ready = set_post_auth_cb;
  29972. server_cbf.on_result = post_auth_cb;
  29973. server_args.callbacks = &server_cbf;
  29974. client_args.callbacks = &client_cbf;
  29975. start_thread(test_server_nofail, &server_args, &serverThread);
  29976. wait_tcp_ready(&server_args);
  29977. test_client_nofail(&client_args, NULL);
  29978. join_thread(serverThread);
  29979. FreeTcpReady(&ready);
  29980. printf(resultFmt, passed);
  29981. #endif
  29982. return 0;
  29983. }
  29984. static int test_wolfSSL_X509_NID(void)
  29985. {
  29986. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29987. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  29988. int sigType;
  29989. int nameSz;
  29990. X509* cert;
  29991. EVP_PKEY* pubKeyTmp;
  29992. X509_NAME* name;
  29993. char commonName[80];
  29994. char countryName[80];
  29995. char localityName[80];
  29996. char stateName[80];
  29997. char orgName[80];
  29998. char orgUnit[80];
  29999. printf(testingFmt, "wolfSSL_X509_NID()");
  30000. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  30001. /* convert cert from DER to internal WOLFSSL_X509 struct */
  30002. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  30003. sizeof_client_cert_der_2048));
  30004. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  30005. /* extract PUBLIC KEY from cert */
  30006. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  30007. /* extract signatureType */
  30008. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  30009. /* extract subjectName info */
  30010. AssertNotNull(name = X509_get_subject_name(cert));
  30011. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  30012. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30013. NULL, 0)), 0);
  30014. AssertIntEQ(nameSz, 15);
  30015. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30016. commonName, sizeof(commonName))), 0);
  30017. AssertIntEQ(nameSz, 15);
  30018. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  30019. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30020. commonName, 9)), 0);
  30021. AssertIntEQ(nameSz, 8);
  30022. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  30023. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  30024. countryName, sizeof(countryName))), 0);
  30025. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  30026. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  30027. localityName, sizeof(localityName))), 0);
  30028. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  30029. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  30030. stateName, sizeof(stateName))), 0);
  30031. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  30032. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  30033. orgName, sizeof(orgName))), 0);
  30034. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  30035. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  30036. orgUnit, sizeof(orgUnit))), 0);
  30037. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  30038. EVP_PKEY_free(pubKeyTmp);
  30039. X509_free(cert);
  30040. printf(resultFmt, passed);
  30041. #endif
  30042. return 0;
  30043. }
  30044. static int test_wolfSSL_CTX_set_srp_username(void)
  30045. {
  30046. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  30047. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  30048. WOLFSSL_CTX* ctx;
  30049. WOLFSSL* ssl;
  30050. const char *username = "TESTUSER";
  30051. const char *password = "TESTPASSWORD";
  30052. int r;
  30053. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  30054. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30055. AssertNotNull(ctx);
  30056. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  30057. AssertIntEQ(r,SSL_SUCCESS);
  30058. wolfSSL_CTX_free(ctx);
  30059. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30060. AssertNotNull(ctx);
  30061. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  30062. AssertIntEQ(r,SSL_SUCCESS);
  30063. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  30064. AssertIntEQ(r,SSL_SUCCESS);
  30065. AssertNotNull(ssl = SSL_new(ctx));
  30066. AssertNotNull(SSL_get_srp_username(ssl));
  30067. AssertStrEQ(SSL_get_srp_username(ssl), username);
  30068. wolfSSL_free(ssl);
  30069. wolfSSL_CTX_free(ctx);
  30070. printf(resultFmt, passed);
  30071. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  30072. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  30073. return 0;
  30074. }
  30075. static int test_wolfSSL_CTX_set_srp_password(void)
  30076. {
  30077. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  30078. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  30079. WOLFSSL_CTX* ctx;
  30080. const char *username = "TESTUSER";
  30081. const char *password = "TESTPASSWORD";
  30082. int r;
  30083. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  30084. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30085. AssertNotNull(ctx);
  30086. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  30087. AssertIntEQ(r,SSL_SUCCESS);
  30088. wolfSSL_CTX_free(ctx);
  30089. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30090. AssertNotNull(ctx);
  30091. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  30092. AssertIntEQ(r,SSL_SUCCESS);
  30093. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  30094. AssertIntEQ(r,SSL_SUCCESS);
  30095. wolfSSL_CTX_free(ctx);
  30096. printf(resultFmt, passed);
  30097. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  30098. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  30099. return 0;
  30100. }
  30101. static int test_wolfSSL_X509_STORE(void)
  30102. {
  30103. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30104. X509_STORE *store;
  30105. #ifdef HAVE_CRL
  30106. X509_STORE_CTX *storeCtx;
  30107. X509_CRL *crl;
  30108. X509 *ca, *cert;
  30109. const char crlPem[] = "./certs/crl/crl.revoked";
  30110. const char srvCert[] = "./certs/server-revoked-cert.pem";
  30111. const char caCert[] = "./certs/ca-cert.pem";
  30112. XFILE fp;
  30113. printf(testingFmt, "test_wolfSSL_X509_STORE");
  30114. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30115. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  30116. SSL_FILETYPE_PEM)));
  30117. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  30118. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  30119. SSL_FILETYPE_PEM)));
  30120. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  30121. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  30122. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  30123. X509_STORE_free(store);
  30124. X509_STORE_CTX_free(storeCtx);
  30125. X509_free(cert);
  30126. X509_free(ca);
  30127. /* should fail to verify now after adding in CRL */
  30128. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30129. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  30130. SSL_FILETYPE_PEM)));
  30131. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  30132. fp = XFOPEN(crlPem, "rb");
  30133. AssertTrue((fp != XBADFILE));
  30134. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  30135. NULL, NULL));
  30136. XFCLOSE(fp);
  30137. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  30138. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  30139. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  30140. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  30141. SSL_FILETYPE_PEM)));
  30142. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  30143. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  30144. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  30145. X509_CRL_free(crl);
  30146. X509_STORE_free(store);
  30147. X509_STORE_CTX_free(storeCtx);
  30148. X509_free(cert);
  30149. X509_free(ca);
  30150. #endif /* HAVE_CRL */
  30151. #ifndef WOLFCRYPT_ONLY
  30152. {
  30153. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30154. SSL_CTX* ctx;
  30155. SSL* ssl;
  30156. int i;
  30157. for (i = 0; i < 2; i++) {
  30158. #ifndef NO_WOLFSSL_SERVER
  30159. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30160. #else
  30161. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30162. #endif
  30163. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30164. SSL_CTX_set_cert_store(ctx, store);
  30165. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30166. SSL_CTX_set_cert_store(ctx, store);
  30167. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30168. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30169. SSL_FILETYPE_PEM), SSL_SUCCESS);
  30170. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30171. SSL_FILETYPE_PEM), SSL_SUCCESS);
  30172. AssertNotNull(ssl = SSL_new(ctx));
  30173. if (i == 0) {
  30174. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  30175. }
  30176. else {
  30177. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  30178. X509_STORE_free(store);
  30179. }
  30180. SSL_free(ssl);
  30181. SSL_CTX_free(ctx);
  30182. }
  30183. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30184. }
  30185. #endif
  30186. printf(resultFmt, passed);
  30187. #endif
  30188. return 0;
  30189. }
  30190. static int test_wolfSSL_X509_STORE_load_locations(void)
  30191. {
  30192. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  30193. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  30194. SSL_CTX *ctx;
  30195. X509_STORE *store;
  30196. const char ca_file[] = "./certs/ca-cert.pem";
  30197. const char client_pem_file[] = "./certs/client-cert.pem";
  30198. const char client_der_file[] = "./certs/client-cert.der";
  30199. const char ecc_file[] = "./certs/ecc-key.pem";
  30200. const char certs_path[] = "./certs/";
  30201. const char bad_path[] = "./bad-path/";
  30202. #ifdef HAVE_CRL
  30203. const char crl_path[] = "./certs/crl/";
  30204. const char crl_file[] = "./certs/crl/crl.pem";
  30205. #endif
  30206. printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
  30207. #ifndef NO_WOLFSSL_SERVER
  30208. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30209. #else
  30210. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30211. #endif
  30212. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  30213. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  30214. /* Test bad arguments */
  30215. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  30216. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  30217. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  30218. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  30219. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  30220. #ifdef HAVE_CRL
  30221. /* Test with CRL */
  30222. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  30223. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  30224. #endif
  30225. /* Test with CA */
  30226. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  30227. /* Test with client_cert and certs path */
  30228. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  30229. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  30230. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  30231. /* Clear nodes */
  30232. ERR_clear_error();
  30233. #endif
  30234. SSL_CTX_free(ctx);
  30235. printf(resultFmt, passed);
  30236. #endif
  30237. return 0;
  30238. }
  30239. static int test_X509_STORE_get0_objects(void)
  30240. {
  30241. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  30242. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  30243. X509_STORE *store;
  30244. X509_STORE *store_cpy;
  30245. SSL_CTX *ctx;
  30246. X509_OBJECT *obj;
  30247. STACK_OF(X509_OBJECT) *objs;
  30248. int i;
  30249. printf(testingFmt, "wolfSSL_X509_STORE_get0_objects");
  30250. /* Setup store */
  30251. #ifndef NO_WOLFSSL_SERVER
  30252. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30253. #else
  30254. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30255. #endif
  30256. AssertNotNull(store_cpy = X509_STORE_new());
  30257. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  30258. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  30259. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  30260. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  30261. #ifdef HAVE_CRL
  30262. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  30263. #endif
  30264. /* Store ready */
  30265. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  30266. AssertNotNull(objs = X509_STORE_get0_objects(store));
  30267. #ifdef HAVE_CRL
  30268. #ifdef WOLFSSL_SIGNER_DER_CERT
  30269. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  30270. #else
  30271. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  30272. #endif
  30273. #else
  30274. #ifdef WOLFSSL_SIGNER_DER_CERT
  30275. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  30276. #else
  30277. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  30278. #endif
  30279. #endif
  30280. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  30281. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  30282. switch (X509_OBJECT_get_type(obj)) {
  30283. case X509_LU_X509:
  30284. AssertNotNull(X509_OBJECT_get0_X509(obj));
  30285. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  30286. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  30287. break;
  30288. case X509_LU_CRL:
  30289. #ifdef HAVE_CRL
  30290. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  30291. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  30292. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  30293. break;
  30294. #endif
  30295. case X509_LU_NONE:
  30296. default:
  30297. Fail(("X509_OBJECT_get_type should return x509 or crl "
  30298. "(when built with crl support)"),
  30299. ("Unrecognized X509_OBJECT type or none"));
  30300. }
  30301. }
  30302. X509_STORE_free(store_cpy);
  30303. SSL_CTX_free(ctx);
  30304. printf(resultFmt, passed);
  30305. #endif
  30306. return 0;
  30307. }
  30308. static int test_wolfSSL_BN(void)
  30309. {
  30310. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  30311. BIGNUM* a;
  30312. BIGNUM* b;
  30313. BIGNUM* c;
  30314. BIGNUM* d;
  30315. ASN1_INTEGER* ai;
  30316. printf(testingFmt, "wolfSSL_BN()");
  30317. AssertNotNull(b = BN_new());
  30318. AssertNotNull(c = BN_new());
  30319. AssertNotNull(d = BN_new());
  30320. ai = ASN1_INTEGER_new();
  30321. AssertNotNull(ai);
  30322. /* at the moment hard setting since no set function */
  30323. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  30324. ai->data[1] = 0x01; /* length of integer */
  30325. ai->data[2] = 0x03;
  30326. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  30327. ASN1_INTEGER_free(ai);
  30328. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  30329. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  30330. /* a + 3 = */
  30331. AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
  30332. AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
  30333. /* check result 3 + 3*/
  30334. AssertIntEQ(BN_get_word(a), 6);
  30335. /* set a back to 3 */
  30336. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  30337. /* a - 3 = */
  30338. AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
  30339. AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
  30340. /* check result 3 - 3*/
  30341. AssertIntEQ(BN_get_word(a), 0);
  30342. /* set a back to 3 */
  30343. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  30344. /* a^b mod c = */
  30345. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  30346. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  30347. /* check result 3^2 mod 5 */
  30348. AssertIntEQ(BN_get_word(d), 4);
  30349. /* a*b = */
  30350. AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
  30351. AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
  30352. /* check result 3*2 */
  30353. AssertIntEQ(BN_get_word(d), 6);
  30354. /* c/b => db + a */
  30355. AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
  30356. AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
  30357. /* check result 5/2 */
  30358. AssertIntEQ(BN_get_word(d), 2); /* check quotient */
  30359. AssertIntEQ(BN_get_word(a), 1); /* check remainder */
  30360. /* set a back to 3 */
  30361. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  30362. /* a*b mod c = */
  30363. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  30364. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  30365. /* check result 3*2 mod 5 */
  30366. AssertIntEQ(BN_get_word(d), 1);
  30367. AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
  30368. AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
  30369. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  30370. /* gcd of a and b */
  30371. AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
  30372. AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
  30373. /* check result gcd(16, 24) */
  30374. AssertIntEQ(BN_get_word(d), 8);
  30375. #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
  30376. AssertIntEQ(BN_set_word(a, 1 << 6), SSL_SUCCESS);
  30377. AssertIntEQ(BN_rshift(b, a, 6), SSL_SUCCESS);
  30378. AssertIntEQ(BN_is_zero(b), 0);
  30379. AssertIntEQ(BN_rshift(b, a, 7), SSL_SUCCESS);
  30380. AssertIntEQ(BN_is_zero(b), 1);
  30381. AssertIntEQ(BN_rshift1(b, a), SSL_SUCCESS);
  30382. AssertIntEQ(BN_is_zero(b), 0);
  30383. /* set b back to 2 */
  30384. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  30385. {
  30386. /* BN_mod_inverse test */
  30387. BIGNUM *r = BN_new();
  30388. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  30389. AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
  30390. BN_free(val);
  30391. }
  30392. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  30393. defined(WOLFSSL_SP_INT_NEGATIVE))
  30394. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  30395. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  30396. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  30397. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  30398. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  30399. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  30400. {
  30401. char* ret;
  30402. AssertNotNull(ret = BN_bn2dec(c));
  30403. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  30404. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  30405. }
  30406. #endif
  30407. AssertIntEQ(BN_get_word(c), 4);
  30408. #endif
  30409. BN_free(a);
  30410. BN_free(b);
  30411. BN_free(c);
  30412. BN_clear_free(d);
  30413. /* check that converting NULL and the null string returns an error */
  30414. a = NULL;
  30415. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  30416. AssertIntLE(BN_hex2bn(&a, ""), 0);
  30417. AssertNull(a);
  30418. /* check that getting a string and a bin of the same number are equal,
  30419. * and that the comparison works EQ, LT and GT */
  30420. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  30421. AssertNotNull(b = BN_new());
  30422. AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
  30423. AssertNotNull(c = BN_new());
  30424. AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
  30425. AssertIntEQ(BN_cmp(a, b), 0);
  30426. AssertIntLT(BN_cmp(a, c), 0);
  30427. AssertIntGT(BN_cmp(c, b), 0);
  30428. AssertIntEQ(BN_set_word(a, 0), 1);
  30429. AssertIntEQ(BN_is_zero(a), 1);
  30430. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  30431. AssertIntEQ(BN_is_zero(a), 0);
  30432. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  30433. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  30434. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  30435. AssertIntEQ(BN_is_zero(a), 1);
  30436. BN_free(a);
  30437. BN_free(b);
  30438. BN_free(c);
  30439. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  30440. {
  30441. BIGNUM *ap;
  30442. BIGNUM bv;
  30443. BIGNUM cv;
  30444. BIGNUM dv;
  30445. AssertNotNull(ap = BN_new());
  30446. BN_init(&bv);
  30447. BN_init(&cv);
  30448. BN_init(&dv);
  30449. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  30450. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  30451. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  30452. /* a^b mod c = */
  30453. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  30454. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  30455. /* check result 3^2 mod 5 */
  30456. AssertIntEQ(BN_get_word(&dv), 4);
  30457. /* a*b mod c = */
  30458. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  30459. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  30460. /* check result 3*2 mod 5 */
  30461. AssertIntEQ(BN_get_word(&dv), 1);
  30462. BN_free(ap);
  30463. }
  30464. #endif
  30465. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  30466. AssertNotNull(a = BN_new());
  30467. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
  30468. SSL_SUCCESS);
  30469. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
  30470. BN_free(a);
  30471. #endif
  30472. printf(resultFmt, passed);
  30473. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  30474. return 0;
  30475. }
  30476. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30477. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30478. #define TEST_ARG 0x1234
  30479. static void msg_cb(int write_p, int version, int content_type,
  30480. const void *buf, size_t len, SSL *ssl, void *arg)
  30481. {
  30482. (void)write_p;
  30483. (void)version;
  30484. (void)content_type;
  30485. (void)buf;
  30486. (void)len;
  30487. (void)ssl;
  30488. AssertTrue(arg == (void*)TEST_ARG);
  30489. }
  30490. #endif
  30491. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30492. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  30493. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  30494. !defined(NO_WOLFSSL_SERVER)
  30495. #ifndef SINGLE_THREADED
  30496. #if defined(SESSION_CERTS)
  30497. #include "wolfssl/internal.h"
  30498. #endif
  30499. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  30500. {
  30501. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  30502. STACK_OF(X509)* sk;
  30503. X509* x509;
  30504. int i, num;
  30505. BIO* bio;
  30506. #endif
  30507. (void) ctx;
  30508. printf("\n===== msgcb called ====\n");
  30509. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  30510. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  30511. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  30512. AssertNotNull(SSL_get0_verified_chain(ssl));
  30513. #else
  30514. (void) ssl;
  30515. #endif
  30516. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  30517. bio = BIO_new(BIO_s_file());
  30518. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  30519. sk = SSL_get_peer_cert_chain(ssl);
  30520. AssertNotNull(sk);
  30521. if (!sk) {
  30522. BIO_free(bio);
  30523. return SSL_FAILURE;
  30524. }
  30525. num = sk_X509_num(sk);
  30526. AssertTrue(num > 0);
  30527. for (i = 0; i < num; i++) {
  30528. x509 = sk_X509_value(sk,i);
  30529. AssertNotNull(x509);
  30530. if (!x509)
  30531. break;
  30532. printf("Certificate at index [%d] = :\n",i);
  30533. X509_print(bio,x509);
  30534. printf("\n\n");
  30535. }
  30536. BIO_free(bio);
  30537. #endif
  30538. return SSL_SUCCESS;
  30539. }
  30540. #endif
  30541. #endif
  30542. static int test_wolfSSL_msgCb(void)
  30543. {
  30544. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30545. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  30546. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  30547. !defined(NO_WOLFSSL_SERVER)
  30548. tcp_ready ready;
  30549. func_args client_args;
  30550. func_args server_args;
  30551. #ifndef SINGLE_THREADED
  30552. THREAD_TYPE serverThread;
  30553. #endif
  30554. callback_functions client_cb;
  30555. callback_functions server_cb;
  30556. printf(testingFmt, "test_wolfSSL_msgCb");
  30557. /* create a failed connection and inspect the error */
  30558. #ifdef WOLFSSL_TIRTOS
  30559. fdOpenSession(Task_self());
  30560. #endif
  30561. XMEMSET(&client_args, 0, sizeof(func_args));
  30562. XMEMSET(&server_args, 0, sizeof(func_args));
  30563. StartTCP();
  30564. InitTcpReady(&ready);
  30565. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30566. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30567. #ifndef WOLFSSL_NO_TLS12
  30568. client_cb.method = wolfTLSv1_2_client_method;
  30569. server_cb.method = wolfTLSv1_2_server_method;
  30570. #else
  30571. client_cb.method = wolfTLSv1_3_client_method;
  30572. server_cb.method = wolfTLSv1_3_server_method;
  30573. #endif
  30574. server_args.signal = &ready;
  30575. server_args.callbacks = &server_cb;
  30576. client_args.signal = &ready;
  30577. client_args.callbacks = &client_cb;
  30578. client_args.return_code = TEST_FAIL;
  30579. #ifndef SINGLE_THREADED
  30580. start_thread(test_server_nofail, &server_args, &serverThread);
  30581. wait_tcp_ready(&server_args);
  30582. test_client_nofail(&client_args, msgCb);
  30583. join_thread(serverThread);
  30584. #endif
  30585. FreeTcpReady(&ready);
  30586. #ifndef SINGLE_THREADED
  30587. AssertTrue(client_args.return_code);
  30588. AssertTrue(server_args.return_code);
  30589. #endif
  30590. #ifdef WOLFSSL_TIRTOS
  30591. fdOpenSession(Task_self());
  30592. #endif
  30593. printf(resultFmt, passed);
  30594. #endif
  30595. return 0;
  30596. }
  30597. static int test_wolfSSL_either_side(void)
  30598. {
  30599. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  30600. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30601. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  30602. tcp_ready ready;
  30603. func_args client_args;
  30604. func_args server_args;
  30605. #ifndef SINGLE_THREADED
  30606. THREAD_TYPE serverThread;
  30607. #endif
  30608. callback_functions client_cb;
  30609. callback_functions server_cb;
  30610. printf(testingFmt, "test_wolfSSL_either_side");
  30611. /* create a failed connection and inspect the error */
  30612. #ifdef WOLFSSL_TIRTOS
  30613. fdOpenSession(Task_self());
  30614. #endif
  30615. XMEMSET(&client_args, 0, sizeof(func_args));
  30616. XMEMSET(&server_args, 0, sizeof(func_args));
  30617. StartTCP();
  30618. InitTcpReady(&ready);
  30619. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30620. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30621. /* Use different CTX for client and server */
  30622. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  30623. AssertNotNull(client_cb.ctx);
  30624. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  30625. AssertNotNull(server_cb.ctx);
  30626. /* we are responsible for free'ing WOLFSSL_CTX */
  30627. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  30628. server_args.signal = &ready;
  30629. server_args.callbacks = &server_cb;
  30630. client_args.signal = &ready;
  30631. client_args.callbacks = &client_cb;
  30632. client_args.return_code = TEST_FAIL;
  30633. #ifndef SINGLE_THREADED
  30634. start_thread(test_server_nofail, &server_args, &serverThread);
  30635. wait_tcp_ready(&server_args);
  30636. test_client_nofail(&client_args, NULL);
  30637. join_thread(serverThread);
  30638. #endif
  30639. wolfSSL_CTX_free(client_cb.ctx);
  30640. wolfSSL_CTX_free(server_cb.ctx);
  30641. FreeTcpReady(&ready);
  30642. #ifndef SINGLE_THREADED
  30643. AssertTrue(client_args.return_code);
  30644. AssertTrue(server_args.return_code);
  30645. #endif
  30646. #ifdef WOLFSSL_TIRTOS
  30647. fdOpenSession(Task_self());
  30648. #endif
  30649. printf(resultFmt, passed);
  30650. #endif
  30651. return 0;
  30652. }
  30653. static int test_wolfSSL_DTLS_either_side(void)
  30654. {
  30655. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  30656. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30657. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  30658. defined(WOLFSSL_DTLS)
  30659. tcp_ready ready;
  30660. func_args client_args;
  30661. func_args server_args;
  30662. #ifndef SINGLE_THREADED
  30663. THREAD_TYPE serverThread;
  30664. #endif
  30665. callback_functions client_cb;
  30666. callback_functions server_cb;
  30667. printf(testingFmt, "test_wolfSSL_DTLS_either_side");
  30668. /* create a failed connection and inspect the error */
  30669. #ifdef WOLFSSL_TIRTOS
  30670. fdOpenSession(Task_self());
  30671. #endif
  30672. XMEMSET(&client_args, 0, sizeof(func_args));
  30673. XMEMSET(&server_args, 0, sizeof(func_args));
  30674. StartTCP();
  30675. InitTcpReady(&ready);
  30676. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30677. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30678. /* Use different CTX for client and server */
  30679. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  30680. AssertNotNull(client_cb.ctx);
  30681. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  30682. AssertNotNull(server_cb.ctx);
  30683. /* we are responsible for free'ing WOLFSSL_CTX */
  30684. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  30685. server_args.signal = &ready;
  30686. server_args.callbacks = &server_cb;
  30687. client_args.signal = &ready;
  30688. client_args.callbacks = &client_cb;
  30689. client_args.return_code = TEST_FAIL;
  30690. #ifndef SINGLE_THREADED
  30691. start_thread(test_server_nofail, &server_args, &serverThread);
  30692. wait_tcp_ready(&server_args);
  30693. test_client_nofail(&client_args, NULL);
  30694. join_thread(serverThread);
  30695. #endif
  30696. wolfSSL_CTX_free(client_cb.ctx);
  30697. wolfSSL_CTX_free(server_cb.ctx);
  30698. FreeTcpReady(&ready);
  30699. #ifndef SINGLE_THREADED
  30700. AssertTrue(client_args.return_code);
  30701. AssertTrue(server_args.return_code);
  30702. #endif
  30703. #ifdef WOLFSSL_TIRTOS
  30704. fdOpenSession(Task_self());
  30705. #endif
  30706. printf(resultFmt, passed);
  30707. #endif
  30708. return 0;
  30709. }
  30710. static int test_generate_cookie(void)
  30711. {
  30712. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  30713. SSL_CTX* ctx;
  30714. SSL* ssl;
  30715. byte buf[FOURK_BUF] = {0};
  30716. printf(testingFmt, "test_generate_cookie");
  30717. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  30718. AssertNotNull(ssl = SSL_new(ctx));
  30719. /* Test unconnected */
  30720. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  30721. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  30722. wolfSSL_SetCookieCtx(ssl, ctx);
  30723. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  30724. AssertNull(wolfSSL_GetCookieCtx(NULL));
  30725. SSL_free(ssl);
  30726. SSL_CTX_free(ctx);
  30727. printf(resultFmt, passed);
  30728. #endif
  30729. return 0;
  30730. }
  30731. static int test_wolfSSL_set_options(void)
  30732. {
  30733. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30734. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30735. WOLFSSL* ssl;
  30736. WOLFSSL_CTX* ctx;
  30737. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  30738. char appData[] = "extra msg";
  30739. #endif
  30740. #ifdef OPENSSL_EXTRA
  30741. unsigned char protos[] = {
  30742. 7, 't', 'l', 's', '/', '1', '.', '2',
  30743. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  30744. };
  30745. unsigned int len = sizeof(protos);
  30746. void *arg = (void *)TEST_ARG;
  30747. #endif
  30748. printf(testingFmt, "wolfSSL_set_options()");
  30749. #ifndef NO_WOLFSSL_SERVER
  30750. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  30751. #else
  30752. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30753. #endif
  30754. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30755. WOLFSSL_FILETYPE_PEM));
  30756. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30757. WOLFSSL_FILETYPE_PEM));
  30758. AssertTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  30759. == WOLFSSL_OP_NO_TLSv1);
  30760. AssertTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  30761. AssertIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  30762. WOLFSSL_OP_NO_SSLv2)), 0);
  30763. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  30764. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  30765. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  30766. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  30767. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  30768. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  30769. AssertFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  30770. WOLFSSL_OP_NO_COMPRESSION));
  30771. wolfSSL_CTX_free(ctx);
  30772. #ifndef NO_WOLFSSL_SERVER
  30773. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  30774. AssertNotNull(ctx);
  30775. #else
  30776. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30777. AssertNotNull(ctx);
  30778. #endif
  30779. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30780. WOLFSSL_FILETYPE_PEM));
  30781. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30782. WOLFSSL_FILETYPE_PEM));
  30783. #ifdef OPENSSL_EXTRA
  30784. AssertTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  30785. #endif
  30786. AssertNotNull(ssl = wolfSSL_new(ctx));
  30787. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  30788. #ifdef HAVE_EX_DATA
  30789. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  30790. AssertNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  30791. if (ssl) {
  30792. AssertIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  30793. appData, sizeof(appData)), 0);
  30794. }
  30795. #else
  30796. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  30797. AssertNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  30798. #endif
  30799. #endif
  30800. AssertTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  30801. WOLFSSL_OP_NO_TLSv1);
  30802. AssertTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  30803. AssertIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  30804. WOLFSSL_OP_NO_SSLv2)), 0);
  30805. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  30806. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  30807. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  30808. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  30809. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  30810. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  30811. #ifdef OPENSSL_EXTRA
  30812. AssertNull((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  30813. WOLFSSL_OP_NO_COMPRESSION));
  30814. #endif
  30815. #ifdef OPENSSL_EXTRA
  30816. AssertTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  30817. wolfSSL_set_msg_callback_arg(ssl, arg);
  30818. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30819. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  30820. #else
  30821. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  30822. #endif
  30823. #endif
  30824. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  30825. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  30826. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  30827. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  30828. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30829. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  30830. #else
  30831. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  30832. #endif
  30833. #endif /* HAVE_ALPN && !NO_BIO */
  30834. #endif
  30835. wolfSSL_free(ssl);
  30836. wolfSSL_CTX_free(ctx);
  30837. printf(resultFmt, passed);
  30838. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30839. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30840. return 0;
  30841. }
  30842. static int test_wolfSSL_sk_SSL_CIPHER(void)
  30843. {
  30844. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30845. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30846. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30847. SSL* ssl;
  30848. SSL_CTX* ctx;
  30849. STACK_OF(SSL_CIPHER) *sk, *dupSk;
  30850. printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
  30851. #ifndef NO_WOLFSSL_SERVER
  30852. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30853. #else
  30854. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30855. #endif
  30856. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30857. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30858. AssertNotNull(ssl = SSL_new(ctx));
  30859. AssertNotNull(sk = SSL_get_ciphers(ssl));
  30860. AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  30861. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  30862. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  30863. /* error case because connection has not been established yet */
  30864. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  30865. sk_SSL_CIPHER_free(dupSk);
  30866. /* sk is pointer to internal struct that should be free'd in SSL_free */
  30867. SSL_free(ssl);
  30868. SSL_CTX_free(ctx);
  30869. printf(resultFmt, passed);
  30870. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30871. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30872. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30873. return 0;
  30874. }
  30875. static int test_wolfSSL_set1_curves_list(void)
  30876. {
  30877. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  30878. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30879. SSL* ssl = NULL;
  30880. SSL_CTX* ctx = NULL;
  30881. #ifndef NO_WOLFSSL_SERVER
  30882. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30883. #else
  30884. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30885. #endif
  30886. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30887. SSL_FILETYPE_PEM));
  30888. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  30889. AssertNotNull(ssl = SSL_new(ctx));
  30890. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  30891. #ifdef HAVE_ECC
  30892. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  30893. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  30894. #endif
  30895. #ifdef HAVE_CURVE25519
  30896. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  30897. #else
  30898. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  30899. #endif
  30900. #ifdef HAVE_CURVE448
  30901. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  30902. #else
  30903. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  30904. #endif
  30905. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  30906. #ifdef HAVE_ECC
  30907. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  30908. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  30909. #endif
  30910. #ifdef HAVE_CURVE25519
  30911. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  30912. #else
  30913. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  30914. #endif
  30915. #ifdef HAVE_CURVE448
  30916. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  30917. #else
  30918. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  30919. #endif
  30920. SSL_free(ssl);
  30921. SSL_CTX_free(ctx);
  30922. printf(resultFmt, passed);
  30923. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30924. #endif
  30925. return 0;
  30926. }
  30927. static int test_wolfSSL_set1_sigalgs_list(void)
  30928. {
  30929. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  30930. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30931. SSL* ssl;
  30932. SSL_CTX* ctx;
  30933. #ifndef NO_WOLFSSL_SERVER
  30934. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30935. #else
  30936. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30937. #endif
  30938. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30939. SSL_FILETYPE_PEM));
  30940. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30941. AssertNotNull(ssl = SSL_new(ctx));
  30942. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  30943. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  30944. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  30945. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  30946. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  30947. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  30948. #ifndef NO_RSA
  30949. #ifndef NO_SHA256
  30950. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  30951. WOLFSSL_FAILURE);
  30952. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  30953. WOLFSSL_FAILURE);
  30954. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  30955. WOLFSSL_SUCCESS);
  30956. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  30957. WOLFSSL_SUCCESS);
  30958. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  30959. WOLFSSL_FAILURE);
  30960. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  30961. WOLFSSL_FAILURE);
  30962. #ifdef WC_RSA_PSS
  30963. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  30964. WOLFSSL_SUCCESS);
  30965. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  30966. WOLFSSL_SUCCESS);
  30967. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  30968. WOLFSSL_SUCCESS);
  30969. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  30970. WOLFSSL_SUCCESS);
  30971. #endif
  30972. #ifdef WOLFSSL_SHA512
  30973. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  30974. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  30975. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  30976. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  30977. #elif defined(WOLFSSL_SHA384)
  30978. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  30979. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  30980. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  30981. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  30982. #endif
  30983. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  30984. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  30985. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  30986. WOLFSSL_FAILURE);
  30987. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  30988. WOLFSSL_FAILURE);
  30989. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  30990. WOLFSSL_FAILURE);
  30991. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  30992. WOLFSSL_FAILURE);
  30993. #endif
  30994. #endif
  30995. #ifdef HAVE_ECC
  30996. #ifndef NO_SHA256
  30997. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  30998. WOLFSSL_SUCCESS);
  30999. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  31000. #ifdef WOLFSSL_SHA512
  31001. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31002. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  31003. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31004. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  31005. #elif defined(WOLFSSL_SHA384)
  31006. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31007. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  31008. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31009. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  31010. #endif
  31011. #endif
  31012. #endif
  31013. #ifdef HAVE_ED25519
  31014. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  31015. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  31016. #endif
  31017. #ifdef HAVE_ED448
  31018. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  31019. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  31020. #endif
  31021. #ifndef NO_DSA
  31022. #ifndef NO_SHA256
  31023. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  31024. WOLFSSL_SUCCESS);
  31025. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  31026. WOLFSSL_SUCCESS);
  31027. #endif
  31028. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  31029. defined(WOLFSSL_ALLOW_TLS_SHA1))
  31030. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  31031. WOLFSSL_SUCCESS);
  31032. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  31033. WOLFSSL_SUCCESS);
  31034. #endif
  31035. #endif
  31036. SSL_free(ssl);
  31037. SSL_CTX_free(ctx);
  31038. printf(resultFmt, passed);
  31039. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31040. #endif
  31041. return 0;
  31042. }
  31043. /* Testing wolfSSL_set_tlsext_status_type function.
  31044. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  31045. */
  31046. static int test_wolfSSL_set_tlsext_status_type(void){
  31047. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  31048. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  31049. SSL* ssl;
  31050. SSL_CTX* ctx;
  31051. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  31052. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31053. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  31054. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31055. AssertNotNull(ssl = SSL_new(ctx));
  31056. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  31057. SSL_SUCCESS);
  31058. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  31059. SSL_free(ssl);
  31060. SSL_CTX_free(ctx);
  31061. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  31062. return 0;
  31063. }
  31064. #ifndef NO_BIO
  31065. static int test_wolfSSL_PEM_read_bio(void)
  31066. {
  31067. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31068. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31069. byte buff[6000];
  31070. XFILE f;
  31071. int bytes;
  31072. X509* x509;
  31073. BIO* bio = NULL;
  31074. BUF_MEM* buf;
  31075. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  31076. f = XFOPEN(cliCertFile, "rb");
  31077. AssertTrue((f != XBADFILE));
  31078. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  31079. XFCLOSE(f);
  31080. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  31081. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31082. AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  31083. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  31084. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  31085. /* BIO should return the set EOF value */
  31086. AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  31087. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  31088. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  31089. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  31090. BIO_free(bio);
  31091. BUF_MEM_free(buf);
  31092. X509_free(x509);
  31093. printf(resultFmt, passed);
  31094. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31095. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31096. return 0;
  31097. }
  31098. #if defined(OPENSSL_EXTRA)
  31099. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  31100. long argl, long ret)
  31101. {
  31102. (void)bio;
  31103. (void)cmd;
  31104. (void)argp;
  31105. (void)argi;
  31106. (void)argl;
  31107. return ret;
  31108. }
  31109. #endif
  31110. static int test_wolfSSL_BIO(void)
  31111. {
  31112. #if defined(OPENSSL_EXTRA)
  31113. const unsigned char* p;
  31114. byte buff[20];
  31115. BIO* bio1;
  31116. BIO* bio2;
  31117. BIO* bio3;
  31118. char* bufPt;
  31119. int i;
  31120. printf(testingFmt, "wolfSSL_BIO()");
  31121. for (i = 0; i < 20; i++) {
  31122. buff[i] = i;
  31123. }
  31124. /* test BIO_free with NULL */
  31125. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  31126. /* Creating and testing type BIO_s_bio */
  31127. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  31128. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  31129. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  31130. /* read/write before set up */
  31131. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  31132. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  31133. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  31134. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  31135. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  31136. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  31137. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  31138. XMEMCPY(bufPt, buff, 10);
  31139. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  31140. /* write buffer full */
  31141. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  31142. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  31143. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  31144. /* write the other direction with pair */
  31145. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  31146. XMEMCPY(bufPt, buff, 8);
  31147. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  31148. /* try read */
  31149. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  31150. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  31151. /* try read using ctrl function */
  31152. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  31153. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  31154. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  31155. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  31156. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  31157. for (i = 0; i < 20; i++) {
  31158. AssertIntEQ((int)bufPt[i], i);
  31159. }
  31160. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  31161. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  31162. for (i = 0; i < 8; i++) {
  31163. AssertIntEQ((int)bufPt[i], i);
  31164. }
  31165. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  31166. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  31167. /* new pair */
  31168. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  31169. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  31170. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  31171. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  31172. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  31173. /* test wrap around... */
  31174. AssertIntEQ(BIO_reset(bio1), 0);
  31175. AssertIntEQ(BIO_reset(bio3), 0);
  31176. /* fill write buffer, read only small amount then write again */
  31177. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  31178. XMEMCPY(bufPt, buff, 20);
  31179. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  31180. for (i = 0; i < 4; i++) {
  31181. AssertIntEQ(bufPt[i], i);
  31182. }
  31183. /* try writing over read index */
  31184. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  31185. XMEMSET(bufPt, 0, 4);
  31186. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  31187. /* read and write 0 bytes */
  31188. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  31189. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  31190. /* should read only to end of write buffer then need to read again */
  31191. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  31192. for (i = 0; i < 16; i++) {
  31193. AssertIntEQ(bufPt[i], buff[4 + i]);
  31194. }
  31195. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  31196. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  31197. for (i = 0; i < 4; i++) {
  31198. AssertIntEQ(bufPt[i], 0);
  31199. }
  31200. /* read index should not have advanced with nread0 */
  31201. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  31202. for (i = 0; i < 4; i++) {
  31203. AssertIntEQ(bufPt[i], 0);
  31204. }
  31205. /* write and fill up buffer checking reset of index state */
  31206. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  31207. XMEMCPY(bufPt, buff, 20);
  31208. /* test reset on data in bio1 write buffer */
  31209. AssertIntEQ(BIO_reset(bio1), 0);
  31210. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  31211. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  31212. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  31213. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  31214. AssertNotNull(p);
  31215. XMEMCPY(bufPt, buff, 20);
  31216. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  31217. for (i = 0; i < 6; i++) {
  31218. AssertIntEQ(bufPt[i], i);
  31219. }
  31220. /* test case of writing twice with offset read index */
  31221. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  31222. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  31223. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  31224. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  31225. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  31226. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  31227. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  31228. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  31229. BIO_free(bio1);
  31230. BIO_free(bio3);
  31231. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  31232. {
  31233. BIO* bioA = NULL;
  31234. BIO* bioB = NULL;
  31235. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  31236. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  31237. BIO_free(bioA);
  31238. bioA = NULL;
  31239. BIO_free(bioB);
  31240. bioB = NULL;
  31241. }
  31242. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  31243. /* BIOs with file pointers */
  31244. #if !defined(NO_FILESYSTEM)
  31245. {
  31246. XFILE f1;
  31247. XFILE f2;
  31248. BIO* f_bio1;
  31249. BIO* f_bio2;
  31250. unsigned char cert[300];
  31251. char testFile[] = "tests/bio_write_test.txt";
  31252. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  31253. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  31254. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  31255. /* Failure due to wrong BIO type */
  31256. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  31257. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  31258. f1 = XFOPEN(svrCertFile, "rwb");
  31259. AssertTrue((f1 != XBADFILE));
  31260. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  31261. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  31262. WOLFSSL_SUCCESS);
  31263. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  31264. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  31265. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  31266. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  31267. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  31268. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  31269. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  31270. AssertIntEQ(BIO_reset(f_bio2), 0);
  31271. AssertIntEQ(BIO_tell(NULL),-1);
  31272. AssertIntEQ(BIO_tell(f_bio2),0);
  31273. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  31274. AssertIntEQ(BIO_tell(f_bio2),4);
  31275. BIO_free(f_bio1);
  31276. BIO_free(f_bio2);
  31277. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  31278. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  31279. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  31280. BIO_free(f_bio1);
  31281. }
  31282. #endif /* !defined(NO_FILESYSTEM) */
  31283. /* BIO info callback */
  31284. {
  31285. const char* testArg = "test";
  31286. BIO* cb_bio;
  31287. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  31288. BIO_set_callback(cb_bio, bioCallback);
  31289. AssertNotNull(BIO_get_callback(cb_bio));
  31290. BIO_set_callback(cb_bio, NULL);
  31291. AssertNull(BIO_get_callback(cb_bio));
  31292. BIO_set_callback_arg(cb_bio, (char*)testArg);
  31293. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  31294. AssertNull(BIO_get_callback_arg(NULL));
  31295. BIO_free(cb_bio);
  31296. }
  31297. /* BIO_vfree */
  31298. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  31299. BIO_vfree(NULL);
  31300. BIO_vfree(bio1);
  31301. printf(resultFmt, passed);
  31302. #endif
  31303. return 0;
  31304. }
  31305. #endif /* !NO_BIO */
  31306. static int test_wolfSSL_ASN1_STRING(void)
  31307. {
  31308. #if defined(OPENSSL_EXTRA)
  31309. ASN1_STRING* str = NULL;
  31310. const char data[] = "hello wolfSSL";
  31311. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  31312. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  31313. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  31314. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  31315. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  31316. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  31317. ASN1_STRING_free(str);
  31318. printf(resultFmt, passed);
  31319. #endif
  31320. return 0;
  31321. }
  31322. static int test_wolfSSL_ASN1_BIT_STRING(void)
  31323. {
  31324. #ifdef OPENSSL_ALL
  31325. ASN1_BIT_STRING* str;
  31326. printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
  31327. AssertNotNull(str = ASN1_BIT_STRING_new());
  31328. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  31329. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  31330. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  31331. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  31332. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  31333. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  31334. ASN1_BIT_STRING_free(str);
  31335. printf(resultFmt, passed);
  31336. #endif
  31337. return 0;
  31338. }
  31339. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  31340. {
  31341. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  31342. BIO *bio, *out;
  31343. ASN1_INTEGER* ai;
  31344. char buf[] = "123456\n12345\n112345678912345678901234567890\n";
  31345. char tmp[1024];
  31346. int tmpSz;
  31347. const char expected1[] = "123456";
  31348. const char expected2[] = "112345678912345678901234567890";
  31349. printf(testingFmt, "test_wolfSSL_a2i_ASN1_INTEGER()");
  31350. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  31351. AssertNotNull(out = BIO_new(BIO_s_mem()));
  31352. AssertNotNull(ai = ASN1_INTEGER_new());
  31353. /* read first line */
  31354. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  31355. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  31356. XMEMSET(tmp, 0, 1024);
  31357. tmpSz = BIO_read(out, tmp, 1024);
  31358. AssertIntEQ(tmpSz, 6);
  31359. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  31360. /* fail on second line (not % 2) */
  31361. AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  31362. /* read 3rd long line */
  31363. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  31364. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  31365. XMEMSET(tmp, 0, 1024);
  31366. tmpSz = BIO_read(out, tmp, 1024);
  31367. AssertIntEQ(tmpSz, 30);
  31368. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  31369. BIO_free(out);
  31370. BIO_free(bio);
  31371. ASN1_INTEGER_free(ai);
  31372. printf(resultFmt, passed);
  31373. #endif
  31374. return 0;
  31375. }
  31376. static int test_wolfSSL_a2i_IPADDRESS(void)
  31377. {
  31378. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  31379. const unsigned char* data;
  31380. int dataSz = 0;
  31381. ASN1_OCTET_STRING *st;
  31382. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  31383. const unsigned char ipv6_exp[] = {
  31384. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  31385. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  31386. };
  31387. const unsigned char ipv6_home[] = {
  31388. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31389. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  31390. };
  31391. printf(testingFmt, "test_wolfSSL_a2i_IPADDRESS()");
  31392. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  31393. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  31394. data = ASN1_STRING_get0_data(st);
  31395. dataSz = ASN1_STRING_length(st);
  31396. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  31397. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  31398. ASN1_STRING_free(st);
  31399. AssertNotNull(st = a2i_IPADDRESS("::1"));
  31400. data = ASN1_STRING_get0_data(st);
  31401. dataSz = ASN1_STRING_length(st);
  31402. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  31403. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  31404. ASN1_STRING_free(st);
  31405. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  31406. data = ASN1_STRING_get0_data(st);
  31407. dataSz = ASN1_STRING_length(st);
  31408. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  31409. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  31410. ASN1_STRING_free(st);
  31411. printf(resultFmt, passed);
  31412. #endif
  31413. return 0;
  31414. }
  31415. static int test_wolfSSL_DES_ecb_encrypt(void)
  31416. {
  31417. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  31418. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  31419. WOLFSSL_DES_key_schedule key;
  31420. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  31421. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  31422. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  31423. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  31424. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  31425. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  31426. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  31427. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  31428. /* Encrypt messages */
  31429. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  31430. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  31431. {
  31432. /* Decrypt messages */
  31433. int ret1 = 0;
  31434. int ret2 = 0;
  31435. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  31436. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  31437. AssertIntEQ(ret1,0);
  31438. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  31439. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  31440. AssertIntEQ(ret2,0);
  31441. }
  31442. printf(resultFmt, passed);
  31443. #endif
  31444. return 0;
  31445. }
  31446. static int test_wolfSSL_ASN1_TIME_adj(void)
  31447. {
  31448. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  31449. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  31450. const int year = 365*24*60*60;
  31451. const int day = 24*60*60;
  31452. const int hour = 60*60;
  31453. const int mini = 60;
  31454. const byte asn_utc_time = ASN_UTC_TIME;
  31455. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  31456. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  31457. #endif
  31458. WOLFSSL_ASN1_TIME *asn_time, *s;
  31459. int offset_day;
  31460. long offset_sec;
  31461. char date_str[CTC_DATE_SIZE + 1];
  31462. time_t t;
  31463. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  31464. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  31465. /* UTC notation test */
  31466. /* 2000/2/15 20:30:00 */
  31467. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  31468. offset_day = 7;
  31469. offset_sec = 45 * mini;
  31470. /* offset_sec = -45 * min;*/
  31471. AssertNotNull(asn_time =
  31472. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  31473. AssertTrue(asn_time->type == asn_utc_time);
  31474. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31475. date_str[CTC_DATE_SIZE] = '\0';
  31476. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  31477. /* negative offset */
  31478. offset_sec = -45 * mini;
  31479. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  31480. AssertTrue(asn_time->type == asn_utc_time);
  31481. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31482. date_str[CTC_DATE_SIZE] = '\0';
  31483. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  31484. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  31485. XMEMSET(date_str, 0, sizeof(date_str));
  31486. /* Generalized time will overflow time_t if not long */
  31487. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  31488. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  31489. DYNAMIC_TYPE_OPENSSL);
  31490. /* GeneralizedTime notation test */
  31491. /* 2055/03/01 09:00:00 */
  31492. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  31493. offset_day = 12;
  31494. offset_sec = 10 * mini;
  31495. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  31496. AssertTrue(asn_time->type == asn_gen_time);
  31497. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31498. date_str[CTC_DATE_SIZE] = '\0';
  31499. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  31500. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  31501. XMEMSET(date_str, 0, sizeof(date_str));
  31502. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  31503. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  31504. s = NULL;
  31505. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  31506. offset_day = 7;
  31507. offset_sec = 45 * mini;
  31508. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  31509. AssertTrue(asn_time->type == asn_utc_time);
  31510. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31511. date_str[CTC_DATE_SIZE] = '\0';
  31512. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  31513. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  31514. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  31515. AssertTrue(asn_time->type == asn_utc_time);
  31516. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31517. date_str[CTC_DATE_SIZE] = '\0';
  31518. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  31519. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  31520. printf(resultFmt, passed);
  31521. #endif
  31522. return 0;
  31523. }
  31524. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  31525. {
  31526. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  31527. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) \
  31528. && !defined(NO_ASN_TIME)
  31529. ASN1_TIME asnTime;
  31530. struct tm tm;
  31531. printf(testingFmt, "wolfSSL_ASN1_TIME_to_tm()");
  31532. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  31533. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  31534. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  31535. AssertIntEQ(tm.tm_sec, 15);
  31536. AssertIntEQ(tm.tm_min, 15);
  31537. AssertIntEQ(tm.tm_hour, 21);
  31538. AssertIntEQ(tm.tm_mday, 22);
  31539. AssertIntEQ(tm.tm_mon, 1);
  31540. AssertIntEQ(tm.tm_year, 100);
  31541. AssertIntEQ(tm.tm_isdst, 0);
  31542. #ifdef XMKTIME
  31543. AssertIntEQ(tm.tm_wday, 2);
  31544. AssertIntEQ(tm.tm_yday, 52);
  31545. #endif
  31546. printf(resultFmt, passed);
  31547. #endif
  31548. return 0;
  31549. }
  31550. static int test_wolfSSL_X509_cmp_time(void)
  31551. {
  31552. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  31553. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  31554. WOLFSSL_ASN1_TIME asn_time;
  31555. time_t t;
  31556. printf(testingFmt, "wolfSSL_X509_cmp_time()");
  31557. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  31558. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  31559. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  31560. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  31561. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  31562. printf(resultFmt, passed);
  31563. #endif
  31564. return 0;
  31565. }
  31566. static int test_wolfSSL_X509_time_adj(void)
  31567. {
  31568. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  31569. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  31570. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  31571. !defined(NO_ASN_TIME)
  31572. X509* x509;
  31573. time_t t, not_before, not_after;
  31574. printf(testingFmt, "wolfSSL_X509_time_adj()");
  31575. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  31576. client_cert_der_2048, sizeof_client_cert_der_2048,
  31577. WOLFSSL_FILETYPE_ASN1));
  31578. t = 0;
  31579. not_before = wc_Time(0);
  31580. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  31581. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  31582. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  31583. /* Check X509_gmtime_adj, too. */
  31584. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  31585. X509_free(x509);
  31586. printf(resultFmt, passed);
  31587. #endif
  31588. return 0;
  31589. }
  31590. static int test_wolfSSL_X509(void)
  31591. {
  31592. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  31593. && !defined(NO_RSA)
  31594. X509* x509;
  31595. #ifndef NO_BIO
  31596. BIO* bio;
  31597. X509_STORE_CTX* ctx;
  31598. X509_STORE* store;
  31599. #endif
  31600. char der[] = "certs/ca-cert.der";
  31601. XFILE fp;
  31602. printf(testingFmt, "wolfSSL_X509()");
  31603. AssertNotNull(x509 = X509_new());
  31604. X509_free(x509);
  31605. #ifndef NO_BIO
  31606. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  31607. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31608. #ifdef WOLFSSL_CERT_GEN
  31609. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  31610. #endif
  31611. AssertNotNull(ctx = X509_STORE_CTX_new());
  31612. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  31613. AssertNotNull(store = X509_STORE_new());
  31614. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  31615. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  31616. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  31617. X509_STORE_CTX_free(ctx);
  31618. X509_STORE_free(store);
  31619. X509_free(x509);
  31620. BIO_free(bio);
  31621. #endif
  31622. /** d2i_X509_fp test **/
  31623. fp = XFOPEN(der, "rb");
  31624. AssertTrue((fp != XBADFILE));
  31625. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  31626. AssertNotNull(x509);
  31627. X509_free(x509);
  31628. XFCLOSE(fp);
  31629. fp = XFOPEN(der, "rb");
  31630. AssertTrue((fp != XBADFILE));
  31631. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  31632. AssertNotNull(x509);
  31633. X509_free(x509);
  31634. XFCLOSE(fp);
  31635. /* X509_up_ref test */
  31636. AssertIntEQ(X509_up_ref(NULL), 0);
  31637. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  31638. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  31639. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  31640. X509_free(x509); /* refCount = 2 */
  31641. X509_free(x509); /* refCount = 1 */
  31642. X509_free(x509); /* refCount = 0, free */
  31643. printf(resultFmt, passed);
  31644. #endif
  31645. return 0;
  31646. }
  31647. static int test_wolfSSL_X509_get_ext_count(void)
  31648. {
  31649. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31650. !defined(NO_RSA)
  31651. int ret = 0;
  31652. WOLFSSL_X509* x509;
  31653. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  31654. FILE* f;
  31655. printf(testingFmt, "wolfSSL_X509_get_ext_count()");
  31656. /* NULL parameter check */
  31657. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  31658. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  31659. SSL_FILETYPE_PEM));
  31660. AssertIntEQ(X509_get_ext_count(x509), 5);
  31661. wolfSSL_X509_free(x509);
  31662. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  31663. SSL_FILETYPE_PEM));
  31664. AssertIntEQ(X509_get_ext_count(x509), 5);
  31665. wolfSSL_X509_free(x509);
  31666. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  31667. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  31668. fclose(f);
  31669. printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
  31670. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  31671. printf(resultFmt, ret == 4 ? passed : failed);
  31672. printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
  31673. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  31674. printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
  31675. wolfSSL_X509_free(x509);
  31676. printf(resultFmt, passed);
  31677. #endif
  31678. return 0;
  31679. }
  31680. static int test_wolfSSL_X509_sign2(void)
  31681. {
  31682. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31683. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  31684. defined(WOLFSSL_CERT_EXT) && \
  31685. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  31686. WOLFSSL_X509 *x509, *ca;
  31687. const unsigned char *der;
  31688. const unsigned char *pt;
  31689. WOLFSSL_EVP_PKEY *priv;
  31690. WOLFSSL_X509_NAME *name;
  31691. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  31692. int derSz;
  31693. const int year = 365*24*60*60;
  31694. const int day = 24*60*60;
  31695. const int hour = 60*60;
  31696. const int mini = 60;
  31697. time_t t;
  31698. const unsigned char expected[] = {
  31699. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xfb, 0xa0, 0x03, 0x02, 0x01,
  31700. 0x02, 0x02, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3, 0xa6,
  31701. 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c, 0x30,
  31702. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  31703. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
  31704. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03,
  31705. 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61,
  31706. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42,
  31707. 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03,
  31708. 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74,
  31709. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0a,
  31710. 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18,
  31711. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  31712. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  31713. 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  31714. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f,
  31715. 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17,
  31716. 0x0d, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  31717. 0x30, 0x5a, 0x17, 0x0d, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  31718. 0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  31719. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  31720. 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  31721. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07,
  31722. 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x15, 0x30,
  31723. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c, 0x77, 0x6f, 0x6c, 0x66,
  31724. 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  31725. 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50, 0x72, 0x6f, 0x67, 0x72,
  31726. 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32, 0x30, 0x34, 0x38, 0x31,
  31727. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77,
  31728. 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f,
  31729. 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  31730. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  31731. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82,
  31732. 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  31733. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82,
  31734. 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc3, 0x03, 0xd1, 0x2b, 0xfe,
  31735. 0x39, 0xa4, 0x32, 0x45, 0x3b, 0x53, 0xc8, 0x84, 0x2b, 0x2a, 0x7c, 0x74,
  31736. 0x9a, 0xbd, 0xaa, 0x2a, 0x52, 0x07, 0x47, 0xd6, 0xa6, 0x36, 0xb2, 0x07,
  31737. 0x32, 0x8e, 0xd0, 0xba, 0x69, 0x7b, 0xc6, 0xc3, 0x44, 0x9e, 0xd4, 0x81,
  31738. 0x48, 0xfd, 0x2d, 0x68, 0xa2, 0x8b, 0x67, 0xbb, 0xa1, 0x75, 0xc8, 0x36,
  31739. 0x2c, 0x4a, 0xd2, 0x1b, 0xf7, 0x8b, 0xba, 0xcf, 0x0d, 0xf9, 0xef, 0xec,
  31740. 0xf1, 0x81, 0x1e, 0x7b, 0x9b, 0x03, 0x47, 0x9a, 0xbf, 0x65, 0xcc, 0x7f,
  31741. 0x65, 0x24, 0x69, 0xa6, 0xe8, 0x14, 0x89, 0x5b, 0xe4, 0x34, 0xf7, 0xc5,
  31742. 0xb0, 0x14, 0x93, 0xf5, 0x67, 0x7b, 0x3a, 0x7a, 0x78, 0xe1, 0x01, 0x56,
  31743. 0x56, 0x91, 0xa6, 0x13, 0x42, 0x8d, 0xd2, 0x3c, 0x40, 0x9c, 0x4c, 0xef,
  31744. 0xd1, 0x86, 0xdf, 0x37, 0x51, 0x1b, 0x0c, 0xa1, 0x3b, 0xf5, 0xf1, 0xa3,
  31745. 0x4a, 0x35, 0xe4, 0xe1, 0xce, 0x96, 0xdf, 0x1b, 0x7e, 0xbf, 0x4e, 0x97,
  31746. 0xd0, 0x10, 0xe8, 0xa8, 0x08, 0x30, 0x81, 0xaf, 0x20, 0x0b, 0x43, 0x14,
  31747. 0xc5, 0x74, 0x67, 0xb4, 0x32, 0x82, 0x6f, 0x8d, 0x86, 0xc2, 0x88, 0x40,
  31748. 0x99, 0x36, 0x83, 0xba, 0x1e, 0x40, 0x72, 0x22, 0x17, 0xd7, 0x52, 0x65,
  31749. 0x24, 0x73, 0xb0, 0xce, 0xef, 0x19, 0xcd, 0xae, 0xff, 0x78, 0x6c, 0x7b,
  31750. 0xc0, 0x12, 0x03, 0xd4, 0x4e, 0x72, 0x0d, 0x50, 0x6d, 0x3b, 0xa3, 0x3b,
  31751. 0xa3, 0x99, 0x5e, 0x9d, 0xc8, 0xd9, 0x0c, 0x85, 0xb3, 0xd9, 0x8a, 0xd9,
  31752. 0x54, 0x26, 0xdb, 0x6d, 0xfa, 0xac, 0xbb, 0xff, 0x25, 0x4c, 0xc4, 0xd1,
  31753. 0x79, 0xf4, 0x71, 0xd3, 0x86, 0x40, 0x18, 0x13, 0xb0, 0x63, 0xb5, 0x72,
  31754. 0x4e, 0x30, 0xc4, 0x97, 0x84, 0x86, 0x2d, 0x56, 0x2f, 0xd7, 0x15, 0xf7,
  31755. 0x7f, 0xc0, 0xae, 0xf5, 0xfc, 0x5b, 0xe5, 0xfb, 0xa1, 0xba, 0xd3, 0x02,
  31756. 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x4f, 0x30, 0x82, 0x01, 0x4b,
  31757. 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  31758. 0x01, 0xff, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x15, 0x30,
  31759. 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
  31760. 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x30, 0x1d, 0x06, 0x03,
  31761. 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  31762. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  31763. 0x85, 0x65, 0xc0, 0x30, 0x81, 0xde, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04,
  31764. 0x81, 0xd6, 0x30, 0x81, 0xd3, 0x80, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  31765. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  31766. 0x85, 0x65, 0xc0, 0xa1, 0x81, 0xa4, 0xa4, 0x81, 0xa1, 0x30, 0x81, 0x9e,
  31767. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  31768. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07,
  31769. 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  31770. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61,
  31771. 0x6e, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c,
  31772. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38,
  31773. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50,
  31774. 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32,
  31775. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  31776. 0x0c, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73,
  31777. 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  31778. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e,
  31779. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63,
  31780. 0x6f, 0x6d, 0x82, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3,
  31781. 0xa6, 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c,
  31782. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  31783. 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b,
  31784. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  31785. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  31786. 0x01, 0x01, 0x00, 0xa3, 0x41, 0x43, 0x93, 0x30, 0x92, 0x98, 0xfe, 0x57,
  31787. 0xd0, 0x39, 0x7c, 0x50, 0x06, 0x50, 0x20, 0x80, 0x0e, 0x28, 0x95, 0x79,
  31788. 0xb4, 0xf1, 0x6b, 0x6a, 0xab, 0x78, 0x30, 0x93, 0x49, 0x0a, 0x6a, 0x19,
  31789. 0x09, 0xae, 0x31, 0xc6, 0x8e, 0xcc, 0x69, 0x26, 0x89, 0x37, 0xc1, 0x57,
  31790. 0x58, 0x75, 0xae, 0xbf, 0x13, 0xc8, 0xd6, 0xad, 0xd0, 0x0f, 0x57, 0xcd,
  31791. 0x32, 0xa8, 0xda, 0xa8, 0x1b, 0xbf, 0xb5, 0xcd, 0x16, 0x14, 0x56, 0x86,
  31792. 0x84, 0xb4, 0xab, 0x93, 0x52, 0x74, 0xfd, 0x96, 0x9f, 0x6d, 0xbe, 0xdb,
  31793. 0x75, 0x5e, 0x76, 0xfe, 0xa6, 0x37, 0xe5, 0x5f, 0xcb, 0x62, 0x77, 0xc7,
  31794. 0xd6, 0xcb, 0xb4, 0xf6, 0x43, 0xc8, 0x47, 0xdf, 0x12, 0x16, 0x28, 0x29,
  31795. 0x61, 0xd1, 0xdc, 0x9d, 0x37, 0x9f, 0xe5, 0x71, 0x52, 0xae, 0xb8, 0x12,
  31796. 0xec, 0x32, 0x9f, 0x03, 0x1a, 0x66, 0x98, 0xd8, 0xb0, 0x40, 0x71, 0x4c,
  31797. 0xee, 0x64, 0x15, 0x48, 0x0c, 0x5c, 0x8a, 0x47, 0x20, 0xbd, 0x07, 0xc0,
  31798. 0x30, 0xf8, 0x84, 0xe6, 0x29, 0x6d, 0xa9, 0x32, 0x53, 0x02, 0x4d, 0x3c,
  31799. 0x99, 0x6e, 0x63, 0xfe, 0x39, 0x9c, 0x05, 0xa6, 0xa0, 0x0c, 0x1e, 0x11,
  31800. 0xa4, 0x86, 0x6a, 0x89, 0x76, 0x54, 0x17, 0x68, 0x5d, 0x35, 0x9a, 0xd7,
  31801. 0x5e, 0x27, 0x0e, 0xbb, 0xba, 0x67, 0x4d, 0x62, 0x12, 0xa8, 0x46, 0x1f,
  31802. 0x0e, 0xd8, 0x7d, 0xc0, 0xae, 0x30, 0xc2, 0x45, 0x71, 0xab, 0xb1, 0xc1,
  31803. 0xfb, 0xdc, 0x03, 0x7a, 0x52, 0xe6, 0x57, 0xf9, 0x7f, 0x65, 0x6b, 0x4e,
  31804. 0x44, 0x64, 0xe8, 0x77, 0x82, 0x1c, 0xc8, 0xfa, 0x09, 0xc7, 0x2f, 0xa9,
  31805. 0x40, 0x87, 0x8e, 0x0e, 0x49, 0xc2, 0x7d, 0x97, 0x27, 0x79, 0x90, 0xc2,
  31806. 0x90, 0x13, 0xa7, 0x49, 0xb7, 0xd7, 0xc5, 0x02, 0x32, 0x4f, 0x1e, 0x34,
  31807. 0x4a, 0xa6, 0xe4, 0xbd, 0xa5, 0xc6, 0xec
  31808. };
  31809. printf(testingFmt, "wolfSSL_X509_sign2");
  31810. pt = ca_key_der_2048;
  31811. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  31812. sizeof_ca_key_der_2048));
  31813. pt = client_cert_der_2048;
  31814. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  31815. sizeof_client_cert_der_2048));
  31816. pt = ca_cert_der_2048;
  31817. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  31818. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  31819. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  31820. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  31821. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  31822. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  31823. AssertIntEQ(notAfter->length, 13);
  31824. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  31825. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  31826. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  31827. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  31828. AssertIntEQ(derSz, sizeof(expected));
  31829. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  31830. wolfSSL_X509_free(ca);
  31831. wolfSSL_X509_free(x509);
  31832. wolfSSL_EVP_PKEY_free(priv);
  31833. wolfSSL_ASN1_TIME_free(notBefore);
  31834. wolfSSL_ASN1_TIME_free(notAfter);
  31835. printf(resultFmt, passed);
  31836. #endif
  31837. return 0;
  31838. }
  31839. static int test_wolfSSL_X509_sign(void)
  31840. {
  31841. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31842. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  31843. int ret;
  31844. char *cn;
  31845. word32 cnSz;
  31846. X509_NAME *name;
  31847. X509 *x509, *ca;
  31848. DecodedCert dCert;
  31849. EVP_PKEY *pub;
  31850. EVP_PKEY *priv;
  31851. EVP_MD_CTX *mctx;
  31852. #if defined(USE_CERT_BUFFERS_1024)
  31853. const unsigned char* rsaPriv = client_key_der_1024;
  31854. const unsigned char* rsaPub = client_keypub_der_1024;
  31855. const unsigned char* certIssuer = client_cert_der_1024;
  31856. long clientKeySz = (long)sizeof_client_key_der_1024;
  31857. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  31858. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  31859. #elif defined(USE_CERT_BUFFERS_2048)
  31860. const unsigned char* rsaPriv = client_key_der_2048;
  31861. const unsigned char* rsaPub = client_keypub_der_2048;
  31862. const unsigned char* certIssuer = client_cert_der_2048;
  31863. long clientKeySz = (long)sizeof_client_key_der_2048;
  31864. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  31865. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  31866. #endif
  31867. byte sn[16];
  31868. int snSz = sizeof(sn);
  31869. printf(testingFmt, "wolfSSL_X509_sign");
  31870. /* Set X509_NAME fields */
  31871. AssertNotNull(name = X509_NAME_new());
  31872. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  31873. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  31874. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  31875. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  31876. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  31877. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  31878. /* Get private and public keys */
  31879. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  31880. clientKeySz));
  31881. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  31882. AssertNotNull(x509 = X509_new());
  31883. /* Set version 3 */
  31884. AssertIntNE(X509_set_version(x509, 2L), 0);
  31885. /* Set subject name, add pubkey, and sign certificate */
  31886. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  31887. X509_NAME_free(name);
  31888. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  31889. #ifdef WOLFSSL_ALT_NAMES
  31890. /* Add some subject alt names */
  31891. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  31892. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  31893. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31894. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  31895. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31896. "sphygmomanometer",
  31897. ASN_DNS_TYPE), SSL_SUCCESS);
  31898. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31899. "supercalifragilisticexpialidocious",
  31900. ASN_DNS_TYPE), SSL_SUCCESS);
  31901. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31902. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  31903. ASN_DNS_TYPE), SSL_SUCCESS);
  31904. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  31905. {
  31906. unsigned char ip4_type[] = {127,128,0,255};
  31907. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  31908. 0xff, 0xee, 0x99, 0x88,
  31909. 0x77, 0x66, 0x55, 0x44,
  31910. 0x00, 0x33, 0x22, 0x11};
  31911. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  31912. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  31913. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  31914. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  31915. }
  31916. #endif
  31917. #endif /* WOLFSSL_ALT_NAMES */
  31918. /* test valid sign case */
  31919. ret = X509_sign(x509, priv, EVP_sha256());
  31920. /* test valid X509_sign_ctx case */
  31921. AssertNotNull(mctx = EVP_MD_CTX_new());
  31922. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  31923. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  31924. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  31925. AssertIntEQ(X509_get_ext_count(x509), 1);
  31926. #endif
  31927. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  31928. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  31929. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  31930. #endif
  31931. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  31932. WOLFSSL_SUCCESS);
  31933. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  31934. /* Variation in size depends on ASN.1 encoding when MSB is set.
  31935. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  31936. * with the MSB set. See GenerateInteger in asn.c */
  31937. #ifndef USE_CERT_BUFFERS_1024
  31938. #ifndef WOLFSSL_ALT_NAMES
  31939. /* Valid case - size should be 798-797 with 16 byte serial number */
  31940. AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
  31941. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  31942. /* Valid case - size should be 955-956 with 16 byte serial number */
  31943. AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
  31944. #else
  31945. /* Valid case - size should be 926-927 with 16 byte serial number */
  31946. AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
  31947. #endif
  31948. #else
  31949. #ifndef WOLFSSL_ALT_NAMES
  31950. /* Valid case - size should be 537-538 with 16 byte serial number */
  31951. AssertTrue((ret == 521 + snSz) || (ret == 522 + snSz));
  31952. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  31953. /* Valid case - size should be 695-696 with 16 byte serial number */
  31954. AssertTrue((ret == 679 + snSz) || (ret == 680 + snSz));
  31955. #else
  31956. /* Valid case - size should be 666-667 with 16 byte serial number */
  31957. AssertTrue((ret == 650 + snSz) || (ret == 651 + snSz));
  31958. #endif
  31959. #endif
  31960. /* check that issuer name is as expected after signature */
  31961. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  31962. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  31963. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  31964. AssertNotNull(name = X509_get_subject_name(ca));
  31965. cnSz = X509_NAME_get_sz(name);
  31966. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  31967. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  31968. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  31969. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31970. #ifdef WOLFSSL_MULTI_ATTRIB
  31971. /* test adding multiple OU's to the signer */
  31972. AssertNotNull(name = X509_get_subject_name(ca));
  31973. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  31974. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  31975. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  31976. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  31977. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  31978. #endif
  31979. AssertNotNull(name = X509_get_subject_name(ca));
  31980. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  31981. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  31982. AssertNotNull(name = X509_get_issuer_name(x509));
  31983. cnSz = X509_NAME_get_sz(name);
  31984. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  31985. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  31986. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  31987. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  31988. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31989. FreeDecodedCert(&dCert);
  31990. /* Test invalid parameters */
  31991. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  31992. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  31993. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  31994. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  31995. EVP_MD_CTX_free(mctx);
  31996. AssertNotNull(mctx = EVP_MD_CTX_new());
  31997. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  31998. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  31999. /* test invalid version number */
  32000. #if defined(OPENSSL_ALL)
  32001. AssertIntNE(X509_set_version(x509, 6L), 0);
  32002. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  32003. /* uses ParseCert which fails on bad version number */
  32004. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  32005. #endif
  32006. EVP_MD_CTX_free(mctx);
  32007. EVP_PKEY_free(priv);
  32008. EVP_PKEY_free(pub);
  32009. X509_free(x509);
  32010. X509_free(ca);
  32011. printf(resultFmt, passed);
  32012. #endif
  32013. return 0;
  32014. }
  32015. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  32016. {
  32017. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  32018. X509* x509 = NULL;
  32019. const X509_ALGOR* alg;
  32020. printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
  32021. AssertNotNull(x509 = X509_new());
  32022. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  32023. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  32024. X509_free(x509);
  32025. printf(resultFmt, passed);
  32026. #endif
  32027. return 0;
  32028. }
  32029. static int test_wolfSSL_X509_ALGOR_get0(void)
  32030. {
  32031. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32032. !defined(NO_SHA256) && !defined(NO_RSA)
  32033. X509* x509 = NULL;
  32034. const ASN1_OBJECT* obj = NULL;
  32035. const X509_ALGOR* alg;
  32036. int pptype = 0;
  32037. const void *ppval = NULL;
  32038. printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
  32039. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  32040. SSL_FILETYPE_PEM));
  32041. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  32042. /* Invalid case */
  32043. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  32044. AssertNull(obj);
  32045. /* Valid case */
  32046. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  32047. AssertNotNull(obj);
  32048. AssertNull(ppval);
  32049. AssertIntNE(pptype, 0);
  32050. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  32051. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  32052. X509_free(x509);
  32053. printf(resultFmt, passed);
  32054. #endif
  32055. return 0;
  32056. }
  32057. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  32058. {
  32059. #if defined(OPENSSL_EXTRA)
  32060. X509_VERIFY_PARAM *paramTo;
  32061. X509_VERIFY_PARAM *paramFrom;
  32062. int ret;
  32063. char testIPv4[] = "127.0.0.1";
  32064. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  32065. char testhostName1[] = "foo.hoge.com";
  32066. char testhostName2[] = "foobar.hoge.com";
  32067. printf(testingFmt, "wolfSSL_X509()");
  32068. paramTo = X509_VERIFY_PARAM_new();
  32069. AssertNotNull(paramTo);
  32070. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  32071. paramFrom = X509_VERIFY_PARAM_new();
  32072. AssertNotNull(paramFrom);
  32073. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  32074. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  32075. (int)XSTRLEN(testhostName1));
  32076. AssertIntEQ(1, ret);
  32077. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  32078. (int)XSTRLEN(testhostName1)));
  32079. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  32080. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  32081. AssertIntEQ(0x01, paramFrom->hostFlags);
  32082. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  32083. AssertIntEQ(0, ret);
  32084. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  32085. AssertIntEQ(1, ret);
  32086. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  32087. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  32088. AssertIntEQ(1, ret);
  32089. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  32090. AssertIntEQ(1, ret);
  32091. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  32092. /* null pointer */
  32093. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  32094. AssertIntEQ(WOLFSSL_FAILURE, ret);
  32095. /* in the case of "from" null, returns success */
  32096. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  32097. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  32098. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  32099. AssertIntEQ(WOLFSSL_FAILURE, ret);
  32100. /* inherit flags test : VPARAM_DEFAULT */
  32101. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  32102. AssertIntEQ(1, ret);
  32103. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  32104. (int)XSTRLEN(testhostName1)));
  32105. AssertIntEQ(0x01, paramTo->hostFlags);
  32106. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  32107. /* inherit flags test : VPARAM OVERWRITE */
  32108. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  32109. (int)XSTRLEN(testhostName2));
  32110. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  32111. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  32112. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  32113. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  32114. AssertIntEQ(1, ret);
  32115. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  32116. (int)XSTRLEN(testhostName1)));
  32117. AssertIntEQ(0x01, paramTo->hostFlags);
  32118. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  32119. /* inherit flags test : VPARAM_RESET_FLAGS */
  32120. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  32121. (int)XSTRLEN(testhostName2));
  32122. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  32123. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  32124. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  32125. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  32126. AssertIntEQ(1, ret);
  32127. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  32128. (int)XSTRLEN(testhostName1)));
  32129. AssertIntEQ(0x01, paramTo->hostFlags);
  32130. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  32131. /* inherit flags test : VPARAM_LOCKED */
  32132. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  32133. (int)XSTRLEN(testhostName2));
  32134. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  32135. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  32136. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  32137. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  32138. AssertIntEQ(1, ret);
  32139. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  32140. (int)XSTRLEN(testhostName2)));
  32141. AssertIntEQ(0x00, paramTo->hostFlags);
  32142. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  32143. /* test for incorrect parameters */
  32144. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  32145. AssertIntEQ(0, ret);
  32146. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  32147. AssertIntEQ(0, ret);
  32148. /* inherit flags test : VPARAM_ONCE, not testable yet */
  32149. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  32150. AssertIntEQ(1, ret);
  32151. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  32152. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  32153. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  32154. AssertIntEQ(1, ret);
  32155. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  32156. AssertIntEQ(0, ret);
  32157. X509_VERIFY_PARAM_free(paramTo);
  32158. X509_VERIFY_PARAM_free(paramFrom);
  32159. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  32160. printf(resultFmt, passed);
  32161. #endif
  32162. return 0;
  32163. }
  32164. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  32165. static int test_wolfSSL_check_domain_verify_count = 0;
  32166. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  32167. WOLFSSL_X509_STORE_CTX* store)
  32168. {
  32169. AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
  32170. AssertIntEQ(preverify, 1);
  32171. test_wolfSSL_check_domain_verify_count++;
  32172. return 1;
  32173. }
  32174. static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  32175. {
  32176. X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
  32177. /* Domain check should only be done on the leaf cert */
  32178. X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  32179. AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
  32180. "wolfSSL Server Chain", 0), 1);
  32181. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  32182. test_wolfSSL_check_domain_verify_cb);
  32183. }
  32184. static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  32185. {
  32186. /* Use a cert with different domains in chain */
  32187. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  32188. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  32189. }
  32190. static int test_wolfSSL_check_domain(void)
  32191. {
  32192. tcp_ready ready;
  32193. func_args client_args;
  32194. func_args server_args;
  32195. THREAD_TYPE serverThread;
  32196. callback_functions func_cb_client;
  32197. callback_functions func_cb_server;
  32198. printf(testingFmt, "wolfSSL_check_domain");
  32199. XMEMSET(&client_args, 0, sizeof(func_args));
  32200. XMEMSET(&server_args, 0, sizeof(func_args));
  32201. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  32202. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  32203. #ifdef WOLFSSL_TIRTOS
  32204. fdOpenSession(Task_self());
  32205. #endif
  32206. StartTCP();
  32207. InitTcpReady(&ready);
  32208. #if defined(USE_WINDOWS_API)
  32209. /* use RNG to get random port if using windows */
  32210. ready.port = GetRandomPort();
  32211. #endif
  32212. server_args.signal = &ready;
  32213. client_args.signal = &ready;
  32214. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  32215. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  32216. client_args.callbacks = &func_cb_client;
  32217. server_args.callbacks = &func_cb_server;
  32218. start_thread(test_server_nofail, &server_args, &serverThread);
  32219. wait_tcp_ready(&server_args);
  32220. test_client_nofail(&client_args, NULL);
  32221. join_thread(serverThread);
  32222. AssertTrue(client_args.return_code);
  32223. AssertTrue(server_args.return_code);
  32224. FreeTcpReady(&ready);
  32225. /* Should have been called once for each cert in sent chain */
  32226. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  32227. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  32228. #else
  32229. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  32230. #endif
  32231. printf(resultFmt, passed);
  32232. return 0;
  32233. }
  32234. #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
  32235. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  32236. {
  32237. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  32238. X509* x509 = NULL;
  32239. X509_PUBKEY* pubKey;
  32240. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  32241. AssertNotNull(x509 = X509_new());
  32242. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  32243. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  32244. X509_free(x509);
  32245. printf(resultFmt, passed);
  32246. #endif
  32247. return 0;
  32248. }
  32249. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  32250. {
  32251. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32252. !defined(NO_SHA256) && !defined(NO_RSA)
  32253. X509* x509 = NULL;
  32254. ASN1_OBJECT* obj = NULL;
  32255. const ASN1_OBJECT* pa_oid = NULL;
  32256. X509_PUBKEY* pubKey;
  32257. X509_PUBKEY* pubKey2;
  32258. EVP_PKEY* evpKey;
  32259. const unsigned char *pk;
  32260. int ppklen, pptype;
  32261. X509_ALGOR *pa;
  32262. const void *pval;
  32263. printf(testingFmt, "wolfSSL_X509_PUBKEY_RSA");
  32264. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  32265. SSL_FILETYPE_PEM));
  32266. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  32267. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  32268. AssertNotNull(pk);
  32269. AssertNotNull(pa);
  32270. AssertNotNull(pubKey);
  32271. AssertIntGT(ppklen, 0);
  32272. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  32273. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  32274. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  32275. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  32276. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  32277. AssertNotNull(pk);
  32278. AssertNotNull(pa);
  32279. AssertIntGT(ppklen, 0);
  32280. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  32281. AssertNotNull(pa_oid);
  32282. AssertNull(pval);
  32283. AssertIntEQ(pptype, V_ASN1_NULL);
  32284. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  32285. X509_PUBKEY_free(pubKey2);
  32286. X509_free(x509);
  32287. EVP_PKEY_free(evpKey);
  32288. printf(resultFmt, passed);
  32289. #endif
  32290. return 0;
  32291. }
  32292. static int test_wolfSSL_X509_PUBKEY_EC(void)
  32293. {
  32294. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  32295. X509* x509 = NULL;
  32296. ASN1_OBJECT* obj = NULL;
  32297. ASN1_OBJECT* poid;
  32298. const ASN1_OBJECT* pa_oid = NULL;
  32299. X509_PUBKEY* pubKey;
  32300. X509_PUBKEY* pubKey2;
  32301. EVP_PKEY* evpKey;
  32302. const unsigned char *pk;
  32303. int ppklen, pptype;
  32304. X509_ALGOR *pa;
  32305. const void *pval;
  32306. char buf[50];
  32307. printf(testingFmt, "wolfSSL_X509_PUBKEY_EC");
  32308. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  32309. SSL_FILETYPE_PEM));
  32310. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  32311. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  32312. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  32313. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  32314. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  32315. AssertNotNull(pk);
  32316. AssertNotNull(pa);
  32317. AssertIntGT(ppklen, 0);
  32318. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  32319. AssertNotNull(pa_oid);
  32320. AssertNotNull(pval);
  32321. AssertIntEQ(pptype, V_ASN1_OBJECT);
  32322. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  32323. poid = (ASN1_OBJECT *)pval;
  32324. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  32325. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  32326. X509_PUBKEY_free(pubKey2);
  32327. X509_free(x509);
  32328. EVP_PKEY_free(evpKey);
  32329. printf(resultFmt, passed);
  32330. #endif
  32331. return 0;
  32332. }
  32333. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  32334. {
  32335. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  32336. word32 bytes;
  32337. #ifdef USE_CERT_BUFFERS_1024
  32338. byte tmp[ONEK_BUF];
  32339. #elif defined(USE_CERT_BUFFERS_2048)
  32340. byte tmp[TWOK_BUF];
  32341. #else
  32342. byte tmp[TWOK_BUF];
  32343. #endif /* END USE_CERT_BUFFERS_1024 */
  32344. const unsigned char* dsaKeyDer = tmp;
  32345. ASN1_OBJECT* obj = NULL;
  32346. ASN1_STRING* str;
  32347. const ASN1_OBJECT* pa_oid = NULL;
  32348. X509_PUBKEY* pubKey = NULL;
  32349. EVP_PKEY* evpKey = NULL;
  32350. const unsigned char *pk;
  32351. int ppklen, pptype;
  32352. X509_ALGOR *pa;
  32353. const void *pval;
  32354. printf(testingFmt, "wolfSSL_X509_PUBKEY_DSA");
  32355. #ifdef USE_CERT_BUFFERS_1024
  32356. XMEMSET(tmp, 0, sizeof(tmp));
  32357. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  32358. bytes = sizeof_dsa_key_der_1024;
  32359. #elif defined(USE_CERT_BUFFERS_2048)
  32360. XMEMSET(tmp, 0, sizeof(tmp));
  32361. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  32362. bytes = sizeof_dsa_key_der_2048;
  32363. #else
  32364. {
  32365. XFILE fp;
  32366. XMEMSET(tmp, 0, sizeof(tmp));
  32367. fp = XFOPEN("./certs/dsa2048.der", "rb");
  32368. if (fp == XBADFILE) {
  32369. return WOLFSSL_BAD_FILE;
  32370. }
  32371. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  32372. XFCLOSE(fp);
  32373. }
  32374. #endif
  32375. /* Initialize pkey with der format dsa key */
  32376. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  32377. AssertNotNull(pubKey = X509_PUBKEY_new());
  32378. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  32379. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  32380. AssertNotNull(pk);
  32381. AssertNotNull(pa);
  32382. AssertIntGT(ppklen, 0);
  32383. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  32384. AssertNotNull(pa_oid);
  32385. AssertNotNull(pval);
  32386. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  32387. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  32388. str = (ASN1_STRING *)pval;
  32389. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  32390. #ifdef USE_CERT_BUFFERS_1024
  32391. AssertIntEQ(ASN1_STRING_length(str), 291);
  32392. #else
  32393. AssertIntEQ(ASN1_STRING_length(str), 549);
  32394. #endif /* END USE_CERT_BUFFERS_1024 */
  32395. X509_PUBKEY_free(pubKey);
  32396. EVP_PKEY_free(evpKey);
  32397. printf(resultFmt, passed);
  32398. #endif
  32399. return 0;
  32400. }
  32401. static int test_wolfSSL_RAND(void)
  32402. {
  32403. #if defined(OPENSSL_EXTRA)
  32404. byte seed[16];
  32405. printf(testingFmt, "wolfSSL_RAND()");
  32406. RAND_seed(seed, sizeof(seed));
  32407. AssertIntEQ(RAND_poll(), 1);
  32408. RAND_cleanup();
  32409. AssertIntEQ(RAND_egd(NULL), -1);
  32410. #ifndef NO_FILESYSTEM
  32411. {
  32412. char fname[100];
  32413. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  32414. AssertIntEQ(RAND_write_file(NULL), 0);
  32415. }
  32416. #endif
  32417. printf(resultFmt, passed);
  32418. #endif
  32419. return 0;
  32420. }
  32421. static int test_wolfSSL_BUF(void)
  32422. {
  32423. #if defined(OPENSSL_EXTRA)
  32424. BUF_MEM* buf;
  32425. AssertNotNull(buf = BUF_MEM_new());
  32426. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  32427. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  32428. BUF_MEM_free(buf);
  32429. #endif /* OPENSSL_EXTRA */
  32430. return 0;
  32431. }
  32432. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  32433. static int stub_rand_seed(const void *buf, int num)
  32434. {
  32435. (void)buf;
  32436. (void)num;
  32437. return 123;
  32438. }
  32439. static int stub_rand_bytes(unsigned char *buf, int num)
  32440. {
  32441. (void)buf;
  32442. (void)num;
  32443. return 456;
  32444. }
  32445. static byte* was_stub_rand_cleanup_called(void)
  32446. {
  32447. static byte was_called = 0;
  32448. return &was_called;
  32449. }
  32450. static void stub_rand_cleanup(void)
  32451. {
  32452. byte* was_called = was_stub_rand_cleanup_called();
  32453. *was_called = 1;
  32454. return;
  32455. }
  32456. static byte* was_stub_rand_add_called(void)
  32457. {
  32458. static byte was_called = 0;
  32459. return &was_called;
  32460. }
  32461. static int stub_rand_add(const void *buf, int num, double entropy)
  32462. {
  32463. byte* was_called = was_stub_rand_add_called();
  32464. (void)buf;
  32465. (void)num;
  32466. (void)entropy;
  32467. *was_called = 1;
  32468. return 0;
  32469. }
  32470. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  32471. {
  32472. (void)buf;
  32473. (void)num;
  32474. return 9876;
  32475. }
  32476. static int stub_rand_status(void)
  32477. {
  32478. return 5432;
  32479. }
  32480. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  32481. static int test_wolfSSL_RAND_set_rand_method(void)
  32482. {
  32483. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  32484. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  32485. unsigned char* buf = NULL;
  32486. int num = 0;
  32487. double entropy = 0;
  32488. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  32489. byte* was_add_called = was_stub_rand_add_called();
  32490. printf(testingFmt, "wolfSSL_RAND_set_rand_method()");
  32491. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  32492. DYNAMIC_TYPE_TMP_BUFFER);
  32493. AssertIntNE(wolfSSL_RAND_status(), 5432);
  32494. AssertIntEQ(*was_cleanup_called, 0);
  32495. RAND_cleanup();
  32496. AssertIntEQ(*was_cleanup_called, 0);
  32497. rand_methods.seed = &stub_rand_seed;
  32498. rand_methods.bytes = &stub_rand_bytes;
  32499. rand_methods.cleanup = &stub_rand_cleanup;
  32500. rand_methods.add = &stub_rand_add;
  32501. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  32502. rand_methods.status = &stub_rand_status;
  32503. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  32504. AssertIntEQ(RAND_seed(buf, num), 123);
  32505. AssertIntEQ(RAND_bytes(buf, num), 456);
  32506. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  32507. AssertIntEQ(RAND_status(), 5432);
  32508. AssertIntEQ(*was_add_called, 0);
  32509. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  32510. RAND_add(buf, num, entropy);
  32511. AssertIntEQ(*was_add_called, 1);
  32512. was_add_called = 0;
  32513. AssertIntEQ(*was_cleanup_called, 0);
  32514. RAND_cleanup();
  32515. AssertIntEQ(*was_cleanup_called, 1);
  32516. *was_cleanup_called = 0;
  32517. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  32518. AssertIntNE(RAND_status(), 5432);
  32519. AssertIntEQ(*was_cleanup_called, 0);
  32520. RAND_cleanup();
  32521. AssertIntEQ(*was_cleanup_called, 0);
  32522. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  32523. printf(resultFmt, passed);
  32524. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  32525. return 0;
  32526. }
  32527. static int test_wolfSSL_RAND_bytes(void)
  32528. {
  32529. #if defined(OPENSSL_EXTRA)
  32530. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  32531. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  32532. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  32533. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  32534. int max_bufsize;
  32535. byte *my_buf;
  32536. printf(testingFmt, "test_wolfSSL_RAND_bytes()");
  32537. /* sanity check */
  32538. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  32539. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  32540. max_bufsize = size4;
  32541. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  32542. DYNAMIC_TYPE_TMP_BUFFER);
  32543. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  32544. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  32545. AssertNotNull(my_buf);
  32546. XMEMSET(my_buf, 0, max_bufsize);
  32547. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  32548. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  32549. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  32550. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  32551. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  32552. printf(resultFmt, passed);
  32553. #endif
  32554. return 0;
  32555. }
  32556. static int test_wolfSSL_BN_rand(void)
  32557. {
  32558. #if defined(OPENSSL_EXTRA)
  32559. BIGNUM* bn;
  32560. BIGNUM* range;
  32561. printf(testingFmt, "wolfSSL_BN_rand()");
  32562. /* Error conditions. */
  32563. /* NULL BN. */
  32564. AssertIntEQ(BN_rand(NULL, 0, 0, 0), SSL_FAILURE);
  32565. AssertNotNull(bn = BN_new());
  32566. /* Negative bits. */
  32567. AssertIntEQ(BN_rand(bn, -2, 0, 0), SSL_FAILURE);
  32568. /* 0 bits and top is not -1. */
  32569. AssertIntEQ(BN_rand(bn, 0, 1, 0), SSL_FAILURE);
  32570. /* 0 bits and bottom is not 0. */
  32571. AssertIntEQ(BN_rand(bn, 0, 0, 1), SSL_FAILURE);
  32572. /* 1 bit and top is 1. */
  32573. AssertIntEQ(BN_rand(bn, 1, 1, 0), SSL_FAILURE);
  32574. AssertIntEQ(BN_rand(bn, 0, -1, 0), SSL_SUCCESS);
  32575. AssertIntEQ(BN_num_bits(bn), 0);
  32576. AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
  32577. AssertIntEQ(BN_num_bits(bn), 8);
  32578. /* When top is 0, top bit should be 1. */
  32579. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  32580. AssertIntEQ(BN_rand(bn, 8, 1, 0), SSL_SUCCESS);
  32581. /* When top is 1, top 2 bits should be 1. */
  32582. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  32583. AssertIntEQ(BN_is_bit_set(bn, 6), SSL_SUCCESS);
  32584. AssertIntEQ(BN_rand(bn, 8, 0, 1), SSL_SUCCESS);
  32585. /* When bottom is 1, bottom bit should be 1. */
  32586. AssertIntEQ(BN_is_bit_set(bn, 0), SSL_SUCCESS);
  32587. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  32588. * requested number of bits up to the nearest multiple of 8. E.g. in this
  32589. * case, requesting a 13-bit random number would actually return a 16-bit
  32590. * random number. */
  32591. AssertIntEQ(BN_rand(bn, 13, 0, 0), SSL_SUCCESS);
  32592. AssertIntEQ(BN_num_bits(bn), 13);
  32593. AssertNotNull(range = BN_new());
  32594. AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
  32595. AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
  32596. BN_free(bn);
  32597. BN_free(range);
  32598. printf(resultFmt, passed);
  32599. #endif
  32600. return 0;
  32601. }
  32602. static int test_wolfSSL_pseudo_rand(void)
  32603. {
  32604. #if defined(OPENSSL_EXTRA)
  32605. BIGNUM* bn;
  32606. unsigned char bin[8];
  32607. int i;
  32608. printf(testingFmt, "wolfSSL_pseudo_rand()");
  32609. /* BN_pseudo_rand returns 1 on success 0 on failure
  32610. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  32611. for (i = 0; i < 10; i++) {
  32612. AssertNotNull(bn = BN_new());
  32613. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  32614. AssertIntGT(BN_bn2bin(bn, bin),0);
  32615. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  32616. BN_free(bn);
  32617. }
  32618. for (i = 0; i < 10; i++) {
  32619. AssertNotNull(bn = BN_new());
  32620. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  32621. AssertIntGT(BN_bn2bin(bn, bin),0);
  32622. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  32623. BN_free(bn);
  32624. }
  32625. printf(resultFmt, passed);
  32626. #endif
  32627. return 0;
  32628. }
  32629. static int test_wolfSSL_PKCS8_Compat(void)
  32630. {
  32631. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  32632. #ifndef NO_BIO
  32633. PKCS8_PRIV_KEY_INFO* pt;
  32634. BIO* bio;
  32635. XFILE f;
  32636. int bytes;
  32637. char pkcs8_buffer[512];
  32638. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  32639. EVP_PKEY *pkey = NULL;
  32640. #endif
  32641. printf(testingFmt, "wolfSSL_pkcs8()");
  32642. /* file from wolfssl/certs/ directory */
  32643. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  32644. AssertTrue(f != XBADFILE);
  32645. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  32646. XFCLOSE(f);
  32647. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  32648. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  32649. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  32650. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  32651. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  32652. /* gets PKCS8 pointer to pkey */
  32653. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  32654. EVP_PKEY_free(pkey);
  32655. #endif
  32656. BIO_free(bio);
  32657. PKCS8_PRIV_KEY_INFO_free(pt);
  32658. printf(resultFmt, passed);
  32659. #endif
  32660. #endif
  32661. return 0;
  32662. }
  32663. static int test_wolfSSL_PKCS8_d2i(void)
  32664. {
  32665. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  32666. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  32667. * requires a 12 byte password in FIPS mode. This test ends up
  32668. * trying to use an 8 byte password. */
  32669. #ifndef NO_FILESYSTEM
  32670. unsigned char pkcs8_buffer[2048];
  32671. const unsigned char* p;
  32672. int bytes;
  32673. XFILE file;
  32674. WOLFSSL_EVP_PKEY* pkey = NULL;
  32675. #ifndef NO_BIO
  32676. BIO* bio;
  32677. #if defined(OPENSSL_ALL) && \
  32678. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  32679. defined(HAVE_ECC)) && \
  32680. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32681. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  32682. #endif
  32683. #endif
  32684. #ifndef NO_RSA
  32685. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  32686. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  32687. #ifndef NO_DES3
  32688. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  32689. #endif
  32690. #endif /* NO_RSA */
  32691. #ifdef HAVE_ECC
  32692. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  32693. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  32694. #ifndef NO_DES3
  32695. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  32696. #endif
  32697. #endif /* HAVE_ECC */
  32698. #endif /* !NO_FILESYSTEM */
  32699. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  32700. #ifndef NO_RSA
  32701. #ifdef USE_CERT_BUFFERS_1024
  32702. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  32703. int rsaSz = sizeof_server_key_der_1024;
  32704. #else
  32705. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  32706. int rsaSz = sizeof_server_key_der_2048;
  32707. #endif
  32708. #endif
  32709. #ifdef HAVE_ECC
  32710. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  32711. int ecSz = sizeof_ecc_key_der_256;
  32712. #endif
  32713. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  32714. #ifndef NO_FILESYSTEM
  32715. (void)pkcs8_buffer;
  32716. (void)p;
  32717. (void)bytes;
  32718. (void)file;
  32719. #ifndef NO_BIO
  32720. (void)bio;
  32721. #endif
  32722. #endif
  32723. #ifdef OPENSSL_ALL
  32724. #ifndef NO_RSA
  32725. /* Try to auto-detect normal RSA private key */
  32726. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  32727. EVP_PKEY_free(pkey);
  32728. #endif
  32729. #ifdef HAVE_ECC
  32730. /* Try to auto-detect normal EC private key */
  32731. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  32732. EVP_PKEY_free(pkey);
  32733. #endif
  32734. #endif /* OPENSSL_ALL */
  32735. #ifndef NO_FILESYSTEM
  32736. #ifndef NO_RSA
  32737. /* Get DER encoded RSA PKCS#8 data. */
  32738. file = XFOPEN(rsaDerPkcs8File, "rb");
  32739. AssertTrue(file != XBADFILE);
  32740. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32741. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32742. file)), 0);
  32743. XFCLOSE(file);
  32744. p = pkcs8_buffer;
  32745. #ifdef OPENSSL_ALL
  32746. /* Try to decode - auto-detect key type. */
  32747. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  32748. #else
  32749. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  32750. #endif
  32751. /* Get PEM encoded RSA PKCS#8 data. */
  32752. file = XFOPEN(rsaPemPkcs8File, "rb");
  32753. AssertTrue(file != XBADFILE);
  32754. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32755. file)), 0);
  32756. XFCLOSE(file);
  32757. #if defined(OPENSSL_ALL) && \
  32758. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32759. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32760. /* Write PKCS#8 PEM to BIO. */
  32761. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32762. NULL), bytes);
  32763. /* Compare file and written data */
  32764. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  32765. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  32766. BIO_free(bio);
  32767. #ifndef NO_DES3
  32768. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32769. /* Write Encrypted PKCS#8 PEM to BIO. */
  32770. bytes = 1834;
  32771. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  32772. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  32773. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  32774. (void*)"yassl123"));
  32775. EVP_PKEY_free(evpPkey);
  32776. BIO_free(bio);
  32777. #endif /* !NO_DES3 */
  32778. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  32779. EVP_PKEY_free(pkey);
  32780. /* PKCS#8 encrypted RSA key */
  32781. #ifndef NO_DES3
  32782. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  32783. AssertTrue(file != XBADFILE);
  32784. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32785. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32786. file)), 0);
  32787. XFCLOSE(file);
  32788. #if defined(OPENSSL_ALL) && \
  32789. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32790. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  32791. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  32792. (void*)"yassl123"));
  32793. EVP_PKEY_free(pkey);
  32794. BIO_free(bio);
  32795. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  32796. #endif /* !NO_DES3 */
  32797. #endif /* NO_RSA */
  32798. #ifdef HAVE_ECC
  32799. /* PKCS#8 encode EC key */
  32800. file = XFOPEN(ecDerPkcs8File, "rb");
  32801. AssertTrue(file != XBADFILE);
  32802. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32803. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32804. file)), 0);
  32805. XFCLOSE(file);
  32806. p = pkcs8_buffer;
  32807. #ifdef OPENSSL_ALL
  32808. /* Try to decode - auto-detect key type. */
  32809. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  32810. #else
  32811. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  32812. #endif
  32813. /* Get PEM encoded RSA PKCS#8 data. */
  32814. file = XFOPEN(ecPemPkcs8File, "rb");
  32815. AssertTrue(file != XBADFILE);
  32816. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32817. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32818. file)), 0);
  32819. XFCLOSE(file);
  32820. #if defined(OPENSSL_ALL) && \
  32821. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  32822. defined(HAVE_AES_CBC)
  32823. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32824. /* Write PKCS#8 PEM to BIO. */
  32825. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32826. NULL), bytes);
  32827. /* Compare file and written data */
  32828. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  32829. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  32830. BIO_free(bio);
  32831. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32832. /* Write Encrypted PKCS#8 PEM to BIO. */
  32833. bytes = 379;
  32834. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  32835. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  32836. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  32837. (void*)"yassl123"));
  32838. EVP_PKEY_free(evpPkey);
  32839. BIO_free(bio);
  32840. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  32841. EVP_PKEY_free(pkey);
  32842. /* PKCS#8 encrypted EC key */
  32843. #ifndef NO_DES3
  32844. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  32845. AssertTrue(file != XBADFILE);
  32846. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32847. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32848. file)), 0);
  32849. XFCLOSE(file);
  32850. #if defined(OPENSSL_ALL) && \
  32851. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32852. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  32853. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  32854. (void*)"yassl123"));
  32855. EVP_PKEY_free(pkey);
  32856. BIO_free(bio);
  32857. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  32858. #endif /* !NO_DES3 */
  32859. #endif /* HAVE_ECC */
  32860. #endif /* !NO_FILESYSTEM */
  32861. printf(resultFmt, passed);
  32862. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  32863. return 0;
  32864. }
  32865. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  32866. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  32867. #define LOGGING_THREADS 5
  32868. #define ERROR_COUNT 10
  32869. static volatile int loggingThreadsReady;
  32870. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  32871. {
  32872. const char* file;
  32873. int line;
  32874. int err;
  32875. int errorCount = 0;
  32876. int i;
  32877. (void)args;
  32878. while (!loggingThreadsReady);
  32879. for (i = 0; i < ERROR_COUNT; i++)
  32880. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  32881. while ((err = ERR_get_error_line(&file, &line))) {
  32882. AssertIntEQ(err, 990 + errorCount);
  32883. errorCount++;
  32884. }
  32885. AssertIntEQ(errorCount, ERROR_COUNT);
  32886. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  32887. errorCount = 0;
  32888. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  32889. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  32890. while ((err = ERR_get_error_line(&file, &line))) {
  32891. AssertIntEQ(err, 990 + errorCount);
  32892. errorCount++;
  32893. }
  32894. /* test that the 3 errors over the max were dropped */
  32895. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  32896. return 0;
  32897. }
  32898. #endif
  32899. static int test_error_queue_per_thread(void)
  32900. {
  32901. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  32902. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  32903. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  32904. int i;
  32905. printf(testingFmt, "error_queue_per_thread()");
  32906. ERR_clear_error(); /* clear out any error nodes */
  32907. loggingThreadsReady = 0;
  32908. for (i = 0; i < LOGGING_THREADS; i++)
  32909. start_thread(test_logging, NULL, &loggingThreads[i]);
  32910. loggingThreadsReady = 1;
  32911. for (i = 0; i < LOGGING_THREADS; i++)
  32912. join_thread(loggingThreads[i]);
  32913. printf(resultFmt, passed);
  32914. #endif
  32915. return 0;
  32916. }
  32917. static int test_wolfSSL_ERR_put_error(void)
  32918. {
  32919. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  32920. defined(DEBUG_WOLFSSL)
  32921. const char* file;
  32922. int line;
  32923. printf(testingFmt, "wolfSSL_ERR_put_error()");
  32924. ERR_clear_error(); /* clear out any error nodes */
  32925. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  32926. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  32927. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  32928. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  32929. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  32930. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  32931. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  32932. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  32933. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  32934. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  32935. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  32936. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  32937. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  32938. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  32939. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  32940. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  32941. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  32942. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  32943. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  32944. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  32945. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  32946. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  32947. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  32948. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  32949. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  32950. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  32951. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  32952. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  32953. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  32954. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  32955. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  32956. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  32957. #ifdef WOLFSSL_PYTHON
  32958. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  32959. "this file", 100);
  32960. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  32961. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  32962. AssertIntEQ(line, 100);
  32963. AssertIntEQ(wolfSSL_ERR_peek_error(),
  32964. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  32965. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  32966. #endif
  32967. /* try reading past end of error queue */
  32968. file = NULL;
  32969. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  32970. AssertNull(file);
  32971. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  32972. PEMerr(4,4);
  32973. AssertIntEQ(ERR_get_error(), 4);
  32974. /* Empty and free up all error nodes */
  32975. ERR_clear_error();
  32976. /* Verify all nodes are cleared */
  32977. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  32978. ERR_clear_error();
  32979. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  32980. printf(resultFmt, passed);
  32981. #endif
  32982. return 0;
  32983. }
  32984. /*
  32985. * This is a regression test for a bug where the peek/get error functions were
  32986. * drawing from the end of the queue rather than the front.
  32987. */
  32988. static int test_wolfSSL_ERR_get_error_order(void)
  32989. {
  32990. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  32991. printf(testingFmt, "test_wolfSSL_ERR_get_error_order");
  32992. /* Empty the queue. */
  32993. wolfSSL_ERR_clear_error();
  32994. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  32995. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  32996. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  32997. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  32998. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  32999. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  33000. printf(resultFmt, passed);
  33001. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  33002. return 0;
  33003. }
  33004. #ifndef NO_BIO
  33005. static int test_wolfSSL_ERR_print_errors(void)
  33006. {
  33007. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33008. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  33009. BIO* bio;
  33010. char buf[1024];
  33011. printf(testingFmt, "wolfSSL_ERR_print_errors()");
  33012. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33013. ERR_clear_error(); /* clear out any error nodes */
  33014. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  33015. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  33016. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  33017. ERR_print_errors(bio);
  33018. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  33019. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  33020. buf, 55), 0);
  33021. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  33022. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  33023. buf, 56), 0);
  33024. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  33025. AssertIntEQ(buf[0], '\0');
  33026. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  33027. BIO_free(bio);
  33028. printf(resultFmt, passed);
  33029. #endif
  33030. return 0;
  33031. }
  33032. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33033. defined(DEBUG_WOLFSSL)
  33034. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  33035. {
  33036. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  33037. return 0;
  33038. }
  33039. #endif
  33040. static int test_wolfSSL_ERR_print_errors_cb(void)
  33041. {
  33042. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33043. defined(DEBUG_WOLFSSL)
  33044. BIO* bio;
  33045. char buf[1024];
  33046. printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
  33047. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33048. ERR_clear_error(); /* clear out any error nodes */
  33049. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  33050. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  33051. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  33052. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  33053. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  33054. buf, 53), 0);
  33055. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  33056. buf + 53, 55), 0);
  33057. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  33058. BIO_free(bio);
  33059. printf(resultFmt, passed);
  33060. #endif
  33061. return 0;
  33062. }
  33063. /*
  33064. * Testing WOLFSSL_ERROR_MSG
  33065. */
  33066. static int test_WOLFSSL_ERROR_MSG(void)
  33067. {
  33068. int ret = 0;
  33069. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  33070. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  33071. const char* msg = TEST_STRING;
  33072. printf(testingFmt, "WOLFSSL_ERROR_MSG()");
  33073. WOLFSSL_ERROR_MSG(msg);
  33074. printf(resultFmt, ret == 0 ? passed : failed);
  33075. fflush(stdout);
  33076. #endif
  33077. return ret;
  33078. }/*End test_WOLFSSL_ERROR_MSG*/
  33079. /*
  33080. * Testing wc_ERR_remove_state
  33081. */
  33082. static int test_wc_ERR_remove_state(void)
  33083. {
  33084. int ret = 0;
  33085. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  33086. printf(testingFmt, "wc_ERR_remove_state()");
  33087. wc_ERR_remove_state();
  33088. printf(resultFmt, ret == 0 ? passed : failed);
  33089. fflush(stdout);
  33090. #endif
  33091. return ret;
  33092. }/*End test_wc_ERR_remove_state*/
  33093. /*
  33094. * Testing wc_ERR_print_errors_fp
  33095. */
  33096. static int test_wc_ERR_print_errors_fp(void)
  33097. {
  33098. int ret = 0;
  33099. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  33100. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  33101. long sz;
  33102. XFILE fp;
  33103. printf(testingFmt, "wc_ERR_print_errors_fp()");
  33104. WOLFSSL_ERROR(BAD_FUNC_ARG);
  33105. fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  33106. wc_ERR_print_errors_fp(fp);
  33107. #if defined(DEBUG_WOLFSSL)
  33108. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  33109. sz = XFTELL(fp);
  33110. #ifdef NO_ERROR_QUEUE
  33111. /* File should be empty when NO_ERROR_QUEUE is defined */
  33112. if (sz != 0) {
  33113. ret = BAD_FUNC_ARG;
  33114. }
  33115. #else
  33116. if (sz == 0) {
  33117. ret = BAD_FUNC_ARG;
  33118. }
  33119. #endif
  33120. #endif
  33121. printf(resultFmt, ret == 0 ? passed : failed);
  33122. fflush(stdout);
  33123. XFCLOSE(fp);
  33124. (void)sz;
  33125. #endif
  33126. return ret;
  33127. }/*End test_wc_ERR_print_errors_fp*/
  33128. #ifdef DEBUG_WOLFSSL
  33129. static void Logging_cb(const int logLevel, const char *const logMessage)
  33130. {
  33131. (void)logLevel;
  33132. (void)logMessage;
  33133. }
  33134. #endif
  33135. /*
  33136. * Testing wolfSSL_GetLoggingCb
  33137. */
  33138. static int test_wolfSSL_GetLoggingCb(void)
  33139. {
  33140. int ret = 0;
  33141. printf(testingFmt, "wolfSSL_GetLoggingCb()");
  33142. #ifdef DEBUG_WOLFSSL
  33143. /* Testing without wolfSSL_SetLoggingCb() */
  33144. if (ret == 0) {
  33145. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  33146. ret = 0;
  33147. }
  33148. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  33149. ret = -1;
  33150. }
  33151. }
  33152. /* Testing with wolfSSL_SetLoggingCb() */
  33153. if (ret == 0) {
  33154. ret = wolfSSL_SetLoggingCb(Logging_cb);
  33155. if (ret == 0){
  33156. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  33157. ret = -1;
  33158. }
  33159. if (ret == 0) {
  33160. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  33161. ret = 0;
  33162. }
  33163. }
  33164. /* reset logging callback */
  33165. wolfSSL_SetLoggingCb(NULL);
  33166. }
  33167. }
  33168. #endif
  33169. if (ret == 0) {
  33170. if (wolfSSL_GetLoggingCb() != NULL) {
  33171. ret = -1;
  33172. }
  33173. }
  33174. printf(resultFmt, ret == 0 ? passed : failed);
  33175. fflush(stdout);
  33176. return ret;
  33177. }/*End test_wolfSSL_GetLoggingCb*/
  33178. #endif /* !NO_BIO */
  33179. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  33180. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  33181. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  33182. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  33183. {
  33184. static const unsigned char key[] = "simple test key";
  33185. HMAC_CTX* hmac;
  33186. ENGINE* e = NULL;
  33187. unsigned char hash[WC_MAX_DIGEST_SIZE];
  33188. unsigned int len;
  33189. AssertNotNull(hmac = HMAC_CTX_new());
  33190. HMAC_CTX_init(hmac);
  33191. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  33192. SSL_SUCCESS);
  33193. /* re-using test key as data to hash */
  33194. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  33195. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  33196. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  33197. AssertIntEQ(len, md_len);
  33198. AssertIntEQ(HMAC_size(hmac), md_len);
  33199. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  33200. HMAC_cleanup(hmac);
  33201. HMAC_CTX_free(hmac);
  33202. len = 0;
  33203. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  33204. AssertIntEQ(len, md_len);
  33205. return 0;
  33206. }
  33207. #endif
  33208. static int test_wolfSSL_HMAC(void)
  33209. {
  33210. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  33211. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  33212. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  33213. printf(testingFmt, "wolfSSL_HMAC()");
  33214. #ifndef NO_SHA256
  33215. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  33216. #endif
  33217. #ifdef WOLFSSL_SHA224
  33218. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  33219. #endif
  33220. #ifdef WOLFSSL_SHA384
  33221. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  33222. #endif
  33223. #ifdef WOLFSSL_SHA512
  33224. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  33225. #endif
  33226. #ifdef WOLFSSL_SHA3
  33227. #ifndef WOLFSSL_NOSHA3_224
  33228. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  33229. #endif
  33230. #ifndef WOLFSSL_NOSHA3_256
  33231. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  33232. #endif
  33233. #ifndef WOLFSSL_NOSHA3_384
  33234. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  33235. #endif
  33236. #ifndef WOLFSSL_NOSHA3_512
  33237. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  33238. #endif
  33239. #endif
  33240. printf(resultFmt, passed);
  33241. #endif
  33242. return 0;
  33243. }
  33244. static int test_wolfSSL_CMAC(void)
  33245. {
  33246. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  33247. defined(WOLFSSL_AES_DIRECT)
  33248. int i;
  33249. byte key[AES_128_KEY_SIZE];
  33250. CMAC_CTX* cmacCtx = NULL;
  33251. byte out[AES_BLOCK_SIZE];
  33252. size_t outLen = AES_BLOCK_SIZE;
  33253. printf(testingFmt, "test_wolfSSL_CMAC()");
  33254. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  33255. key[i] = i;
  33256. }
  33257. AssertNotNull(cmacCtx = CMAC_CTX_new());
  33258. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  33259. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  33260. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  33261. NULL), SSL_SUCCESS);
  33262. /* re-using test key as data to hash */
  33263. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  33264. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  33265. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  33266. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  33267. CMAC_CTX_free(cmacCtx);
  33268. printf(resultFmt, passed);
  33269. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  33270. return 0;
  33271. }
  33272. static int test_wolfSSL_OBJ(void)
  33273. {
  33274. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  33275. * mode
  33276. */
  33277. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  33278. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  33279. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  33280. ASN1_OBJECT *obj = NULL;
  33281. ASN1_OBJECT *obj2 = NULL;
  33282. char buf[50];
  33283. XFILE fp;
  33284. X509 *x509 = NULL;
  33285. X509_NAME *x509Name;
  33286. X509_NAME_ENTRY *x509NameEntry;
  33287. ASN1_OBJECT *asn1Name = NULL;
  33288. int numNames;
  33289. BIO *bio = NULL;
  33290. int nid;
  33291. int i, j;
  33292. const char *f[] = {
  33293. #ifndef NO_RSA
  33294. "./certs/ca-cert.der",
  33295. #endif
  33296. #ifdef HAVE_ECC
  33297. "./certs/ca-ecc-cert.der",
  33298. "./certs/ca-ecc384-cert.der",
  33299. #endif
  33300. NULL};
  33301. ASN1_OBJECT *field_name_obj = NULL;
  33302. int lastpos = -1;
  33303. int tmp = -1;
  33304. ASN1_STRING *asn1 = NULL;
  33305. unsigned char *buf_dyn = NULL;
  33306. printf(testingFmt, "wolfSSL_OBJ()");
  33307. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  33308. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  33309. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  33310. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  33311. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  33312. ASN1_OBJECT_free(obj);
  33313. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  33314. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  33315. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  33316. #ifdef WOLFSSL_CERT_EXT
  33317. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  33318. #endif
  33319. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  33320. AssertNotNull(obj2 = OBJ_dup(obj));
  33321. AssertIntEQ(OBJ_cmp(obj, obj2), 0);
  33322. ASN1_OBJECT_free(obj);
  33323. ASN1_OBJECT_free(obj2);
  33324. for (i = 0; f[i] != NULL; i++)
  33325. {
  33326. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  33327. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  33328. XFCLOSE(fp);
  33329. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  33330. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  33331. /* Get the Common Name by using OBJ_txt2obj */
  33332. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  33333. do
  33334. {
  33335. lastpos = tmp;
  33336. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  33337. } while (tmp > -1);
  33338. AssertIntNE(lastpos, -1);
  33339. ASN1_OBJECT_free(field_name_obj);
  33340. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  33341. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  33342. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  33343. /*
  33344. * All Common Names should be www.wolfssl.com
  33345. * This makes testing easier as we can test for the expected value.
  33346. */
  33347. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  33348. OPENSSL_free(buf_dyn);
  33349. bio = BIO_new(BIO_s_mem());
  33350. AssertTrue(bio != NULL);
  33351. for (j = 0; j < numNames; j++)
  33352. {
  33353. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  33354. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  33355. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  33356. }
  33357. BIO_free(bio);
  33358. X509_free(x509);
  33359. }
  33360. #ifdef HAVE_PKCS12
  33361. {
  33362. PKCS12 *p12;
  33363. int boolRet;
  33364. EVP_PKEY *pkey = NULL;
  33365. const char *p12_f[] = {
  33366. #if !defined(NO_DES3) && !defined(NO_RSA)
  33367. "./certs/test-servercert.p12",
  33368. #endif
  33369. NULL};
  33370. for (i = 0; p12_f[i] != NULL; i++)
  33371. {
  33372. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  33373. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  33374. XFCLOSE(fp);
  33375. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  33376. &pkey, &x509, NULL)) > 0);
  33377. wc_PKCS12_free(p12);
  33378. EVP_PKEY_free(pkey);
  33379. x509Name = X509_get_issuer_name(x509);
  33380. AssertNotNull(x509Name);
  33381. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  33382. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  33383. for (j = 0; j < numNames; j++)
  33384. {
  33385. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  33386. AssertNotNull(asn1Name =
  33387. X509_NAME_ENTRY_get_object(x509NameEntry));
  33388. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  33389. }
  33390. BIO_free(bio);
  33391. X509_free(x509);
  33392. }
  33393. }
  33394. #endif /* HAVE_PKCS12 */
  33395. printf(resultFmt, passed);
  33396. #endif
  33397. return 0;
  33398. }
  33399. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  33400. {
  33401. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  33402. ASN1_OBJECT *obj = NULL;
  33403. BIO *bio = NULL;
  33404. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  33405. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  33406. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  33407. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  33408. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  33409. BIO_free(bio);
  33410. ASN1_OBJECT_free(obj);
  33411. #endif
  33412. return 0;
  33413. }
  33414. static int test_wolfSSL_OBJ_cmp(void)
  33415. {
  33416. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  33417. ASN1_OBJECT *obj = NULL;
  33418. ASN1_OBJECT *obj2 = NULL;
  33419. printf(testingFmt, "wolfSSL_OBJ_cmp()");
  33420. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  33421. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  33422. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  33423. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  33424. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  33425. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  33426. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  33427. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  33428. ASN1_OBJECT_free(obj);
  33429. ASN1_OBJECT_free(obj2);
  33430. printf(resultFmt, passed);
  33431. #endif
  33432. return 0;
  33433. }
  33434. static int test_wolfSSL_OBJ_txt2nid(void)
  33435. {
  33436. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  33437. int i;
  33438. static const struct {
  33439. const char* sn;
  33440. const char* ln;
  33441. const char* oid;
  33442. int nid;
  33443. } testVals[] = {
  33444. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  33445. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  33446. NID_id_on_dnsSRV },
  33447. { "msUPN", "Microsoft User Principal Name",
  33448. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  33449. { NULL, NULL, NULL, NID_undef }
  33450. };
  33451. printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
  33452. /* Invalid cases */
  33453. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  33454. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  33455. /* Valid cases */
  33456. for (i = 0; testVals[i].sn != NULL; i++) {
  33457. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  33458. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  33459. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  33460. }
  33461. printf(resultFmt, passed);
  33462. #endif
  33463. return 0;
  33464. }
  33465. static int test_wolfSSL_OBJ_txt2obj(void)
  33466. {
  33467. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  33468. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  33469. int i;
  33470. char buf[50];
  33471. ASN1_OBJECT* obj;
  33472. static const struct {
  33473. const char* oidStr;
  33474. const char* sn;
  33475. const char* ln;
  33476. } objs_list[] = {
  33477. #if defined(WOLFSSL_APACHE_HTTPD)
  33478. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  33479. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  33480. #endif
  33481. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  33482. { NULL, NULL, NULL }
  33483. };
  33484. static const struct {
  33485. const char* numeric;
  33486. const char* name;
  33487. } objs_named[] = {
  33488. /* In dictionary but not in normal list. */
  33489. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  33490. /* Made up OID. */
  33491. { "1.3.5.7", "1.3.5.7" },
  33492. { NULL, NULL }
  33493. };
  33494. printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
  33495. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  33496. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  33497. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  33498. /* Test numerical value of oid (oidStr) */
  33499. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  33500. /* Convert object back to text to confirm oid is correct */
  33501. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  33502. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  33503. ASN1_OBJECT_free(obj);
  33504. XMEMSET(buf, 0, sizeof(buf));
  33505. /* Test short name (sn) */
  33506. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  33507. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  33508. /* Convert object back to text to confirm oid is correct */
  33509. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  33510. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  33511. ASN1_OBJECT_free(obj);
  33512. XMEMSET(buf, 0, sizeof(buf));
  33513. /* Test long name (ln) - should fail when no_name = 1 */
  33514. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  33515. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  33516. /* Convert object back to text to confirm oid is correct */
  33517. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  33518. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  33519. ASN1_OBJECT_free(obj);
  33520. XMEMSET(buf, 0, sizeof(buf));
  33521. }
  33522. for (i = 0; objs_named[i].numeric != NULL; i++) {
  33523. AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  33524. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  33525. AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  33526. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  33527. AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  33528. ASN1_OBJECT_free(obj);
  33529. }
  33530. printf(resultFmt, passed);
  33531. #endif
  33532. return 0;
  33533. }
  33534. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  33535. {
  33536. #if defined(OPENSSL_EXTRA) && \
  33537. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  33538. char buf[50] = {0};
  33539. ASN1_OBJECT* obj;
  33540. const char* oid = "2.5.29.19";
  33541. const char* ln = "X509v3 Basic Constraints";
  33542. printf(testingFmt, "test_wolfSSL_i2t_ASN1_OBJECT()");
  33543. obj = NULL;
  33544. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
  33545. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
  33546. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
  33547. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  33548. XMEMSET(buf, 0, sizeof(buf));
  33549. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  33550. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  33551. ASN1_OBJECT_free(obj);
  33552. printf(resultFmt, passed);
  33553. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  33554. return 0;
  33555. }
  33556. static int test_wolfSSL_PEM_write_bio_X509(void)
  33557. {
  33558. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_AKID_NAME) && \
  33559. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && \
  33560. !defined(NO_BIO) && !defined(NO_RSA)
  33561. /* This test contains the hard coded expected
  33562. * lengths. Update if necessary */
  33563. BIO* input;
  33564. BIO* output;
  33565. X509* x509a = NULL;
  33566. X509* x509b = NULL;
  33567. ASN1_TIME* notBeforeA = NULL;
  33568. ASN1_TIME* notAfterA = NULL;
  33569. ASN1_TIME* notBeforeB = NULL;
  33570. ASN1_TIME* notAfterB = NULL;
  33571. int expectedLen;
  33572. printf(testingFmt, "wolfSSL_PEM_write_bio_X509()");
  33573. AssertNotNull(input = BIO_new_file(
  33574. "certs/test/cert-ext-multiple.pem", "rb"));
  33575. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  33576. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  33577. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  33578. AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
  33579. AssertNotNull(notAfterA = X509_get_notAfter(x509a));
  33580. /* write X509 back to PEM BIO */
  33581. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  33582. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  33583. /* compare length against expected */
  33584. expectedLen = 2000;
  33585. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  33586. /* read exported X509 PEM back into struct, sanity check on export,
  33587. * make sure notBefore/notAfter are the same. */
  33588. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  33589. AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
  33590. AssertNotNull(notAfterB = X509_get_notAfter(x509b));
  33591. AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  33592. AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  33593. X509_free(x509b);
  33594. /* Reset output buffer */
  33595. BIO_free(output);
  33596. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  33597. /* Test forcing the AKID to be generated just from KeyIdentifier */
  33598. if (x509a->authKeyIdSrc != NULL) {
  33599. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  33600. x509a->authKeyId = x509a->authKeyIdSrc;
  33601. x509a->authKeyIdSrc = NULL;
  33602. x509a->authKeyIdSrcSz = 0;
  33603. }
  33604. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  33605. /* Check that we generate a smaller output since the AKID will
  33606. * only contain the KeyIdentifier without any additional
  33607. * information */
  33608. /* Here we copy the validity struct from the original */
  33609. expectedLen = 1688;
  33610. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  33611. /* Reset buffers and x509 */
  33612. BIO_free(input);
  33613. BIO_free(output);
  33614. X509_free(x509a);
  33615. /* test CA and basicConstSet values are encoded when
  33616. * the cert is a CA */
  33617. AssertNotNull(input = BIO_new_file(
  33618. "certs/server-cert.pem", "rb"));
  33619. /* read PEM into X509 struct */
  33620. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  33621. /* write X509 back to PEM BIO */
  33622. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  33623. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  33624. /* read exported X509 PEM back into struct, ensure isCa and
  33625. * basicConstSet values are maintained */
  33626. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  33627. AssertIntEQ(x509b->isCa, 1);
  33628. AssertIntEQ(x509b->basicConstSet, 1);
  33629. X509_free(x509a);
  33630. X509_free(x509b);
  33631. BIO_free(input);
  33632. BIO_free(output);
  33633. /* test CA and basicConstSet values are encoded when
  33634. * the cert is not CA */
  33635. AssertNotNull(input = BIO_new_file(
  33636. "certs/client-uri-cert.pem", "rb"));
  33637. /* read PEM into X509 struct */
  33638. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  33639. /* write X509 back to PEM BIO */
  33640. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  33641. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  33642. /* read exported X509 PEM back into struct, ensure isCa and
  33643. * basicConstSet values are maintained */
  33644. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  33645. AssertIntEQ(x509b->isCa, 0);
  33646. AssertIntEQ(x509b->basicConstSet, 1);
  33647. X509_free(x509a);
  33648. X509_free(x509b);
  33649. BIO_free(input);
  33650. BIO_free(output);
  33651. printf(resultFmt, passed);
  33652. #endif
  33653. return 0;
  33654. }
  33655. static int test_wolfSSL_X509_NAME_ENTRY(void)
  33656. {
  33657. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  33658. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  33659. X509* x509;
  33660. #ifndef NO_BIO
  33661. BIO* bio;
  33662. #endif
  33663. X509_NAME* nm;
  33664. X509_NAME_ENTRY* entry;
  33665. unsigned char cn[] = "another name to add";
  33666. #ifdef OPENSSL_ALL
  33667. int i, names_len;
  33668. #endif
  33669. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  33670. AssertNotNull(x509 =
  33671. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  33672. #ifndef NO_BIO
  33673. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33674. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  33675. #endif
  33676. #ifdef WOLFSSL_CERT_REQ
  33677. {
  33678. X509_REQ* req;
  33679. #ifndef NO_BIO
  33680. BIO* bReq;
  33681. #endif
  33682. AssertNotNull(req =
  33683. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  33684. #ifndef NO_BIO
  33685. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  33686. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  33687. BIO_free(bReq);
  33688. #endif
  33689. X509_free(req);
  33690. }
  33691. #endif
  33692. AssertNotNull(nm = X509_get_subject_name(x509));
  33693. /* Test add entry */
  33694. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  33695. 0x0c, cn, (int)sizeof(cn)));
  33696. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  33697. #ifdef WOLFSSL_CERT_EXT
  33698. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  33699. (byte*)"support@wolfssl.com", 19, -1,
  33700. 1), WOLFSSL_SUCCESS);
  33701. #endif
  33702. X509_NAME_ENTRY_free(entry);
  33703. #ifdef WOLFSSL_CERT_REQ
  33704. {
  33705. unsigned char srv_pkcs9p[] = "Server";
  33706. char* subject;
  33707. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  33708. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  33709. subject = X509_NAME_oneline(nm, 0, 0);
  33710. #ifdef DEBUG_WOLFSSL
  33711. printf("\n\t%s\n", subject);
  33712. #endif
  33713. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  33714. }
  33715. #endif
  33716. /* Test add entry by text */
  33717. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  33718. 0x0c, cn, (int)sizeof(cn)));
  33719. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  33720. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  33721. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  33722. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  33723. #endif
  33724. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  33725. X509_NAME_ENTRY_free(entry);
  33726. /* Test add entry by NID */
  33727. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  33728. cn, -1, -1, 0), SSL_SUCCESS);
  33729. #ifdef OPENSSL_ALL
  33730. /* stack of name entry */
  33731. AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  33732. for (i=0; i<names_len; i++) {
  33733. AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  33734. }
  33735. #endif
  33736. #ifndef NO_BIO
  33737. BIO_free(bio);
  33738. #endif
  33739. X509_free(x509); /* free's nm */
  33740. printf(resultFmt, passed);
  33741. #endif
  33742. return 0;
  33743. }
  33744. static int test_wolfSSL_X509_set_name(void)
  33745. {
  33746. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33747. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  33748. X509* x509;
  33749. X509_NAME* name;
  33750. printf(testingFmt, "wolfSSL_X509_set_name()");
  33751. AssertNotNull(name = X509_NAME_new());
  33752. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  33753. (byte*)"wolfssl.com", 11, 0, 1),
  33754. WOLFSSL_SUCCESS);
  33755. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  33756. (byte*)"support@wolfssl.com", 19, -1,
  33757. 1), WOLFSSL_SUCCESS);
  33758. AssertNotNull(x509 = X509_new());
  33759. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  33760. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  33761. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  33762. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  33763. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  33764. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  33765. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  33766. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  33767. X509_free(x509);
  33768. X509_NAME_free(name);
  33769. printf(resultFmt, passed);
  33770. #endif /* OPENSSL_ALL && !NO_CERTS */
  33771. return 0;
  33772. }
  33773. static int test_wolfSSL_X509_set_notAfter(void)
  33774. {
  33775. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  33776. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  33777. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  33778. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  33779. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  33780. /* Generalized time will overflow time_t if not long */
  33781. X509* x;
  33782. BIO* bio;
  33783. ASN1_TIME *asn_time, *time_check;
  33784. const int year = 365*24*60*60;
  33785. const int day = 24*60*60;
  33786. const int hour = 60*60;
  33787. const int mini = 60;
  33788. int offset_day;
  33789. unsigned char buf[25];
  33790. time_t t;
  33791. printf(testingFmt, "wolfSSL_X509_set_notAfter()");
  33792. /*
  33793. * Setup asn_time. APACHE HTTPD uses time(NULL)
  33794. */
  33795. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  33796. offset_day = 7;
  33797. /*
  33798. * Free these.
  33799. */
  33800. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  33801. AssertNotNull(asn_time);
  33802. AssertNotNull(x = X509_new());
  33803. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33804. /*
  33805. * Tests
  33806. */
  33807. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  33808. /* time_check is simply (ANS1_TIME*)x->notAfter */
  33809. AssertNotNull(time_check = X509_get_notAfter(x));
  33810. /* ANS1_TIME_check validates by checking if argument can be parsed */
  33811. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  33812. /* Convert to human readable format and compare to intended date */
  33813. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  33814. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  33815. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  33816. /*
  33817. * Cleanup
  33818. */
  33819. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  33820. X509_free(x);
  33821. BIO_free(bio);
  33822. printf(resultFmt, passed);
  33823. #endif
  33824. return 0;
  33825. }
  33826. static int test_wolfSSL_X509_set_notBefore(void)
  33827. {
  33828. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  33829. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  33830. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  33831. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  33832. X509* x;
  33833. BIO* bio;
  33834. ASN1_TIME *asn_time, *time_check;
  33835. const int year = 365*24*60*60;
  33836. const int day = 24*60*60;
  33837. const int hour = 60*60;
  33838. const int mini = 60;
  33839. int offset_day;
  33840. unsigned char buf[25];
  33841. time_t t;
  33842. printf(testingFmt, "wolfSSL_X509_set_notBefore()");
  33843. /*
  33844. * Setup asn_time. APACHE HTTPD uses time(NULL)
  33845. */
  33846. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  33847. offset_day = 7;
  33848. /*
  33849. * Free these.
  33850. */
  33851. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  33852. AssertNotNull(asn_time);
  33853. AssertNotNull(x = X509_new());
  33854. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33855. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  33856. /*
  33857. * Main Tests
  33858. */
  33859. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  33860. /* time_check == (ANS1_TIME*)x->notBefore */
  33861. AssertNotNull(time_check = X509_get_notBefore(x));
  33862. /* ANS1_TIME_check validates by checking if argument can be parsed */
  33863. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  33864. /* Convert to human readable format and compare to intended date */
  33865. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  33866. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  33867. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  33868. /*
  33869. * Cleanup
  33870. */
  33871. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  33872. X509_free(x);
  33873. BIO_free(bio);
  33874. printf(resultFmt, passed);
  33875. #endif
  33876. return 0;
  33877. }
  33878. static int test_wolfSSL_X509_set_version(void)
  33879. {
  33880. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33881. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  33882. X509* x509;
  33883. long v = 2L;
  33884. long maxInt = INT_MAX;
  33885. AssertNotNull(x509 = X509_new());
  33886. /* These should pass. */
  33887. AssertTrue(wolfSSL_X509_set_version(x509, v));
  33888. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  33889. /* Fail Case: When v(long) is greater than x509->version(int). */
  33890. v = maxInt+1;
  33891. AssertFalse(wolfSSL_X509_set_version(x509, v));
  33892. /* Cleanup */
  33893. X509_free(x509);
  33894. printf(resultFmt, passed);
  33895. #endif
  33896. return 0;
  33897. }
  33898. #ifndef NO_BIO
  33899. static int test_wolfSSL_BIO_gets(void)
  33900. {
  33901. #if defined(OPENSSL_EXTRA)
  33902. BIO* bio;
  33903. BIO* bio2;
  33904. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  33905. char emp[] = "";
  33906. char bio_buffer[20];
  33907. int bufferSz = 20;
  33908. printf(testingFmt, "wolfSSL_BIO_gets()");
  33909. /* try with bad args */
  33910. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  33911. /* try with real msg */
  33912. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  33913. XMEMSET(bio_buffer, 0, bufferSz);
  33914. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  33915. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  33916. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  33917. AssertFalse(bio2 != BIO_next(bio));
  33918. /* make buffer filled with no terminating characters */
  33919. XMEMSET(bio_buffer, 1, bufferSz);
  33920. /* BIO_gets reads a line of data */
  33921. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  33922. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  33923. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  33924. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  33925. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  33926. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  33927. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  33928. /* check not null terminated string */
  33929. BIO_free(bio);
  33930. msg[0] = 0x33;
  33931. msg[1] = 0x33;
  33932. msg[2] = 0x33;
  33933. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  33934. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  33935. AssertIntEQ(bio_buffer[0], msg[0]);
  33936. AssertIntEQ(bio_buffer[1], msg[1]);
  33937. AssertIntNE(bio_buffer[2], msg[2]);
  33938. BIO_free(bio);
  33939. msg[3] = 0x33;
  33940. bio_buffer[3] = 0x33;
  33941. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  33942. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  33943. AssertIntEQ(bio_buffer[0], msg[0]);
  33944. AssertIntEQ(bio_buffer[1], msg[1]);
  33945. AssertIntEQ(bio_buffer[2], msg[2]);
  33946. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  33947. /* check reading an empty string */
  33948. BIO_free(bio);
  33949. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  33950. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  33951. AssertStrEQ(emp, bio_buffer);
  33952. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  33953. /* check error cases */
  33954. BIO_free(bio);
  33955. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  33956. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33957. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  33958. #if !defined(NO_FILESYSTEM)
  33959. {
  33960. BIO* f_bio;
  33961. XFILE f;
  33962. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  33963. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  33964. f = XFOPEN(svrCertFile, "rb");
  33965. AssertTrue((f != XBADFILE));
  33966. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  33967. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  33968. BIO_free(f_bio);
  33969. }
  33970. #endif /* NO_FILESYSTEM */
  33971. BIO_free(bio);
  33972. BIO_free(bio2);
  33973. /* try with type BIO */
  33974. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  33975. sizeof(msg));
  33976. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  33977. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  33978. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  33979. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  33980. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  33981. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  33982. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  33983. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  33984. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  33985. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  33986. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  33987. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  33988. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  33989. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  33990. BIO_free(bio);
  33991. BIO_free(bio2);
  33992. /* check reading an empty string */
  33993. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  33994. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  33995. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  33996. AssertStrEQ(emp, bio_buffer);
  33997. BIO_free(bio);
  33998. printf(resultFmt, passed);
  33999. #endif
  34000. return 0;
  34001. }
  34002. static int test_wolfSSL_BIO_puts(void)
  34003. {
  34004. #if defined(OPENSSL_EXTRA)
  34005. BIO* bio;
  34006. char input[] = "hello\0world\n.....ok\n\0";
  34007. char output[128];
  34008. printf(testingFmt, "wolfSSL_BIO_puts()");
  34009. XMEMSET(output, 0, sizeof(output));
  34010. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34011. AssertIntEQ(BIO_puts(bio, input), 5);
  34012. AssertIntEQ(BIO_pending(bio), 5);
  34013. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  34014. AssertIntEQ(BIO_pending(bio), 19);
  34015. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  34016. AssertStrEQ(output, "helloworld\n");
  34017. AssertIntEQ(BIO_pending(bio), 8);
  34018. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  34019. AssertStrEQ(output, ".....ok\n");
  34020. AssertIntEQ(BIO_pending(bio), 0);
  34021. AssertIntEQ(BIO_puts(bio, ""), -1);
  34022. BIO_free(bio);
  34023. printf(resultFmt, passed);
  34024. #endif
  34025. return 0;
  34026. }
  34027. static int test_wolfSSL_BIO_dump(void)
  34028. {
  34029. #if defined(OPENSSL_EXTRA)
  34030. BIO* bio;
  34031. static const unsigned char data[] = {
  34032. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  34033. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  34034. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  34035. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  34036. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  34037. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  34038. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  34039. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  34040. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  34041. 0xB4
  34042. };
  34043. /* Generated with OpenSSL. */
  34044. static const char expected[] =
  34045. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  34046. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  34047. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  34048. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  34049. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  34050. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  34051. static const char expectedAll[] =
  34052. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  34053. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  34054. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  34055. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  34056. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  34057. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  34058. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  34059. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  34060. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  34061. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  34062. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  34063. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  34064. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  34065. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  34066. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  34067. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  34068. char output[16 * 80];
  34069. int i;
  34070. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34071. /* Example key dumped. */
  34072. AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  34073. sizeof(expected) - 1);
  34074. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  34075. AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  34076. /* Try every possible value for a character. */
  34077. for (i = 0; i < 256; i++)
  34078. output[i] = i;
  34079. AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  34080. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  34081. AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  34082. BIO_free(bio);
  34083. printf(resultFmt, passed);
  34084. #endif
  34085. return 0;
  34086. }
  34087. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  34088. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  34089. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  34090. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  34091. {
  34092. (void)ssl;
  34093. (void)buf;
  34094. (void)sz;
  34095. (void)ctx;
  34096. return WOLFSSL_CBIO_ERR_WANT_READ;
  34097. }
  34098. #endif
  34099. static int test_wolfSSL_BIO_should_retry(void)
  34100. {
  34101. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  34102. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  34103. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  34104. tcp_ready ready;
  34105. func_args server_args;
  34106. THREAD_TYPE serverThread;
  34107. SOCKET_T sockfd = 0;
  34108. WOLFSSL_CTX* ctx;
  34109. WOLFSSL* ssl;
  34110. char msg[64] = "hello wolfssl!";
  34111. char reply[1024];
  34112. int msgSz = (int)XSTRLEN(msg);
  34113. int ret;
  34114. BIO* bio;
  34115. printf(testingFmt, "wolfSSL_BIO_should_retry()");
  34116. XMEMSET(&server_args, 0, sizeof(func_args));
  34117. #ifdef WOLFSSL_TIRTOS
  34118. fdOpenSession(Task_self());
  34119. #endif
  34120. StartTCP();
  34121. InitTcpReady(&ready);
  34122. #if defined(USE_WINDOWS_API)
  34123. /* use RNG to get random port if using windows */
  34124. ready.port = GetRandomPort();
  34125. #endif
  34126. server_args.signal = &ready;
  34127. start_thread(test_server_nofail, &server_args, &serverThread);
  34128. wait_tcp_ready(&server_args);
  34129. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34130. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  34131. AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  34132. #endif
  34133. AssertIntEQ(WOLFSSL_SUCCESS,
  34134. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  34135. AssertIntEQ(WOLFSSL_SUCCESS,
  34136. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34137. AssertIntEQ(WOLFSSL_SUCCESS,
  34138. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34139. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  34140. /* force retry */
  34141. ssl = wolfSSL_new(ctx);
  34142. AssertNotNull(ssl);
  34143. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  34144. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  34145. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  34146. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  34147. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  34148. AssertIntNE(BIO_should_retry(bio), 0);
  34149. /* now perform successful connection */
  34150. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  34151. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  34152. BIO_read(bio, reply, sizeof(reply));
  34153. ret = wolfSSL_get_error(ssl, -1);
  34154. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  34155. AssertIntNE(BIO_should_retry(bio), 0);
  34156. }
  34157. else {
  34158. AssertIntEQ(BIO_should_retry(bio), 0);
  34159. }
  34160. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  34161. XSTRLEN("I hear you fa shizzle!")), 0);
  34162. BIO_free(bio);
  34163. wolfSSL_CTX_free(ctx);
  34164. join_thread(serverThread);
  34165. FreeTcpReady(&ready);
  34166. #ifdef WOLFSSL_TIRTOS
  34167. fdOpenSession(Task_self());
  34168. #endif
  34169. printf(resultFmt, passed);
  34170. #endif
  34171. return 0;
  34172. }
  34173. static int test_wolfSSL_BIO_connect(void)
  34174. {
  34175. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34176. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  34177. tcp_ready ready;
  34178. func_args server_args;
  34179. THREAD_TYPE serverThread;
  34180. BIO *tcpBio;
  34181. BIO *sslBio;
  34182. SSL_CTX* ctx;
  34183. SSL *ssl;
  34184. SSL *sslPtr;
  34185. char msg[] = "hello wolfssl!";
  34186. char reply[30];
  34187. char buff[10] = {0};
  34188. printf(testingFmt, "wolfSSL_BIO_new_connect()");
  34189. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34190. AssertIntEQ(WOLFSSL_SUCCESS,
  34191. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  34192. AssertIntEQ(WOLFSSL_SUCCESS,
  34193. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34194. AssertIntEQ(WOLFSSL_SUCCESS,
  34195. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34196. /* Setup server */
  34197. XMEMSET(&server_args, 0, sizeof(func_args));
  34198. StartTCP();
  34199. InitTcpReady(&ready);
  34200. #if defined(USE_WINDOWS_API)
  34201. /* use RNG to get random port if using windows */
  34202. ready.port = GetRandomPort();
  34203. #endif
  34204. server_args.signal = &ready;
  34205. start_thread(test_server_nofail, &server_args, &serverThread);
  34206. wait_tcp_ready(&server_args);
  34207. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  34208. /* Start the test proper */
  34209. /* Setup the TCP BIO */
  34210. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  34211. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  34212. /* Setup the SSL object */
  34213. AssertNotNull(ssl = SSL_new(ctx));
  34214. SSL_set_connect_state(ssl);
  34215. /* Setup the SSL BIO */
  34216. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  34217. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  34218. /* Verify that BIO_get_ssl works. */
  34219. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  34220. AssertPtrEq(ssl, sslPtr);
  34221. /* Link BIO's so that sslBio uses tcpBio for IO */
  34222. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  34223. /* Do TCP connect */
  34224. AssertIntEQ(BIO_do_connect(sslBio), 1);
  34225. /* Do TLS handshake */
  34226. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  34227. /* Test writing */
  34228. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  34229. /* Expect length of default wolfSSL reply */
  34230. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  34231. /* Clean it all up */
  34232. BIO_free_all(sslBio);
  34233. /* Server clean up */
  34234. join_thread(serverThread);
  34235. FreeTcpReady(&ready);
  34236. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  34237. * after. */
  34238. XMEMSET(&server_args, 0, sizeof(func_args));
  34239. StartTCP();
  34240. InitTcpReady(&ready);
  34241. #if defined(USE_WINDOWS_API)
  34242. /* use RNG to get random port if using windows */
  34243. ready.port = GetRandomPort();
  34244. #endif
  34245. server_args.signal = &ready;
  34246. start_thread(test_server_nofail, &server_args, &serverThread);
  34247. wait_tcp_ready(&server_args);
  34248. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  34249. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  34250. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  34251. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  34252. AssertIntEQ(BIO_do_connect(sslBio), 1);
  34253. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  34254. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  34255. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  34256. /* Attempt to close the TLS connection gracefully. */
  34257. BIO_ssl_shutdown(sslBio);
  34258. BIO_free_all(sslBio);
  34259. join_thread(serverThread);
  34260. FreeTcpReady(&ready);
  34261. SSL_CTX_free(ctx);
  34262. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  34263. wc_ecc_fp_free(); /* free per thread cache */
  34264. #endif
  34265. printf(resultFmt, passed);
  34266. #endif
  34267. return 0;
  34268. }
  34269. static int test_wolfSSL_BIO_tls(void)
  34270. {
  34271. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  34272. SSL_CTX* ctx;
  34273. SSL *ssl;
  34274. BIO *readBio;
  34275. BIO *writeBio;
  34276. int ret, err = 0;
  34277. printf(testingFmt, "test_wolfSSL_BIO_tls()");
  34278. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  34279. AssertNotNull(ssl = SSL_new(ctx));
  34280. AssertNotNull(readBio = BIO_new(BIO_s_mem()));
  34281. AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
  34282. /* Qt reads data from write-bio,
  34283. * then writes the read data into plain packet.
  34284. * Qt reads data from plain packet,
  34285. * then writes the read data into read-bio.
  34286. */
  34287. SSL_set_bio(ssl, readBio, writeBio);
  34288. do {
  34289. #ifdef WOLFSSL_ASYNC_CRYPT
  34290. if (err == WC_PENDING_E) {
  34291. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34292. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34293. }
  34294. #endif
  34295. ret = SSL_connect(ssl);
  34296. err = SSL_get_error(ssl, 0);
  34297. } while (err == WC_PENDING_E);
  34298. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  34299. /* in this use case, should return WANT READ
  34300. * so that Qt will read the data from plain packet for next state.
  34301. */
  34302. AssertIntEQ(err, SSL_ERROR_WANT_READ);
  34303. SSL_free(ssl);
  34304. SSL_CTX_free(ctx);
  34305. printf(resultFmt, passed);
  34306. #endif
  34307. return 0;
  34308. }
  34309. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  34310. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  34311. {
  34312. BIO* clientBio;
  34313. SSL* sslClient;
  34314. SSL_CTX* ctx;
  34315. char connectAddr[20]; /* IP + port */;
  34316. (void)args;
  34317. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  34318. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  34319. AssertIntEQ(BIO_do_connect(clientBio), 1);
  34320. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  34321. AssertNotNull(sslClient = SSL_new(ctx));
  34322. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  34323. SSL_set_bio(sslClient, clientBio, clientBio);
  34324. AssertIntEQ(SSL_connect(sslClient), 1);
  34325. SSL_free(sslClient);
  34326. SSL_CTX_free(ctx);
  34327. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  34328. wc_ecc_fp_free(); /* free per thread cache */
  34329. #endif
  34330. return 0;
  34331. }
  34332. #endif
  34333. static int test_wolfSSL_BIO_accept(void)
  34334. {
  34335. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  34336. BIO* serverBindBio;
  34337. BIO* serverAcceptBio;
  34338. SSL* sslServer;
  34339. SSL_CTX* ctx;
  34340. func_args args;
  34341. THREAD_TYPE thread;
  34342. char port[10]; /* 10 bytes should be enough to store the string
  34343. * representation of the port */
  34344. printf(testingFmt, "wolfSSL_BIO_new_accept()");
  34345. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  34346. AssertNotNull(serverBindBio = BIO_new_accept(port));
  34347. /* First BIO_do_accept binds the port */
  34348. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  34349. XMEMSET(&args, 0, sizeof(func_args));
  34350. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  34351. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  34352. /* Let's plug it into SSL to test */
  34353. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  34354. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  34355. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  34356. AssertNotNull(sslServer = SSL_new(ctx));
  34357. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  34358. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  34359. AssertIntEQ(SSL_accept(sslServer), 1);
  34360. join_thread(thread);
  34361. BIO_free(serverBindBio);
  34362. SSL_free(sslServer);
  34363. SSL_CTX_free(ctx);
  34364. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  34365. wc_ecc_fp_free(); /* free per thread cache */
  34366. #endif
  34367. printf(resultFmt, passed);
  34368. #endif
  34369. return 0;
  34370. }
  34371. static int test_wolfSSL_BIO_write(void)
  34372. {
  34373. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  34374. BIO* bio;
  34375. BIO* bio64;
  34376. BIO* ptr;
  34377. int sz;
  34378. char msg[] = "conversion test";
  34379. char out[40];
  34380. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  34381. void* bufPtr = NULL;
  34382. BUF_MEM* buf = NULL;
  34383. printf(testingFmt, "wolfSSL_BIO_write()");
  34384. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  34385. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  34386. /* now should convert to base64 then write to memory */
  34387. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  34388. BIO_flush(bio);
  34389. /* test BIO chain */
  34390. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  34391. AssertNotNull(buf);
  34392. AssertIntEQ(buf->length, 25);
  34393. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  34394. AssertPtrEq(buf->data, bufPtr);
  34395. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  34396. sz = sizeof(out);
  34397. XMEMSET(out, 0, sz);
  34398. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  34399. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  34400. /* write then read should return the same message */
  34401. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  34402. sz = sizeof(out);
  34403. XMEMSET(out, 0, sz);
  34404. AssertIntEQ(BIO_read(bio, out, sz), 16);
  34405. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  34406. /* now try encoding with no line ending */
  34407. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  34408. #ifdef HAVE_EX_DATA
  34409. BIO_set_ex_data(bio64, 0, (void*) "data");
  34410. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  34411. #endif
  34412. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  34413. BIO_flush(bio);
  34414. sz = sizeof(out);
  34415. XMEMSET(out, 0, sz);
  34416. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  34417. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  34418. BIO_free_all(bio); /* frees bio64 also */
  34419. /* test with more than one bio64 in list */
  34420. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  34421. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  34422. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  34423. /* now should convert to base64 when stored and then decode with read */
  34424. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  34425. BIO_flush(bio);
  34426. sz = sizeof(out);
  34427. XMEMSET(out, 0, sz);
  34428. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  34429. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  34430. BIO_clear_flags(bio64, ~0);
  34431. BIO_set_retry_read(bio);
  34432. BIO_free_all(bio); /* frees bio64s also */
  34433. AssertNotNull(bio = BIO_new_mem_buf(out, 0));
  34434. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  34435. BIO_free(bio);
  34436. printf(resultFmt, passed);
  34437. #endif
  34438. return 0;
  34439. }
  34440. static int test_wolfSSL_BIO_printf(void)
  34441. {
  34442. #if defined(OPENSSL_ALL)
  34443. BIO* bio;
  34444. int sz = 7;
  34445. char msg[] = "TLS 1.3 for the world";
  34446. char out[60];
  34447. char expected[] = "TLS 1.3 for the world : sz = 7";
  34448. printf(testingFmt, "wolfSSL_BIO_printf()");
  34449. XMEMSET(out, 0, sizeof(out));
  34450. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34451. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  34452. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  34453. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  34454. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  34455. BIO_free(bio);
  34456. printf(resultFmt, passed);
  34457. #endif
  34458. return 0;
  34459. }
  34460. static int test_wolfSSL_BIO_f_md(void)
  34461. {
  34462. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  34463. BIO *bio, *mem;
  34464. char msg[] = "message to hash";
  34465. char out[60];
  34466. EVP_MD_CTX* ctx;
  34467. const unsigned char testKey[] =
  34468. {
  34469. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  34470. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  34471. 0x0b, 0x0b, 0x0b, 0x0b
  34472. };
  34473. const char testData[] = "Hi There";
  34474. const unsigned char testResult[] =
  34475. {
  34476. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  34477. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  34478. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  34479. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  34480. };
  34481. const unsigned char expectedHash[] =
  34482. {
  34483. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  34484. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  34485. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  34486. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  34487. };
  34488. const unsigned char emptyHash[] =
  34489. {
  34490. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  34491. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  34492. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  34493. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  34494. };
  34495. unsigned char check[sizeof(testResult) + 1];
  34496. size_t checkSz = -1;
  34497. EVP_PKEY* key;
  34498. printf(testingFmt, "wolfSSL_BIO_f_md()");
  34499. XMEMSET(out, 0, sizeof(out));
  34500. AssertNotNull(bio = BIO_new(BIO_f_md()));
  34501. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  34502. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  34503. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  34504. /* should not be able to write/read yet since just digest wrapper and no
  34505. * data is passing through the bio */
  34506. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  34507. AssertIntEQ(BIO_pending(bio), 0);
  34508. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  34509. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  34510. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  34511. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  34512. BIO_reset(bio);
  34513. /* append BIO mem to bio in order to read/write */
  34514. AssertNotNull(bio = BIO_push(bio, mem));
  34515. XMEMSET(out, 0, sizeof(out));
  34516. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  34517. AssertIntEQ(BIO_pending(bio), 16);
  34518. /* this just reads the message and does not hash it (gets calls final) */
  34519. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  34520. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  34521. /* create a message digest using BIO */
  34522. XMEMSET(out, 0, sizeof(out));
  34523. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  34524. AssertIntEQ(BIO_pending(mem), 16);
  34525. AssertIntEQ(BIO_pending(bio), 16);
  34526. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  34527. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  34528. BIO_free(bio);
  34529. BIO_free(mem);
  34530. /* test with HMAC */
  34531. XMEMSET(out, 0, sizeof(out));
  34532. AssertNotNull(bio = BIO_new(BIO_f_md()));
  34533. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  34534. BIO_get_md_ctx(bio, &ctx);
  34535. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  34536. testKey, (int)sizeof(testKey)));
  34537. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  34538. AssertNotNull(bio = BIO_push(bio, mem));
  34539. BIO_write(bio, testData, (int)strlen(testData));
  34540. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  34541. EVP_DigestSignFinal(ctx, check, &checkSz);
  34542. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  34543. EVP_PKEY_free(key);
  34544. BIO_free(bio);
  34545. BIO_free(mem);
  34546. printf(resultFmt, passed);
  34547. #endif
  34548. return 0;
  34549. }
  34550. static int test_wolfSSL_BIO_up_ref(void)
  34551. {
  34552. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  34553. BIO* bio;
  34554. printf(testingFmt, "wolfSSL_BIO_up_ref()");
  34555. AssertNotNull(bio = BIO_new(BIO_f_md()));
  34556. AssertIntEQ(BIO_up_ref(NULL), 0);
  34557. AssertIntEQ(BIO_up_ref(bio), 1);
  34558. BIO_free(bio);
  34559. AssertIntEQ(BIO_up_ref(bio), 1);
  34560. BIO_free(bio);
  34561. BIO_free(bio);
  34562. printf(resultFmt, "passed");
  34563. #endif
  34564. return 0;
  34565. }
  34566. #endif /* !NO_BIO */
  34567. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  34568. /* test that the callback arg is correct */
  34569. static int certCbArg = 0;
  34570. static int clientCertCb(WOLFSSL* ssl, void* arg)
  34571. {
  34572. if (ssl == NULL || arg != &certCbArg)
  34573. return 0;
  34574. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  34575. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  34576. return 0;
  34577. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  34578. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  34579. return 0;
  34580. return 1;
  34581. }
  34582. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  34583. {
  34584. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  34585. }
  34586. /**
  34587. * This is only done because test_client_nofail has no way to stop
  34588. * certificate and key loading
  34589. */
  34590. static void clientCertClearCb(WOLFSSL* ssl)
  34591. {
  34592. /* Clear the loaded certs to force the callbacks to set them up */
  34593. SSL_certs_clear(ssl);
  34594. }
  34595. static int serverCertCb(WOLFSSL* ssl, void* arg)
  34596. {
  34597. if (ssl == NULL || arg != &certCbArg)
  34598. return 0;
  34599. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  34600. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  34601. return 0;
  34602. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  34603. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  34604. return 0;
  34605. return 1;
  34606. }
  34607. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  34608. {
  34609. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  34610. }
  34611. /**
  34612. * This is only done because test_server_nofail has no way to stop
  34613. * certificate and key loading
  34614. */
  34615. static void serverCertClearCb(WOLFSSL* ssl)
  34616. {
  34617. /* Clear the loaded certs to force the callbacks to set them up */
  34618. SSL_certs_clear(ssl);
  34619. }
  34620. #endif
  34621. static int test_wolfSSL_cert_cb(void)
  34622. {
  34623. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  34624. callback_functions func_cb_client;
  34625. callback_functions func_cb_server;
  34626. tcp_ready ready;
  34627. func_args client_args;
  34628. func_args server_args;
  34629. THREAD_TYPE serverThread;
  34630. XMEMSET(&client_args, 0, sizeof(func_args));
  34631. XMEMSET(&server_args, 0, sizeof(func_args));
  34632. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  34633. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  34634. #ifdef WOLFSSL_TIRTOS
  34635. fdOpenSession(Task_self());
  34636. #endif
  34637. StartTCP();
  34638. InitTcpReady(&ready);
  34639. #if defined(USE_WINDOWS_API)
  34640. /* use RNG to get random port if using windows */
  34641. ready.port = GetRandomPort();
  34642. #endif
  34643. server_args.signal = &ready;
  34644. client_args.signal = &ready;
  34645. client_args.callbacks = &func_cb_client;
  34646. server_args.callbacks = &func_cb_server;
  34647. func_cb_client.ctx_ready = clientCertSetupCb;
  34648. func_cb_client.ssl_ready = clientCertClearCb;
  34649. func_cb_server.ctx_ready = serverCertSetupCb;
  34650. func_cb_server.ssl_ready = serverCertClearCb;
  34651. start_thread(test_server_nofail, &server_args, &serverThread);
  34652. wait_tcp_ready(&server_args);
  34653. test_client_nofail(&client_args, NULL);
  34654. join_thread(serverThread);
  34655. AssertTrue(client_args.return_code);
  34656. AssertTrue(server_args.return_code);
  34657. FreeTcpReady(&ready);
  34658. #ifdef WOLFSSL_TIRTOS
  34659. fdOpenSession(Task_self());
  34660. #endif
  34661. #endif
  34662. return 0;
  34663. }
  34664. static int test_wolfSSL_SESSION(void)
  34665. {
  34666. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  34667. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34668. !defined(NO_SESSION_CACHE)
  34669. WOLFSSL* ssl;
  34670. WOLFSSL_CTX* ctx;
  34671. WOLFSSL_SESSION* sess;
  34672. WOLFSSL_SESSION* sess_copy;
  34673. #ifdef OPENSSL_EXTRA
  34674. unsigned char* sessDer = NULL;
  34675. unsigned char* ptr = NULL;
  34676. const unsigned char context[] = "user app context";
  34677. unsigned int contextSz = (unsigned int)sizeof(context);
  34678. int sz;
  34679. #endif
  34680. int ret, err;
  34681. SOCKET_T sockfd;
  34682. tcp_ready ready;
  34683. func_args server_args;
  34684. THREAD_TYPE serverThread;
  34685. char msg[80];
  34686. const char* sendGET = "GET";
  34687. printf(testingFmt, "wolfSSL_SESSION()");
  34688. /* TLS v1.3 requires session tickets */
  34689. /* CHACHA and POLY1305 required for myTicketEncCb */
  34690. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  34691. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  34692. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  34693. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  34694. #else
  34695. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34696. #endif
  34697. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  34698. WOLFSSL_FILETYPE_PEM));
  34699. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  34700. WOLFSSL_FILETYPE_PEM));
  34701. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  34702. WOLFSSL_SUCCESS);
  34703. #ifdef WOLFSSL_ENCRYPTED_KEYS
  34704. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  34705. #endif
  34706. #ifdef HAVE_SESSION_TICKET
  34707. /* Use session tickets, for ticket tests below */
  34708. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  34709. #endif
  34710. XMEMSET(&server_args, 0, sizeof(func_args));
  34711. #ifdef WOLFSSL_TIRTOS
  34712. fdOpenSession(Task_self());
  34713. #endif
  34714. StartTCP();
  34715. InitTcpReady(&ready);
  34716. #if defined(USE_WINDOWS_API)
  34717. /* use RNG to get random port if using windows */
  34718. ready.port = GetRandomPort();
  34719. #endif
  34720. server_args.signal = &ready;
  34721. start_thread(test_server_nofail, &server_args, &serverThread);
  34722. wait_tcp_ready(&server_args);
  34723. /* client connection */
  34724. ssl = wolfSSL_new(ctx);
  34725. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  34726. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  34727. #ifdef WOLFSSL_ASYNC_CRYPT
  34728. err = 0; /* Reset error */
  34729. #endif
  34730. do {
  34731. #ifdef WOLFSSL_ASYNC_CRYPT
  34732. if (err == WC_PENDING_E) {
  34733. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34734. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34735. }
  34736. #endif
  34737. ret = wolfSSL_connect(ssl);
  34738. err = wolfSSL_get_error(ssl, 0);
  34739. } while (err == WC_PENDING_E);
  34740. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  34741. #ifdef WOLFSSL_ASYNC_CRYPT
  34742. err = 0; /* Reset error */
  34743. #endif
  34744. do {
  34745. #ifdef WOLFSSL_ASYNC_CRYPT
  34746. if (err == WC_PENDING_E) {
  34747. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34748. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34749. }
  34750. #endif
  34751. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  34752. err = wolfSSL_get_error(ssl, 0);
  34753. } while (err == WC_PENDING_E);
  34754. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  34755. #ifdef WOLFSSL_ASYNC_CRYPT
  34756. err = 0; /* Reset error */
  34757. #endif
  34758. do {
  34759. #ifdef WOLFSSL_ASYNC_CRYPT
  34760. if (err == WC_PENDING_E) {
  34761. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34762. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34763. }
  34764. #endif
  34765. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  34766. err = wolfSSL_get_error(ssl, 0);
  34767. } while (err == WC_PENDING_E);
  34768. AssertIntEQ(ret, 23);
  34769. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  34770. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  34771. #ifdef HAVE_EXT_CACHE
  34772. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  34773. * HAVE_EXT_CACHE we get new objects each time */
  34774. #endif
  34775. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  34776. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  34777. sess = wolfSSL_get_session(ssl);
  34778. #ifdef OPENSSL_EXTRA
  34779. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  34780. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  34781. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  34782. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  34783. #ifdef HAVE_SESSION_TICKET
  34784. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  34785. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  34786. SESSION_TICKET_HINT_DEFAULT);
  34787. #else
  34788. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  34789. #endif
  34790. #else
  34791. (void)sess;
  34792. #endif /* OPENSSL_EXTRA */
  34793. /* Retain copy of the session for later testing */
  34794. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  34795. wolfSSL_shutdown(ssl);
  34796. wolfSSL_free(ssl);
  34797. join_thread(serverThread);
  34798. FreeTcpReady(&ready);
  34799. #ifdef WOLFSSL_TIRTOS
  34800. fdOpenSession(Task_self());
  34801. #endif
  34802. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  34803. {
  34804. X509 *x509;
  34805. char buf[30];
  34806. int bufSz;
  34807. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  34808. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  34809. X509_get_subject_name(x509), NID_organizationalUnitName,
  34810. buf, sizeof(buf))), 0);
  34811. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  34812. if (bufSz == 7) {
  34813. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  34814. }
  34815. if (bufSz == 16) {
  34816. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  34817. }
  34818. }
  34819. #endif
  34820. #ifdef HAVE_EXT_CACHE
  34821. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  34822. wolfSSL_SESSION_free(sess_copy);
  34823. sess_copy = NULL;
  34824. #endif
  34825. #ifdef OPENSSL_EXTRA
  34826. /* get session from DER and update the timeout */
  34827. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  34828. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  34829. wolfSSL_SESSION_free(sess);
  34830. sess = NULL;
  34831. ptr = sessDer;
  34832. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  34833. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  34834. (const unsigned char**)&ptr, sz));
  34835. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  34836. sessDer = NULL;
  34837. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  34838. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  34839. #endif
  34840. /* successful set session test */
  34841. AssertNotNull(ssl = wolfSSL_new(ctx));
  34842. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  34843. #ifdef HAVE_SESSION_TICKET
  34844. /* Test set/get session ticket */
  34845. {
  34846. const char* ticket = "This is a session ticket";
  34847. char buf[64] = {0};
  34848. word32 bufSz = (word32)sizeof(buf);
  34849. AssertIntEQ(SSL_SUCCESS,
  34850. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  34851. (word32)XSTRLEN(ticket)));
  34852. AssertIntEQ(SSL_SUCCESS,
  34853. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  34854. AssertStrEQ(ticket, buf);
  34855. }
  34856. #endif
  34857. #ifdef OPENSSL_EXTRA
  34858. /* session timeout case */
  34859. /* make the session to be expired */
  34860. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  34861. XSLEEP_MS(1200);
  34862. /* SSL_set_session should reject specified session but return success
  34863. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  34864. */
  34865. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  34866. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  34867. #else
  34868. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  34869. #endif
  34870. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  34871. /* fail case with miss match session context IDs (use compatibility API) */
  34872. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  34873. SSL_SUCCESS);
  34874. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  34875. wolfSSL_free(ssl);
  34876. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  34877. SSL_FAILURE);
  34878. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  34879. SSL_SUCCESS);
  34880. AssertNotNull(ssl = wolfSSL_new(ctx));
  34881. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  34882. #endif /* OPENSSL_EXTRA */
  34883. wolfSSL_free(ssl);
  34884. wolfSSL_SESSION_free(sess);
  34885. wolfSSL_CTX_free(ctx);
  34886. printf(resultFmt, passed);
  34887. #endif
  34888. return 0;
  34889. }
  34890. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34891. defined(HAVE_EX_DATA)
  34892. static int clientSessRemCountMalloc = 0;
  34893. static int serverSessRemCountMalloc = 0;
  34894. static int clientSessRemCountFree = 0;
  34895. static int serverSessRemCountFree = 0;
  34896. static WOLFSSL_CTX* serverSessCtx = NULL;
  34897. static WOLFSSL_SESSION* serverSess = NULL;
  34898. #ifndef NO_SESSION_CACHE_REF
  34899. static WOLFSSL_CTX* clientSessCtx = NULL;
  34900. static WOLFSSL_SESSION* clientSess = NULL;
  34901. #endif
  34902. static int serverSessRemIdx = 3;
  34903. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  34904. {
  34905. int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  34906. (void)ctx;
  34907. AssertNotNull(mallocedData);
  34908. if (!*mallocedData)
  34909. clientSessRemCountFree++;
  34910. else
  34911. serverSessRemCountFree++;
  34912. XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
  34913. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  34914. }
  34915. static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  34916. {
  34917. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  34918. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  34919. !defined(NO_SESSION_CACHE_REF)
  34920. /* Allow downgrade, set min version, and disable TLS 1.3.
  34921. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  34922. * reference to the session cache. But with WOLFSSL_TLS13 and without
  34923. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  34924. * session in the cache. In this case we need to downgrade to previous
  34925. * versions to just use the legacy session ID field. */
  34926. AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  34927. AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
  34928. #endif
  34929. }
  34930. static void SessRemSslSetupCb(WOLFSSL* ssl)
  34931. {
  34932. int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
  34933. AssertNotNull(mallocedData);
  34934. *mallocedData = SSL_is_server(ssl);
  34935. if (!*mallocedData) {
  34936. clientSessRemCountMalloc++;
  34937. #ifndef NO_SESSION_CACHE_REF
  34938. AssertNotNull(clientSess = SSL_get1_session(ssl));
  34939. AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  34940. SSL_SUCCESS);
  34941. #endif
  34942. }
  34943. else {
  34944. serverSessRemCountMalloc++;
  34945. AssertNotNull(serverSess = SSL_get1_session(ssl));
  34946. AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  34947. SSL_SUCCESS);
  34948. }
  34949. AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
  34950. mallocedData), SSL_SUCCESS);
  34951. }
  34952. #endif
  34953. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  34954. {
  34955. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34956. defined(HAVE_EX_DATA)
  34957. /* Check that the remove callback gets called for external data in a
  34958. * session object */
  34959. callback_functions func_cb;
  34960. tcp_ready ready;
  34961. func_args client_args;
  34962. func_args server_args;
  34963. THREAD_TYPE serverThread;
  34964. printf(testingFmt, "wolfSSL_CTX_sess_set_remove_cb()");
  34965. XMEMSET(&client_args, 0, sizeof(func_args));
  34966. XMEMSET(&server_args, 0, sizeof(func_args));
  34967. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  34968. #ifdef WOLFSSL_TIRTOS
  34969. fdOpenSession(Task_self());
  34970. #endif
  34971. StartTCP();
  34972. InitTcpReady(&ready);
  34973. #if defined(USE_WINDOWS_API)
  34974. /* use RNG to get random port if using windows */
  34975. ready.port = GetRandomPort();
  34976. #endif
  34977. server_args.signal = &ready;
  34978. client_args.signal = &ready;
  34979. client_args.callbacks = &func_cb;
  34980. server_args.callbacks = &func_cb;
  34981. func_cb.ctx_ready = SessRemCtxSetupCb;
  34982. func_cb.on_result = SessRemSslSetupCb;
  34983. start_thread(test_server_nofail, &server_args, &serverThread);
  34984. wait_tcp_ready(&server_args);
  34985. test_client_nofail(&client_args, NULL);
  34986. join_thread(serverThread);
  34987. AssertTrue(client_args.return_code);
  34988. AssertTrue(server_args.return_code);
  34989. FreeTcpReady(&ready);
  34990. #ifdef WOLFSSL_TIRTOS
  34991. fdOpenSession(Task_self());
  34992. #endif
  34993. /* Both should have been allocated */
  34994. AssertIntEQ(clientSessRemCountMalloc, 1);
  34995. AssertIntEQ(serverSessRemCountMalloc, 1);
  34996. #ifdef NO_SESSION_CACHE_REF
  34997. /* Client session should not be added to cache so this should be free'd when
  34998. * the SSL object was being free'd */
  34999. AssertIntEQ(clientSessRemCountFree, 1);
  35000. #else
  35001. /* Client session is in cache due to requiring a persistent reference */
  35002. AssertIntEQ(clientSessRemCountFree, 0);
  35003. /* Force a cache lookup */
  35004. AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  35005. /* Force a cache update */
  35006. AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  35007. /* This should set the timeout to 0 and call the remove callback from within
  35008. * the session cache. */
  35009. AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  35010. AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  35011. AssertIntEQ(clientSessRemCountFree, 1);
  35012. #endif
  35013. /* Server session is in the cache so ex_data isn't free'd with the SSL
  35014. * object */
  35015. AssertIntEQ(serverSessRemCountFree, 0);
  35016. /* Force a cache lookup */
  35017. AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  35018. /* Force a cache update */
  35019. AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  35020. /* This should set the timeout to 0 and call the remove callback from within
  35021. * the session cache. */
  35022. AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  35023. AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  35024. AssertIntEQ(serverSessRemCountFree, 1);
  35025. /* Need to free the references that we kept */
  35026. SSL_CTX_free(serverSessCtx);
  35027. SSL_SESSION_free(serverSess);
  35028. #ifndef NO_SESSION_CACHE_REF
  35029. SSL_CTX_free(clientSessCtx);
  35030. SSL_SESSION_free(clientSess);
  35031. #endif
  35032. printf(resultFmt, passed);
  35033. #endif
  35034. return 0;
  35035. }
  35036. static int test_wolfSSL_ticket_keys(void)
  35037. {
  35038. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  35039. !defined(NO_WOLFSSL_SERVER)
  35040. WOLFSSL_CTX* ctx;
  35041. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  35042. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35043. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  35044. WOLFSSL_FAILURE);
  35045. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  35046. WOLFSSL_FAILURE);
  35047. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  35048. WOLFSSL_FAILURE);
  35049. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  35050. WOLFSSL_FAILURE);
  35051. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  35052. WOLFSSL_FAILURE);
  35053. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  35054. WOLFSSL_FAILURE);
  35055. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  35056. WOLFSSL_FAILURE);
  35057. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  35058. WOLFSSL_FAILURE);
  35059. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  35060. WOLFSSL_FAILURE);
  35061. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  35062. WOLFSSL_FAILURE);
  35063. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  35064. WOLFSSL_FAILURE);
  35065. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  35066. WOLFSSL_FAILURE);
  35067. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  35068. WOLFSSL_FAILURE);
  35069. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  35070. WOLFSSL_FAILURE);
  35071. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  35072. WOLFSSL_SUCCESS);
  35073. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  35074. WOLFSSL_SUCCESS);
  35075. wolfSSL_CTX_free(ctx);
  35076. #endif
  35077. return 0;
  35078. }
  35079. #ifndef NO_BIO
  35080. static int test_wolfSSL_d2i_PUBKEY(void)
  35081. {
  35082. #if defined(OPENSSL_EXTRA)
  35083. BIO* bio;
  35084. EVP_PKEY* pkey;
  35085. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  35086. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35087. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  35088. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  35089. /* RSA PUBKEY test */
  35090. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  35091. sizeof_client_keypub_der_2048), 0);
  35092. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  35093. EVP_PKEY_free(pkey);
  35094. #endif
  35095. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  35096. /* ECC PUBKEY test */
  35097. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  35098. sizeof_ecc_clikeypub_der_256), 0);
  35099. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  35100. EVP_PKEY_free(pkey);
  35101. #endif
  35102. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  35103. /* DSA PUBKEY test */
  35104. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  35105. sizeof_dsa_pub_key_der_2048), 0);
  35106. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  35107. EVP_PKEY_free(pkey);
  35108. #endif
  35109. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  35110. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  35111. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  35112. (HAVE_FIPS_VERSION > 2))
  35113. /* DH PUBKEY test */
  35114. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  35115. sizeof_dh_pub_key_der_2048), 0);
  35116. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  35117. EVP_PKEY_free(pkey);
  35118. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  35119. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  35120. BIO_free(bio);
  35121. (void)pkey;
  35122. printf(resultFmt, passed);
  35123. #endif
  35124. return 0;
  35125. }
  35126. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  35127. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  35128. {
  35129. BIO* bio = NULL;
  35130. EVP_PKEY* pkey = NULL;
  35131. #ifndef NO_RSA
  35132. #endif
  35133. WOLFSSL_CTX* ctx;
  35134. #if defined(WOLFSSL_KEY_GEN)
  35135. unsigned char buff[4096];
  35136. unsigned char* bufPtr = buff;
  35137. #endif
  35138. printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
  35139. /* test creating new EVP_PKEY with bad arg */
  35140. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  35141. /* test loading RSA key using BIO */
  35142. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  35143. {
  35144. XFILE file;
  35145. const char* fname = "./certs/server-key.der";
  35146. size_t sz;
  35147. byte* buf;
  35148. file = XFOPEN(fname, "rb");
  35149. AssertTrue((file != XBADFILE));
  35150. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  35151. sz = XFTELL(file);
  35152. XREWIND(file);
  35153. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  35154. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  35155. XFCLOSE(file);
  35156. /* Test using BIO new mem and loading DER private key */
  35157. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  35158. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  35159. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  35160. BIO_free(bio);
  35161. bio = NULL;
  35162. EVP_PKEY_free(pkey);
  35163. pkey = NULL;
  35164. }
  35165. #endif
  35166. /* test loading ECC key using BIO */
  35167. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  35168. {
  35169. XFILE file;
  35170. const char* fname = "./certs/ecc-key.der";
  35171. size_t sz;
  35172. byte* buf;
  35173. file = XFOPEN(fname, "rb");
  35174. AssertTrue((file != XBADFILE));
  35175. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  35176. sz = XFTELL(file);
  35177. XREWIND(file);
  35178. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  35179. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  35180. XFCLOSE(file);
  35181. /* Test using BIO new mem and loading DER private key */
  35182. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  35183. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  35184. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  35185. BIO_free(bio);
  35186. bio = NULL;
  35187. EVP_PKEY_free(pkey);
  35188. pkey = NULL;
  35189. }
  35190. #endif
  35191. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35192. #ifndef NO_WOLFSSL_SERVER
  35193. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35194. #else
  35195. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35196. #endif
  35197. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  35198. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  35199. {
  35200. RSA* rsa = NULL;
  35201. /* Tests bad parameters */
  35202. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  35203. /* RSA not set yet, expecting to fail*/
  35204. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  35205. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  35206. /* set RSA using bio*/
  35207. AssertIntGT(BIO_write(bio, client_key_der_2048,
  35208. sizeof_client_key_der_2048), 0);
  35209. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  35210. AssertNotNull(rsa);
  35211. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  35212. /*i2d RSAprivate key tests */
  35213. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  35214. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  35215. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  35216. sizeof_client_key_der_2048);
  35217. bufPtr -= sizeof_client_key_der_2048;
  35218. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  35219. sizeof_client_key_der_2048), 0);
  35220. bufPtr = NULL;
  35221. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  35222. sizeof_client_key_der_2048);
  35223. AssertNotNull(bufPtr);
  35224. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  35225. sizeof_client_key_der_2048), 0);
  35226. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  35227. RSA_free(rsa);
  35228. rsa = RSA_new();
  35229. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  35230. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  35231. RSA_free(rsa);
  35232. }
  35233. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  35234. SSL_CTX_free(ctx);
  35235. ctx = NULL;
  35236. BIO_free(bio);
  35237. bio = NULL;
  35238. printf(resultFmt, passed);
  35239. return 0;
  35240. }
  35241. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35242. #endif /* !NO_BIO */
  35243. static int test_wolfSSL_sk_GENERAL_NAME(void)
  35244. {
  35245. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35246. !defined(NO_RSA)
  35247. X509* x509;
  35248. GENERAL_NAME* gn;
  35249. unsigned char buf[4096];
  35250. const unsigned char* bufPt;
  35251. int bytes, i;
  35252. XFILE f;
  35253. STACK_OF(GENERAL_NAME)* sk;
  35254. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  35255. f = XFOPEN(cliCertDerFileExt, "rb");
  35256. AssertTrue((f != XBADFILE));
  35257. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  35258. XFCLOSE(f);
  35259. bufPt = buf;
  35260. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  35261. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  35262. NID_subject_alt_name, NULL, NULL));
  35263. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  35264. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  35265. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  35266. switch (gn->type) {
  35267. case GEN_DNS:
  35268. printf("found type GEN_DNS\n");
  35269. break;
  35270. case GEN_EMAIL:
  35271. printf("found type GEN_EMAIL\n");
  35272. break;
  35273. case GEN_URI:
  35274. printf("found type GEN_URI\n");
  35275. break;
  35276. }
  35277. }
  35278. X509_free(x509);
  35279. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  35280. printf(resultFmt, passed);
  35281. #endif
  35282. return 0;
  35283. }
  35284. static int test_wolfSSL_GENERAL_NAME_print(void)
  35285. {
  35286. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  35287. X509* x509;
  35288. GENERAL_NAME* gn;
  35289. unsigned char buf[4096];
  35290. const unsigned char* bufPt;
  35291. int bytes;
  35292. XFILE f;
  35293. STACK_OF(GENERAL_NAME)* sk;
  35294. BIO* out;
  35295. unsigned char outbuf[128];
  35296. X509_EXTENSION* ext;
  35297. AUTHORITY_INFO_ACCESS* aia;
  35298. ACCESS_DESCRIPTION* ad;
  35299. const unsigned char v4Addr[] = {192,168,53,1};
  35300. const unsigned char v6Addr[] =
  35301. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35302. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  35303. const unsigned char email[] =
  35304. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  35305. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  35306. const char* dnsStr = "DNS:example.com";
  35307. const char* uriStr = "URI:http://127.0.0.1:22220";
  35308. const char* v4addStr = "IP Address:192.168.53.1";
  35309. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  35310. const char* emailStr = "email:info@wolfssl.com";
  35311. const char* othrStr = "othername:<unsupported>";
  35312. const char* x400Str = "X400Name:<unsupported>";
  35313. const char* ediStr = "EdiPartyName:<unsupported>";
  35314. printf(testingFmt, "test_wolfSSL_GENERAL_NAME_print()");
  35315. /* BIO to output */
  35316. AssertNotNull(out = BIO_new(BIO_s_mem()));
  35317. /* test for NULL param */
  35318. gn = NULL;
  35319. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  35320. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  35321. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  35322. /* test for GEN_DNS */
  35323. f = XFOPEN(cliCertDerFileExt, "rb");
  35324. AssertTrue((f != XBADFILE));
  35325. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  35326. XFCLOSE(f);
  35327. bufPt = buf;
  35328. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  35329. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  35330. NID_subject_alt_name, NULL, NULL));
  35331. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  35332. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35333. XMEMSET(outbuf,0,sizeof(outbuf));
  35334. BIO_read(out, outbuf, sizeof(outbuf));
  35335. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  35336. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  35337. X509_free(x509);
  35338. /* test for GEN_URI */
  35339. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  35340. AssertTrue((f != XBADFILE));
  35341. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  35342. XFCLOSE(f);
  35343. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  35344. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  35345. AssertNotNull(aia);
  35346. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  35347. gn = ad->location;
  35348. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35349. XMEMSET(outbuf,0,sizeof(outbuf));
  35350. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35351. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  35352. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  35353. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  35354. AssertNotNull(aia);
  35355. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  35356. X509_free(x509);
  35357. /* test for GEN_IPADD */
  35358. /* ip v4 address */
  35359. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35360. gn->type = GEN_IPADD;
  35361. gn->d.iPAddress->length = sizeof(v4Addr);
  35362. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  35363. sizeof(v4Addr)), 1);
  35364. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35365. XMEMSET(outbuf,0,sizeof(outbuf));
  35366. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35367. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  35368. GENERAL_NAME_free(gn);
  35369. /* ip v6 address */
  35370. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35371. gn->type = GEN_IPADD;
  35372. gn->d.iPAddress->length = sizeof(v6Addr);
  35373. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  35374. sizeof(v6Addr)), 1);
  35375. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35376. XMEMSET(outbuf,0,sizeof(outbuf));
  35377. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35378. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  35379. GENERAL_NAME_free(gn);
  35380. /* test for GEN_EMAIL */
  35381. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35382. gn->type = GEN_EMAIL;
  35383. gn->d.rfc822Name->length = sizeof(email);
  35384. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  35385. sizeof(email)), 1);
  35386. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35387. XMEMSET(outbuf,0,sizeof(outbuf));
  35388. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35389. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  35390. GENERAL_NAME_free(gn);
  35391. /* test for GEN_OTHERNAME */
  35392. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35393. gn->type = GEN_OTHERNAME;
  35394. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35395. XMEMSET(outbuf,0,sizeof(outbuf));
  35396. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35397. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  35398. GENERAL_NAME_free(gn);
  35399. /* test for GEN_X400 */
  35400. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35401. gn->type = GEN_X400;
  35402. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35403. XMEMSET(outbuf,0,sizeof(outbuf));
  35404. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35405. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  35406. GENERAL_NAME_free(gn);
  35407. /* test for GEN_EDIPARTY */
  35408. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35409. gn->type = GEN_EDIPARTY;
  35410. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35411. XMEMSET(outbuf,0,sizeof(outbuf));
  35412. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35413. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  35414. GENERAL_NAME_free(gn);
  35415. BIO_free(out);
  35416. printf(resultFmt, passed);
  35417. #endif /* OPENSSL_ALL */
  35418. return 0;
  35419. }
  35420. static int test_wolfSSL_sk_DIST_POINT(void)
  35421. {
  35422. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35423. !defined(NO_RSA)
  35424. X509* x509;
  35425. unsigned char buf[4096];
  35426. const unsigned char* bufPt;
  35427. int bytes, i, j;
  35428. XFILE f;
  35429. DIST_POINT* dp;
  35430. GENERAL_NAME* gn;
  35431. ASN1_IA5STRING* uri;
  35432. STACK_OF(DIST_POINT)* dps;
  35433. STACK_OF(GENERAL_NAME)* gns;
  35434. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  35435. printf(testingFmt, "wolfSSL_sk_DIST_POINT()");
  35436. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  35437. AssertTrue((f != XBADFILE));
  35438. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  35439. XFCLOSE(f);
  35440. bufPt = buf;
  35441. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  35442. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  35443. NID_crl_distribution_points, NULL, NULL));
  35444. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  35445. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  35446. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  35447. gns = dp->distpoint->name.fullname;
  35448. AssertNotNull(gns);
  35449. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  35450. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  35451. gn = sk_GENERAL_NAME_value(gns, j);
  35452. AssertIntEQ(gn->type, GEN_URI);
  35453. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  35454. AssertNotNull(uri->data);
  35455. AssertIntGT(uri->length, 0);
  35456. }
  35457. }
  35458. X509_free(x509);
  35459. CRL_DIST_POINTS_free(dps);
  35460. printf(resultFmt, passed);
  35461. #endif
  35462. return 0;
  35463. }
  35464. static int test_wolfSSL_MD4(void)
  35465. {
  35466. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  35467. MD4_CTX md4;
  35468. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  35469. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  35470. "789012345678901234567890";
  35471. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  35472. "\xcc\x05\x36";
  35473. int msgSz = (int)XSTRLEN(msg);
  35474. printf(testingFmt, "wolfSSL_MD4()");
  35475. XMEMSET(out, 0, sizeof(out));
  35476. MD4_Init(&md4);
  35477. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  35478. MD4_Final(out, &md4);
  35479. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  35480. printf(resultFmt, passed);
  35481. #endif
  35482. return 0;
  35483. }
  35484. static int test_wolfSSL_verify_mode(void)
  35485. {
  35486. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  35487. WOLFSSL* ssl;
  35488. WOLFSSL_CTX* ctx;
  35489. printf(testingFmt, "test_wolfSSL_verify()");
  35490. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35491. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35492. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35493. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  35494. AssertNotNull(ssl = SSL_new(ctx));
  35495. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  35496. SSL_free(ssl);
  35497. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  35498. AssertNotNull(ssl = SSL_new(ctx));
  35499. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  35500. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  35501. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  35502. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  35503. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  35504. SSL_free(ssl);
  35505. wolfSSL_CTX_set_verify(ctx,
  35506. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  35507. AssertNotNull(ssl = SSL_new(ctx));
  35508. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  35509. AssertIntEQ(SSL_get_verify_mode(ssl),
  35510. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  35511. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  35512. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  35513. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  35514. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  35515. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  35516. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  35517. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  35518. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  35519. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  35520. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  35521. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  35522. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  35523. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  35524. #endif
  35525. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  35526. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  35527. SSL_free(ssl);
  35528. SSL_CTX_free(ctx);
  35529. printf(resultFmt, passed);
  35530. #endif
  35531. return 0;
  35532. }
  35533. static int test_wolfSSL_verify_depth(void)
  35534. {
  35535. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  35536. WOLFSSL* ssl;
  35537. WOLFSSL_CTX* ctx;
  35538. long depth;
  35539. printf(testingFmt, "test_wolfSSL_verify_depth()");
  35540. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35541. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35542. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35543. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  35544. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  35545. AssertNotNull(ssl = SSL_new(ctx));
  35546. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  35547. SSL_free(ssl);
  35548. SSL_CTX_set_verify_depth(ctx, -1);
  35549. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  35550. SSL_CTX_set_verify_depth(ctx, 2);
  35551. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  35552. AssertNotNull(ssl = SSL_new(ctx));
  35553. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  35554. SSL_free(ssl);
  35555. SSL_CTX_free(ctx);
  35556. printf(resultFmt, passed);
  35557. #endif
  35558. return 0;
  35559. }
  35560. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  35561. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  35562. * buffer of 64 bytes.
  35563. *
  35564. * returns the size of the digest buffer on success and a negative value on
  35565. * failure.
  35566. */
  35567. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  35568. {
  35569. HMAC_CTX ctx1;
  35570. HMAC_CTX ctx2;
  35571. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  35572. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  35573. unsigned char long_key[] =
  35574. "0123456789012345678901234567890123456789"
  35575. "0123456789012345678901234567890123456789"
  35576. "0123456789012345678901234567890123456789"
  35577. "0123456789012345678901234567890123456789";
  35578. unsigned char msg[] = "message to hash";
  35579. unsigned int digestSz = 64;
  35580. int keySz = sizeof(key);
  35581. int long_keySz = sizeof(long_key);
  35582. int msgSz = sizeof(msg);
  35583. unsigned char digest2[64];
  35584. unsigned int digestSz2 = 64;
  35585. HMAC_CTX_init(&ctx1);
  35586. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  35587. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35588. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  35589. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35590. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  35591. HMAC_CTX_cleanup(&ctx1);
  35592. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35593. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  35594. HMAC_CTX_cleanup(&ctx2);
  35595. AssertIntEQ(digestSz, digestSz2);
  35596. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  35597. /* test HMAC_Init with NULL key */
  35598. /* init after copy */
  35599. printf("test HMAC_Init with NULL key (0)\n");
  35600. HMAC_CTX_init(&ctx1);
  35601. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  35602. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35603. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  35604. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  35605. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35606. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35607. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  35608. HMAC_CTX_cleanup(&ctx1);
  35609. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  35610. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35611. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35612. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  35613. HMAC_CTX_cleanup(&ctx2);
  35614. AssertIntEQ(digestSz, digestSz2);
  35615. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  35616. /* long key */
  35617. printf("test HMAC_Init with NULL key (1)\n");
  35618. HMAC_CTX_init(&ctx1);
  35619. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  35620. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35621. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  35622. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  35623. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35624. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35625. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  35626. HMAC_CTX_cleanup(&ctx1);
  35627. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  35628. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35629. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35630. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  35631. HMAC_CTX_cleanup(&ctx2);
  35632. AssertIntEQ(digestSz, digestSz2);
  35633. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  35634. /* init before copy */
  35635. printf("test HMAC_Init with NULL key (2)\n");
  35636. HMAC_CTX_init(&ctx1);
  35637. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  35638. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35639. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  35640. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  35641. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35642. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35643. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  35644. HMAC_CTX_cleanup(&ctx1);
  35645. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35646. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35647. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  35648. HMAC_CTX_cleanup(&ctx2);
  35649. AssertIntEQ(digestSz, digestSz2);
  35650. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  35651. return digestSz;
  35652. }
  35653. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  35654. static int test_wolfSSL_HMAC_CTX(void)
  35655. {
  35656. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  35657. unsigned char digest[64];
  35658. int digestSz;
  35659. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  35660. #ifndef NO_SHA
  35661. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  35662. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  35663. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  35664. #endif /* !NO_SHA */
  35665. #ifdef WOLFSSL_SHA224
  35666. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  35667. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  35668. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  35669. "\x02\x0E", digest, digestSz), 0);
  35670. #endif /* WOLFSSL_SHA224 */
  35671. #ifndef NO_SHA256
  35672. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  35673. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  35674. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  35675. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  35676. #endif /* !NO_SHA256 */
  35677. #ifdef WOLFSSL_SHA384
  35678. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  35679. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  35680. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  35681. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  35682. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  35683. digest, digestSz), 0);
  35684. #endif /* WOLFSSL_SHA384 */
  35685. #ifdef WOLFSSL_SHA512
  35686. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  35687. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  35688. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  35689. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  35690. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  35691. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  35692. digest, digestSz), 0);
  35693. #endif /* WOLFSSL_SHA512 */
  35694. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  35695. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  35696. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  35697. "\xE4\x98\xDD", digest, digestSz), 0);
  35698. #endif /* !NO_MD5 */
  35699. printf(resultFmt, passed);
  35700. #endif
  35701. return 0;
  35702. }
  35703. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  35704. static void sslMsgCb(int w, int version, int type, const void* buf,
  35705. size_t sz, SSL* ssl, void* arg)
  35706. {
  35707. int i;
  35708. unsigned char* pt = (unsigned char*)buf;
  35709. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  35710. (int)sz, version, type);
  35711. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  35712. printf("\n");
  35713. (void)ssl;
  35714. (void)arg;
  35715. }
  35716. #endif /* OPENSSL_EXTRA */
  35717. static int test_wolfSSL_msg_callback(void)
  35718. {
  35719. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  35720. WOLFSSL* ssl;
  35721. WOLFSSL_CTX* ctx;
  35722. printf(testingFmt, "wolfSSL_msg_callback()");
  35723. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35724. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  35725. SSL_FILETYPE_PEM));
  35726. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  35727. SSL_FILETYPE_PEM));
  35728. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  35729. SSL_SUCCESS);
  35730. AssertNotNull(ssl = SSL_new(ctx));
  35731. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  35732. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  35733. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  35734. SSL_free(ssl);
  35735. SSL_CTX_free(ctx);
  35736. printf(resultFmt, passed);
  35737. #endif
  35738. return 0;
  35739. }
  35740. static int test_wolfSSL_SHA(void)
  35741. {
  35742. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  35743. printf(testingFmt, "wolfSSL_SHA()");
  35744. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  35745. (!defined(HAVE_FIPS) || \
  35746. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  35747. {
  35748. const unsigned char in[] = "abc";
  35749. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  35750. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  35751. unsigned char out[WC_SHA_DIGEST_SIZE];
  35752. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35753. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  35754. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35755. /* SHA interface test */
  35756. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35757. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  35758. AssertNotNull(SHA(in, 0, out));
  35759. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  35760. AssertNotNull(SHA(NULL, 0, out));
  35761. AssertNotNull(SHA(NULL, 0, NULL));
  35762. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  35763. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35764. }
  35765. #endif
  35766. #if !defined(NO_SHA256)
  35767. {
  35768. const unsigned char in[] = "abc";
  35769. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  35770. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  35771. "\x15\xAD";
  35772. unsigned char out[WC_SHA256_DIGEST_SIZE];
  35773. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  35774. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35775. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  35776. #else
  35777. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  35778. #endif
  35779. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  35780. }
  35781. #endif
  35782. #if defined(WOLFSSL_SHA384)
  35783. {
  35784. const unsigned char in[] = "abc";
  35785. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  35786. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  35787. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  35788. "\xc8\x25\xa7";
  35789. unsigned char out[WC_SHA384_DIGEST_SIZE];
  35790. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  35791. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35792. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  35793. #else
  35794. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  35795. #endif
  35796. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  35797. }
  35798. #endif
  35799. #if defined(WOLFSSL_SHA512)
  35800. {
  35801. const unsigned char in[] = "abc";
  35802. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  35803. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  35804. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  35805. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  35806. "\xa5\x4c\xa4\x9f";
  35807. unsigned char out[WC_SHA512_DIGEST_SIZE];
  35808. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  35809. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35810. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  35811. #else
  35812. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  35813. #endif
  35814. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  35815. }
  35816. #endif
  35817. printf(resultFmt, passed);
  35818. #endif
  35819. return 0;
  35820. }
  35821. static int test_wolfSSL_DH_1536_prime(void)
  35822. {
  35823. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  35824. BIGNUM* bn;
  35825. unsigned char bits[200];
  35826. int sz = 192; /* known binary size */
  35827. const byte expected[] = {
  35828. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  35829. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  35830. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  35831. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  35832. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  35833. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  35834. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  35835. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  35836. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  35837. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  35838. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  35839. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  35840. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  35841. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  35842. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  35843. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  35844. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  35845. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  35846. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  35847. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  35848. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  35849. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  35850. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  35851. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  35852. };
  35853. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  35854. bn = get_rfc3526_prime_1536(NULL);
  35855. AssertNotNull(bn);
  35856. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  35857. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  35858. BN_free(bn);
  35859. printf(resultFmt, passed);
  35860. #endif
  35861. return 0;
  35862. }
  35863. static int test_wolfSSL_DH_get_2048_256(void)
  35864. {
  35865. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  35866. WOLFSSL_DH* dh;
  35867. const WOLFSSL_BIGNUM* pBn;
  35868. const WOLFSSL_BIGNUM* gBn;
  35869. const WOLFSSL_BIGNUM* qBn;
  35870. const byte pExpected[] = {
  35871. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  35872. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  35873. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  35874. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  35875. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  35876. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  35877. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  35878. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  35879. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  35880. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  35881. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  35882. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  35883. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  35884. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  35885. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  35886. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  35887. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  35888. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  35889. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  35890. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  35891. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  35892. 0x1E, 0x1A, 0x15, 0x97
  35893. };
  35894. const byte gExpected[] = {
  35895. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  35896. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  35897. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  35898. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  35899. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  35900. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  35901. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  35902. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  35903. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  35904. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  35905. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  35906. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  35907. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  35908. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  35909. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  35910. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  35911. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  35912. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  35913. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  35914. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  35915. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  35916. 0x6C, 0xC4, 0x16, 0x59
  35917. };
  35918. const byte qExpected[] = {
  35919. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  35920. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  35921. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  35922. };
  35923. int pSz;
  35924. int qSz;
  35925. int gSz;
  35926. byte* pReturned;
  35927. byte* qReturned;
  35928. byte* gReturned;
  35929. printf(testingFmt, "wolfSSL_DH_get_2048_256()");
  35930. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  35931. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  35932. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  35933. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  35934. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  35935. AssertIntEQ(pSz, sizeof(pExpected));
  35936. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  35937. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  35938. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  35939. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  35940. AssertIntEQ(qSz, sizeof(qExpected));
  35941. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  35942. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  35943. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  35944. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  35945. AssertIntEQ(gSz, sizeof(gExpected));
  35946. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  35947. wolfSSL_DH_free(dh);
  35948. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35949. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35950. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35951. printf(resultFmt, passed);
  35952. #endif
  35953. return 0;
  35954. }
  35955. static int test_wolfSSL_PEM_write_DHparams(void)
  35956. {
  35957. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  35958. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  35959. DH* dh;
  35960. BIO* bio;
  35961. XFILE fp;
  35962. byte pem[2048];
  35963. int pemSz;
  35964. const char expected[] =
  35965. "-----BEGIN DH PARAMETERS-----\n\
  35966. MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n\
  35967. v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n\
  35968. nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n\
  35969. joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n\
  35970. wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n\
  35971. tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n\
  35972. -----END DH PARAMETERS-----\n";
  35973. printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
  35974. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  35975. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  35976. XFCLOSE(fp);
  35977. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35978. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  35979. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  35980. BIO_free(bio);
  35981. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  35982. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  35983. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  35984. XFCLOSE(fp);
  35985. DH_free(dh);
  35986. /* check results */
  35987. XMEMSET(pem, 0, sizeof(pem));
  35988. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  35989. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  35990. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  35991. XFCLOSE(fp);
  35992. printf(resultFmt, passed);
  35993. #endif
  35994. return 0;
  35995. }
  35996. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  35997. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  35998. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  35999. static void binary_dump(void *ptr, int size)
  36000. {
  36001. #ifdef WOLFSSL_EVP_PRINT
  36002. int i = 0;
  36003. unsigned char *p = (unsigned char *) ptr;
  36004. printf("{");
  36005. while((p != NULL) && (i < size)) {
  36006. if((i % 8) == 0) {
  36007. printf("\n");
  36008. printf(" ");
  36009. }
  36010. printf("0x%02x, ", p[i]);
  36011. i++;
  36012. }
  36013. printf("\n};\n");
  36014. #else
  36015. (void) ptr;
  36016. (void) size;
  36017. #endif
  36018. }
  36019. static int last_val = 0x0f;
  36020. static int check_result(unsigned char *data, int len)
  36021. {
  36022. int i;
  36023. for( ; len; ) {
  36024. last_val = (last_val + 1) % 16;
  36025. for(i = 0; i < 16; len--, i++, data++)
  36026. if(*data != last_val) {
  36027. return -1;
  36028. }
  36029. }
  36030. return 0;
  36031. }
  36032. static int r_offset;
  36033. static int w_offset;
  36034. static void init_offset(void)
  36035. {
  36036. r_offset = 0;
  36037. w_offset = 0;
  36038. }
  36039. static void get_record(unsigned char *data, unsigned char *buf, int len)
  36040. {
  36041. XMEMCPY(buf, data+r_offset, len);
  36042. r_offset += len;
  36043. }
  36044. static void set_record(unsigned char *data, unsigned char *buf, int len)
  36045. {
  36046. XMEMCPY(data+w_offset, buf, len);
  36047. w_offset += len;
  36048. }
  36049. static void set_plain(unsigned char *plain, int rec)
  36050. {
  36051. int i, j;
  36052. unsigned char *p = plain;
  36053. #define BLOCKSZ 16
  36054. for(i=0; i<(rec/BLOCKSZ); i++){
  36055. for(j=0; j<BLOCKSZ; j++)
  36056. *p++ = (i % 16);
  36057. }
  36058. }
  36059. #endif
  36060. static int test_wolfSSL_EVP_Cipher_extra(void)
  36061. {
  36062. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  36063. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  36064. /* aes128-cbc, keylen=16, ivlen=16 */
  36065. byte aes128_cbc_key[] = {
  36066. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36067. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36068. };
  36069. byte aes128_cbc_iv[] = {
  36070. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  36071. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  36072. };
  36073. /* teset data size table */
  36074. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  36075. int test_drive2[] = {8, 3, 8, 512, 0};
  36076. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  36077. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  36078. int test_drive_len[100];
  36079. int ret = 0;
  36080. EVP_CIPHER_CTX *evp = NULL;
  36081. int ilen = 0;
  36082. int klen = 0;
  36083. int i, j;
  36084. const EVP_CIPHER *type;
  36085. byte *iv;
  36086. byte *key;
  36087. int ivlen;
  36088. int keylen;
  36089. #define RECORDS 16
  36090. #define BUFFSZ 512
  36091. byte plain [BUFFSZ * RECORDS];
  36092. byte cipher[BUFFSZ * RECORDS];
  36093. byte inb[BUFFSZ];
  36094. byte outb[BUFFSZ+16];
  36095. int outl, inl;
  36096. iv = aes128_cbc_iv;
  36097. ivlen = sizeof(aes128_cbc_iv);
  36098. key = aes128_cbc_key;
  36099. keylen = sizeof(aes128_cbc_key);
  36100. type = EVP_aes_128_cbc();
  36101. set_plain(plain, BUFFSZ * RECORDS);
  36102. SSL_library_init();
  36103. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  36104. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  36105. klen = EVP_CIPHER_CTX_key_length(evp);
  36106. if (klen > 0 && keylen != klen) {
  36107. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  36108. }
  36109. ilen = EVP_CIPHER_CTX_iv_length(evp);
  36110. if (ilen > 0 && ivlen != ilen) {
  36111. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  36112. }
  36113. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  36114. for (j = 0; j<RECORDS; j++)
  36115. {
  36116. inl = BUFFSZ;
  36117. get_record(plain, inb, inl);
  36118. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  36119. set_record(cipher, outb, outl);
  36120. }
  36121. for (i = 0; test_drive[i]; i++) {
  36122. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  36123. init_offset();
  36124. test_drive_len[i] = 0;
  36125. for (j = 0; test_drive[i][j]; j++)
  36126. {
  36127. inl = test_drive[i][j];
  36128. test_drive_len[i] += inl;
  36129. get_record(plain, inb, inl);
  36130. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  36131. /* output to cipher buffer, so that following Dec test can detect
  36132. if any error */
  36133. set_record(cipher, outb, outl);
  36134. }
  36135. EVP_CipherFinal(evp, outb, &outl);
  36136. if(outl > 0)
  36137. set_record(cipher, outb, outl);
  36138. }
  36139. for (i = 0; test_drive[i]; i++) {
  36140. last_val = 0x0f;
  36141. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  36142. init_offset();
  36143. for (j = 0; test_drive[i][j]; j++){
  36144. inl = test_drive[i][j];
  36145. get_record(cipher, inb, inl);
  36146. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  36147. binary_dump(outb, outl);
  36148. AssertIntEQ((ret = check_result(outb, outl)), 0);
  36149. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  36150. }
  36151. ret = EVP_CipherFinal(evp, outb, &outl);
  36152. binary_dump(outb, outl);
  36153. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  36154. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  36155. AssertTrue(ret);
  36156. }
  36157. EVP_CIPHER_CTX_free(evp);
  36158. #endif /* test_EVP_Cipher */
  36159. return 0;
  36160. }
  36161. static int test_wolfSSL_PEM_read_DHparams(void)
  36162. {
  36163. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  36164. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  36165. DH* dh;
  36166. XFILE fp;
  36167. unsigned char derOut[300];
  36168. unsigned char* derOutBuf = derOut;
  36169. int derOutSz = 0;
  36170. unsigned char derExpected[300];
  36171. int derExpectedSz = 0;
  36172. printf(testingFmt, "wolfSSL_PEM_read_DHparams()");
  36173. XMEMSET(derOut, 0, sizeof(derOut));
  36174. XMEMSET(derExpected, 0, sizeof(derExpected));
  36175. /* open DH param file, read into DH struct */
  36176. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  36177. /* bad args */
  36178. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  36179. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  36180. /* good args */
  36181. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  36182. XFCLOSE(fp);
  36183. /* read in certs/dh2048.der for comparison against exported params */
  36184. fp = XFOPEN("./certs/dh2048.der", "rb");
  36185. AssertTrue(fp != XBADFILE);
  36186. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  36187. XFCLOSE(fp);
  36188. /* export DH back to DER and compare */
  36189. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  36190. AssertIntEQ(derOutSz, derExpectedSz);
  36191. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  36192. /* Test parsing with X9.42 header */
  36193. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  36194. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  36195. XFCLOSE(fp);
  36196. DH_free(dh);
  36197. printf(resultFmt, passed);
  36198. #endif
  36199. return 0;
  36200. }
  36201. static int test_wolfSSL_AES_ecb_encrypt(void)
  36202. {
  36203. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  36204. AES_KEY aes;
  36205. const byte msg[] =
  36206. {
  36207. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  36208. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  36209. };
  36210. const byte verify[] =
  36211. {
  36212. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  36213. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  36214. };
  36215. const byte key[] =
  36216. {
  36217. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  36218. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  36219. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  36220. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  36221. };
  36222. byte out[AES_BLOCK_SIZE];
  36223. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  36224. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  36225. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36226. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  36227. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  36228. #ifdef HAVE_AES_DECRYPT
  36229. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  36230. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36231. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  36232. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  36233. #endif
  36234. /* test bad arguments */
  36235. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  36236. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  36237. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  36238. printf(resultFmt, passed);
  36239. #endif
  36240. return 0;
  36241. }
  36242. static int test_wolfSSL_MD5(void)
  36243. {
  36244. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36245. byte input1[] = "";
  36246. byte input2[] = "message digest";
  36247. byte hash[WC_MD5_DIGEST_SIZE];
  36248. unsigned char output1[] =
  36249. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  36250. unsigned char output2[] =
  36251. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  36252. WOLFSSL_MD5_CTX md5;
  36253. printf(testingFmt, "wolfSSL_MD5()");
  36254. XMEMSET(&md5, 0, sizeof(md5));
  36255. /* Test cases for illegal parameters */
  36256. AssertIntEQ(MD5_Init(NULL), 0);
  36257. AssertIntEQ(MD5_Init(&md5), 1);
  36258. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  36259. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  36260. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  36261. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  36262. AssertIntEQ(MD5_Final(hash, NULL), 0);
  36263. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  36264. /* Init MD5 CTX */
  36265. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36266. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  36267. XSTRLEN((const char*)&input1)), 1);
  36268. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36269. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36270. /* Init MD5 CTX */
  36271. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36272. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  36273. (int)XSTRLEN((const char*)input2)), 1);
  36274. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36275. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36276. #if !defined(NO_OLD_NAMES) && \
  36277. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  36278. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  36279. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  36280. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  36281. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  36282. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  36283. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36284. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  36285. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36286. {
  36287. byte data[] = "Data to be hashed.";
  36288. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  36289. AssertNotNull(MD5(data, sizeof(data), NULL));
  36290. AssertNotNull(MD5(data, sizeof(data), hash));
  36291. AssertNotNull(MD5(NULL, 0, hash));
  36292. AssertNull(MD5(NULL, sizeof(data), hash));
  36293. }
  36294. #endif
  36295. printf(resultFmt, passed);
  36296. #endif
  36297. return 0;
  36298. }
  36299. static int test_wolfSSL_MD5_Transform(void)
  36300. {
  36301. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36302. byte input1[] = "";
  36303. byte input2[] = "abc";
  36304. byte local[WC_MD5_BLOCK_SIZE];
  36305. word32 sLen = 0;
  36306. #ifdef BIG_ENDIAN_ORDER
  36307. unsigned char output1[] =
  36308. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  36309. unsigned char output2[] =
  36310. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  36311. #else
  36312. unsigned char output1[] =
  36313. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  36314. unsigned char output2[] =
  36315. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  36316. #endif
  36317. union {
  36318. wc_Md5 native;
  36319. MD5_CTX compat;
  36320. } md5;
  36321. printf(testingFmt, "wolfSSL_MD5_Transform()");
  36322. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  36323. XMEMSET(&local, 0, sizeof(local));
  36324. /* sanity check */
  36325. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  36326. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  36327. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  36328. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  36329. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36330. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  36331. /* Init MD5 CTX */
  36332. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  36333. /* Do Transform*/
  36334. sLen = (word32)XSTRLEN((char*)input1);
  36335. XMEMCPY(local, input1, sLen);
  36336. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36337. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  36338. WC_MD5_DIGEST_SIZE), 0);
  36339. /* Init MD5 CTX */
  36340. AssertIntEQ(MD5_Init(&md5.compat), 1);
  36341. sLen = (word32)XSTRLEN((char*)input2);
  36342. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  36343. XMEMCPY(local, input2, sLen);
  36344. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36345. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  36346. WC_MD5_DIGEST_SIZE), 0);
  36347. printf(resultFmt, passed);
  36348. #endif
  36349. return 0;
  36350. }
  36351. static int test_wolfSSL_SHA224(void)
  36352. {
  36353. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  36354. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36355. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36356. unsigned char input[] =
  36357. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36358. unsigned char output[] =
  36359. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  36360. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  36361. size_t inLen;
  36362. byte hash[WC_SHA224_DIGEST_SIZE];
  36363. printf(testingFmt, "wolfSSL_SHA224()");
  36364. inLen = XSTRLEN((char*)input);
  36365. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  36366. AssertNull(SHA224(NULL, inLen, hash));
  36367. AssertNotNull(SHA224(input, 0, hash));
  36368. AssertNotNull(SHA224(input, inLen, NULL));
  36369. AssertNotNull(SHA224(NULL, 0, hash));
  36370. AssertNotNull(SHA224(NULL, 0, NULL));
  36371. AssertNotNull(SHA224(input, inLen, hash));
  36372. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  36373. printf(resultFmt, passed);
  36374. #endif
  36375. return 0;
  36376. }
  36377. static int test_wolfSSL_SHA_Transform(void)
  36378. {
  36379. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  36380. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36381. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  36382. byte input1[] = "";
  36383. byte input2[] = "abc";
  36384. byte local[WC_SHA_BLOCK_SIZE];
  36385. word32 sLen = 0;
  36386. #ifdef BIG_ENDIAN_ORDER
  36387. unsigned char output1[] =
  36388. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  36389. "\x09\xf7\x3a\x93";
  36390. unsigned char output2[] =
  36391. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  36392. "\x7c\x6e\x08\xb8";
  36393. #else
  36394. unsigned char output1[] =
  36395. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  36396. "\x93\x3a\xf7\x09";
  36397. unsigned char output2[] =
  36398. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  36399. "\xb8\x08\x6e\x7c";
  36400. #endif
  36401. union {
  36402. wc_Sha native;
  36403. SHA_CTX compat;
  36404. } sha;
  36405. union {
  36406. wc_Sha native;
  36407. SHA_CTX compat;
  36408. } sha1;
  36409. printf(testingFmt, "wolfSSL_SHA_Transform()");
  36410. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  36411. XMEMSET(&local, 0, sizeof(local));
  36412. /* sanity check */
  36413. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  36414. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  36415. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  36416. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  36417. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  36418. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  36419. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  36420. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36421. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  36422. /* Init SHA CTX */
  36423. AssertIntEQ(SHA_Init(&sha.compat), 1);
  36424. /* Do Transform*/
  36425. sLen = (word32)XSTRLEN((char*)input1);
  36426. XMEMCPY(local, input1, sLen);
  36427. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36428. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  36429. WC_SHA_DIGEST_SIZE), 0);
  36430. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36431. /* Init SHA CTX */
  36432. AssertIntEQ(SHA_Init(&sha.compat), 1);
  36433. sLen = (word32)XSTRLEN((char*)input2);
  36434. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36435. XMEMCPY(local, input2, sLen);
  36436. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36437. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  36438. WC_SHA_DIGEST_SIZE), 0);
  36439. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36440. /* SHA1 */
  36441. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36442. /* Init SHA CTX */
  36443. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  36444. /* Do Transform*/
  36445. sLen = (word32)XSTRLEN((char*)input1);
  36446. XMEMCPY(local, input1, sLen);
  36447. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36448. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  36449. WC_SHA_DIGEST_SIZE), 0);
  36450. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  36451. /* Init SHA CTX */
  36452. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  36453. sLen = (word32)XSTRLEN((char*)input2);
  36454. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36455. XMEMCPY(local, input2, sLen);
  36456. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36457. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  36458. WC_SHA_DIGEST_SIZE), 0);
  36459. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  36460. printf(resultFmt, passed);
  36461. #endif
  36462. #endif
  36463. return 0;
  36464. }
  36465. static int test_wolfSSL_SHA256_Transform(void)
  36466. {
  36467. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  36468. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36469. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36470. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH)
  36471. byte input1[] = "";
  36472. byte input2[] = "abc";
  36473. byte local[WC_SHA256_BLOCK_SIZE];
  36474. word32 sLen = 0;
  36475. #ifdef BIG_ENDIAN_ORDER
  36476. unsigned char output1[] =
  36477. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  36478. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  36479. unsigned char output2[] =
  36480. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  36481. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  36482. #else
  36483. unsigned char output1[] =
  36484. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  36485. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  36486. unsigned char output2[] =
  36487. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  36488. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  36489. #endif
  36490. union {
  36491. wc_Sha256 native;
  36492. SHA256_CTX compat;
  36493. } sha256;
  36494. printf(testingFmt, "wolfSSL_SHA256_Transform()");
  36495. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  36496. XMEMSET(&local, 0, sizeof(local));
  36497. /* sanity check */
  36498. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  36499. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  36500. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  36501. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  36502. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36503. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  36504. /* Init SHA256 CTX */
  36505. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  36506. /* Do Transform*/
  36507. sLen = (word32)XSTRLEN((char*)input1);
  36508. XMEMCPY(local, input1, sLen);
  36509. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36510. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  36511. WC_SHA256_DIGEST_SIZE), 0);
  36512. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36513. /* Init SHA256 CTX */
  36514. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  36515. sLen = (word32)XSTRLEN((char*)input2);
  36516. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  36517. XMEMCPY(local, input2, sLen);
  36518. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36519. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  36520. WC_SHA256_DIGEST_SIZE), 0);
  36521. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36522. printf(resultFmt, passed);
  36523. #endif
  36524. #endif
  36525. return 0;
  36526. }
  36527. static int test_wolfSSL_SHA256(void)
  36528. {
  36529. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  36530. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  36531. unsigned char input[] =
  36532. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36533. unsigned char output[] =
  36534. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  36535. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  36536. "\x06\xC1";
  36537. size_t inLen;
  36538. byte hash[WC_SHA256_DIGEST_SIZE];
  36539. printf(testingFmt, "wolfSSL_SHA256()");
  36540. inLen = XSTRLEN((char*)input);
  36541. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  36542. AssertNotNull(SHA256(input, inLen, hash));
  36543. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  36544. printf(resultFmt, passed);
  36545. #endif
  36546. return 0;
  36547. }
  36548. static int test_wolfSSL_SHA512_Transform(void)
  36549. {
  36550. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  36551. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36552. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  36553. byte input1[] = "";
  36554. byte input2[] = "abc";
  36555. byte local[WC_SHA512_BLOCK_SIZE];
  36556. word32 sLen = 0;
  36557. #ifdef BIG_ENDIAN_ORDER
  36558. unsigned char output1[] =
  36559. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  36560. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  36561. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  36562. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  36563. unsigned char output2[] =
  36564. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  36565. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  36566. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  36567. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  36568. #else
  36569. unsigned char output1[] =
  36570. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  36571. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  36572. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  36573. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  36574. unsigned char output2[] =
  36575. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  36576. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  36577. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  36578. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  36579. #endif
  36580. union {
  36581. wc_Sha512 native;
  36582. SHA512_CTX compat;
  36583. } sha512;
  36584. printf(testingFmt, "wolfSSL_SHA512_Transform()");
  36585. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36586. XMEMSET(&local, 0, sizeof(local));
  36587. /* sanity check */
  36588. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  36589. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  36590. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  36591. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  36592. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36593. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36594. /* Init SHA512 CTX */
  36595. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  36596. /* Do Transform*/
  36597. sLen = (word32)XSTRLEN((char*)input1);
  36598. XMEMCPY(local, input1, sLen);
  36599. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36600. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  36601. WC_SHA512_DIGEST_SIZE), 0);
  36602. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36603. /* Init SHA512 CTX */
  36604. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  36605. sLen = (word32)XSTRLEN((char*)input2);
  36606. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36607. XMEMCPY(local, input2, sLen);
  36608. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36609. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  36610. WC_SHA512_DIGEST_SIZE), 0);
  36611. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36612. (void)input1;
  36613. printf(resultFmt, passed);
  36614. #endif
  36615. #endif
  36616. return 0;
  36617. }
  36618. static int test_wolfSSL_X509_get_serialNumber(void)
  36619. {
  36620. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36621. ASN1_INTEGER* a;
  36622. BIGNUM* bn;
  36623. X509* x509;
  36624. char *serialHex;
  36625. byte serial[3];
  36626. int serialSz;
  36627. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  36628. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36629. SSL_FILETYPE_PEM));
  36630. AssertNotNull(a = X509_get_serialNumber(x509));
  36631. /* check on value of ASN1 Integer */
  36632. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  36633. /* test setting serial number and then retrieving it */
  36634. AssertNotNull(a = ASN1_INTEGER_new());
  36635. ASN1_INTEGER_set(a, 3);
  36636. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  36637. serialSz = sizeof(serial);
  36638. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  36639. WOLFSSL_SUCCESS);
  36640. AssertIntEQ(serialSz, 1);
  36641. AssertIntEQ(serial[0], 3);
  36642. ASN1_INTEGER_free(a);
  36643. /* test setting serial number with 0's in it */
  36644. serial[0] = 0x01;
  36645. serial[1] = 0x00;
  36646. serial[2] = 0x02;
  36647. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  36648. a->data[0] = ASN_INTEGER;
  36649. a->data[1] = sizeof(serial);
  36650. XMEMCPY(&a->data[2], serial, sizeof(serial));
  36651. a->length = sizeof(serial) + 2;
  36652. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  36653. XMEMSET(serial, 0, sizeof(serial));
  36654. serialSz = sizeof(serial);
  36655. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  36656. WOLFSSL_SUCCESS);
  36657. AssertIntEQ(serialSz, 3);
  36658. AssertIntEQ(serial[0], 0x01);
  36659. AssertIntEQ(serial[1], 0x00);
  36660. AssertIntEQ(serial[2], 0x02);
  36661. ASN1_INTEGER_free(a);
  36662. X509_free(x509); /* free's a */
  36663. AssertNotNull(serialHex = BN_bn2hex(bn));
  36664. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  36665. AssertStrEQ(serialHex, "01");
  36666. #else
  36667. AssertStrEQ(serialHex, "1");
  36668. #endif
  36669. OPENSSL_free(serialHex);
  36670. AssertIntEQ(BN_get_word(bn), 1);
  36671. BN_free(bn);
  36672. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  36673. a = ASN1_INTEGER_new();
  36674. if (a) {
  36675. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  36676. DYNAMIC_TYPE_OPENSSL));
  36677. a->isDynamic = 1;
  36678. ASN1_INTEGER_free(a);
  36679. }
  36680. printf(resultFmt, passed);
  36681. #endif
  36682. return 0;
  36683. }
  36684. static int test_wolfSSL_OpenSSL_add_all_algorithms(void){
  36685. #if defined(OPENSSL_EXTRA)
  36686. printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
  36687. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  36688. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  36689. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  36690. printf(resultFmt, passed);
  36691. #endif
  36692. return 0;
  36693. }
  36694. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  36695. {
  36696. #if defined(OPENSSL_EXTRA)
  36697. #define MAX_HEXSTR_BUFSZ 9
  36698. #define NUM_CASES 5
  36699. struct Output {
  36700. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  36701. long ret;
  36702. };
  36703. int i;
  36704. int j;
  36705. const char* inputs[NUM_CASES] = {
  36706. "aabcd1357e",
  36707. "01:12:23:34:a5:b6:c7:d8:e9",
  36708. ":01:02",
  36709. "012",
  36710. ":ab:ac:d"
  36711. };
  36712. struct Output expectedOutputs[NUM_CASES] = {
  36713. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  36714. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  36715. {{0x01, 0x02}, 2},
  36716. {{0x00}, 0},
  36717. {{0x00}, 0}
  36718. };
  36719. long len = 0;
  36720. unsigned char* returnedBuf = NULL;
  36721. printf(testingFmt, "test_wolfSSL_OPENSSL_hexstr2buf()");
  36722. for (i = 0; i < NUM_CASES; ++i) {
  36723. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  36724. if (returnedBuf == NULL) {
  36725. AssertIntEQ(expectedOutputs[i].ret, 0);
  36726. continue;
  36727. }
  36728. AssertIntEQ(expectedOutputs[i].ret, len);
  36729. for (j = 0; j < len; ++j) {
  36730. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  36731. }
  36732. OPENSSL_free(returnedBuf);
  36733. }
  36734. printf(resultFmt, passed);
  36735. #endif
  36736. return 0;
  36737. }
  36738. static int test_wolfSSL_ASN1_STRING_print_ex(void){
  36739. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  36740. #ifndef NO_BIO
  36741. ASN1_STRING* asn_str;
  36742. const char data[] = "Hello wolfSSL!";
  36743. ASN1_STRING* esc_str;
  36744. const char esc_data[] = "a+;<>";
  36745. BIO *bio;
  36746. unsigned long flags;
  36747. int p_len;
  36748. unsigned char rbuf[255];
  36749. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  36750. /* setup */
  36751. XMEMSET(rbuf, 0, 255);
  36752. bio = BIO_new(BIO_s_mem());
  36753. BIO_set_write_buf_size(bio,255);
  36754. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  36755. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  36756. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  36757. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  36758. /* no flags */
  36759. XMEMSET(rbuf, 0, 255);
  36760. flags = 0;
  36761. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36762. AssertIntEQ(p_len, 15);
  36763. BIO_read(bio, (void*)rbuf, 15);
  36764. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  36765. /* RFC2253 Escape */
  36766. XMEMSET(rbuf, 0, 255);
  36767. flags = ASN1_STRFLGS_ESC_2253;
  36768. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  36769. AssertIntEQ(p_len, 9);
  36770. BIO_read(bio, (void*)rbuf, 9);
  36771. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  36772. /* Show type */
  36773. XMEMSET(rbuf, 0, 255);
  36774. flags = ASN1_STRFLGS_SHOW_TYPE;
  36775. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36776. AssertIntEQ(p_len, 28);
  36777. BIO_read(bio, (void*)rbuf, 28);
  36778. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  36779. /* Dump All */
  36780. XMEMSET(rbuf, 0, 255);
  36781. flags = ASN1_STRFLGS_DUMP_ALL;
  36782. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36783. AssertIntEQ(p_len, 31);
  36784. BIO_read(bio, (void*)rbuf, 31);
  36785. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  36786. /* Dump Der */
  36787. XMEMSET(rbuf, 0, 255);
  36788. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  36789. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36790. AssertIntEQ(p_len, 35);
  36791. BIO_read(bio, (void*)rbuf, 35);
  36792. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  36793. /* Dump All + Show type */
  36794. XMEMSET(rbuf, 0, 255);
  36795. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  36796. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36797. AssertIntEQ(p_len, 44);
  36798. BIO_read(bio, (void*)rbuf, 44);
  36799. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  36800. BIO_free(bio);
  36801. ASN1_STRING_free(asn_str);
  36802. ASN1_STRING_free(esc_str);
  36803. printf(resultFmt, passed);
  36804. #endif /* !NO_BIO */
  36805. #endif
  36806. return 0;
  36807. }
  36808. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  36809. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  36810. WOLFSSL_ASN1_TIME *t;
  36811. WOLFSSL_ASN1_TIME *out;
  36812. WOLFSSL_ASN1_TIME *gtime;
  36813. int tlen = 0;
  36814. unsigned char *data;
  36815. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  36816. /* UTC Time test */
  36817. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  36818. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  36819. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  36820. t->type = ASN_UTC_TIME;
  36821. t->length = ASN_UTC_TIME_SIZE;
  36822. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  36823. tlen = wolfSSL_ASN1_TIME_get_length(t);
  36824. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  36825. data = wolfSSL_ASN1_TIME_get_data(t);
  36826. AssertStrEQ((char*)data, "050727123456Z");
  36827. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  36828. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  36829. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  36830. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  36831. /* Generalized Time test */
  36832. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  36833. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  36834. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  36835. t->type = ASN_GENERALIZED_TIME;
  36836. t->length = ASN_GENERALIZED_TIME_SIZE;
  36837. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  36838. tlen = wolfSSL_ASN1_TIME_get_length(t);
  36839. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  36840. data = wolfSSL_ASN1_TIME_get_data(t);
  36841. AssertStrEQ((char*)data, "20050727123456Z");
  36842. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  36843. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  36844. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  36845. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  36846. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36847. /* Null parameter test */
  36848. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  36849. gtime = NULL;
  36850. out = NULL;
  36851. t->type = ASN_UTC_TIME;
  36852. t->length = ASN_UTC_TIME_SIZE;
  36853. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  36854. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  36855. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  36856. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  36857. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  36858. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36859. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36860. printf(resultFmt, passed);
  36861. #endif
  36862. return 0;
  36863. }
  36864. static int test_wolfSSL_X509_CA_num(void){
  36865. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36866. defined(HAVE_ECC) && !defined(NO_RSA)
  36867. WOLFSSL_X509_STORE *store;
  36868. WOLFSSL_X509 *x509_1, *x509_2;
  36869. int ca_num = 0;
  36870. printf(testingFmt, "wolfSSL_X509_CA_num()");
  36871. store = wolfSSL_X509_STORE_new();
  36872. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  36873. wolfSSL_X509_STORE_add_cert(store, x509_1);
  36874. ca_num = wolfSSL_X509_CA_num(store);
  36875. AssertIntEQ(ca_num, 1);
  36876. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  36877. wolfSSL_X509_STORE_add_cert(store, x509_2);
  36878. ca_num = wolfSSL_X509_CA_num(store);
  36879. AssertIntEQ(ca_num, 2);
  36880. wolfSSL_X509_free(x509_1);
  36881. wolfSSL_X509_free(x509_2);
  36882. wolfSSL_X509_STORE_free(store);
  36883. printf(resultFmt, passed);
  36884. #endif
  36885. return 0;
  36886. }
  36887. static int test_wolfSSL_X509_check_ca(void){
  36888. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36889. WOLFSSL_X509 *x509;
  36890. printf(testingFmt, "wolfSSL_X509_check_ca()");
  36891. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  36892. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  36893. wolfSSL_X509_free(x509);
  36894. printf(resultFmt, passed);
  36895. #endif
  36896. return 0;
  36897. }
  36898. static int test_wolfSSL_X509_check_ip_asc(void){
  36899. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36900. WOLFSSL_X509 *x509;
  36901. printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
  36902. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  36903. #if 0
  36904. /* TODO: add cert gen for testing positive case */
  36905. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  36906. #endif
  36907. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  36908. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  36909. wolfSSL_X509_free(x509);
  36910. printf(resultFmt, passed);
  36911. #endif
  36912. return 0;
  36913. }
  36914. static int test_wolfSSL_make_cert(void)
  36915. {
  36916. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  36917. int ret;
  36918. Cert cert;
  36919. CertName name;
  36920. RsaKey key;
  36921. WC_RNG rng;
  36922. byte der[FOURK_BUF];
  36923. word32 idx;
  36924. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  36925. #ifdef OPENSSL_EXTRA
  36926. const unsigned char* pt;
  36927. int certSz;
  36928. X509* x509;
  36929. X509_NAME* x509name;
  36930. X509_NAME_ENTRY* entry;
  36931. ASN1_STRING* entryValue;
  36932. #endif
  36933. printf(testingFmt, "wolfSSL Make Certs");
  36934. XMEMSET(&name, 0, sizeof(CertName));
  36935. /* set up cert name */
  36936. XMEMCPY(name.country, "US", sizeof("US"));
  36937. name.countryEnc = CTC_PRINTABLE;
  36938. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  36939. name.stateEnc = CTC_UTF8;
  36940. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  36941. name.localityEnc = CTC_UTF8;
  36942. XMEMCPY(name.sur, "Test", sizeof("Test"));
  36943. name.surEnc = CTC_UTF8;
  36944. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  36945. name.orgEnc = CTC_UTF8;
  36946. XMEMCPY(name.unit, "Development", sizeof("Development"));
  36947. name.unitEnc = CTC_UTF8;
  36948. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  36949. name.commonNameEnc = CTC_UTF8;
  36950. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  36951. name.serialDevEnc = CTC_PRINTABLE;
  36952. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  36953. name.userIdEnc = CTC_PRINTABLE;
  36954. #ifdef WOLFSSL_MULTI_ATTRIB
  36955. #if CTC_MAX_ATTRIB > 2
  36956. {
  36957. NameAttrib* n;
  36958. n = &name.name[0];
  36959. n->id = ASN_DOMAIN_COMPONENT;
  36960. n->type = CTC_UTF8;
  36961. n->sz = sizeof("com");
  36962. XMEMCPY(n->value, "com", sizeof("com"));
  36963. n = &name.name[1];
  36964. n->id = ASN_DOMAIN_COMPONENT;
  36965. n->type = CTC_UTF8;
  36966. n->sz = sizeof("wolfssl");
  36967. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  36968. }
  36969. #endif
  36970. #endif /* WOLFSSL_MULTI_ATTRIB */
  36971. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  36972. #ifndef HAVE_FIPS
  36973. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, devId), 0);
  36974. #else
  36975. AssertIntEQ(wc_InitRng(&rng), 0);
  36976. #endif
  36977. /* load test RSA key */
  36978. idx = 0;
  36979. #if defined(USE_CERT_BUFFERS_1024)
  36980. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  36981. sizeof_server_key_der_1024), 0);
  36982. #elif defined(USE_CERT_BUFFERS_2048)
  36983. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  36984. sizeof_server_key_der_2048), 0);
  36985. #else
  36986. /* error case, no RSA key loaded, happens later */
  36987. (void)idx;
  36988. #endif
  36989. XMEMSET(&cert, 0 , sizeof(Cert));
  36990. AssertIntEQ(wc_InitCert(&cert), 0);
  36991. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  36992. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  36993. cert.serialSz = (int)sizeof(mySerial);
  36994. cert.isCA = 1;
  36995. #ifndef NO_SHA256
  36996. cert.sigType = CTC_SHA256wRSA;
  36997. #else
  36998. cert.sigType = CTC_SHAwRSA;
  36999. #endif
  37000. /* add SKID from the Public Key */
  37001. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37002. /* add AKID from the Public Key */
  37003. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37004. ret = 0;
  37005. do {
  37006. #if defined(WOLFSSL_ASYNC_CRYPT)
  37007. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  37008. #endif
  37009. if (ret >= 0) {
  37010. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  37011. }
  37012. } while (ret == WC_PENDING_E);
  37013. AssertIntGT(ret, 0);
  37014. #ifdef OPENSSL_EXTRA
  37015. /* der holds a certificate with DC's now check X509 parsing of it */
  37016. certSz = ret;
  37017. pt = der;
  37018. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  37019. AssertNotNull(x509name = X509_get_subject_name(x509));
  37020. #ifdef WOLFSSL_MULTI_ATTRIB
  37021. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37022. -1)), 5);
  37023. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37024. idx)), 6);
  37025. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37026. idx)), -1);
  37027. #endif /* WOLFSSL_MULTI_ATTRIB */
  37028. /* compare DN at index 0 */
  37029. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  37030. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37031. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  37032. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  37033. #ifdef WOLFSSL_MULTI_ATTRIB
  37034. /* get first and second DC and compare result */
  37035. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37036. -1)), 5);
  37037. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37038. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37039. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  37040. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37041. idx)), 6);
  37042. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37043. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37044. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  37045. #endif /* WOLFSSL_MULTI_ATTRIB */
  37046. /* try invalid index locations for regression test and sanity check */
  37047. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  37048. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  37049. X509_free(x509);
  37050. #endif /* OPENSSL_EXTRA */
  37051. wc_FreeRsaKey(&key);
  37052. wc_FreeRng(&rng);
  37053. printf(resultFmt, passed);
  37054. #endif
  37055. return 0;
  37056. }
  37057. static int test_wolfSSL_X509_get_version(void){
  37058. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37059. WOLFSSL_X509 *x509;
  37060. printf(testingFmt, "wolfSSL_X509_get_version()");
  37061. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37062. AssertNotNull(x509);
  37063. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  37064. wolfSSL_X509_free(x509);
  37065. printf(resultFmt, passed);
  37066. #endif
  37067. return 0;
  37068. }
  37069. static int test_wolfSSL_DES_ncbc(void){
  37070. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37071. const_DES_cblock myDes;
  37072. DES_cblock iv = {1};
  37073. DES_key_schedule key = {0};
  37074. unsigned char msg[] = "hello wolfssl";
  37075. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  37076. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  37077. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  37078. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  37079. printf(testingFmt, "wolfSSL_DES_ncbc()");
  37080. /* partial block test */
  37081. DES_set_key(&key, &myDes);
  37082. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  37083. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  37084. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37085. DES_set_key(&key, &myDes);
  37086. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37087. *((byte*)&iv) = 1;
  37088. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  37089. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  37090. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37091. /* full block test */
  37092. DES_set_key(&key, &myDes);
  37093. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  37094. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37095. *((byte*)&iv) = 1;
  37096. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  37097. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  37098. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37099. DES_set_key(&key, &myDes);
  37100. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37101. *((byte*)&iv) = 1;
  37102. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  37103. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  37104. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37105. printf(resultFmt, passed);
  37106. #endif
  37107. return 0;
  37108. }
  37109. static int test_wolfSSL_AES_cbc_encrypt(void)
  37110. {
  37111. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  37112. AES_KEY aes;
  37113. AES_KEY* aesN = NULL;
  37114. size_t len = 0;
  37115. size_t lenB = 0;
  37116. int keySz0 = 0;
  37117. int keySzN = -1;
  37118. byte out[AES_BLOCK_SIZE] = {0};
  37119. byte* outN = NULL;
  37120. /* Test vectors retrieved from:
  37121. * <begin URL>
  37122. * https://csrc.nist.gov/
  37123. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  37124. * documents/aes/KAT_AES.zip
  37125. * </end URL>
  37126. */
  37127. const byte* pt128N = NULL;
  37128. byte* key128N = NULL;
  37129. byte* iv128N = NULL;
  37130. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  37131. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37132. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37133. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  37134. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  37135. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37136. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37137. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  37138. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  37139. len = sizeof(pt128);
  37140. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  37141. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  37142. AssertIntNE(XMEMCMP(b, g, h), i)
  37143. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  37144. printf(testingFmt, "Stressing wolfSSL_AES_cbc_encrypt()");
  37145. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37146. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  37147. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  37148. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37149. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  37150. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37151. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37152. printf(resultFmt, "Stress Tests: passed");
  37153. printf(testingFmt, "Stressing wolfSSL_AES_set_encrypt_key");
  37154. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37155. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  37156. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  37157. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  37158. printf(resultFmt, "Stress Tests: passed");
  37159. printf(testingFmt, "Stressing wolfSSL_AES_set_decrypt_key");
  37160. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37161. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  37162. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  37163. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  37164. printf(resultFmt, "Stress Tests: passed");
  37165. #ifdef WOLFSSL_AES_128
  37166. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit");
  37167. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37168. RESET_IV(iv128tmp, iv128);
  37169. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37170. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  37171. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37172. printf(resultFmt, "passed");
  37173. #ifdef HAVE_AES_DECRYPT
  37174. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode");
  37175. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37176. RESET_IV(iv128tmp, iv128);
  37177. len = sizeof(ct128);
  37178. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37179. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  37180. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  37181. printf(resultFmt, "passed");
  37182. #endif
  37183. #endif /* WOLFSSL_AES_128 */
  37184. #ifdef WOLFSSL_AES_192
  37185. {
  37186. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  37187. * Appendix F.2.3 */
  37188. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  37189. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37190. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37191. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  37192. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  37193. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37194. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37195. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  37196. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  37197. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  37198. len = sizeof(pt192);
  37199. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit");
  37200. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37201. RESET_IV(iv192tmp, iv192);
  37202. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37203. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  37204. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  37205. printf(resultFmt, "passed");
  37206. #ifdef HAVE_AES_DECRYPT
  37207. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode");
  37208. len = sizeof(ct192);
  37209. RESET_IV(iv192tmp, iv192);
  37210. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37211. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37212. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  37213. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  37214. printf(resultFmt, "passed");
  37215. #endif
  37216. }
  37217. #endif /* WOLFSSL_AES_192 */
  37218. #ifdef WOLFSSL_AES_256
  37219. {
  37220. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  37221. * Appendix F.2.5 */
  37222. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  37223. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37224. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37225. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  37226. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  37227. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37228. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37229. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37230. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37231. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37232. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  37233. len = sizeof(pt256);
  37234. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit");
  37235. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37236. RESET_IV(iv256tmp, iv256);
  37237. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37238. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  37239. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  37240. printf(resultFmt, "passed");
  37241. #ifdef HAVE_AES_DECRYPT
  37242. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode");
  37243. len = sizeof(ct256);
  37244. RESET_IV(iv256tmp, iv256);
  37245. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37246. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37247. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  37248. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  37249. printf(resultFmt, "passed");
  37250. #endif
  37251. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37252. {
  37253. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  37254. byte wrapPlain[sizeof(key256)] = { 0 };
  37255. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  37256. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit NULL iv");
  37257. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37258. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37259. 15), WOLFSSL_FAILURE);
  37260. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37261. sizeof(key256)), sizeof(wrapCipher));
  37262. printf(resultFmt, "passed");
  37263. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit NULL iv");
  37264. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37265. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37266. 23), WOLFSSL_FAILURE);
  37267. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37268. sizeof(wrapCipher)), sizeof(wrapPlain));
  37269. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37270. printf(resultFmt, "passed");
  37271. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  37272. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  37273. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit custom iv");
  37274. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37275. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  37276. sizeof(key256)), sizeof(wrapCipher));
  37277. printf(resultFmt, "passed");
  37278. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit custom iv");
  37279. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37280. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  37281. sizeof(wrapCipher)), sizeof(wrapPlain));
  37282. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37283. printf(resultFmt, "passed");
  37284. }
  37285. #endif /* HAVE_AES_KEYWRAP */
  37286. }
  37287. #endif /* WOLFSSL_AES_256 */
  37288. #endif
  37289. return 0;
  37290. }
  37291. static int test_wolfSSL_CRYPTO_cts128(void)
  37292. {
  37293. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  37294. && defined(HAVE_CTS)
  37295. byte tmp[64]; /* Largest vector size */
  37296. /* Test vectors taken form RFC3962 Appendix B */
  37297. const testVector vects[] = {
  37298. {
  37299. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37300. "\x20",
  37301. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  37302. "\x97",
  37303. 17, 17
  37304. },
  37305. {
  37306. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37307. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  37308. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  37309. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  37310. 31, 31
  37311. },
  37312. {
  37313. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37314. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  37315. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37316. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  37317. 32, 32
  37318. },
  37319. {
  37320. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37321. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37322. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  37323. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37324. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  37325. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  37326. 47, 47
  37327. },
  37328. {
  37329. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37330. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37331. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  37332. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37333. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  37334. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  37335. 48, 48
  37336. },
  37337. {
  37338. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37339. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37340. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  37341. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  37342. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37343. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37344. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  37345. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  37346. 64, 64
  37347. }
  37348. };
  37349. byte keyBytes[AES_128_KEY_SIZE] = {
  37350. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  37351. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  37352. };
  37353. size_t i;
  37354. XMEMSET(tmp, 0, sizeof(tmp));
  37355. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  37356. AES_KEY encKey;
  37357. AES_KEY decKey;
  37358. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  37359. XMEMSET(iv, 0, sizeof(iv));
  37360. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  37361. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  37362. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  37363. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  37364. vects[i].outLen);
  37365. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  37366. XMEMSET(iv, 0, sizeof(iv));
  37367. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  37368. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  37369. vects[i].inLen);
  37370. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  37371. }
  37372. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  37373. return 0;
  37374. }
  37375. #if defined(OPENSSL_ALL)
  37376. #if !defined(NO_ASN)
  37377. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  37378. {
  37379. #if !defined(NO_RSA)
  37380. WOLFSSL_X509* x509;
  37381. WOLFSSL_X509_NAME* subject;
  37382. WOLFSSL_X509_NAME_ENTRY* e;
  37383. WOLFSSL_ASN1_STRING* a;
  37384. FILE* file;
  37385. int idx = 0;
  37386. char targetOutput[16] = "www.wolfssl.com";
  37387. unsigned char* actual_output;
  37388. int len = 0;
  37389. int result = 0;
  37390. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  37391. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  37392. fclose(file);
  37393. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName");
  37394. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  37395. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  37396. NID_commonName, -1)), 5);
  37397. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  37398. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  37399. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  37400. result = strncmp((const char*)actual_output, targetOutput, len);
  37401. AssertIntEQ(result, 0);
  37402. printf(resultFmt, result == 0 ? passed : failed);
  37403. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
  37404. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  37405. WOLFSSL_FATAL_ERROR);
  37406. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  37407. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
  37408. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  37409. WOLFSSL_FATAL_ERROR);
  37410. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  37411. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
  37412. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  37413. WOLFSSL_FATAL_ERROR);
  37414. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  37415. wolfSSL_X509_free(x509);
  37416. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37417. #endif
  37418. return 0;
  37419. }
  37420. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  37421. {
  37422. ASN1_STRING* asn1str_test;
  37423. ASN1_STRING* asn1str_answer;
  37424. /* Each character is encoded using 4 bytes */
  37425. char input[] = {
  37426. 0, 0, 0, 'T',
  37427. 0, 0, 0, 'e',
  37428. 0, 0, 0, 's',
  37429. 0, 0, 0, 't',
  37430. };
  37431. char output[] = "Test";
  37432. printf(testingFmt, "test_wolfSSL_ASN1_UNIVERSALSTRING_to_string()");
  37433. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  37434. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  37435. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  37436. AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  37437. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  37438. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  37439. ASN1_STRING_free(asn1str_test);
  37440. ASN1_STRING_free(asn1str_answer);
  37441. printf(resultFmt, "passed");
  37442. return 0;
  37443. }
  37444. #endif /* !defined(NO_ASN) */
  37445. static int test_wolfSSL_sk_CIPHER_description(void)
  37446. {
  37447. #if !defined(NO_RSA)
  37448. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  37449. int i,j,k;
  37450. int numCiphers = 0;
  37451. const SSL_METHOD *method = NULL;
  37452. const SSL_CIPHER *cipher = NULL;
  37453. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  37454. SSL_CTX *ctx = NULL;
  37455. SSL *ssl = NULL;
  37456. char buf[256];
  37457. char test_str[9] = "0000000";
  37458. const char badStr[] = "unknown";
  37459. const char certPath[] = "./certs/client-cert.pem";
  37460. XMEMSET(buf, 0, sizeof(buf));
  37461. printf(testingFmt, "wolfSSL_sk_CIPHER_description");
  37462. AssertNotNull(method = TLSv1_2_client_method());
  37463. AssertNotNull(ctx = SSL_CTX_new(method));
  37464. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  37465. SSL_CTX_set_verify_depth(ctx, 4);
  37466. SSL_CTX_set_options(ctx, flags);
  37467. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  37468. WOLFSSL_SUCCESS);
  37469. AssertNotNull(ssl = SSL_new(ctx));
  37470. /* SSL_get_ciphers returns a stack of all configured ciphers
  37471. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  37472. */
  37473. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  37474. /* loop through the amount of supportedCiphers */
  37475. numCiphers = sk_num(supportedCiphers);
  37476. for (i = 0; i < numCiphers; ++i) {
  37477. /* sk_value increments "sk->data.cipher->cipherOffset".
  37478. * wolfSSL_sk_CIPHER_description sets the description for
  37479. * the cipher based on the provided offset.
  37480. */
  37481. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  37482. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  37483. }
  37484. /* Search cipher description string for "unknown" descriptor */
  37485. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  37486. k = 0;
  37487. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  37488. test_str[k] = badStr[k];
  37489. j++;
  37490. k++;
  37491. }
  37492. }
  37493. /* Fail if test_str == badStr == "unknown" */
  37494. AssertStrNE(test_str,badStr);
  37495. }
  37496. SSL_free(ssl);
  37497. SSL_CTX_free(ctx);
  37498. printf(resultFmt, passed);
  37499. #endif
  37500. return 0;
  37501. }
  37502. static int test_wolfSSL_get_ciphers_compat(void)
  37503. {
  37504. #if !defined(NO_RSA)
  37505. const SSL_METHOD *method = NULL;
  37506. const char certPath[] = "./certs/client-cert.pem";
  37507. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  37508. SSL_CTX *ctx = NULL;
  37509. WOLFSSL *ssl = NULL;
  37510. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  37511. printf(testingFmt, "wolfSSL_get_ciphers_compat");
  37512. method = SSLv23_client_method();
  37513. AssertNotNull(method);
  37514. ctx = SSL_CTX_new(method);
  37515. AssertNotNull(ctx);
  37516. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  37517. SSL_CTX_set_verify_depth(ctx, 4);
  37518. SSL_CTX_set_options(ctx, flags);
  37519. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  37520. WOLFSSL_SUCCESS);
  37521. AssertNotNull(ssl = SSL_new(ctx));
  37522. /* Test Bad NULL input */
  37523. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  37524. /* Test for Good input */
  37525. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  37526. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  37527. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  37528. SSL_free(ssl);
  37529. SSL_CTX_free(ctx);
  37530. printf(resultFmt, passed);
  37531. #endif
  37532. return 0;
  37533. }
  37534. static int test_wolfSSL_X509_PUBKEY_get(void)
  37535. {
  37536. WOLFSSL_X509_PUBKEY pubkey;
  37537. WOLFSSL_X509_PUBKEY* key;
  37538. WOLFSSL_EVP_PKEY evpkey ;
  37539. WOLFSSL_EVP_PKEY* evpPkey;
  37540. WOLFSSL_EVP_PKEY* retEvpPkey;
  37541. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  37542. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  37543. key = &pubkey;
  37544. evpPkey = &evpkey;
  37545. evpPkey->type = WOLFSSL_SUCCESS;
  37546. key->pkey = evpPkey;
  37547. printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
  37548. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  37549. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  37550. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  37551. key->pkey = NULL;
  37552. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  37553. printf(resultFmt,retEvpPkey == NULL ? passed : failed);
  37554. return 0;
  37555. }
  37556. static int test_wolfSSL_d2i_DHparams(void)
  37557. {
  37558. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  37559. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  37560. FILE* f = NULL;
  37561. unsigned char buf[4096];
  37562. const unsigned char* pt = buf;
  37563. #ifdef HAVE_FFDHE_2048
  37564. const char* params1 = "./certs/dh2048.der";
  37565. #endif
  37566. #ifdef HAVE_FFDHE_3072
  37567. const char* params2 = "./certs/dh3072.der";
  37568. #endif
  37569. long len = 0;
  37570. WOLFSSL_DH* dh = NULL;
  37571. XMEMSET(buf, 0, sizeof(buf));
  37572. /* Test 2048 bit parameters */
  37573. #ifdef HAVE_FFDHE_2048
  37574. printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
  37575. f = XFOPEN(params1, "rb");
  37576. AssertTrue(f != XBADFILE);
  37577. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37578. XFCLOSE(f);
  37579. /* Valid case */
  37580. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37581. AssertNotNull(dh->p);
  37582. AssertNotNull(dh->g);
  37583. AssertTrue(pt != buf);
  37584. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  37585. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), WOLFSSL_SUCCESS);
  37586. #endif
  37587. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  37588. /* Invalid cases */
  37589. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  37590. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  37591. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  37592. DH_free(dh);
  37593. printf(resultFmt, passed);
  37594. *buf = 0;
  37595. pt = buf;
  37596. #endif /* HAVE_FFDHE_2048 */
  37597. /* Test 3072 bit parameters */
  37598. #ifdef HAVE_FFDHE_3072
  37599. printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
  37600. f = XFOPEN(params2, "rb");
  37601. AssertTrue(f != XBADFILE);
  37602. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37603. XFCLOSE(f);
  37604. /* Valid case */
  37605. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37606. AssertNotNull(dh->p);
  37607. AssertNotNull(dh->g);
  37608. AssertTrue(pt != buf);
  37609. AssertIntEQ(DH_generate_key(dh), 1);
  37610. /* Invalid cases */
  37611. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  37612. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  37613. DH_free(dh);
  37614. printf(resultFmt, passed);
  37615. #endif /* HAVE_FFDHE_3072 */
  37616. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  37617. #endif /* !NO_DH */
  37618. return 0;
  37619. }
  37620. static int test_wolfSSL_i2d_DHparams(void)
  37621. {
  37622. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  37623. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  37624. FILE* f;
  37625. unsigned char buf[4096];
  37626. const unsigned char* pt = buf;
  37627. unsigned char* pt2 = buf;
  37628. #ifdef HAVE_FFDHE_2048
  37629. const char* params1 = "./certs/dh2048.der";
  37630. #endif
  37631. #ifdef HAVE_FFDHE_3072
  37632. const char* params2 = "./certs/dh3072.der";
  37633. #endif
  37634. long len;
  37635. WOLFSSL_DH* dh;
  37636. /* Test 2048 bit parameters */
  37637. #ifdef HAVE_FFDHE_2048
  37638. printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
  37639. f = XFOPEN(params1, "rb");
  37640. AssertTrue(f != XBADFILE);
  37641. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37642. XFCLOSE(f);
  37643. /* Valid case */
  37644. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37645. AssertTrue(pt != buf);
  37646. AssertIntEQ(DH_generate_key(dh), 1);
  37647. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  37648. /* Invalid case */
  37649. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  37650. /* Return length only */
  37651. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  37652. DH_free(dh);
  37653. printf(resultFmt, passed);
  37654. *buf = 0;
  37655. pt = buf;
  37656. pt2 = buf;
  37657. #endif
  37658. /* Test 3072 bit parameters */
  37659. #ifdef HAVE_FFDHE_3072
  37660. printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
  37661. f = XFOPEN(params2, "rb");
  37662. AssertTrue(f != XBADFILE);
  37663. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37664. XFCLOSE(f);
  37665. /* Valid case */
  37666. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37667. AssertTrue(pt != buf);
  37668. AssertIntEQ(DH_generate_key(dh), 1);
  37669. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  37670. /* Invalid case */
  37671. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  37672. /* Return length only */
  37673. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  37674. DH_free(dh);
  37675. printf(resultFmt, passed);
  37676. #endif
  37677. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  37678. #endif
  37679. return 0;
  37680. }
  37681. static int test_wolfSSL_EC_KEY_dup(void)
  37682. {
  37683. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  37684. defined(OPENSSL_EXTRA_X509_SMALL))
  37685. WOLFSSL_EC_KEY* ecKey;
  37686. WOLFSSL_EC_KEY* dupKey;
  37687. ecc_key* srcKey;
  37688. ecc_key* destKey;
  37689. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  37690. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37691. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37692. /* Valid cases */
  37693. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37694. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  37695. /* Compare pubkey */
  37696. srcKey = (ecc_key*)ecKey->internal;
  37697. destKey = (ecc_key*)dupKey->internal;
  37698. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  37699. /* compare EC_GROUP */
  37700. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  37701. /* compare EC_POINT */
  37702. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  37703. dupKey->pub_key, NULL), MP_EQ);
  37704. /* compare BIGNUM */
  37705. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  37706. wolfSSL_EC_KEY_free(dupKey);
  37707. /* Invalid cases */
  37708. /* NULL key */
  37709. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  37710. /* NULL ecc_key */
  37711. wc_ecc_free((ecc_key*)ecKey->internal);
  37712. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  37713. ecKey->internal = NULL; /* Set ecc_key to NULL */
  37714. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37715. wolfSSL_EC_KEY_free(ecKey);
  37716. wolfSSL_EC_KEY_free(dupKey);
  37717. /* NULL Group */
  37718. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37719. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37720. wolfSSL_EC_GROUP_free(ecKey->group);
  37721. ecKey->group = NULL; /* Set group to NULL */
  37722. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37723. wolfSSL_EC_KEY_free(ecKey);
  37724. wolfSSL_EC_KEY_free(dupKey);
  37725. /* NULL public key */
  37726. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37727. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37728. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  37729. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  37730. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37731. wolfSSL_EC_POINT_free(ecKey->pub_key);
  37732. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  37733. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37734. wolfSSL_EC_KEY_free(ecKey);
  37735. wolfSSL_EC_KEY_free(dupKey);
  37736. /* NULL private key */
  37737. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37738. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37739. wolfSSL_BN_free(ecKey->priv_key);
  37740. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  37741. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37742. wolfSSL_EC_KEY_free(ecKey);
  37743. wolfSSL_EC_KEY_free(dupKey);
  37744. /* Test EC_KEY_up_ref */
  37745. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37746. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  37747. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  37748. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  37749. /* reference count doesn't follow duplicate */
  37750. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37751. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  37752. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  37753. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  37754. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  37755. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  37756. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  37757. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  37758. printf(resultFmt, passed);
  37759. #endif
  37760. return 0;
  37761. }
  37762. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  37763. {
  37764. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  37765. DSA *dsa = NULL;
  37766. DSA *setDsa = NULL;
  37767. EVP_PKEY *pkey = NULL;
  37768. EVP_PKEY *set1Pkey = NULL;
  37769. SHA_CTX sha;
  37770. byte signature[DSA_SIG_SIZE];
  37771. byte hash[WC_SHA_DIGEST_SIZE];
  37772. word32 bytes;
  37773. int answer;
  37774. #ifdef USE_CERT_BUFFERS_1024
  37775. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  37776. int dsaKeySz = sizeof_dsa_key_der_1024;
  37777. byte tmp[ONEK_BUF];
  37778. XMEMSET(tmp, 0, sizeof(tmp));
  37779. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  37780. bytes = dsaKeySz;
  37781. #elif defined(USE_CERT_BUFFERS_2048)
  37782. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  37783. int dsaKeySz = sizeof_dsa_key_der_2048;
  37784. byte tmp[TWOK_BUF];
  37785. XMEMSET(tmp, 0, sizeof(tmp));
  37786. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  37787. bytes = dsaKeySz;
  37788. #else
  37789. byte tmp[TWOK_BUF];
  37790. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  37791. int dsaKeySz;
  37792. XMEMSET(tmp, 0, sizeof(tmp));
  37793. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  37794. if (fp == XBADFILE) {
  37795. return WOLFSSL_BAD_FILE;
  37796. }
  37797. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  37798. XFCLOSE(fp);
  37799. #endif /* END USE_CERT_BUFFERS_1024 */
  37800. printf(testingFmt,
  37801. "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
  37802. /* Create hash to later Sign and Verify */
  37803. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  37804. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  37805. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  37806. /* Initialize pkey with der format dsa key */
  37807. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  37808. &dsaKeyDer ,(long)dsaKeySz));
  37809. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  37810. /* Should Fail: NULL argument */
  37811. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  37812. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  37813. /* Should Pass: Initialized pkey argument */
  37814. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  37815. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  37816. #ifdef USE_CERT_BUFFERS_1024
  37817. AssertIntEQ(DSA_bits(dsa), 1024);
  37818. #else
  37819. AssertIntEQ(DSA_bits(dsa), 2048);
  37820. #endif
  37821. /* Sign */
  37822. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  37823. /* Verify. */
  37824. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  37825. WOLFSSL_SUCCESS);
  37826. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  37827. /* Should Fail: set1Pkey not initialized */
  37828. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  37829. /* Initialize set1Pkey */
  37830. set1Pkey = EVP_PKEY_new();
  37831. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  37832. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  37833. WOLFSSL_SUCCESS);
  37834. /* Should Pass: set dsa into set1Pkey */
  37835. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  37836. printf(resultFmt, passed);
  37837. DSA_free(dsa);
  37838. DSA_free(setDsa);
  37839. EVP_PKEY_free(pkey);
  37840. EVP_PKEY_free(set1Pkey);
  37841. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  37842. return 0;
  37843. } /* END test_EVP_PKEY_set1_get1_DSA */
  37844. static int test_wolfSSL_DSA_SIG(void)
  37845. {
  37846. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  37847. !defined(HAVE_FIPS)
  37848. DSA *dsa = NULL;
  37849. DSA *dsa2 = NULL;
  37850. DSA_SIG *sig = NULL;
  37851. const BIGNUM *p = NULL;
  37852. const BIGNUM *q = NULL;
  37853. const BIGNUM *g = NULL;
  37854. const BIGNUM *pub = NULL;
  37855. const BIGNUM *priv = NULL;
  37856. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  37857. printf(testingFmt, "wolfSSL_DSA_SIG");
  37858. AssertNotNull(dsa = DSA_generate_parameters(2048,
  37859. NULL, 0, NULL, NULL, NULL, NULL));
  37860. DSA_free(dsa);
  37861. AssertNotNull(dsa = DSA_new());
  37862. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  37863. NULL, 0, NULL, NULL, NULL), 1);
  37864. AssertIntEQ(DSA_generate_key(dsa), 1);
  37865. DSA_get0_pqg(dsa, &p, &q, &g);
  37866. DSA_get0_key(dsa, &pub, &priv);
  37867. AssertNotNull(p = BN_dup(p));
  37868. AssertNotNull(q = BN_dup(q));
  37869. AssertNotNull(g = BN_dup(g));
  37870. AssertNotNull(pub = BN_dup(pub));
  37871. AssertNotNull(priv = BN_dup(priv));
  37872. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  37873. AssertNotNull(dsa2 = DSA_new());
  37874. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  37875. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  37876. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  37877. printf(resultFmt, passed);
  37878. DSA_free(dsa);
  37879. DSA_free(dsa2);
  37880. DSA_SIG_free(sig);
  37881. #endif
  37882. return 0;
  37883. }
  37884. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  37885. {
  37886. #ifdef HAVE_ECC
  37887. WOLFSSL_EC_KEY *ecKey = NULL;
  37888. WOLFSSL_EC_KEY *ecGet1 = NULL;
  37889. EVP_PKEY *pkey = NULL;
  37890. printf(testingFmt,
  37891. "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
  37892. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37893. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  37894. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  37895. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  37896. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  37897. /* Should fail since ecKey is empty */
  37898. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  37899. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37900. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  37901. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  37902. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  37903. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  37904. wolfSSL_EC_KEY_free(ecKey);
  37905. wolfSSL_EC_KEY_free(ecGet1);
  37906. EVP_PKEY_free(pkey);
  37907. /* PASSED */
  37908. printf(resultFmt, passed);
  37909. #endif /* HAVE_ECC */
  37910. return 0;
  37911. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  37912. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  37913. {
  37914. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  37915. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  37916. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  37917. DH *dh = NULL;
  37918. DH *setDh = NULL;
  37919. EVP_PKEY *pkey = NULL;
  37920. FILE* f = NULL;
  37921. unsigned char buf[4096];
  37922. const unsigned char* pt = buf;
  37923. const char* dh2048 = "./certs/dh2048.der";
  37924. long len = 0;
  37925. int code = -1;
  37926. printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
  37927. XMEMSET(buf, 0, sizeof(buf));
  37928. f = XFOPEN(dh2048, "rb");
  37929. AssertTrue(f != XBADFILE);
  37930. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37931. XFCLOSE(f);
  37932. /* Load dh2048.der into DH with internal format */
  37933. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37934. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  37935. AssertIntEQ(code, 0);
  37936. code = -1;
  37937. pkey = wolfSSL_EVP_PKEY_new();
  37938. /* Set DH into PKEY */
  37939. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  37940. /* Get DH from PKEY */
  37941. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  37942. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  37943. AssertIntEQ(code, 0);
  37944. EVP_PKEY_free(pkey);
  37945. DH_free(setDh);
  37946. DH_free(dh);
  37947. printf(resultFmt, passed);
  37948. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  37949. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  37950. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  37951. return 0;
  37952. } /* END test_EVP_PKEY_set1_get1_DH */
  37953. static int test_wolfSSL_CTX_ctrl(void)
  37954. {
  37955. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  37956. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37957. char caFile[] = "./certs/client-ca.pem";
  37958. char clientFile[] = "./certs/client-cert.pem";
  37959. SSL_CTX* ctx;
  37960. X509* x509 = NULL;
  37961. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  37962. byte buf[6000];
  37963. char file[] = "./certs/dsaparams.pem";
  37964. XFILE f;
  37965. int bytes;
  37966. BIO* bio;
  37967. DSA* dsa;
  37968. DH* dh;
  37969. #endif
  37970. #ifdef HAVE_ECC
  37971. WOLFSSL_EC_KEY* ecKey;
  37972. #endif
  37973. printf(testingFmt, "wolfSSL_CTX_ctrl");
  37974. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37975. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  37976. AssertNotNull(x509);
  37977. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  37978. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  37979. AssertNotNull(x509);
  37980. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  37981. /* Initialize DH */
  37982. f = XFOPEN(file, "rb");
  37983. AssertTrue((f != XBADFILE));
  37984. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  37985. XFCLOSE(f);
  37986. bio = BIO_new_mem_buf((void*)buf, bytes);
  37987. AssertNotNull(bio);
  37988. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  37989. AssertNotNull(dsa);
  37990. dh = wolfSSL_DSA_dup_DH(dsa);
  37991. AssertNotNull(dh);
  37992. #endif
  37993. #ifdef HAVE_ECC
  37994. /* Initialize WOLFSSL_EC_KEY */
  37995. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37996. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  37997. #endif
  37998. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  37999. /* additional test of getting EVP_PKEY key size from X509
  38000. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  38001. * allowed with user RSA */
  38002. {
  38003. EVP_PKEY* pkey;
  38004. #if defined(HAVE_ECC)
  38005. X509* ecX509;
  38006. #endif /* HAVE_ECC */
  38007. AssertNotNull(pkey = X509_get_pubkey(x509));
  38008. /* current RSA key is 2048 bit (256 bytes) */
  38009. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  38010. EVP_PKEY_free(pkey);
  38011. #if defined(HAVE_ECC)
  38012. #if defined(USE_CERT_BUFFERS_256)
  38013. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  38014. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  38015. SSL_FILETYPE_ASN1));
  38016. #else
  38017. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  38018. cliEccCertFile, SSL_FILETYPE_PEM));
  38019. #endif
  38020. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  38021. /* current ECC key is 256 bit (32 bytes) */
  38022. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  38023. X509_free(ecX509);
  38024. EVP_PKEY_free(pkey);
  38025. #endif /* HAVE_ECC */
  38026. }
  38027. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  38028. /* Tests should fail with passed in NULL pointer */
  38029. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  38030. SSL_FAILURE);
  38031. #if !defined(NO_DH) && !defined(NO_DSA)
  38032. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  38033. SSL_FAILURE);
  38034. #endif
  38035. #ifdef HAVE_ECC
  38036. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  38037. SSL_FAILURE);
  38038. #endif
  38039. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  38040. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  38041. */
  38042. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  38043. SSL_SUCCESS);
  38044. /* Test with SSL_CTRL_OPTIONS
  38045. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  38046. */
  38047. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  38048. == SSL_OP_NO_TLSv1);
  38049. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  38050. /* Test with SSL_CTRL_SET_TMP_DH
  38051. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  38052. */
  38053. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38054. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  38055. SSL_SUCCESS);
  38056. #endif
  38057. /* Test with SSL_CTRL_SET_TMP_ECDH
  38058. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  38059. */
  38060. #ifdef HAVE_ECC
  38061. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  38062. SSL_SUCCESS);
  38063. #endif
  38064. #ifdef WOLFSSL_ENCRYPTED_KEYS
  38065. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  38066. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  38067. #endif
  38068. /* Test for min/max proto */
  38069. #ifndef WOLFSSL_NO_TLS12
  38070. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38071. 0, NULL), SSL_SUCCESS);
  38072. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38073. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38074. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  38075. #endif
  38076. #ifdef WOLFSSL_TLS13
  38077. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38078. 0, NULL), SSL_SUCCESS);
  38079. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38080. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  38081. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  38082. #ifndef WOLFSSL_NO_TLS12
  38083. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38084. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38085. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  38086. #endif
  38087. #endif
  38088. /* Cleanup and Pass */
  38089. #if !defined(NO_DH) && !defined(NO_DSA)
  38090. #ifndef NO_BIO
  38091. BIO_free(bio);
  38092. DSA_free(dsa);
  38093. DH_free(dh);
  38094. #endif
  38095. #endif
  38096. #ifdef HAVE_ECC
  38097. wolfSSL_EC_KEY_free(ecKey);
  38098. #endif
  38099. SSL_CTX_free(ctx);
  38100. printf(resultFmt, passed);
  38101. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38102. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  38103. return 0;
  38104. }
  38105. static int test_wolfSSL_DH_check(void)
  38106. {
  38107. #if !defined(NO_DH) && !defined(NO_DSA)
  38108. #ifndef NO_BIO
  38109. byte buf[6000];
  38110. char file[] = "./certs/dsaparams.pem";
  38111. XFILE f;
  38112. int bytes;
  38113. BIO* bio;
  38114. DSA* dsa;
  38115. DH* dh = NULL;
  38116. WOLFSSL_BIGNUM* pTmp = NULL;
  38117. WOLFSSL_BIGNUM* gTmp = NULL;
  38118. int codes = -1;
  38119. printf(testingFmt, "wolfSSL_DH_check");
  38120. /* Initialize DH */
  38121. f = XFOPEN(file, "rb");
  38122. AssertTrue((f != XBADFILE));
  38123. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  38124. XFCLOSE(f);
  38125. bio = BIO_new_mem_buf((void*)buf, bytes);
  38126. AssertNotNull(bio);
  38127. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  38128. AssertNotNull(dsa);
  38129. dh = wolfSSL_DSA_dup_DH(dsa);
  38130. AssertNotNull(dh);
  38131. /* Test assumed to be valid dh.
  38132. * Should return WOLFSSL_SUCCESS
  38133. * codes should be 0
  38134. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  38135. */
  38136. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_SUCCESS);
  38137. AssertIntEQ(codes, 0);
  38138. /* Test NULL dh: expected BAD_FUNC_ARG */
  38139. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), WOLFSSL_FAILURE);
  38140. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  38141. pTmp = dh->p;
  38142. dh->p = NULL;
  38143. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  38144. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  38145. /* set dh->p back to normal so it wont fail on next tests */
  38146. dh->p = pTmp;
  38147. pTmp = NULL;
  38148. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  38149. gTmp = dh->g;
  38150. dh->g = NULL;
  38151. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  38152. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  38153. dh->g = gTmp;
  38154. gTmp = NULL;
  38155. /* Cleanup and Pass Test */
  38156. BIO_free(bio);
  38157. DSA_free(dsa);
  38158. DH_free(dh);
  38159. printf(resultFmt, passed);
  38160. #endif
  38161. #endif /* !NO_DH && !NO_DSA */
  38162. return 0;
  38163. }
  38164. static int test_wolfSSL_EVP_PKEY_assign(void)
  38165. {
  38166. int type;
  38167. WOLFSSL_EVP_PKEY* pkey;
  38168. #ifndef NO_RSA
  38169. WOLFSSL_RSA* rsa;
  38170. #endif
  38171. #ifndef NO_DSA
  38172. WOLFSSL_DSA* dsa;
  38173. #endif
  38174. #ifdef HAVE_ECC
  38175. WOLFSSL_EC_KEY* ecKey;
  38176. #endif
  38177. (void)pkey;
  38178. printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
  38179. #ifndef NO_RSA
  38180. type = EVP_PKEY_RSA;
  38181. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38182. AssertNotNull(rsa = wolfSSL_RSA_new());
  38183. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  38184. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38185. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  38186. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  38187. wolfSSL_EVP_PKEY_free(pkey);
  38188. #endif /* NO_RSA */
  38189. #ifndef NO_DSA
  38190. type = EVP_PKEY_DSA;
  38191. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38192. AssertNotNull(dsa = wolfSSL_DSA_new());
  38193. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  38194. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38195. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  38196. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  38197. wolfSSL_EVP_PKEY_free(pkey);
  38198. #endif /* NO_DSA */
  38199. #ifdef HAVE_ECC
  38200. type = EVP_PKEY_EC;
  38201. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38202. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38203. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  38204. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38205. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  38206. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  38207. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38208. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  38209. wolfSSL_EVP_PKEY_free(pkey);
  38210. #endif /* HAVE_ECC */
  38211. (void)type;
  38212. printf(resultFmt, passed);
  38213. return 0;
  38214. }
  38215. static int test_wolfSSL_EVP_PKEY_base_id(void)
  38216. {
  38217. WOLFSSL_EVP_PKEY* pkey;
  38218. printf(testingFmt, "wolfSSL_EVP_PKEY_base_id");
  38219. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38220. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  38221. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  38222. EVP_PKEY_free(pkey);
  38223. printf(resultFmt, passed);
  38224. return 0;
  38225. }
  38226. static int test_wolfSSL_EVP_PKEY_id(void)
  38227. {
  38228. WOLFSSL_EVP_PKEY* pkey;
  38229. printf(testingFmt, "wolfSSL_EVP_PKEY_id");
  38230. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38231. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  38232. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  38233. EVP_PKEY_free(pkey);
  38234. printf(resultFmt, passed);
  38235. return 0;
  38236. }
  38237. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  38238. {
  38239. #if defined(OPENSSL_ALL) && \
  38240. !defined(NO_ECC_SECP) && \
  38241. /* This last bit is taken from ecc.c. It is the condition that
  38242. * defines ECC256 */ \
  38243. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  38244. ECC_MIN_KEY_SZ <= 256)
  38245. EVP_PKEY_CTX* ctx;
  38246. EVP_PKEY* pkey = NULL;
  38247. printf(testingFmt, "wolfSSL_EVP_PKEY_paramgen");
  38248. /* Test error conditions. */
  38249. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  38250. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38251. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  38252. #ifndef NO_RSA
  38253. EVP_PKEY_CTX_free(ctx);
  38254. /* Parameter generation for RSA not supported yet. */
  38255. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  38256. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  38257. #endif
  38258. #ifdef HAVE_ECC
  38259. EVP_PKEY_CTX_free(ctx);
  38260. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38261. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  38262. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  38263. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  38264. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  38265. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  38266. WOLFSSL_SUCCESS);
  38267. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38268. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  38269. #endif
  38270. EVP_PKEY_CTX_free(ctx);
  38271. EVP_PKEY_free(pkey);
  38272. printf(resultFmt, passed);
  38273. #endif
  38274. return 0;
  38275. }
  38276. static int test_wolfSSL_EVP_PKEY_keygen(void)
  38277. {
  38278. WOLFSSL_EVP_PKEY* pkey = NULL;
  38279. EVP_PKEY_CTX* ctx = NULL;
  38280. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  38281. WOLFSSL_EVP_PKEY* params = NULL;
  38282. DH* dh = NULL;
  38283. const BIGNUM* pubkey = NULL;
  38284. const BIGNUM* privkey = NULL;
  38285. ASN1_INTEGER* asn1int = NULL;
  38286. unsigned int length = 0;
  38287. byte* derBuffer = NULL;
  38288. #endif
  38289. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen");
  38290. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38291. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38292. /* Bad cases */
  38293. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  38294. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  38295. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  38296. /* Good case */
  38297. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  38298. EVP_PKEY_CTX_free(ctx);
  38299. EVP_PKEY_free(pkey);
  38300. pkey = NULL;
  38301. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  38302. /* Test DH keygen */
  38303. {
  38304. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  38305. AssertNotNull(dh = DH_get_2048_256());
  38306. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  38307. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  38308. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38309. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  38310. DH_free(dh);
  38311. EVP_PKEY_CTX_free(ctx);
  38312. EVP_PKEY_free(params);
  38313. /* try exporting generated key to DER, to verify */
  38314. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  38315. DH_get0_key(dh, &pubkey, &privkey);
  38316. AssertNotNull(pubkey);
  38317. AssertNotNull(privkey);
  38318. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  38319. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  38320. ASN1_INTEGER_free(asn1int);
  38321. DH_free(dh);
  38322. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38323. EVP_PKEY_free(pkey);
  38324. }
  38325. #endif
  38326. printf(resultFmt, passed);
  38327. return 0;
  38328. }
  38329. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  38330. {
  38331. WOLFSSL_EVP_PKEY* pkey;
  38332. EVP_PKEY_CTX *ctx;
  38333. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen_init");
  38334. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38335. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38336. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38337. EVP_PKEY_CTX_free(ctx);
  38338. EVP_PKEY_free(pkey);
  38339. printf(resultFmt, passed);
  38340. return 0;
  38341. }
  38342. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  38343. {
  38344. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  38345. WOLFSSL_EVP_PKEY* pkey;
  38346. printf(testingFmt, "wolfSSL_EVP_PKEY_missing_parameters");
  38347. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38348. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  38349. EVP_PKEY_free(pkey);
  38350. printf(resultFmt, passed);
  38351. #endif
  38352. return 0;
  38353. }
  38354. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  38355. {
  38356. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  38357. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  38358. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  38359. !defined(NO_FILESYSTEM)
  38360. WOLFSSL_EVP_PKEY* params = NULL;
  38361. WOLFSSL_EVP_PKEY* copy = NULL;
  38362. DH* dh = NULL;
  38363. BIGNUM* p1;
  38364. BIGNUM* g1;
  38365. BIGNUM* q1;
  38366. BIGNUM* p2;
  38367. BIGNUM* g2;
  38368. BIGNUM* q2;
  38369. printf(testingFmt, "wolfSSL_EVP_PKEY_copy_parameters");
  38370. /* create DH with DH_get_2048_256 params */
  38371. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  38372. AssertNotNull(dh = DH_get_2048_256());
  38373. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  38374. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  38375. (const BIGNUM**)&q1,
  38376. (const BIGNUM**)&g1);
  38377. DH_free(dh);
  38378. /* create DH with random generated DH params */
  38379. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  38380. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  38381. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  38382. DH_free(dh);
  38383. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  38384. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  38385. AssertNotNull(dh->p);
  38386. AssertNotNull(dh->g);
  38387. AssertNotNull(dh->q);
  38388. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  38389. (const BIGNUM**)&q2,
  38390. (const BIGNUM**)&g2);
  38391. AssertIntEQ(BN_cmp(p1, p2), 0);
  38392. AssertIntEQ(BN_cmp(q1, q2), 0);
  38393. AssertIntEQ(BN_cmp(g1, g2), 0);
  38394. DH_free(dh);
  38395. EVP_PKEY_free(copy);
  38396. EVP_PKEY_free(params);
  38397. printf(resultFmt, passed);
  38398. #endif
  38399. return 0;
  38400. }
  38401. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  38402. {
  38403. WOLFSSL_EVP_PKEY* pkey;
  38404. EVP_PKEY_CTX *ctx;
  38405. int bits = 2048;
  38406. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits");
  38407. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38408. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38409. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  38410. WOLFSSL_SUCCESS);
  38411. EVP_PKEY_CTX_free(ctx);
  38412. EVP_PKEY_free(pkey);
  38413. printf(resultFmt, passed);
  38414. return 0;
  38415. }
  38416. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  38417. {
  38418. /* This is large enough to be used for all key sizes */
  38419. byte key[AES_256_KEY_SIZE] = {0};
  38420. byte iv[AES_BLOCK_SIZE] = {0};
  38421. int i, enumlen;
  38422. EVP_CIPHER_CTX *ctx;
  38423. const EVP_CIPHER *init;
  38424. int enumArray[] = {
  38425. #ifdef HAVE_AES_CBC
  38426. NID_aes_128_cbc,
  38427. #endif
  38428. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38429. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38430. #ifdef HAVE_AESGCM
  38431. NID_aes_128_gcm,
  38432. #endif
  38433. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38434. #ifdef WOLFSSL_AES_COUNTER
  38435. NID_aes_128_ctr,
  38436. #endif
  38437. #ifndef NO_DES3
  38438. NID_des_cbc,
  38439. NID_des_ede3_cbc,
  38440. #endif
  38441. };
  38442. int iv_lengths[] = {
  38443. #ifdef HAVE_AES_CBC
  38444. AES_BLOCK_SIZE,
  38445. #endif
  38446. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38447. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38448. #ifdef HAVE_AESGCM
  38449. GCM_NONCE_MID_SZ,
  38450. #endif
  38451. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38452. #ifdef WOLFSSL_AES_COUNTER
  38453. AES_BLOCK_SIZE,
  38454. #endif
  38455. #ifndef NO_DES3
  38456. DES_BLOCK_SIZE,
  38457. DES_BLOCK_SIZE,
  38458. #endif
  38459. };
  38460. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_iv_length");
  38461. enumlen = (sizeof(enumArray)/sizeof(int));
  38462. for(i = 0; i < enumlen; i++)
  38463. {
  38464. ctx = EVP_CIPHER_CTX_new();
  38465. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  38466. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38467. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38468. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  38469. EVP_CIPHER_CTX_free(ctx);
  38470. }
  38471. printf(resultFmt, passed);
  38472. return 0;
  38473. }
  38474. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  38475. {
  38476. #if !defined(NO_DES3)
  38477. byte key[AES_256_KEY_SIZE] = {0};
  38478. byte iv[AES_BLOCK_SIZE] = {0};
  38479. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  38480. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  38481. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_key_length");
  38482. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38483. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38484. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  38485. EVP_CIPHER_CTX_free(ctx);
  38486. printf(resultFmt, passed);
  38487. #endif
  38488. return 0;
  38489. }
  38490. static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  38491. {
  38492. #if !defined(NO_DES3)
  38493. byte key[AES_256_KEY_SIZE] = {0};
  38494. byte iv[AES_BLOCK_SIZE] = {0};
  38495. int keylen;
  38496. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  38497. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  38498. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_key_length");
  38499. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38500. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38501. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  38502. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  38503. WOLFSSL_SUCCESS);
  38504. EVP_CIPHER_CTX_free(ctx);
  38505. printf(resultFmt, passed);
  38506. #endif
  38507. return 0;
  38508. }
  38509. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  38510. {
  38511. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  38512. byte key[DES3_KEY_SIZE] = {0};
  38513. byte iv[DES_BLOCK_SIZE] = {0};
  38514. int ivLen, keyLen;
  38515. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  38516. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  38517. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_iv");
  38518. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38519. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38520. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  38521. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  38522. /* Bad cases */
  38523. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  38524. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  38525. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  38526. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  38527. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  38528. /* Good case */
  38529. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  38530. EVP_CIPHER_CTX_free(ctx);
  38531. printf(resultFmt, passed);
  38532. #endif
  38533. return 0;
  38534. }
  38535. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  38536. {
  38537. WOLFSSL_ENGINE* e = NULL;
  38538. int id = 0;
  38539. EVP_PKEY_CTX *ctx;
  38540. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_new_id");
  38541. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  38542. EVP_PKEY_CTX_free(ctx);
  38543. printf(resultFmt, passed);
  38544. return 0;
  38545. }
  38546. static int test_wolfSSL_EVP_rc4(void)
  38547. {
  38548. #if !defined(NO_RC4)
  38549. printf(testingFmt, "wolfSSL_EVP_rc4");
  38550. AssertNotNull(wolfSSL_EVP_rc4());
  38551. printf(resultFmt, passed);
  38552. #endif
  38553. return 0;
  38554. }
  38555. static int test_wolfSSL_EVP_enc_null(void)
  38556. {
  38557. printf(testingFmt, "wolfSSL_EVP_enc_null");
  38558. AssertNotNull(wolfSSL_EVP_enc_null());
  38559. printf(resultFmt, passed);
  38560. return 0;
  38561. }
  38562. static int test_wolfSSL_EVP_rc2_cbc(void)
  38563. {
  38564. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  38565. printf(testingFmt, "wolfSSL_EVP_rc2_cbc");
  38566. AssertNull(wolfSSL_EVP_rc2_cbc());
  38567. printf(resultFmt, passed);
  38568. #endif
  38569. return 0;
  38570. }
  38571. static int test_wolfSSL_EVP_mdc2(void)
  38572. {
  38573. #if !defined(NO_WOLFSSL_STUB)
  38574. printf(testingFmt, "wolfSSL_EVP_mdc2");
  38575. AssertNull(wolfSSL_EVP_mdc2());
  38576. printf(resultFmt, passed);
  38577. #endif
  38578. return 0;
  38579. }
  38580. static int test_wolfSSL_EVP_md4(void)
  38581. {
  38582. #if !defined(NO_MD4)
  38583. printf(testingFmt, "wolfSSL_EVP_md4");
  38584. AssertNotNull(wolfSSL_EVP_md4());
  38585. printf(resultFmt, passed);
  38586. #endif
  38587. return 0;
  38588. }
  38589. static int test_wolfSSL_EVP_aes_256_gcm(void)
  38590. {
  38591. printf(testingFmt, "wolfSSL_EVP_aes_256_gcm");
  38592. AssertNotNull(wolfSSL_EVP_aes_256_gcm());
  38593. printf(resultFmt, passed);
  38594. return 0;
  38595. }
  38596. static int test_wolfSSL_EVP_aes_192_gcm(void)
  38597. {
  38598. printf(testingFmt, "wolfSSL_EVP_aes_192_gcm");
  38599. AssertNotNull(wolfSSL_EVP_aes_192_gcm());
  38600. printf(resultFmt, passed);
  38601. return 0;
  38602. }
  38603. static int test_wolfSSL_EVP_ripemd160(void)
  38604. {
  38605. #if !defined(NO_WOLFSSL_STUB)
  38606. printf(testingFmt, "wolfSSL_EVP_ripemd160");
  38607. AssertNull(wolfSSL_EVP_ripemd160());
  38608. printf(resultFmt, passed);
  38609. #endif
  38610. return 0;
  38611. }
  38612. static int test_wolfSSL_EVP_get_digestbynid(void)
  38613. {
  38614. printf(testingFmt, "wolfSSL_EVP_get_digestbynid");
  38615. #ifndef NO_MD5
  38616. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  38617. #endif
  38618. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  38619. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  38620. printf(resultFmt, passed);
  38621. return 0;
  38622. }
  38623. static int test_wolfSSL_EVP_MD_nid(void)
  38624. {
  38625. printf(testingFmt, "wolfSSL_EVP_MD_nid");
  38626. #ifndef NO_MD5
  38627. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  38628. #endif
  38629. #ifndef NO_SHA
  38630. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  38631. #endif
  38632. #ifndef NO_SHA256
  38633. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  38634. #endif
  38635. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  38636. printf(resultFmt, passed);
  38637. return 0;
  38638. }
  38639. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  38640. {
  38641. #if defined(HAVE_ECC)
  38642. WOLFSSL_EVP_PKEY* pkey;
  38643. printf(testingFmt, "wolfSSL_EVP_PKEY_get0_EC_KEY");
  38644. AssertNotNull(pkey = EVP_PKEY_new());
  38645. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  38646. EVP_PKEY_free(pkey);
  38647. printf(resultFmt, passed);
  38648. #endif
  38649. return 0;
  38650. }
  38651. static int test_wolfSSL_EVP_X_STATE(void)
  38652. {
  38653. #if !defined(NO_DES3) && !defined(NO_RC4)
  38654. byte key[DES3_KEY_SIZE] = {0};
  38655. byte iv[DES_IV_SIZE] = {0};
  38656. EVP_CIPHER_CTX *ctx;
  38657. const EVP_CIPHER *init;
  38658. printf(testingFmt, "wolfSSL_EVP_X_STATE");
  38659. /* Bad test cases */
  38660. ctx = EVP_CIPHER_CTX_new();
  38661. init = EVP_des_ede3_cbc();
  38662. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38663. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38664. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  38665. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  38666. EVP_CIPHER_CTX_free(ctx);
  38667. /* Good test case */
  38668. ctx = EVP_CIPHER_CTX_new();
  38669. init = wolfSSL_EVP_rc4();
  38670. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38671. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38672. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  38673. EVP_CIPHER_CTX_free(ctx);
  38674. printf(resultFmt, passed);
  38675. #endif
  38676. return 0;
  38677. }
  38678. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  38679. {
  38680. #if !defined(NO_DES3) && !defined(NO_RC4)
  38681. byte key[DES3_KEY_SIZE] = {0};
  38682. byte iv[DES_IV_SIZE] = {0};
  38683. EVP_CIPHER_CTX *ctx;
  38684. const EVP_CIPHER *init;
  38685. printf(testingFmt, "wolfSSL_EVP_X_STATE_LEN");
  38686. /* Bad test cases */
  38687. ctx = EVP_CIPHER_CTX_new();
  38688. init = EVP_des_ede3_cbc();
  38689. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38690. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38691. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  38692. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  38693. EVP_CIPHER_CTX_free(ctx);
  38694. /* Good test case */
  38695. ctx = EVP_CIPHER_CTX_new();
  38696. init = wolfSSL_EVP_rc4();
  38697. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38698. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38699. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  38700. EVP_CIPHER_CTX_free(ctx);
  38701. printf(resultFmt, passed);
  38702. #endif
  38703. return 0;
  38704. }
  38705. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  38706. {
  38707. #ifdef HAVE_AES_CBC
  38708. #ifdef WOLFSSL_AES_128
  38709. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  38710. #endif
  38711. #ifdef WOLFSSL_AES_192
  38712. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  38713. #endif
  38714. #ifdef WOLFSSL_AES_256
  38715. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  38716. #endif
  38717. #endif
  38718. #ifdef HAVE_AESGCM
  38719. #ifdef WOLFSSL_AES_128
  38720. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  38721. #endif
  38722. #ifdef WOLFSSL_AES_192
  38723. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  38724. #endif
  38725. #ifdef WOLFSSL_AES_256
  38726. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  38727. #endif
  38728. #endif
  38729. #ifdef WOLFSSL_AES_COUNTER
  38730. #ifdef WOLFSSL_AES_128
  38731. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  38732. #endif
  38733. #ifdef WOLFSSL_AES_192
  38734. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  38735. #endif
  38736. #ifdef WOLFSSL_AES_256
  38737. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  38738. #endif
  38739. #endif
  38740. #ifdef HAVE_AES_ECB
  38741. #ifdef WOLFSSL_AES_128
  38742. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  38743. #endif
  38744. #ifdef WOLFSSL_AES_192
  38745. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  38746. #endif
  38747. #ifdef WOLFSSL_AES_256
  38748. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  38749. #endif
  38750. #endif
  38751. #ifdef WOLFSSL_AES_OFB
  38752. #ifdef WOLFSSL_AES_128
  38753. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  38754. #endif
  38755. #ifdef WOLFSSL_AES_192
  38756. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  38757. #endif
  38758. #ifdef WOLFSSL_AES_256
  38759. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  38760. #endif
  38761. #endif
  38762. #ifndef NO_RC4
  38763. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  38764. #endif
  38765. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  38766. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  38767. #endif
  38768. return 0;
  38769. }
  38770. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  38771. {
  38772. int i, enumlen;
  38773. int enumArray[] = {
  38774. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  38775. #ifdef WOLFSSL_AES_128
  38776. NID_aes_128_cbc,
  38777. #endif
  38778. #ifdef WOLFSSL_AES_192
  38779. NID_aes_192_cbc,
  38780. #endif
  38781. #ifdef WOLFSSL_AES_256
  38782. NID_aes_256_cbc,
  38783. #endif
  38784. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  38785. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38786. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38787. #ifdef HAVE_AESGCM
  38788. #ifdef WOLFSSL_AES_128
  38789. NID_aes_128_gcm,
  38790. #endif
  38791. #ifdef WOLFSSL_AES_192
  38792. NID_aes_192_gcm,
  38793. #endif
  38794. #ifdef WOLFSSL_AES_256
  38795. NID_aes_256_gcm,
  38796. #endif
  38797. #endif /* HAVE_AESGCM */
  38798. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38799. #ifdef WOLFSSL_AES_COUNTER
  38800. #ifdef WOLFSSL_AES_128
  38801. NID_aes_128_ctr,
  38802. #endif
  38803. #ifdef WOLFSSL_AES_192
  38804. NID_aes_192_ctr,
  38805. #endif
  38806. #ifdef WOLFSSL_AES_256
  38807. NID_aes_256_ctr,
  38808. #endif
  38809. #endif
  38810. #ifndef NO_DES3
  38811. NID_des_cbc,
  38812. NID_des_ede3_cbc,
  38813. #endif
  38814. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  38815. NID_chacha20_poly1305,
  38816. #endif
  38817. };
  38818. int iv_lengths[] = {
  38819. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  38820. #ifdef WOLFSSL_AES_128
  38821. AES_BLOCK_SIZE,
  38822. #endif
  38823. #ifdef WOLFSSL_AES_192
  38824. AES_BLOCK_SIZE,
  38825. #endif
  38826. #ifdef WOLFSSL_AES_256
  38827. AES_BLOCK_SIZE,
  38828. #endif
  38829. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  38830. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38831. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38832. #ifdef HAVE_AESGCM
  38833. #ifdef WOLFSSL_AES_128
  38834. GCM_NONCE_MID_SZ,
  38835. #endif
  38836. #ifdef WOLFSSL_AES_192
  38837. GCM_NONCE_MID_SZ,
  38838. #endif
  38839. #ifdef WOLFSSL_AES_256
  38840. GCM_NONCE_MID_SZ,
  38841. #endif
  38842. #endif /* HAVE_AESGCM */
  38843. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38844. #ifdef WOLFSSL_AES_COUNTER
  38845. #ifdef WOLFSSL_AES_128
  38846. AES_BLOCK_SIZE,
  38847. #endif
  38848. #ifdef WOLFSSL_AES_192
  38849. AES_BLOCK_SIZE,
  38850. #endif
  38851. #ifdef WOLFSSL_AES_256
  38852. AES_BLOCK_SIZE,
  38853. #endif
  38854. #endif
  38855. #ifndef NO_DES3
  38856. DES_BLOCK_SIZE,
  38857. DES_BLOCK_SIZE,
  38858. #endif
  38859. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  38860. CHACHA20_POLY1305_AEAD_IV_SIZE,
  38861. #endif
  38862. };
  38863. printf(testingFmt, "wolfSSL_EVP_CIPHER_iv_length");
  38864. enumlen = (sizeof(enumArray)/sizeof(int));
  38865. for(i = 0; i < enumlen; i++)
  38866. {
  38867. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  38868. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  38869. }
  38870. printf(resultFmt, passed);
  38871. return 0;
  38872. }
  38873. static int test_wolfSSL_EVP_SignInit_ex(void)
  38874. {
  38875. WOLFSSL_EVP_MD_CTX mdCtx;
  38876. WOLFSSL_ENGINE* e = 0;
  38877. const EVP_MD* md;
  38878. md = "SHA256";
  38879. printf(testingFmt, "wolfSSL_EVP_SignInit_ex");
  38880. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  38881. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  38882. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  38883. printf(resultFmt, passed);
  38884. return 0;
  38885. }
  38886. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  38887. {
  38888. #if !defined(NO_SHA256)
  38889. WOLFSSL_EVP_MD_CTX mdCtx;
  38890. unsigned int s = 0;
  38891. unsigned char md[WC_SHA256_DIGEST_SIZE];
  38892. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  38893. printf(testingFmt, "wolfSSL_EVP_DigestFinal_ex");
  38894. /* Bad Case */
  38895. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38896. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  38897. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  38898. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  38899. #else
  38900. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  38901. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  38902. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  38903. #endif
  38904. /* Good Case */
  38905. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  38906. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  38907. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  38908. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  38909. printf(resultFmt, passed);
  38910. #endif
  38911. return 0;
  38912. }
  38913. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  38914. {
  38915. #if !defined(NO_DH) && \
  38916. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38917. FILE* f = NULL;
  38918. unsigned char buf[4096];
  38919. const unsigned char* pt = buf;
  38920. const char* params1 = "./certs/dh2048.der";
  38921. long len = 0;
  38922. WOLFSSL_DH* dh = NULL;
  38923. WOLFSSL_EVP_PKEY* pkey;
  38924. XMEMSET(buf, 0, sizeof(buf));
  38925. f = XFOPEN(params1, "rb");
  38926. AssertTrue(f != XBADFILE);
  38927. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  38928. XFCLOSE(f);
  38929. printf(testingFmt, "wolfSSL_EVP_PKEY_assign_DH");
  38930. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38931. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  38932. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38933. /* Bad cases */
  38934. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  38935. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  38936. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  38937. /* Good case */
  38938. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  38939. EVP_PKEY_free(pkey);
  38940. printf(resultFmt, passed);
  38941. #endif
  38942. return 0;
  38943. }
  38944. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  38945. {
  38946. #if defined(OPENSSL_EXTRA)
  38947. EVP_PKEY* pkey;
  38948. EVP_PKEY_CTX* ctx;
  38949. printf(testingFmt, "test_wolfSSL_QT_EVP_PKEY_CTX_free");
  38950. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38951. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38952. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  38953. /* void */
  38954. EVP_PKEY_CTX_free(ctx);
  38955. AssertTrue(1);
  38956. #else
  38957. /* int */
  38958. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  38959. #endif
  38960. EVP_PKEY_free(pkey);
  38961. printf(resultFmt, passed);
  38962. #endif
  38963. return 0;
  38964. }
  38965. static int test_wolfSSL_EVP_PKEY_param_check(void)
  38966. {
  38967. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  38968. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  38969. DH *dh = NULL;
  38970. DH *setDh = NULL;
  38971. EVP_PKEY *pkey = NULL;
  38972. EVP_PKEY_CTX* ctx = NULL;
  38973. FILE* f = NULL;
  38974. unsigned char buf[512];
  38975. const unsigned char* pt = buf;
  38976. const char* dh2048 = "./certs/dh2048.der";
  38977. long len = 0;
  38978. int code = -1;
  38979. printf(testingFmt, "test_wolfSSL_EVP_PKEY_param_check");
  38980. XMEMSET(buf, 0, sizeof(buf));
  38981. f = XFOPEN(dh2048, "rb");
  38982. AssertTrue(f != XBADFILE);
  38983. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  38984. XFCLOSE(f);
  38985. /* Load dh2048.der into DH with internal format */
  38986. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  38987. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  38988. AssertIntEQ(code, 0);
  38989. code = -1;
  38990. pkey = wolfSSL_EVP_PKEY_new();
  38991. /* Set DH into PKEY */
  38992. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  38993. /* create ctx from pkey */
  38994. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38995. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  38996. /* */
  38997. /* TO DO invlaid case */
  38998. /* */
  38999. EVP_PKEY_CTX_free(ctx);
  39000. EVP_PKEY_free(pkey);
  39001. DH_free(setDh);
  39002. DH_free(dh);
  39003. printf(resultFmt, passed);
  39004. #endif
  39005. #endif
  39006. return 0;
  39007. }
  39008. static int test_wolfSSL_EVP_BytesToKey(void)
  39009. {
  39010. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39011. byte key[AES_BLOCK_SIZE] = {0};
  39012. byte iv[AES_BLOCK_SIZE] = {0};
  39013. int sz = 5;
  39014. int count = 0;
  39015. const EVP_MD* md = "SHA256";
  39016. const EVP_CIPHER *type;
  39017. const unsigned char *salt = (unsigned char *)"salt1234";
  39018. const byte data[] = {
  39019. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39020. 0x72,0x6c,0x64
  39021. };
  39022. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  39023. printf(testingFmt, "EVP_BytesToKey");
  39024. /* Bad cases */
  39025. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  39026. 0);
  39027. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  39028. 16);
  39029. md = "2";
  39030. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39031. WOLFSSL_FAILURE);
  39032. /* Good case */
  39033. md = "SHA256";
  39034. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39035. 16);
  39036. printf(resultFmt, passed);
  39037. #endif
  39038. return 0;
  39039. }
  39040. static int test_evp_cipher_aes_gcm(void)
  39041. {
  39042. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  39043. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  39044. (HAVE_FIPS_VERSION >= 2)))
  39045. /*
  39046. * This test checks data at various points in the encrypt/decrypt process
  39047. * against known values produced using the same test with OpenSSL. This
  39048. * interop testing is critical for verifying the correctness of our
  39049. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  39050. * a flow supported by OpenSSL that uses the control command
  39051. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  39052. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  39053. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  39054. * wolfSSL OpenSSH clients because there was a bug in this flow that
  39055. * happened to "cancel out" if both sides of the connection had the bug.
  39056. */
  39057. enum {
  39058. NUM_ENCRYPTIONS = 3,
  39059. AAD_SIZE = 4
  39060. };
  39061. byte plainText1[] = {
  39062. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  39063. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  39064. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  39065. };
  39066. byte plainText2[] = {
  39067. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  39068. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  39069. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  39070. };
  39071. byte plainText3[] = {
  39072. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  39073. 0x5b, 0x57, 0x10
  39074. };
  39075. byte* plainTexts[NUM_ENCRYPTIONS] = {
  39076. plainText1,
  39077. plainText2,
  39078. plainText3
  39079. };
  39080. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  39081. sizeof(plainText1),
  39082. sizeof(plainText2),
  39083. sizeof(plainText3)
  39084. };
  39085. byte aad1[AAD_SIZE] = {
  39086. 0x00, 0x00, 0x00, 0x01
  39087. };
  39088. byte aad2[AAD_SIZE] = {
  39089. 0x00, 0x00, 0x00, 0x10
  39090. };
  39091. byte aad3[AAD_SIZE] = {
  39092. 0x00, 0x00, 0x01, 0x00
  39093. };
  39094. byte* aads[NUM_ENCRYPTIONS] = {
  39095. aad1,
  39096. aad2,
  39097. aad3
  39098. };
  39099. const byte iv[GCM_NONCE_MID_SZ] = {
  39100. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  39101. };
  39102. byte currentIv[GCM_NONCE_MID_SZ];
  39103. const byte key[] = {
  39104. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  39105. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  39106. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  39107. };
  39108. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  39109. {
  39110. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39111. 0xEF
  39112. },
  39113. {
  39114. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39115. 0xF0
  39116. },
  39117. {
  39118. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39119. 0xF1
  39120. }
  39121. };
  39122. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  39123. {
  39124. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  39125. 0x3D, 0x32, 0x18, 0x34, 0xA9
  39126. },
  39127. {
  39128. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  39129. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  39130. },
  39131. {
  39132. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  39133. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  39134. }
  39135. };
  39136. const byte expCipherText1[] = {
  39137. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  39138. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  39139. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  39140. };
  39141. const byte expCipherText2[] = {
  39142. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  39143. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  39144. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  39145. };
  39146. const byte expCipherText3[] = {
  39147. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  39148. 0x80, 0x5E, 0x6B,
  39149. };
  39150. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  39151. expCipherText1,
  39152. expCipherText2,
  39153. expCipherText3
  39154. };
  39155. byte* cipherText;
  39156. byte* calcPlainText;
  39157. byte tag[AES_BLOCK_SIZE];
  39158. EVP_CIPHER_CTX* encCtx = NULL;
  39159. EVP_CIPHER_CTX* decCtx = NULL;
  39160. int i, j, outl;
  39161. printf(testingFmt, "test_evp_cipher_aes_gcm");
  39162. /****************************************************/
  39163. for (i = 0; i < 3; ++i) {
  39164. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  39165. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  39166. /* First iteration, set key before IV. */
  39167. if (i == 0) {
  39168. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  39169. SSL_SUCCESS);
  39170. /*
  39171. * The call to EVP_CipherInit below (with NULL key) should clear the
  39172. * gcmIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  39173. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  39174. * behavior.
  39175. */
  39176. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39177. (void*)iv), SSL_SUCCESS);
  39178. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  39179. SSL_SUCCESS);
  39180. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39181. currentIv), SSL_FAILURE);
  39182. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  39183. SSL_SUCCESS);
  39184. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  39185. SSL_SUCCESS);
  39186. }
  39187. /* Second iteration, IV before key. */
  39188. else {
  39189. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  39190. SSL_SUCCESS);
  39191. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  39192. SSL_SUCCESS);
  39193. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  39194. SSL_SUCCESS);
  39195. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  39196. SSL_SUCCESS);
  39197. }
  39198. /*
  39199. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  39200. * been issued first.
  39201. */
  39202. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39203. currentIv), SSL_FAILURE);
  39204. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39205. (void*)iv), SSL_SUCCESS);
  39206. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39207. (void*)iv), SSL_SUCCESS);
  39208. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  39209. /*************** Encrypt ***************/
  39210. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39211. currentIv), SSL_SUCCESS);
  39212. /* Check current IV against expected. */
  39213. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39214. /* Add AAD. */
  39215. if (i == 2) {
  39216. /* Test streaming API. */
  39217. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  39218. AAD_SIZE), SSL_SUCCESS);
  39219. }
  39220. else {
  39221. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  39222. AAD_SIZE);
  39223. }
  39224. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  39225. DYNAMIC_TYPE_TMP_BUFFER));
  39226. /* Encrypt plaintext. */
  39227. if (i == 2){
  39228. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  39229. plainTexts[j], plainTextSzs[j]),
  39230. SSL_SUCCESS);
  39231. }
  39232. else {
  39233. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  39234. plainTextSzs[j]), plainTextSzs[j]);
  39235. }
  39236. if (i == 2) {
  39237. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  39238. SSL_SUCCESS);
  39239. }
  39240. else {
  39241. /*
  39242. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  39243. * akin to calling EVP_CipherFinal.
  39244. */
  39245. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  39246. }
  39247. /* Check ciphertext against expected. */
  39248. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  39249. 0);
  39250. /* Get and check tag against expected. */
  39251. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  39252. sizeof(tag), tag), SSL_SUCCESS);
  39253. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  39254. /*************** Decrypt ***************/
  39255. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39256. currentIv), SSL_SUCCESS);
  39257. /* Check current IV against expected. */
  39258. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39259. /* Add AAD. */
  39260. if (i == 2) {
  39261. /* Test streaming API. */
  39262. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  39263. AAD_SIZE), SSL_SUCCESS);
  39264. }
  39265. else {
  39266. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  39267. AAD_SIZE);
  39268. }
  39269. /* Set expected tag. */
  39270. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  39271. sizeof(tag), tag), SSL_SUCCESS);
  39272. /* Decrypt ciphertext. */
  39273. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  39274. DYNAMIC_TYPE_TMP_BUFFER));
  39275. if (i == 2){
  39276. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  39277. cipherText, plainTextSzs[j]),
  39278. SSL_SUCCESS);
  39279. }
  39280. else {
  39281. /* This first EVP_Cipher call will check the tag, too. */
  39282. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  39283. plainTextSzs[j]), plainTextSzs[j]);
  39284. }
  39285. if (i == 2) {
  39286. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  39287. SSL_SUCCESS);
  39288. }
  39289. else {
  39290. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  39291. }
  39292. /* Check plaintext against expected. */
  39293. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  39294. 0);
  39295. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39296. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39297. }
  39298. EVP_CIPHER_CTX_free(encCtx);
  39299. EVP_CIPHER_CTX_free(decCtx);
  39300. }
  39301. printf(resultFmt, passed);
  39302. #endif
  39303. return 0;
  39304. }
  39305. static int test_wolfSSL_OBJ_ln(void)
  39306. {
  39307. const int nid_set[] = {
  39308. NID_commonName,
  39309. NID_serialNumber,
  39310. NID_countryName,
  39311. NID_localityName,
  39312. NID_stateOrProvinceName,
  39313. NID_organizationName,
  39314. NID_organizationalUnitName,
  39315. NID_domainComponent,
  39316. NID_businessCategory,
  39317. NID_jurisdictionCountryName,
  39318. NID_jurisdictionStateOrProvinceName,
  39319. NID_emailAddress
  39320. };
  39321. const char* ln_set[] = {
  39322. "commonName",
  39323. "serialNumber",
  39324. "countryName",
  39325. "localityName",
  39326. "stateOrProvinceName",
  39327. "organizationName",
  39328. "organizationalUnitName",
  39329. "domainComponent",
  39330. "businessCategory",
  39331. "jurisdictionCountryName",
  39332. "jurisdictionStateOrProvinceName",
  39333. "emailAddress",
  39334. };
  39335. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  39336. printf(testingFmt, "wolfSSL_OBJ_ln");
  39337. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  39338. #ifdef HAVE_ECC
  39339. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  39340. {
  39341. EC_builtin_curve r[27];
  39342. size_t nCurves = sizeof(r) / sizeof(r[0]);
  39343. nCurves = EC_get_builtin_curves(r,nCurves);
  39344. for (i = 0; i < nCurves; i++) {
  39345. /* skip ECC_CURVE_INVALID */
  39346. if (r[i].nid != ECC_CURVE_INVALID) {
  39347. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  39348. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  39349. }
  39350. }
  39351. }
  39352. #endif
  39353. #endif
  39354. for (i = 0; i < maxIdx; i++) {
  39355. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  39356. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  39357. }
  39358. printf(resultFmt, passed);
  39359. return 0;
  39360. }
  39361. static int test_wolfSSL_OBJ_sn(void)
  39362. {
  39363. int i = 0, maxIdx = 7;
  39364. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  39365. NID_stateOrProvinceName,NID_organizationName,
  39366. NID_organizationalUnitName,NID_emailAddress};
  39367. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  39368. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  39369. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  39370. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  39371. WOLFSSL_EMAIL_ADDR};
  39372. printf(testingFmt, "wolfSSL_OBJ_sn");
  39373. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  39374. for (i = 0; i < maxIdx; i++) {
  39375. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  39376. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  39377. }
  39378. printf(resultFmt, passed);
  39379. return 0;
  39380. }
  39381. #if !defined(NO_BIO)
  39382. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  39383. {
  39384. return lh_strhash(s[3]);
  39385. }
  39386. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  39387. {
  39388. return XSTRCMP(a[3], b[3]);
  39389. }
  39390. #endif
  39391. static int test_wolfSSL_TXT_DB(void)
  39392. {
  39393. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  39394. BIO *bio;
  39395. TXT_DB *db = NULL;
  39396. const int columns = 6;
  39397. const char *fields[6] = {
  39398. "V",
  39399. "320926161116Z",
  39400. "",
  39401. "12BD",
  39402. "unknown",
  39403. "/CN=rsa doe",
  39404. };
  39405. char** fields_copy;
  39406. printf(testingFmt, "wolfSSL_TXT_DB");
  39407. /* Test read */
  39408. AssertNotNull(bio = BIO_new(BIO_s_file()));
  39409. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  39410. AssertNotNull(db = TXT_DB_read(bio, columns));
  39411. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  39412. DYNAMIC_TYPE_OPENSSL));
  39413. XMEMCPY(fields_copy, fields, sizeof(fields));
  39414. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  39415. BIO_free(bio);
  39416. /* Test write */
  39417. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39418. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  39419. BIO_free(bio);
  39420. /* Test index */
  39421. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  39422. (wolf_sk_compare_cb)TXT_DB_cmp), 1);
  39423. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  39424. fields[3] = "12DA";
  39425. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  39426. fields[3] = "FFFF";
  39427. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  39428. fields[3] = "";
  39429. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  39430. TXT_DB_free(db);
  39431. printf(resultFmt, passed);
  39432. #endif
  39433. return 0;
  39434. }
  39435. static int test_wolfSSL_NCONF(void)
  39436. {
  39437. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  39438. const char* confFile = "./tests/NCONF_test.cnf";
  39439. CONF* conf = NULL;
  39440. long eline = 0;
  39441. long num = 0;
  39442. printf(testingFmt, "wolfSSL_NCONF");
  39443. AssertNotNull(conf = NCONF_new(NULL));
  39444. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  39445. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  39446. AssertIntEQ(num, 1234);
  39447. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  39448. AssertIntEQ(num, 4321);
  39449. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  39450. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  39451. "./test-dir/file1");
  39452. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  39453. "./test-dir/file1:./test-dir/file2:./section1:file2");
  39454. NCONF_free(conf);
  39455. printf(resultFmt, passed);
  39456. #endif
  39457. return 0;
  39458. }
  39459. #endif /* OPENSSL_ALL */
  39460. static int test_wolfSSL_EC_KEY_set_group(void)
  39461. {
  39462. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  39463. defined(OPENSSL_EXTRA)
  39464. EC_KEY *key = NULL;
  39465. EC_GROUP *group = NULL;
  39466. const EC_GROUP *group2 = NULL;
  39467. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  39468. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  39469. AssertNotNull(key = EC_KEY_new());
  39470. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  39471. AssertNotNull(group2 = EC_KEY_get0_group(key));
  39472. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  39473. EC_GROUP_free(group);
  39474. EC_KEY_free(key);
  39475. printf(resultFmt, passed);
  39476. #endif
  39477. return 0;
  39478. }
  39479. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  39480. {
  39481. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA)
  39482. BIO* bio;
  39483. EC_KEY* key;
  39484. printf(testingFmt, "test_wolfSSL_EC_KEY_set_conv_form");
  39485. /* Error condition: NULL key. */
  39486. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  39487. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  39488. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  39489. /* Conversion form defaults to uncompressed. */
  39490. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  39491. #ifdef HAVE_COMP_KEY
  39492. /* Explicitly set to compressed. */
  39493. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  39494. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  39495. #endif
  39496. BIO_free(bio);
  39497. EC_KEY_free(key);
  39498. printf(resultFmt, passed);
  39499. #endif
  39500. return 0;
  39501. }
  39502. static int test_wolfSSL_EC_KEY_print_fp(void)
  39503. {
  39504. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  39505. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256 && \
  39506. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  39507. !defined(NO_STDIO_FILESYSTEM)
  39508. EC_KEY* key = NULL;
  39509. printf(testingFmt, "test_wolfSSL_EC_KEY_print_fp");
  39510. /* Bad file pointer. */
  39511. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  39512. /* NULL key. */
  39513. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, NULL, 0), WOLFSSL_FAILURE);
  39514. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  39515. /* Negative indent. */
  39516. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, -1), WOLFSSL_FAILURE);
  39517. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  39518. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  39519. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  39520. wolfSSL_EC_KEY_free(key);
  39521. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  39522. NID_X9_62_prime256v1)));
  39523. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  39524. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  39525. wolfSSL_EC_KEY_free(key);
  39526. printf(resultFmt, passed);
  39527. #endif
  39528. return 0;
  39529. }
  39530. static int test_wolfSSL_X509V3_EXT_get(void) {
  39531. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39532. FILE* f;
  39533. int numOfExt =0;
  39534. int extNid = 0;
  39535. int i = 0;
  39536. WOLFSSL_X509* x509;
  39537. WOLFSSL_X509_EXTENSION* ext;
  39538. const WOLFSSL_v3_ext_method* method;
  39539. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  39540. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39541. fclose(f);
  39542. printf(testingFmt, "wolfSSL_X509V3_EXT_get() return struct and nid test");
  39543. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  39544. for (i = 0; i < numOfExt; i++) {
  39545. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39546. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  39547. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  39548. AssertIntEQ(method->ext_nid, extNid);
  39549. }
  39550. printf(resultFmt, "passed");
  39551. printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
  39552. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  39553. printf(resultFmt, "passed");
  39554. wolfSSL_X509_free(x509);
  39555. #endif
  39556. return 0;
  39557. }
  39558. static int test_wolfSSL_X509V3_EXT_nconf(void)
  39559. {
  39560. #ifdef OPENSSL_ALL
  39561. const char *ext_names[] = {
  39562. "subjectKeyIdentifier",
  39563. "authorityKeyIdentifier",
  39564. "subjectAltName",
  39565. "keyUsage",
  39566. };
  39567. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  39568. int ext_nids[] = {
  39569. NID_subject_key_identifier,
  39570. NID_authority_key_identifier,
  39571. NID_subject_alt_name,
  39572. NID_key_usage,
  39573. };
  39574. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  39575. const char *ext_values[] = {
  39576. "hash",
  39577. "hash",
  39578. "DNS:example.com, IP:127.0.0.1",
  39579. "digitalSignature,keyEncipherment,dataEncipherment",
  39580. };
  39581. size_t i;
  39582. X509_EXTENSION* ext;
  39583. X509* x509 = X509_new();
  39584. printf(testingFmt, "wolfSSL_X509V3_EXT_nconf()");
  39585. for (i = 0; i < ext_names_count; i++) {
  39586. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  39587. AssertNotNull(ext);
  39588. X509_EXTENSION_free(ext);
  39589. }
  39590. for (i = 0; i < ext_nids_count; i++) {
  39591. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  39592. AssertNotNull(ext);
  39593. X509_EXTENSION_free(ext);
  39594. }
  39595. /* Test adding extension to X509 */
  39596. for (i = 0; i < ext_nids_count; i++) {
  39597. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  39598. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  39599. X509_EXTENSION_free(ext);
  39600. }
  39601. X509_free(x509);
  39602. printf(resultFmt, "passed");
  39603. #endif
  39604. return 0;
  39605. }
  39606. static int test_wolfSSL_X509V3_EXT(void) {
  39607. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39608. FILE* f;
  39609. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  39610. char* str;
  39611. unsigned char* data;
  39612. const WOLFSSL_v3_ext_method* method;
  39613. WOLFSSL_X509* x509;
  39614. WOLFSSL_X509_EXTENSION* ext;
  39615. WOLFSSL_X509_EXTENSION* ext2;
  39616. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  39617. WOLFSSL_ASN1_STRING* asn1str;
  39618. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  39619. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  39620. WOLFSSL_BASIC_CONSTRAINTS* bc;
  39621. WOLFSSL_ACCESS_DESCRIPTION* ad;
  39622. WOLFSSL_GENERAL_NAME* gn;
  39623. printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
  39624. /* Check NULL argument */
  39625. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  39626. /* Using OCSP cert with X509V3 extensions */
  39627. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  39628. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39629. fclose(f);
  39630. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  39631. /* Basic Constraints */
  39632. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39633. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39634. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  39635. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  39636. AssertIntEQ(bc->ca, 1);
  39637. AssertNull(bc->pathlen);
  39638. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  39639. i++;
  39640. /* Subject Key Identifier */
  39641. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39642. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39643. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  39644. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  39645. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  39646. asn1str));
  39647. X509_EXTENSION_free(ext2);
  39648. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  39649. AssertNotNull(method->i2s);
  39650. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  39651. wolfSSL_ASN1_STRING_free(asn1str);
  39652. actual = strcmp(str,
  39653. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  39654. AssertIntEQ(actual, 0);
  39655. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39656. i++;
  39657. /* Authority Key Identifier */
  39658. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39659. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39660. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  39661. AssertNotNull(aKeyId =
  39662. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  39663. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  39664. AssertNotNull(asn1str = aKeyId->keyid);
  39665. AssertNotNull(str =
  39666. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  39667. actual = strcmp(str,
  39668. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  39669. AssertIntEQ(actual, 0);
  39670. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39671. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  39672. i++;
  39673. /* Key Usage */
  39674. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39675. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39676. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  39677. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  39678. #if defined(WOLFSSL_QT)
  39679. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  39680. #else
  39681. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  39682. #endif
  39683. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  39684. #ifdef BIG_ENDIAN_ORDER
  39685. actual = data[1];
  39686. #else
  39687. actual = data[0];
  39688. #endif
  39689. AssertIntEQ(actual, expected);
  39690. wolfSSL_ASN1_STRING_free(asn1str);
  39691. #if 1
  39692. i++;
  39693. /* Authority Info Access */
  39694. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39695. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39696. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  39697. AssertNotNull(aia =
  39698. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  39699. #if defined(WOLFSSL_QT)
  39700. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  39701. #else
  39702. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  39703. #endif
  39704. /* URI entry is an ACCESS_DESCRIPTION type */
  39705. #if defined(WOLFSSL_QT)
  39706. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  39707. #else
  39708. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  39709. #endif
  39710. AssertNotNull(adObj = ad->method);
  39711. /* Make sure nid is OCSP */
  39712. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  39713. /* GENERAL_NAME stores URI as an ASN1_STRING */
  39714. AssertNotNull(gn = ad->location);
  39715. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  39716. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  39717. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  39718. #if defined(WOLFSSL_QT)
  39719. str = (char*)ASN1_STRING_get0_data(asn1str);
  39720. #else
  39721. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  39722. #endif
  39723. actual = strcmp(str, "http://127.0.0.1:22220");
  39724. AssertIntEQ(actual, 0);
  39725. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  39726. #else
  39727. (void) aia; (void) ad; (void) adObj; (void) gn;
  39728. #endif
  39729. wolfSSL_X509_free(x509);
  39730. printf(resultFmt, "passed");
  39731. #endif
  39732. return 0;
  39733. }
  39734. static int test_wolfSSL_X509_get_extension_flags(void)
  39735. {
  39736. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  39737. XFILE f;
  39738. X509* x509;
  39739. unsigned int extFlags;
  39740. unsigned int keyUsageFlags;
  39741. unsigned int extKeyUsageFlags;
  39742. printf(testingFmt, "test_wolfSSL_X509_get_extension_flags");
  39743. /* client-int-cert.pem has the following extension flags. */
  39744. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  39745. /* and the following key usage flags. */
  39746. keyUsageFlags = KU_DIGITAL_SIGNATURE
  39747. | KU_NON_REPUDIATION
  39748. | KU_KEY_ENCIPHERMENT;
  39749. /* and the following extended key usage flags. */
  39750. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  39751. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  39752. AssertTrue(f != XBADFILE);
  39753. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  39754. XFCLOSE(f);
  39755. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  39756. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  39757. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  39758. X509_free(x509);
  39759. /* client-cert-ext.pem has the following extension flags. */
  39760. extFlags = EXFLAG_KUSAGE;
  39761. /* and the following key usage flags. */
  39762. keyUsageFlags = KU_DIGITAL_SIGNATURE
  39763. | KU_KEY_CERT_SIGN
  39764. | KU_CRL_SIGN;
  39765. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  39766. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  39767. XFCLOSE(f);
  39768. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  39769. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  39770. X509_free(x509);
  39771. printf(resultFmt, passed);
  39772. #endif /* OPENSSL_ALL */
  39773. return 0;
  39774. }
  39775. static int test_wolfSSL_X509_get_ext(void){
  39776. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39777. int ret = 0;
  39778. FILE* f;
  39779. WOLFSSL_X509* x509;
  39780. WOLFSSL_X509_EXTENSION* foundExtension;
  39781. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  39782. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39783. fclose(f);
  39784. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  39785. printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
  39786. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  39787. printf(resultFmt, "passed");
  39788. printf(testingFmt, "wolfSSL_X509_get_ext() valid x509, idx out of bounds");
  39789. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  39790. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  39791. printf(resultFmt, "passed");
  39792. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, idx out of bounds");
  39793. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  39794. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  39795. printf(resultFmt, "passed");
  39796. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
  39797. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  39798. printf(resultFmt, "passed");
  39799. wolfSSL_X509_free(x509);
  39800. #endif
  39801. return 0;
  39802. }
  39803. static int test_wolfSSL_X509_get_ext_by_NID(void)
  39804. {
  39805. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  39806. int rc;
  39807. FILE* f;
  39808. WOLFSSL_X509* x509;
  39809. ASN1_OBJECT* obj = NULL;
  39810. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  39811. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39812. fclose(f);
  39813. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  39814. AssertIntGE(rc, 0);
  39815. /* Start search from last location (should fail) */
  39816. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  39817. AssertIntGE(rc, -1);
  39818. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  39819. AssertIntGE(rc, -1);
  39820. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  39821. AssertIntEQ(rc, -1);
  39822. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  39823. AssertIntEQ(rc, -1);
  39824. /* NID_ext_key_usage, check also its nid and oid */
  39825. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  39826. AssertIntGT(rc, -1);
  39827. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  39828. AssertIntEQ(obj->nid, NID_ext_key_usage);
  39829. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  39830. wolfSSL_X509_free(x509);
  39831. #endif
  39832. return 0;
  39833. }
  39834. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  39835. {
  39836. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  39837. int rc;
  39838. XFILE f;
  39839. WOLFSSL_X509* x509;
  39840. WOLFSSL_X509_EXTENSION* ext;
  39841. WOLFSSL_ASN1_STRING* sanString;
  39842. byte* sanDer;
  39843. const byte expectedDer[] = {
  39844. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  39845. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  39846. printf(testingFmt, "test_wolfSSL_X509_get_ext_subj_alt_name");
  39847. f = XFOPEN("./certs/server-cert.pem", "rb");
  39848. AssertTrue(f != XBADFILE);
  39849. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  39850. fclose(f);
  39851. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  39852. AssertIntNE(rc, -1);
  39853. AssertNotNull(ext = X509_get_ext(x509, rc));
  39854. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  39855. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  39856. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  39857. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  39858. X509_free(x509);
  39859. printf(resultFmt, passed);
  39860. #endif
  39861. return 0;
  39862. }
  39863. static int test_wolfSSL_X509_EXTENSION_new(void)
  39864. {
  39865. #if defined (OPENSSL_ALL)
  39866. WOLFSSL_X509_EXTENSION* ext;
  39867. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  39868. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  39869. ext->obj->nid = WOLFSSL_SUCCESS;
  39870. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  39871. wolfSSL_X509_EXTENSION_free(ext);
  39872. #endif
  39873. return 0;
  39874. }
  39875. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  39876. {
  39877. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39878. WOLFSSL_X509* x509;
  39879. WOLFSSL_X509_EXTENSION* ext;
  39880. WOLFSSL_ASN1_OBJECT* o;
  39881. FILE* file;
  39882. int nid = 0;
  39883. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  39884. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  39885. fclose(file);
  39886. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() testing ext idx 0");
  39887. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  39888. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  39889. AssertIntEQ(o->nid, 128);
  39890. nid = o->nid;
  39891. printf(resultFmt, nid == 128 ? passed : failed);
  39892. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
  39893. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  39894. printf(resultFmt, passed);
  39895. wolfSSL_X509_free(x509);
  39896. #endif
  39897. return 0;
  39898. }
  39899. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  39900. {
  39901. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39902. WOLFSSL_X509* x509;
  39903. WOLFSSL_X509_EXTENSION* ext;
  39904. WOLFSSL_ASN1_STRING* str;
  39905. FILE* file;
  39906. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
  39907. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  39908. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  39909. fclose(file);
  39910. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  39911. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  39912. printf(resultFmt, passed);
  39913. wolfSSL_X509_free(x509);
  39914. #endif
  39915. return 0;
  39916. }
  39917. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  39918. {
  39919. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39920. WOLFSSL_X509* x509;
  39921. WOLFSSL_X509_EXTENSION* ext;
  39922. FILE* file;
  39923. int crit;
  39924. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
  39925. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  39926. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  39927. fclose(file);
  39928. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  39929. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  39930. AssertIntEQ(crit, 0);
  39931. printf(resultFmt, passed);
  39932. wolfSSL_X509_free(x509);
  39933. #endif
  39934. return 0;
  39935. }
  39936. static int test_wolfSSL_X509V3_EXT_print(void)
  39937. {
  39938. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  39939. !defined(NO_RSA)
  39940. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  39941. {
  39942. FILE* f;
  39943. WOLFSSL_X509* x509;
  39944. X509_EXTENSION * ext = NULL;
  39945. int loc;
  39946. BIO *bio = NULL;
  39947. AssertNotNull(f = fopen(svrCertFile, "rb"));
  39948. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39949. fclose(f);
  39950. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  39951. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  39952. AssertIntGT(loc, -1);
  39953. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  39954. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  39955. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  39956. AssertIntGT(loc, -1);
  39957. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  39958. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  39959. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  39960. AssertIntGT(loc, -1);
  39961. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  39962. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  39963. wolfSSL_BIO_free(bio);
  39964. wolfSSL_X509_free(x509);
  39965. }
  39966. {
  39967. X509 *x509;
  39968. BIO *bio;
  39969. X509_EXTENSION *ext;
  39970. unsigned int i;
  39971. unsigned int idx;
  39972. /* Some NIDs to test with */
  39973. int nids[] = {
  39974. /* NID_key_usage, currently X509_get_ext returns this as a bit
  39975. * string, which messes up X509V3_EXT_print */
  39976. /* NID_ext_key_usage, */
  39977. NID_subject_alt_name,
  39978. };
  39979. int* n;
  39980. AssertNotNull(bio = BIO_new_fp(stdout, BIO_NOCLOSE));
  39981. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  39982. WOLFSSL_FILETYPE_PEM));
  39983. printf("\nPrinting extension values:\n");
  39984. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  39985. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  39986. * update the index */
  39987. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  39988. AssertNotNull(ext = X509_get_ext(x509, idx));
  39989. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  39990. printf("\n");
  39991. }
  39992. BIO_free(bio);
  39993. X509_free(x509);
  39994. }
  39995. printf(resultFmt, passed);
  39996. #endif
  39997. return 0;
  39998. }
  39999. static int test_wolfSSL_X509_cmp(void)
  40000. {
  40001. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40002. FILE* file1;
  40003. FILE* file2;
  40004. WOLFSSL_X509* cert1;
  40005. WOLFSSL_X509* cert2;
  40006. int ret = 0;
  40007. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  40008. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  40009. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  40010. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  40011. fclose(file1);
  40012. fclose(file2);
  40013. printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
  40014. ret = wolfSSL_X509_cmp(cert1, cert1);
  40015. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  40016. printf(resultFmt, ret == 0 ? passed : failed);
  40017. fflush(stdout);
  40018. printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
  40019. ret = wolfSSL_X509_cmp(cert1, cert2);
  40020. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  40021. printf(resultFmt, ret == -1 ? passed : failed);
  40022. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
  40023. ret = wolfSSL_X509_cmp(NULL, cert2);
  40024. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  40025. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  40026. printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
  40027. ret = wolfSSL_X509_cmp(cert1, NULL);
  40028. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  40029. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  40030. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
  40031. ret = wolfSSL_X509_cmp(NULL, NULL);
  40032. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  40033. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  40034. wolfSSL_X509_free(cert1);
  40035. wolfSSL_X509_free(cert2);
  40036. #endif
  40037. return 0;
  40038. }
  40039. static int test_wolfSSL_PKEY_up_ref(void)
  40040. {
  40041. #if defined(OPENSSL_ALL)
  40042. EVP_PKEY* pkey;
  40043. printf(testingFmt, "wolfSSL_PKEY_up_ref()");
  40044. pkey = EVP_PKEY_new();
  40045. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  40046. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40047. EVP_PKEY_free(pkey);
  40048. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40049. EVP_PKEY_free(pkey);
  40050. EVP_PKEY_free(pkey);
  40051. printf(resultFmt, "passed");
  40052. #endif
  40053. return 0;
  40054. }
  40055. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  40056. {
  40057. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  40058. EVP_PKEY* pkey;
  40059. const unsigned char* p;
  40060. unsigned char* der = NULL;
  40061. int derLen;
  40062. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_PublicKey()");
  40063. p = client_keypub_der_2048;
  40064. /* Check that key can be successfully decoded. */
  40065. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  40066. sizeof_client_keypub_der_2048));
  40067. /* Check that key can be successfully encoded. */
  40068. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  40069. /* Ensure that the encoded version matches the original. */
  40070. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  40071. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  40072. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40073. EVP_PKEY_free(pkey);
  40074. printf(resultFmt, passed);
  40075. #endif
  40076. return 0;
  40077. }
  40078. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  40079. {
  40080. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  40081. DSA* dsa;
  40082. char file[] = "./certs/dsaparams.der";
  40083. XFILE f;
  40084. int derInLen;
  40085. byte* derIn;
  40086. int derOutLen;
  40087. byte* derOut = NULL;
  40088. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_DSAparams()");
  40089. f = XFOPEN(file, "rb");
  40090. AssertTrue(f != XBADFILE);
  40091. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  40092. derInLen = (int)XFTELL(f);
  40093. XREWIND(f);
  40094. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  40095. DYNAMIC_TYPE_TMP_BUFFER));
  40096. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  40097. XFCLOSE(f);
  40098. /* Check that params can be successfully decoded. */
  40099. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  40100. /* Check that params can be successfully encoded. */
  40101. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  40102. /* Ensure that the encoded version matches the original. */
  40103. AssertIntEQ(derInLen, derOutLen);
  40104. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  40105. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40106. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40107. DSA_free(dsa);
  40108. printf(resultFmt, passed);
  40109. #endif
  40110. return 0;
  40111. }
  40112. static int test_wolfSSL_i2d_PrivateKey(void)
  40113. {
  40114. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  40115. printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
  40116. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  40117. {
  40118. EVP_PKEY* pkey;
  40119. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  40120. unsigned char buf[FOURK_BUF];
  40121. unsigned char* pt = NULL;
  40122. int bufSz;
  40123. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  40124. (long)sizeof_server_key_der_2048));
  40125. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  40126. pt = buf;
  40127. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  40128. AssertIntNE((pt - buf), 0);
  40129. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  40130. EVP_PKEY_free(pkey);
  40131. }
  40132. #endif
  40133. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  40134. {
  40135. EVP_PKEY* pkey;
  40136. const unsigned char* client_key =
  40137. (const unsigned char*)ecc_clikey_der_256;
  40138. unsigned char buf[FOURK_BUF];
  40139. unsigned char* pt = NULL;
  40140. int bufSz;
  40141. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  40142. sizeof_ecc_clikey_der_256)));
  40143. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  40144. pt = buf;
  40145. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  40146. AssertIntNE((pt - buf), 0);
  40147. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  40148. EVP_PKEY_free(pkey);
  40149. }
  40150. #endif
  40151. printf(resultFmt, "passed");
  40152. #endif
  40153. return 0;
  40154. }
  40155. static int test_wolfSSL_OCSP_id_get0_info(void)
  40156. {
  40157. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  40158. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40159. X509* cert;
  40160. X509* issuer;
  40161. OCSP_CERTID* id;
  40162. OCSP_CERTID* id2;
  40163. ASN1_STRING* name = NULL;
  40164. ASN1_OBJECT* pmd = NULL;
  40165. ASN1_STRING* keyHash = NULL;
  40166. ASN1_INTEGER* serial = NULL;
  40167. ASN1_INTEGER* x509Int;
  40168. printf(testingFmt, "wolfSSL_OCSP_id_get0_info()");
  40169. AssertNotNull(cert =
  40170. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  40171. AssertNotNull(issuer =
  40172. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  40173. id = OCSP_cert_to_id(NULL, cert, issuer);
  40174. AssertNotNull(id);
  40175. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  40176. AssertNotNull(id2);
  40177. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  40178. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  40179. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  40180. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  40181. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  40182. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  40183. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  40184. AssertNotNull(serial);
  40185. /* compare serial number to one in cert, should be equal */
  40186. x509Int = X509_get_serialNumber(cert);
  40187. AssertNotNull(x509Int);
  40188. AssertIntEQ(x509Int->length, serial->length);
  40189. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  40190. /* test OCSP_id_cmp */
  40191. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  40192. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  40193. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  40194. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  40195. id->issuerHash[0] = ~id->issuerHash[0];
  40196. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  40197. OCSP_CERTID_free(id);
  40198. OCSP_CERTID_free(id2);
  40199. X509_free(cert); /* free's x509Int */
  40200. X509_free(issuer);
  40201. printf(resultFmt, "passed");
  40202. #endif
  40203. return 0;
  40204. }
  40205. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  40206. {
  40207. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  40208. WOLFSSL_OCSP_CERTID certId;
  40209. byte* targetBuffer;
  40210. byte* beginTargetBuffer;
  40211. /* OCSP CertID bytes taken from PCAP */
  40212. byte rawCertId[] = {
  40213. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  40214. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  40215. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  40216. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  40217. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  40218. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  40219. 0xcf, 0xbc, 0x91
  40220. };
  40221. int ret, i;
  40222. printf(testingFmt, "wolfSSL_i2d_OCSP_CERTID()");
  40223. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  40224. certId.rawCertId = rawCertId;
  40225. certId.rawCertIdSize = sizeof(rawCertId);
  40226. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40227. beginTargetBuffer = targetBuffer;
  40228. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40229. /* If target buffer is not null, function increments targetBuffer to point
  40230. just past the end of the encoded data. */
  40231. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  40232. /* Function returns the size of the encoded data. */
  40233. AssertIntEQ(ret, sizeof(rawCertId));
  40234. for (i = 0; i < ret; ++i)
  40235. {
  40236. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  40237. }
  40238. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40239. targetBuffer = NULL;
  40240. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40241. /* If target buffer is null, function allocates memory for a buffer and
  40242. copies the encoded data into it. targetBuffer then points to the start of
  40243. this newly allocate buffer. */
  40244. AssertIntEQ(ret, sizeof(rawCertId));
  40245. for (i = 0; i < ret; ++i)
  40246. {
  40247. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  40248. }
  40249. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  40250. printf(resultFmt, passed);
  40251. #endif
  40252. return 0;
  40253. }
  40254. static int test_wolfSSL_OCSP_id_cmp(void)
  40255. {
  40256. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40257. OCSP_CERTID id1;
  40258. OCSP_CERTID id2;
  40259. printf(testingFmt, "wolfSSL_OCSP_id_cmp()");
  40260. XMEMSET(&id1, 0, sizeof(id1));
  40261. XMEMSET(&id2, 0, sizeof(id2));
  40262. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  40263. printf(resultFmt, passed);
  40264. #endif
  40265. return 0;
  40266. }
  40267. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  40268. {
  40269. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40270. WOLFSSL_OCSP_SINGLERESP single;
  40271. const WOLFSSL_OCSP_CERTID* certId;
  40272. XMEMSET(&single, 0, sizeof(single));
  40273. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  40274. printf(testingFmt, "wolfSSL_OCSP_SINGLERESP_get0_id()");
  40275. AssertPtrEq(&single, certId);
  40276. printf(resultFmt, passed);
  40277. #endif
  40278. return 0;
  40279. }
  40280. static int test_wolfSSL_OCSP_single_get0_status(void)
  40281. {
  40282. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40283. WOLFSSL_OCSP_SINGLERESP single;
  40284. CertStatus certStatus;
  40285. WOLFSSL_ASN1_TIME* thisDate;
  40286. WOLFSSL_ASN1_TIME* nextDate;
  40287. int ret, i;
  40288. printf(testingFmt, "wolfSSL_OCSP_single_get0_status()");
  40289. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40290. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  40291. /* Fill the date fields with some dummy data. */
  40292. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  40293. certStatus.thisDateParsed.data[i] = i;
  40294. certStatus.nextDateParsed.data[i] = i;
  40295. }
  40296. certStatus.status = CERT_GOOD;
  40297. single.status = &certStatus;
  40298. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  40299. &nextDate);
  40300. AssertIntEQ(ret, CERT_GOOD);
  40301. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  40302. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  40303. printf(resultFmt, passed);
  40304. #endif
  40305. return 0;
  40306. }
  40307. static int test_wolfSSL_OCSP_resp_count(void)
  40308. {
  40309. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40310. WOLFSSL_OCSP_BASICRESP basicResp;
  40311. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  40312. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  40313. int count;
  40314. printf(testingFmt, "wolfSSL_OCSP_resp_count()");
  40315. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  40316. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40317. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40318. count = wolfSSL_OCSP_resp_count(&basicResp);
  40319. AssertIntEQ(count, 0);
  40320. basicResp.single = &singleRespOne;
  40321. count = wolfSSL_OCSP_resp_count(&basicResp);
  40322. AssertIntEQ(count, 1);
  40323. singleRespOne.next = &singleRespTwo;
  40324. count = wolfSSL_OCSP_resp_count(&basicResp);
  40325. AssertIntEQ(count, 2);
  40326. printf(resultFmt, passed);
  40327. #endif
  40328. return 0;
  40329. }
  40330. static int test_wolfSSL_OCSP_resp_get0(void)
  40331. {
  40332. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40333. WOLFSSL_OCSP_BASICRESP basicResp;
  40334. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  40335. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  40336. WOLFSSL_OCSP_SINGLERESP* ret;
  40337. printf(testingFmt, "wolfSSL_OCSP_resp_get0()");
  40338. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  40339. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40340. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40341. basicResp.single = &singleRespOne;
  40342. singleRespOne.next = &singleRespTwo;
  40343. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  40344. AssertPtrEq(ret, &singleRespOne);
  40345. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  40346. AssertPtrEq(ret, &singleRespTwo);
  40347. printf(resultFmt, passed);
  40348. #endif
  40349. return 0;
  40350. }
  40351. static int test_wolfSSL_EVP_PKEY_derive(void)
  40352. {
  40353. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  40354. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  40355. EVP_PKEY_CTX *ctx;
  40356. unsigned char *skey;
  40357. size_t skeylen;
  40358. EVP_PKEY *pkey, *peerkey;
  40359. const unsigned char* key;
  40360. printf(testingFmt, "wolfSSL_EVP_PKEY_derive()");
  40361. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  40362. /* DH */
  40363. key = dh_key_der_2048;
  40364. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  40365. sizeof_dh_key_der_2048)));
  40366. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  40367. key = dh_key_der_2048;
  40368. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  40369. sizeof_dh_key_der_2048)));
  40370. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  40371. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40372. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  40373. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  40374. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  40375. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  40376. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  40377. EVP_PKEY_CTX_free(ctx);
  40378. EVP_PKEY_free(peerkey);
  40379. EVP_PKEY_free(pkey);
  40380. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  40381. #endif
  40382. #ifdef HAVE_ECC
  40383. /* ECDH */
  40384. key = ecc_clikey_der_256;
  40385. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  40386. sizeof_ecc_clikey_der_256)));
  40387. key = ecc_clikeypub_der_256;
  40388. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  40389. sizeof_ecc_clikeypub_der_256)));
  40390. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40391. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  40392. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  40393. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  40394. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  40395. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  40396. EVP_PKEY_CTX_free(ctx);
  40397. EVP_PKEY_free(peerkey);
  40398. EVP_PKEY_free(pkey);
  40399. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  40400. #endif /* HAVE_ECC */
  40401. printf(resultFmt, "passed");
  40402. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  40403. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  40404. return 0;
  40405. }
  40406. static int test_wolfSSL_EVP_PBE_scrypt(void)
  40407. {
  40408. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  40409. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  40410. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  40411. int ret;
  40412. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  40413. int pwdlen = sizeof(pwd);
  40414. const byte salt[] = {'N','a','C','l'};
  40415. int saltlen = sizeof(salt);
  40416. byte key[80];
  40417. word64 numOvr32 = (word64)INT32_MAX + 1;
  40418. /* expected derived key for N:16, r:1, p:1 */
  40419. const byte expectedKey[] = {
  40420. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  40421. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  40422. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  40423. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  40424. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  40425. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  40426. 0xE7, 0xE9, 0xC0, 0x9A};
  40427. printf(testingFmt, "wolfSSL_EVP_PBE_scrypt()");
  40428. /* N r p mx key keylen */
  40429. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  40430. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  40431. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  40432. AssertIntEQ(ret, 0); /* N must be power of 2 */
  40433. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  40434. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  40435. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  40436. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  40437. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  40438. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  40439. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  40440. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  40441. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  40442. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  40443. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  40444. AssertIntEQ(ret, 1); /* should succeed */
  40445. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  40446. key, 64);
  40447. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  40448. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  40449. key, 64);
  40450. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  40451. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  40452. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  40453. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  40454. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  40455. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  40456. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  40457. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  40458. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  40459. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  40460. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  40461. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  40462. AssertIntEQ(ret, 1);
  40463. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  40464. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  40465. printf(resultFmt, "passed");
  40466. #endif /* !NO_PWDBASED && !NO_SHA256 */
  40467. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  40468. return 0;
  40469. }
  40470. static int test_wolfSSL_EC_get_builtin_curves(void)
  40471. {
  40472. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  40473. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40474. EC_builtin_curve* curves = NULL;
  40475. size_t crv_len = 0;
  40476. size_t i = 0;
  40477. printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
  40478. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  40479. AssertNotNull(curves = (EC_builtin_curve*)
  40480. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  40481. DYNAMIC_TYPE_TMP_BUFFER));
  40482. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  40483. for (i = 0; i < crv_len; i++)
  40484. {
  40485. if (curves[i].comment != NULL)
  40486. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  40487. }
  40488. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40489. printf(resultFmt, passed);
  40490. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  40491. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  40492. return 0;
  40493. }
  40494. static int test_no_op_functions(void)
  40495. {
  40496. #if defined(OPENSSL_EXTRA)
  40497. printf(testingFmt, "no_op_functions()");
  40498. /* this makes sure wolfSSL can compile and run these no-op functions */
  40499. SSL_load_error_strings();
  40500. ENGINE_load_builtin_engines();
  40501. OpenSSL_add_all_ciphers();
  40502. AssertIntEQ(CRYPTO_malloc_init(), 0);
  40503. printf(resultFmt, passed);
  40504. #endif
  40505. return 0;
  40506. }
  40507. static int test_wolfSSL_CRYPTO_memcmp(void)
  40508. {
  40509. #ifdef OPENSSL_EXTRA
  40510. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  40511. "implementation of TLS/SSL for embedded devices to the cloud.";
  40512. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  40513. "implementation of TLS/SSL for embedded devices to the cloud.";
  40514. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  40515. "implementation of TLS/SSL for embedded devices to the cloud!";
  40516. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  40517. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  40518. #endif
  40519. return 0;
  40520. }
  40521. /*----------------------------------------------------------------------------*
  40522. | wolfCrypt ASN
  40523. *----------------------------------------------------------------------------*/
  40524. static int test_wc_CreateEncryptedPKCS8Key(void)
  40525. {
  40526. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  40527. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  40528. WC_RNG rng;
  40529. byte* encKey = NULL;
  40530. word32 encKeySz = 0;
  40531. word32 decKeySz = 0;
  40532. const char password[] = "Lorem ipsum dolor sit amet";
  40533. word32 passwordSz = (word32)XSTRLEN(password);
  40534. word32 tradIdx = 0;
  40535. printf(testingFmt, "test_wc_CreateEncryptedPKCS8Key");
  40536. AssertIntEQ(wc_InitRng(&rng), 0);
  40537. /* Call with NULL for out buffer to get necessary length. */
  40538. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  40539. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  40540. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  40541. LENGTH_ONLY_E);
  40542. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  40543. DYNAMIC_TYPE_TMP_BUFFER));
  40544. /* Call with the allocated out buffer. */
  40545. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  40546. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  40547. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  40548. 0);
  40549. /* Decrypt the encrypted PKCS8 key we just made. */
  40550. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  40551. passwordSz)), 0);
  40552. /* encKey now holds the decrypted key (decrypted in place). */
  40553. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  40554. /* Check that the decrypted key matches the key prior to encryption. */
  40555. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  40556. sizeof_server_key_der_2048), 0);
  40557. if (encKey != NULL)
  40558. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40559. wc_FreeRng(&rng);
  40560. printf(resultFmt, passed);
  40561. #endif
  40562. return 0;
  40563. }
  40564. static int test_wc_GetPkcs8TraditionalOffset(void)
  40565. {
  40566. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  40567. int length, derSz;
  40568. word32 inOutIdx;
  40569. const char* path = "./certs/server-keyPkcs8.der";
  40570. XFILE file;
  40571. byte der[2048];
  40572. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  40573. file = XFOPEN(path, "rb");
  40574. AssertTrue(file != XBADFILE);
  40575. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  40576. XFCLOSE(file);
  40577. /* valid case */
  40578. inOutIdx = 0;
  40579. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  40580. AssertIntGT(length, 0);
  40581. /* inOutIdx > sz */
  40582. inOutIdx = 4000;
  40583. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  40584. AssertIntEQ(length, BAD_FUNC_ARG);
  40585. /* null input */
  40586. inOutIdx = 0;
  40587. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  40588. AssertIntEQ(length, BAD_FUNC_ARG);
  40589. /* invalid input, fill buffer with 1's */
  40590. XMEMSET(der, 1, sizeof(der));
  40591. inOutIdx = 0;
  40592. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  40593. AssertIntEQ(length, ASN_PARSE_E);
  40594. printf(resultFmt, passed);
  40595. #endif /* NO_ASN */
  40596. return 0;
  40597. }
  40598. static int test_wc_SetSubjectRaw(void)
  40599. {
  40600. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40601. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  40602. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  40603. WOLFSSL_X509* x509;
  40604. int peerCertSz;
  40605. const byte* peerCertBuf;
  40606. Cert forgedCert;
  40607. printf(testingFmt, "test_wc_SetSubjectRaw()");
  40608. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  40609. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  40610. AssertIntEQ(0, wc_InitCert(&forgedCert));
  40611. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  40612. wolfSSL_FreeX509(x509);
  40613. printf(resultFmt, passed);
  40614. #endif
  40615. return 0;
  40616. }
  40617. static int test_wc_GetSubjectRaw(void)
  40618. {
  40619. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40620. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  40621. Cert cert;
  40622. byte *subjectRaw;
  40623. printf(testingFmt, "test_wc_GetSubjectRaw()");
  40624. AssertIntEQ(0, wc_InitCert(&cert));
  40625. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  40626. printf(resultFmt, passed);
  40627. #endif
  40628. return 0;
  40629. }
  40630. static int test_wc_SetIssuerRaw(void)
  40631. {
  40632. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40633. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  40634. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  40635. WOLFSSL_X509* x509;
  40636. int peerCertSz;
  40637. const byte* peerCertBuf;
  40638. Cert forgedCert;
  40639. printf(testingFmt, "test_wc_SetIssuerRaw()");
  40640. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  40641. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  40642. AssertIntEQ(0, wc_InitCert(&forgedCert));
  40643. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  40644. wolfSSL_FreeX509(x509);
  40645. printf(resultFmt, passed);
  40646. #endif
  40647. return 0;
  40648. }
  40649. static int test_wc_SetIssueBuffer(void)
  40650. {
  40651. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40652. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  40653. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  40654. WOLFSSL_X509* x509;
  40655. int peerCertSz;
  40656. const byte* peerCertBuf;
  40657. Cert forgedCert;
  40658. printf(testingFmt, "test_wc_SetIssuerBuffer()");
  40659. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  40660. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  40661. AssertIntEQ(0, wc_InitCert(&forgedCert));
  40662. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  40663. wolfSSL_FreeX509(x509);
  40664. printf(resultFmt, passed);
  40665. #endif
  40666. return 0;
  40667. }
  40668. /*
  40669. * Testing wc_SetSubjectKeyId
  40670. */
  40671. static int test_wc_SetSubjectKeyId(void)
  40672. {
  40673. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40674. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  40675. Cert cert;
  40676. const char* file = "certs/ecc-client-keyPub.pem";
  40677. printf(testingFmt, "wc_SetSubjectKeyId()");
  40678. AssertIntEQ(0, wc_InitCert(&cert));
  40679. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  40680. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  40681. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  40682. printf(resultFmt, passed);
  40683. #endif
  40684. return 0;
  40685. } /* END test_wc_SetSubjectKeyId */
  40686. /*
  40687. * Testing wc_SetSubject
  40688. */
  40689. static int test_wc_SetSubject(void)
  40690. {
  40691. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40692. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  40693. Cert cert;
  40694. const char* file = "./certs/ca-ecc-cert.pem";
  40695. printf(testingFmt, "wc_SetSubject()");
  40696. AssertIntEQ(0, wc_InitCert(&cert));
  40697. AssertIntEQ(0, wc_SetSubject(&cert, file));
  40698. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  40699. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  40700. printf(resultFmt, passed);
  40701. #endif
  40702. return 0;
  40703. } /* END test_wc_SetSubject */
  40704. static int test_CheckCertSignature(void)
  40705. {
  40706. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  40707. WOLFSSL_CERT_MANAGER* cm = NULL;
  40708. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  40709. FILE* fp;
  40710. byte cert[4096];
  40711. int certSz;
  40712. #endif
  40713. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  40714. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  40715. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  40716. #ifndef NO_RSA
  40717. #ifdef USE_CERT_BUFFERS_1024
  40718. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  40719. sizeof_server_cert_der_1024, NULL, cm));
  40720. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  40721. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  40722. WOLFSSL_FILETYPE_ASN1));
  40723. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  40724. sizeof_server_cert_der_1024, NULL, cm));
  40725. #elif defined(USE_CERT_BUFFERS_2048)
  40726. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  40727. sizeof_server_cert_der_2048, NULL, cm));
  40728. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  40729. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  40730. WOLFSSL_FILETYPE_ASN1));
  40731. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  40732. sizeof_server_cert_der_2048, NULL, cm));
  40733. #endif
  40734. #endif
  40735. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  40736. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  40737. sizeof_serv_ecc_der_256, NULL, cm));
  40738. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  40739. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  40740. WOLFSSL_FILETYPE_ASN1));
  40741. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  40742. NULL, cm));
  40743. #endif
  40744. #if !defined(NO_FILESYSTEM)
  40745. wolfSSL_CertManagerFree(cm);
  40746. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  40747. #ifndef NO_RSA
  40748. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  40749. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  40750. XFCLOSE(fp);
  40751. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  40752. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  40753. "./certs/ca-cert.pem", NULL));
  40754. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  40755. #endif
  40756. #ifdef HAVE_ECC
  40757. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  40758. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  40759. XFCLOSE(fp);
  40760. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  40761. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  40762. "./certs/ca-ecc-cert.pem", NULL));
  40763. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  40764. #endif
  40765. #endif
  40766. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  40767. (void)fp;
  40768. (void)cert;
  40769. (void)certSz;
  40770. #endif
  40771. wolfSSL_CertManagerFree(cm);
  40772. #endif
  40773. return 0;
  40774. }
  40775. static int test_wc_ParseCert(void)
  40776. {
  40777. #if !defined(NO_CERTS) && !defined(NO_RSA)
  40778. DecodedCert decodedCert;
  40779. const byte* rawCert = client_cert_der_2048;
  40780. const int rawCertSize = sizeof_client_cert_der_2048;
  40781. printf(testingFmt, "wc_ParseCert");
  40782. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  40783. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  40784. #ifndef IGNORE_NAME_CONSTRAINTS
  40785. /* check that the subjects emailAddress was not put in the alt name list */
  40786. AssertNotNull(decodedCert.subjectEmail);
  40787. AssertNull(decodedCert.altEmailNames);
  40788. #endif
  40789. wc_FreeDecodedCert(&decodedCert);
  40790. printf(resultFmt, passed);
  40791. #endif
  40792. return 0;
  40793. }
  40794. static int test_MakeCertWithPathLen(void)
  40795. {
  40796. #if defined(WOLFSSL_CERT_REQ) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  40797. const byte expectedPathLen = 7;
  40798. Cert cert;
  40799. DecodedCert decodedCert;
  40800. byte der[FOURK_BUF];
  40801. int derSize = 0;
  40802. WC_RNG rng;
  40803. ecc_key key;
  40804. printf(testingFmt, "test_MakeCertWithPathLen");
  40805. AssertIntEQ(wc_InitRng(&rng), 0);
  40806. AssertIntEQ(wc_ecc_init(&key), 0);
  40807. AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  40808. AssertIntEQ(wc_InitCert(&cert), 0);
  40809. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  40810. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  40811. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  40812. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  40813. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  40814. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
  40815. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
  40816. cert.selfSigned = 1;
  40817. cert.isCA = 1;
  40818. cert.pathLen = expectedPathLen;
  40819. cert.pathLenSet = 1;
  40820. cert.sigType = CTC_SHA256wECDSA;
  40821. #ifdef WOLFSSL_CERT_EXT
  40822. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  40823. #endif
  40824. AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  40825. derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
  40826. &key, &rng);
  40827. AssertIntGE(derSize, 0);
  40828. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  40829. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  40830. AssertIntEQ(decodedCert.pathLength, expectedPathLen);
  40831. wc_FreeDecodedCert(&decodedCert);
  40832. AssertIntEQ(wc_ecc_free(&key), 0);
  40833. AssertIntEQ(wc_FreeRng(&rng), 0);
  40834. printf(resultFmt, passed);
  40835. #endif
  40836. return 0;
  40837. }
  40838. /*----------------------------------------------------------------------------*
  40839. | wolfCrypt ECC
  40840. *----------------------------------------------------------------------------*/
  40841. static int test_wc_ecc_get_curve_size_from_name(void)
  40842. {
  40843. #ifdef HAVE_ECC
  40844. int ret;
  40845. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  40846. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40847. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  40848. AssertIntEQ(ret, 32);
  40849. #endif
  40850. /* invalid case */
  40851. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  40852. AssertIntEQ(ret, -1);
  40853. /* NULL input */
  40854. ret = wc_ecc_get_curve_size_from_name(NULL);
  40855. AssertIntEQ(ret, BAD_FUNC_ARG);
  40856. printf(resultFmt, passed);
  40857. #endif /* HAVE_ECC */
  40858. return 0;
  40859. }
  40860. static int test_wc_ecc_get_curve_id_from_name(void)
  40861. {
  40862. #ifdef HAVE_ECC
  40863. int id;
  40864. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  40865. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40866. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  40867. AssertIntEQ(id, ECC_SECP256R1);
  40868. #endif
  40869. /* invalid case */
  40870. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  40871. AssertIntEQ(id, -1);
  40872. /* NULL input */
  40873. id = wc_ecc_get_curve_id_from_name(NULL);
  40874. AssertIntEQ(id, BAD_FUNC_ARG);
  40875. printf(resultFmt, passed);
  40876. #endif /* HAVE_ECC */
  40877. return 0;
  40878. }
  40879. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  40880. !defined(HAVE_SELFTEST) && \
  40881. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  40882. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  40883. {
  40884. int id;
  40885. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40886. int curve_id;
  40887. ecc_key* key;
  40888. const ecc_set_type* params;
  40889. int ret;
  40890. #endif
  40891. WOLFSSL_EC_KEY *ecKey = NULL;
  40892. printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
  40893. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40894. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  40895. AssertIntEQ(id, ECC_SECP256R1);
  40896. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  40897. AssertNotNull(ecKey);
  40898. ret = EC_KEY_generate_key(ecKey);
  40899. if (ret == 0) {
  40900. /* normal test */
  40901. key = (ecc_key*)ecKey->internal;
  40902. params = key->dp;
  40903. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  40904. AssertIntEQ(curve_id, id);
  40905. }
  40906. #endif
  40907. /* invalid case, NULL input*/
  40908. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  40909. AssertIntEQ(id, BAD_FUNC_ARG);
  40910. wolfSSL_EC_KEY_free(ecKey);
  40911. printf(resultFmt, passed);
  40912. return 0;
  40913. }
  40914. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  40915. static int test_wc_ecc_get_curve_id_from_params(void)
  40916. {
  40917. #ifdef HAVE_ECC
  40918. int id;
  40919. const byte prime[] =
  40920. {
  40921. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  40922. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40923. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  40924. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  40925. };
  40926. const byte primeInvalid[] =
  40927. {
  40928. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  40929. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40930. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  40931. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  40932. };
  40933. const byte Af[] =
  40934. {
  40935. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  40936. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40937. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  40938. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  40939. };
  40940. const byte Bf[] =
  40941. {
  40942. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  40943. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  40944. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  40945. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  40946. };
  40947. const byte order[] =
  40948. {
  40949. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  40950. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  40951. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  40952. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  40953. };
  40954. const byte Gx[] =
  40955. {
  40956. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  40957. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  40958. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  40959. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  40960. };
  40961. const byte Gy[] =
  40962. {
  40963. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  40964. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  40965. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  40966. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  40967. };
  40968. int cofactor = 1;
  40969. int fieldSize = 256;
  40970. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  40971. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40972. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  40973. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  40974. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  40975. AssertIntEQ(id, ECC_SECP256R1);
  40976. #endif
  40977. /* invalid case, fieldSize = 0 */
  40978. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  40979. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  40980. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  40981. AssertIntEQ(id, ECC_CURVE_INVALID);
  40982. /* invalid case, NULL prime */
  40983. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  40984. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  40985. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  40986. AssertIntEQ(id, BAD_FUNC_ARG);
  40987. /* invalid case, invalid prime */
  40988. id = wc_ecc_get_curve_id_from_params(fieldSize,
  40989. primeInvalid, sizeof(primeInvalid),
  40990. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  40991. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  40992. AssertIntEQ(id, ECC_CURVE_INVALID);
  40993. printf(resultFmt, passed);
  40994. #endif
  40995. return 0;
  40996. }
  40997. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  40998. {
  40999. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41000. !defined(HAVE_FAST_RSA)
  41001. WOLFSSL_RSA* rsa = NULL;
  41002. WOLFSSL_EVP_PKEY* pkey = NULL;
  41003. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41004. const char* in = "What is easy to do is easy not to do.";
  41005. size_t inlen = XSTRLEN(in);
  41006. size_t outEncLen = 0;
  41007. byte* outEnc = NULL;
  41008. byte* outDec = NULL;
  41009. size_t outDecLen = 0;
  41010. size_t rsaKeySz = 2048/8; /* Bytes */
  41011. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41012. byte* inTmp = NULL;
  41013. byte* outEncTmp = NULL;
  41014. byte* outDecTmp = NULL;
  41015. #endif
  41016. printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
  41017. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41018. XMEMSET(outEnc, 0, rsaKeySz);
  41019. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41020. XMEMSET(outDec, 0, rsaKeySz);
  41021. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41022. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41023. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41024. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41025. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41026. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41027. WOLFSSL_SUCCESS);
  41028. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  41029. since ctx uses it.*/
  41030. AssertIntEQ(pkey->references, 2);
  41031. EVP_PKEY_free(pkey);
  41032. AssertIntEQ(pkey->references, 1);
  41033. /* Encrypt data */
  41034. /* Check that we can get the required output buffer length by passing in a
  41035. * NULL output buffer. */
  41036. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  41037. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41038. AssertIntEQ(rsaKeySz, outEncLen);
  41039. /* Now do the actual encryption. */
  41040. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  41041. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41042. /* Decrypt data */
  41043. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41044. /* Check that we can get the required output buffer length by passing in a
  41045. * NULL output buffer. */
  41046. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  41047. WOLFSSL_SUCCESS);
  41048. AssertIntEQ(rsaKeySz, outDecLen);
  41049. /* Now do the actual decryption. */
  41050. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  41051. WOLFSSL_SUCCESS);
  41052. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  41053. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41054. /* The input length must be the same size as the RSA key.*/
  41055. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41056. XMEMSET(inTmp, 9, rsaKeySz);
  41057. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41058. XMEMSET(outEncTmp, 0, rsaKeySz);
  41059. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41060. XMEMSET(outDecTmp, 0, rsaKeySz);
  41061. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41062. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41063. WOLFSSL_SUCCESS);
  41064. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  41065. WOLFSSL_SUCCESS);
  41066. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41067. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  41068. WOLFSSL_SUCCESS);
  41069. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  41070. #endif
  41071. EVP_PKEY_CTX_free(ctx);
  41072. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41073. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41074. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41075. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41076. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41077. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41078. #endif
  41079. printf(resultFmt, passed);
  41080. #endif
  41081. return 0;
  41082. }
  41083. static int test_wolfSSL_EVP_PKEY_sign_verify(void)
  41084. {
  41085. #if defined(OPENSSL_EXTRA)
  41086. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41087. WOLFSSL_DSA* dsa = NULL;
  41088. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41089. WOLFSSL_EVP_PKEY* pkey = NULL;
  41090. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41091. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  41092. const char* in = "What is easy to do is easy not to do.";
  41093. size_t inlen = XSTRLEN(in);
  41094. byte hash[SHA256_DIGEST_LENGTH] = {0};
  41095. byte zero[SHA256_DIGEST_LENGTH] = {0};
  41096. SHA256_CTX c;
  41097. byte* sig = NULL;
  41098. byte* sigVerify = NULL;
  41099. size_t siglen;
  41100. size_t siglenOnlyLen;
  41101. size_t keySz = 2048/8; /* Bytes */
  41102. int i;
  41103. int encs[3] = {0};
  41104. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41105. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41106. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41107. encs[0] = EVP_PKEY_RSA;
  41108. #endif
  41109. #endif
  41110. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41111. encs[1] = EVP_PKEY_DSA;
  41112. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41113. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41114. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41115. encs[2] = EVP_PKEY_EC;
  41116. #endif
  41117. #endif
  41118. printf(testingFmt, "wolfSSL_EVP_PKEY_sign_verify()");
  41119. AssertNotNull(sig =
  41120. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41121. AssertNotNull(sigVerify =
  41122. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41123. for (i = 0; i < 3; i++) {
  41124. if (encs[i] == 0)
  41125. continue;
  41126. siglen = keySz;
  41127. XMEMSET(sig, 0, keySz);
  41128. XMEMSET(sigVerify, 0, keySz);
  41129. /* Generate hash */
  41130. SHA256_Init(&c);
  41131. SHA256_Update(&c, in, inlen);
  41132. SHA256_Final(hash, &c);
  41133. #ifdef WOLFSSL_SMALL_STACK_CACHE
  41134. /* workaround for small stack cache case */
  41135. wc_Sha256Free((wc_Sha256*)&c);
  41136. #endif
  41137. /* Generate key */
  41138. AssertNotNull(pkey = EVP_PKEY_new());
  41139. switch (encs[i]) {
  41140. case EVP_PKEY_RSA:
  41141. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41142. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41143. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41144. {
  41145. WOLFSSL_RSA* rsa = NULL;
  41146. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41147. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41148. }
  41149. #endif
  41150. #endif
  41151. break;
  41152. case EVP_PKEY_DSA:
  41153. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41154. AssertNotNull(dsa = DSA_new());
  41155. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  41156. NULL, 0, NULL, NULL, NULL), 1);
  41157. AssertIntEQ(DSA_generate_key(dsa), 1);
  41158. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  41159. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41160. break;
  41161. case EVP_PKEY_EC:
  41162. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41163. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41164. {
  41165. WOLFSSL_EC_KEY* ecKey = NULL;
  41166. AssertNotNull(ecKey = EC_KEY_new());
  41167. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  41168. AssertIntEQ(
  41169. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41170. }
  41171. #endif
  41172. #endif
  41173. break;
  41174. }
  41175. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41176. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41177. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41178. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41179. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41180. if (encs[i] == EVP_PKEY_RSA)
  41181. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41182. WOLFSSL_SUCCESS);
  41183. #endif
  41184. #endif
  41185. /* Check returning only length */
  41186. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  41187. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41188. AssertIntGT(siglenOnlyLen, 0);
  41189. /* Sign data */
  41190. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  41191. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41192. AssertIntGE(siglenOnlyLen, siglen);
  41193. /* Verify signature */
  41194. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  41195. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41196. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41197. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41198. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41199. if (encs[i] == EVP_PKEY_RSA)
  41200. AssertIntEQ(
  41201. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  41202. WOLFSSL_SUCCESS);
  41203. #endif
  41204. #endif
  41205. AssertIntEQ(EVP_PKEY_verify(
  41206. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  41207. WOLFSSL_SUCCESS);
  41208. AssertIntEQ(EVP_PKEY_verify(
  41209. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  41210. WOLFSSL_FAILURE);
  41211. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41212. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41213. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41214. if (encs[i] == EVP_PKEY_RSA) {
  41215. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  41216. /* Try RSA sign/verify with no padding. */
  41217. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41218. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41219. WOLFSSL_SUCCESS);
  41220. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  41221. siglen), WOLFSSL_SUCCESS);
  41222. AssertIntGE(siglenOnlyLen, siglen);
  41223. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41224. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41225. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  41226. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  41227. siglen), WOLFSSL_SUCCESS);
  41228. #endif
  41229. /* Wrong padding schemes. */
  41230. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41231. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  41232. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  41233. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  41234. siglen), WOLFSSL_SUCCESS);
  41235. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41236. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41237. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  41238. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  41239. siglen), WOLFSSL_SUCCESS);
  41240. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41241. WOLFSSL_SUCCESS);
  41242. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41243. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  41244. }
  41245. #endif
  41246. #endif
  41247. /* error cases */
  41248. siglen = keySz; /* Reset because sig size may vary slightly */
  41249. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  41250. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41251. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  41252. WOLFSSL_SUCCESS);
  41253. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  41254. WOLFSSL_SUCCESS);
  41255. EVP_PKEY_free(pkey);
  41256. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41257. DSA_free(dsa);
  41258. dsa = NULL;
  41259. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41260. EVP_PKEY_CTX_free(ctx_verify);
  41261. EVP_PKEY_CTX_free(ctx);
  41262. }
  41263. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41264. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41265. printf(resultFmt, passed);
  41266. #endif /* OPENSSL_EXTRA */
  41267. return 0;
  41268. }
  41269. static int test_EVP_PKEY_rsa(void)
  41270. {
  41271. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  41272. WOLFSSL_RSA* rsa;
  41273. WOLFSSL_EVP_PKEY* pkey;
  41274. AssertNotNull(rsa = wolfSSL_RSA_new());
  41275. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41276. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  41277. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  41278. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41279. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  41280. wolfSSL_EVP_PKEY_free(pkey);
  41281. printf(resultFmt, passed);
  41282. #endif
  41283. return 0;
  41284. }
  41285. static int test_EVP_PKEY_ec(void)
  41286. {
  41287. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41288. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41289. WOLFSSL_EC_KEY* ecKey;
  41290. WOLFSSL_EVP_PKEY* pkey;
  41291. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  41292. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41293. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  41294. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  41295. /* Should fail since ecKey is empty */
  41296. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  41297. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41298. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41299. wolfSSL_EVP_PKEY_free(pkey);
  41300. printf(resultFmt, passed);
  41301. #endif
  41302. #endif
  41303. return 0;
  41304. }
  41305. static int test_EVP_PKEY_cmp(void)
  41306. {
  41307. #if defined(OPENSSL_EXTRA)
  41308. EVP_PKEY *a, *b;
  41309. const unsigned char *in;
  41310. printf(testingFmt, "wolfSSL_EVP_PKEY_cmp()");
  41311. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  41312. in = client_key_der_2048;
  41313. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  41314. &in, (long)sizeof_client_key_der_2048));
  41315. in = client_key_der_2048;
  41316. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  41317. &in, (long)sizeof_client_key_der_2048));
  41318. /* Test success case RSA */
  41319. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41320. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  41321. #else
  41322. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  41323. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  41324. EVP_PKEY_free(b);
  41325. EVP_PKEY_free(a);
  41326. #endif
  41327. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41328. in = ecc_clikey_der_256;
  41329. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  41330. &in, (long)sizeof_ecc_clikey_der_256));
  41331. in = ecc_clikey_der_256;
  41332. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  41333. &in, (long)sizeof_ecc_clikey_der_256));
  41334. /* Test success case ECC */
  41335. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41336. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  41337. #else
  41338. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  41339. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  41340. EVP_PKEY_free(b);
  41341. EVP_PKEY_free(a);
  41342. #endif
  41343. /* Test failure cases */
  41344. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  41345. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41346. in = client_key_der_2048;
  41347. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  41348. &in, (long)sizeof_client_key_der_2048));
  41349. in = ecc_clikey_der_256;
  41350. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  41351. &in, (long)sizeof_ecc_clikey_der_256));
  41352. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41353. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  41354. #else
  41355. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  41356. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  41357. EVP_PKEY_free(b);
  41358. EVP_PKEY_free(a);
  41359. #endif
  41360. /* invalid or empty failure cases */
  41361. a = EVP_PKEY_new();
  41362. b = EVP_PKEY_new();
  41363. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41364. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  41365. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  41366. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  41367. #ifdef NO_RSA
  41368. /* Type check will fail since RSA is the default EVP key type */
  41369. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  41370. #else
  41371. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  41372. #endif
  41373. #else
  41374. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  41375. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  41376. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  41377. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  41378. #endif
  41379. EVP_PKEY_free(b);
  41380. EVP_PKEY_free(a);
  41381. (void)in;
  41382. printf(resultFmt, passed);
  41383. #endif
  41384. return 0;
  41385. }
  41386. static int test_ERR_load_crypto_strings(void)
  41387. {
  41388. #if defined(OPENSSL_ALL)
  41389. ERR_load_crypto_strings();
  41390. printf(resultFmt, passed);
  41391. #endif
  41392. return 0;
  41393. }
  41394. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  41395. static void free_x509(X509* x)
  41396. {
  41397. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  41398. }
  41399. #endif
  41400. static int test_sk_X509(void)
  41401. {
  41402. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  41403. STACK_OF(X509)* s;
  41404. AssertNotNull(s = sk_X509_new());
  41405. AssertIntEQ(sk_X509_num(s), 0);
  41406. sk_X509_pop_free(s, NULL);
  41407. AssertNotNull(s = sk_X509_new_null());
  41408. AssertIntEQ(sk_X509_num(s), 0);
  41409. sk_X509_pop_free(s, NULL);
  41410. AssertNotNull(s = sk_X509_new());
  41411. sk_X509_push(s, (X509*)1);
  41412. AssertIntEQ(sk_X509_num(s), 1);
  41413. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  41414. sk_X509_push(s, (X509*)2);
  41415. AssertIntEQ(sk_X509_num(s), 2);
  41416. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  41417. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  41418. sk_X509_push(s, (X509*)2);
  41419. sk_X509_pop_free(s, free_x509);
  41420. printf(resultFmt, passed);
  41421. #endif
  41422. return 0;
  41423. }
  41424. static int test_sk_X509_CRL(void)
  41425. {
  41426. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  41427. X509_CRL* crl;
  41428. XFILE fp;
  41429. STACK_OF(X509_CRL)* s;
  41430. printf(testingFmt, "test_sk_X509_CRL");
  41431. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  41432. AssertTrue((fp != XBADFILE));
  41433. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  41434. XFCLOSE(fp);
  41435. AssertNotNull(s = sk_X509_CRL_new());
  41436. AssertIntEQ(sk_X509_CRL_num(s), 0);
  41437. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  41438. AssertIntEQ(sk_X509_CRL_num(s), 1);
  41439. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  41440. sk_X509_CRL_free(s);
  41441. printf(resultFmt, passed);
  41442. #endif
  41443. return 0;
  41444. }
  41445. static int test_X509_get_signature_nid(void)
  41446. {
  41447. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41448. X509* x509;
  41449. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  41450. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41451. SSL_FILETYPE_PEM));
  41452. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  41453. X509_free(x509);
  41454. printf(resultFmt, passed);
  41455. #endif
  41456. return 0;
  41457. }
  41458. static int test_X509_REQ(void)
  41459. {
  41460. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  41461. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  41462. X509_NAME* name;
  41463. #ifndef NO_RSA
  41464. X509_NAME* subject;
  41465. #endif
  41466. #if !defined(NO_RSA) || defined(HAVE_ECC)
  41467. X509_REQ* req;
  41468. EVP_PKEY* priv;
  41469. EVP_PKEY* pub;
  41470. unsigned char* der = NULL;
  41471. int len;
  41472. #endif
  41473. #ifndef NO_RSA
  41474. EVP_MD_CTX *mctx = NULL;
  41475. EVP_PKEY_CTX *pkctx = NULL;
  41476. #ifdef USE_CERT_BUFFERS_1024
  41477. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  41478. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  41479. #elif defined(USE_CERT_BUFFERS_2048)
  41480. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  41481. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  41482. #endif
  41483. #endif
  41484. #ifdef HAVE_ECC
  41485. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  41486. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  41487. #endif
  41488. AssertNotNull(name = X509_NAME_new());
  41489. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  41490. (byte*)"wolfssl.com", 11, 0, 1),
  41491. WOLFSSL_SUCCESS);
  41492. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  41493. (byte*)"support@wolfssl.com", 19, -1,
  41494. 1), WOLFSSL_SUCCESS);
  41495. #ifndef NO_RSA
  41496. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  41497. (long)sizeof_client_key_der_2048));
  41498. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  41499. (long)sizeof_client_keypub_der_2048));
  41500. AssertNotNull(req = X509_REQ_new());
  41501. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  41502. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  41503. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  41504. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  41505. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  41506. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  41507. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  41508. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  41509. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  41510. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  41511. len = i2d_X509_REQ(req, &der);
  41512. DEBUG_WRITE_DER(der, len, "req.der");
  41513. #ifdef USE_CERT_BUFFERS_1024
  41514. AssertIntEQ(len, 381);
  41515. #else
  41516. AssertIntEQ(len, 643);
  41517. #endif
  41518. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  41519. der = NULL;
  41520. mctx = EVP_MD_CTX_new();
  41521. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  41522. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  41523. EVP_MD_CTX_free(mctx);
  41524. X509_REQ_free(NULL);
  41525. X509_REQ_free(req);
  41526. /* Test getting the subject from a newly created X509_REQ */
  41527. AssertNotNull(req = X509_REQ_new());
  41528. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  41529. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  41530. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  41531. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  41532. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  41533. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  41534. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  41535. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  41536. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  41537. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  41538. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  41539. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  41540. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  41541. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  41542. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  41543. len = i2d_X509_REQ(req, &der);
  41544. DEBUG_WRITE_DER(der, len, "req2.der");
  41545. #ifdef USE_CERT_BUFFERS_1024
  41546. AssertIntEQ(len, 435);
  41547. #else
  41548. AssertIntEQ(len, 696);
  41549. #endif
  41550. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  41551. der = NULL;
  41552. EVP_PKEY_free(pub);
  41553. EVP_PKEY_free(priv);
  41554. X509_REQ_free(req);
  41555. #endif
  41556. #ifdef HAVE_ECC
  41557. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  41558. sizeof_ecc_clikey_der_256));
  41559. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  41560. sizeof_ecc_clikeypub_der_256));
  41561. AssertNotNull(req = X509_REQ_new());
  41562. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  41563. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  41564. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  41565. /* Signature is random and may be shorter or longer. */
  41566. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  41567. AssertIntLE(len, 253);
  41568. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  41569. X509_REQ_free(req);
  41570. EVP_PKEY_free(pub);
  41571. EVP_PKEY_free(priv);
  41572. #ifdef FP_ECC
  41573. wc_ecc_fp_free();
  41574. #endif
  41575. #endif /* HAVE_ECC */
  41576. X509_NAME_free(name);
  41577. printf(resultFmt, passed);
  41578. #endif
  41579. return 0;
  41580. }
  41581. static int test_wolfssl_PKCS7(void)
  41582. {
  41583. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO)
  41584. PKCS7* pkcs7;
  41585. byte data[FOURK_BUF];
  41586. word32 len = sizeof(data);
  41587. const byte* p = data;
  41588. byte content[] = "Test data to encode.";
  41589. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  41590. BIO* bio;
  41591. byte key[sizeof(client_key_der_2048)];
  41592. word32 keySz = (word32)sizeof(key);
  41593. byte* out = NULL;
  41594. #endif
  41595. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  41596. (word32)sizeof(content),
  41597. 0, 0)), 0);
  41598. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  41599. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  41600. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  41601. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  41602. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  41603. PKCS7_free(pkcs7);
  41604. /* fail case, without PKCS7_NOVERIFY */
  41605. p = data;
  41606. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  41607. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  41608. 0), WOLFSSL_FAILURE);
  41609. PKCS7_free(pkcs7);
  41610. /* success case, with PKCS7_NOVERIFY */
  41611. p = data;
  41612. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  41613. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  41614. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  41615. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  41616. /* test i2d */
  41617. XMEMCPY(key, client_key_der_2048, keySz);
  41618. pkcs7->privateKey = key;
  41619. pkcs7->privateKeySz = (word32)sizeof(key);
  41620. pkcs7->encryptOID = RSAk;
  41621. pkcs7->hashOID = SHAh;
  41622. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  41623. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  41624. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  41625. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41626. BIO_free(bio);
  41627. #endif
  41628. PKCS7_free(NULL);
  41629. PKCS7_free(pkcs7);
  41630. printf(resultFmt, passed);
  41631. #endif
  41632. return 0;
  41633. }
  41634. static int test_wolfSSL_PKCS7_sign(void)
  41635. {
  41636. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  41637. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41638. PKCS7* p7 = NULL;
  41639. PKCS7* p7Ver = NULL;
  41640. byte* out = NULL;
  41641. byte* tmpPtr = NULL;
  41642. int outLen = 0;
  41643. int flags = 0;
  41644. byte data[] = "Test data to encode.";
  41645. const char* cert = "./certs/server-cert.pem";
  41646. const char* key = "./certs/server-key.pem";
  41647. const char* ca = "./certs/ca-cert.pem";
  41648. WOLFSSL_BIO* certBio = NULL;
  41649. WOLFSSL_BIO* keyBio = NULL;
  41650. WOLFSSL_BIO* caBio = NULL;
  41651. WOLFSSL_BIO* inBio = NULL;
  41652. X509* signCert = NULL;
  41653. EVP_PKEY* signKey = NULL;
  41654. X509* caCert = NULL;
  41655. X509_STORE* store = NULL;
  41656. printf(testingFmt, "wolfSSL_PKCS7_sign()");
  41657. /* read signer cert/key into BIO */
  41658. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  41659. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  41660. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  41661. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  41662. /* read CA cert into store (for verify) */
  41663. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  41664. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  41665. AssertNotNull(store = X509_STORE_new());
  41666. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  41667. /* data to be signed into BIO */
  41668. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41669. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41670. /* PKCS7_sign, bad args: signer NULL */
  41671. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  41672. /* PKCS7_sign, bad args: signer key NULL */
  41673. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  41674. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  41675. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  41676. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  41677. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  41678. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  41679. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  41680. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  41681. {
  41682. flags = PKCS7_BINARY;
  41683. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41684. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  41685. /* verify with d2i_PKCS7 */
  41686. tmpPtr = out;
  41687. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  41688. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  41689. PKCS7_free(p7Ver);
  41690. /* verify with wc_PKCS7_VerifySignedData */
  41691. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, devId));
  41692. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  41693. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  41694. /* compare the signer found to expected signer */
  41695. AssertIntNE(p7Ver->verifyCertSz, 0);
  41696. tmpPtr = NULL;
  41697. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  41698. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  41699. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  41700. tmpPtr = NULL;
  41701. wc_PKCS7_Free(p7Ver);
  41702. AssertNotNull(out);
  41703. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41704. out = NULL;
  41705. PKCS7_free(p7);
  41706. }
  41707. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  41708. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  41709. {
  41710. /* re-populate input BIO, may have been consumed */
  41711. BIO_free(inBio);
  41712. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41713. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41714. flags = PKCS7_BINARY | PKCS7_STREAM;
  41715. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41716. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  41717. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  41718. /* PKCS7_final, bad args: PKCS7 null */
  41719. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  41720. /* PKCS7_final, bad args: PKCS7 null */
  41721. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  41722. tmpPtr = out;
  41723. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  41724. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  41725. PKCS7_free(p7Ver);
  41726. AssertNotNull(out);
  41727. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41728. out = NULL;
  41729. PKCS7_free(p7);
  41730. }
  41731. /* TEST SUCCESS: Detached, not streaming, not MIME */
  41732. {
  41733. /* re-populate input BIO, may have been consumed */
  41734. BIO_free(inBio);
  41735. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41736. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41737. flags = PKCS7_BINARY | PKCS7_DETACHED;
  41738. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41739. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  41740. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  41741. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, devId));
  41742. p7Ver->content = data;
  41743. p7Ver->contentSz = sizeof(data);
  41744. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  41745. wc_PKCS7_Free(p7Ver);
  41746. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  41747. * yet support detached content */
  41748. tmpPtr = out;
  41749. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  41750. PKCS7_free(p7Ver);
  41751. AssertNotNull(out);
  41752. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41753. out = NULL;
  41754. PKCS7_free(p7);
  41755. }
  41756. /* TEST SUCCESS: Detached, streaming, not MIME */
  41757. {
  41758. /* re-populate input BIO, may have been consumed */
  41759. BIO_free(inBio);
  41760. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41761. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41762. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  41763. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41764. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  41765. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  41766. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  41767. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, devId));
  41768. p7Ver->content = data;
  41769. p7Ver->contentSz = sizeof(data);
  41770. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  41771. wc_PKCS7_Free(p7Ver);
  41772. AssertNotNull(out);
  41773. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41774. PKCS7_free(p7);
  41775. }
  41776. X509_STORE_free(store);
  41777. X509_free(caCert);
  41778. X509_free(signCert);
  41779. EVP_PKEY_free(signKey);
  41780. BIO_free(inBio);
  41781. BIO_free(keyBio);
  41782. BIO_free(certBio);
  41783. BIO_free(caBio);
  41784. printf(resultFmt, passed);
  41785. #endif
  41786. return 0;
  41787. }
  41788. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  41789. {
  41790. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  41791. PKCS7_SIGNED* pkcs7;
  41792. printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
  41793. pkcs7 = PKCS7_SIGNED_new();
  41794. AssertNotNull(pkcs7);
  41795. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  41796. PKCS7_SIGNED_free(pkcs7);
  41797. printf(resultFmt, passed);
  41798. #endif
  41799. return 0;
  41800. }
  41801. #ifndef NO_BIO
  41802. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  41803. {
  41804. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  41805. PKCS7* pkcs7 = NULL;
  41806. BIO* bio = NULL;
  41807. const byte* cert_buf = NULL;
  41808. int ret = 0;
  41809. WC_RNG rng;
  41810. const byte data[] = { /* Hello World */
  41811. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41812. 0x72,0x6c,0x64
  41813. };
  41814. #ifndef NO_RSA
  41815. #if defined(USE_CERT_BUFFERS_2048)
  41816. byte key[sizeof(client_key_der_2048)];
  41817. byte cert[sizeof(client_cert_der_2048)];
  41818. word32 keySz = (word32)sizeof(key);
  41819. word32 certSz = (word32)sizeof(cert);
  41820. XMEMSET(key, 0, keySz);
  41821. XMEMSET(cert, 0, certSz);
  41822. XMEMCPY(key, client_key_der_2048, keySz);
  41823. XMEMCPY(cert, client_cert_der_2048, certSz);
  41824. #elif defined(USE_CERT_BUFFERS_1024)
  41825. byte key[sizeof_client_key_der_1024];
  41826. byte cert[sizeof(sizeof_client_cert_der_1024)];
  41827. word32 keySz = (word32)sizeof(key);
  41828. word32 certSz = (word32)sizeof(cert);
  41829. XMEMSET(key, 0, keySz);
  41830. XMEMSET(cert, 0, certSz);
  41831. XMEMCPY(key, client_key_der_1024, keySz);
  41832. XMEMCPY(cert, client_cert_der_1024, certSz);
  41833. #else
  41834. unsigned char cert[ONEK_BUF];
  41835. unsigned char key[ONEK_BUF];
  41836. XFILE fp;
  41837. int certSz;
  41838. int keySz;
  41839. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  41840. AssertTrue((fp != XBADFILE));
  41841. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  41842. XFCLOSE(fp);
  41843. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  41844. AssertTrue(fp != XBADFILE);
  41845. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  41846. XFCLOSE(fp);
  41847. #endif
  41848. #elif defined(HAVE_ECC)
  41849. #if defined(USE_CERT_BUFFERS_256)
  41850. unsigned char cert[sizeof(cliecc_cert_der_256)];
  41851. unsigned char key[sizeof(ecc_clikey_der_256)];
  41852. int certSz = (int)sizeof(cert);
  41853. int keySz = (int)sizeof(key);
  41854. XMEMSET(cert, 0, certSz);
  41855. XMEMSET(key, 0, keySz);
  41856. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  41857. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  41858. #else
  41859. unsigned char cert[ONEK_BUF];
  41860. unsigned char key[ONEK_BUF];
  41861. XFILE fp;
  41862. int certSz, keySz;
  41863. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  41864. AssertTrue(fp != XBADFILE);
  41865. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  41866. XFCLOSE(fp);
  41867. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  41868. AssertTrue(fp != XBADFILE);
  41869. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  41870. XFCLOSE(fp);
  41871. #endif
  41872. #else
  41873. #error PKCS7 requires ECC or RSA
  41874. #endif
  41875. printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
  41876. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
  41877. /* initialize with DER encoded cert */
  41878. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  41879. /* init rng */
  41880. AssertIntEQ(wc_InitRng(&rng), 0);
  41881. pkcs7->rng = &rng;
  41882. pkcs7->content = (byte*)data; /* not used for ex */
  41883. pkcs7->contentSz = (word32)sizeof(data);
  41884. pkcs7->contentOID = SIGNED_DATA;
  41885. pkcs7->privateKey = key;
  41886. pkcs7->privateKeySz = (word32)sizeof(key);
  41887. pkcs7->encryptOID = RSAk;
  41888. pkcs7->hashOID = SHAh;
  41889. pkcs7->signedAttribs = NULL;
  41890. pkcs7->signedAttribsSz = 0;
  41891. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  41892. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  41893. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  41894. /* Read PKCS#7 PEM from BIO */
  41895. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  41896. AssertIntGE(ret, 0);
  41897. BIO_free(bio);
  41898. wc_PKCS7_Free(pkcs7);
  41899. wc_FreeRng(&rng);
  41900. printf(resultFmt, passed);
  41901. #endif
  41902. return 0;
  41903. }
  41904. #ifdef HAVE_SMIME
  41905. static int test_wolfSSL_SMIME_read_PKCS7(void)
  41906. {
  41907. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  41908. !defined(NO_RSA)
  41909. PKCS7* pkcs7 = NULL;
  41910. BIO* bio = NULL;
  41911. BIO* bcont = NULL;
  41912. BIO* out = NULL;
  41913. const byte* outBuf = NULL;
  41914. int outBufLen = 0;
  41915. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  41916. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  41917. printf(testingFmt, "wolfSSL_SMIME_read_PKCS7()");
  41918. /* smime-test.p7s */
  41919. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  41920. AssertNotNull(bio);
  41921. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41922. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41923. AssertNotNull(pkcs7);
  41924. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  41925. PKCS7_NOVERIFY), SSL_SUCCESS);
  41926. XFCLOSE(smimeTestFile);
  41927. if (bcont) BIO_free(bcont);
  41928. wolfSSL_PKCS7_free(pkcs7);
  41929. /* smime-test-multipart.p7s */
  41930. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  41931. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41932. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41933. AssertNotNull(pkcs7);
  41934. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  41935. PKCS7_NOVERIFY), SSL_SUCCESS);
  41936. XFCLOSE(smimeTestFile);
  41937. if (bcont) BIO_free(bcont);
  41938. wolfSSL_PKCS7_free(pkcs7);
  41939. /* smime-test-multipart-badsig.p7s */
  41940. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  41941. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41942. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41943. AssertNull(pkcs7);
  41944. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  41945. PKCS7_NOVERIFY), SSL_FAILURE);
  41946. XFCLOSE(smimeTestFile);
  41947. if (bcont) BIO_free(bcont);
  41948. wolfSSL_PKCS7_free(pkcs7);
  41949. /* smime-test-canon.p7s */
  41950. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  41951. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41952. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41953. AssertNotNull(pkcs7);
  41954. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  41955. PKCS7_NOVERIFY), SSL_SUCCESS);
  41956. XFCLOSE(smimeTestFile);
  41957. if (bcont) BIO_free(bcont);
  41958. wolfSSL_PKCS7_free(pkcs7);
  41959. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  41960. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  41961. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  41962. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  41963. AssertNotNull(pkcs7);
  41964. out = wolfSSL_BIO_new(BIO_s_mem());
  41965. AssertNotNull(out);
  41966. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  41967. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  41968. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  41969. /* Content-Type should not show up at beginning of output buffer */
  41970. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  41971. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  41972. BIO_free(out);
  41973. BIO_free(bio);
  41974. if (bcont) BIO_free(bcont);
  41975. wolfSSL_PKCS7_free(pkcs7);
  41976. printf(resultFmt, passed);
  41977. #endif
  41978. return 0;
  41979. }
  41980. static int test_wolfSSL_SMIME_write_PKCS7(void)
  41981. {
  41982. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  41983. PKCS7* p7 = NULL;
  41984. PKCS7* p7Ver = NULL;
  41985. int flags = 0;
  41986. byte data[] = "Test data to encode.";
  41987. const char* cert = "./certs/server-cert.pem";
  41988. const char* key = "./certs/server-key.pem";
  41989. const char* ca = "./certs/ca-cert.pem";
  41990. WOLFSSL_BIO* certBio = NULL;
  41991. WOLFSSL_BIO* keyBio = NULL;
  41992. WOLFSSL_BIO* caBio = NULL;
  41993. WOLFSSL_BIO* inBio = NULL;
  41994. WOLFSSL_BIO* outBio = NULL;
  41995. WOLFSSL_BIO* content = NULL;
  41996. X509* signCert = NULL;
  41997. EVP_PKEY* signKey = NULL;
  41998. X509* caCert = NULL;
  41999. X509_STORE* store = NULL;
  42000. printf(testingFmt, "wolfSSL_SMIME_write_PKCS7()");
  42001. /* read signer cert/key into BIO */
  42002. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  42003. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  42004. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42005. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42006. /* read CA cert into store (for verify) */
  42007. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  42008. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42009. AssertNotNull(store = X509_STORE_new());
  42010. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42011. /* generate and verify SMIME: not detached */
  42012. {
  42013. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42014. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42015. flags = PKCS7_STREAM;
  42016. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42017. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42018. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42019. /* bad arg: out NULL */
  42020. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  42021. /* bad arg: pkcs7 NULL */
  42022. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  42023. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42024. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42025. BIO_free(content);
  42026. BIO_free(inBio);
  42027. BIO_free(outBio);
  42028. PKCS7_free(p7Ver);
  42029. PKCS7_free(p7);
  42030. }
  42031. /* generate and verify SMIME: not detached, add Content-Type */
  42032. {
  42033. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42034. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42035. flags = PKCS7_STREAM | PKCS7_TEXT;
  42036. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42037. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42038. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42039. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42040. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42041. BIO_free(content);
  42042. BIO_free(inBio);
  42043. BIO_free(outBio);
  42044. PKCS7_free(p7Ver);
  42045. PKCS7_free(p7);
  42046. }
  42047. /* generate and verify SMIME: detached */
  42048. {
  42049. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42050. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42051. flags = PKCS7_DETACHED | PKCS7_STREAM;
  42052. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42053. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42054. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42055. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42056. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42057. BIO_free(content);
  42058. BIO_free(inBio);
  42059. BIO_free(outBio);
  42060. PKCS7_free(p7Ver);
  42061. PKCS7_free(p7);
  42062. }
  42063. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  42064. {
  42065. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42066. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42067. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  42068. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42069. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42070. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42071. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42072. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42073. BIO_free(content);
  42074. BIO_free(inBio);
  42075. BIO_free(outBio);
  42076. PKCS7_free(p7Ver);
  42077. PKCS7_free(p7);
  42078. }
  42079. X509_STORE_free(store);
  42080. X509_free(caCert);
  42081. X509_free(signCert);
  42082. EVP_PKEY_free(signKey);
  42083. BIO_free(keyBio);
  42084. BIO_free(certBio);
  42085. BIO_free(caBio);
  42086. printf(resultFmt, passed);
  42087. #endif
  42088. return 0;
  42089. }
  42090. #endif /* HAVE_SMIME */
  42091. #endif /* !NO_BIO */
  42092. /*----------------------------------------------------------------------------*
  42093. | Certificate Failure Checks
  42094. *----------------------------------------------------------------------------*/
  42095. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  42096. !defined(WOLFSSL_NO_CLIENT_AUTH))
  42097. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  42098. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  42099. int type)
  42100. {
  42101. int ret;
  42102. WOLFSSL_CERT_MANAGER* cm = NULL;
  42103. switch (type) {
  42104. case TESTING_RSA:
  42105. #ifdef NO_RSA
  42106. printf("RSA disabled, skipping test\n");
  42107. return ASN_SIG_CONFIRM_E;
  42108. #else
  42109. break;
  42110. #endif
  42111. case TESTING_ECC:
  42112. #ifndef HAVE_ECC
  42113. printf("ECC disabled, skipping test\n");
  42114. return ASN_SIG_CONFIRM_E;
  42115. #else
  42116. break;
  42117. #endif
  42118. default:
  42119. printf("Bad function argument\n");
  42120. return BAD_FUNC_ARG;
  42121. }
  42122. cm = wolfSSL_CertManagerNew();
  42123. if (cm == NULL) {
  42124. printf("wolfSSL_CertManagerNew failed\n");
  42125. return -1;
  42126. }
  42127. #ifndef NO_FILESYSTEM
  42128. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  42129. if (ret != WOLFSSL_SUCCESS) {
  42130. printf("wolfSSL_CertManagerLoadCA failed\n");
  42131. wolfSSL_CertManagerFree(cm);
  42132. return ret;
  42133. }
  42134. #else
  42135. (void)ca;
  42136. #endif
  42137. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  42138. /* Let AssertIntEQ handle return code */
  42139. wolfSSL_CertManagerFree(cm);
  42140. return ret;
  42141. }
  42142. static int test_RsaSigFailure_cm(void)
  42143. {
  42144. int ret = 0;
  42145. const char* ca_cert = "./certs/ca-cert.pem";
  42146. const char* server_cert = "./certs/server-cert.der";
  42147. byte* cert_buf = NULL;
  42148. size_t cert_sz = 0;
  42149. ret = load_file(server_cert, &cert_buf, &cert_sz);
  42150. if (ret == 0) {
  42151. /* corrupt DER - invert last byte, which is signature */
  42152. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  42153. /* test bad cert */
  42154. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  42155. }
  42156. printf("Signature failure test: RSA: Ret %d\n", ret);
  42157. if (cert_buf)
  42158. free(cert_buf);
  42159. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  42160. if (ret == WOLFSSL_FATAL_ERROR) {
  42161. ret = 0;
  42162. }
  42163. #else
  42164. if (ret == ASN_SIG_CONFIRM_E) {
  42165. ret = 0;
  42166. }
  42167. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  42168. return ret;
  42169. }
  42170. static int test_EccSigFailure_cm(void)
  42171. {
  42172. int ret = 0;
  42173. /* self-signed ECC cert, so use server cert as CA */
  42174. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  42175. const char* server_cert = "./certs/server-ecc.der";
  42176. byte* cert_buf = NULL;
  42177. size_t cert_sz = 0;
  42178. ret = load_file(server_cert, &cert_buf, &cert_sz);
  42179. if (ret == 0) {
  42180. /* corrupt DER - invert last byte, which is signature */
  42181. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  42182. /* test bad cert */
  42183. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  42184. }
  42185. printf("Signature failure test: ECC: Ret %d\n", ret);
  42186. if (cert_buf)
  42187. free(cert_buf);
  42188. #ifdef FP_ECC
  42189. wc_ecc_fp_free();
  42190. #endif
  42191. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  42192. if (ret == WOLFSSL_FATAL_ERROR) {
  42193. ret = 0;
  42194. }
  42195. #else
  42196. if (ret == ASN_SIG_CONFIRM_E) {
  42197. ret = 0;
  42198. }
  42199. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  42200. return ret;
  42201. }
  42202. #endif /* NO_CERTS */
  42203. #ifdef WOLFSSL_TLS13
  42204. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  42205. #ifdef WC_SHA384_DIGEST_SIZE
  42206. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  42207. #else
  42208. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  42209. #endif
  42210. #endif
  42211. #ifdef WOLFSSL_EARLY_DATA
  42212. static const char earlyData[] = "Early Data";
  42213. static char earlyDataBuffer[1];
  42214. #endif
  42215. static int test_tls13_apis(void)
  42216. {
  42217. int ret = 0;
  42218. #ifndef WOLFSSL_NO_TLS12
  42219. #ifndef NO_WOLFSSL_CLIENT
  42220. WOLFSSL_CTX* clientTls12Ctx;
  42221. WOLFSSL* clientTls12Ssl;
  42222. #endif
  42223. #ifndef NO_WOLFSSL_SERVER
  42224. WOLFSSL_CTX* serverTls12Ctx;
  42225. WOLFSSL* serverTls12Ssl;
  42226. #endif
  42227. #endif
  42228. #ifndef NO_WOLFSSL_CLIENT
  42229. WOLFSSL_CTX* clientCtx;
  42230. WOLFSSL* clientSsl;
  42231. #endif
  42232. #ifndef NO_WOLFSSL_SERVER
  42233. WOLFSSL_CTX* serverCtx;
  42234. WOLFSSL* serverSsl;
  42235. #ifndef NO_CERTS
  42236. const char* ourCert = svrCertFile;
  42237. const char* ourKey = svrKeyFile;
  42238. #endif
  42239. #endif
  42240. int required;
  42241. #ifdef WOLFSSL_EARLY_DATA
  42242. int outSz;
  42243. #endif
  42244. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  42245. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  42246. #ifdef HAVE_PQC
  42247. WOLFSSL_SABER_LEVEL3
  42248. #else
  42249. WOLFSSL_ECC_SECP256R1
  42250. #endif
  42251. };
  42252. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  42253. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  42254. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  42255. int numGroups = 2;
  42256. #endif
  42257. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42258. char groupList[] =
  42259. #ifndef NO_ECC_SECP
  42260. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  42261. "P-521:"
  42262. #endif
  42263. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  42264. "P-384:"
  42265. #endif
  42266. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  42267. "P-256"
  42268. #ifdef HAVE_PQC
  42269. ":P256_SABER_LEVEL1"
  42270. #endif
  42271. #endif
  42272. #ifdef HAVE_PQC
  42273. ":KYBER_LEVEL1"
  42274. #endif
  42275. "";
  42276. #endif /* !defined(NO_ECC_SECP) */
  42277. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  42278. (void)ret;
  42279. #ifndef WOLFSSL_NO_TLS12
  42280. #ifndef NO_WOLFSSL_CLIENT
  42281. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  42282. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  42283. #endif
  42284. #ifndef NO_WOLFSSL_SERVER
  42285. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  42286. #ifndef NO_CERTS
  42287. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  42288. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  42289. #endif
  42290. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  42291. #endif
  42292. #endif
  42293. #ifndef NO_WOLFSSL_CLIENT
  42294. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  42295. clientSsl = wolfSSL_new(clientCtx);
  42296. #endif
  42297. #ifndef NO_WOLFSSL_SERVER
  42298. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  42299. #ifndef NO_CERTS
  42300. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  42301. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  42302. #endif
  42303. serverSsl = wolfSSL_new(serverCtx);
  42304. #endif
  42305. #ifdef WOLFSSL_SEND_HRR_COOKIE
  42306. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  42307. #ifndef NO_WOLFSSL_CLIENT
  42308. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  42309. #endif
  42310. #ifndef NO_WOLFSSL_SERVER
  42311. #ifndef WOLFSSL_NO_TLS12
  42312. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  42313. #endif
  42314. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  42315. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  42316. WOLFSSL_SUCCESS);
  42317. #endif
  42318. #endif
  42319. #ifdef HAVE_SUPPORTED_CURVES
  42320. #ifdef HAVE_ECC
  42321. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  42322. #ifndef NO_WOLFSSL_SERVER
  42323. do {
  42324. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  42325. #ifdef WOLFSSL_ASYNC_CRYPT
  42326. if (ret == WC_PENDING_E)
  42327. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  42328. #endif
  42329. } while (ret == WC_PENDING_E);
  42330. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42331. #endif
  42332. #ifndef NO_WOLFSSL_CLIENT
  42333. #ifndef WOLFSSL_NO_TLS12
  42334. do {
  42335. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  42336. #ifdef WOLFSSL_ASYNC_CRYPT
  42337. if (ret == WC_PENDING_E)
  42338. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  42339. #endif
  42340. } while (ret == WC_PENDING_E);
  42341. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42342. #endif
  42343. do {
  42344. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  42345. #ifdef WOLFSSL_ASYNC_CRYPT
  42346. if (ret == WC_PENDING_E)
  42347. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  42348. #endif
  42349. } while (ret == WC_PENDING_E);
  42350. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42351. #endif
  42352. #elif defined(HAVE_CURVE25519)
  42353. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  42354. #ifndef NO_WOLFSSL_SERVER
  42355. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  42356. WOLFSSL_SUCCESS);
  42357. #endif
  42358. #ifndef NO_WOLFSSL_CLIENT
  42359. #ifndef WOLFSSL_NO_TLS12
  42360. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  42361. WOLFSSL_SUCCESS);
  42362. #endif
  42363. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  42364. WOLFSSL_SUCCESS);
  42365. #endif
  42366. #elif defined(HAVE_CURVE448)
  42367. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  42368. #ifndef NO_WOLFSSL_SERVER
  42369. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  42370. WOLFSSL_SUCCESS);
  42371. #endif
  42372. #ifndef NO_WOLFSSL_CLIENT
  42373. #ifndef WOLFSSL_NO_TLS12
  42374. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  42375. WOLFSSL_SUCCESS);
  42376. #endif
  42377. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  42378. WOLFSSL_SUCCESS);
  42379. #endif
  42380. #else
  42381. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  42382. #ifndef NO_WOLFSSL_CLIENT
  42383. #ifndef WOLFSSL_NO_TLS12
  42384. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  42385. NOT_COMPILED_IN);
  42386. #endif
  42387. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  42388. NOT_COMPILED_IN);
  42389. #endif
  42390. #endif
  42391. #if defined(HAVE_PQC)
  42392. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  42393. #ifndef NO_WOLFSSL_SERVER
  42394. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  42395. WOLFSSL_SUCCESS);
  42396. #endif
  42397. #ifndef NO_WOLFSSL_CLIENT
  42398. #ifndef WOLFSSL_NO_TLS12
  42399. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  42400. BAD_FUNC_ARG);
  42401. #endif
  42402. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  42403. WOLFSSL_SUCCESS);
  42404. #endif
  42405. #endif
  42406. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  42407. #ifndef NO_WOLFSSL_SERVER
  42408. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  42409. #endif
  42410. #ifndef NO_WOLFSSL_CLIENT
  42411. #ifndef WOLFSSL_NO_TLS12
  42412. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  42413. #endif
  42414. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  42415. #endif
  42416. #endif /* HAVE_SUPPORTED_CURVES */
  42417. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  42418. #ifndef NO_WOLFSSL_CLIENT
  42419. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  42420. #endif
  42421. #ifndef NO_WOLFSSL_SERVER
  42422. #ifndef WOLFSSL_NO_TLS12
  42423. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  42424. #endif
  42425. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  42426. #endif
  42427. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  42428. #ifndef NO_WOLFSSL_CLIENT
  42429. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  42430. #endif
  42431. #ifndef NO_WOLFSSL_SERVER
  42432. #ifndef WOLFSSL_NO_TLS12
  42433. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  42434. #endif
  42435. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  42436. #endif
  42437. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  42438. #ifndef NO_WOLFSSL_CLIENT
  42439. #ifndef WOLFSSL_NO_TLS12
  42440. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  42441. #endif
  42442. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  42443. #endif
  42444. #ifndef NO_WOLFSSL_SERVER
  42445. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  42446. #endif
  42447. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  42448. #ifndef NO_WOLFSSL_CLIENT
  42449. #ifndef WOLFSSL_NO_TLS12
  42450. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  42451. #endif
  42452. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  42453. #endif
  42454. #ifndef NO_WOLFSSL_SERVER
  42455. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  42456. #endif
  42457. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  42458. #ifndef NO_WOLFSSL_CLIENT
  42459. #ifndef WOLFSSL_NO_TLS12
  42460. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  42461. #endif
  42462. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  42463. #endif
  42464. #ifndef NO_WOLFSSL_SERVER
  42465. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  42466. #endif
  42467. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  42468. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  42469. #ifndef NO_WOLFSSL_CLIENT
  42470. #ifndef WOLFSSL_NO_TLS12
  42471. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  42472. BAD_FUNC_ARG);
  42473. #endif
  42474. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  42475. #endif
  42476. #ifndef NO_WOLFSSL_SERVER
  42477. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  42478. #endif
  42479. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  42480. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  42481. #ifndef NO_WOLFSSL_SERVER
  42482. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  42483. #endif
  42484. #ifndef NO_WOLFSSL_CLIENT
  42485. #ifndef WOLFSSL_NO_TLS12
  42486. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  42487. BAD_FUNC_ARG);
  42488. #endif
  42489. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  42490. #endif
  42491. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  42492. #ifndef NO_WOLFSSL_SERVER
  42493. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  42494. #endif
  42495. #ifndef NO_WOLFSSL_CLIENT
  42496. #ifndef WOLFSSL_NO_TLS12
  42497. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  42498. BAD_FUNC_ARG);
  42499. #endif
  42500. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  42501. #endif
  42502. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  42503. #ifndef NO_WOLFSSL_CLIENT
  42504. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  42505. #endif
  42506. #ifndef NO_WOLFSSL_SERVER
  42507. #ifndef WOLFSSL_NO_TLS12
  42508. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  42509. BAD_FUNC_ARG);
  42510. #endif
  42511. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  42512. #endif
  42513. #endif
  42514. #ifdef HAVE_ECC
  42515. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  42516. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  42517. #ifndef NO_WOLFSSL_SERVER
  42518. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  42519. #endif
  42520. #ifndef NO_WOLFSSL_CLIENT
  42521. #ifndef WOLFSSL_NO_TLS12
  42522. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  42523. #endif
  42524. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  42525. #endif
  42526. #endif
  42527. #ifdef HAVE_SUPPORTED_CURVES
  42528. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  42529. #ifndef NO_WOLFSSL_CLIENT
  42530. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  42531. #endif
  42532. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  42533. #ifndef NO_WOLFSSL_CLIENT
  42534. #ifndef WOLFSSL_NO_TLS12
  42535. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  42536. BAD_FUNC_ARG);
  42537. #endif
  42538. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  42539. WOLFSSL_MAX_GROUP_COUNT + 1),
  42540. BAD_FUNC_ARG);
  42541. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  42542. WOLFSSL_SUCCESS);
  42543. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  42544. BAD_FUNC_ARG);
  42545. #endif
  42546. #ifndef NO_WOLFSSL_SERVER
  42547. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  42548. WOLFSSL_SUCCESS);
  42549. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  42550. BAD_FUNC_ARG);
  42551. #endif
  42552. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  42553. #ifndef NO_WOLFSSL_CLIENT
  42554. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  42555. #endif
  42556. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  42557. #ifndef NO_WOLFSSL_CLIENT
  42558. #ifndef WOLFSSL_NO_TLS12
  42559. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  42560. BAD_FUNC_ARG);
  42561. #endif
  42562. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  42563. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  42564. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  42565. WOLFSSL_SUCCESS);
  42566. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  42567. BAD_FUNC_ARG);
  42568. #endif
  42569. #ifndef NO_WOLFSSL_SERVER
  42570. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  42571. WOLFSSL_SUCCESS);
  42572. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  42573. BAD_FUNC_ARG);
  42574. #endif
  42575. #ifdef OPENSSL_EXTRA
  42576. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  42577. #ifndef NO_WOLFSSL_CLIENT
  42578. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  42579. #endif
  42580. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  42581. #ifndef NO_WOLFSSL_CLIENT
  42582. #ifndef WOLFSSL_NO_TLS12
  42583. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  42584. WOLFSSL_FAILURE);
  42585. #endif
  42586. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  42587. WOLFSSL_SUCCESS);
  42588. #endif
  42589. #ifndef NO_WOLFSSL_SERVER
  42590. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  42591. WOLFSSL_SUCCESS);
  42592. #endif
  42593. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  42594. #ifndef NO_WOLFSSL_CLIENT
  42595. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  42596. #endif
  42597. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  42598. #ifndef NO_WOLFSSL_CLIENT
  42599. #ifndef WOLFSSL_NO_TLS12
  42600. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  42601. WOLFSSL_FAILURE);
  42602. #endif
  42603. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  42604. WOLFSSL_SUCCESS);
  42605. #endif
  42606. #ifndef NO_WOLFSSL_SERVER
  42607. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  42608. WOLFSSL_SUCCESS);
  42609. #endif
  42610. #endif /* OPENSSL_EXTRA */
  42611. #endif /* HAVE_SUPPORTED_CURVES */
  42612. #endif /* HAVE_ECC */
  42613. #ifdef WOLFSSL_EARLY_DATA
  42614. #ifndef OPENSSL_EXTRA
  42615. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  42616. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  42617. #else
  42618. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  42619. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  42620. #endif
  42621. #ifndef NO_WOLFSSL_CLIENT
  42622. #ifndef OPENSSL_EXTRA
  42623. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  42624. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  42625. #else
  42626. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  42627. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  42628. #endif
  42629. #endif
  42630. #ifndef NO_WOLFSSL_SERVER
  42631. #ifndef WOLFSSL_NO_TLS12
  42632. #ifndef OPENSSL_EXTRA
  42633. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  42634. BAD_FUNC_ARG);
  42635. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  42636. #else
  42637. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  42638. BAD_FUNC_ARG);
  42639. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  42640. #endif
  42641. #endif
  42642. #ifndef OPENSSL_EXTRA
  42643. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  42644. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  42645. #else
  42646. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  42647. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  42648. #endif
  42649. #endif
  42650. #ifndef OPENSSL_EXTRA
  42651. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  42652. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  42653. #else
  42654. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  42655. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  42656. #endif
  42657. #ifndef NO_WOLFSSL_CLIENT
  42658. #ifndef OPENSSL_EXTRA
  42659. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  42660. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), SIDE_ERROR);
  42661. #else
  42662. AssertIntEQ(SSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  42663. AssertIntEQ(SSL_get_max_early_data(clientSsl), SIDE_ERROR);
  42664. #endif
  42665. #endif
  42666. #ifndef NO_WOLFSSL_SERVER
  42667. #ifndef WOLFSSL_NO_TLS12
  42668. #ifndef OPENSSL_EXTRA
  42669. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  42670. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  42671. #else
  42672. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  42673. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  42674. #endif
  42675. #endif
  42676. #ifndef OPENSSL_EXTRA
  42677. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  42678. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  42679. #else
  42680. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  42681. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  42682. #endif
  42683. #endif
  42684. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  42685. &outSz), BAD_FUNC_ARG);
  42686. #ifndef NO_WOLFSSL_CLIENT
  42687. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  42688. &outSz), BAD_FUNC_ARG);
  42689. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  42690. BAD_FUNC_ARG);
  42691. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  42692. sizeof(earlyData), NULL),
  42693. BAD_FUNC_ARG);
  42694. #endif
  42695. #ifndef NO_WOLFSSL_SERVER
  42696. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  42697. sizeof(earlyData), &outSz),
  42698. SIDE_ERROR);
  42699. #endif
  42700. #ifndef NO_WOLFSSL_CLIENT
  42701. #ifndef WOLFSSL_NO_TLS12
  42702. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  42703. sizeof(earlyData), &outSz),
  42704. BAD_FUNC_ARG);
  42705. #endif
  42706. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  42707. sizeof(earlyData), &outSz),
  42708. WOLFSSL_FATAL_ERROR);
  42709. #endif
  42710. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  42711. sizeof(earlyDataBuffer), &outSz),
  42712. BAD_FUNC_ARG);
  42713. #ifndef NO_WOLFSSL_SERVER
  42714. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  42715. sizeof(earlyDataBuffer), &outSz),
  42716. BAD_FUNC_ARG);
  42717. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  42718. BAD_FUNC_ARG);
  42719. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  42720. sizeof(earlyDataBuffer), NULL),
  42721. BAD_FUNC_ARG);
  42722. #endif
  42723. #ifndef NO_WOLFSSL_CLIENT
  42724. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  42725. sizeof(earlyDataBuffer), &outSz),
  42726. SIDE_ERROR);
  42727. #endif
  42728. #ifndef NO_WOLFSSL_SERVER
  42729. #ifndef WOLFSSL_NO_TLS12
  42730. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  42731. sizeof(earlyDataBuffer), &outSz),
  42732. BAD_FUNC_ARG);
  42733. #endif
  42734. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  42735. sizeof(earlyDataBuffer), &outSz),
  42736. WOLFSSL_FATAL_ERROR);
  42737. #endif
  42738. #endif
  42739. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  42740. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  42741. #endif
  42742. #ifndef NO_WOLFSSL_SERVER
  42743. wolfSSL_free(serverSsl);
  42744. wolfSSL_CTX_free(serverCtx);
  42745. #endif
  42746. #ifndef NO_WOLFSSL_CLIENT
  42747. wolfSSL_free(clientSsl);
  42748. wolfSSL_CTX_free(clientCtx);
  42749. #endif
  42750. #ifndef WOLFSSL_NO_TLS12
  42751. #ifndef NO_WOLFSSL_SERVER
  42752. wolfSSL_free(serverTls12Ssl);
  42753. wolfSSL_CTX_free(serverTls12Ctx);
  42754. #endif
  42755. #ifndef NO_WOLFSSL_CLIENT
  42756. wolfSSL_free(clientTls12Ssl);
  42757. wolfSSL_CTX_free(clientTls12Ctx);
  42758. #endif
  42759. #endif
  42760. return 0;
  42761. }
  42762. #endif
  42763. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  42764. !defined(NO_OLD_TLS))
  42765. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  42766. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  42767. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  42768. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  42769. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  42770. const unsigned char* priv, unsigned int privSz,
  42771. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  42772. unsigned char* out, unsigned int* outlen,
  42773. void* ctx)
  42774. {
  42775. int result;
  42776. /* Test fail when context associated with WOLFSSL is NULL */
  42777. if (ctx == NULL) {
  42778. return -1;
  42779. }
  42780. (void)ssl;
  42781. /* return 0 on success */
  42782. PRIVATE_KEY_UNLOCK();
  42783. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  42784. PRIVATE_KEY_LOCK();
  42785. return result;
  42786. }
  42787. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  42788. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  42789. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  42790. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  42791. WOLFSSL_SUCCESS);
  42792. #endif
  42793. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  42794. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  42795. WOLFSSL_SUCCESS);
  42796. #endif
  42797. }
  42798. static void test_dh_ssl_setup(WOLFSSL* ssl)
  42799. {
  42800. static int dh_test_ctx = 1;
  42801. int ret;
  42802. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  42803. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  42804. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  42805. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  42806. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42807. }
  42808. }
  42809. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  42810. {
  42811. int ret;
  42812. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  42813. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  42814. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  42815. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  42816. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42817. }
  42818. }
  42819. #endif
  42820. static int test_DhCallbacks(void)
  42821. {
  42822. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  42823. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  42824. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  42825. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  42826. WOLFSSL_CTX *ctx;
  42827. WOLFSSL *ssl;
  42828. tcp_ready ready;
  42829. func_args server_args;
  42830. func_args client_args;
  42831. THREAD_TYPE serverThread;
  42832. callback_functions func_cb_client;
  42833. callback_functions func_cb_server;
  42834. int test;
  42835. printf(testingFmt, "test_DhCallbacks");
  42836. #ifndef NO_WOLFSSL_CLIENT
  42837. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42838. #else
  42839. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  42840. #endif
  42841. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  42842. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  42843. /* load client ca cert */
  42844. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  42845. WOLFSSL_SUCCESS);
  42846. /* test with NULL arguments */
  42847. wolfSSL_SetDhAgreeCtx(NULL, &test);
  42848. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  42849. /* test success case */
  42850. test = 1;
  42851. AssertNotNull(ssl = wolfSSL_new(ctx));
  42852. wolfSSL_SetDhAgreeCtx(ssl, &test);
  42853. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  42854. wolfSSL_free(ssl);
  42855. wolfSSL_CTX_free(ctx);
  42856. /* test a connection where callback is used */
  42857. #ifdef WOLFSSL_TIRTOS
  42858. fdOpenSession(Task_self());
  42859. #endif
  42860. XMEMSET(&server_args, 0, sizeof(func_args));
  42861. XMEMSET(&client_args, 0, sizeof(func_args));
  42862. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  42863. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  42864. StartTCP();
  42865. InitTcpReady(&ready);
  42866. #if defined(USE_WINDOWS_API)
  42867. /* use RNG to get random port if using windows */
  42868. ready.port = GetRandomPort();
  42869. #endif
  42870. server_args.signal = &ready;
  42871. client_args.signal = &ready;
  42872. server_args.return_code = TEST_FAIL;
  42873. client_args.return_code = TEST_FAIL;
  42874. /* set callbacks to use DH functions */
  42875. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  42876. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  42877. #ifndef WOLFSSL_NO_TLS12
  42878. func_cb_client.method = wolfTLSv1_2_client_method;
  42879. #else
  42880. func_cb_client.method = wolfTLSv1_3_client_method;
  42881. #endif
  42882. client_args.callbacks = &func_cb_client;
  42883. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  42884. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  42885. #ifndef WOLFSSL_NO_TLS12
  42886. func_cb_server.method = wolfTLSv1_2_server_method;
  42887. #else
  42888. func_cb_server.method = wolfTLSv1_3_server_method;
  42889. #endif
  42890. server_args.callbacks = &func_cb_server;
  42891. start_thread(test_server_nofail, &server_args, &serverThread);
  42892. wait_tcp_ready(&server_args);
  42893. test_client_nofail(&client_args, NULL);
  42894. join_thread(serverThread);
  42895. AssertTrue(client_args.return_code);
  42896. AssertTrue(server_args.return_code);
  42897. FreeTcpReady(&ready);
  42898. #ifdef WOLFSSL_TIRTOS
  42899. fdOpenSession(Task_self());
  42900. #endif
  42901. /* now set user ctx to not be 1 so that the callback returns fail case */
  42902. #ifdef WOLFSSL_TIRTOS
  42903. fdOpenSession(Task_self());
  42904. #endif
  42905. XMEMSET(&server_args, 0, sizeof(func_args));
  42906. XMEMSET(&client_args, 0, sizeof(func_args));
  42907. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  42908. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  42909. StartTCP();
  42910. InitTcpReady(&ready);
  42911. #if defined(USE_WINDOWS_API)
  42912. /* use RNG to get random port if using windows */
  42913. ready.port = GetRandomPort();
  42914. #endif
  42915. server_args.signal = &ready;
  42916. client_args.signal = &ready;
  42917. server_args.return_code = TEST_FAIL;
  42918. client_args.return_code = TEST_FAIL;
  42919. /* set callbacks to use DH functions */
  42920. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  42921. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  42922. #ifndef WOLFSSL_NO_TLS12
  42923. func_cb_client.method = wolfTLSv1_2_client_method;
  42924. #else
  42925. func_cb_client.method = wolfTLSv1_3_client_method;
  42926. #endif
  42927. client_args.callbacks = &func_cb_client;
  42928. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  42929. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  42930. #ifndef WOLFSSL_NO_TLS12
  42931. func_cb_server.method = wolfTLSv1_2_server_method;
  42932. #else
  42933. func_cb_server.method = wolfTLSv1_3_server_method;
  42934. #endif
  42935. server_args.callbacks = &func_cb_server;
  42936. start_thread(test_server_nofail, &server_args, &serverThread);
  42937. wait_tcp_ready(&server_args);
  42938. test_client_nofail(&client_args, NULL);
  42939. join_thread(serverThread);
  42940. AssertIntEQ(client_args.return_code, TEST_FAIL);
  42941. AssertIntEQ(server_args.return_code, TEST_FAIL);
  42942. FreeTcpReady(&ready);
  42943. #ifdef WOLFSSL_TIRTOS
  42944. fdOpenSession(Task_self());
  42945. #endif
  42946. printf(resultFmt, passed);
  42947. #endif
  42948. return 0;
  42949. }
  42950. #endif /* HAVE_PK_CALLBACKS */
  42951. #ifdef HAVE_HASHDRBG
  42952. #ifdef TEST_RESEED_INTERVAL
  42953. static int test_wc_RNG_GenerateBlock_Reseed(void)
  42954. {
  42955. int i, ret;
  42956. WC_RNG rng;
  42957. byte key[32];
  42958. ret = wc_InitRng(&rng);
  42959. if (ret == 0) {
  42960. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  42961. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  42962. if (ret != 0) {
  42963. break;
  42964. }
  42965. }
  42966. }
  42967. wc_FreeRng(&rng);
  42968. return ret;
  42969. }
  42970. #endif /* TEST_RESEED_INTERVAL */
  42971. static int test_wc_RNG_GenerateBlock(void)
  42972. {
  42973. int i, ret;
  42974. WC_RNG rng;
  42975. byte key[32];
  42976. ret = wc_InitRng(&rng);
  42977. if (ret == 0) {
  42978. for(i = 0; i < 10; i++) {
  42979. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  42980. if (ret != 0) {
  42981. break;
  42982. }
  42983. }
  42984. }
  42985. wc_FreeRng(&rng);
  42986. (void)rng; /* for WC_NO_RNG case */
  42987. (void)key;
  42988. return ret;
  42989. }
  42990. #endif
  42991. /*
  42992. * Testing get_rand_digit
  42993. */
  42994. static int test_get_rand_digit(void)
  42995. {
  42996. int ret = 0;
  42997. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  42998. WC_RNG rng;
  42999. mp_digit d;
  43000. printf(testingFmt, "get_rand_digit()");
  43001. ret = wc_InitRng(&rng);
  43002. if (ret == 0) {
  43003. ret = get_rand_digit(&rng, &d);
  43004. }
  43005. if (ret == 0) {
  43006. ret = get_rand_digit(NULL, NULL);
  43007. if (ret == BAD_FUNC_ARG) {
  43008. ret = 0;
  43009. }
  43010. }
  43011. if (ret == 0) {
  43012. ret = get_rand_digit(NULL, &d);
  43013. if (ret == BAD_FUNC_ARG) {
  43014. ret = 0;
  43015. }
  43016. }
  43017. if (ret == 0) {
  43018. ret = get_rand_digit(&rng, NULL);
  43019. if (ret == BAD_FUNC_ARG) {
  43020. ret = 0;
  43021. }
  43022. }
  43023. if (ret == 0) {
  43024. ret = wc_FreeRng(&rng);
  43025. }
  43026. printf(resultFmt, ret == 0 ? passed : failed);
  43027. fflush(stdout);
  43028. #endif
  43029. return ret;
  43030. }/* End test_get_rand_digit*/
  43031. /*
  43032. * Testing get_digit_count
  43033. */
  43034. static int test_get_digit_count(void)
  43035. {
  43036. int ret = 0;
  43037. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  43038. mp_int a;
  43039. printf(testingFmt, "get_digit_count()");
  43040. if (mp_init(&a) != MP_OKAY) {
  43041. ret = -1;
  43042. }
  43043. if (ret == 0) {
  43044. ret = get_digit_count(NULL);
  43045. }
  43046. if (ret == 0) {
  43047. ret = get_digit_count(&a);
  43048. }
  43049. printf(resultFmt, ret == 0 ? passed : failed);
  43050. fflush(stdout);
  43051. mp_clear(&a);
  43052. #endif
  43053. return ret;
  43054. }/* End test_get_digit_count*/
  43055. /*
  43056. * Testing mp_cond_copy
  43057. */
  43058. static int test_mp_cond_copy(void)
  43059. {
  43060. int ret = 0;
  43061. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  43062. defined(WOLFSSL_PUBLIC_MP)
  43063. mp_int a;
  43064. mp_int b;
  43065. int copy = 0;
  43066. printf(testingFmt, "mp_cond_copy()");
  43067. if (mp_init(&a) != MP_OKAY) {
  43068. ret = -1;
  43069. }
  43070. if (ret == 0) {
  43071. if (mp_init(&b) != MP_OKAY) {
  43072. ret = -1;
  43073. }
  43074. }
  43075. if (ret == 0) {
  43076. ret = mp_cond_copy(NULL, copy, NULL);
  43077. if (ret == BAD_FUNC_ARG) {
  43078. ret = 0;
  43079. }
  43080. }
  43081. if (ret == 0) {
  43082. ret = mp_cond_copy(NULL, copy, &b);
  43083. if (ret == BAD_FUNC_ARG) {
  43084. ret = 0;
  43085. }
  43086. }
  43087. if (ret == 0) {
  43088. ret = mp_cond_copy(&a, copy, NULL);
  43089. if (ret == BAD_FUNC_ARG) {
  43090. ret = 0;
  43091. }
  43092. }
  43093. if (ret == 0) {
  43094. ret = mp_cond_copy(&a, copy, &b);
  43095. }
  43096. printf(resultFmt, ret == 0 ? passed : failed);
  43097. fflush(stdout);
  43098. mp_clear(&a);
  43099. mp_clear(&b);
  43100. #endif
  43101. return ret;
  43102. }/* End test_mp_cond_copy*/
  43103. /*
  43104. * Testing mp_rand
  43105. */
  43106. static int test_mp_rand(void)
  43107. {
  43108. int ret = 0;
  43109. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  43110. mp_int a;
  43111. int digits = 1;
  43112. WC_RNG rng;
  43113. printf(testingFmt, "mp_rand()");
  43114. if (mp_init(&a) != MP_OKAY) {
  43115. ret = -1;
  43116. }
  43117. if (ret == 0) {
  43118. ret = wc_InitRng(&rng);
  43119. }
  43120. if (ret == 0) {
  43121. ret = mp_rand(&a, digits, NULL);
  43122. if (ret == MISSING_RNG_E) {
  43123. ret = 0;
  43124. }
  43125. }
  43126. if (ret == 0) {
  43127. ret = mp_rand(NULL, digits, &rng);
  43128. if (ret == BAD_FUNC_ARG) {
  43129. ret = 0;
  43130. }
  43131. }
  43132. if (ret == 0) {
  43133. ret = mp_rand(&a, 0, &rng);
  43134. if (ret == BAD_FUNC_ARG) {
  43135. ret = 0;
  43136. }
  43137. }
  43138. if (ret == 0) {
  43139. ret = mp_rand(&a, digits, &rng);
  43140. }
  43141. printf(resultFmt, ret == 0 ? passed : failed);
  43142. fflush(stdout);
  43143. mp_clear(&a);
  43144. wc_FreeRng(&rng);
  43145. #endif
  43146. return ret;
  43147. }/* End test_mp_rand*/
  43148. /*
  43149. * Testing get_digit
  43150. */
  43151. static int test_get_digit(void)
  43152. {
  43153. int ret = 0;
  43154. #if defined(WOLFSSL_PUBLIC_MP)
  43155. mp_int a;
  43156. int n = 0;
  43157. printf(testingFmt, "get_digit()");
  43158. if (mp_init(&a) != MP_OKAY) {
  43159. ret = -1;
  43160. }
  43161. if (ret == 0) {
  43162. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  43163. ret = -1;
  43164. }
  43165. }
  43166. if (ret == 0) {
  43167. if (get_digit(NULL, n) == 0) { /* Should hit this */
  43168. ret = 0;
  43169. }
  43170. }
  43171. if (ret == 0) {
  43172. if (get_digit(&a, n) != 0) { /* Should not hit this */
  43173. ret = -1;
  43174. }
  43175. }
  43176. if (ret == 0) {
  43177. if (get_digit(&a, n) == 0) { /* Should hit this */
  43178. ret = 0;
  43179. }
  43180. }
  43181. printf(resultFmt, ret == 0 ? passed : failed);
  43182. fflush(stdout);
  43183. mp_clear(&a);
  43184. #endif
  43185. return ret;
  43186. }/* End test_get_digit*/
  43187. /*
  43188. * Testing wc_export_int
  43189. */
  43190. static int test_wc_export_int(void)
  43191. {
  43192. int ret = 0;
  43193. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  43194. defined(WOLFSSL_PUBLIC_MP)
  43195. mp_int mp;
  43196. byte buf[32];
  43197. word32 keySz = (word32)sizeof(buf);
  43198. word32 len = (word32)sizeof(buf);
  43199. printf(testingFmt, "wc_export_int()");
  43200. if (mp_init(&mp) != MP_OKAY) {
  43201. ret = -1;
  43202. }
  43203. if (ret == 0) {
  43204. ret = mp_set(&mp, 1234);
  43205. }
  43206. if (ret == 0) {
  43207. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  43208. if (ret == BAD_FUNC_ARG) {
  43209. ret = 0;
  43210. }
  43211. }
  43212. if (ret == 0) {
  43213. len = sizeof(buf)-1;
  43214. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  43215. if (ret == BUFFER_E) {
  43216. ret = 0;
  43217. }
  43218. }
  43219. if (ret == 0) {
  43220. len = sizeof(buf);
  43221. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  43222. }
  43223. if (ret == 0) {
  43224. len = 4; /* test input too small */
  43225. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  43226. if (ret == BUFFER_E) {
  43227. ret = 0;
  43228. }
  43229. }
  43230. if (ret == 0) {
  43231. len = sizeof(buf);
  43232. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  43233. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  43234. if (ret == 0 && len != 5) {
  43235. ret = BAD_FUNC_ARG;
  43236. }
  43237. }
  43238. printf(resultFmt, ret == 0 ? passed : failed);
  43239. fflush(stdout);
  43240. mp_clear(&mp);
  43241. #endif
  43242. return ret;
  43243. }/* End test_wc_export_int*/
  43244. static int test_wc_InitRngNonce(void)
  43245. {
  43246. int ret=0;
  43247. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  43248. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  43249. WC_RNG rng;
  43250. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  43251. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  43252. word32 nonceSz = sizeof(nonce);
  43253. printf(testingFmt, "wc_InitRngNonce()");
  43254. if (ret == 0){
  43255. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  43256. }
  43257. wc_FreeRng(&rng);
  43258. printf(resultFmt, ret == 0 ? passed : failed);
  43259. fflush(stdout);
  43260. #endif
  43261. return ret;
  43262. }/* End test_wc_InitRngNonce*/
  43263. /*
  43264. * Testing wc_InitRngNonce_ex
  43265. */
  43266. static int test_wc_InitRngNonce_ex(void)
  43267. {
  43268. int ret=0;
  43269. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  43270. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  43271. WC_RNG rng;
  43272. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  43273. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  43274. word32 nonceSz = sizeof(nonce);
  43275. printf(testingFmt, "wc_InitRngNonce_ex()");
  43276. if (ret == 0){
  43277. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, devId);
  43278. }
  43279. wc_FreeRng(&rng);
  43280. printf(resultFmt, ret == 0 ? passed : failed);
  43281. fflush(stdout);
  43282. #endif
  43283. return ret;
  43284. }/*End test_wc_InitRngNonce_ex*/
  43285. static int test_wolfSSL_X509_CRL(void)
  43286. {
  43287. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  43288. X509_CRL *crl;
  43289. char pem[][100] = {
  43290. "./certs/crl/crl.pem",
  43291. "./certs/crl/crl2.pem",
  43292. "./certs/crl/caEccCrl.pem",
  43293. "./certs/crl/eccCliCRL.pem",
  43294. "./certs/crl/eccSrvCRL.pem",
  43295. ""
  43296. };
  43297. #ifndef NO_BIO
  43298. BIO *bio;
  43299. #endif
  43300. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  43301. char der[][100] = {
  43302. "./certs/crl/crl.der",
  43303. "./certs/crl/crl2.der",
  43304. ""};
  43305. #endif
  43306. XFILE fp;
  43307. int i;
  43308. printf(testingFmt, "test_wolfSSL_X509_CRL");
  43309. for (i = 0; pem[i][0] != '\0'; i++)
  43310. {
  43311. fp = XFOPEN(pem[i], "rb");
  43312. AssertTrue((fp != XBADFILE));
  43313. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  43314. AssertNotNull(crl);
  43315. X509_CRL_free(crl);
  43316. XFCLOSE(fp);
  43317. fp = XFOPEN(pem[i], "rb");
  43318. AssertTrue((fp != XBADFILE));
  43319. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  43320. AssertNotNull(crl);
  43321. X509_CRL_free(crl);
  43322. XFCLOSE(fp);
  43323. }
  43324. #ifndef NO_BIO
  43325. for (i = 0; pem[i][0] != '\0'; i++)
  43326. {
  43327. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  43328. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  43329. X509_CRL_free(crl);
  43330. BIO_free(bio);
  43331. }
  43332. #endif
  43333. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  43334. for(i = 0; der[i][0] != '\0'; i++){
  43335. fp = XFOPEN(der[i], "rb");
  43336. AssertTrue((fp != XBADFILE));
  43337. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  43338. AssertNotNull(crl);
  43339. X509_CRL_free(crl);
  43340. XFCLOSE(fp);
  43341. fp = XFOPEN(der[i], "rb");
  43342. AssertTrue((fp != XBADFILE));
  43343. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  43344. AssertNotNull(crl);
  43345. X509_CRL_free(crl);
  43346. XFCLOSE(fp);
  43347. }
  43348. #endif
  43349. printf(resultFmt, passed);
  43350. fflush(stdout);
  43351. #endif
  43352. return 0;
  43353. }
  43354. static int test_wolfSSL_X509_load_crl_file(void)
  43355. {
  43356. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  43357. !defined(NO_RSA) && !defined(NO_BIO)
  43358. int i;
  43359. char pem[][100] = {
  43360. "./certs/crl/crl.pem",
  43361. "./certs/crl/crl2.pem",
  43362. "./certs/crl/caEccCrl.pem",
  43363. "./certs/crl/eccCliCRL.pem",
  43364. "./certs/crl/eccSrvCRL.pem",
  43365. ""
  43366. };
  43367. char der[][100] = {
  43368. "./certs/crl/crl.der",
  43369. "./certs/crl/crl2.der",
  43370. ""
  43371. };
  43372. WOLFSSL_X509_STORE* store;
  43373. WOLFSSL_X509_LOOKUP* lookup;
  43374. printf(testingFmt, "wolfSSL_X509_load_crl_file");
  43375. AssertNotNull(store = wolfSSL_X509_STORE_new());
  43376. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  43377. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  43378. X509_FILETYPE_PEM), 1);
  43379. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  43380. X509_FILETYPE_PEM), 1);
  43381. if (store) {
  43382. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  43383. WOLFSSL_FILETYPE_PEM), 1);
  43384. /* since store hasn't yet known the revoked cert*/
  43385. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  43386. WOLFSSL_FILETYPE_PEM), 1);
  43387. }
  43388. for (i = 0; pem[i][0] != '\0'; i++)
  43389. {
  43390. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  43391. }
  43392. if (store) {
  43393. /* since store knows crl list */
  43394. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  43395. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  43396. }
  43397. /* once feeing store */
  43398. X509_STORE_free(store);
  43399. store = NULL;
  43400. AssertNotNull(store = wolfSSL_X509_STORE_new());
  43401. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  43402. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  43403. X509_FILETYPE_PEM), 1);
  43404. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  43405. X509_FILETYPE_PEM), 1);
  43406. if (store) {
  43407. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  43408. WOLFSSL_FILETYPE_PEM), 1);
  43409. /* since store hasn't yet known the revoked cert*/
  43410. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  43411. WOLFSSL_FILETYPE_PEM), 1);
  43412. }
  43413. for (i = 0; der[i][0] != '\0'; i++)
  43414. {
  43415. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  43416. }
  43417. if (store) {
  43418. /* since store knows crl list */
  43419. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  43420. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  43421. }
  43422. /* test for incorrect parameter */
  43423. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  43424. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  43425. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  43426. X509_STORE_free(store);
  43427. store = NULL;
  43428. printf(resultFmt, passed);
  43429. #endif
  43430. return 0;
  43431. }
  43432. static int test_wolfSSL_d2i_X509_REQ(void)
  43433. {
  43434. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  43435. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  43436. !defined(WOLFSSL_SP_MATH)
  43437. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  43438. * generated by libest
  43439. * ./certs/csr.attr.der contains sample attributes
  43440. * ./certs/csr.ext.der contains sample extensions */
  43441. const char* csrFile = "./certs/csr.signed.der";
  43442. const char* csrPopFile = "./certs/csr.attr.der";
  43443. const char* csrExtFile = "./certs/csr.ext.der";
  43444. /* ./certs/csr.dsa.pem is generated using
  43445. * openssl req -newkey dsa:certs/dsaparams.pem \
  43446. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  43447. * -outform PEM
  43448. * with the passphrase "wolfSSL"
  43449. */
  43450. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  43451. const char* csrDsaFile = "./certs/csr.dsa.pem";
  43452. XFILE f;
  43453. #endif
  43454. BIO* bio = NULL;
  43455. X509* req = NULL;
  43456. EVP_PKEY *pub_key = NULL;
  43457. {
  43458. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  43459. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  43460. /*
  43461. * Extract the public key from the CSR
  43462. */
  43463. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  43464. /*
  43465. * Verify the signature in the CSR
  43466. */
  43467. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43468. X509_free(req);
  43469. BIO_free(bio);
  43470. EVP_PKEY_free(pub_key);
  43471. }
  43472. {
  43473. #ifdef OPENSSL_ALL
  43474. X509_ATTRIBUTE* attr;
  43475. ASN1_TYPE *at;
  43476. #endif
  43477. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  43478. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  43479. /*
  43480. * Extract the public key from the CSR
  43481. */
  43482. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  43483. /*
  43484. * Verify the signature in the CSR
  43485. */
  43486. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43487. #ifdef OPENSSL_ALL
  43488. /*
  43489. * Obtain the challenge password from the CSR
  43490. */
  43491. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  43492. 1);
  43493. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  43494. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  43495. AssertNotNull(at->value.asn1_string);
  43496. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  43497. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  43498. #endif
  43499. X509_free(req);
  43500. BIO_free(bio);
  43501. EVP_PKEY_free(pub_key);
  43502. }
  43503. {
  43504. #ifdef OPENSSL_ALL
  43505. X509_ATTRIBUTE* attr;
  43506. ASN1_TYPE *at;
  43507. STACK_OF(X509_EXTENSION) *exts = NULL;
  43508. #endif
  43509. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  43510. /* This CSR contains an Extension Request attribute so
  43511. * we test extension parsing in a CSR attribute here. */
  43512. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  43513. /*
  43514. * Extract the public key from the CSR
  43515. */
  43516. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  43517. /*
  43518. * Verify the signature in the CSR
  43519. */
  43520. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43521. #ifdef OPENSSL_ALL
  43522. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  43523. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  43524. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  43525. /*
  43526. * Obtain the challenge password from the CSR
  43527. */
  43528. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  43529. 0);
  43530. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  43531. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  43532. AssertNotNull(at->value.asn1_string);
  43533. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  43534. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  43535. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  43536. #endif
  43537. X509_free(req);
  43538. BIO_free(bio);
  43539. EVP_PKEY_free(pub_key);
  43540. }
  43541. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  43542. {
  43543. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  43544. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  43545. /*
  43546. * Extract the public key from the CSR
  43547. */
  43548. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  43549. /*
  43550. * Verify the signature in the CSR
  43551. */
  43552. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43553. X509_free(req);
  43554. BIO_free(bio);
  43555. /* Run the same test, but with a file pointer instead of a BIO.
  43556. * (PEM_read_X509_REQ)*/
  43557. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  43558. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  43559. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43560. X509_free(req);
  43561. EVP_PKEY_free(pub_key);
  43562. }
  43563. #endif /* !NO_DSA && !HAVE_SELFTEST */
  43564. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  43565. return 0;
  43566. }
  43567. static int test_wolfSSL_PEM_read_X509(void)
  43568. {
  43569. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  43570. !defined(NO_RSA)
  43571. X509 *x509 = NULL;
  43572. XFILE fp;
  43573. printf(testingFmt, "wolfSSL_PEM_read_X509");
  43574. fp = XFOPEN(svrCertFile, "rb");
  43575. AssertTrue((fp != XBADFILE));
  43576. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  43577. X509_free(x509);
  43578. XFCLOSE(fp);
  43579. printf(resultFmt, passed);
  43580. #endif
  43581. return 0;
  43582. }
  43583. static int test_wolfSSL_PEM_read(void)
  43584. {
  43585. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43586. const char* filename = "./certs/server-keyEnc.pem";
  43587. XFILE fp;
  43588. char* name = NULL;
  43589. char* header = NULL;
  43590. byte* data = NULL;
  43591. long len;
  43592. EVP_CIPHER_INFO cipher;
  43593. WOLFSSL_BIO* bio;
  43594. byte* fileData;
  43595. size_t fileDataSz;
  43596. byte* out;
  43597. printf(testingFmt, "wolfSSL_PEM_read");
  43598. fp = XFOPEN(filename, "rb");
  43599. AssertTrue((fp != XBADFILE));
  43600. /* Fail cases. */
  43601. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  43602. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  43603. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  43604. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  43605. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  43606. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  43607. AssertIntGT(XSTRLEN(header), 0);
  43608. AssertIntGT(len, 0);
  43609. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  43610. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  43611. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  43612. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  43613. DYNAMIC_TYPE_TMP_BUFFER));
  43614. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  43615. XFCLOSE(fp);
  43616. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  43617. /* Fail cases. */
  43618. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  43619. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  43620. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  43621. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  43622. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  43623. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  43624. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  43625. /* Fail cases. */
  43626. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  43627. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  43628. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  43629. #ifndef NO_DES3
  43630. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  43631. #endif
  43632. /* Fail cases. */
  43633. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  43634. (void*)"yassl123"), WOLFSSL_FAILURE);
  43635. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  43636. (void*)"yassl123"), WOLFSSL_FAILURE);
  43637. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  43638. (void*)"yassl123"), WOLFSSL_FAILURE);
  43639. #if !defined(NO_DES3) && !defined(NO_MD5)
  43640. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  43641. (void*)"yassl123"), WOLFSSL_SUCCESS);
  43642. #endif
  43643. BIO_free(bio);
  43644. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43645. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43646. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43647. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43648. name = NULL;
  43649. header = NULL;
  43650. data = NULL;
  43651. fp = XFOPEN(svrKeyFile, "rb");
  43652. AssertTrue((fp != XBADFILE));
  43653. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  43654. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  43655. AssertIntEQ(XSTRLEN(header), 0);
  43656. AssertIntGT(len, 0);
  43657. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  43658. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  43659. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  43660. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  43661. DYNAMIC_TYPE_TMP_BUFFER));
  43662. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  43663. XFCLOSE(fp);
  43664. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  43665. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  43666. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  43667. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  43668. BIO_free(bio);
  43669. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43670. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43671. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43672. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43673. printf(resultFmt, passed);
  43674. #endif
  43675. return 0;
  43676. }
  43677. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  43678. {
  43679. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  43680. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  43681. const byte iv[12] = { 0 };
  43682. const byte key[16] = { 0 };
  43683. const byte cleartext[16] = { 0 };
  43684. const byte aad[] = {
  43685. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  43686. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  43687. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  43688. 0x4f
  43689. };
  43690. byte out1Part[16];
  43691. byte outTag1Part[16];
  43692. byte out2Part[16];
  43693. byte outTag2Part[16];
  43694. byte decryptBuf[16];
  43695. int len;
  43696. int tlen;
  43697. EVP_CIPHER_CTX* ctx = NULL;
  43698. printf(testingFmt, "wolfssl_EVP_aes_gcm_AAD_2_parts");
  43699. /* ENCRYPT */
  43700. /* Send AAD and data in 1 part */
  43701. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  43702. tlen = 0;
  43703. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  43704. 1);
  43705. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  43706. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  43707. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  43708. sizeof(cleartext)), 1);
  43709. tlen += len;
  43710. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  43711. tlen += len;
  43712. AssertIntEQ(tlen, sizeof(cleartext));
  43713. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  43714. outTag1Part), 1);
  43715. EVP_CIPHER_CTX_free(ctx);
  43716. /* DECRYPT */
  43717. /* Send AAD and data in 1 part */
  43718. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  43719. tlen = 0;
  43720. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  43721. 1);
  43722. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  43723. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  43724. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  43725. sizeof(cleartext)), 1);
  43726. tlen += len;
  43727. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  43728. outTag1Part), 1);
  43729. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  43730. tlen += len;
  43731. AssertIntEQ(tlen, sizeof(cleartext));
  43732. EVP_CIPHER_CTX_free(ctx);
  43733. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  43734. /* ENCRYPT */
  43735. /* Send AAD and data in 2 parts */
  43736. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  43737. tlen = 0;
  43738. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  43739. 1);
  43740. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  43741. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  43742. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  43743. 1);
  43744. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  43745. tlen += len;
  43746. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  43747. sizeof(cleartext) - 1), 1);
  43748. tlen += len;
  43749. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  43750. tlen += len;
  43751. AssertIntEQ(tlen, sizeof(cleartext));
  43752. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  43753. outTag2Part), 1);
  43754. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  43755. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  43756. EVP_CIPHER_CTX_free(ctx);
  43757. /* DECRYPT */
  43758. /* Send AAD and data in 2 parts */
  43759. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  43760. tlen = 0;
  43761. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  43762. 1);
  43763. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  43764. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  43765. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  43766. 1);
  43767. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  43768. tlen += len;
  43769. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  43770. sizeof(cleartext) - 1), 1);
  43771. tlen += len;
  43772. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  43773. outTag1Part), 1);
  43774. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  43775. tlen += len;
  43776. AssertIntEQ(tlen, sizeof(cleartext));
  43777. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  43778. /* Test AAD re-use */
  43779. EVP_CIPHER_CTX_free(ctx);
  43780. printf(resultFmt, passed);
  43781. #endif
  43782. return 0;
  43783. }
  43784. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  43785. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  43786. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  43787. {
  43788. /* Zero length plain text */
  43789. byte key[] = {
  43790. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43791. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43792. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43793. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  43794. }; /* align */
  43795. byte iv[] = {
  43796. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  43797. }; /* align */
  43798. byte plaintxt[1];
  43799. int ivSz = 12;
  43800. int plaintxtSz = 0;
  43801. unsigned char tag[16];
  43802. unsigned char tag_kat[] =
  43803. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  43804. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  43805. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  43806. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  43807. int ciphertxtSz = 0;
  43808. int decryptedtxtSz = 0;
  43809. int len = 0;
  43810. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  43811. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  43812. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  43813. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  43814. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  43815. plaintxtSz));
  43816. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  43817. ciphertxtSz += len;
  43818. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  43819. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  43820. AssertIntEQ(0, ciphertxtSz);
  43821. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  43822. EVP_CIPHER_CTX_init(de);
  43823. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  43824. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  43825. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  43826. decryptedtxtSz = len;
  43827. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  43828. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  43829. decryptedtxtSz += len;
  43830. AssertIntEQ(0, decryptedtxtSz);
  43831. EVP_CIPHER_CTX_free(en);
  43832. EVP_CIPHER_CTX_free(de);
  43833. return 0;
  43834. }
  43835. #endif
  43836. static int test_wolfssl_EVP_aes_gcm(void)
  43837. {
  43838. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  43839. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  43840. /* A 256 bit key, AES_128 will use the first 128 bit*/
  43841. byte *key = (byte*)"01234567890123456789012345678901";
  43842. /* A 128 bit IV */
  43843. byte *iv = (byte*)"0123456789012345";
  43844. int ivSz = AES_BLOCK_SIZE;
  43845. /* Message to be encrypted */
  43846. byte *plaintxt = (byte*)"for things to change you have to change";
  43847. /* Additional non-confidential data */
  43848. byte *aad = (byte*)"Don't spend major time on minor things.";
  43849. unsigned char tag[AES_BLOCK_SIZE] = {0};
  43850. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  43851. int aadSz = (int)XSTRLEN((char*)aad);
  43852. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  43853. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  43854. int ciphertxtSz = 0;
  43855. int decryptedtxtSz = 0;
  43856. int len = 0;
  43857. int i = 0;
  43858. EVP_CIPHER_CTX en[2];
  43859. EVP_CIPHER_CTX de[2];
  43860. printf(testingFmt, "wolfssl_EVP_aes_gcm");
  43861. for (i = 0; i < 2; i++) {
  43862. EVP_CIPHER_CTX_init(&en[i]);
  43863. if (i == 0) {
  43864. /* Default uses 96-bits IV length */
  43865. #ifdef WOLFSSL_AES_128
  43866. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  43867. #elif defined(WOLFSSL_AES_192)
  43868. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  43869. #elif defined(WOLFSSL_AES_256)
  43870. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  43871. #endif
  43872. }
  43873. else {
  43874. #ifdef WOLFSSL_AES_128
  43875. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  43876. #elif defined(WOLFSSL_AES_192)
  43877. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  43878. #elif defined(WOLFSSL_AES_256)
  43879. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  43880. #endif
  43881. /* non-default must to set the IV length first */
  43882. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  43883. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  43884. }
  43885. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  43886. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  43887. ciphertxtSz = len;
  43888. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  43889. ciphertxtSz += len;
  43890. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  43891. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  43892. EVP_CIPHER_CTX_init(&de[i]);
  43893. if (i == 0) {
  43894. /* Default uses 96-bits IV length */
  43895. #ifdef WOLFSSL_AES_128
  43896. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  43897. #elif defined(WOLFSSL_AES_192)
  43898. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  43899. #elif defined(WOLFSSL_AES_256)
  43900. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  43901. #endif
  43902. }
  43903. else {
  43904. #ifdef WOLFSSL_AES_128
  43905. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  43906. #elif defined(WOLFSSL_AES_192)
  43907. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  43908. #elif defined(WOLFSSL_AES_256)
  43909. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  43910. #endif
  43911. /* non-default must to set the IV length first */
  43912. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  43913. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  43914. }
  43915. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  43916. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  43917. decryptedtxtSz = len;
  43918. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  43919. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  43920. decryptedtxtSz += len;
  43921. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  43922. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  43923. /* modify tag*/
  43924. tag[AES_BLOCK_SIZE-1]+=0xBB;
  43925. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  43926. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  43927. /* fail due to wrong tag */
  43928. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  43929. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  43930. AssertIntEQ(0, len);
  43931. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  43932. }
  43933. test_wolfssl_EVP_aes_gcm_zeroLen();
  43934. printf(resultFmt, passed);
  43935. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  43936. return 0;
  43937. }
  43938. static int test_wolfssl_EVP_chacha20_poly1305(void)
  43939. {
  43940. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43941. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  43942. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  43943. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  43944. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  43945. byte cipherText[sizeof(plainText)];
  43946. byte decryptedText[sizeof(plainText)];
  43947. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  43948. EVP_CIPHER_CTX* ctx;
  43949. int outSz;
  43950. printf(testingFmt, "test_wolfssl_EVP_chacha20_poly1305");
  43951. /* Encrypt. */
  43952. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  43953. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  43954. NULL), WOLFSSL_SUCCESS);
  43955. /* Invalid IV length. */
  43956. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  43957. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  43958. /* Valid IV length. */
  43959. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  43960. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  43961. /* Invalid tag length. */
  43962. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  43963. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  43964. /* Valid tag length. */
  43965. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  43966. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  43967. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  43968. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  43969. WOLFSSL_SUCCESS);
  43970. AssertIntEQ(outSz, sizeof(aad));
  43971. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  43972. sizeof(plainText)), WOLFSSL_SUCCESS);
  43973. AssertIntEQ(outSz, sizeof(plainText));
  43974. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  43975. AssertIntEQ(outSz, 0);
  43976. /* Invalid tag length. */
  43977. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  43978. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  43979. /* Valid tag length. */
  43980. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  43981. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  43982. EVP_CIPHER_CTX_free(ctx);
  43983. /* Decrypt. */
  43984. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  43985. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  43986. NULL), WOLFSSL_SUCCESS);
  43987. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  43988. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  43989. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  43990. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  43991. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  43992. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  43993. WOLFSSL_SUCCESS);
  43994. AssertIntEQ(outSz, sizeof(aad));
  43995. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  43996. sizeof(cipherText)), WOLFSSL_SUCCESS);
  43997. AssertIntEQ(outSz, sizeof(cipherText));
  43998. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  43999. WOLFSSL_SUCCESS);
  44000. AssertIntEQ(outSz, 0);
  44001. EVP_CIPHER_CTX_free(ctx);
  44002. printf(resultFmt, passed);
  44003. #endif
  44004. return 0;
  44005. }
  44006. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  44007. {
  44008. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  44009. EVP_PKEY_CTX* ctx;
  44010. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  44011. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  44012. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  44013. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  44014. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  44015. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  44016. byte outKey[34];
  44017. size_t outKeySz = sizeof(outKey);
  44018. /* These expected outputs were gathered by running the same test below using
  44019. * OpenSSL. */
  44020. const byte extractAndExpand[] = {
  44021. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  44022. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  44023. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  44024. };
  44025. const byte extractOnly[] = {
  44026. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  44027. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  44028. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  44029. };
  44030. const byte expandOnly[] = {
  44031. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  44032. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  44033. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  44034. };
  44035. const byte extractAndExpandAddInfo[] = {
  44036. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  44037. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  44038. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  44039. };
  44040. printf(testingFmt, "test_wolfSSL_EVP_PKEY_hkdf");
  44041. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  44042. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  44043. /* NULL ctx. */
  44044. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  44045. /* NULL md. */
  44046. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  44047. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  44048. /* NULL ctx. */
  44049. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  44050. WOLFSSL_FAILURE);
  44051. /* NULL salt is ok. */
  44052. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  44053. WOLFSSL_SUCCESS);
  44054. /* Salt length <= 0. */
  44055. /* Length 0 salt is ok. */
  44056. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  44057. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  44058. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  44059. WOLFSSL_SUCCESS);
  44060. /* NULL ctx. */
  44061. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  44062. WOLFSSL_FAILURE);
  44063. /* NULL key. */
  44064. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  44065. WOLFSSL_FAILURE);
  44066. /* Key length <= 0 */
  44067. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  44068. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  44069. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  44070. WOLFSSL_SUCCESS);
  44071. /* NULL ctx. */
  44072. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  44073. WOLFSSL_FAILURE);
  44074. /* NULL info is ok. */
  44075. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  44076. WOLFSSL_SUCCESS);
  44077. /* Info length <= 0 */
  44078. /* Length 0 info is ok. */
  44079. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  44080. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  44081. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  44082. WOLFSSL_SUCCESS);
  44083. /* NULL ctx. */
  44084. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  44085. WOLFSSL_FAILURE);
  44086. /* Extract and expand (default). */
  44087. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  44088. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  44089. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  44090. /* Extract only. */
  44091. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  44092. WOLFSSL_SUCCESS);
  44093. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  44094. AssertIntEQ(outKeySz, sizeof(extractOnly));
  44095. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  44096. outKeySz = sizeof(outKey);
  44097. /* Expand only. */
  44098. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  44099. WOLFSSL_SUCCESS);
  44100. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  44101. AssertIntEQ(outKeySz, sizeof(expandOnly));
  44102. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  44103. outKeySz = sizeof(outKey);
  44104. /* Extract and expand with appended additional info. */
  44105. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  44106. WOLFSSL_SUCCESS);
  44107. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  44108. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  44109. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  44110. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  44111. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  44112. EVP_PKEY_CTX_free(ctx);
  44113. printf(resultFmt, passed);
  44114. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  44115. return 0;
  44116. }
  44117. #ifndef NO_BIO
  44118. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  44119. {
  44120. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44121. BIO* bio;
  44122. X509_INFO* info;
  44123. STACK_OF(X509_INFO)* sk;
  44124. char* subject;
  44125. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  44126. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  44127. printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
  44128. AssertNotNull(bio = BIO_new(BIO_s_file()));
  44129. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  44130. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  44131. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  44132. /* using dereference to maintain testing for Apache port*/
  44133. AssertNotNull(info = sk_X509_INFO_pop(sk));
  44134. AssertNotNull(subject =
  44135. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  44136. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  44137. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  44138. X509_INFO_free(info);
  44139. AssertNotNull(info = sk_X509_INFO_pop(sk));
  44140. AssertNotNull(subject =
  44141. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  44142. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  44143. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  44144. X509_INFO_free(info);
  44145. AssertNull(info = sk_X509_INFO_pop(sk));
  44146. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  44147. BIO_free(bio);
  44148. printf(resultFmt, passed);
  44149. #endif
  44150. return 0;
  44151. }
  44152. #endif /* !NO_BIO */
  44153. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  44154. {
  44155. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44156. X509 *x509;
  44157. X509_NAME* name;
  44158. int idx = 0;
  44159. X509_NAME_ENTRY *ne;
  44160. ASN1_OBJECT *object = NULL;
  44161. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
  44162. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  44163. AssertNotNull(x509);
  44164. name = X509_get_subject_name(x509);
  44165. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  44166. AssertIntGE(idx, 0);
  44167. ne = X509_NAME_get_entry(name, idx);
  44168. AssertNotNull(ne);
  44169. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  44170. X509_free(x509);
  44171. printf(resultFmt, passed);
  44172. #endif
  44173. return 0;
  44174. }
  44175. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  44176. {
  44177. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  44178. ASN1_INTEGER *a;
  44179. long val;
  44180. int ret;
  44181. printf(testingFmt, "test_wolfSSL_ASN1_INTEGER_get_set");
  44182. a = ASN1_INTEGER_new();
  44183. val = 0;
  44184. ret = ASN1_INTEGER_set(NULL, val);
  44185. AssertIntEQ(ret, 0);
  44186. ASN1_INTEGER_free(a);
  44187. /* 0 */
  44188. a = ASN1_INTEGER_new();
  44189. val = 0;
  44190. ret = ASN1_INTEGER_set(a, val);
  44191. AssertIntEQ(ret, 1);
  44192. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44193. ASN1_INTEGER_free(a);
  44194. /* 40 */
  44195. a = ASN1_INTEGER_new();
  44196. val = 40;
  44197. ret = ASN1_INTEGER_set(a, val);
  44198. AssertIntEQ(ret, 1);
  44199. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44200. ASN1_INTEGER_free(a);
  44201. /* -40 */
  44202. a = ASN1_INTEGER_new();
  44203. val = -40;
  44204. ret = ASN1_INTEGER_set(a, val);
  44205. AssertIntEQ(ret, 1);
  44206. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44207. ASN1_INTEGER_free(a);
  44208. /* 128 */
  44209. a = ASN1_INTEGER_new();
  44210. val = 128;
  44211. ret = ASN1_INTEGER_set(a, val);
  44212. AssertIntEQ(ret, 1);
  44213. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44214. ASN1_INTEGER_free(a);
  44215. /* -128 */
  44216. a = ASN1_INTEGER_new();
  44217. val = -128;
  44218. ret = ASN1_INTEGER_set(a, val);
  44219. AssertIntEQ(ret, 1);
  44220. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44221. ASN1_INTEGER_free(a);
  44222. /* 200 */
  44223. a = ASN1_INTEGER_new();
  44224. val = 200;
  44225. ret = ASN1_INTEGER_set(a, val);
  44226. AssertIntEQ(ret, 1);
  44227. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44228. ASN1_INTEGER_free(a);
  44229. /* int max (2147483647) */
  44230. a = ASN1_INTEGER_new();
  44231. val = 2147483647;
  44232. ret = ASN1_INTEGER_set(a, val);
  44233. AssertIntEQ(ret, 1);
  44234. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44235. ASN1_INTEGER_free(a);
  44236. /* int min (-2147483648) */
  44237. a = ASN1_INTEGER_new();
  44238. val = -2147483647 - 1;
  44239. ret = ASN1_INTEGER_set(a, val);
  44240. AssertIntEQ(ret, 1);
  44241. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44242. ASN1_INTEGER_free(a);
  44243. printf(resultFmt, passed);
  44244. #endif
  44245. return 0;
  44246. }
  44247. #if defined(OPENSSL_EXTRA)
  44248. typedef struct ASN1IntTestVector {
  44249. const byte* der;
  44250. const size_t derSz;
  44251. const long value;
  44252. } ASN1IntTestVector;
  44253. #endif
  44254. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  44255. {
  44256. #if defined(OPENSSL_EXTRA)
  44257. size_t i;
  44258. WOLFSSL_ASN1_INTEGER* a = NULL;
  44259. WOLFSSL_ASN1_INTEGER* b = NULL;
  44260. WOLFSSL_ASN1_INTEGER* c = NULL;
  44261. const byte* p = NULL;
  44262. byte* reEncoded = NULL;
  44263. int reEncodedSz;
  44264. static const byte zeroDer[] = {
  44265. 0x02, 0x01, 0x00
  44266. };
  44267. static const byte oneDer[] = {
  44268. 0x02, 0x01, 0x01
  44269. };
  44270. static const byte negativeDer[] = {
  44271. 0x02, 0x03, 0xC1, 0x16, 0x0D
  44272. };
  44273. static const byte positiveDer[] = {
  44274. 0x02, 0x03, 0x01, 0x00, 0x01
  44275. };
  44276. static const byte primeDer[] = {
  44277. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  44278. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  44279. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  44280. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  44281. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  44282. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  44283. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  44284. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  44285. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  44286. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  44287. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  44288. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  44289. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  44290. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  44291. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  44292. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  44293. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  44294. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  44295. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  44296. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  44297. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  44298. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  44299. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  44300. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  44301. };
  44302. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  44303. static const ASN1IntTestVector testVectors[] = {
  44304. {zeroDer, sizeof(zeroDer), 0},
  44305. {oneDer, sizeof(oneDer), 1},
  44306. {negativeDer, sizeof(negativeDer), -4123123},
  44307. {positiveDer, sizeof(positiveDer), 65537},
  44308. {primeDer, sizeof(primeDer), 0}
  44309. };
  44310. static const size_t NUM_TEST_VECTORS = sizeof(testVectors)/sizeof(testVectors[0]);
  44311. printf(testingFmt, "test_wolfSSL_d2i_ASN1_INTEGER");
  44312. /* Check d2i error conditions */
  44313. /* NULL pointer to input. */
  44314. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  44315. AssertNull(b);
  44316. /* NULL input. */
  44317. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  44318. AssertNull(b);
  44319. /* 0 length. */
  44320. p = testVectors[0].der;
  44321. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  44322. AssertNull(b);
  44323. /* Negative length. */
  44324. p = testVectors[0].der;
  44325. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  44326. AssertNull(b);
  44327. /* Garbage DER input. */
  44328. p = garbageDer;
  44329. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  44330. AssertNull(b);
  44331. {
  44332. /* Check i2d error conditions */
  44333. /* NULL input. */
  44334. byte* p2 = NULL;
  44335. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  44336. /* 0 length input data buffer (a->length == 0). */
  44337. AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  44338. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  44339. a->data = NULL;
  44340. /* NULL input data buffer. */
  44341. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  44342. /* Reset a->data. */
  44343. a->data = a->intData;
  44344. /* Set a to valid value. */
  44345. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  44346. /* NULL output buffer. */
  44347. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  44348. wolfSSL_ASN1_INTEGER_free(a);
  44349. }
  44350. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  44351. p = testVectors[i].der;
  44352. a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
  44353. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  44354. if (testVectors[i].derSz <= sizeof(long)) {
  44355. c = wolfSSL_ASN1_INTEGER_new();
  44356. wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
  44357. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  44358. wolfSSL_ASN1_INTEGER_free(c);
  44359. }
  44360. /* Convert to DER without a pre-allocated output buffer. */
  44361. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  44362. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  44363. AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  44364. /* Convert to DER with a pre-allocated output buffer. In this case, the
  44365. * output buffer pointer should be incremented just past the end of the
  44366. * encoded data. */
  44367. p = reEncoded;
  44368. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  44369. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  44370. AssertPtrEq(p, reEncoded - reEncodedSz);
  44371. AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
  44372. XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
  44373. reEncoded = NULL;
  44374. wolfSSL_ASN1_INTEGER_free(a);
  44375. }
  44376. printf(resultFmt, passed);
  44377. #endif /* OPENSSL_EXTRA */
  44378. return 0;
  44379. }
  44380. static int test_wolfSSL_X509_STORE_get1_certs(void)
  44381. {
  44382. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  44383. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44384. X509_STORE_CTX *storeCtx;
  44385. X509_STORE *store;
  44386. X509 *caX509;
  44387. X509 *svrX509;
  44388. X509_NAME *subject;
  44389. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  44390. printf(testingFmt, "wolfSSL_X509_STORE_get1_certs()");
  44391. AssertNotNull(caX509 =
  44392. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  44393. AssertNotNull((svrX509 =
  44394. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  44395. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  44396. AssertNotNull(store = X509_STORE_new());
  44397. AssertNotNull(subject = X509_get_subject_name(caX509));
  44398. /* Errors */
  44399. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  44400. AssertNull(X509_STORE_get1_certs(NULL, subject));
  44401. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  44402. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  44403. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  44404. /* Should find the cert */
  44405. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  44406. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  44407. sk_X509_pop_free(certs, NULL);
  44408. /* Should not find the cert */
  44409. AssertNotNull(subject = X509_get_subject_name(svrX509));
  44410. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  44411. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  44412. sk_X509_pop_free(certs, NULL);
  44413. X509_STORE_free(store);
  44414. X509_STORE_CTX_free(storeCtx);
  44415. X509_free(svrX509);
  44416. X509_free(caX509);
  44417. printf(resultFmt, passed);
  44418. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  44419. return 0;
  44420. }
  44421. /* Testing code used in dpp.c in hostap */
  44422. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44423. typedef struct {
  44424. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  44425. * as an OID identifying the curve */
  44426. X509_ALGOR *alg;
  44427. /* Compressed format public key per ANSI X9.63 */
  44428. ASN1_BIT_STRING *pub_key;
  44429. } DPP_BOOTSTRAPPING_KEY;
  44430. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  44431. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  44432. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  44433. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  44434. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  44435. #endif
  44436. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  44437. {
  44438. /* Testing code used in dpp.c in hostap */
  44439. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44440. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44441. EC_KEY *eckey;
  44442. EVP_PKEY *key;
  44443. size_t len;
  44444. unsigned char *der = NULL;
  44445. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  44446. const unsigned char *in = ecc_clikey_der_256;
  44447. const EC_GROUP *group;
  44448. const EC_POINT *point;
  44449. int nid;
  44450. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  44451. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  44452. (long)sizeof_ecc_clikey_der_256));
  44453. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  44454. AssertNotNull(group = EC_KEY_get0_group(eckey));
  44455. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  44456. nid = EC_GROUP_get_curve_name(group);
  44457. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  44458. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  44459. #ifdef HAVE_COMP_KEY
  44460. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  44461. NULL, 0, NULL)), 0);
  44462. #else
  44463. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  44464. NULL, 0, NULL)), 0);
  44465. #endif
  44466. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  44467. #ifdef HAVE_COMP_KEY
  44468. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  44469. der, len, NULL), len);
  44470. #else
  44471. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  44472. der, len, NULL), len);
  44473. #endif
  44474. bootstrap->pub_key->data = der;
  44475. bootstrap->pub_key->length = (int)len;
  44476. /* Not actually used */
  44477. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  44478. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  44479. der = NULL;
  44480. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  44481. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  44482. EVP_PKEY_free(key);
  44483. EC_KEY_free(eckey);
  44484. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  44485. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  44486. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  44487. return 0;
  44488. }
  44489. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  44490. {
  44491. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  44492. ASN1_INTEGER *a;
  44493. unsigned char *pp,*tpp;
  44494. int ret;
  44495. printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
  44496. a = wolfSSL_ASN1_INTEGER_new();
  44497. /* 40 */
  44498. a->intData[0] = ASN_INTEGER;
  44499. a->intData[1] = 1;
  44500. a->intData[2] = 40;
  44501. ret = i2c_ASN1_INTEGER(a, NULL);
  44502. AssertIntEQ(ret, 1);
  44503. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44504. DYNAMIC_TYPE_TMP_BUFFER));
  44505. tpp = pp;
  44506. XMEMSET(pp, 0, ret + 1);
  44507. i2c_ASN1_INTEGER(a, &pp);
  44508. pp--;
  44509. AssertIntEQ(*pp, 40);
  44510. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44511. /* 128 */
  44512. a->intData[0] = ASN_INTEGER;
  44513. a->intData[1] = 1;
  44514. a->intData[2] = 128;
  44515. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  44516. AssertIntEQ(ret, 2);
  44517. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44518. DYNAMIC_TYPE_TMP_BUFFER));
  44519. tpp = pp;
  44520. XMEMSET(pp, 0, ret + 1);
  44521. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  44522. pp--;
  44523. AssertIntEQ(*(pp--), 128);
  44524. AssertIntEQ(*pp, 0);
  44525. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44526. /* -40 */
  44527. a->intData[0] = ASN_INTEGER;
  44528. a->intData[1] = 1;
  44529. a->intData[2] = 40;
  44530. a->negative = 1;
  44531. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  44532. AssertIntEQ(ret, 1);
  44533. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44534. DYNAMIC_TYPE_TMP_BUFFER));
  44535. tpp = pp;
  44536. XMEMSET(pp, 0, ret + 1);
  44537. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  44538. pp--;
  44539. AssertIntEQ(*pp, 216);
  44540. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44541. /* -128 */
  44542. a->intData[0] = ASN_INTEGER;
  44543. a->intData[1] = 1;
  44544. a->intData[2] = 128;
  44545. a->negative = 1;
  44546. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  44547. AssertIntEQ(ret, 1);
  44548. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44549. DYNAMIC_TYPE_TMP_BUFFER));
  44550. tpp = pp;
  44551. XMEMSET(pp, 0, ret + 1);
  44552. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  44553. pp--;
  44554. AssertIntEQ(*pp, 128);
  44555. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44556. /* -200 */
  44557. a->intData[0] = ASN_INTEGER;
  44558. a->intData[1] = 1;
  44559. a->intData[2] = 200;
  44560. a->negative = 1;
  44561. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  44562. AssertIntEQ(ret, 2);
  44563. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44564. DYNAMIC_TYPE_TMP_BUFFER));
  44565. tpp = pp;
  44566. XMEMSET(pp, 0, ret + 1);
  44567. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  44568. pp--;
  44569. AssertIntEQ(*(pp--), 56);
  44570. AssertIntEQ(*pp, 255);
  44571. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44572. wolfSSL_ASN1_INTEGER_free(a);
  44573. printf(resultFmt, passed);
  44574. #endif /* OPENSSL_EXTRA && !NO_ASN */
  44575. return 0;
  44576. }
  44577. #ifndef NO_INLINE
  44578. #define WOLFSSL_MISC_INCLUDED
  44579. #include <wolfcrypt/src/misc.c>
  44580. #else
  44581. #include <wolfssl/wolfcrypt/misc.h>
  44582. #endif
  44583. static int test_ForceZero(void)
  44584. {
  44585. unsigned char data[32];
  44586. unsigned int i, j, len;
  44587. /* Test case with 0 length */
  44588. ForceZero(data, 0);
  44589. /* Test ForceZero */
  44590. for (i = 0; i < sizeof(data); i++) {
  44591. for (len = 1; len < sizeof(data) - i; len++) {
  44592. for (j = 0; j < sizeof(data); j++)
  44593. data[j] = j + 1;
  44594. ForceZero(data + i, len);
  44595. for (j = 0; j < sizeof(data); j++) {
  44596. if (j < i || j >= i + len) {
  44597. if (data[j] == 0x00)
  44598. return -10200;
  44599. }
  44600. else if (data[j] != 0x00)
  44601. return -10201;
  44602. }
  44603. }
  44604. }
  44605. return 0;
  44606. }
  44607. #ifndef NO_BIO
  44608. static int test_wolfSSL_X509_print(void)
  44609. {
  44610. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  44611. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  44612. X509 *x509;
  44613. BIO *bio;
  44614. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  44615. const X509_ALGOR *cert_sig_alg;
  44616. #endif
  44617. printf(testingFmt, "wolfSSL_X509_print");
  44618. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  44619. AssertNotNull(x509);
  44620. /* print to memory */
  44621. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  44622. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  44623. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  44624. /* Will print IP address subject alt name. */
  44625. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3240);
  44626. #else
  44627. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3218);
  44628. #endif
  44629. BIO_free(bio);
  44630. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  44631. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  44632. /* Print signature */
  44633. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  44634. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  44635. #endif
  44636. /* print to stdout */
  44637. #if !defined(NO_WOLFSSL_DIR)
  44638. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  44639. #endif
  44640. /* print again */
  44641. AssertIntEQ(X509_print_fp(stdout, x509), SSL_SUCCESS);
  44642. X509_free(x509);
  44643. BIO_free(bio);
  44644. printf(resultFmt, passed);
  44645. #endif
  44646. return 0;
  44647. }
  44648. static int test_wolfSSL_BIO_get_len(void)
  44649. {
  44650. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  44651. BIO *bio = NULL;
  44652. const char txt[] = "Some example text to push to the BIO.";
  44653. printf(testingFmt, "wolfSSL_BIO_get_len");
  44654. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  44655. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  44656. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  44657. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  44658. BIO_free(bio);
  44659. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  44660. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  44661. BIO_free(bio);
  44662. printf(resultFmt, passed);
  44663. #endif
  44664. return 0;
  44665. }
  44666. static int test_wolfSSL_ASN1_STRING_print(void){
  44667. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  44668. ASN1_STRING* asnStr = NULL;
  44669. const char HELLO_DATA[]= \
  44670. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  44671. #define MAX_UNPRINTABLE_CHAR 32
  44672. #define MAX_BUF 255
  44673. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  44674. unsigned char expected[sizeof(unprintableData)+1];
  44675. unsigned char rbuf[MAX_BUF];
  44676. BIO *bio;
  44677. int p_len, i;
  44678. printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
  44679. /* setup */
  44680. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  44681. unprintableData[i] = HELLO_DATA[i];
  44682. expected[i] = HELLO_DATA[i];
  44683. }
  44684. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  44685. unprintableData[sizeof(HELLO_DATA)+i] = i;
  44686. if (i == (int)'\n' || i == (int)'\r')
  44687. expected[sizeof(HELLO_DATA)+i] = i;
  44688. else
  44689. expected[sizeof(HELLO_DATA)+i] = '.';
  44690. }
  44691. unprintableData[sizeof(unprintableData)-1] = '\0';
  44692. expected[sizeof(expected)-1] = '\0';
  44693. XMEMSET(rbuf, 0, MAX_BUF);
  44694. bio = BIO_new(BIO_s_mem());
  44695. BIO_set_write_buf_size(bio, MAX_BUF);
  44696. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  44697. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  44698. (int)sizeof(unprintableData));
  44699. /* test */
  44700. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  44701. AssertIntEQ(p_len, 46);
  44702. BIO_read(bio, (void*)rbuf, 46);
  44703. AssertStrEQ((char*)rbuf, (const char*)expected);
  44704. BIO_free(bio);
  44705. ASN1_STRING_free(asnStr);
  44706. printf(resultFmt, passed);
  44707. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  44708. return 0;
  44709. }
  44710. #endif /* !NO_BIO */
  44711. static int test_wolfSSL_ASN1_get_object(void)
  44712. {
  44713. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44714. const unsigned char* derBuf = cliecc_cert_der_256;
  44715. int len = sizeof_cliecc_cert_der_256;
  44716. long asnLen = 0;
  44717. int tag = 0, cls = 0;
  44718. ASN1_OBJECT *a;
  44719. printf(testingFmt, "wolfSSL_ASN1_get_object()");
  44720. /* Read a couple TLV triplets and make sure they match the expected values */
  44721. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  44722. AssertIntEQ(asnLen, 862);
  44723. AssertIntEQ(tag, 0x10);
  44724. AssertIntEQ(cls, 0);
  44725. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44726. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44727. AssertIntEQ(asnLen, 772);
  44728. AssertIntEQ(tag, 0x10);
  44729. AssertIntEQ(cls, 0);
  44730. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44731. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44732. AssertIntEQ(asnLen, 3);
  44733. AssertIntEQ(tag, 0);
  44734. AssertIntEQ(cls, 0x80);
  44735. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44736. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44737. AssertIntEQ(asnLen, 1);
  44738. AssertIntEQ(tag, 0x2);
  44739. AssertIntEQ(cls, 0);
  44740. derBuf += asnLen;
  44741. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44742. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44743. AssertIntEQ(asnLen, 20);
  44744. AssertIntEQ(tag, 0x2);
  44745. AssertIntEQ(cls, 0);
  44746. derBuf += asnLen;
  44747. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44748. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44749. AssertIntEQ(asnLen, 10);
  44750. AssertIntEQ(tag, 0x10);
  44751. AssertIntEQ(cls, 0);
  44752. /* Read an ASN OBJECT */
  44753. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  44754. ASN1_OBJECT_free(a);
  44755. printf(resultFmt, passed);
  44756. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  44757. return 0;
  44758. }
  44759. static int test_wolfSSL_RSA(void)
  44760. {
  44761. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  44762. defined(WOLFSSL_KEY_GEN)
  44763. RSA* rsa;
  44764. const BIGNUM *n;
  44765. const BIGNUM *e;
  44766. const BIGNUM *d;
  44767. const BIGNUM *p;
  44768. const BIGNUM *q;
  44769. const BIGNUM *dmp1;
  44770. const BIGNUM *dmq1;
  44771. const BIGNUM *iqmp;
  44772. printf(testingFmt, "wolfSSL_RSA()");
  44773. AssertNotNull(rsa = RSA_new());
  44774. AssertIntEQ(RSA_size(NULL), 0);
  44775. AssertIntEQ(RSA_size(rsa), 0);
  44776. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  44777. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  44778. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  44779. #ifdef WOLFSSL_RSA_KEY_CHECK
  44780. AssertIntEQ(RSA_check_key(rsa), 0);
  44781. #endif
  44782. RSA_free(rsa);
  44783. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44784. AssertIntEQ(RSA_size(rsa), 256);
  44785. #ifdef WOLFSSL_RSA_KEY_CHECK
  44786. AssertIntEQ(RSA_check_key(NULL), 0);
  44787. AssertIntEQ(RSA_check_key(rsa), 1);
  44788. #endif
  44789. /* sanity check */
  44790. AssertIntEQ(RSA_bits(NULL), 0);
  44791. /* key */
  44792. AssertIntEQ(RSA_bits(rsa), 2048);
  44793. RSA_get0_key(rsa, &n, &e, &d);
  44794. AssertPtrEq(rsa->n, n);
  44795. AssertPtrEq(rsa->e, e);
  44796. AssertPtrEq(rsa->d, d);
  44797. AssertNotNull(n = BN_new());
  44798. AssertNotNull(e = BN_new());
  44799. AssertNotNull(d = BN_new());
  44800. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  44801. AssertPtrEq(rsa->n, n);
  44802. AssertPtrEq(rsa->e, e);
  44803. AssertPtrEq(rsa->d, d);
  44804. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  44805. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  44806. /* crt_params */
  44807. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  44808. AssertPtrEq(rsa->dmp1, dmp1);
  44809. AssertPtrEq(rsa->dmq1, dmq1);
  44810. AssertPtrEq(rsa->iqmp, iqmp);
  44811. AssertNotNull(dmp1 = BN_new());
  44812. AssertNotNull(dmq1 = BN_new());
  44813. AssertNotNull(iqmp = BN_new());
  44814. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  44815. (BIGNUM*)iqmp), 1);
  44816. AssertPtrEq(rsa->dmp1, dmp1);
  44817. AssertPtrEq(rsa->dmq1, dmq1);
  44818. AssertPtrEq(rsa->iqmp, iqmp);
  44819. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  44820. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  44821. (BIGNUM*)iqmp), 0);
  44822. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  44823. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  44824. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  44825. AssertNull(dmp1);
  44826. AssertNull(dmq1);
  44827. AssertNull(iqmp);
  44828. /* factors */
  44829. RSA_get0_factors(rsa, NULL, NULL);
  44830. RSA_get0_factors(rsa, &p, &q);
  44831. AssertPtrEq(rsa->p, p);
  44832. AssertPtrEq(rsa->q, q);
  44833. AssertNotNull(p = BN_new());
  44834. AssertNotNull(q = BN_new());
  44835. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  44836. AssertPtrEq(rsa->p, p);
  44837. AssertPtrEq(rsa->q, q);
  44838. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  44839. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  44840. RSA_get0_factors(NULL, NULL, NULL);
  44841. RSA_get0_factors(NULL, &p, &q);
  44842. AssertNull(p);
  44843. AssertNull(q);
  44844. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  44845. AssertIntEQ(RSA_bits(rsa), 21);
  44846. RSA_free(rsa);
  44847. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  44848. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  44849. AssertIntEQ(RSA_size(rsa), 384);
  44850. AssertIntEQ(RSA_bits(rsa), 3072);
  44851. RSA_free(rsa);
  44852. #endif
  44853. /* remove for now with odd key size until adjusting rsa key size check with
  44854. wc_MakeRsaKey()
  44855. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  44856. RSA_free(rsa);
  44857. */
  44858. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  44859. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  44860. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  44861. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  44862. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  44863. {
  44864. byte buff[FOURK_BUF];
  44865. byte der[FOURK_BUF];
  44866. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  44867. XFILE f;
  44868. int bytes;
  44869. /* test loading encrypted RSA private pem w/o password */
  44870. f = XFOPEN(PrivKeyPemFile, "rb");
  44871. AssertTrue((f != XBADFILE));
  44872. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  44873. XFCLOSE(f);
  44874. XMEMSET(der, 0, sizeof(der));
  44875. /* test that error value is returned with no password */
  44876. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  44877. }
  44878. #endif
  44879. printf(resultFmt, passed);
  44880. #endif
  44881. return 0;
  44882. }
  44883. static int test_wolfSSL_RSA_DER(void)
  44884. {
  44885. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44886. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  44887. RSA *rsa;
  44888. int i;
  44889. const unsigned char *buff = NULL;
  44890. unsigned char *newBuff = NULL;
  44891. struct tbl_s
  44892. {
  44893. const unsigned char *der;
  44894. int sz;
  44895. } tbl[] = {
  44896. #ifdef USE_CERT_BUFFERS_1024
  44897. {client_key_der_1024, sizeof_client_key_der_1024},
  44898. {server_key_der_1024, sizeof_server_key_der_1024},
  44899. #endif
  44900. #ifdef USE_CERT_BUFFERS_2048
  44901. {client_key_der_2048, sizeof_client_key_der_2048},
  44902. {server_key_der_2048, sizeof_server_key_der_2048},
  44903. #endif
  44904. {NULL, 0}
  44905. };
  44906. /* Public Key DER */
  44907. struct tbl_s pub[] = {
  44908. #ifdef USE_CERT_BUFFERS_1024
  44909. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  44910. #endif
  44911. #ifdef USE_CERT_BUFFERS_2048
  44912. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  44913. #endif
  44914. {NULL, 0}
  44915. };
  44916. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  44917. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  44918. buff = pub[0].der;
  44919. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  44920. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  44921. buff = tbl[0].der;
  44922. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  44923. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  44924. rsa = RSA_new();
  44925. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  44926. RSA_free(rsa);
  44927. for (i = 0; tbl[i].der != NULL; i++)
  44928. {
  44929. /* Passing in pointer results in pointer moving. */
  44930. buff = tbl[i].der;
  44931. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  44932. AssertNotNull(rsa);
  44933. RSA_free(rsa);
  44934. }
  44935. for (i = 0; tbl[i].der != NULL; i++)
  44936. {
  44937. /* Passing in pointer results in pointer moving. */
  44938. buff = tbl[i].der;
  44939. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  44940. AssertNotNull(rsa);
  44941. RSA_free(rsa);
  44942. }
  44943. for (i = 0; pub[i].der != NULL; i++)
  44944. {
  44945. buff = pub[i].der;
  44946. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  44947. AssertNotNull(rsa);
  44948. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  44949. newBuff = NULL;
  44950. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  44951. AssertNotNull(newBuff);
  44952. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  44953. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44954. RSA_free(rsa);
  44955. }
  44956. printf(resultFmt, passed);
  44957. #endif
  44958. return 0;
  44959. }
  44960. static int test_wolfSSL_RSA_print(void)
  44961. {
  44962. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  44963. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44964. !defined(HAVE_FAST_RSA) && !defined(NO_BIO)
  44965. BIO *bio;
  44966. WOLFSSL_RSA* rsa = NULL;
  44967. printf(testingFmt, "wolfSSL_RSA_print");
  44968. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  44969. AssertNotNull(rsa = RSA_new());
  44970. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  44971. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  44972. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  44973. AssertIntEQ(RSA_print_fp(stdout, NULL, 0), 0);
  44974. /* Some very large number of indent spaces. */
  44975. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  44976. /* RSA is empty. */
  44977. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  44978. AssertIntEQ(RSA_print_fp(stdout, rsa, 0), 0);
  44979. RSA_free(rsa);
  44980. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44981. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  44982. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  44983. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  44984. AssertIntEQ(RSA_print_fp(stdout, rsa, 0), 1);
  44985. AssertIntEQ(RSA_print_fp(stdout, rsa, 4), 1);
  44986. AssertIntEQ(RSA_print_fp(stdout, rsa, -1), 1);
  44987. BIO_free(bio);
  44988. RSA_free(rsa);
  44989. printf(resultFmt, passed);
  44990. #endif
  44991. return 0;
  44992. }
  44993. #ifndef NO_RSA
  44994. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  44995. {
  44996. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  44997. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44998. RSA *rsa;
  44999. const unsigned char *derBuf = client_key_der_2048;
  45000. unsigned char em[256] = {0}; /* len = 2048/8 */
  45001. /* Random data simulating a hash */
  45002. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  45003. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  45004. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  45005. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  45006. };
  45007. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  45008. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  45009. RSA_PSS_SALTLEN_DIGEST), 0);
  45010. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  45011. RSA_PSS_SALTLEN_DIGEST), 0);
  45012. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  45013. RSA_PSS_SALTLEN_DIGEST), 0);
  45014. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  45015. RSA_PSS_SALTLEN_DIGEST), 0);
  45016. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  45017. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  45018. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45019. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  45020. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45021. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  45022. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45023. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  45024. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45025. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  45026. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45027. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  45028. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  45029. RSA_PSS_SALTLEN_DIGEST), 1);
  45030. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  45031. RSA_PSS_SALTLEN_DIGEST), 1);
  45032. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  45033. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  45034. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  45035. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  45036. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  45037. RSA_PSS_SALTLEN_MAX), 1);
  45038. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  45039. RSA_PSS_SALTLEN_MAX), 1);
  45040. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  45041. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  45042. RSA_free(rsa);
  45043. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  45044. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  45045. return 0;
  45046. }
  45047. #endif
  45048. static int test_wolfSSL_RSA_sign_sha3(void)
  45049. {
  45050. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  45051. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  45052. RSA *rsa;
  45053. const unsigned char *derBuf = client_key_der_2048;
  45054. unsigned char sigRet[256] = {0};
  45055. unsigned int sigLen = sizeof(sigRet);
  45056. /* Random data simulating a hash */
  45057. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  45058. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  45059. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  45060. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  45061. };
  45062. printf(testingFmt, "wolfSSL_RSA_sign_sha3");
  45063. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  45064. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  45065. &sigLen, rsa), 1);
  45066. RSA_free(rsa);
  45067. printf(resultFmt, passed);
  45068. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  45069. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  45070. return 0;
  45071. }
  45072. static int test_wolfSSL_RSA_get0_key(void)
  45073. {
  45074. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  45075. RSA *rsa = NULL;
  45076. const BIGNUM* n = NULL;
  45077. const BIGNUM* e = NULL;
  45078. const BIGNUM* d = NULL;
  45079. const unsigned char* der;
  45080. int derSz;
  45081. #ifdef USE_CERT_BUFFERS_1024
  45082. der = client_key_der_1024;
  45083. derSz = sizeof_client_key_der_1024;
  45084. #elif defined(USE_CERT_BUFFERS_2048)
  45085. der = client_key_der_2048;
  45086. derSz = sizeof_client_key_der_2048;
  45087. #else
  45088. der = NULL;
  45089. derSz = 0;
  45090. #endif
  45091. printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
  45092. if (der != NULL) {
  45093. RSA_get0_key(NULL, NULL, NULL, NULL);
  45094. RSA_get0_key(rsa, NULL, NULL, NULL);
  45095. RSA_get0_key(NULL, &n, &e, &d);
  45096. AssertNull(n);
  45097. AssertNull(e);
  45098. AssertNull(d);
  45099. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  45100. AssertNotNull(rsa);
  45101. RSA_get0_key(rsa, NULL, NULL, NULL);
  45102. RSA_get0_key(rsa, &n, NULL, NULL);
  45103. AssertNotNull(n);
  45104. RSA_get0_key(rsa, NULL, &e, NULL);
  45105. AssertNotNull(e);
  45106. RSA_get0_key(rsa, NULL, NULL, &d);
  45107. AssertNotNull(d);
  45108. RSA_get0_key(rsa, &n, &e, &d);
  45109. AssertNotNull(n);
  45110. AssertNotNull(e);
  45111. AssertNotNull(d);
  45112. RSA_free(rsa);
  45113. }
  45114. printf(resultFmt, passed);
  45115. #endif
  45116. return 0;
  45117. }
  45118. static int test_wolfSSL_RSA_meth(void)
  45119. {
  45120. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45121. RSA *rsa;
  45122. RSA_METHOD *rsa_meth;
  45123. printf(testingFmt, "test_wolfSSL_RSA_meth");
  45124. #ifdef WOLFSSL_KEY_GEN
  45125. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45126. RSA_free(rsa);
  45127. #else
  45128. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45129. #endif
  45130. AssertNotNull(RSA_get_default_method());
  45131. wolfSSL_RSA_meth_free(NULL);
  45132. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  45133. AssertNotNull(rsa_meth =
  45134. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  45135. #ifndef NO_WOLFSSL_STUB
  45136. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  45137. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  45138. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  45139. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  45140. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  45141. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  45142. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  45143. #endif
  45144. AssertIntEQ(RSA_flags(NULL), 0);
  45145. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  45146. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  45147. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  45148. AssertNotNull(rsa = RSA_new());
  45149. /* No method set. */
  45150. AssertIntEQ(RSA_flags(rsa), 0);
  45151. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  45152. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  45153. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  45154. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  45155. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  45156. AssertNull(RSA_get_method(NULL));
  45157. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  45158. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  45159. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  45160. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  45161. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  45162. RSA_METHOD_FLAG_NO_CHECK);
  45163. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  45164. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  45165. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  45166. /* rsa_meth is freed here */
  45167. RSA_free(rsa);
  45168. printf(resultFmt, passed);
  45169. #endif
  45170. return 0;
  45171. }
  45172. static int test_wolfSSL_RSA_verify(void)
  45173. {
  45174. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  45175. !defined(NO_FILESYSTEM)
  45176. #ifndef NO_BIO
  45177. XFILE fp;
  45178. RSA *pKey, *pubKey;
  45179. X509 *cert;
  45180. const char *text = "Hello wolfSSL !";
  45181. unsigned char hash[SHA256_DIGEST_LENGTH];
  45182. unsigned char signature[2048/8];
  45183. unsigned int signatureLength;
  45184. byte *buf;
  45185. BIO *bio;
  45186. SHA256_CTX c;
  45187. EVP_PKEY *evpPkey, *evpPubkey;
  45188. size_t sz;
  45189. printf(testingFmt, "wolfSSL_RSA_verify");
  45190. /* generate hash */
  45191. SHA256_Init(&c);
  45192. SHA256_Update(&c, text, strlen(text));
  45193. SHA256_Final(hash, &c);
  45194. #ifdef WOLFSSL_SMALL_STACK_CACHE
  45195. /* workaround for small stack cache case */
  45196. wc_Sha256Free((wc_Sha256*)&c);
  45197. #endif
  45198. /* read privete key file */
  45199. fp = XFOPEN(svrKeyFile, "rb");
  45200. AssertTrue((fp != XBADFILE));
  45201. AssertIntGE(XFSEEK(fp, 0, XSEEK_END), 0);
  45202. sz = XFTELL(fp);
  45203. XREWIND(fp);
  45204. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  45205. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  45206. XFCLOSE(fp);
  45207. /* read private key and sign hash data */
  45208. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  45209. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  45210. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  45211. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  45212. signature, &signatureLength, pKey), SSL_SUCCESS);
  45213. /* read public key and verify signed data */
  45214. fp = XFOPEN(svrCertFile,"rb");
  45215. AssertTrue((fp != XBADFILE));
  45216. cert = PEM_read_X509(fp, 0, 0, 0 );
  45217. XFCLOSE(fp);
  45218. evpPubkey = X509_get_pubkey(cert);
  45219. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  45220. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  45221. signatureLength, pubKey), SSL_SUCCESS);
  45222. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  45223. signatureLength, NULL), SSL_FAILURE);
  45224. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  45225. signatureLength, pubKey), SSL_FAILURE);
  45226. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  45227. signatureLength, pubKey), SSL_FAILURE);
  45228. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  45229. signatureLength, NULL), SSL_FAILURE);
  45230. RSA_free(pKey);
  45231. EVP_PKEY_free(evpPkey);
  45232. RSA_free(pubKey);
  45233. EVP_PKEY_free(evpPubkey);
  45234. X509_free(cert);
  45235. BIO_free(bio);
  45236. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  45237. printf(resultFmt, passed);
  45238. #endif
  45239. #endif
  45240. return 0;
  45241. }
  45242. static int test_wolfSSL_RSA_sign(void)
  45243. {
  45244. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45245. RSA *rsa;
  45246. unsigned char hash[SHA256_DIGEST_LENGTH];
  45247. #ifdef USE_CERT_BUFFERS_1024
  45248. const unsigned char* privDer = client_key_der_1024;
  45249. size_t privDerSz = sizeof_client_key_der_1024;
  45250. const unsigned char* pubDer = client_keypub_der_1024;
  45251. size_t pubDerSz = sizeof_client_keypub_der_1024;
  45252. unsigned char signature[1024/8];
  45253. #else
  45254. const unsigned char* privDer = client_key_der_2048;
  45255. size_t privDerSz = sizeof_client_key_der_2048;
  45256. const unsigned char* pubDer = client_keypub_der_2048;
  45257. size_t pubDerSz = sizeof_client_keypub_der_2048;
  45258. unsigned char signature[2048/8];
  45259. #endif
  45260. unsigned int signatureLen;
  45261. const unsigned char* der;
  45262. printf(testingFmt, "wolfSSL_RSA_sign");
  45263. XMEMSET(hash, 0, sizeof(hash));
  45264. der = privDer;
  45265. rsa = NULL;
  45266. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45267. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  45268. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  45269. &signatureLen, rsa), 0);
  45270. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  45271. &signatureLen, rsa), 0);
  45272. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  45273. &signatureLen, rsa), 0);
  45274. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  45275. NULL, rsa), 0);
  45276. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  45277. &signatureLen, NULL), 0);
  45278. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  45279. &signatureLen, rsa), 1);
  45280. RSA_free(rsa);
  45281. der = pubDer;
  45282. rsa = NULL;
  45283. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  45284. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  45285. signatureLen, rsa), 1);
  45286. RSA_free(rsa);
  45287. printf(resultFmt, passed);
  45288. #endif
  45289. return 0;
  45290. }
  45291. static int test_wolfSSL_RSA_sign_ex(void)
  45292. {
  45293. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45294. RSA *rsa;
  45295. unsigned char hash[SHA256_DIGEST_LENGTH];
  45296. #ifdef USE_CERT_BUFFERS_1024
  45297. const unsigned char* privDer = client_key_der_1024;
  45298. size_t privDerSz = sizeof_client_key_der_1024;
  45299. const unsigned char* pubDer = client_keypub_der_1024;
  45300. size_t pubDerSz = sizeof_client_keypub_der_1024;
  45301. unsigned char signature[1024/8];
  45302. #else
  45303. const unsigned char* privDer = client_key_der_2048;
  45304. size_t privDerSz = sizeof_client_key_der_2048;
  45305. const unsigned char* pubDer = client_keypub_der_2048;
  45306. size_t pubDerSz = sizeof_client_keypub_der_2048;
  45307. unsigned char signature[2048/8];
  45308. #endif
  45309. unsigned int signatureLen;
  45310. const unsigned char* der;
  45311. unsigned char encodedHash[51];
  45312. unsigned int encodedHashLen;
  45313. const unsigned char expEncHash[] = {
  45314. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  45315. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  45316. 0x00, 0x04, 0x20,
  45317. /* Hash data */
  45318. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45319. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45320. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45321. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45322. };
  45323. printf(testingFmt, "wolfSSL_RSA_sign_ex");
  45324. XMEMSET(hash, 0, sizeof(hash));
  45325. AssertNotNull(rsa = wolfSSL_RSA_new());
  45326. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45327. &signatureLen, rsa, 1), 0);
  45328. wolfSSL_RSA_free(rsa);
  45329. der = privDer;
  45330. rsa = NULL;
  45331. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45332. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  45333. -1), 0);
  45334. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  45335. signature, &signatureLen, rsa, 1), 0);
  45336. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  45337. &signatureLen, rsa, 1), 0);
  45338. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  45339. &signatureLen, rsa, 1), 0);
  45340. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45341. NULL, rsa, 1), 0);
  45342. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45343. &signatureLen, NULL, 1), 0);
  45344. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45345. &signatureLen, rsa, -1), 0);
  45346. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  45347. &signatureLen, rsa, 0), 0);
  45348. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  45349. &signatureLen, rsa, 0), 0);
  45350. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45351. NULL, rsa, 0), 0);
  45352. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45353. &signatureLen, rsa, 1), 1);
  45354. /* Test returning encoded hash. */
  45355. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  45356. &encodedHashLen, rsa, 0), 1);
  45357. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  45358. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  45359. RSA_free(rsa);
  45360. der = pubDer;
  45361. rsa = NULL;
  45362. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  45363. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  45364. signatureLen, rsa), 1);
  45365. RSA_free(rsa);
  45366. printf(resultFmt, passed);
  45367. #endif
  45368. return 0;
  45369. }
  45370. static int test_wolfSSL_RSA_public_decrypt(void)
  45371. {
  45372. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45373. RSA *rsa;
  45374. unsigned char msg[SHA256_DIGEST_LENGTH];
  45375. #ifdef USE_CERT_BUFFERS_1024
  45376. const unsigned char* pubDer = client_keypub_der_1024;
  45377. size_t pubDerSz = sizeof_client_keypub_der_1024;
  45378. unsigned char decMsg[1024/8];
  45379. const unsigned char encMsg[] = {
  45380. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  45381. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  45382. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  45383. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  45384. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  45385. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  45386. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  45387. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  45388. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  45389. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  45390. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  45391. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  45392. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  45393. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  45394. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  45395. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  45396. };
  45397. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  45398. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  45399. defined(WC_RSA_NO_PADDING)
  45400. const unsigned char encMsgNoPad[] = {
  45401. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  45402. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  45403. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  45404. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  45405. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  45406. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  45407. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  45408. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  45409. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  45410. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  45411. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  45412. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  45413. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  45414. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  45415. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  45416. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  45417. };
  45418. #endif
  45419. #else
  45420. const unsigned char* pubDer = client_keypub_der_2048;
  45421. size_t pubDerSz = sizeof_client_keypub_der_2048;
  45422. unsigned char decMsg[2048/8];
  45423. const unsigned char encMsg[] = {
  45424. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  45425. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  45426. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  45427. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  45428. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  45429. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  45430. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  45431. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  45432. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  45433. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  45434. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  45435. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  45436. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  45437. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  45438. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  45439. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  45440. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  45441. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  45442. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  45443. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  45444. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  45445. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  45446. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  45447. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  45448. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  45449. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  45450. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  45451. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  45452. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  45453. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  45454. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  45455. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  45456. };
  45457. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  45458. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  45459. defined(WC_RSA_NO_PADDING)
  45460. const unsigned char encMsgNoPad[] = {
  45461. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  45462. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  45463. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  45464. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  45465. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  45466. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  45467. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  45468. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  45469. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  45470. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  45471. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  45472. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  45473. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  45474. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  45475. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  45476. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  45477. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  45478. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  45479. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  45480. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  45481. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  45482. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  45483. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  45484. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  45485. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  45486. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  45487. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  45488. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  45489. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  45490. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  45491. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  45492. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  45493. };
  45494. #endif
  45495. #endif
  45496. const unsigned char* der;
  45497. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  45498. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  45499. defined(WC_RSA_NO_PADDING)
  45500. int i;
  45501. #endif
  45502. printf(testingFmt, "wolfSSL_RSA_public_decrypt");
  45503. XMEMSET(msg, 0, sizeof(msg));
  45504. der = pubDer;
  45505. rsa = NULL;
  45506. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  45507. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  45508. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  45509. RSA_PKCS1_PADDING), -1);
  45510. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  45511. RSA_PKCS1_PADDING), -1);
  45512. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  45513. RSA_PKCS1_PADDING), -1);
  45514. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  45515. RSA_PKCS1_PADDING), -1);
  45516. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  45517. RSA_PKCS1_PSS_PADDING), -1);
  45518. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  45519. RSA_PKCS1_PADDING), 32);
  45520. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  45521. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  45522. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  45523. defined(WC_RSA_NO_PADDING)
  45524. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  45525. rsa, RSA_NO_PADDING), sizeof(decMsg));
  45526. /* Zeros before actual data. */
  45527. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  45528. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  45529. }
  45530. /* Check actual data. */
  45531. XMEMSET(msg, 0x01, sizeof(msg));
  45532. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  45533. #endif
  45534. RSA_free(rsa);
  45535. printf(resultFmt, passed);
  45536. #endif
  45537. return 0;
  45538. }
  45539. static int test_wolfSSL_RSA_private_encrypt(void)
  45540. {
  45541. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45542. RSA *rsa;
  45543. unsigned char msg[SHA256_DIGEST_LENGTH];
  45544. #ifdef USE_CERT_BUFFERS_1024
  45545. const unsigned char* privDer = client_key_der_1024;
  45546. size_t privDerSz = sizeof_client_key_der_1024;
  45547. unsigned char encMsg[1024/8];
  45548. const unsigned char expEncMsg[] = {
  45549. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  45550. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  45551. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  45552. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  45553. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  45554. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  45555. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  45556. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  45557. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  45558. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  45559. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  45560. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  45561. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  45562. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  45563. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  45564. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  45565. };
  45566. #ifdef WC_RSA_NO_PADDING
  45567. const unsigned char expEncMsgNoPad[] = {
  45568. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  45569. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  45570. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  45571. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  45572. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  45573. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  45574. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  45575. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  45576. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  45577. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  45578. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  45579. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  45580. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  45581. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  45582. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  45583. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  45584. };
  45585. #endif
  45586. #else
  45587. const unsigned char* privDer = client_key_der_2048;
  45588. size_t privDerSz = sizeof_client_key_der_2048;
  45589. unsigned char encMsg[2048/8];
  45590. const unsigned char expEncMsg[] = {
  45591. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  45592. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  45593. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  45594. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  45595. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  45596. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  45597. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  45598. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  45599. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  45600. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  45601. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  45602. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  45603. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  45604. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  45605. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  45606. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  45607. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  45608. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  45609. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  45610. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  45611. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  45612. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  45613. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  45614. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  45615. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  45616. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  45617. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  45618. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  45619. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  45620. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  45621. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  45622. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  45623. };
  45624. #ifdef WC_RSA_NO_PADDING
  45625. const unsigned char expEncMsgNoPad[] = {
  45626. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  45627. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  45628. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  45629. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  45630. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  45631. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  45632. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  45633. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  45634. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  45635. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  45636. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  45637. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  45638. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  45639. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  45640. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  45641. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  45642. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  45643. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  45644. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  45645. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  45646. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  45647. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  45648. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  45649. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  45650. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  45651. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  45652. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  45653. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  45654. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  45655. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  45656. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  45657. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  45658. };
  45659. #endif
  45660. #endif
  45661. const unsigned char* der;
  45662. printf(testingFmt, "wolfSSL_RSA_private_encrypt");
  45663. XMEMSET(msg, 0x00, sizeof(msg));
  45664. der = privDer;
  45665. rsa = NULL;
  45666. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45667. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  45668. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  45669. -1);
  45670. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  45671. RSA_PKCS1_PADDING), -1);
  45672. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  45673. RSA_PKCS1_PADDING), -1);
  45674. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  45675. RSA_PKCS1_PADDING), -1);
  45676. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  45677. RSA_PKCS1_PSS_PADDING), -1);
  45678. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  45679. RSA_PKCS1_PADDING), sizeof(encMsg));
  45680. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  45681. #ifdef WC_RSA_NO_PADDING
  45682. /* Non-zero message. */
  45683. XMEMSET(msg, 0x01, sizeof(msg));
  45684. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  45685. RSA_NO_PADDING), sizeof(encMsg));
  45686. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  45687. #endif
  45688. RSA_free(rsa);
  45689. printf(resultFmt, passed);
  45690. #endif
  45691. return 0;
  45692. }
  45693. static int test_wolfSSL_RSA_public_encrypt(void)
  45694. {
  45695. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45696. RSA* rsa;
  45697. const unsigned char msg[2048/8] = { 0 };
  45698. unsigned char encMsg[2048/8];
  45699. printf(testingFmt, "wolfSSL_RSA_public_decrypt");
  45700. AssertNotNull(rsa = RSA_new());
  45701. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  45702. RSA_PKCS1_PADDING), -1);
  45703. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  45704. RSA_PKCS1_PADDING), -1);
  45705. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  45706. RSA_PKCS1_PADDING), -1);
  45707. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  45708. RSA_PKCS1_PADDING), -1);
  45709. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  45710. RSA_PKCS1_PSS_PADDING), -1);
  45711. /* Empty RSA key. */
  45712. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  45713. RSA_PKCS1_PADDING), -1);
  45714. RSA_free(rsa);
  45715. printf(resultFmt, passed);
  45716. #endif
  45717. return 0;
  45718. }
  45719. static int test_wolfSSL_RSA_private_decrypt(void)
  45720. {
  45721. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45722. RSA* rsa;
  45723. unsigned char msg[2048/8];
  45724. const unsigned char encMsg[2048/8] = { 0 };
  45725. printf(testingFmt, "wolfSSL_RSA_private_decrypt");
  45726. AssertNotNull(rsa = RSA_new());
  45727. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  45728. RSA_PKCS1_PADDING), -1);
  45729. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  45730. RSA_PKCS1_PADDING), -1);
  45731. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  45732. RSA_PKCS1_PADDING), -1);
  45733. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  45734. RSA_PKCS1_PADDING), -1);
  45735. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  45736. RSA_PKCS1_PSS_PADDING), -1);
  45737. /* Empty RSA key. */
  45738. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  45739. RSA_PKCS1_PADDING), -1);
  45740. RSA_free(rsa);
  45741. printf(resultFmt, passed);
  45742. #endif
  45743. return 0;
  45744. }
  45745. static int test_wolfSSL_RSA_GenAdd(void)
  45746. {
  45747. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45748. RSA *rsa;
  45749. #ifdef USE_CERT_BUFFERS_1024
  45750. const unsigned char* privDer = client_key_der_1024;
  45751. size_t privDerSz = sizeof_client_key_der_1024;
  45752. const unsigned char* pubDer = client_keypub_der_1024;
  45753. size_t pubDerSz = sizeof_client_keypub_der_1024;
  45754. #else
  45755. const unsigned char* privDer = client_key_der_2048;
  45756. size_t privDerSz = sizeof_client_key_der_2048;
  45757. const unsigned char* pubDer = client_keypub_der_2048;
  45758. size_t pubDerSz = sizeof_client_keypub_der_2048;
  45759. #endif
  45760. const unsigned char* der;
  45761. printf(testingFmt, "wolfSSL_RSA_GenAdd");
  45762. der = privDer;
  45763. rsa = NULL;
  45764. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45765. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  45766. #ifndef RSA_LOW_MEM
  45767. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  45768. #else
  45769. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  45770. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  45771. #endif
  45772. RSA_free(rsa);
  45773. der = pubDer;
  45774. rsa = NULL;
  45775. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  45776. /* Need private values. */
  45777. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  45778. RSA_free(rsa);
  45779. printf(resultFmt, passed);
  45780. #endif
  45781. return 0;
  45782. }
  45783. static int test_wolfSSL_RSA_blinding_on(void)
  45784. {
  45785. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  45786. RSA *rsa;
  45787. WOLFSSL_BN_CTX *bnCtx;
  45788. #ifdef USE_CERT_BUFFERS_1024
  45789. const unsigned char* privDer = client_key_der_1024;
  45790. size_t privDerSz = sizeof_client_key_der_1024;
  45791. #else
  45792. const unsigned char* privDer = client_key_der_2048;
  45793. size_t privDerSz = sizeof_client_key_der_2048;
  45794. #endif
  45795. const unsigned char* der;
  45796. printf(testingFmt, "wolfSSL_RSA_blinding_on");
  45797. der = privDer;
  45798. rsa = NULL;
  45799. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45800. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  45801. /* Does nothing so all parameters are valid. */
  45802. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  45803. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  45804. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  45805. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  45806. wolfSSL_BN_CTX_free(bnCtx);
  45807. RSA_free(rsa);
  45808. printf(resultFmt, passed);
  45809. #endif
  45810. return 0;
  45811. }
  45812. static int test_wolfSSL_RSA_ex_data(void)
  45813. {
  45814. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  45815. RSA* rsa;
  45816. unsigned char data[1];
  45817. printf(testingFmt, "wolfSSL_RSA_ex_data");
  45818. rsa = RSA_new();
  45819. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  45820. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  45821. #ifdef MAX_EX_DATA
  45822. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  45823. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  45824. #endif
  45825. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  45826. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  45827. #ifdef HAVE_EX_DATA
  45828. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  45829. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  45830. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  45831. #else
  45832. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  45833. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  45834. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  45835. #endif
  45836. RSA_free(rsa);
  45837. printf(resultFmt, passed);
  45838. #endif /* !NO_RSA && OPENSSL_EXTRA */
  45839. return 0;
  45840. }
  45841. static int test_wolfSSL_RSA_LoadDer(void)
  45842. {
  45843. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  45844. defined(OPENSSL_EXTRA_X509_SMALL))
  45845. RSA *rsa;
  45846. #ifdef USE_CERT_BUFFERS_1024
  45847. const unsigned char* privDer = client_key_der_1024;
  45848. size_t privDerSz = sizeof_client_key_der_1024;
  45849. #else
  45850. const unsigned char* privDer = client_key_der_2048;
  45851. size_t privDerSz = sizeof_client_key_der_2048;
  45852. #endif
  45853. printf(testingFmt, "wolfSSL_RSA_LoadDer");
  45854. AssertNotNull(rsa = RSA_new());
  45855. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  45856. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  45857. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  45858. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  45859. RSA_free(rsa);
  45860. printf(resultFmt, passed);
  45861. #endif
  45862. return 0;
  45863. }
  45864. /* Local API. */
  45865. static int test_wolfSSL_RSA_To_Der(void)
  45866. {
  45867. #ifdef WOLFSSL_TEST_STATIC_BUILD
  45868. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  45869. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45870. RSA* rsa;
  45871. #ifdef USE_CERT_BUFFERS_1024
  45872. const unsigned char* privDer = client_key_der_1024;
  45873. size_t privDerSz = sizeof_client_key_der_1024;
  45874. const unsigned char* pubDer = client_keypub_der_1024;
  45875. size_t pubDerSz = sizeof_client_keypub_der_1024;
  45876. unsigned char out[sizeof(client_key_der_1024)];
  45877. #else
  45878. const unsigned char* privDer = client_key_der_2048;
  45879. size_t privDerSz = sizeof_client_key_der_2048;
  45880. const unsigned char* pubDer = client_keypub_der_2048;
  45881. size_t pubDerSz = sizeof_client_keypub_der_2048;
  45882. unsigned char out[sizeof(client_key_der_2048)];
  45883. #endif
  45884. const unsigned char* der;
  45885. unsigned char* outDer = NULL;
  45886. printf(testingFmt, "wolfSSL_RSA_To_Der");
  45887. der = privDer;
  45888. rsa = NULL;
  45889. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45890. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  45891. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  45892. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  45893. outDer = out;
  45894. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  45895. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  45896. outDer = NULL;
  45897. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  45898. AssertNotNull(outDer);
  45899. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  45900. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45901. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  45902. outDer = out;
  45903. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  45904. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  45905. RSA_free(rsa);
  45906. AssertNotNull(rsa = RSA_new());
  45907. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  45908. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  45909. RSA_free(rsa);
  45910. der = pubDer;
  45911. rsa = NULL;
  45912. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  45913. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  45914. RSA_free(rsa);
  45915. printf(resultFmt, passed);
  45916. #endif
  45917. #endif
  45918. return 0;
  45919. }
  45920. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  45921. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  45922. {
  45923. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  45924. !defined(NO_WOLFSSL_STUB)
  45925. RSA* rsa = NULL;
  45926. XFILE fp;
  45927. printf(testingFmt, "wolfSSL_PEM_read_RSAPublicKey");
  45928. fp = XFOPEN("./certs/client-keyPub.pem", "rb");
  45929. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  45930. /* Valid but stub so returns NULL. */
  45931. AssertNull(wolfSSL_PEM_read_RSAPublicKey(fp, NULL, NULL, NULL));
  45932. /* Valid but stub so returns NULL. */
  45933. AssertNull(wolfSSL_PEM_read_RSAPublicKey(fp, &rsa, NULL, NULL));
  45934. XFCLOSE(fp);
  45935. printf(resultFmt, passed);
  45936. #endif
  45937. return 0;
  45938. }
  45939. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  45940. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  45941. {
  45942. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  45943. !defined(NO_WOLFSSL_STUB)
  45944. RSA* rsa = NULL;
  45945. printf(testingFmt, "wolfSSL_PEM_write_RSA_PUBKEY");
  45946. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  45947. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stdout, NULL), 0);
  45948. /* Valid but stub so returns 0. */
  45949. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stdout, rsa), 0);
  45950. printf(resultFmt, passed);
  45951. #endif
  45952. return 0;
  45953. }
  45954. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  45955. {
  45956. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  45957. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  45958. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  45959. RSA* rsa;
  45960. #ifdef USE_CERT_BUFFERS_1024
  45961. const unsigned char* privDer = client_key_der_1024;
  45962. size_t privDerSz = sizeof_client_key_der_1024;
  45963. #else
  45964. const unsigned char* privDer = client_key_der_2048;
  45965. size_t privDerSz = sizeof_client_key_der_2048;
  45966. #endif
  45967. const unsigned char* der;
  45968. #ifndef NO_AES
  45969. unsigned char passwd[] = "password";
  45970. #endif
  45971. printf(testingFmt, "wolfSSL_PEM_write_RSAPrivateKey");
  45972. AssertNotNull(rsa = RSA_new());
  45973. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0,
  45974. NULL, NULL), 0);
  45975. RSA_free(rsa);
  45976. der = privDer;
  45977. rsa = NULL;
  45978. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45979. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  45980. NULL, NULL), 0);
  45981. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, NULL, NULL, NULL, 0,
  45982. NULL, NULL), 0);
  45983. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0,
  45984. NULL, NULL), 1);
  45985. #ifndef NO_AES
  45986. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
  45987. NULL, 0, NULL, NULL), 1);
  45988. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
  45989. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  45990. #endif
  45991. RSA_free(rsa);
  45992. printf(resultFmt, passed);
  45993. #endif
  45994. return 0;
  45995. }
  45996. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  45997. {
  45998. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  45999. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  46000. defined(WOLFSSL_DER_TO_PEM))
  46001. RSA* rsa;
  46002. #ifdef USE_CERT_BUFFERS_1024
  46003. const unsigned char* privDer = client_key_der_1024;
  46004. size_t privDerSz = sizeof_client_key_der_1024;
  46005. #else
  46006. const unsigned char* privDer = client_key_der_2048;
  46007. size_t privDerSz = sizeof_client_key_der_2048;
  46008. #endif
  46009. const unsigned char* der;
  46010. #ifndef NO_AES
  46011. unsigned char passwd[] = "password";
  46012. #endif
  46013. unsigned char* pem;
  46014. int plen;
  46015. printf(testingFmt, "wolfSSL_PEM_write_mem_RSAPrivateKey");
  46016. AssertNotNull(rsa = RSA_new());
  46017. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  46018. &plen), 0);
  46019. RSA_free(rsa);
  46020. der = privDer;
  46021. rsa = NULL;
  46022. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46023. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  46024. &plen), 0);
  46025. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  46026. &plen), 0);
  46027. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  46028. NULL), 0);
  46029. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  46030. &plen), 1);
  46031. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  46032. #ifndef NO_AES
  46033. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  46034. NULL, 0, &pem, &plen), 1);
  46035. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  46036. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  46037. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  46038. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  46039. #endif
  46040. RSA_free(rsa);
  46041. printf(resultFmt, passed);
  46042. #endif
  46043. return 0;
  46044. }
  46045. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  46046. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  46047. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  46048. {
  46049. X509* x509 = NULL;
  46050. BIGNUM* serial_number = NULL;
  46051. X509_NAME* name = NULL;
  46052. time_t epoch_off = 0;
  46053. ASN1_INTEGER* asn1_serial_number;
  46054. long not_before, not_after;
  46055. AssertNotNull(x509 = X509_new());
  46056. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  46057. AssertNotNull(serial_number = BN_new());
  46058. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  46059. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  46060. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  46061. /* version 3 */
  46062. AssertIntNE(X509_set_version(x509, 2L), 0);
  46063. AssertNotNull(name = X509_NAME_new());
  46064. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  46065. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  46066. AssertIntNE(X509_set_subject_name(x509, name), 0);
  46067. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  46068. not_before = (long)wc_Time(NULL);
  46069. not_after = not_before + (365 * 24 * 60 * 60);
  46070. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  46071. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  46072. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  46073. BN_free(serial_number);
  46074. X509_NAME_free(name);
  46075. X509_free(x509);
  46076. return 0;
  46077. }
  46078. #endif
  46079. static int test_openssl_generate_key_and_cert(void)
  46080. {
  46081. #if defined(OPENSSL_EXTRA)
  46082. #if !defined(NO_RSA)
  46083. {
  46084. EVP_PKEY* pkey = EVP_PKEY_new();
  46085. int key_length = 2048;
  46086. BIGNUM* exponent = BN_new();
  46087. RSA* rsa = RSA_new();
  46088. AssertNotNull(pkey);
  46089. AssertNotNull(exponent);
  46090. AssertNotNull(rsa);
  46091. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  46092. #ifndef WOLFSSL_KEY_GEN
  46093. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  46094. #if defined(USE_CERT_BUFFERS_1024)
  46095. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  46096. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  46097. key_length = 1024;
  46098. #elif defined(USE_CERT_BUFFERS_2048)
  46099. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  46100. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  46101. #else
  46102. RSA_free(rsa);
  46103. rsa = NULL;
  46104. #endif
  46105. #else
  46106. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  46107. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  46108. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  46109. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  46110. #endif
  46111. if (rsa) {
  46112. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  46113. BN_free(exponent);
  46114. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  46115. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  46116. test_openssl_make_self_signed_certificate(pkey);
  46117. #endif
  46118. }
  46119. EVP_PKEY_free(pkey);
  46120. }
  46121. #endif /* !NO_RSA */
  46122. #ifdef HAVE_ECC
  46123. {
  46124. EVP_PKEY* pkey = EVP_PKEY_new();
  46125. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  46126. AssertNotNull(pkey);
  46127. AssertNotNull(ec_key);
  46128. #ifndef NO_WOLFSSL_STUB
  46129. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  46130. #endif
  46131. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  46132. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  46133. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  46134. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  46135. test_openssl_make_self_signed_certificate(pkey);
  46136. #endif
  46137. EVP_PKEY_free(pkey);
  46138. }
  46139. #endif /* HAVE_ECC */
  46140. #endif /* OPENSSL_EXTRA */
  46141. return 0;
  46142. }
  46143. static int test_stubs_are_stubs(void)
  46144. {
  46145. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  46146. WOLFSSL_CTX* ctx = NULL;
  46147. WOLFSSL_CTX* ctxN = NULL;
  46148. #ifndef NO_WOLFSSL_CLIENT
  46149. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  46150. AssertNotNull(ctx);
  46151. #elif !defined(NO_WOLFSSL_SERVER)
  46152. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  46153. AssertNotNull(ctx);
  46154. #else
  46155. return;
  46156. #endif
  46157. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  46158. AssertIntEQ((int) x(z), 0)
  46159. /* test logic, all stubs return same result regardless of ctx being NULL
  46160. * as there are no sanity checks, it's just a stub! If at some
  46161. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  46162. * if invalid inputs are supplied. Test calling both
  46163. * with and without valid inputs, if a stub functionality remains unchanged.
  46164. */
  46165. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  46166. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  46167. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  46168. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  46169. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  46170. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  46171. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  46172. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  46173. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  46174. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  46175. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  46176. wolfSSL_CTX_free(ctx);
  46177. ctx = NULL;
  46178. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  46179. return 0;
  46180. }
  46181. static int test_CONF_modules_xxx(void)
  46182. {
  46183. #if defined(OPENSSL_EXTRA)
  46184. CONF_modules_free();
  46185. AssertTrue(1); /* to confirm previous call gives no harm */
  46186. CONF_modules_unload(0);
  46187. AssertTrue(1);
  46188. CONF_modules_unload(1);
  46189. AssertTrue(1);
  46190. CONF_modules_unload(-1);
  46191. AssertTrue(1);
  46192. #endif /* OPENSSL_EXTRA */
  46193. return 0;
  46194. }
  46195. static int test_CRYPTO_set_dynlock_xxx(void)
  46196. {
  46197. #if defined(OPENSSL_EXTRA)
  46198. printf(testingFmt, "CRYPTO_set_dynlock_xxx()");
  46199. CRYPTO_set_dynlock_create_callback(
  46200. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  46201. CRYPTO_set_dynlock_create_callback(
  46202. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  46203. CRYPTO_set_dynlock_destroy_callback(
  46204. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  46205. CRYPTO_set_dynlock_destroy_callback(
  46206. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  46207. CRYPTO_set_dynlock_lock_callback(
  46208. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  46209. CRYPTO_set_dynlock_lock_callback(
  46210. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  46211. AssertTrue(1); /* to confirm previous call gives no harm */
  46212. printf(resultFmt, passed);
  46213. #endif /* OPENSSL_EXTRA */
  46214. return 0;
  46215. }
  46216. static int test_CRYPTO_THREADID_xxx(void)
  46217. {
  46218. #if defined(OPENSSL_EXTRA)
  46219. printf(testingFmt, "CRYPTO_THREADID_xxx()");
  46220. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  46221. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  46222. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  46223. printf(resultFmt, passed);
  46224. #endif /* OPENSSL_EXTRA */
  46225. return 0;
  46226. }
  46227. static int test_ENGINE_cleanup(void)
  46228. {
  46229. #if defined(OPENSSL_EXTRA)
  46230. printf(testingFmt, "ENGINE_cleanup()");
  46231. ENGINE_cleanup();
  46232. AssertTrue(1); /* to confirm previous call gives no harm */
  46233. printf(resultFmt, passed);
  46234. #endif /* OPENSSL_EXTRA */
  46235. return 0;
  46236. }
  46237. static int test_wolfSSL_CTX_LoadCRL(void)
  46238. {
  46239. #if defined(HAVE_CRL) && !defined(NO_RSA)
  46240. WOLFSSL_CTX* ctx = NULL;
  46241. WOLFSSL* ssl = NULL;
  46242. const char* badPath = "dummypath";
  46243. const char* validPath = "./certs/crl";
  46244. const char* validFilePath = "./certs/crl/cliCrl.pem";
  46245. const char* issuerCert = "./certs/client-cert.pem";
  46246. int derType = WOLFSSL_FILETYPE_ASN1;
  46247. int pemType = WOLFSSL_FILETYPE_PEM;
  46248. int monitor = WOLFSSL_CRL_MONITOR;
  46249. WOLFSSL_CERT_MANAGER* cm = NULL;
  46250. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  46251. BAD_FUNC_ARG)
  46252. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  46253. WOLFSSL_SUCCESS)
  46254. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  46255. #ifndef NO_WOLFSSL_CLIENT
  46256. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46257. #elif !defined(NO_WOLFSSL_SERVER)
  46258. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46259. #else
  46260. return;
  46261. #endif
  46262. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  46263. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  46264. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  46265. wolfSSL_CTX_free(ctx);
  46266. #ifndef NO_WOLFSSL_CLIENT
  46267. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46268. #elif !defined(NO_WOLFSSL_SERVER)
  46269. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46270. #else
  46271. return;
  46272. #endif
  46273. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  46274. WOLFSSL_SUCCESS);
  46275. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  46276. wolfSSL_CTX_free(ctx);
  46277. #ifndef NO_WOLFSSL_CLIENT
  46278. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46279. #elif !defined(NO_WOLFSSL_SERVER)
  46280. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46281. #else
  46282. return;
  46283. #endif
  46284. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  46285. WOLFSSL_SUCCESS);
  46286. AssertNotNull(ssl = wolfSSL_new(ctx));
  46287. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  46288. wolfSSL_free(ssl);
  46289. wolfSSL_CTX_free(ctx);
  46290. AssertNotNull(cm = wolfSSL_CertManagerNew());
  46291. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  46292. WOLFSSL_SUCCESS);
  46293. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  46294. wolfSSL_CertManagerFree(cm);
  46295. #endif
  46296. return 0;
  46297. }
  46298. static int test_SetTmpEC_DHE_Sz(void)
  46299. {
  46300. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  46301. WOLFSSL_CTX *ctx;
  46302. WOLFSSL *ssl;
  46303. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  46304. AssertNotNull(ctx);
  46305. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  46306. ssl = wolfSSL_new(ctx);
  46307. AssertNotNull(ssl);
  46308. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  46309. wolfSSL_free(ssl);
  46310. wolfSSL_CTX_free(ctx);
  46311. #endif
  46312. return 0;
  46313. }
  46314. static int test_wolfSSL_CTX_get0_privatekey(void)
  46315. {
  46316. #ifdef OPENSSL_ALL
  46317. WOLFSSL_CTX* ctx = NULL;
  46318. printf(testingFmt, "wolfSSL_CTX_get0_privatekey()");
  46319. #ifndef NO_RSA
  46320. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  46321. AssertNull(SSL_CTX_get0_privatekey(ctx));
  46322. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  46323. WOLFSSL_FILETYPE_PEM));
  46324. AssertNull(SSL_CTX_get0_privatekey(ctx));
  46325. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  46326. WOLFSSL_FILETYPE_PEM));
  46327. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  46328. wolfSSL_CTX_free(ctx);
  46329. #endif
  46330. #ifdef HAVE_ECC
  46331. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  46332. AssertNull(SSL_CTX_get0_privatekey(ctx));
  46333. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  46334. WOLFSSL_FILETYPE_PEM));
  46335. AssertNull(SSL_CTX_get0_privatekey(ctx));
  46336. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  46337. WOLFSSL_FILETYPE_PEM));
  46338. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  46339. wolfSSL_CTX_free(ctx);
  46340. #endif
  46341. printf(resultFmt, passed);
  46342. #endif
  46343. return 0;
  46344. }
  46345. static int test_wolfSSL_dtls_set_mtu(void)
  46346. {
  46347. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  46348. defined(WOLFSSL_DTLS)
  46349. WOLFSSL_CTX* ctx = NULL;
  46350. WOLFSSL* ssl = NULL;
  46351. const char* testCertFile;
  46352. const char* testKeyFile;
  46353. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  46354. #ifndef NO_RSA
  46355. testCertFile = svrCertFile;
  46356. testKeyFile = svrKeyFile;
  46357. #elif defined(HAVE_ECC)
  46358. testCertFile = eccCertFile;
  46359. testKeyFile = eccKeyFile;
  46360. #endif
  46361. if (testCertFile != NULL && testKeyFile != NULL) {
  46362. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  46363. WOLFSSL_FILETYPE_PEM));
  46364. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  46365. WOLFSSL_FILETYPE_PEM));
  46366. }
  46367. AssertNotNull(ssl = wolfSSL_new(ctx));
  46368. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  46369. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  46370. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  46371. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  46372. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  46373. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  46374. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  46375. wolfSSL_free(ssl);
  46376. wolfSSL_CTX_free(ctx);
  46377. printf(testingFmt, "wolfSSL_dtls_set_mtu()");
  46378. printf(resultFmt, passed);
  46379. #endif
  46380. return 0;
  46381. }
  46382. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  46383. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  46384. !defined(WOLFSSL_NO_CLIENT_AUTH))
  46385. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  46386. {
  46387. int ret;
  46388. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  46389. printf("loading cert %s failed\n", certA);
  46390. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  46391. return -1;
  46392. }
  46393. return 0;
  46394. }
  46395. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  46396. {
  46397. int ret;
  46398. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  46399. != WOLFSSL_SUCCESS) {
  46400. printf("could not verify the cert: %s\n", certA);
  46401. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  46402. return -1;
  46403. } else {
  46404. printf("successfully verified: %s\n", certA);
  46405. }
  46406. return 0;
  46407. }
  46408. #define LOAD_ONE_CA(a, b, c, d) \
  46409. do { \
  46410. (a) = load_ca_into_cm(c, d); \
  46411. if ((a) != 0) \
  46412. return (b); \
  46413. else \
  46414. (b)--; \
  46415. } while(0)
  46416. #define VERIFY_ONE_CERT(a, b, c, d) \
  46417. do { \
  46418. (a) = verify_cert_with_cm(c, d); \
  46419. if ((a) != 0) \
  46420. return (b); \
  46421. else \
  46422. (b)--; \
  46423. } while(0)
  46424. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  46425. {
  46426. int ret;
  46427. int i = -1;
  46428. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  46429. char chainGArr[9][50] = {"certs/ca-cert.pem",
  46430. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  46431. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  46432. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  46433. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  46434. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  46435. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  46436. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  46437. "certs/test-pathlen/chainG-entity.pem"};
  46438. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  46439. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  46440. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  46441. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  46442. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  46443. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  46444. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  46445. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  46446. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  46447. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  46448. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  46449. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  46450. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  46451. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  46452. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  46453. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  46454. /* test validating the entity twice, should have no effect on pathLen since
  46455. * entity/leaf cert */
  46456. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  46457. return ret;
  46458. }
  46459. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  46460. {
  46461. int ret;
  46462. int i = -1;
  46463. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  46464. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  46465. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  46466. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  46467. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  46468. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  46469. */
  46470. char chainHArr[6][50] = {"certs/ca-cert.pem",
  46471. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  46472. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  46473. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  46474. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  46475. "certs/test-pathlen/chainH-entity.pem"};
  46476. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  46477. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  46478. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  46479. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  46480. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  46481. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  46482. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  46483. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  46484. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  46485. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  46486. return ret;
  46487. }
  46488. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  46489. {
  46490. int ret;
  46491. int i = -1;
  46492. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  46493. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  46494. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  46495. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  46496. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  46497. */
  46498. char chainIArr[5][50] = {"certs/ca-cert.pem",
  46499. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  46500. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  46501. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  46502. "certs/test-pathlen/chainI-entity.pem"};
  46503. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  46504. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  46505. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  46506. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  46507. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  46508. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  46509. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  46510. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  46511. return ret;
  46512. }
  46513. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  46514. {
  46515. int ret;
  46516. int i = -1;
  46517. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  46518. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  46519. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  46520. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  46521. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  46522. */
  46523. char chainJArr[6][50] = {"certs/ca-cert.pem",
  46524. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  46525. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  46526. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  46527. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  46528. "certs/test-pathlen/chainJ-entity.pem"};
  46529. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  46530. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  46531. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  46532. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  46533. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  46534. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  46535. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  46536. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  46537. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  46538. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  46539. return ret;
  46540. }
  46541. static int test_various_pathlen_chains(void)
  46542. {
  46543. int ret;
  46544. WOLFSSL_CERT_MANAGER* cm;
  46545. /* Test chain G (large chain with varying pathLens) */
  46546. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  46547. printf("cert manager new failed\n");
  46548. return -1;
  46549. }
  46550. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46551. AssertIntEQ(test_chainG(cm), -1);
  46552. #else
  46553. AssertIntEQ(test_chainG(cm), 0);
  46554. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  46555. ret = wolfSSL_CertManagerUnloadCAs(cm);
  46556. if (ret != WOLFSSL_SUCCESS)
  46557. return -1;
  46558. wolfSSL_CertManagerFree(cm);
  46559. /* end test chain G */
  46560. /* Test chain H (5 chain with same pathLens) */
  46561. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  46562. printf("cert manager new failed\n");
  46563. return -1;
  46564. }
  46565. AssertIntLT(test_chainH(cm), 0);
  46566. wolfSSL_CertManagerUnloadCAs(cm);
  46567. wolfSSL_CertManagerFree(cm);
  46568. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  46569. printf("cert manager new failed\n");
  46570. return -1;
  46571. }
  46572. ret = wolfSSL_CertManagerUnloadCAs(cm);
  46573. if (ret != WOLFSSL_SUCCESS)
  46574. return -1;
  46575. wolfSSL_CertManagerFree(cm);
  46576. /* end test chain H */
  46577. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  46578. * followed by 2 ICA's, should pass) */
  46579. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  46580. printf("cert manager new failed\n");
  46581. return -1;
  46582. }
  46583. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  46584. AssertIntEQ(test_chainI(cm), -1);
  46585. #else
  46586. AssertIntEQ(test_chainI(cm), 0);
  46587. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  46588. wolfSSL_CertManagerUnloadCAs(cm);
  46589. wolfSSL_CertManagerFree(cm);
  46590. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  46591. printf("cert manager new failed\n");
  46592. return -1;
  46593. }
  46594. ret = wolfSSL_CertManagerUnloadCAs(cm);
  46595. if (ret != WOLFSSL_SUCCESS)
  46596. return -1;
  46597. wolfSSL_CertManagerFree(cm);
  46598. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  46599. * this time followed by 3 ICA's, should fail */
  46600. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  46601. printf("cert manager new failed\n");
  46602. return -1;
  46603. }
  46604. AssertIntLT(test_chainJ(cm), 0);
  46605. wolfSSL_CertManagerUnloadCAs(cm);
  46606. wolfSSL_CertManagerFree(cm);
  46607. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  46608. printf("cert manager new failed\n");
  46609. return -1;
  46610. }
  46611. ret = wolfSSL_CertManagerUnloadCAs(cm);
  46612. wolfSSL_CertManagerFree(cm);
  46613. if (ret == WOLFSSL_SUCCESS) {
  46614. ret = 0;
  46615. }
  46616. return ret;
  46617. }
  46618. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  46619. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  46620. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  46621. {
  46622. byte ekm[100] = {0};
  46623. (void)ctx;
  46624. /* Succes Cases */
  46625. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  46626. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  46627. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  46628. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  46629. /* Use some random context */
  46630. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  46631. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  46632. /* Failure cases */
  46633. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  46634. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  46635. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  46636. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  46637. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  46638. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  46639. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  46640. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  46641. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  46642. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  46643. return 0;
  46644. }
  46645. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  46646. {
  46647. wolfSSL_KeepArrays(ssl);
  46648. }
  46649. static int test_export_keying_material(void)
  46650. {
  46651. #ifndef SINGLE_THREADED
  46652. tcp_ready ready;
  46653. callback_functions clientCb;
  46654. func_args client_args;
  46655. func_args server_args;
  46656. THREAD_TYPE serverThread;
  46657. XMEMSET(&client_args, 0, sizeof(func_args));
  46658. XMEMSET(&server_args, 0, sizeof(func_args));
  46659. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  46660. #ifdef WOLFSSL_TIRTOS
  46661. fdOpenSession(Task_self());
  46662. #endif
  46663. StartTCP();
  46664. InitTcpReady(&ready);
  46665. #if defined(USE_WINDOWS_API)
  46666. /* use RNG to get random port if using windows */
  46667. ready.port = GetRandomPort();
  46668. #endif
  46669. server_args.signal = &ready;
  46670. client_args.signal = &ready;
  46671. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  46672. client_args.callbacks = &clientCb;
  46673. start_thread(test_server_nofail, &server_args, &serverThread);
  46674. wait_tcp_ready(&server_args);
  46675. test_client_nofail(&client_args, test_export_keying_material_cb);
  46676. join_thread(serverThread);
  46677. AssertTrue(client_args.return_code);
  46678. AssertTrue(server_args.return_code);
  46679. FreeTcpReady(&ready);
  46680. #ifdef WOLFSSL_TIRTOS
  46681. fdOpenSession(Task_self());
  46682. #endif
  46683. #endif /* !SINGLE_THREADED */
  46684. return 0;
  46685. }
  46686. #endif /* HAVE_KEYING_MATERIAL */
  46687. static int test_wolfSSL_THREADID_hash(void)
  46688. {
  46689. int ret = 0;
  46690. unsigned long res;
  46691. #if defined(OPENSSL_EXTRA)
  46692. CRYPTO_THREADID id;
  46693. printf(testingFmt, "wolfSSL_THREADID_hash");
  46694. CRYPTO_THREADID_current(NULL);
  46695. AssertTrue(1);
  46696. res = CRYPTO_THREADID_hash(NULL);
  46697. AssertTrue( res == 0UL);
  46698. XMEMSET(&id, 0, sizeof(id));
  46699. res = CRYPTO_THREADID_hash(&id);
  46700. AssertTrue( res == 0UL);
  46701. printf(resultFmt, passed);
  46702. #endif /* OPENSSL_EXTRA */
  46703. (void)res;
  46704. return ret;
  46705. }
  46706. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  46707. {
  46708. int ret = 0;
  46709. WOLFSSL_CTX* ctx = NULL;
  46710. #if defined(OPENSSL_EXTRA)
  46711. printf(testingFmt, "SSL_CTX_set_ecdh_auto");
  46712. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  46713. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  46714. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  46715. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  46716. printf(resultFmt, passed);
  46717. #endif /* OPENSSL_EXTRA */
  46718. (void)ctx;
  46719. return ret;
  46720. }
  46721. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  46722. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  46723. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  46724. {
  46725. callback_functions* callbacks = NULL;
  46726. WOLFSSL_CTX* ctx = NULL;
  46727. WOLFSSL* ssl = NULL;
  46728. SOCKET_T sfd = 0;
  46729. SOCKET_T cfd = 0;
  46730. word16 port;
  46731. char msg[] = "I hear you fa shizzle!";
  46732. int len = (int) XSTRLEN(msg);
  46733. char input[1024];
  46734. int ret, err;
  46735. if (!args)
  46736. return 0;
  46737. ((func_args*)args)->return_code = TEST_FAIL;
  46738. callbacks = ((func_args*)args)->callbacks;
  46739. ctx = wolfSSL_CTX_new(callbacks->method());
  46740. #if defined(USE_WINDOWS_API)
  46741. port = ((func_args*)args)->signal->port;
  46742. #else
  46743. /* Let tcp_listen assign port */
  46744. port = 0;
  46745. #endif
  46746. #ifdef WOLFSSL_TIRTOS
  46747. fdOpenSession(Task_self());
  46748. #endif
  46749. AssertIntEQ(WOLFSSL_SUCCESS,
  46750. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  46751. AssertIntEQ(WOLFSSL_SUCCESS,
  46752. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  46753. WOLFSSL_FILETYPE_PEM));
  46754. AssertIntEQ(WOLFSSL_SUCCESS,
  46755. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  46756. WOLFSSL_FILETYPE_PEM));
  46757. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  46758. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  46759. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  46760. #elif !defined(NO_DH)
  46761. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  46762. #endif
  46763. if (callbacks->ctx_ready)
  46764. callbacks->ctx_ready(ctx);
  46765. ssl = wolfSSL_new(ctx);
  46766. AssertNotNull(ssl);
  46767. /* listen and accept */
  46768. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  46769. CloseSocket(sfd);
  46770. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  46771. if (callbacks->ssl_ready)
  46772. callbacks->ssl_ready(ssl);
  46773. do {
  46774. err = 0; /* Reset error */
  46775. ret = wolfSSL_accept(ssl);
  46776. if (ret != WOLFSSL_SUCCESS) {
  46777. err = wolfSSL_get_error(ssl, 0);
  46778. }
  46779. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  46780. if (ret != WOLFSSL_SUCCESS) {
  46781. wolfSSL_free(ssl);
  46782. wolfSSL_CTX_free(ctx);
  46783. CloseSocket(cfd);
  46784. ((func_args*)args)->return_code = TEST_FAIL;
  46785. return 0;
  46786. }
  46787. /* read and write data */
  46788. XMEMSET( input, 0, sizeof(input));
  46789. while (1) {
  46790. ret = wolfSSL_read(ssl, input, sizeof(input));
  46791. if (ret > 0) {
  46792. break;
  46793. }
  46794. else {
  46795. err = wolfSSL_get_error(ssl,ret);
  46796. if (err == WOLFSSL_ERROR_WANT_READ) {
  46797. continue;
  46798. }
  46799. break;
  46800. }
  46801. }
  46802. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  46803. do {
  46804. ret = wolfSSL_write(ssl, msg, len);
  46805. if (ret > 0) {
  46806. break;
  46807. }
  46808. } while (ret < 0);
  46809. }
  46810. /* bidirectional shutdown */
  46811. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  46812. continue;
  46813. }
  46814. /* wait for the peer to disconnect the tcp connection */
  46815. do {
  46816. ret = wolfSSL_read(ssl, input, sizeof(input));
  46817. err = wolfSSL_get_error(ssl, ret);
  46818. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  46819. /* detect TCP disconnect */
  46820. AssertIntLE(ret,WOLFSSL_FAILURE);
  46821. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  46822. ((func_args*)args)->return_code = TEST_SUCCESS;
  46823. wolfSSL_free(ssl);
  46824. wolfSSL_CTX_free(ctx);
  46825. CloseSocket(cfd);
  46826. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  46827. wc_ecc_fp_free(); /* free per thread cache */
  46828. #endif
  46829. return 0;
  46830. }
  46831. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  46832. {
  46833. callback_functions* callbacks = NULL;
  46834. WOLFSSL_CTX* ctx = NULL;
  46835. WOLFSSL* ssl = NULL;
  46836. SOCKET_T sfd = 0;
  46837. char msg[] = "hello wolfssl server!";
  46838. int len = (int) XSTRLEN(msg);
  46839. char input[1024];
  46840. int idx;
  46841. int ret, err;
  46842. if (!args)
  46843. return 0;
  46844. ((func_args*)args)->return_code = TEST_FAIL;
  46845. callbacks = ((func_args*)args)->callbacks;
  46846. ctx = wolfSSL_CTX_new(callbacks->method());
  46847. #ifdef WOLFSSL_TIRTOS
  46848. fdOpenSession(Task_self());
  46849. #endif
  46850. AssertIntEQ(WOLFSSL_SUCCESS,
  46851. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  46852. AssertIntEQ(WOLFSSL_SUCCESS,
  46853. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  46854. WOLFSSL_FILETYPE_PEM));
  46855. AssertIntEQ(WOLFSSL_SUCCESS,
  46856. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  46857. WOLFSSL_FILETYPE_PEM));
  46858. AssertNotNull((ssl = wolfSSL_new(ctx)));
  46859. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  46860. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  46861. do {
  46862. err = 0; /* Reset error */
  46863. ret = wolfSSL_connect(ssl);
  46864. if (ret != WOLFSSL_SUCCESS) {
  46865. err = wolfSSL_get_error(ssl, 0);
  46866. }
  46867. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  46868. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  46869. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  46870. input[idx] = 0;
  46871. }
  46872. ret = wolfSSL_shutdown(ssl);
  46873. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  46874. ret = wolfSSL_shutdown(ssl);
  46875. }
  46876. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  46877. ((func_args*)args)->return_code = TEST_SUCCESS;
  46878. wolfSSL_free(ssl);
  46879. wolfSSL_CTX_free(ctx);
  46880. CloseSocket(sfd);
  46881. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  46882. wc_ecc_fp_free(); /* free per thread cache */
  46883. #endif
  46884. return 0;
  46885. }
  46886. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  46887. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  46888. /* This test is to check wolfSSL_read behaves as same as
  46889. * openSSL when it is called after SSL_shutdown completes.
  46890. */
  46891. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  46892. {
  46893. int ret = 0;
  46894. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  46895. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  46896. tcp_ready ready;
  46897. func_args client_args;
  46898. func_args server_args;
  46899. THREAD_TYPE serverThread;
  46900. THREAD_TYPE clientThread;
  46901. callback_functions server_cbf;
  46902. callback_functions client_cbf;
  46903. printf(testingFmt, "wolfSSL_read_detect_TCP_disconnect()");
  46904. #ifdef WOLFSSL_TIRTOS
  46905. fdOpenSession(Task_self());
  46906. #endif
  46907. StartTCP();
  46908. InitTcpReady(&ready);
  46909. #if defined(USE_WINDOWS_API)
  46910. /* use RNG to get random port if using windows */
  46911. ready.port = GetRandomPort();
  46912. #endif
  46913. XMEMSET(&client_args, 0, sizeof(func_args));
  46914. XMEMSET(&server_args, 0, sizeof(func_args));
  46915. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  46916. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  46917. server_cbf.method = wolfTLSv1_2_server_method;
  46918. client_cbf.method = wolfTLSv1_2_client_method;
  46919. server_args.callbacks = &server_cbf;
  46920. client_args.callbacks = &client_cbf;
  46921. server_args.signal = &ready;
  46922. client_args.signal = &ready;
  46923. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  46924. wait_tcp_ready(&server_args);
  46925. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  46926. join_thread(clientThread);
  46927. join_thread(serverThread);
  46928. AssertTrue(client_args.return_code);
  46929. AssertTrue(server_args.return_code);
  46930. FreeTcpReady(&ready);
  46931. printf(resultFmt, passed);
  46932. #endif
  46933. return ret;
  46934. }
  46935. static int test_wolfSSL_CTX_get_min_proto_version(void)
  46936. {
  46937. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  46938. WOLFSSL_CTX *ctx;
  46939. (void)ctx;
  46940. printf(testingFmt, "wolfSSL_CTX_get_min_proto_version()");
  46941. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  46942. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  46943. #ifdef WOLFSSL_ALLOW_SSLV3
  46944. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  46945. #else
  46946. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  46947. #endif
  46948. wolfSSL_CTX_free(ctx);
  46949. #ifdef WOLFSSL_ALLOW_TLSV10
  46950. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  46951. #else
  46952. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  46953. #endif
  46954. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  46955. #ifdef WOLFSSL_ALLOW_TLSV10
  46956. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  46957. #else
  46958. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  46959. #endif
  46960. wolfSSL_CTX_free(ctx);
  46961. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  46962. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  46963. #ifndef NO_OLD_TLS
  46964. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  46965. #else
  46966. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  46967. #endif
  46968. wolfSSL_CTX_free(ctx);
  46969. #ifndef WOLFSSL_NO_TLS12
  46970. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  46971. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  46972. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  46973. wolfSSL_CTX_free(ctx);
  46974. #endif
  46975. #ifdef WOLFSSL_TLS13
  46976. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  46977. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  46978. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  46979. wolfSSL_CTX_free(ctx);
  46980. #endif
  46981. printf(resultFmt, passed);
  46982. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  46983. return 0;
  46984. }
  46985. static int test_wolfSSL_security_level(void)
  46986. {
  46987. #if defined(OPENSSL_EXTRA)
  46988. SSL_CTX *ctx;
  46989. printf(testingFmt, "test_wolfSSL_security_level()");
  46990. #ifdef WOLFSSL_TLS13
  46991. #ifdef NO_WOLFSSL_SERVER
  46992. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  46993. #else
  46994. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  46995. #endif
  46996. SSL_CTX_set_security_level(ctx, 1);
  46997. AssertTrue(1);
  46998. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  46999. SSL_CTX_free(ctx);
  47000. #else
  47001. (void)ctx;
  47002. #endif
  47003. printf(resultFmt, passed);
  47004. #endif
  47005. return 0;
  47006. }
  47007. static int test_wolfSSL_SSL_in_init(void)
  47008. {
  47009. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  47010. SSL_CTX* ctx;
  47011. SSL* ssl;
  47012. const char* testCertFile;
  47013. const char* testKeyFile;
  47014. printf(testingFmt, "test_wolfSSL_SSL_in_init()");
  47015. #ifdef WOLFSSL_TLS13
  47016. #ifdef NO_WOLFSSL_SERVER
  47017. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  47018. #else
  47019. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  47020. #endif
  47021. #ifndef NO_RSA
  47022. testCertFile = svrCertFile;
  47023. testKeyFile = svrKeyFile;
  47024. #elif defined(HAVE_ECC)
  47025. testCertFile = eccCertFile;
  47026. testKeyFile = eccKeyFile;
  47027. #else
  47028. testCertFile = NULL;
  47029. testKeyFile = NULL;
  47030. #endif
  47031. if (testCertFile != NULL && testKeyFile != NULL) {
  47032. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  47033. SSL_FILETYPE_PEM));
  47034. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  47035. SSL_FILETYPE_PEM));
  47036. }
  47037. ssl = SSL_new(ctx);
  47038. AssertNotNull(ssl);
  47039. AssertIntEQ(SSL_in_init(ssl), 1);
  47040. SSL_CTX_free(ctx);
  47041. SSL_free(ssl);
  47042. #else
  47043. (void)ctx;
  47044. (void)ssl;
  47045. (void)testCertFile;
  47046. (void)testKeyFile;
  47047. #endif
  47048. printf(resultFmt, passed);
  47049. #endif
  47050. return 0;
  47051. }
  47052. static int test_wolfSSL_EC_curve(void)
  47053. {
  47054. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47055. int nid = NID_secp160k1;
  47056. const char* nid_name;
  47057. printf(testingFmt, "test_wolfSSL_EC_curve()");
  47058. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  47059. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  47060. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  47061. printf(resultFmt, passed);
  47062. #endif
  47063. return 0;
  47064. }
  47065. static int test_wolfSSL_CTX_set_timeout(void)
  47066. {
  47067. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  47068. int timeout;
  47069. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  47070. (void)timeout;
  47071. printf(testingFmt, "test_wolfSSL_CTX_set_timeout()");
  47072. AssertNotNull(ctx);
  47073. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47074. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  47075. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  47076. */
  47077. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  47078. /* giving 0 as timeout value sets default timeout */
  47079. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  47080. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  47081. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  47082. #else
  47083. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  47084. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  47085. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  47086. #endif
  47087. wolfSSL_CTX_free(ctx);
  47088. printf(resultFmt, passed);
  47089. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  47090. return 0;
  47091. }
  47092. static int test_wolfSSL_OpenSSL_version(void)
  47093. {
  47094. #if defined(OPENSSL_EXTRA)
  47095. const char* ver;
  47096. printf(testingFmt, "test_wolfSSL_OpenSSL_version()");
  47097. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  47098. AssertNotNull(ver = OpenSSL_version(0));
  47099. #else
  47100. AssertNotNull(ver = OpenSSL_version());
  47101. #endif
  47102. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  47103. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  47104. printf(resultFmt, passed);
  47105. #endif
  47106. return 0;
  47107. }
  47108. static int test_CONF_CTX_CMDLINE(void)
  47109. {
  47110. #if defined(OPENSSL_ALL)
  47111. SSL_CTX* ctx = NULL;
  47112. SSL_CONF_CTX* cctx = NULL;
  47113. printf(testingFmt, "test_CONF_CTX_CMDLINE");
  47114. AssertNotNull(cctx = SSL_CONF_CTX_new());
  47115. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  47116. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  47117. AssertTrue(1);
  47118. /* set flags */
  47119. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  47120. WOLFSSL_CONF_FLAG_CMDLINE);
  47121. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  47122. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  47123. /* cmd invalid command */
  47124. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  47125. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  47126. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  47127. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  47128. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  47129. /* cmd Certificate and Private Key*/
  47130. {
  47131. #if !defined(NO_CERTS) && !defined(NO_RSA)
  47132. const char* ourCert = svrCertFile;
  47133. const char* ourKey = svrKeyFile;
  47134. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  47135. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  47136. WOLFSSL_SUCCESS);
  47137. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  47138. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  47139. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47140. #endif
  47141. }
  47142. /* cmd curves */
  47143. {
  47144. #if defined(HAVE_ECC)
  47145. const char* curve = "secp256r1";
  47146. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  47147. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  47148. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47149. #endif
  47150. }
  47151. /* cmd CipherString */
  47152. {
  47153. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  47154. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  47155. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  47156. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47157. }
  47158. /* cmd DH parameter */
  47159. {
  47160. #if !defined(NO_DH) && !defined(NO_BIO)
  47161. const char* ourdhcert = "./certs/dh2048.pem";
  47162. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  47163. -3);
  47164. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  47165. WOLFSSL_SUCCESS);
  47166. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47167. #endif
  47168. }
  47169. SSL_CTX_free(ctx);
  47170. SSL_CONF_CTX_free(cctx);
  47171. printf(resultFmt, passed);
  47172. #endif /* OPENSSL_EXTRA */
  47173. return 0;
  47174. }
  47175. static int test_CONF_CTX_FILE(void)
  47176. {
  47177. #if defined(OPENSSL_ALL)
  47178. SSL_CTX* ctx = NULL;
  47179. SSL_CONF_CTX* cctx = NULL;
  47180. printf(testingFmt, "test_CONF_CTX_FILE");
  47181. AssertNotNull(cctx = SSL_CONF_CTX_new());
  47182. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  47183. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  47184. AssertTrue(1);
  47185. /* set flags */
  47186. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  47187. WOLFSSL_CONF_FLAG_FILE);
  47188. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  47189. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  47190. /* sanity check */
  47191. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  47192. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  47193. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  47194. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  47195. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  47196. /* cmd Certificate and Private Key*/
  47197. {
  47198. #if !defined(NO_CERTS) && !defined(NO_RSA)
  47199. const char* ourCert = svrCertFile;
  47200. const char* ourKey = svrKeyFile;
  47201. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  47202. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  47203. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  47204. WOLFSSL_SUCCESS);
  47205. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  47206. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47207. #endif
  47208. }
  47209. /* cmd curves */
  47210. {
  47211. #if defined(HAVE_ECC)
  47212. const char* curve = "secp256r1";
  47213. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  47214. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  47215. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47216. #endif
  47217. }
  47218. /* cmd CipherString */
  47219. {
  47220. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  47221. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  47222. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  47223. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47224. }
  47225. /* cmd DH parameter */
  47226. {
  47227. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  47228. const char* ourdhcert = "./certs/dh3072.pem";
  47229. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  47230. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  47231. WOLFSSL_SUCCESS);
  47232. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47233. #endif
  47234. }
  47235. SSL_CTX_free(ctx);
  47236. SSL_CONF_CTX_free(cctx);
  47237. printf(resultFmt, passed);
  47238. #endif /* OPENSSL_EXTRA */
  47239. return 0;
  47240. }
  47241. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  47242. {
  47243. #ifdef HAVE_EX_DATA
  47244. int idx1,idx2;
  47245. printf(testingFmt, "test_wolfSSL_CRYPTO_get_ex_new_index()");
  47246. /* test for unsupported class index */
  47247. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  47248. 0,NULL, NULL, NULL, NULL ), -1);
  47249. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  47250. 0,NULL, NULL, NULL, NULL ), -1);
  47251. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  47252. 0,NULL, NULL, NULL, NULL ), -1);
  47253. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  47254. 0,NULL, NULL, NULL, NULL ), -1);
  47255. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  47256. 0,NULL, NULL, NULL, NULL ), -1);
  47257. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  47258. 0,NULL, NULL, NULL, NULL ), -1);
  47259. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  47260. 0,NULL, NULL, NULL, NULL ), -1);
  47261. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  47262. 0,NULL, NULL, NULL, NULL ), -1);
  47263. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  47264. 0,NULL, NULL, NULL, NULL ), -1);
  47265. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  47266. 0,NULL, NULL, NULL, NULL ), -1);
  47267. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  47268. 0,NULL, NULL, NULL, NULL ), -1);
  47269. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  47270. 0,NULL, NULL, NULL, NULL ), -1);
  47271. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  47272. /* test for supported class index */
  47273. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  47274. 0,NULL, NULL, NULL, NULL );
  47275. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  47276. 0,NULL, NULL, NULL, NULL );
  47277. AssertIntNE(idx1, -1);
  47278. AssertIntNE(idx2, -1);
  47279. AssertIntNE(idx1, idx2);
  47280. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  47281. 0,NULL, NULL, NULL, NULL );
  47282. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  47283. 0,NULL, NULL, NULL, NULL );
  47284. AssertIntNE(idx1, -1);
  47285. AssertIntNE(idx2, -1);
  47286. AssertIntNE(idx1, idx2);
  47287. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  47288. 0,NULL, NULL, NULL, NULL );
  47289. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  47290. 0,NULL, NULL, NULL, NULL );
  47291. AssertIntNE(idx1, -1);
  47292. AssertIntNE(idx2, -1);
  47293. AssertIntNE(idx1, idx2);
  47294. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  47295. 0,NULL, NULL, NULL, NULL );
  47296. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  47297. 0,NULL, NULL, NULL, NULL );
  47298. AssertIntNE(idx1, -1);
  47299. AssertIntNE(idx2, -1);
  47300. AssertIntNE(idx1, idx2);
  47301. printf(resultFmt, "passed");
  47302. #endif /* HAVE_EX_DATA */
  47303. return 0;
  47304. }
  47305. static int test_wolfSSL_set_psk_use_session_callback(void)
  47306. {
  47307. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  47308. SSL_CTX* ctx;
  47309. SSL* ssl;
  47310. const char* testCertFile;
  47311. const char* testKeyFile;
  47312. printf(testingFmt, "test_wolfSSL_set_psk_use_session_callback()");
  47313. #ifdef WOLFSSL_TLS13
  47314. #ifdef NO_WOLFSSL_SERVER
  47315. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  47316. #else
  47317. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  47318. #endif
  47319. #ifndef NO_RSA
  47320. testCertFile = svrCertFile;
  47321. testKeyFile = svrKeyFile;
  47322. #elif defined(HAVE_ECC)
  47323. testCertFile = eccCertFile;
  47324. testKeyFile = eccKeyFile;
  47325. #else
  47326. testCertFile = NULL;
  47327. testKeyFile = NULL;
  47328. #endif
  47329. if (testCertFile != NULL && testKeyFile != NULL) {
  47330. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  47331. SSL_FILETYPE_PEM));
  47332. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  47333. SSL_FILETYPE_PEM));
  47334. }
  47335. ssl = SSL_new(ctx);
  47336. AssertNotNull(ssl);
  47337. SSL_set_psk_use_session_callback(ssl,
  47338. my_psk_use_session_cb);
  47339. AssertTrue(1);
  47340. SSL_CTX_free(ctx);
  47341. SSL_free(ssl);
  47342. #else
  47343. (void)ctx;
  47344. (void)ssl;
  47345. (void)testCertFile;
  47346. (void)testKeyFile;
  47347. #endif
  47348. printf(resultFmt, passed);
  47349. #endif
  47350. return 0;
  47351. }
  47352. static int test_wolfSSL_DH(void)
  47353. {
  47354. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47355. DH *dh = NULL;
  47356. BIGNUM* p;
  47357. BIGNUM* q;
  47358. BIGNUM* g;
  47359. BIGNUM* pub;
  47360. BIGNUM* priv;
  47361. #if defined(OPENSSL_ALL) && defined(WOLFSSL_KEY_GEN)
  47362. #if !defined(HAVE_FIPS) || \
  47363. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  47364. FILE* f = NULL;
  47365. unsigned char buf[268];
  47366. const unsigned char* pt = buf;
  47367. long len = 0;
  47368. dh = NULL;
  47369. XMEMSET(buf, 0, sizeof(buf));
  47370. /* Test 2048 bit parameters */
  47371. f = XFOPEN("./certs/dh2048.der", "rb");
  47372. AssertTrue(f != XBADFILE);
  47373. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47374. XFCLOSE(f);
  47375. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  47376. AssertNotNull(dh->p);
  47377. AssertNotNull(dh->g);
  47378. AssertTrue(pt != buf);
  47379. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  47380. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47381. (const BIGNUM**)&q,
  47382. (const BIGNUM**) &g);
  47383. AssertPtrEq(p, dh->p);
  47384. AssertPtrEq(q, dh->q);
  47385. AssertPtrEq(g, dh->g);
  47386. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47387. AssertPtrEq(pub, dh->pub_key);
  47388. AssertPtrEq(priv, dh->priv_key);
  47389. AssertNotNull(pub = BN_new());
  47390. AssertNotNull(priv = BN_new());
  47391. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47392. AssertPtrEq(pub, dh->pub_key);
  47393. AssertPtrEq(priv, dh->priv_key);
  47394. DH_free(dh);
  47395. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  47396. DH_free(dh);
  47397. #endif
  47398. #endif
  47399. (void)dh;
  47400. (void)p;
  47401. (void)q;
  47402. (void)g;
  47403. (void)pub;
  47404. (void)priv;
  47405. printf(testingFmt, "test_wolfSSL_DH");
  47406. dh = wolfSSL_DH_new();
  47407. AssertNotNull(dh);
  47408. /* invalid parameters test */
  47409. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  47410. (const BIGNUM**)&q,
  47411. (const BIGNUM**)&g);
  47412. DH_get0_pqg(dh, NULL,
  47413. (const BIGNUM**)&q,
  47414. (const BIGNUM**)&g);
  47415. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  47416. DH_get0_pqg(dh, NULL, NULL, NULL);
  47417. AssertTrue(1);
  47418. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47419. (const BIGNUM**)&q,
  47420. (const BIGNUM**)&g);
  47421. AssertPtrEq(p, NULL);
  47422. AssertPtrEq(q, NULL);
  47423. AssertPtrEq(g, NULL);
  47424. DH_free(dh);
  47425. /* Test DH_up_ref() */
  47426. dh = wolfSSL_DH_new();
  47427. AssertNotNull(dh);
  47428. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  47429. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  47430. DH_free(dh); /* decrease ref count */
  47431. DH_free(dh); /* free WOLFSSL_DH */
  47432. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  47433. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  47434. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  47435. #ifdef HAVE_FFDHE_2048
  47436. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47437. DH_free(dh);
  47438. #endif
  47439. #ifdef HAVE_FFDHE_3072
  47440. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  47441. DH_free(dh);
  47442. #endif
  47443. #ifdef HAVE_FFDHE_4096
  47444. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  47445. DH_free(dh);
  47446. #endif
  47447. #else
  47448. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47449. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  47450. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  47451. printf(resultFmt, passed);
  47452. #endif /* OPENSSL_EXTRA && !NO_DH */
  47453. return 0;
  47454. }
  47455. static int test_wolfSSL_ERR_strings(void)
  47456. {
  47457. const char* err1 = "unsupported cipher suite";
  47458. const char* err2 = "wolfSSL PEM routines";
  47459. const char* err = NULL;
  47460. (void)err;
  47461. (void)err1;
  47462. (void)err2;
  47463. #if !defined(NO_ERROR_STRINGS)
  47464. printf(testingFmt, "test_wolfSSL_ERR_strings");
  47465. #if defined(OPENSSL_EXTRA)
  47466. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  47467. AssertTrue(err != NULL);
  47468. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  47469. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  47470. AssertTrue(err != NULL);
  47471. AssertIntEQ((*err == '\0'), 1);
  47472. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  47473. AssertTrue(err != NULL);
  47474. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  47475. #else
  47476. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  47477. AssertTrue(err != NULL);
  47478. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  47479. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  47480. AssertTrue(err != NULL);
  47481. AssertIntEQ((*err == '\0'), 1);
  47482. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  47483. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  47484. AssertTrue(err != NULL);
  47485. AssertIntEQ((*err == '\0'), 1);
  47486. #endif
  47487. printf(resultFmt, passed);
  47488. #endif
  47489. return 0;
  47490. }
  47491. static int test_wolfSSL_EVP_shake128(void)
  47492. {
  47493. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  47494. defined(WOLFSSL_SHAKE128)
  47495. printf(testingFmt, "test_wolfSSL_EVP_shake128");
  47496. const EVP_MD* md = NULL;
  47497. md = EVP_shake128();
  47498. AssertTrue(md != NULL);
  47499. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  47500. printf(resultFmt, passed);
  47501. #endif
  47502. return 0;
  47503. }
  47504. static int test_wolfSSL_EVP_shake256(void)
  47505. {
  47506. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  47507. defined(WOLFSSL_SHAKE256)
  47508. const EVP_MD* md = NULL;
  47509. printf(testingFmt, "test_wolfSSL_EVP_shake256");
  47510. md = EVP_shake256();
  47511. AssertTrue(md != NULL);
  47512. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  47513. printf(resultFmt, passed);
  47514. #endif
  47515. return 0;
  47516. }
  47517. static int test_EVP_blake2(void)
  47518. {
  47519. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  47520. const EVP_MD* md = NULL;
  47521. (void)md;
  47522. printf(testingFmt, "test_EVP_blake2");
  47523. #if defined(HAVE_BLAKE2)
  47524. md = EVP_blake2b512();
  47525. AssertTrue(md != NULL);
  47526. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  47527. #endif
  47528. #if defined(HAVE_BLAKE2S)
  47529. md = EVP_blake2s256();
  47530. AssertTrue(md != NULL);
  47531. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  47532. #endif
  47533. printf(resultFmt, passed);
  47534. #endif
  47535. return 0;
  47536. }
  47537. #if defined(OPENSSL_EXTRA)
  47538. static void list_md_fn(const EVP_MD* m, const char* from,
  47539. const char* to, void* arg)
  47540. {
  47541. const char* mn;
  47542. BIO *bio;
  47543. (void) from;
  47544. (void) to;
  47545. (void) arg;
  47546. (void) mn;
  47547. (void) bio;
  47548. if (!m) {
  47549. /* alias */
  47550. AssertNull(m);
  47551. AssertNotNull(to);
  47552. }
  47553. else {
  47554. AssertNotNull(m);
  47555. AssertNull(to);
  47556. }
  47557. AssertNotNull(from);
  47558. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  47559. mn = EVP_get_digestbyname(from);
  47560. /* print to stdout */
  47561. AssertNotNull(arg);
  47562. bio = BIO_new(BIO_s_file());
  47563. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  47564. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  47565. BIO_free(bio);
  47566. #endif
  47567. }
  47568. #endif
  47569. static int test_EVP_MD_do_all(void)
  47570. {
  47571. #if defined(OPENSSL_EXTRA)
  47572. printf(testingFmt, "test_EVP_MD_do_all");
  47573. EVP_MD_do_all(NULL, stdout);
  47574. /* to confirm previous call gives no harm */
  47575. AssertTrue(1);
  47576. EVP_MD_do_all(list_md_fn, stdout);
  47577. /* to confirm previous call gives no harm */
  47578. AssertTrue(1);
  47579. printf(resultFmt, passed);
  47580. #endif
  47581. return 0;
  47582. }
  47583. #if defined(OPENSSL_EXTRA)
  47584. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  47585. {
  47586. (void)arg;
  47587. (void)nm;
  47588. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  47589. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  47590. /* print to stdout */
  47591. AssertNotNull(arg);
  47592. bio = BIO_new(BIO_s_file());
  47593. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  47594. BIO_printf(bio, "%s\n", mn);
  47595. BIO_free(bio);
  47596. #endif
  47597. }
  47598. #endif
  47599. static int test_OBJ_NAME_do_all(void)
  47600. {
  47601. #if defined(OPENSSL_EXTRA)
  47602. printf(testingFmt, "test_OBJ_NAME_do_all");
  47603. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  47604. /* to confirm previous call gives no harm */
  47605. AssertTrue(1);
  47606. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stdout);
  47607. /* to confirm previous call gives no harm */
  47608. AssertTrue(1);
  47609. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stdout);
  47610. AssertTrue(1);
  47611. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stdout);
  47612. AssertTrue(1);
  47613. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stdout);
  47614. AssertTrue(1);
  47615. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stdout);
  47616. AssertTrue(1);
  47617. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stdout);
  47618. AssertTrue(1);
  47619. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stdout);
  47620. AssertTrue(1);
  47621. OBJ_NAME_do_all(-1, obj_name_t, stdout);
  47622. AssertTrue(1);
  47623. printf(resultFmt, passed);
  47624. #endif
  47625. return 0;
  47626. }
  47627. static int test_SSL_CIPHER_get_xxx(void)
  47628. {
  47629. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  47630. !defined(NO_FILESYSTEM)
  47631. const SSL_CIPHER* cipher = NULL;
  47632. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  47633. int i, numCiphers = 0;
  47634. SSL_CTX* ctx = NULL;
  47635. SSL* ssl = NULL;
  47636. const char* testCertFile;
  47637. const char* testKeyFile;
  47638. char buf[256] = {0};
  47639. const char* cipher_id = NULL;
  47640. int expect_nid1 = NID_undef;
  47641. int expect_nid2 = NID_undef;
  47642. int expect_nid3 = NID_undef;
  47643. int expect_nid4 = NID_undef;
  47644. int expect_nid5 = 0;
  47645. const char* cipher_id2 = NULL;
  47646. int expect_nid21 = NID_undef;
  47647. int expect_nid22 = NID_undef;
  47648. int expect_nid23 = NID_undef;
  47649. int expect_nid24 = NID_undef;
  47650. int expect_nid25 = 0;
  47651. (void)cipher;
  47652. (void)supportedCiphers;
  47653. (void)i;
  47654. (void)numCiphers;
  47655. (void)ctx;
  47656. (void)ssl;
  47657. (void)testCertFile;
  47658. (void)testKeyFile;
  47659. printf(testingFmt, "test_SSL_CIPHER_get_xxx");
  47660. #if defined(WOLFSSL_TLS13)
  47661. cipher_id = "TLS13-AES128-GCM-SHA256";
  47662. expect_nid1 = NID_auth_rsa;
  47663. expect_nid2 = NID_aes_128_gcm;
  47664. expect_nid3 = NID_sha256;
  47665. expect_nid4 = NID_kx_any;
  47666. expect_nid5 = 1;
  47667. #if !defined(WOLFSSL_NO_TLS12)
  47668. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  47669. expect_nid21 = NID_auth_rsa;
  47670. expect_nid22 = NID_aes_256_gcm;
  47671. expect_nid23 = NID_sha384;
  47672. expect_nid24 = NID_kx_ecdhe;
  47673. expect_nid25 = 1;
  47674. #endif
  47675. #endif
  47676. #ifdef NO_WOLFSSL_SERVER
  47677. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  47678. #else
  47679. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  47680. #endif
  47681. if (cipher_id) {
  47682. #ifndef NO_RSA
  47683. testCertFile = svrCertFile;
  47684. testKeyFile = svrKeyFile;
  47685. #elif defined(HAVE_ECC)
  47686. testCertFile = eccCertFile;
  47687. testKeyFile = eccKeyFile;
  47688. #else
  47689. testCertFile = NULL;
  47690. testKeyFile = NULL;
  47691. #endif
  47692. if (testCertFile != NULL && testKeyFile != NULL) {
  47693. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  47694. SSL_FILETYPE_PEM));
  47695. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  47696. SSL_FILETYPE_PEM));
  47697. }
  47698. ssl = SSL_new(ctx);
  47699. AssertNotNull(ssl);
  47700. AssertIntEQ(SSL_in_init(ssl), 1);
  47701. supportedCiphers = SSL_get_ciphers(ssl);
  47702. numCiphers = sk_num(supportedCiphers);
  47703. for (i = 0; i < numCiphers; ++i) {
  47704. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  47705. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  47706. }
  47707. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  47708. break;
  47709. }
  47710. }
  47711. /* test case for */
  47712. if (i != numCiphers) {
  47713. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  47714. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  47715. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  47716. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  47717. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  47718. }
  47719. if (cipher_id2) {
  47720. for (i = 0; i < numCiphers; ++i) {
  47721. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  47722. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  47723. }
  47724. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  47725. break;
  47726. }
  47727. }
  47728. /* test case for */
  47729. if (i != numCiphers) {
  47730. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  47731. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  47732. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  47733. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  47734. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  47735. }
  47736. }
  47737. }
  47738. if (ctx)
  47739. SSL_CTX_free(ctx);
  47740. if(ssl)
  47741. SSL_free(ssl);
  47742. printf(resultFmt, passed);
  47743. #endif
  47744. return 0;
  47745. }
  47746. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  47747. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  47748. int* keyFormat)
  47749. {
  47750. int ret;
  47751. byte* key_buf = NULL;
  47752. size_t key_sz = 0;
  47753. EncryptedInfo encInfo;
  47754. XMEMSET(&encInfo, 0, sizeof(encInfo));
  47755. ret = load_file(privKeyFile, &key_buf, &key_sz);
  47756. if (ret == 0) {
  47757. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  47758. NULL, &encInfo, keyFormat);
  47759. }
  47760. if (key_buf != NULL) {
  47761. free(key_buf); key_buf = NULL;
  47762. }
  47763. (void)encInfo; /* not used in this test */
  47764. #ifdef DEBUG_WOLFSSL
  47765. printf("%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  47766. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  47767. (*pDer)->length);
  47768. #endif
  47769. return ret;
  47770. }
  47771. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  47772. {
  47773. int ret = CRYPTOCB_UNAVAILABLE;
  47774. const char* privKeyFile = (const char*)ctx;
  47775. DerBuffer* pDer = NULL;
  47776. int keyFormat = 0;
  47777. if (info->algo_type == WC_ALGO_TYPE_PK) {
  47778. #ifdef DEBUG_WOLFSSL
  47779. printf("test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  47780. #endif
  47781. #ifndef NO_RSA
  47782. if (info->pk.type == WC_PK_TYPE_RSA) {
  47783. switch (info->pk.rsa.type) {
  47784. case RSA_PUBLIC_ENCRYPT:
  47785. case RSA_PUBLIC_DECRYPT:
  47786. /* perform software based RSA public op */
  47787. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  47788. break;
  47789. case RSA_PRIVATE_ENCRYPT:
  47790. case RSA_PRIVATE_DECRYPT:
  47791. {
  47792. RsaKey key;
  47793. /* perform software based RSA private op */
  47794. #ifdef DEBUG_WOLFSSL
  47795. printf("test_CryptoCb_Func: RSA Priv\n");
  47796. #endif
  47797. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  47798. &keyFormat);
  47799. if (ret != 0) {
  47800. return ret;
  47801. }
  47802. ret = wc_InitRsaKey(&key, HEAP_HINT);
  47803. if (ret == 0) {
  47804. word32 keyIdx = 0;
  47805. /* load RSA private key and perform private transform */
  47806. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  47807. &key, pDer->length);
  47808. if (ret == 0) {
  47809. ret = wc_RsaFunction(
  47810. info->pk.rsa.in, info->pk.rsa.inLen,
  47811. info->pk.rsa.out, info->pk.rsa.outLen,
  47812. info->pk.rsa.type, &key, info->pk.rsa.rng);
  47813. }
  47814. else {
  47815. /* if decode fails, then fall-back to software based crypto */
  47816. printf("test_CryptoCb_Func: RSA private key decode "
  47817. "failed %d, falling back to software\n", ret);
  47818. ret = CRYPTOCB_UNAVAILABLE;
  47819. }
  47820. wc_FreeRsaKey(&key);
  47821. }
  47822. wc_FreeDer(&pDer); pDer = NULL;
  47823. break;
  47824. }
  47825. }
  47826. #ifdef DEBUG_WOLFSSL
  47827. printf("test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  47828. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  47829. #endif
  47830. }
  47831. #endif /* !NO_RSA */
  47832. #ifdef HAVE_ECC
  47833. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  47834. /* mark this key as ephemeral */
  47835. if (info->pk.eckg.key != NULL) {
  47836. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  47837. sizeof(info->pk.eckg.key->label));
  47838. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  47839. }
  47840. }
  47841. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  47842. ecc_key key;
  47843. /* perform software based ECC sign */
  47844. #ifdef DEBUG_WOLFSSL
  47845. printf("test_CryptoCb_Func: ECC Sign\n");
  47846. #endif
  47847. if (info->pk.eccsign.key != NULL &&
  47848. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  47849. /* this is an empheral key */
  47850. #ifdef DEBUG_WOLFSSL
  47851. printf("test_CryptoCb_Func: skipping signing op on ephemeral key\n");
  47852. #endif
  47853. return CRYPTOCB_UNAVAILABLE;
  47854. }
  47855. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  47856. if (ret != 0) {
  47857. return ret;
  47858. }
  47859. ret = wc_ecc_init(&key);
  47860. if (ret == 0) {
  47861. word32 keyIdx = 0;
  47862. /* load ECC private key and perform private transform */
  47863. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  47864. &key, pDer->length);
  47865. if (ret == 0) {
  47866. ret = wc_ecc_sign_hash(
  47867. info->pk.eccsign.in, info->pk.eccsign.inlen,
  47868. info->pk.eccsign.out, info->pk.eccsign.outlen,
  47869. info->pk.eccsign.rng, &key);
  47870. }
  47871. else {
  47872. /* if decode fails, then fall-back to software based crypto */
  47873. printf("test_CryptoCb_Func: ECC private key decode "
  47874. "failed %d, falling back to software\n", ret);
  47875. ret = CRYPTOCB_UNAVAILABLE;
  47876. }
  47877. wc_ecc_free(&key);
  47878. }
  47879. wc_FreeDer(&pDer); pDer = NULL;
  47880. #ifdef DEBUG_WOLFSSL
  47881. printf("test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  47882. ret, *info->pk.eccsign.outlen);
  47883. #endif
  47884. }
  47885. #endif /* HAVE_ECC */
  47886. #ifdef HAVE_ED25519
  47887. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  47888. ed25519_key key;
  47889. /* perform software based ED25519 sign */
  47890. #ifdef DEBUG_WOLFSSL
  47891. printf("test_CryptoCb_Func: ED25519 Sign\n");
  47892. #endif
  47893. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  47894. if (ret != 0) {
  47895. return ret;
  47896. }
  47897. ret = wc_ed25519_init(&key);
  47898. if (ret == 0) {
  47899. word32 keyIdx = 0;
  47900. /* load ED25519 private key and perform private transform */
  47901. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  47902. &key, pDer->length);
  47903. if (ret == 0) {
  47904. /* calculate public key */
  47905. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  47906. if (ret == 0) {
  47907. key.pubKeySet = 1;
  47908. ret = wc_ed25519_sign_msg_ex(
  47909. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  47910. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  47911. &key, info->pk.ed25519sign.type,
  47912. info->pk.ed25519sign.context,
  47913. info->pk.ed25519sign.contextLen);
  47914. }
  47915. }
  47916. else {
  47917. /* if decode fails, then fall-back to software based crypto */
  47918. printf("test_CryptoCb_Func: ED25519 private key decode "
  47919. "failed %d, falling back to software\n", ret);
  47920. ret = CRYPTOCB_UNAVAILABLE;
  47921. }
  47922. wc_ed25519_free(&key);
  47923. }
  47924. wc_FreeDer(&pDer); pDer = NULL;
  47925. #ifdef DEBUG_WOLFSSL
  47926. printf("test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  47927. ret, *info->pk.ed25519sign.outLen);
  47928. #endif
  47929. }
  47930. #endif /* HAVE_ED25519 */
  47931. }
  47932. (void)thisDevId;
  47933. (void)keyFormat;
  47934. return ret;
  47935. }
  47936. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  47937. static void test_wc_CryptoCb_TLS(int tlsVer,
  47938. const char* cliCaPemFile, const char* cliCertPemFile,
  47939. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  47940. const char* svrCaPemFile, const char* svrCertPemFile,
  47941. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  47942. {
  47943. callback_functions client_cbf;
  47944. callback_functions server_cbf;
  47945. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  47946. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  47947. if (tlsVer == WOLFSSL_TLSV1_3) {
  47948. #ifdef WOLFSSL_TLS13
  47949. server_cbf.method = wolfTLSv1_3_server_method;
  47950. client_cbf.method = wolfTLSv1_3_client_method;
  47951. #endif
  47952. }
  47953. else if (tlsVer == WOLFSSL_TLSV1_2) {
  47954. #ifndef WOLFSSL_NO_TLS12
  47955. server_cbf.method = wolfTLSv1_2_server_method;
  47956. client_cbf.method = wolfTLSv1_2_client_method;
  47957. #endif
  47958. }
  47959. else if (tlsVer == WOLFSSL_TLSV1_1) {
  47960. #ifndef NO_OLD_TLS
  47961. server_cbf.method = wolfTLSv1_1_server_method;
  47962. client_cbf.method = wolfTLSv1_1_client_method;
  47963. #endif
  47964. }
  47965. else if (tlsVer == WOLFSSL_TLSV1) {
  47966. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  47967. server_cbf.method = wolfTLSv1_server_method;
  47968. client_cbf.method = wolfTLSv1_client_method;
  47969. #endif
  47970. }
  47971. else if (tlsVer == WOLFSSL_SSLV3) {
  47972. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  47973. defined(WOLFSSL_STATIC_RSA)
  47974. server_cbf.method = wolfSSLv3_server_method;
  47975. client_cbf.method = wolfSSLv3_client_method;
  47976. #endif
  47977. }
  47978. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  47979. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  47980. server_cbf.method = wolfDTLSv1_2_server_method;
  47981. client_cbf.method = wolfDTLSv1_2_client_method;
  47982. #endif
  47983. }
  47984. else if (tlsVer == WOLFSSL_DTLSV1) {
  47985. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  47986. server_cbf.method = wolfDTLSv1_server_method;
  47987. client_cbf.method = wolfDTLSv1_client_method;
  47988. #endif
  47989. }
  47990. if (server_cbf.method == NULL) {
  47991. /* not enabled */
  47992. return;
  47993. }
  47994. /* Setup the keys for the TLS test */
  47995. client_cbf.certPemFile = cliCertPemFile;
  47996. client_cbf.keyPemFile = cliPubKeyPemFile;
  47997. client_cbf.caPemFile = cliCaPemFile;
  47998. server_cbf.certPemFile = svrCertPemFile;
  47999. server_cbf.keyPemFile = svrPubKeyPemFile;
  48000. server_cbf.caPemFile = svrCaPemFile;
  48001. /* Setup a crypto callback with pointer to private key file for testing */
  48002. client_cbf.devId = 1;
  48003. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  48004. (void*)cliPrivKeyPemFile);
  48005. server_cbf.devId = 2;
  48006. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  48007. (void*)svrPrivKeyPemFile);
  48008. /* Perform TLS server and client test */
  48009. /* First test is at WOLFSSL_CTX level */
  48010. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  48011. /* Check for success */
  48012. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  48013. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  48014. /* Second test is a WOLFSSL object level */
  48015. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  48016. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  48017. /* Check for success */
  48018. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  48019. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  48020. /* Un register the devId's */
  48021. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  48022. client_cbf.devId = INVALID_DEVID;
  48023. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  48024. server_cbf.devId = INVALID_DEVID;
  48025. }
  48026. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  48027. static int test_wc_CryptoCb(void)
  48028. {
  48029. #ifdef WOLF_CRYPTO_CB
  48030. /* TODO: Add crypto callback API tests */
  48031. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48032. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  48033. int tlsVer;
  48034. #endif
  48035. #ifndef NO_RSA
  48036. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  48037. test_wc_CryptoCb_TLS(tlsVer,
  48038. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  48039. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  48040. }
  48041. #endif
  48042. #ifdef HAVE_ECC
  48043. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  48044. test_wc_CryptoCb_TLS(tlsVer,
  48045. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  48046. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  48047. }
  48048. #endif
  48049. #ifdef HAVE_ED25519
  48050. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  48051. if (tlsVer == WOLFSSL_DTLSV1) continue;
  48052. test_wc_CryptoCb_TLS(tlsVer,
  48053. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  48054. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  48055. }
  48056. #endif
  48057. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  48058. #endif /* WOLF_CRYPTO_CB */
  48059. return 0;
  48060. }
  48061. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  48062. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  48063. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  48064. const char* cliCaPemFile, const char* cliCertPemFile,
  48065. const char* cliPrivKeyPemFile,
  48066. const char* svrCaPemFile, const char* svrCertPemFile,
  48067. const char* svrPrivKeyPemFile,
  48068. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  48069. {
  48070. callback_functions client_cbf;
  48071. callback_functions server_cbf;
  48072. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  48073. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  48074. if (tlsVer == WOLFSSL_TLSV1_3) {
  48075. #ifdef WOLFSSL_TLS13
  48076. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  48077. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  48078. #endif
  48079. }
  48080. else if (tlsVer == WOLFSSL_TLSV1_2) {
  48081. #ifndef WOLFSSL_NO_TLS12
  48082. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  48083. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  48084. #endif
  48085. }
  48086. else if (tlsVer == WOLFSSL_TLSV1_1) {
  48087. #ifndef NO_OLD_TLS
  48088. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  48089. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  48090. #endif
  48091. }
  48092. else if (tlsVer == WOLFSSL_TLSV1) {
  48093. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  48094. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  48095. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  48096. #endif
  48097. }
  48098. else if (tlsVer == WOLFSSL_SSLV3) {
  48099. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  48100. defined(WOLFSSL_STATIC_RSA)
  48101. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  48102. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  48103. #endif
  48104. }
  48105. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  48106. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  48107. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  48108. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  48109. #endif
  48110. }
  48111. else if (tlsVer == WOLFSSL_DTLSV1) {
  48112. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  48113. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  48114. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  48115. #endif
  48116. }
  48117. if (server_cbf.method_ex == NULL) {
  48118. /* not enabled */
  48119. return;
  48120. }
  48121. /* Setup the keys for the TLS test */
  48122. client_cbf.certPemFile = cliCertPemFile;
  48123. client_cbf.keyPemFile = cliPrivKeyPemFile;
  48124. client_cbf.caPemFile = cliCaPemFile;
  48125. server_cbf.certPemFile = svrCertPemFile;
  48126. server_cbf.keyPemFile = svrPrivKeyPemFile;
  48127. server_cbf.caPemFile = svrCaPemFile;
  48128. client_cbf.mem = cliMem;
  48129. client_cbf.memSz = cliMemSz;
  48130. server_cbf.mem = svrMem;
  48131. server_cbf.memSz = svrMemSz;
  48132. client_cbf.devId = INVALID_DEVID;
  48133. server_cbf.devId = INVALID_DEVID;
  48134. /* Perform TLS server and client test */
  48135. /* First test is at WOLFSSL_CTX level */
  48136. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  48137. /* Check for success */
  48138. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  48139. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  48140. /* Second test is a WOLFSSL object level */
  48141. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  48142. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  48143. /* Check for success */
  48144. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  48145. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  48146. }
  48147. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  48148. #ifdef WOLFSSL_STATIC_MEMORY
  48149. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  48150. defined(SESSION_CERTS)
  48151. #ifdef OPENSSL_EXTRA
  48152. #define TEST_TLS_STATIC_MEMSZ (400000)
  48153. #else
  48154. #define TEST_TLS_STATIC_MEMSZ (320000)
  48155. #endif
  48156. #else
  48157. #define TEST_TLS_STATIC_MEMSZ (80000)
  48158. #endif
  48159. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  48160. {
  48161. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  48162. WOLFSSL_MEM_STATS mem_stats;
  48163. WOLFSSL_MEM_CONN_STATS ssl_stats;
  48164. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  48165. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  48166. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  48167. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  48168. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  48169. #endif
  48170. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  48171. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  48172. /* this should fail because kMaxCtxClients == 2 */
  48173. AssertNull((ssl3 = wolfSSL_new(ctx)));
  48174. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  48175. #ifdef DEBUG_WOLFSSL
  48176. wolfSSL_PrintStatsConn(&ssl_stats);
  48177. #endif
  48178. (void)ssl_stats;
  48179. }
  48180. /* display collected statistics */
  48181. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  48182. #ifdef DEBUG_WOLFSSL
  48183. wolfSSL_PrintStats(&mem_stats);
  48184. #endif
  48185. (void)mem_stats;
  48186. }
  48187. wolfSSL_free(ssl1);
  48188. wolfSSL_free(ssl2);
  48189. return 0;
  48190. }
  48191. #endif /* WOLFSSL_STATIC_MEMORY */
  48192. static int test_wolfSSL_CTX_StaticMemory(void)
  48193. {
  48194. #ifdef WOLFSSL_STATIC_MEMORY
  48195. wolfSSL_method_func method_func;
  48196. WOLFSSL_CTX* ctx;
  48197. const int kMaxCtxClients = 2;
  48198. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48199. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  48200. int tlsVer;
  48201. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  48202. #endif
  48203. #endif
  48204. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  48205. printf(testingFmt, "test_wolfSSL_CTX_StaticMemory()");
  48206. #ifndef NO_WOLFSSL_SERVER
  48207. #ifndef WOLFSSL_NO_TLS12
  48208. method_func = wolfTLSv1_2_server_method_ex;
  48209. #else
  48210. method_func = wolfTLSv1_3_server_method_ex;
  48211. #endif
  48212. #else
  48213. #ifndef WOLFSSL_NO_TLS12
  48214. method_func = wolfTLSv1_2_client_method_ex;
  48215. #else
  48216. method_func = wolfTLSv1_3_client_method_ex;
  48217. #endif
  48218. #endif
  48219. /* Test creating CTX directly from static memory pool */
  48220. ctx = NULL;
  48221. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  48222. &ctx, method_func, svrMem, sizeof(svrMem),
  48223. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  48224. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  48225. wolfSSL_CTX_free(ctx);
  48226. ctx = NULL;
  48227. /* Test for heap allocated CTX, then assigning static pool to it */
  48228. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  48229. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  48230. NULL, svrMem, sizeof(svrMem),
  48231. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  48232. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  48233. wolfSSL_CTX_free(ctx);
  48234. /* TLS Level Tests using static memory */
  48235. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48236. #ifndef NO_RSA
  48237. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  48238. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  48239. svrCertFile, cliCertFile, cliKeyFile,
  48240. cliCertFile, svrCertFile, svrKeyFile,
  48241. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  48242. }
  48243. #endif
  48244. #ifdef HAVE_ECC
  48245. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  48246. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  48247. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  48248. cliEccCertFile, eccCertFile, eccKeyFile,
  48249. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  48250. }
  48251. #endif
  48252. #ifdef HAVE_ED25519
  48253. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  48254. if (tlsVer == WOLFSSL_DTLSV1) continue;
  48255. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  48256. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  48257. cliEdCertFile, edCertFile, edKeyFile,
  48258. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  48259. }
  48260. #endif
  48261. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  48262. printf(resultFmt, passed);
  48263. #endif
  48264. return 0;
  48265. }
  48266. static int test_openssl_FIPS_drbg(void)
  48267. {
  48268. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  48269. DRBG_CTX* dctx;
  48270. byte data1[32], data2[32], zeroData[32];
  48271. byte testSeed[16];
  48272. size_t dlen = sizeof(data1);
  48273. int i;
  48274. XMEMSET(data1, 0, dlen);
  48275. XMEMSET(data2, 0, dlen);
  48276. XMEMSET(zeroData, 0, sizeof(zeroData));
  48277. for (i=0; i<(int)sizeof(testSeed); i++) {
  48278. testSeed[i] = (byte)i;
  48279. }
  48280. printf(testingFmt, "test_openssl_FIPS_drbg()");
  48281. AssertNotNull(dctx = FIPS_get_default_drbg());
  48282. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  48283. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  48284. WOLFSSL_SUCCESS);
  48285. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  48286. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  48287. WOLFSSL_SUCCESS);
  48288. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  48289. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  48290. WOLFSSL_SUCCESS);
  48291. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  48292. WOLFSSL_SUCCESS);
  48293. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  48294. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  48295. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  48296. printf(resultFmt, passed);
  48297. #endif
  48298. return 0;
  48299. }
  48300. static int test_wolfSSL_FIPS_mode(void)
  48301. {
  48302. #if defined(OPENSSL_ALL)
  48303. printf(testingFmt, "test_wolfSSL_FIPS_mode()");
  48304. #ifdef HAVE_FIPS
  48305. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  48306. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  48307. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  48308. #else
  48309. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  48310. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  48311. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  48312. #endif
  48313. printf(resultFmt, passed);
  48314. #endif
  48315. return 0;
  48316. }
  48317. #ifdef WOLFSSL_DTLS
  48318. /* Prints out the current window */
  48319. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  48320. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  48321. int i;
  48322. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  48323. word32 b = w[i];
  48324. int j;
  48325. /* Prints out a 32 bit binary number in big endian order */
  48326. for (j = 0; j < 32; j++, b <<= 1) {
  48327. if (b & (((word32)1) << 31))
  48328. printf("1");
  48329. else
  48330. printf("0");
  48331. }
  48332. printf(" ");
  48333. }
  48334. printf("cur_hi %u cur_lo %u\n", h, l);
  48335. #else
  48336. (void)h;
  48337. (void)l;
  48338. (void)w;
  48339. #endif
  48340. }
  48341. /* a - cur_hi
  48342. * b - cur_lo
  48343. * c - next_hi
  48344. * d - next_lo
  48345. * e - window
  48346. * f - expected next_hi
  48347. * g - expected next_lo
  48348. * h - expected window[1]
  48349. * i - expected window[0]
  48350. */
  48351. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  48352. wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
  48353. DUW_TEST_print_window_binary((a), (b), (e)); \
  48354. AssertIntEQ((c), (f)); \
  48355. AssertIntEQ((d), (g)); \
  48356. AssertIntEQ((e)[1], (h)); \
  48357. AssertIntEQ((e)[0], (i)); \
  48358. } while (0)
  48359. static int test_wolfSSL_DtlsUpdateWindow(void)
  48360. {
  48361. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  48362. word32 next_lo = 0;
  48363. word16 next_hi = 0;
  48364. printf(testingFmt, "wolfSSL_DtlsUpdateWindow()");
  48365. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  48366. printf("\n");
  48367. #endif
  48368. XMEMSET(window, 0, sizeof window);
  48369. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  48370. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  48371. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  48372. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  48373. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  48374. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  48375. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  48376. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  48377. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  48378. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  48379. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  48380. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  48381. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  48382. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  48383. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  48384. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  48385. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  48386. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  48387. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  48388. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  48389. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  48390. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  48391. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  48392. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  48393. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  48394. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  48395. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  48396. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  48397. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  48398. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  48399. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  48400. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  48401. printf(resultFmt, passed);
  48402. fflush(stdout);
  48403. return 0;
  48404. }
  48405. #endif /* WOLFSSL_DTLS */
  48406. /*----------------------------------------------------------------------------*
  48407. | Main
  48408. *----------------------------------------------------------------------------*/
  48409. typedef int (*TEST_FUNC)(void);
  48410. typedef struct {
  48411. const char *name;
  48412. TEST_FUNC func;
  48413. } TEST_CASE;
  48414. #define TEST_DECL(func) { #func, func }
  48415. TEST_CASE testCases[] = {
  48416. TEST_DECL(test_fileAccess),
  48417. TEST_DECL(test_wolfSSL_Init),
  48418. TEST_DECL(test_wolfSSL_Method_Allocators),
  48419. #ifndef NO_WOLFSSL_SERVER
  48420. TEST_DECL(test_wolfSSL_CTX_new),
  48421. #endif
  48422. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  48423. (!defined(NO_RSA) || defined(HAVE_ECC))
  48424. TEST_DECL(test_for_double_Free),
  48425. #endif
  48426. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48427. TEST_DECL(test_wolfSSL_get_finished),
  48428. TEST_DECL(test_wolfSSL_CTX_add_session),
  48429. #endif
  48430. TEST_DECL(test_SSL_CIPHER_get_xxx),
  48431. TEST_DECL(test_wolfSSL_ERR_strings),
  48432. TEST_DECL(test_wolfSSL_EVP_shake128),
  48433. TEST_DECL(test_wolfSSL_EVP_shake256),
  48434. TEST_DECL(test_EVP_blake2),
  48435. TEST_DECL(test_EVP_MD_do_all),
  48436. TEST_DECL(test_OBJ_NAME_do_all),
  48437. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  48438. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  48439. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  48440. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  48441. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  48442. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  48443. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  48444. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  48445. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  48446. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  48447. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  48448. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  48449. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  48450. TEST_DECL(test_wolfSSL_FPKI),
  48451. TEST_DECL(test_wolfSSL_CertRsaPss),
  48452. TEST_DECL(test_wolfSSL_CertManagerCRL),
  48453. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  48454. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  48455. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  48456. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  48457. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  48458. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  48459. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  48460. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  48461. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  48462. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  48463. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  48464. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  48465. TEST_DECL(test_server_wolfSSL_new),
  48466. TEST_DECL(test_client_wolfSSL_new),
  48467. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  48468. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  48469. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  48470. TEST_DECL(test_SetTmpEC_DHE_Sz),
  48471. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  48472. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  48473. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  48474. defined(HAVE_IO_TESTS_DEPENDENCIES)
  48475. TEST_DECL(test_wolfSSL_read_write),
  48476. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  48477. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  48478. #endif
  48479. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
  48480. TEST_DECL(test_wolfSSL_dtls_export),
  48481. TEST_DECL(test_wolfSSL_tls_export),
  48482. #endif
  48483. TEST_DECL(test_wolfSSL_SetMinVersion),
  48484. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  48485. /* TLS extensions tests */
  48486. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48487. TEST_DECL(test_wolfSSL_UseSNI),
  48488. #endif
  48489. TEST_DECL(test_wolfSSL_UseTrustedCA),
  48490. TEST_DECL(test_wolfSSL_UseMaxFragment),
  48491. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  48492. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  48493. TEST_DECL(test_wolfSSL_UseALPN),
  48494. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  48495. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  48496. /* X509 tests */
  48497. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  48498. TEST_DECL(test_wolfSSL_PKCS12),
  48499. TEST_DECL(test_wolfSSL_no_password_cb),
  48500. TEST_DECL(test_wolfSSL_PKCS8),
  48501. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  48502. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  48503. TEST_DECL(test_wolfSSL_PKCS5),
  48504. TEST_DECL(test_wolfSSL_URI),
  48505. TEST_DECL(test_wolfSSL_TBS),
  48506. TEST_DECL(test_wolfSSL_X509_verify),
  48507. TEST_DECL(test_wolfSSL_X509_TLS_version),
  48508. TEST_DECL(test_wc_PemToDer),
  48509. TEST_DECL(test_wc_AllocDer),
  48510. TEST_DECL(test_wc_CertPemToDer),
  48511. TEST_DECL(test_wc_PubKeyPemToDer),
  48512. TEST_DECL(test_wc_PemPubKeyToDer),
  48513. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  48514. TEST_DECL(test_wc_CheckCertSigPubKey),
  48515. /* OCSP Stapling */
  48516. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  48517. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  48518. /* Multicast */
  48519. TEST_DECL(test_wolfSSL_mcast),
  48520. /* compatibility tests */
  48521. TEST_DECL(test_wolfSSL_lhash),
  48522. TEST_DECL(test_wolfSSL_X509_NAME),
  48523. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  48524. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  48525. #ifndef NO_BIO
  48526. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  48527. TEST_DECL(test_wolfSSL_X509_INFO),
  48528. #endif
  48529. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  48530. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  48531. TEST_DECL(test_wolfSSL_X509_check_host),
  48532. TEST_DECL(test_wolfSSL_X509_check_email),
  48533. TEST_DECL(test_wolfSSL_DES),
  48534. TEST_DECL(test_wolfSSL_certs),
  48535. TEST_DECL(test_wolfSSL_X509_check_private_key),
  48536. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  48537. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  48538. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  48539. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  48540. TEST_DECL(test_wolfSSL_private_keys),
  48541. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  48542. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  48543. #ifndef NO_BIO
  48544. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  48545. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  48546. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  48547. TEST_DECL(test_wolfSSL_PEM_RSAPrivateKey),
  48548. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  48549. #endif
  48550. TEST_DECL(test_DSA_do_sign_verify),
  48551. TEST_DECL(test_wolfSSL_tmp_dh),
  48552. TEST_DECL(test_wolfSSL_ctrl),
  48553. TEST_DECL(test_wolfSSL_EVP_MD_size),
  48554. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  48555. TEST_DECL(test_wolfSSL_EVP_Digest),
  48556. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  48557. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  48558. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  48559. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  48560. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  48561. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  48562. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  48563. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  48564. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  48565. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  48566. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  48567. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  48568. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  48569. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  48570. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  48571. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  48572. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  48573. #endif
  48574. #ifndef NO_BIO
  48575. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  48576. TEST_DECL(test_wolfSSL_GetLoggingCb),
  48577. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  48578. TEST_DECL(test_wc_ERR_remove_state),
  48579. TEST_DECL(test_wc_ERR_print_errors_fp),
  48580. #endif
  48581. TEST_DECL(test_wolfSSL_set_options),
  48582. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  48583. TEST_DECL(test_wolfSSL_set1_curves_list),
  48584. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  48585. TEST_DECL(test_wolfSSL_PKCS7_certs),
  48586. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  48587. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  48588. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  48589. TEST_DECL(test_wolfSSL_msgCb),
  48590. TEST_DECL(test_wolfSSL_either_side),
  48591. TEST_DECL(test_wolfSSL_DTLS_either_side),
  48592. TEST_DECL(test_generate_cookie),
  48593. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  48594. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  48595. TEST_DECL(test_wolfSSL_X509_Name_canon),
  48596. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  48597. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  48598. TEST_DECL(test_wolfSSL_X509_NID),
  48599. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  48600. TEST_DECL(test_wolfSSL_get0_param),
  48601. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  48602. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  48603. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  48604. TEST_DECL(test_wolfSSL_X509_STORE),
  48605. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  48606. TEST_DECL(test_X509_STORE_get0_objects),
  48607. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  48608. TEST_DECL(test_wolfSSL_BN),
  48609. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  48610. #ifndef NO_BIO
  48611. TEST_DECL(test_wolfSSL_PEM_read_bio),
  48612. TEST_DECL(test_wolfSSL_BIO),
  48613. #endif
  48614. TEST_DECL(test_wolfSSL_ASN1_STRING),
  48615. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  48616. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  48617. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  48618. TEST_DECL(test_wolfSSL_X509),
  48619. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  48620. TEST_DECL(test_wolfSSL_X509_sign),
  48621. TEST_DECL(test_wolfSSL_X509_sign2),
  48622. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  48623. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  48624. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  48625. TEST_DECL(test_wolfSSL_check_domain),
  48626. #endif
  48627. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  48628. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  48629. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  48630. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  48631. TEST_DECL(test_wolfSSL_RAND),
  48632. TEST_DECL(test_wolfSSL_BUF),
  48633. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  48634. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  48635. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  48636. TEST_DECL(test_wolfSSL_X509_cmp_time),
  48637. TEST_DECL(test_wolfSSL_X509_time_adj),
  48638. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  48639. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  48640. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  48641. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  48642. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  48643. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  48644. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  48645. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  48646. TEST_DECL(test_wolfSSL_Tls13_postauth),
  48647. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  48648. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  48649. TEST_DECL(test_wolfSSL_THREADID_hash),
  48650. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  48651. TEST_DECL(test_wolfSSL_RAND_bytes),
  48652. TEST_DECL(test_wolfSSL_BN_rand),
  48653. TEST_DECL(test_wolfSSL_pseudo_rand),
  48654. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  48655. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  48656. TEST_DECL(test_error_queue_per_thread),
  48657. TEST_DECL(test_wolfSSL_ERR_put_error),
  48658. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  48659. #ifndef NO_BIO
  48660. TEST_DECL(test_wolfSSL_ERR_print_errors),
  48661. #endif
  48662. TEST_DECL(test_wolfSSL_HMAC),
  48663. TEST_DECL(test_wolfSSL_CMAC),
  48664. TEST_DECL(test_wolfSSL_OBJ),
  48665. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  48666. TEST_DECL(test_wolfSSL_OBJ_cmp),
  48667. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  48668. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  48669. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  48670. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  48671. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  48672. TEST_DECL(test_wolfSSL_X509_set_name),
  48673. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  48674. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  48675. TEST_DECL(test_wolfSSL_X509_set_version),
  48676. #ifndef NO_BIO
  48677. TEST_DECL(test_wolfSSL_BIO_gets),
  48678. TEST_DECL(test_wolfSSL_BIO_puts),
  48679. TEST_DECL(test_wolfSSL_BIO_dump),
  48680. TEST_DECL(test_wolfSSL_BIO_should_retry),
  48681. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  48682. TEST_DECL(test_wolfSSL_BIO_write),
  48683. TEST_DECL(test_wolfSSL_BIO_connect),
  48684. TEST_DECL(test_wolfSSL_BIO_accept),
  48685. TEST_DECL(test_wolfSSL_BIO_printf),
  48686. TEST_DECL(test_wolfSSL_BIO_f_md),
  48687. TEST_DECL(test_wolfSSL_BIO_up_ref),
  48688. TEST_DECL(test_wolfSSL_BIO_tls),
  48689. #endif
  48690. TEST_DECL(test_wolfSSL_cert_cb),
  48691. TEST_DECL(test_wolfSSL_SESSION),
  48692. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  48693. TEST_DECL(test_wolfSSL_ticket_keys),
  48694. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  48695. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  48696. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  48697. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  48698. TEST_DECL(test_wolfSSL_MD4),
  48699. TEST_DECL(test_wolfSSL_verify_mode),
  48700. TEST_DECL(test_wolfSSL_verify_depth),
  48701. TEST_DECL(test_wolfSSL_HMAC_CTX),
  48702. TEST_DECL(test_wolfSSL_msg_callback),
  48703. TEST_DECL(test_wolfSSL_SHA),
  48704. TEST_DECL(test_wolfSSL_DH_1536_prime),
  48705. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  48706. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  48707. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  48708. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  48709. TEST_DECL(test_wolfSSL_MD5),
  48710. TEST_DECL(test_wolfSSL_MD5_Transform),
  48711. TEST_DECL(test_wolfSSL_SHA_Transform),
  48712. TEST_DECL(test_wolfSSL_SHA256),
  48713. TEST_DECL(test_wolfSSL_SHA256_Transform),
  48714. TEST_DECL(test_wolfSSL_SHA224),
  48715. TEST_DECL(test_wolfSSL_SHA512_Transform),
  48716. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  48717. TEST_DECL(test_wolfSSL_X509_CRL),
  48718. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  48719. TEST_DECL(test_wolfSSL_PEM_read_X509),
  48720. TEST_DECL(test_wolfSSL_PEM_read),
  48721. #ifndef NO_BIO
  48722. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  48723. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  48724. #endif
  48725. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  48726. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  48727. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  48728. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  48729. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  48730. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  48731. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  48732. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  48733. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  48734. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  48735. TEST_DECL(test_wolfSSL_X509_check_ca),
  48736. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  48737. TEST_DECL(test_wolfSSL_make_cert),
  48738. TEST_DECL(test_wolfSSL_DES_ncbc),
  48739. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  48740. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  48741. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  48742. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  48743. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  48744. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  48745. TEST_DECL(test_wolfSSL_PKEY_up_ref),
  48746. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  48747. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  48748. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  48749. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  48750. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  48751. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  48752. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  48753. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  48754. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  48755. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  48756. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  48757. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  48758. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  48759. TEST_DECL(test_CONF_modules_xxx),
  48760. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  48761. TEST_DECL(test_CRYPTO_THREADID_xxx),
  48762. TEST_DECL(test_ENGINE_cleanup),
  48763. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  48764. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  48765. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  48766. #ifdef OPENSSL_ALL
  48767. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  48768. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  48769. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  48770. TEST_DECL(test_wolfSSL_d2i_DHparams),
  48771. TEST_DECL(test_wolfSSL_i2d_DHparams),
  48772. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  48773. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  48774. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  48775. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  48776. TEST_DECL(test_wolfSSL_DSA_SIG),
  48777. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  48778. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  48779. TEST_DECL(test_wolfSSL_CTX_ctrl),
  48780. TEST_DECL(test_wolfSSL_DH_check),
  48781. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  48782. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  48783. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  48784. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  48785. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  48786. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  48787. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  48788. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  48789. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  48790. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  48791. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  48792. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
  48793. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  48794. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  48795. TEST_DECL(test_wolfSSL_EVP_rc4),
  48796. TEST_DECL(test_wolfSSL_EVP_enc_null),
  48797. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  48798. TEST_DECL(test_wolfSSL_EVP_mdc2),
  48799. TEST_DECL(test_wolfSSL_EVP_md4),
  48800. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  48801. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  48802. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  48803. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  48804. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  48805. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  48806. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  48807. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  48808. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  48809. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  48810. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  48811. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  48812. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  48813. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  48814. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  48815. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  48816. TEST_DECL(test_evp_cipher_aes_gcm),
  48817. TEST_DECL(test_wolfSSL_OBJ_ln),
  48818. TEST_DECL(test_wolfSSL_OBJ_sn),
  48819. TEST_DECL(test_wolfSSL_TXT_DB),
  48820. TEST_DECL(test_wolfSSL_NCONF),
  48821. #endif /* OPENSSL_ALL */
  48822. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  48823. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  48824. #ifndef NO_BIO
  48825. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  48826. #endif /* !NO_BIO */
  48827. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  48828. TEST_DECL(test_wolfSSL_X509_CA_num),
  48829. TEST_DECL(test_wolfSSL_X509_get_version),
  48830. #ifndef NO_BIO
  48831. TEST_DECL(test_wolfSSL_X509_print),
  48832. TEST_DECL(test_wolfSSL_BIO_get_len),
  48833. #endif
  48834. TEST_DECL(test_wolfSSL_RSA),
  48835. TEST_DECL(test_wolfSSL_RSA_DER),
  48836. TEST_DECL(test_wolfSSL_RSA_print),
  48837. #ifndef NO_RSA
  48838. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  48839. #endif
  48840. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  48841. TEST_DECL(test_wolfSSL_RSA_get0_key),
  48842. TEST_DECL(test_wolfSSL_RSA_meth),
  48843. TEST_DECL(test_wolfSSL_RSA_verify),
  48844. TEST_DECL(test_wolfSSL_RSA_sign),
  48845. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  48846. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  48847. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  48848. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  48849. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  48850. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  48851. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  48852. TEST_DECL(test_wolfSSL_RSA_ex_data),
  48853. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  48854. TEST_DECL(test_wolfSSL_RSA_To_Der),
  48855. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  48856. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  48857. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  48858. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  48859. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  48860. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  48861. TEST_DECL(test_wolfSSL_X509V3_EXT),
  48862. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  48863. TEST_DECL(test_wolfSSL_X509_get_ext),
  48864. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  48865. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  48866. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  48867. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  48868. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  48869. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  48870. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  48871. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  48872. TEST_DECL(test_wolfSSL_X509_cmp),
  48873. #ifndef NO_BIO
  48874. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  48875. #endif
  48876. TEST_DECL(test_wolfSSL_ASN1_get_object),
  48877. TEST_DECL(test_openssl_generate_key_and_cert),
  48878. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  48879. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  48880. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  48881. /* test the no op functions for compatibility */
  48882. TEST_DECL(test_no_op_functions),
  48883. /* OpenSSL EVP_PKEY API tests */
  48884. TEST_DECL(test_EVP_PKEY_rsa),
  48885. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  48886. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
  48887. TEST_DECL(test_EVP_PKEY_ec),
  48888. TEST_DECL(test_EVP_PKEY_cmp),
  48889. /* OpenSSL error API tests */
  48890. TEST_DECL(test_ERR_load_crypto_strings),
  48891. /* OpenSSL sk_X509 API test */
  48892. TEST_DECL(test_sk_X509),
  48893. /* OpenSSL sk_X509_CRL API test */
  48894. TEST_DECL(test_sk_X509_CRL),
  48895. /* OpenSSL X509 API test */
  48896. TEST_DECL(test_X509_get_signature_nid),
  48897. /* OpenSSL X509 REQ API test */
  48898. TEST_DECL(test_X509_REQ),
  48899. /* OpenSSL PKCS7 API test */
  48900. TEST_DECL(test_wolfssl_PKCS7),
  48901. TEST_DECL(test_wolfSSL_PKCS7_sign),
  48902. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  48903. #ifndef NO_BIO
  48904. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  48905. #ifdef HAVE_SMIME
  48906. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  48907. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  48908. #endif /* HAVE_SMIME */
  48909. #endif /* !NO_BIO */
  48910. /* wolfCrypt ASN tests */
  48911. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  48912. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  48913. TEST_DECL(test_wc_SetSubjectRaw),
  48914. TEST_DECL(test_wc_GetSubjectRaw),
  48915. TEST_DECL(test_wc_SetIssuerRaw),
  48916. TEST_DECL(test_wc_SetIssueBuffer),
  48917. TEST_DECL(test_wc_SetSubjectKeyId),
  48918. TEST_DECL(test_wc_SetSubject),
  48919. TEST_DECL(test_CheckCertSignature),
  48920. TEST_DECL(test_wc_ParseCert),
  48921. TEST_DECL(test_MakeCertWithPathLen),
  48922. /* wolfCrypt ECC tests */
  48923. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  48924. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  48925. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  48926. #ifdef WOLFSSL_TLS13
  48927. /* TLS v1.3 API tests */
  48928. TEST_DECL(test_tls13_apis),
  48929. #endif
  48930. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  48931. !defined(WOLFSSL_NO_CLIENT_AUTH))
  48932. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  48933. /* Bad certificate signature tests */
  48934. TEST_DECL(test_EccSigFailure_cm),
  48935. TEST_DECL(test_RsaSigFailure_cm),
  48936. #endif /* NO_CERTS */
  48937. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  48938. !defined(NO_OLD_TLS))
  48939. TEST_DECL(test_DhCallbacks),
  48940. #endif
  48941. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  48942. TEST_DECL(test_export_keying_material),
  48943. #endif
  48944. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  48945. TEST_DECL(test_wolfSSL_security_level),
  48946. TEST_DECL(test_wolfSSL_SSL_in_init),
  48947. TEST_DECL(test_wolfSSL_EC_curve),
  48948. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  48949. TEST_DECL(test_wolfSSL_OpenSSL_version),
  48950. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  48951. TEST_DECL(test_CONF_CTX_FILE),
  48952. TEST_DECL(test_CONF_CTX_CMDLINE),
  48953. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  48954. TEST_DECL(test_wolfSSL_DH),
  48955. /* wolfcrypt */
  48956. TEST_DECL(test_wolfCrypt_Init),
  48957. TEST_DECL(test_wc_InitMd5),
  48958. TEST_DECL(test_wc_Md5Update),
  48959. TEST_DECL(test_wc_Md5Final),
  48960. TEST_DECL(test_wc_InitSha),
  48961. TEST_DECL(test_wc_ShaUpdate),
  48962. TEST_DECL(test_wc_ShaFinal),
  48963. TEST_DECL(test_wc_InitSha256),
  48964. TEST_DECL(test_wc_Sha256Update),
  48965. TEST_DECL(test_wc_Sha256Final),
  48966. TEST_DECL(test_wc_Sha256FinalRaw),
  48967. TEST_DECL(test_wc_Sha256GetFlags),
  48968. TEST_DECL(test_wc_Sha256Free),
  48969. TEST_DECL(test_wc_Sha256GetHash),
  48970. TEST_DECL(test_wc_Sha256Copy),
  48971. TEST_DECL(test_wc_InitSha512),
  48972. TEST_DECL(test_wc_Sha512Update),
  48973. TEST_DECL(test_wc_Sha512Final),
  48974. TEST_DECL(test_wc_Sha512GetFlags),
  48975. TEST_DECL(test_wc_Sha512FinalRaw),
  48976. TEST_DECL(test_wc_Sha512Free),
  48977. TEST_DECL(test_wc_Sha512GetHash),
  48978. TEST_DECL(test_wc_Sha512Copy),
  48979. TEST_DECL(test_wc_InitSha512_224),
  48980. TEST_DECL(test_wc_Sha512_224Update),
  48981. TEST_DECL(test_wc_Sha512_224Final),
  48982. TEST_DECL(test_wc_Sha512_224GetFlags),
  48983. TEST_DECL(test_wc_Sha512_224FinalRaw),
  48984. TEST_DECL(test_wc_Sha512_224Free),
  48985. TEST_DECL(test_wc_Sha512_224GetHash),
  48986. TEST_DECL(test_wc_Sha512_224Copy),
  48987. TEST_DECL(test_wc_InitSha512_256),
  48988. TEST_DECL(test_wc_Sha512_256Update),
  48989. TEST_DECL(test_wc_Sha512_256Final),
  48990. TEST_DECL(test_wc_Sha512_256GetFlags),
  48991. TEST_DECL(test_wc_Sha512_256FinalRaw),
  48992. TEST_DECL(test_wc_Sha512_256Free),
  48993. TEST_DECL(test_wc_Sha512_256GetHash),
  48994. TEST_DECL(test_wc_Sha512_256Copy),
  48995. TEST_DECL(test_wc_InitSha384),
  48996. TEST_DECL(test_wc_Sha384Update),
  48997. TEST_DECL(test_wc_Sha384Final),
  48998. TEST_DECL(test_wc_Sha384GetFlags),
  48999. TEST_DECL(test_wc_Sha384FinalRaw),
  49000. TEST_DECL(test_wc_Sha384Free),
  49001. TEST_DECL(test_wc_Sha384GetHash),
  49002. TEST_DECL(test_wc_Sha384Copy),
  49003. TEST_DECL(test_wc_InitSha224),
  49004. TEST_DECL(test_wc_Sha224Update),
  49005. TEST_DECL(test_wc_Sha224Final),
  49006. TEST_DECL(test_wc_Sha224SetFlags),
  49007. TEST_DECL(test_wc_Sha224GetFlags),
  49008. TEST_DECL(test_wc_Sha224Free),
  49009. TEST_DECL(test_wc_Sha224GetHash),
  49010. TEST_DECL(test_wc_Sha224Copy),
  49011. TEST_DECL(test_wc_InitBlake2b),
  49012. TEST_DECL(test_wc_InitBlake2b_WithKey),
  49013. TEST_DECL(test_wc_InitBlake2s_WithKey),
  49014. TEST_DECL(test_wc_InitRipeMd),
  49015. TEST_DECL(test_wc_RipeMdUpdate),
  49016. TEST_DECL(test_wc_RipeMdFinal),
  49017. TEST_DECL(test_wc_InitSha3),
  49018. TEST_DECL(testing_wc_Sha3_Update),
  49019. TEST_DECL(test_wc_Sha3_224_Final),
  49020. TEST_DECL(test_wc_Sha3_256_Final),
  49021. TEST_DECL(test_wc_Sha3_384_Final),
  49022. TEST_DECL(test_wc_Sha3_512_Final),
  49023. TEST_DECL(test_wc_Sha3_224_Copy),
  49024. TEST_DECL(test_wc_Sha3_256_Copy),
  49025. TEST_DECL(test_wc_Sha3_384_Copy),
  49026. TEST_DECL(test_wc_Sha3_512_Copy),
  49027. TEST_DECL(test_wc_Sha3_GetFlags),
  49028. TEST_DECL(test_wc_InitShake256),
  49029. TEST_DECL(testing_wc_Shake256_Update),
  49030. TEST_DECL(test_wc_Shake256_Final),
  49031. TEST_DECL(test_wc_Shake256_Copy),
  49032. TEST_DECL(test_wc_Shake256Hash),
  49033. TEST_DECL(test_wc_Md5HmacSetKey),
  49034. TEST_DECL(test_wc_Md5HmacUpdate),
  49035. TEST_DECL(test_wc_Md5HmacFinal),
  49036. TEST_DECL(test_wc_ShaHmacSetKey),
  49037. TEST_DECL(test_wc_ShaHmacUpdate),
  49038. TEST_DECL(test_wc_ShaHmacFinal),
  49039. TEST_DECL(test_wc_Sha224HmacSetKey),
  49040. TEST_DECL(test_wc_Sha224HmacUpdate),
  49041. TEST_DECL(test_wc_Sha224HmacFinal),
  49042. TEST_DECL(test_wc_Sha256HmacSetKey),
  49043. TEST_DECL(test_wc_Sha256HmacUpdate),
  49044. TEST_DECL(test_wc_Sha256HmacFinal),
  49045. TEST_DECL(test_wc_Sha384HmacSetKey),
  49046. TEST_DECL(test_wc_Sha384HmacUpdate),
  49047. TEST_DECL(test_wc_Sha384HmacFinal),
  49048. TEST_DECL(test_wc_HashInit),
  49049. TEST_DECL(test_wc_HashSetFlags),
  49050. TEST_DECL(test_wc_HashGetFlags),
  49051. TEST_DECL(test_wc_InitCmac),
  49052. TEST_DECL(test_wc_CmacUpdate),
  49053. TEST_DECL(test_wc_CmacFinal),
  49054. TEST_DECL(test_wc_AesCmacGenerate),
  49055. TEST_DECL(test_wc_AesGcmStream),
  49056. TEST_DECL(test_wc_Des3_SetIV),
  49057. TEST_DECL(test_wc_Des3_SetKey),
  49058. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  49059. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  49060. TEST_DECL(test_wc_Des3_EcbEncrypt),
  49061. TEST_DECL(test_wc_Chacha_SetKey),
  49062. TEST_DECL(test_wc_Chacha_Process),
  49063. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  49064. TEST_DECL(test_wc_Poly1305SetKey),
  49065. TEST_DECL(test_wc_CamelliaSetKey),
  49066. TEST_DECL(test_wc_CamelliaSetIV),
  49067. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  49068. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  49069. TEST_DECL(test_wc_Arc4SetKey),
  49070. TEST_DECL(test_wc_Arc4Process),
  49071. TEST_DECL(test_wc_Rc2SetKey),
  49072. TEST_DECL(test_wc_Rc2SetIV),
  49073. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  49074. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  49075. TEST_DECL(test_wc_AesSetKey),
  49076. TEST_DECL(test_wc_AesSetIV),
  49077. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  49078. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  49079. TEST_DECL(test_wc_AesGcmSetKey),
  49080. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  49081. TEST_DECL(test_wc_GmacSetKey),
  49082. TEST_DECL(test_wc_GmacUpdate),
  49083. TEST_DECL(test_wc_InitRsaKey),
  49084. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  49085. TEST_DECL(test_wc_RsaPublicKeyDecode),
  49086. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  49087. TEST_DECL(test_wc_MakeRsaKey),
  49088. TEST_DECL(test_wc_SetKeyUsage),
  49089. TEST_DECL(test_wc_CheckProbablePrime),
  49090. TEST_DECL(test_wc_RsaPSS_Verify),
  49091. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  49092. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  49093. TEST_DECL(test_wc_SetMutexCb),
  49094. TEST_DECL(test_wc_LockMutex_ex),
  49095. TEST_DECL(test_wc_RsaKeyToDer),
  49096. TEST_DECL(test_wc_RsaKeyToPublicDer),
  49097. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  49098. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  49099. TEST_DECL(test_wc_RsaEncryptSize),
  49100. TEST_DECL(test_wc_RsaSSL_SignVerify),
  49101. TEST_DECL(test_wc_RsaFlattenPublicKey),
  49102. TEST_DECL(test_RsaDecryptBoundsCheck),
  49103. TEST_DECL(test_wc_AesCcmSetKey),
  49104. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  49105. TEST_DECL(test_wc_InitDsaKey),
  49106. TEST_DECL(test_wc_DsaSignVerify),
  49107. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  49108. TEST_DECL(test_wc_MakeDsaKey),
  49109. TEST_DECL(test_wc_DsaKeyToDer),
  49110. TEST_DECL(test_wc_DsaKeyToPublicDer),
  49111. TEST_DECL(test_wc_DsaImportParamsRaw),
  49112. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  49113. TEST_DECL(test_wc_DsaExportParamsRaw),
  49114. TEST_DECL(test_wc_DsaExportKeyRaw),
  49115. TEST_DECL(test_wc_SignatureGetSize_ecc),
  49116. TEST_DECL(test_wc_SignatureGetSize_rsa),
  49117. /*
  49118. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  49119. * avoid memory leaks.
  49120. */
  49121. TEST_DECL(test_wolfCrypt_Cleanup),
  49122. #ifdef OPENSSL_EXTRA
  49123. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  49124. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  49125. TEST_DECL(test_wolfSSL_EC),
  49126. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  49127. TEST_DECL(test_ECDSA_size_sign),
  49128. TEST_DECL(test_ED25519),
  49129. TEST_DECL(test_ED448),
  49130. TEST_DECL(test_EC_i2d),
  49131. #endif
  49132. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  49133. !defined(HAVE_SELFTEST) && \
  49134. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  49135. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  49136. #endif
  49137. #ifdef HAVE_HASHDRBG
  49138. #ifdef TEST_RESEED_INTERVAL
  49139. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  49140. #endif
  49141. TEST_DECL(test_wc_RNG_GenerateBlock),
  49142. #endif
  49143. TEST_DECL(test_get_rand_digit),
  49144. TEST_DECL(test_get_digit_count),
  49145. TEST_DECL(test_mp_cond_copy),
  49146. TEST_DECL(test_mp_rand),
  49147. TEST_DECL(test_get_digit),
  49148. TEST_DECL(test_wc_export_int),
  49149. TEST_DECL(test_wc_InitRngNonce),
  49150. TEST_DECL(test_wc_InitRngNonce_ex),
  49151. TEST_DECL(test_wc_ed25519_make_key),
  49152. TEST_DECL(test_wc_ed25519_init),
  49153. TEST_DECL(test_wc_ed25519_sign_msg),
  49154. TEST_DECL(test_wc_ed25519_import_public),
  49155. TEST_DECL(test_wc_ed25519_import_private_key),
  49156. TEST_DECL(test_wc_ed25519_export),
  49157. TEST_DECL(test_wc_ed25519_size),
  49158. TEST_DECL(test_wc_ed25519_exportKey),
  49159. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  49160. TEST_DECL(test_wc_curve25519_init),
  49161. TEST_DECL(test_wc_curve25519_size),
  49162. TEST_DECL(test_wc_curve25519_export_key_raw),
  49163. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  49164. TEST_DECL(test_wc_curve25519_make_key),
  49165. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  49166. TEST_DECL(test_wc_curve25519_make_pub),
  49167. TEST_DECL(test_wc_curve25519_export_public_ex),
  49168. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  49169. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  49170. TEST_DECL(test_wc_curve25519_import_private),
  49171. TEST_DECL(test_wc_ed448_make_key),
  49172. TEST_DECL(test_wc_ed448_init),
  49173. TEST_DECL(test_wc_ed448_sign_msg),
  49174. TEST_DECL(test_wc_ed448_import_public),
  49175. TEST_DECL(test_wc_ed448_import_private_key),
  49176. TEST_DECL(test_wc_ed448_export),
  49177. TEST_DECL(test_wc_ed448_size),
  49178. TEST_DECL(test_wc_ed448_exportKey),
  49179. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  49180. TEST_DECL(test_wc_curve448_make_key),
  49181. TEST_DECL(test_wc_curve448_shared_secret_ex),
  49182. TEST_DECL(test_wc_curve448_export_public_ex),
  49183. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  49184. TEST_DECL(test_wc_curve448_export_key_raw),
  49185. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  49186. TEST_DECL(test_wc_curve448_import_private),
  49187. TEST_DECL(test_wc_curve448_init),
  49188. TEST_DECL(test_wc_curve448_size),
  49189. TEST_DECL(test_wc_ecc_make_key),
  49190. TEST_DECL(test_wc_ecc_init),
  49191. TEST_DECL(test_wc_ecc_check_key),
  49192. TEST_DECL(test_wc_ecc_get_generator),
  49193. TEST_DECL(test_wc_ecc_size),
  49194. TEST_DECL(test_wc_ecc_params),
  49195. TEST_DECL(test_wc_ecc_signVerify_hash),
  49196. TEST_DECL(test_wc_ecc_shared_secret),
  49197. TEST_DECL(test_wc_ecc_export_x963),
  49198. TEST_DECL(test_wc_ecc_export_x963_ex),
  49199. TEST_DECL(test_wc_ecc_import_x963),
  49200. TEST_DECL(ecc_import_private_key),
  49201. TEST_DECL(test_wc_ecc_export_private_only),
  49202. TEST_DECL(test_wc_ecc_rs_to_sig),
  49203. TEST_DECL(test_wc_ecc_import_raw),
  49204. TEST_DECL(test_wc_ecc_import_unsigned),
  49205. TEST_DECL(test_wc_ecc_sig_size),
  49206. TEST_DECL(test_wc_ecc_ctx_new),
  49207. TEST_DECL(test_wc_ecc_ctx_reset),
  49208. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  49209. TEST_DECL(test_wc_ecc_ctx_set_info),
  49210. TEST_DECL(test_wc_ecc_encryptDecrypt),
  49211. TEST_DECL(test_wc_ecc_del_point),
  49212. TEST_DECL(test_wc_ecc_pointFns),
  49213. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  49214. TEST_DECL(test_wc_ecc_verify_hash_ex),
  49215. TEST_DECL(test_wc_ecc_mulmod),
  49216. TEST_DECL(test_wc_ecc_is_valid_idx),
  49217. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  49218. TEST_DECL(test_wc_ecc_sig_size_calc),
  49219. TEST_DECL(test_ToTraditional),
  49220. TEST_DECL(test_wc_EccPrivateKeyToDer),
  49221. TEST_DECL(test_wc_DhPublicKeyDecode),
  49222. TEST_DECL(test_wc_Ed25519KeyToDer),
  49223. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  49224. TEST_DECL(test_wc_Ed448KeyToDer),
  49225. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  49226. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  49227. TEST_DECL(test_wc_SetSubjectBuffer),
  49228. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  49229. TEST_DECL(test_wc_PKCS7_New),
  49230. TEST_DECL(test_wc_PKCS7_Init),
  49231. TEST_DECL(test_wc_PKCS7_InitWithCert),
  49232. TEST_DECL(test_wc_PKCS7_EncodeData),
  49233. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  49234. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  49235. TEST_DECL(test_wc_PKCS7_VerifySignedData),
  49236. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  49237. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  49238. TEST_DECL(test_wc_PKCS7_Degenerate),
  49239. TEST_DECL(test_wc_PKCS7_BER),
  49240. TEST_DECL(test_PKCS7_signed_enveloped),
  49241. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  49242. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  49243. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  49244. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  49245. TEST_DECL(test_wc_i2d_PKCS12),
  49246. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  49247. TEST_DECL(test_openssl_FIPS_drbg),
  49248. TEST_DECL(test_wc_CryptoCb),
  49249. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  49250. TEST_DECL(test_wolfSSL_FIPS_mode),
  49251. #ifdef WOLFSSL_DTLS
  49252. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  49253. #endif
  49254. TEST_DECL(test_ForceZero),
  49255. TEST_DECL(test_wolfSSL_Cleanup),
  49256. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  49257. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  49258. !defined(WOLFSSL_NO_CLIENT_AUTH))
  49259. TEST_DECL(test_various_pathlen_chains),
  49260. #endif
  49261. /* If at some point a stub get implemented this test should fail indicating
  49262. * a need to implement a new test case
  49263. */
  49264. TEST_DECL(test_stubs_are_stubs)
  49265. };
  49266. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  49267. static void TestSetup(void)
  49268. {
  49269. /* Stub, for now. Add common test setup code here. */
  49270. }
  49271. static void TestCleanup(void)
  49272. {
  49273. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  49274. /* Clear any errors added to the error queue during the test run. */
  49275. wolfSSL_ERR_clear_error();
  49276. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  49277. }
  49278. void ApiTest(void)
  49279. {
  49280. int i;
  49281. int ret;
  49282. printf(" Begin API Tests\n");
  49283. fflush(stdout);
  49284. for (i = 0; i < TEST_CASE_CNT; ++i) {
  49285. TestSetup();
  49286. ret = testCases[i].func();
  49287. if (ret != 0) {
  49288. fprintf(stderr, "%s failed.\n", testCases[i].name);
  49289. }
  49290. AssertIntEQ(ret, 0);
  49291. TestCleanup();
  49292. }
  49293. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  49294. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  49295. wc_ecc_fp_free(); /* free per thread cache */
  49296. #endif
  49297. wolfSSL_Cleanup();
  49298. (void)devId;
  49299. printf(" End API Tests\n");
  49300. fflush(stdout);
  49301. }